Import Upstream version 1.18
[cascardo/sendxmpp.git] / sendxmpp
1 #!/usr/bin/perl -w
2
3 eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5
6 #
7 # script to send message using xmpp (aka jabber),
8 # somewhat resembling mail(1)
9 #
10 # Author:     Dirk-Jan C. Binnema <djcb AT djcbsoftware.nl>
11 # Maintainer: Lubomir Host 'rajo' <rajo AT platon.sk>
12 # Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema
13 # Copyright (c) 2006 - 2009 Lubomir Host 'rajo'
14 #
15 # Homepage: http://sendxmpp.platon.sk
16 #
17 # Released under the terms of the GNU General Public License v2
18 #
19 # $Platon: sendxmpp/sendxmpp,v 1.18 2009-01-10 11:40:14 rajo Exp $
20 # $Id: $
21
22 use Authen::SASL qw(Perl); # authentication broken if Authen::SASL::Cyrus module installed
23 use Net::XMPP;
24 use Getopt::Long;
25 use strict;
26
27 use open ':utf8';
28 use open ':std';
29
30 # subroutines decls
31 sub xmpp_login($$$$$$$$);
32 sub xmpp_send ($$$$);
33 sub xmpp_send_raw_xml($$);
34 sub xmpp_send_message($$$$$$);
35 sub xmpp_send_chatroom_message($$$$$);
36 sub xmpp_logout($);
37 sub xmpp_check_result;
38 sub parse_cmdline();
39 sub error_exit;
40 sub debug_print;
41 sub read_config_file($);
42 sub push_hash($$);
43 sub terminate();
44 sub main();
45
46 my # MakeMaker
47 $VERSION        = [ q$Revision: 1.18 $ =~ m/(\S+)\s*$/g ]->[0];
48 my $RESOURCE = 'sendxmpp';
49 my $VERBOSE  = 0;
50 my $DEBUG    = 0;
51 # http://tools.ietf.org/html/rfc3921#section-2  section 2.1.1 - Types of Message
52 my @suppported_message_types    = qw( chat error groupchat headline );
53 my $message_type                                = 'chat'; # default message type
54
55 # start!
56 &main;
57
58 #
59 # main: main routine
60 #
61 sub main () {
62
63     my $cmdline = parse_cmdline();
64
65     $| = 1; # no output buffering
66
67     $DEBUG   = 1 if ($$cmdline{'debug'});
68     $VERBOSE = 1 if ($$cmdline{'verbose'});
69
70     my $config = read_config_file ($$cmdline{'file'})
71         unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
72
73     # login to xmpp
74     my $cnx =  xmpp_login ($$cmdline{'jserver'}  || $$config{'jserver'},
75                            $$cmdline{'port'}     || $$config{'port'},
76                            $$cmdline{'username'} || $$config{'username'},
77                            $$cmdline{'password'} || $$config{'password'},
78                            $$cmdline{'component'}|| $$config{'component'},
79                            $$cmdline{'resource'},
80                            $$cmdline{'tls'},
81                            $$cmdline{'debug'})
82       or error_exit("cannot login: $!");
83
84
85     # read message from STDIN or or from -m/--message parameter
86     if (!$$cmdline{interactive}) {
87
88         # the non-interactive case
89         my $txt;
90         my $message = $$cmdline{'message'};
91         if ($message) {
92             open (MSG, "<$message")
93               or error_exit ("cannot open message file '$message': $!");
94             while (<MSG>) { $txt .= $_ };
95             close(MSG);
96         }
97         else {
98             $txt .= $_ while (<STDIN>);
99         }
100
101         xmpp_send ($cnx,$cmdline,$config,$txt);
102
103     } else {
104         # the interactive case, read stdin line by line
105
106         # deal with TERM
107         $main::CNX = $cnx;
108         $SIG{INT}=\&terminate;
109
110         # line by line...
111         while (<STDIN>) {
112             chomp;
113             xmpp_send ($cnx,$cmdline,$config,$_);
114         }
115     }
116
117     xmpp_logout($cnx);
118     exit 0;
119 }
120
121
122
123 #
124 # read_config_file: read the configuration file
125 # input: filename
126 # output: hash with 'user', 'jserver' and 'password' keys
127 #
128 sub read_config_file ($) {
129
130     # check permissions
131     my $cfg_file = shift;
132     error_exit ("cannot read $cfg_file: $!")
133         unless (-r $cfg_file);
134     my $owner  = (stat _ )[4];
135     error_exit ("you must own $cfg_file")
136       unless ($owner == $>);
137     my $mode = (stat _ )[2] & 07777;
138     error_exit ("$cfg_file must not be accessible by others")
139       if ($mode & 0077);
140
141     open (CFG,"<$cfg_file")
142       or error_exit("cannot open $cfg_file for reading: $!");
143
144     my %config;
145     my $line = 0;
146         while (<CFG>) {
147
148                 ++$line;
149
150                 next if (/^\s*$/);     # ignore empty lines
151                 next if (/^\s*\#.*/);  # ignore comment lines
152
153                 #s/\#.*$//; # ignore comments in lines
154
155                 # Hugo van der Kooij <hvdkooij AT vanderkooij.org> has account with '#' as username
156                 if (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) {
157                         %config = (
158                                 'username'      => $1,
159                                 'jserver'       => $2,
160                                 'port'          => 0,
161                                 'password'      => $3,
162                                 'component'     => $4,
163                         );
164
165                 }
166                 else {
167                         close CFG;
168                         error_exit ("syntax error in line $line of $cfg_file");
169                 }
170
171                 # account with weird port number
172                 if ($config{'jserver'}  =~ /(.*):(\d+)/) {
173                         $config{'jserver'}      = $1;
174                         $config{'port'}         = $2;
175                 }
176
177                 # account with specific connection host
178                 if ($config{'jserver'}  =~ /(.*);([-\.\w]+)/) {
179                         $config{'jserver'}      = $2;
180                         $config{'username'}     .= "\@$1";
181                 }
182         }
183
184     close CFG;
185
186     error_exit ("no correct config found in $cfg_file")
187       unless (scalar(%config));
188
189     if ($DEBUG || $VERBOSE) {
190         while (my ($key,$val) = each %config) {
191             debug_print ("config: '$key' => '$val'");
192         }
193     }
194
195     return \%config;
196 }
197
198
199
200 #
201 # parse_cmdline: parse commandline options
202 # output: hash with commandline options
203 #
204 sub parse_cmdline () {
205
206     usage() unless (scalar(@ARGV));
207
208         my ($subject,$file,$resource,$jserver,$port,$username,$password,$component,
209         $message, $chatroom, $headline, $debug, $tls, $interactive, $help, $raw, $verbose);
210     my $res = GetOptions ('subject|s=s'    => \$subject,
211                           'file|f=s'       => \$file,
212                           'resource|r=s'   => \$resource,
213                           'jserver|j=s'    => \$jserver,
214                           'component|o=s'  => \$component,
215                           'username|u=s'   => \$username,
216                           'password|p=s'   => \$password,
217                           'message|m=s'    => \$message,
218                           'headline|l'     => \$headline,
219                           'message-type=s' => \$message_type,
220                           'chatroom|c'     => \$chatroom,
221                           'tls|t'          => \$tls,
222                           'interactive|i'  => \$interactive,
223                           'help|usage|h'   => \$help,
224                           'debug|d'        => \$debug,
225                           'raw|w'          => \$raw,
226                           'verbose|v'      => \$verbose);
227     usage () if ($help);
228
229         my @rcpt = @ARGV;
230
231         if (defined($raw) && scalar(@rcpt) > 0) {
232                 error_exit("You must give a recipient or --raw (but not both)");
233         }
234         if ($raw && $subject) {
235                 error_exit("You cannot specify a subject in raw XML mode");
236         }
237         if ($raw && $chatroom) {
238                 error_exit("The chatroom option is pointless in raw XML mode");
239         }
240
241         if ($message && $interactive) {
242                 error_exit("Cannot have both -m (--message) and -i (--interactive)");
243         }
244
245         if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
246                 error_exit("Unsupported message type '$message_type'");
247         }
248
249         if ($headline) {
250                 # --headline withouth --message-type
251                 if ($message_type eq 'message') {
252                         $message_type = 'headline'
253                 }
254                 else {
255                         error_exit("Options --headline and --message-type are mutually exclusive");
256                 }
257         }
258
259         if ($jserver && $jserver =~ /(.*):(\d+)/) {
260                 $jserver = $1;
261                 $port    = $2;
262         }
263
264     my %dict = ('subject'     => ($subject  or ''),
265                 'message'       => ($message or ''),
266                 'resource'    => ($resource or $RESOURCE),
267                 'jserver'     => ($jserver or ''),
268                 'component'   => ($component or ''),
269                 'port'        => ($port or 0),
270                 'username'    => ($username or ''),
271                 'password'    => ($password or ''),
272                 'chatroom'    => ($chatroom or 0),
273                 'message-type'    => $message_type,
274                 'interactive' => ($interactive or 0),
275                 'tls'         => ($tls or 0),
276                 'debug'       => ($debug or 0),
277                 'verbose'     => ($verbose or 0),
278                 'raw'         => ($raw or 0),
279                 'file'        => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
280                 'recipient'   => \@rcpt);
281
282    if ($DEBUG || $VERBOSE) {
283        while (my ($key,$val) = each %dict) {
284            debug_print ("cmdline: '$key' => '$val'");
285        }
286    }
287
288    return \%dict;
289 }
290
291
292 #
293 # xmpp_login: login to the xmpp (jabber) server
294 # input: hostname,port,username,password,resource,tls,debug
295 # output: an XMPP connection object
296 #
297 sub xmpp_login ($$$$$$$$) {
298
299     my ($host, $port, $user, $pw, $comp, $res, $tls, $debug) = @_;
300     my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
301     error_exit "could not create XMPP client object: $!"
302         unless ($cnx);
303
304     my @res;
305         my $arghash = {
306                 hostname                => $host,
307                 tls                             => $tls,
308                 connectiontype  => 'tcpip',
309                 componentname   => $comp
310         };
311
312         delete $arghash->{port} unless $port; 
313         if ($arghash->{port}) {
314                 @res = $cnx->Connect(%$arghash);
315                 error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
316         } else {
317                 @res = $cnx->Connect(%$arghash);
318                 error_exit ("Could not connect to server '$host': $@") unless @res;
319         }
320
321     xmpp_check_result("Connect",\@res,$cnx);
322
323         if ($comp) {
324                 my $sid = $cnx->{SESSION}->{id};
325                 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
326         }
327
328     @res = $cnx->AuthSend(#'hostname' => $host,
329                           'username' => $user,
330                           'password' => $pw,
331                           'resource' => $res);
332     xmpp_check_result('AuthSend',\@res,$cnx);
333
334     return $cnx;
335 }
336
337
338
339
340 #
341 # xmmp_send: send the message, determine from cmdline
342 # whether it's to individual or chatroom
343 #
344 sub xmpp_send ($$$$) {
345
346         my ($cnx, $cmdline, $config, $txt) = @_;
347
348         unless ($$cmdline{'chatroom'}) {
349         unless ($$cmdline{'raw'}) {
350                         map {
351                                 xmpp_send_message ($cnx,
352                                         $_, #$$cmdline{'recipient'},
353                                         $$cmdline{'component'} || $$config{'component'},
354                                         $$cmdline{'subject'},
355                                         $$cmdline{'message-type'},
356                                         $txt)
357                         } @{$$cmdline{'recipient'}};
358         }
359                 else {
360                         xmpp_send_raw_xml ($cnx, $txt);
361         }
362         }
363         else {
364                 map {
365                         xmpp_send_chatroom_message ($cnx,
366                                 $$cmdline{'resource'},
367                                 $$cmdline{'subject'},
368                                 $_, # $$cmdline{'recipient'},
369                                 $txt)
370                 } @{$$cmdline{'recipient'}};
371         }
372 }
373
374
375
376 #
377 # xmpp_send_raw_xml: send a raw XML packet
378 # input: connection,packet
379 #
380 sub xmpp_send_raw_xml ($$) {
381
382     my ($cnx,$packet) = @_;
383
384     # for some reason, Send does not return anything
385     $cnx->Send($packet);
386     xmpp_check_result('Send',0,$cnx);
387 }
388
389
390 #
391 # xmpp_send_message: send a message to some xmpp user
392 # input: connection,recipient,subject,msg
393 #
394 sub xmpp_send_message ($$$$$$) {
395
396     my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
397
398     # for some reason, MessageSend does not return anything
399     $cnx->MessageSend('to'      => $rcpt . ( $comp ? "\@$comp" : '' ),
400                 'type'          => $message_type,
401                 'subject'       => $subject,
402                 'body'          => $msg);
403
404     xmpp_check_result('MessageSend',0,$cnx);
405 }
406
407
408 #
409 # xmpp_send_chatroom_message: send a message to a chatroom
410 # input: connection,resource,subject,recipient,message
411 #
412 sub xmpp_send_chatroom_message ($$$$$) {
413
414     my ($cnx,$resource,$subject,$rcpt,$msg) =  @_;
415
416     # set the presence
417     my $pres = new Net::XMPP::Presence;
418     my $res = $pres->SetTo("$rcpt/$resource");
419
420     $cnx->Send($pres);
421
422     # create/send the message
423     my $groupmsg = new Net::XMPP::Message;
424     $groupmsg->SetMessage(to      => $rcpt,
425                           body    => $msg,
426                           type    => 'groupchat');
427
428     $res = $cnx->Send($groupmsg);
429     xmpp_check_result ('Send',$res,$cnx);
430
431     # leave the group
432     $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
433 }
434
435
436 #
437 # xmpp_logout: log out from the xmpp server
438 # input: connection
439 #
440 sub xmpp_logout($) {
441
442     # HACK
443     # messages may not be received if we log out too quickly...
444     sleep 1;
445
446     my $cnx = shift;
447     $cnx->Disconnect();
448     xmpp_check_result ('Disconnect',0); # well, nothing to check, really
449 }
450
451
452
453 #
454 # xmpp_check_result: check the return value from some xmpp function execution
455 # input: text, result, [connection]
456 #
457 sub xmpp_check_result
458 {
459     my ($txt, $res, $cnx)=@_;
460
461     error_exit ("Error '$txt': result undefined")
462         unless (defined $res);
463
464     # res may be 0
465         if ($res == 0) {
466                 debug_print "$txt";
467                 # result can be true or 'ok'
468         }
469         elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
470                 debug_print "$txt: " .  $$res[0];
471                 # otherwise, there is some error
472         }
473         else {
474                 my $errmsg = $cnx->GetErrorCode() || '?';
475                 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
476         }
477 }
478
479
480 #
481 # terminate; exit the program upon TERM sig reception
482 #
483 sub terminate () {
484     debug_print "caught TERM";
485     xmpp_logout($main::CNX);
486     exit 0;
487 }
488
489
490 #
491 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
492 # input: [array of strings]
493 #
494 sub debug_print {
495     print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
496         if (@_ && ($DEBUG ||$VERBOSE));
497 }
498
499
500 #
501 # error_exit: print error message and exit the program
502 #             logs out if there is a connection
503 # input: error, [connection]
504 #
505 sub error_exit {
506
507     my ($err,$cnx) = @_;
508     print STDERR "$err\n";
509     xmpp_logout ($cnx)
510         if ($cnx);
511
512     exit 1;
513 }
514
515
516 #
517 # usage: print short usage message and exit
518 #
519 sub usage () {
520
521     print STDERR
522         "sendxmpp version $VERSION\n" .
523         "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" .
524         "Copyright (c) 2006 - 2007 Lubomir Host 'rajo'\n" .
525         "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
526         "or refer to the the sendxmpp manpage\n";
527
528     exit 0;
529 }
530
531
532 #
533 # the fine manual
534 #
535 =pod
536
537 =head1 NAME
538
539 sendxmpp - send xmpp messages from the commandline.
540
541 =head1 SYNOPSIS
542
543 sendxmpp [options] <recipient1> [<recipient2> ...]
544
545 sendxmpp --raw [options]
546
547 =head1 DESCRIPTION
548
549 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
550 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
551
552 =head1 OPTIONS
553
554 =over
555
556 =item B<-f>,B<--file> I<file>
557
558 Use I<file> configuration file instead of F<~/.sendxmpprc>
559
560 =item B<-u>,B<--username> I<user>
561
562 Use I<user> instead of the one in the configuration file
563
564 =item B<-p>,B<--password> I<password>
565
566 Use I<password> instead of the one in the configuration file
567
568 =item B<-j>,B<--jserver> I<server>
569
570 Use jabber I<server> instead of the one in the configuration file.
571
572 =item B<-o>,B<--component> I<componentname>
573
574 Use componentname in connect call. Seems needed for Google talk.
575
576 =item B<-r>,B<--resource> I<res>
577
578 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
579
580 =item B<-t>,B<--tls>
581
582 Connect securely, using TLS
583
584 =item B<-l>,B<--headline>
585
586 Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages)
587
588 =item B<--messages-type>
589
590 Set type of message. Supported types are: B<message chat headline>. Default message type is B<message>. Headline type message can be set also with B<--headline> option, see B<--headline>
591
592 =item B<-c>,B<--chatroom>
593
594 Send the message to a chatroom
595
596 =item B<-s>,B<--subject> I<subject>
597
598 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
599
600 =item B<-m>,B<--message> I<message>
601
602 Read the message from I<message> (a file) instead of stdin
603
604 =item B<-i>,B<--interactive>
605
606 Work in interactive mode, reading lines from stdin and sending the one-at-time
607
608 =item B<-w>,B<--raw>
609
610 Send raw XML message to jabber server
611
612 =item B<-v>,B<--verbose>
613
614 Give verbose output about what is happening
615
616 =item B<-h>,B<--help>,B<--usage>
617
618 Show a 'Usage' message
619
620 =item B<-d>,B<--debug>
621
622 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
623
624 =back
625
626 =head1 CONFIGURATION FILE
627
628 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
629 xmpp-account, with a line of the format:
630
631 =over
632
633 I<user>@I<server> I<password> I<componentname>
634
635 =back
636
637 e.g.:
638
639     # my account
640     alice@jabber.org  secret
641
642 ('#' and newlines are allowed like in shellscripts). You can add a I<host> (or IP address) if it is different from the I<server> part of your JID:
643
644     # account with specific connection host
645     alice@myjabberserver.com;foo.com secret
646
647 You can also add a I<port> if it is not the standard XMPP port:
648
649     # account with weird port number
650     alice@myjabberserver.com:1234 secret
651
652 Of course, you may also mix the two:
653
654     # account with a specific host and port
655     alice@myjabberserver.com;foo.com:1234 secret
656
657 B<NOTE>: for your security, sendxmpp demands that the configuration
658 file is owned by you and readable only to you (permissions 600).
659
660 =head1 EXAMPLE
661
662    $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
663
664      or to send to a chatroom:
665
666    $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
667
668      or to send your system logs somewhere, as new lines appear:
669
670    $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
671
672      NOTE: be careful not the overload public jabber services
673
674 =head1 SEE ALSO
675
676 Documentation for the L<Net::XMPP> module
677
678 The jabber homepage: L<http://www.jabber.org/>
679
680 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
681
682 =head1 AUTHOR
683
684 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
685 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
686 Lubomir Host 'rajo' <rajo AT platon.sk>, L<http://rajo.platon.sk>
687
688 =cut