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 - 2007 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.13 2007-09-10 19:08:35 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.13 $ =~ m/(\S+)\s*$/g ]->[0];
48 my $RESOURCE = 'sendxmpp';
60 my $cmdline = parse_cmdline();
62 $| = 1; # no output buffering
64 $DEBUG = 1 if ($$cmdline{'debug'});
65 $VERBOSE = 1 if ($$cmdline{'verbose'});
67 my $config = read_config_file ($$cmdline{'file'})
68 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
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'},
79 or error_exit("cannot login: $!");
82 # read message from STDIN or or from -m/--message parameter
83 if (!$$cmdline{interactive}) {
85 # the non-interactive case
87 my $message = $$cmdline{'message'};
89 open (MSG, "<$message")
90 or error_exit ("cannot open message file '$message': $!");
91 while (<MSG>) {$txt.=$_};
94 $txt.=$_ while (<STDIN>);
97 xmpp_send ($cnx,$cmdline,$config,$txt);
100 # the interactive case, read stdin line by line
104 $SIG{INT}=\&terminate;
109 xmpp_send ($cnx,$cmdline,$config,$_);
120 # read_config_file: read the configuration file
122 # output: hash with 'user', 'jserver' and 'password' keys
124 sub read_config_file ($) {
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")
137 open (CFG,"<$cfg_file")
138 or error_exit("cannot open $cfg_file for reading: $!");
146 next if (/^\s*$/); # ignore empty lines
147 next if (/^\s*\#.*/); # ignore comment lines
149 #s/\#.*$//; # ignore comments in lines
151 # Hugo van der Kooij <hvdkooij AT vanderkooij.org> has ccount with '#' as username
152 if (/([\.\w_#-]+)@([-\.\w:]+)\s+(\S+)\s*(\S+)?$/) {
161 if ($config{'jserver'} =~ /(.*):(\d+)/) {
162 $config{'jserver'} = $1;
163 $config{'port'} = $2;
168 error_exit ("syntax error in line $line of $cfg_file");
174 error_exit ("no correct config found in $cfg_file")
175 unless (scalar(%config));
177 if ($DEBUG || $VERBOSE) {
178 while (my ($key,$val) = each %config) {
179 debug_print ("config: '$key' => '$val'");
189 # parse_cmdline: parse commandline options
190 # output: hash with commandline options
192 sub parse_cmdline () {
194 usage() unless (scalar(@ARGV));
196 my ($subject,$file,$resource,$jserver,$port,$username,$password,$component,
197 $message, $chatroom, $headline, $debug, $tls, $interactive, $help, $raw, $verbose);
198 my $res = GetOptions ('subject|s=s' => \$subject,
199 'file|f=s' => \$file,
200 'resource|r=s' => \$resource,
201 'jserver|j=s' => \$jserver,
202 'component|o=s' => \$component,
203 'username|u=s' => \$username,
204 'password|p=s' => \$password,
205 'message|m=s' => \$message,
206 'headline|l' => \$headline,
207 'chatroom|c' => \$chatroom,
209 'interactive|i' => \$interactive,
210 'help|usage|h' => \$help,
211 'debug|d' => \$debug,
213 'verbose|v' => \$verbose);
218 if (defined($raw) && scalar(@rcpt) > 0) {
219 error_exit "You must give a recipient or --raw (but not both)";
221 if ($raw && $subject) {
222 error_exit("You cannot specify a subject in raw XML mode");
224 if ($raw && $chatroom) {
225 error_exit("The chatroom option is pointless in raw XML mode");
228 if ($message && $interactive) {
229 error_exit "Cannot have both -m (--message) and -i (--interactive)";
232 if ($jserver && $jserver =~ /(.*):(\d+)/) {
237 my %dict = ('subject' => ($subject or ''),
238 'message' => ($message or ''),
239 'resource' => ($resource or $RESOURCE),
240 'jserver' => ($jserver or ''),
241 'component' => ($component or ''),
242 'port' => ($port or 0),
243 'username' => ($username or ''),
244 'password' => ($password or ''),
245 'chatroom' => ($chatroom or 0),
246 'headline' => ($headline or 0),
247 'interactive' => ($interactive or 0),
248 'tls' => ($tls or 0),
249 'debug' => ($debug or 0),
250 'verbose' => ($verbose or 0),
251 'raw' => ($raw or 0),
252 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
253 'recipient' => \@rcpt);
255 if ($DEBUG || $VERBOSE) {
256 while (my ($key,$val) = each %dict) {
257 debug_print ("cmdline: '$key' => '$val'");
266 # xmpp_login: login to the xmpp (jabber) server
267 # input: hostname,port,username,password,resource,tls,debug
268 # output: an XMPP connection object
270 sub xmpp_login ($$$$$$$$) {
272 my ($host, $port, $user, $pw, $comp, $res, $tls, $debug) = @_;
273 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
274 error_exit "could not create XMPP client object: $!"
281 connectiontype => 'tcpip',
282 componentname => $comp
284 $arghash->{port} = $port if (!$port);
286 @res = $cnx->Connect(%$arghash);
287 error_exit ("Could not connect to server '$host': $@") unless @res;
289 @res = $cnx->Connect(%$arghash);
290 error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
293 xmpp_check_result("Connect",\@res,$cnx);
296 my $sid = $cnx->{SESSION}->{id};
297 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
300 @res = $cnx->AuthSend(#'hostname' => $host,
304 xmpp_check_result('AuthSend',\@res,$cnx);
313 # xmmp_send: send the message, determine from cmdline
314 # whether it's to individual or chatroom
316 sub xmpp_send ($$$$) {
318 my ($cnx, $cmdline, $config, $txt) = @_;
320 unless ($$cmdline{'chatroom'}) {
321 unless ($$cmdline{'raw'}) {
323 xmpp_send_message ($cnx,
324 $_, #$$cmdline{'recipient'},
325 $$cmdline{'component'} || $$config{'component'},
326 $$cmdline{'subject'},
327 $$cmdline{'headline'},
329 } @{$$cmdline{'recipient'}};
332 xmpp_send_raw_xml ($cnx, $txt);
337 xmpp_send_chatroom_message ($cnx,
338 $$cmdline{'resource'},
339 $$cmdline{'subject'},
340 $_, # $$cmdline{'recipient'},
342 } @{$$cmdline{'recipient'}};
349 # xmpp_send_raw_xml: send a raw XML packet
350 # input: connection,packet
352 sub xmpp_send_raw_xml ($$) {
354 my ($cnx,$packet) = @_;
356 # for some reason, Send does not return anything
358 xmpp_check_result('Send',0,$cnx);
363 # xmpp_send_message: send a message to some xmpp user
364 # input: connection,recipient,subject,msg
366 sub xmpp_send_message ($$$$$$) {
368 my ($cnx,$rcpt,$comp,$subject,$headline,$msg) = @_;
370 my $type = 'message';
375 # for some reason, MessageSend does not return anything
376 $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ),
378 'subject' => $subject,
381 xmpp_check_result('MessageSend',0,$cnx);
386 # xmpp_send_chatroom_message: send a message to a chatroom
387 # input: connection,resource,subject,recipient,message
389 sub xmpp_send_chatroom_message ($$$$$) {
391 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
394 my $pres = new Net::XMPP::Presence;
395 my $res = $pres->SetTo("$rcpt/$resource");
399 # create/send the message
400 my $groupmsg = new Net::XMPP::Message;
401 $groupmsg->SetMessage(to => $rcpt,
403 type => 'groupchat');
405 $res = $cnx->Send($groupmsg);
406 xmpp_check_result ('Send',$res,$cnx);
409 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
414 # xmpp_logout: log out from the xmpp server
420 # messages may not be received if we log out too quickly...
425 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
431 # xmpp_check_result: check the return value from some xmpp function execution
432 # input: text, result, [connection]
434 sub xmpp_check_result
436 my ($txt, $res, $cnx)=@_;
438 error_exit ("Error '$txt': result undefined")
439 unless (defined $res);
444 # result can be true or 'ok'
446 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
447 debug_print "$txt: " . $$res[0];
448 # otherwise, there is some error
451 my $errmsg = $cnx->GetErrorCode() || '?';
452 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
458 # terminate; exit the program upon TERM sig reception
461 debug_print "caught TERM";
462 xmpp_logout($main::CNX);
468 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
469 # input: [array of strings]
472 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
473 if (@_ && ($DEBUG ||$VERBOSE));
478 # error_exit: print error message and exit the program
479 # logs out if there is a connection
480 # input: error, [connection]
485 print STDERR "$err\n";
494 # usage: print short usage message and exit
499 "sendxmpp version $VERSION\n" .
500 "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" .
501 "Copyright (c) 2006 - 2007 Lubomir Host 'rajo'\n" .
502 "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
503 "or refer to the the sendxmpp manpage\n";
515 sendxmpp - send xmpp messages from the commandline.
519 sendxmpp [options] <recipient1> [<recipient2> ...]
521 sendxmpp --raw [options]
525 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
526 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
532 =item B<-f>,B<--file> I<file>
534 Use I<file> configuration file instead of F<~/.sendxmpprc>
536 =item B<-u>,B<--username> I<user>
538 Use I<user> instead of the one in the configuration file
540 =item B<-p>,B<--password> I<password>
542 Use I<password> instead of the one in the configuration file
544 =item B<-j>,B<--jserver> I<server>
546 Use jabber I<server> instead of the one in the configuration file.
548 =item B<-o>,B<--component> I<componentname>
550 Use componentname in connect call. Seems needed for Google talk.
552 =item B<-r>,B<--resource> I<res>
554 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
558 Connect securely, using TLS
560 =item B<-l>,B<--headline>
562 Send a headline type message (not stored in offline messages)
564 =item B<-c>,B<--chatroom>
566 Send the message to a chatroom
568 =item B<-s>,B<--subject> I<subject>
570 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
572 =item B<-m>,B<--message> I<message>
574 Read the message from I<message> (a file) instead of stdin
576 =item B<-i>,B<--interactive>
578 Work in interactive mode, reading lines from stdin and sending the one-at-time
582 Send raw XML message to jabber server
584 =item B<-v>,B<--verbose>
586 Give verbose output about what is happening
588 =item B<-h>,B<--help>,B<--usage>
590 Show a 'Usage' message
592 =item B<-d>,B<--debug>
594 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
598 =head1 CONFIGURATION FILE
600 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
601 xmpp-account, with a line of the format:
605 I<user>@I<server> I<password> I<componentname>
612 alice@jabber.org secret
614 ('#' 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:
616 # account with specific connection host
617 alice@myjabberserver.com;foo.com secret
619 You can also add a I<port> if it is not the standard XMPP port:
621 # account with weird port number
622 alice@myjabberserver.com:1234 secret
624 Of course, you may also mix the two:
626 # account with a specific host and port
627 alice@myjabberserver.com;foo.com:1234 secret
629 B<NOTE>: for your security, sendxmpp demands that the configuration
630 file is owned by you and readable only to you (permissions 600).
634 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
636 or to send to a chatroom:
638 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
640 or to send your system logs somewhere, as new lines appear:
642 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
644 NOTE: be careful not the overload public jabber services
648 Documentation for the L<Net::XMPP> module
650 The jabber homepage: L<http://www.jabber.org/>
652 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
656 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
657 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
658 Lubomir Host 'rajo' <rajo AT platon.sk>, L<http://rajo.platon.sk>