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.18 2009-01-10 11:40:14 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.18 $ =~ 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'},
76 $$cmdline{'username'} || $$config{'username'},
77 $$cmdline{'password'} || $$config{'password'},
78 $$cmdline{'component'}|| $$config{'component'},
79 $$cmdline{'resource'},
82 or error_exit("cannot login: $!");
85 # read message from STDIN or or from -m/--message parameter
86 if (!$$cmdline{interactive}) {
88 # the non-interactive case
90 my $message = $$cmdline{'message'};
92 open (MSG, "<$message")
93 or error_exit ("cannot open message file '$message': $!");
94 while (<MSG>) { $txt .= $_ };
98 $txt .= $_ while (<STDIN>);
101 xmpp_send ($cnx,$cmdline,$config,$txt);
104 # the interactive case, read stdin line by line
108 $SIG{INT}=\&terminate;
113 xmpp_send ($cnx,$cmdline,$config,$_);
124 # read_config_file: read the configuration file
126 # output: hash with 'user', 'jserver' and 'password' keys
128 sub read_config_file ($) {
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")
141 open (CFG,"<$cfg_file")
142 or error_exit("cannot open $cfg_file for reading: $!");
150 next if (/^\s*$/); # ignore empty lines
151 next if (/^\s*\#.*/); # ignore comment lines
153 #s/\#.*$//; # ignore comments in lines
155 # Hugo van der Kooij <hvdkooij AT vanderkooij.org> has account with '#' as username
156 if (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) {
168 error_exit ("syntax error in line $line of $cfg_file");
171 # account with weird port number
172 if ($config{'jserver'} =~ /(.*):(\d+)/) {
173 $config{'jserver'} = $1;
174 $config{'port'} = $2;
177 # account with specific connection host
178 if ($config{'jserver'} =~ /(.*);([-\.\w]+)/) {
179 $config{'jserver'} = $2;
180 $config{'username'} .= "\@$1";
186 error_exit ("no correct config found in $cfg_file")
187 unless (scalar(%config));
189 if ($DEBUG || $VERBOSE) {
190 while (my ($key,$val) = each %config) {
191 debug_print ("config: '$key' => '$val'");
201 # parse_cmdline: parse commandline options
202 # output: hash with commandline options
204 sub parse_cmdline () {
206 usage() unless (scalar(@ARGV));
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,
222 'interactive|i' => \$interactive,
223 'help|usage|h' => \$help,
224 'debug|d' => \$debug,
226 'verbose|v' => \$verbose);
231 if (defined($raw) && scalar(@rcpt) > 0) {
232 error_exit("You must give a recipient or --raw (but not both)");
234 if ($raw && $subject) {
235 error_exit("You cannot specify a subject in raw XML mode");
237 if ($raw && $chatroom) {
238 error_exit("The chatroom option is pointless in raw XML mode");
241 if ($message && $interactive) {
242 error_exit("Cannot have both -m (--message) and -i (--interactive)");
245 if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
246 error_exit("Unsupported message type '$message_type'");
250 # --headline withouth --message-type
251 if ($message_type eq 'message') {
252 $message_type = 'headline'
255 error_exit("Options --headline and --message-type are mutually exclusive");
259 if ($jserver && $jserver =~ /(.*):(\d+)/) {
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);
282 if ($DEBUG || $VERBOSE) {
283 while (my ($key,$val) = each %dict) {
284 debug_print ("cmdline: '$key' => '$val'");
293 # xmpp_login: login to the xmpp (jabber) server
294 # input: hostname,port,username,password,resource,tls,debug
295 # output: an XMPP connection object
297 sub xmpp_login ($$$$$$$$) {
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: $!"
308 connectiontype => 'tcpip',
309 componentname => $comp
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;
317 @res = $cnx->Connect(%$arghash);
318 error_exit ("Could not connect to server '$host': $@") unless @res;
321 xmpp_check_result("Connect",\@res,$cnx);
324 my $sid = $cnx->{SESSION}->{id};
325 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
328 @res = $cnx->AuthSend(#'hostname' => $host,
332 xmpp_check_result('AuthSend',\@res,$cnx);
341 # xmmp_send: send the message, determine from cmdline
342 # whether it's to individual or chatroom
344 sub xmpp_send ($$$$) {
346 my ($cnx, $cmdline, $config, $txt) = @_;
348 unless ($$cmdline{'chatroom'}) {
349 unless ($$cmdline{'raw'}) {
351 xmpp_send_message ($cnx,
352 $_, #$$cmdline{'recipient'},
353 $$cmdline{'component'} || $$config{'component'},
354 $$cmdline{'subject'},
355 $$cmdline{'message-type'},
357 } @{$$cmdline{'recipient'}};
360 xmpp_send_raw_xml ($cnx, $txt);
365 xmpp_send_chatroom_message ($cnx,
366 $$cmdline{'resource'},
367 $$cmdline{'subject'},
368 $_, # $$cmdline{'recipient'},
370 } @{$$cmdline{'recipient'}};
377 # xmpp_send_raw_xml: send a raw XML packet
378 # input: connection,packet
380 sub xmpp_send_raw_xml ($$) {
382 my ($cnx,$packet) = @_;
384 # for some reason, Send does not return anything
386 xmpp_check_result('Send',0,$cnx);
391 # xmpp_send_message: send a message to some xmpp user
392 # input: connection,recipient,subject,msg
394 sub xmpp_send_message ($$$$$$) {
396 my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
398 # for some reason, MessageSend does not return anything
399 $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ),
400 'type' => $message_type,
401 'subject' => $subject,
404 xmpp_check_result('MessageSend',0,$cnx);
409 # xmpp_send_chatroom_message: send a message to a chatroom
410 # input: connection,resource,subject,recipient,message
412 sub xmpp_send_chatroom_message ($$$$$) {
414 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
417 my $pres = new Net::XMPP::Presence;
418 my $res = $pres->SetTo("$rcpt/$resource");
422 # create/send the message
423 my $groupmsg = new Net::XMPP::Message;
424 $groupmsg->SetMessage(to => $rcpt,
426 type => 'groupchat');
428 $res = $cnx->Send($groupmsg);
429 xmpp_check_result ('Send',$res,$cnx);
432 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
437 # xmpp_logout: log out from the xmpp server
443 # messages may not be received if we log out too quickly...
448 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
454 # xmpp_check_result: check the return value from some xmpp function execution
455 # input: text, result, [connection]
457 sub xmpp_check_result
459 my ($txt, $res, $cnx)=@_;
461 error_exit ("Error '$txt': result undefined")
462 unless (defined $res);
467 # result can be true or 'ok'
469 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
470 debug_print "$txt: " . $$res[0];
471 # otherwise, there is some error
474 my $errmsg = $cnx->GetErrorCode() || '?';
475 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
481 # terminate; exit the program upon TERM sig reception
484 debug_print "caught TERM";
485 xmpp_logout($main::CNX);
491 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
492 # input: [array of strings]
495 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
496 if (@_ && ($DEBUG ||$VERBOSE));
501 # error_exit: print error message and exit the program
502 # logs out if there is a connection
503 # input: error, [connection]
508 print STDERR "$err\n";
517 # usage: print short usage message and exit
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";
539 sendxmpp - send xmpp messages from the commandline.
543 sendxmpp [options] <recipient1> [<recipient2> ...]
545 sendxmpp --raw [options]
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.
556 =item B<-f>,B<--file> I<file>
558 Use I<file> configuration file instead of F<~/.sendxmpprc>
560 =item B<-u>,B<--username> I<user>
562 Use I<user> instead of the one in the configuration file
564 =item B<-p>,B<--password> I<password>
566 Use I<password> instead of the one in the configuration file
568 =item B<-j>,B<--jserver> I<server>
570 Use jabber I<server> instead of the one in the configuration file.
572 =item B<-o>,B<--component> I<componentname>
574 Use componentname in connect call. Seems needed for Google talk.
576 =item B<-r>,B<--resource> I<res>
578 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
582 Connect securely, using TLS
584 =item B<-l>,B<--headline>
586 Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages)
588 =item B<--messages-type>
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>
592 =item B<-c>,B<--chatroom>
594 Send the message to a chatroom
596 =item B<-s>,B<--subject> I<subject>
598 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
600 =item B<-m>,B<--message> I<message>
602 Read the message from I<message> (a file) instead of stdin
604 =item B<-i>,B<--interactive>
606 Work in interactive mode, reading lines from stdin and sending the one-at-time
610 Send raw XML message to jabber server
612 =item B<-v>,B<--verbose>
614 Give verbose output about what is happening
616 =item B<-h>,B<--help>,B<--usage>
618 Show a 'Usage' message
620 =item B<-d>,B<--debug>
622 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
626 =head1 CONFIGURATION FILE
628 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
629 xmpp-account, with a line of the format:
633 I<user>@I<server> I<password> I<componentname>
640 alice@jabber.org secret
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:
644 # account with specific connection host
645 alice@myjabberserver.com;foo.com secret
647 You can also add a I<port> if it is not the standard XMPP port:
649 # account with weird port number
650 alice@myjabberserver.com:1234 secret
652 Of course, you may also mix the two:
654 # account with a specific host and port
655 alice@myjabberserver.com;foo.com:1234 secret
657 B<NOTE>: for your security, sendxmpp demands that the configuration
658 file is owned by you and readable only to you (permissions 600).
662 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
664 or to send to a chatroom:
666 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
668 or to send your system logs somewhere, as new lines appear:
670 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
672 NOTE: be careful not the overload public jabber services
676 Documentation for the L<Net::XMPP> module
678 The jabber homepage: L<http://www.jabber.org/>
680 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
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>