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.14 2008-08-25 09:54:12 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.14 $ =~ 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 account with '#' as username
152 if (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) {
164 error_exit ("syntax error in line $line of $cfg_file");
167 # account with weird port number
168 if ($config{'jserver'} =~ /(.*):(\d+)/) {
169 $config{'jserver'} = $1;
170 $config{'port'} = $2;
173 # account with specific connection host
174 if ($config{'jserver'} =~ /(.*);([-\.\w]+)/) {
175 $config{'jserver'} = $2;
176 $config{'username'} .= "\@$1";
182 error_exit ("no correct config found in $cfg_file")
183 unless (scalar(%config));
185 if ($DEBUG || $VERBOSE) {
186 while (my ($key,$val) = each %config) {
187 debug_print ("config: '$key' => '$val'");
197 # parse_cmdline: parse commandline options
198 # output: hash with commandline options
200 sub parse_cmdline () {
202 usage() unless (scalar(@ARGV));
204 my ($subject,$file,$resource,$jserver,$port,$username,$password,$component,
205 $message, $chatroom, $headline, $debug, $tls, $interactive, $help, $raw, $verbose);
206 my $res = GetOptions ('subject|s=s' => \$subject,
207 'file|f=s' => \$file,
208 'resource|r=s' => \$resource,
209 'jserver|j=s' => \$jserver,
210 'component|o=s' => \$component,
211 'username|u=s' => \$username,
212 'password|p=s' => \$password,
213 'message|m=s' => \$message,
214 'headline|l' => \$headline,
215 'chatroom|c' => \$chatroom,
217 'interactive|i' => \$interactive,
218 'help|usage|h' => \$help,
219 'debug|d' => \$debug,
221 'verbose|v' => \$verbose);
226 if (defined($raw) && scalar(@rcpt) > 0) {
227 error_exit "You must give a recipient or --raw (but not both)";
229 if ($raw && $subject) {
230 error_exit("You cannot specify a subject in raw XML mode");
232 if ($raw && $chatroom) {
233 error_exit("The chatroom option is pointless in raw XML mode");
236 if ($message && $interactive) {
237 error_exit "Cannot have both -m (--message) and -i (--interactive)";
240 if ($jserver && $jserver =~ /(.*):(\d+)/) {
245 my %dict = ('subject' => ($subject or ''),
246 'message' => ($message or ''),
247 'resource' => ($resource or $RESOURCE),
248 'jserver' => ($jserver or ''),
249 'component' => ($component or ''),
250 'port' => ($port or 0),
251 'username' => ($username or ''),
252 'password' => ($password or ''),
253 'chatroom' => ($chatroom or 0),
254 'headline' => ($headline or 0),
255 'interactive' => ($interactive or 0),
256 'tls' => ($tls or 0),
257 'debug' => ($debug or 0),
258 'verbose' => ($verbose or 0),
259 'raw' => ($raw or 0),
260 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
261 'recipient' => \@rcpt);
263 if ($DEBUG || $VERBOSE) {
264 while (my ($key,$val) = each %dict) {
265 debug_print ("cmdline: '$key' => '$val'");
274 # xmpp_login: login to the xmpp (jabber) server
275 # input: hostname,port,username,password,resource,tls,debug
276 # output: an XMPP connection object
278 sub xmpp_login ($$$$$$$$) {
280 my ($host, $port, $user, $pw, $comp, $res, $tls, $debug) = @_;
281 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
282 error_exit "could not create XMPP client object: $!"
289 connectiontype => 'tcpip',
290 componentname => $comp
292 $arghash->{port} = $port if (!$port);
294 @res = $cnx->Connect(%$arghash);
295 error_exit ("Could not connect to server '$host': $@") unless @res;
297 @res = $cnx->Connect(%$arghash);
298 error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
301 xmpp_check_result("Connect",\@res,$cnx);
304 my $sid = $cnx->{SESSION}->{id};
305 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
308 @res = $cnx->AuthSend(#'hostname' => $host,
312 xmpp_check_result('AuthSend',\@res,$cnx);
321 # xmmp_send: send the message, determine from cmdline
322 # whether it's to individual or chatroom
324 sub xmpp_send ($$$$) {
326 my ($cnx, $cmdline, $config, $txt) = @_;
328 unless ($$cmdline{'chatroom'}) {
329 unless ($$cmdline{'raw'}) {
331 xmpp_send_message ($cnx,
332 $_, #$$cmdline{'recipient'},
333 $$cmdline{'component'} || $$config{'component'},
334 $$cmdline{'subject'},
335 $$cmdline{'headline'},
337 } @{$$cmdline{'recipient'}};
340 xmpp_send_raw_xml ($cnx, $txt);
345 xmpp_send_chatroom_message ($cnx,
346 $$cmdline{'resource'},
347 $$cmdline{'subject'},
348 $_, # $$cmdline{'recipient'},
350 } @{$$cmdline{'recipient'}};
357 # xmpp_send_raw_xml: send a raw XML packet
358 # input: connection,packet
360 sub xmpp_send_raw_xml ($$) {
362 my ($cnx,$packet) = @_;
364 # for some reason, Send does not return anything
366 xmpp_check_result('Send',0,$cnx);
371 # xmpp_send_message: send a message to some xmpp user
372 # input: connection,recipient,subject,msg
374 sub xmpp_send_message ($$$$$$) {
376 my ($cnx,$rcpt,$comp,$subject,$headline,$msg) = @_;
378 my $type = 'message';
383 # for some reason, MessageSend does not return anything
384 $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ),
386 'subject' => $subject,
389 xmpp_check_result('MessageSend',0,$cnx);
394 # xmpp_send_chatroom_message: send a message to a chatroom
395 # input: connection,resource,subject,recipient,message
397 sub xmpp_send_chatroom_message ($$$$$) {
399 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
402 my $pres = new Net::XMPP::Presence;
403 my $res = $pres->SetTo("$rcpt/$resource");
407 # create/send the message
408 my $groupmsg = new Net::XMPP::Message;
409 $groupmsg->SetMessage(to => $rcpt,
411 type => 'groupchat');
413 $res = $cnx->Send($groupmsg);
414 xmpp_check_result ('Send',$res,$cnx);
417 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
422 # xmpp_logout: log out from the xmpp server
428 # messages may not be received if we log out too quickly...
433 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
439 # xmpp_check_result: check the return value from some xmpp function execution
440 # input: text, result, [connection]
442 sub xmpp_check_result
444 my ($txt, $res, $cnx)=@_;
446 error_exit ("Error '$txt': result undefined")
447 unless (defined $res);
452 # result can be true or 'ok'
454 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
455 debug_print "$txt: " . $$res[0];
456 # otherwise, there is some error
459 my $errmsg = $cnx->GetErrorCode() || '?';
460 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
466 # terminate; exit the program upon TERM sig reception
469 debug_print "caught TERM";
470 xmpp_logout($main::CNX);
476 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
477 # input: [array of strings]
480 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
481 if (@_ && ($DEBUG ||$VERBOSE));
486 # error_exit: print error message and exit the program
487 # logs out if there is a connection
488 # input: error, [connection]
493 print STDERR "$err\n";
502 # usage: print short usage message and exit
507 "sendxmpp version $VERSION\n" .
508 "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" .
509 "Copyright (c) 2006 - 2007 Lubomir Host 'rajo'\n" .
510 "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
511 "or refer to the the sendxmpp manpage\n";
523 sendxmpp - send xmpp messages from the commandline.
527 sendxmpp [options] <recipient1> [<recipient2> ...]
529 sendxmpp --raw [options]
533 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
534 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
540 =item B<-f>,B<--file> I<file>
542 Use I<file> configuration file instead of F<~/.sendxmpprc>
544 =item B<-u>,B<--username> I<user>
546 Use I<user> instead of the one in the configuration file
548 =item B<-p>,B<--password> I<password>
550 Use I<password> instead of the one in the configuration file
552 =item B<-j>,B<--jserver> I<server>
554 Use jabber I<server> instead of the one in the configuration file.
556 =item B<-o>,B<--component> I<componentname>
558 Use componentname in connect call. Seems needed for Google talk.
560 =item B<-r>,B<--resource> I<res>
562 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
566 Connect securely, using TLS
568 =item B<-l>,B<--headline>
570 Send a headline type message (not stored in offline messages)
572 =item B<-c>,B<--chatroom>
574 Send the message to a chatroom
576 =item B<-s>,B<--subject> I<subject>
578 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
580 =item B<-m>,B<--message> I<message>
582 Read the message from I<message> (a file) instead of stdin
584 =item B<-i>,B<--interactive>
586 Work in interactive mode, reading lines from stdin and sending the one-at-time
590 Send raw XML message to jabber server
592 =item B<-v>,B<--verbose>
594 Give verbose output about what is happening
596 =item B<-h>,B<--help>,B<--usage>
598 Show a 'Usage' message
600 =item B<-d>,B<--debug>
602 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
606 =head1 CONFIGURATION FILE
608 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
609 xmpp-account, with a line of the format:
613 I<user>@I<server> I<password> I<componentname>
620 alice@jabber.org secret
622 ('#' 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:
624 # account with specific connection host
625 alice@myjabberserver.com;foo.com secret
627 You can also add a I<port> if it is not the standard XMPP port:
629 # account with weird port number
630 alice@myjabberserver.com:1234 secret
632 Of course, you may also mix the two:
634 # account with a specific host and port
635 alice@myjabberserver.com;foo.com:1234 secret
637 B<NOTE>: for your security, sendxmpp demands that the configuration
638 file is owned by you and readable only to you (permissions 600).
642 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
644 or to send to a chatroom:
646 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
648 or to send your system logs somewhere, as new lines appear:
650 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
652 NOTE: be careful not the overload public jabber services
656 Documentation for the L<Net::XMPP> module
658 The jabber homepage: L<http://www.jabber.org/>
660 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
664 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
665 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
666 Lubomir Host 'rajo' <rajo AT platon.sk>, L<http://rajo.platon.sk>