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