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