3 eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
7 # script to send message using xmpp (aka jabber),
8 # somewhat resembling mail(1)
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'
15 # Homepage: http://sendxmpp.hostname.sk
17 # Released under the terms of the GNU General Public License v2
20 use Authen::SASL qw(Perl); # authentication broken if Authen::SASL::Cyrus module installed
29 sub xmpp_login($$$$$$$$$$$);
31 sub xmpp_send_raw_xml($$);
32 sub xmpp_send_message($$$$$$);
33 sub xmpp_send_chatroom_message($$$$$);
35 sub xmpp_check_result;
39 sub read_config_file($);
45 $VERSION = [ q$Revision: 1.23 $ =~ m/(\S+)\s*$/g ]->[0];
46 my $RESOURCE = 'sendxmpp';
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
61 my $cmdline = parse_cmdline();
63 $| = 1; # no output buffering
65 $DEBUG = 1 if ($$cmdline{'debug'});
66 $VERBOSE = 1 if ($$cmdline{'verbose'});
68 my $config = read_config_file ($$cmdline{'file'})
69 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
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'} || '',
83 or error_exit("cannot login: $!");
86 # read message from STDIN or or from -m/--message parameter
87 if (!$$cmdline{interactive}) {
89 # the non-interactive case
91 my $message = $$cmdline{'message'};
93 open (MSG, "<$message")
94 or error_exit ("cannot open message file '$message': $!");
95 while (<MSG>) { $txt .= $_ };
99 $txt .= $_ while (<STDIN>);
102 xmpp_send ($cnx,$cmdline,$config,$txt);
105 # the interactive case, read stdin line by line
109 $SIG{INT}=\&terminate;
114 xmpp_send ($cnx,$cmdline,$config,$_);
125 # read_config_file: read the configuration file
127 # output: hash with 'user', 'jserver' and 'password' keys
129 sub read_config_file ($) {
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")
142 open (CFG,"<$cfg_file")
143 or error_exit("cannot open $cfg_file for reading: $!");
151 next if (/^\s*$/); # ignore empty lines
152 next if (/^\s*\#.*/); # ignore comment lines
154 #s/\#.*$//; # ignore comments in lines
156 # Hugo van der Kooij <hvdkooij AT vanderkooij.org> has account with '#' as username
157 if (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) {
169 error_exit ("syntax error in line $line of $cfg_file");
172 # account with weird port number
173 if ($config{'jserver'} =~ /(.*):(\d+)/) {
174 $config{'jserver'} = $1;
175 $config{'port'} = $2;
178 # account with specific connection host
179 if ($config{'jserver'} =~ /(.*);([-\.\w]+)/) {
180 $config{'jserver'} = $2;
181 $config{'username'} .= "\@$1" unless $config{'component'};
187 error_exit ("no correct config found in $cfg_file")
188 unless (scalar(%config));
190 if ($DEBUG || $VERBOSE) {
191 while (my ($key,$val) = each %config) {
192 debug_print ("config: '$key' => '$val'");
202 # parse_cmdline: parse commandline options
203 # output: hash with commandline options
205 sub parse_cmdline () {
207 usage() unless (scalar(@ARGV));
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,
225 'no-tls-verify|n' => \$no_tls_verify,
226 'tls-ca-path|a=s' => \$tls_ca_path,
228 'interactive|i' => \$interactive,
229 'help|usage|h' => \$help,
230 'debug|d' => \$debug,
232 'verbose|v' => \$verbose);
237 if (defined($raw) && scalar(@rcpt) > 0) {
238 error_exit("You must give a recipient or --raw (but not both)");
240 if ($raw && $subject) {
241 error_exit("You cannot specify a subject in raw XML mode");
243 if ($raw && $chatroom) {
244 error_exit("The chatroom option is pointless in raw XML mode");
247 if ($message && $interactive) {
248 error_exit("Cannot have both -m (--message) and -i (--interactive)");
251 if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
252 error_exit("Unsupported message type '$message_type'");
256 error_exit("Connect securely wether using -e (--ssl) or -t (--tls)");
260 # --headline withouth --message-type
261 if ($message_type eq 'message') {
262 $message_type = 'headline'
265 error_exit("Options --headline and --message-type are mutually exclusive");
269 if ($jserver && $jserver =~ /(.*):(\d+)/) {
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);
295 if ($DEBUG || $VERBOSE) {
296 while (my ($key,$val) = each %dict) {
297 debug_print ("cmdline: '$key' => '$val'");
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
310 sub xmpp_login ($$$$$$$$$$$) {
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: $!"
317 my $ssl_verify = 0x01;
318 if ($no_tls_verify) { $ssl_verify = 0x00; }
319 debug_print "ssl_verify: $ssl_verify";
321 debug_print "tls_ca_path: $tls_ca_path";
328 ssl_verify => $ssl_verify,
329 ssl_ca_path => $tls_ca_path,
331 connectiontype => 'tcpip',
332 componentname => $comp
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;
340 @res = $cnx->Connect(%$arghash);
341 error_exit ("Could not connect to server '$host': $@") unless @res;
344 xmpp_check_result("Connect",\@res,$cnx);
347 my $sid = $cnx->{SESSION}->{id};
348 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
351 @res = $cnx->AuthSend(#'hostname' => $host,
355 xmpp_check_result('AuthSend',\@res,$cnx);
364 # xmmp_send: send the message, determine from cmdline
365 # whether it's to individual or chatroom
367 sub xmpp_send ($$$$) {
369 my ($cnx, $cmdline, $config, $txt) = @_;
371 unless ($$cmdline{'chatroom'}) {
372 unless ($$cmdline{'raw'}) {
374 xmpp_send_message ($cnx,
375 $_, #$$cmdline{'recipient'},
376 $$cmdline{'component'} || $$config{'component'},
377 $$cmdline{'subject'},
378 $$cmdline{'message-type'},
380 } @{$$cmdline{'recipient'}};
383 xmpp_send_raw_xml ($cnx, $txt);
388 xmpp_send_chatroom_message ($cnx,
389 $$cmdline{'resource'},
390 $$cmdline{'subject'},
391 $_, # $$cmdline{'recipient'},
393 } @{$$cmdline{'recipient'}};
400 # xmpp_send_raw_xml: send a raw XML packet
401 # input: connection,packet
403 sub xmpp_send_raw_xml ($$) {
405 my ($cnx,$packet) = @_;
407 # for some reason, Send does not return anything
409 xmpp_check_result('Send',0,$cnx);
414 # xmpp_send_message: send a message to some xmpp user
415 # input: connection,recipient,subject,msg
417 sub xmpp_send_message ($$$$$$) {
419 my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
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,
428 xmpp_check_result('MessageSend',0,$cnx);
433 # xmpp_send_chatroom_message: send a message to a chatroom
434 # input: connection,resource,subject,recipient,message
436 sub xmpp_send_chatroom_message ($$$$$) {
438 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
441 my $pres = new Net::XMPP::Presence;
442 my $res = $pres->SetTo("$rcpt/$resource");
446 # create/send the message
447 my $groupmsg = new Net::XMPP::Message;
448 $groupmsg->SetMessage(to => $rcpt,
450 type => 'groupchat');
452 $res = $cnx->Send($groupmsg);
453 xmpp_check_result ('Send',$res,$cnx);
456 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
461 # xmpp_logout: log out from the xmpp server
467 # messages may not be received if we log out too quickly...
472 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
478 # xmpp_check_result: check the return value from some xmpp function execution
479 # input: text, result, [connection]
481 sub xmpp_check_result
483 my ($txt, $res, $cnx)=@_;
485 error_exit ("Error '$txt': result undefined")
486 unless (defined $res);
491 # result can be true or 'ok'
493 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
494 debug_print "$txt: " . $$res[0];
495 # otherwise, there is some error
498 my $errmsg = $cnx->GetErrorCode() || '?';
499 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
505 # terminate; exit the program upon TERM sig reception
508 debug_print "caught TERM";
509 xmpp_logout($main::CNX);
515 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
516 # input: [array of strings]
519 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
520 if (@_ && ($DEBUG ||$VERBOSE));
525 # error_exit: print error message and exit the program
526 # logs out if there is a connection
527 # input: error, [connection]
532 print STDERR "$err\n";
541 # usage: print short usage message and exit
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";
563 sendxmpp - send xmpp messages from the commandline.
567 sendxmpp [options] <recipient1> [<recipient2> ...]
569 sendxmpp --raw [options]
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.
580 =item B<-f>,B<--file> I<file>
582 Use I<file> configuration file instead of F<~/.sendxmpprc>
584 =item B<-u>,B<--username> I<user>
586 Use I<user> instead of the one in the configuration file
588 =item B<-p>,B<--password> I<password>
590 Use I<password> instead of the one in the configuration file
592 =item B<-j>,B<--jserver> I<server>
594 Use jabber I<server> instead of the one in the configuration file.
596 =item B<-o>,B<--component> I<componentname>
598 Use componentname in connect call. Seems needed for Google talk.
600 =item B<-r>,B<--resource> I<res>
602 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
606 Connect securely, using TLS
610 Connect securely, using SSL
612 =item B<-n>,B<--no-tls-verify>
614 Deactivate the verification of SSL certificates. Better way is to use parameter B<--tls-ca-path> with the needed path to CA certificates.
616 =item B<-a>,B<--tls-ca-path>
618 Path to your custom CA certificates, so you can verificate SSL certificates during connecting.
620 =item B<-l>,B<--headline>
622 Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages)
624 =item B<--messages-type>
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>
628 =item B<-c>,B<--chatroom>
630 Send the message to a chatroom
632 =item B<-s>,B<--subject> I<subject>
634 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
636 =item B<-m>,B<--message> I<message>
638 Read the message from I<message> (a file) instead of stdin
640 =item B<-i>,B<--interactive>
642 Work in interactive mode, reading lines from stdin and sending the one-at-time
646 Send raw XML message to jabber server
648 =item B<-v>,B<--verbose>
650 Give verbose output about what is happening
652 =item B<-h>,B<--help>,B<--usage>
654 Show a 'Usage' message
656 =item B<-d>,B<--debug>
658 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
662 =head1 CONFIGURATION FILE
664 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
665 xmpp-account, with a line of the format:
669 I<user>@I<server> I<password> I<componentname>
676 alice@jabber.org secret
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:
680 # account with specific connection host
681 alice@myjabberserver.com;foo.com secret
683 You can also add a I<port> if it is not the standard XMPP port:
685 # account with weird port number
686 alice@myjabberserver.com:1234 secret
688 Of course, you may also mix the two:
690 # account with a specific host and port
691 alice@myjabberserver.com;foo.com:1234 secret
693 B<NOTE>: for your security, sendxmpp demands that the configuration
694 file is owned by you and readable only to you (permissions 600).
698 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
700 or to send to a chatroom:
702 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
704 or to send your system logs somewhere, as new lines appear:
706 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
708 NOTE: be careful not the overload public jabber services
712 Documentation for the L<Net::XMPP> module
714 The jabber homepage: L<http://www.jabber.org/>
716 The sendxmpp homepage: L<http://sendxmpp.hostname.sk>
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>