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 - 2009 Lubomir Host 'rajo'
15 # Homepage: http://sendxmpp.platon.sk
17 # Released under the terms of the GNU General Public License v2
19 # $Platon: sendxmpp/sendxmpp,v 1.20 2010-01-18 00:54:16 rajo Exp $
22 use Authen::SASL qw(Perl); # authentication broken if Authen::SASL::Cyrus module installed
31 sub xmpp_login($$$$$$$$$);
33 sub xmpp_send_raw_xml($$);
34 sub xmpp_send_message($$$$$$);
35 sub xmpp_send_chatroom_message($$$$$);
37 sub xmpp_check_result;
41 sub read_config_file($);
47 $VERSION = [ q$Revision: 1.20 $ =~ m/(\S+)\s*$/g ]->[0];
48 my $RESOURCE = 'sendxmpp';
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
63 my $cmdline = parse_cmdline();
65 $| = 1; # no output buffering
67 $DEBUG = 1 if ($$cmdline{'debug'});
68 $VERBOSE = 1 if ($$cmdline{'verbose'});
70 my $config = read_config_file ($$cmdline{'file'})
71 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
74 my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'},
75 $$cmdline{'port'} || $$config{'port'} || ($$cmdline{'ssl'} ? 5223 : 5222),
76 $$cmdline{'username'} || $$config{'username'},
77 $$cmdline{'password'} || $$config{'password'},
78 $$cmdline{'component'}|| $$config{'component'},
79 $$cmdline{'resource'},
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";
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, $interactive, $help, $raw, $verbose);
211 my $res = GetOptions ('subject|s=s' => \$subject,
212 'file|f=s' => \$file,
213 'resource|r=s' => \$resource,
214 'jserver|j=s' => \$jserver,
215 'component|o=s' => \$component,
216 'username|u=s' => \$username,
217 'password|p=s' => \$password,
218 'message|m=s' => \$message,
219 'headline|l' => \$headline,
220 'message-type=s' => \$message_type,
221 'chatroom|c' => \$chatroom,
224 'interactive|i' => \$interactive,
225 'help|usage|h' => \$help,
226 'debug|d' => \$debug,
228 'verbose|v' => \$verbose);
233 if (defined($raw) && scalar(@rcpt) > 0) {
234 error_exit("You must give a recipient or --raw (but not both)");
236 if ($raw && $subject) {
237 error_exit("You cannot specify a subject in raw XML mode");
239 if ($raw && $chatroom) {
240 error_exit("The chatroom option is pointless in raw XML mode");
243 if ($message && $interactive) {
244 error_exit("Cannot have both -m (--message) and -i (--interactive)");
247 if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
248 error_exit("Unsupported message type '$message_type'");
252 error_exit("Connect securely wether using -e (--ssl) or -t (--tls)");
256 # --headline withouth --message-type
257 if ($message_type eq 'message') {
258 $message_type = 'headline'
261 error_exit("Options --headline and --message-type are mutually exclusive");
265 if ($jserver && $jserver =~ /(.*):(\d+)/) {
270 my %dict = ('subject' => ($subject or ''),
271 'message' => ($message or ''),
272 'resource' => ($resource or $RESOURCE),
273 'jserver' => ($jserver or ''),
274 'component' => ($component or ''),
275 'port' => ($port or 0),
276 'username' => ($username or ''),
277 'password' => ($password or ''),
278 'chatroom' => ($chatroom or 0),
279 'message-type' => $message_type,
280 'interactive' => ($interactive or 0),
281 'tls' => ($tls or 0),
282 'ssl' => ($ssl or 0),
283 'debug' => ($debug or 0),
284 'verbose' => ($verbose or 0),
285 'raw' => ($raw or 0),
286 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
287 'recipient' => \@rcpt);
289 if ($DEBUG || $VERBOSE) {
290 while (my ($key,$val) = each %dict) {
291 debug_print ("cmdline: '$key' => '$val'");
300 # xmpp_login: login to the xmpp (jabber) server
301 # input: hostname,port,username,password,resource,tls,ssl,debug
302 # output: an XMPP connection object
304 sub xmpp_login ($$$$$$$$$) {
306 my ($host, $port, $user, $pw, $comp, $res, $tls, $ssl, $debug) = @_;
307 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
308 error_exit "could not create XMPP client object: $!"
317 connectiontype => 'tcpip',
318 componentname => $comp
321 delete $arghash->{port} unless $port;
322 if ($arghash->{port}) {
323 @res = $cnx->Connect(%$arghash);
324 error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
326 @res = $cnx->Connect(%$arghash);
327 error_exit ("Could not connect to server '$host': $@") unless @res;
330 xmpp_check_result("Connect",\@res,$cnx);
333 my $sid = $cnx->{SESSION}->{id};
334 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
337 @res = $cnx->AuthSend(#'hostname' => $host,
341 xmpp_check_result('AuthSend',\@res,$cnx);
350 # xmmp_send: send the message, determine from cmdline
351 # whether it's to individual or chatroom
353 sub xmpp_send ($$$$) {
355 my ($cnx, $cmdline, $config, $txt) = @_;
357 unless ($$cmdline{'chatroom'}) {
358 unless ($$cmdline{'raw'}) {
360 xmpp_send_message ($cnx,
361 $_, #$$cmdline{'recipient'},
362 $$cmdline{'component'} || $$config{'component'},
363 $$cmdline{'subject'},
364 $$cmdline{'message-type'},
366 } @{$$cmdline{'recipient'}};
369 xmpp_send_raw_xml ($cnx, $txt);
374 xmpp_send_chatroom_message ($cnx,
375 $$cmdline{'resource'},
376 $$cmdline{'subject'},
377 $_, # $$cmdline{'recipient'},
379 } @{$$cmdline{'recipient'}};
386 # xmpp_send_raw_xml: send a raw XML packet
387 # input: connection,packet
389 sub xmpp_send_raw_xml ($$) {
391 my ($cnx,$packet) = @_;
393 # for some reason, Send does not return anything
395 xmpp_check_result('Send',0,$cnx);
400 # xmpp_send_message: send a message to some xmpp user
401 # input: connection,recipient,subject,msg
403 sub xmpp_send_message ($$$$$$) {
405 my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
407 # for some reason, MessageSend does not return anything
408 $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ),
409 'type' => $message_type,
410 'subject' => $subject,
413 xmpp_check_result('MessageSend',0,$cnx);
418 # xmpp_send_chatroom_message: send a message to a chatroom
419 # input: connection,resource,subject,recipient,message
421 sub xmpp_send_chatroom_message ($$$$$) {
423 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
426 my $pres = new Net::XMPP::Presence;
427 my $res = $pres->SetTo("$rcpt/$resource");
431 # create/send the message
432 my $groupmsg = new Net::XMPP::Message;
433 $groupmsg->SetMessage(to => $rcpt,
435 type => 'groupchat');
437 $res = $cnx->Send($groupmsg);
438 xmpp_check_result ('Send',$res,$cnx);
441 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
446 # xmpp_logout: log out from the xmpp server
452 # messages may not be received if we log out too quickly...
457 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
463 # xmpp_check_result: check the return value from some xmpp function execution
464 # input: text, result, [connection]
466 sub xmpp_check_result
468 my ($txt, $res, $cnx)=@_;
470 error_exit ("Error '$txt': result undefined")
471 unless (defined $res);
476 # result can be true or 'ok'
478 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
479 debug_print "$txt: " . $$res[0];
480 # otherwise, there is some error
483 my $errmsg = $cnx->GetErrorCode() || '?';
484 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
490 # terminate; exit the program upon TERM sig reception
493 debug_print "caught TERM";
494 xmpp_logout($main::CNX);
500 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
501 # input: [array of strings]
504 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
505 if (@_ && ($DEBUG ||$VERBOSE));
510 # error_exit: print error message and exit the program
511 # logs out if there is a connection
512 # input: error, [connection]
517 print STDERR "$err\n";
526 # usage: print short usage message and exit
531 "sendxmpp version $VERSION\n" .
532 "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" .
533 "Copyright (c) 2006 - 2007 Lubomir Host 'rajo'\n" .
534 "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
535 "or refer to the the sendxmpp manpage\n";
548 sendxmpp - send xmpp messages from the commandline.
552 sendxmpp [options] <recipient1> [<recipient2> ...]
554 sendxmpp --raw [options]
558 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
559 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
565 =item B<-f>,B<--file> I<file>
567 Use I<file> configuration file instead of F<~/.sendxmpprc>
569 =item B<-u>,B<--username> I<user>
571 Use I<user> instead of the one in the configuration file
573 =item B<-p>,B<--password> I<password>
575 Use I<password> instead of the one in the configuration file
577 =item B<-j>,B<--jserver> I<server>
579 Use jabber I<server> instead of the one in the configuration file.
581 =item B<-o>,B<--component> I<componentname>
583 Use componentname in connect call. Seems needed for Google talk.
585 =item B<-r>,B<--resource> I<res>
587 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
591 Connect securely, using TLS
595 Connect securely, using SSL
597 =item B<-l>,B<--headline>
599 Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages)
601 =item B<--messages-type>
603 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>
605 =item B<-c>,B<--chatroom>
607 Send the message to a chatroom
609 =item B<-s>,B<--subject> I<subject>
611 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
613 =item B<-m>,B<--message> I<message>
615 Read the message from I<message> (a file) instead of stdin
617 =item B<-i>,B<--interactive>
619 Work in interactive mode, reading lines from stdin and sending the one-at-time
623 Send raw XML message to jabber server
625 =item B<-v>,B<--verbose>
627 Give verbose output about what is happening
629 =item B<-h>,B<--help>,B<--usage>
631 Show a 'Usage' message
633 =item B<-d>,B<--debug>
635 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
639 =head1 CONFIGURATION FILE
641 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
642 xmpp-account, with a line of the format:
646 I<user>@I<server> I<password> I<componentname>
653 alice@jabber.org secret
655 ('#' 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:
657 # account with specific connection host
658 alice@myjabberserver.com;foo.com secret
660 You can also add a I<port> if it is not the standard XMPP port:
662 # account with weird port number
663 alice@myjabberserver.com:1234 secret
665 Of course, you may also mix the two:
667 # account with a specific host and port
668 alice@myjabberserver.com;foo.com:1234 secret
670 B<NOTE>: for your security, sendxmpp demands that the configuration
671 file is owned by you and readable only to you (permissions 600).
675 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
677 or to send to a chatroom:
679 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
681 or to send your system logs somewhere, as new lines appear:
683 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
685 NOTE: be careful not the overload public jabber services
689 Documentation for the L<Net::XMPP> module
691 The jabber homepage: L<http://www.jabber.org/>
693 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
697 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
698 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
699 Lubomir Host 'rajo' <rajo AT platon.sk>, L<http://rajo.platon.sk>