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@djcbsoftware.nl>
11 # Copyright (c) 2004, 2005 Dirk-Jan C. Binnema
13 # Maintainer: Lubomir Host 'rajo' <rajo AT platon.sk>
14 # Homepage: http://sendxmpp.platon.sk
16 # Released under the terms of the GNU General Public License v2
27 sub xmpp_login($$$$$$$);
29 sub xmpp_send_raw_xml($$);
30 sub xmpp_send_message($$$$);
31 sub xmpp_send_chatroom_message($$$$$);
33 sub xmpp_check_result;
37 sub read_config_file($);
44 my $RESOURCE = 'sendxmpp';
56 my $cmdline = parse_cmdline();
58 $| = 1; # no output buffering
60 $DEBUG = 1 if ($$cmdline{'debug'});
61 $VERBOSE = 1 if ($$cmdline{'verbose'});
63 my $config = read_config_file ($$cmdline{'file'})
64 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
67 my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'},
68 $$cmdline{'port'} || $$config{'port'},
69 $$cmdline{'username'} || $$config{'username'},
70 $$cmdline{'password'} || $$config{'password'},
71 $$cmdline{'resource'},
74 or error_exit("cannot login: $!");
77 # read message from STDIN or or from -m/--message parameter
78 if (!$$cmdline{interactive}) {
80 # the non-interactive case
82 my $message = $$cmdline{'message'};
84 open (MSG, "<$message")
85 or error_exit ("cannot open message file '$message': $!");
86 while (<MSG>) {$txt.=$_};
89 $txt.=$_ while (<STDIN>);
92 xmpp_send ($cnx,$cmdline,$txt);
95 # the interactive case, read stdin line by line
99 $SIG{INT}=\&terminate;
104 xmpp_send ($cnx,$cmdline,$_);
115 # read_config_file: read the configuration file
117 # output: hash with 'user', 'jserver' and 'password' keys
119 sub read_config_file ($) {
122 my $cfg_file = shift;
123 error_exit ("cannot read $cfg_file: $!")
124 unless (-r $cfg_file);
125 my $owner = (stat($cfg_file))[4];
126 error_exit ("you must own $cfg_file")
127 unless ($owner == $>);
128 my $mode = (stat($cfg_file))[2] & 07777;
129 error_exit ("$cfg_file must have mode 0600")
130 unless ($mode == 0600);
132 open (CFG,"<$cfg_file")
133 or error_exit("cannot open $cfg_file for reading: $!");
141 next if (/^\s*$/); # ignore empty lines
142 next if (/^\s*\#.*/); # ignore comment lines
144 s/\#.*$//; # ignore comments in lines
146 if (/([-\.\w]+)@([-\.\w:]+)\s+(\S+)\s*$/) {
147 %config = ('username' => $1,
152 if ($config{'jserver'} =~ /(.*):(\d+)/) {
153 $config{'jserver'} = $1;
154 $config{'port'} = $2;
158 error_exit ("syntax error in line $line of $cfg_file");
164 error_exit ("no correct config found in $cfg_file")
165 unless (scalar(%config));
167 if ($DEBUG || $VERBOSE) {
168 while (my ($key,$val) = each %config) {
169 debug_print ("config: '$key' => '$val'");
179 # parse_cmdline: parse commandline options
180 # output: hash with commandline options
182 sub parse_cmdline () {
184 usage() unless (scalar(@ARGV));
186 my ($subject,$file,$resource,$jserver,$port,$username,$password,
187 $message, $chatroom, $debug, $tls, $interactive, $help, $raw, $verbose);
188 my $res = GetOptions ('subject|s=s' => \$subject,
189 'file|f=s' => \$file,
190 'resource|r=s' => \$resource,
191 'jserver|j=s' => \$jserver,
192 'username|u=s' => \$username,
193 'password|p=s' => \$password,
194 'message|m=s' => \$message,
195 'chatroom|c' => \$chatroom,
197 'interactive|i' => \$interactive,
198 'help|usage|h' => \$help,
199 'debug|d' => \$debug,
201 'verbose|v' => \$verbose);
206 if (defined($raw) && scalar(@rcpt) > 0) {
207 error_exit "You must give a recipient or --raw (but not both)";
209 if ($raw && $subject) {
210 error_exit("You cannot specify a subject in raw XML mode");
212 if ($raw && $chatroom) {
213 error_exit("The chatroom option is pointless in raw XML mode");
216 if ($message && $interactive) {
217 error_exit "Cannot have both -m (--message) and -i (--interactive)";
220 if ($jserver && $jserver =~ /(.*):(\d+)/) {
225 my %dict = ('subject' => ($subject or ''),
226 'message' => ($message or ''),
227 'resource' => ($resource or $RESOURCE),
228 'jserver' => ($jserver or ''),
229 'port' => ($port or 0),
230 'username' => ($username or ''),
231 'password' => ($password or ''),
232 'chatroom' => ($chatroom or 0),
233 'interactive' => ($interactive or 0),
234 'tls' => ($tls or 0),
235 'debug' => ($debug or 0),
236 'verbose' => ($verbose or 0),
237 'raw' => ($raw or 0),
238 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
239 'recipient' => \@rcpt);
241 if ($DEBUG || $VERBOSE) {
242 while (my ($key,$val) = each %dict) {
243 debug_print ("cmdline: '$key' => '$val'");
252 # xmpp_login: login to the xmpp (jabber) server
253 # input: hostname,port,username,password,resource,tls,debug
254 # output: an XMPP connection object
256 sub xmpp_login ($$$$$$$) {
258 my ($host,$port,$user,$pw,$res,$tls,$debug) = @_;
259 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
260 error_exit "could not create XMPP client object: $!"
265 @res = $cnx->Connect(hostname => $host, tls => $tls);
266 error_exit ("Could not connect to server '$host': $@") unless @res;
268 @res = $cnx->Connect(hostname => $host, port => $port, tls => $tls);
269 error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
272 xmpp_check_result("Connect",\@res,$cnx);
274 @res = $cnx->AuthSend('hostname' => $host,
278 xmpp_check_result('AuthSend',\@res,$cnx);
287 # xmmp_send: send the message, determine from cmdline
288 # whether it's to individual or chatroom
290 sub xmpp_send ($$$) {
292 my ($cnx, $cmdline, $txt) = @_;
294 unless ($$cmdline{'chatroom'}) {
295 unless ($$cmdline{'raw'}) {
297 xmpp_send_message ($cnx,
298 $_, #$$cmdline{'recipient'},
299 $$cmdline{'subject'},
301 } @{$$cmdline{'recipient'}};
304 xmpp_send_raw_xml ($cnx, $txt);
309 xmpp_send_chatroom_message ($cnx,
310 $$cmdline{'resource'},
311 $$cmdline{'subject'},
312 $_, # $$cmdline{'recipient'},
314 } @{$$cmdline{'recipient'}};
321 # xmpp_send_raw_xml: send a raw XML packet
322 # input: connection,packet
324 sub xmpp_send_raw_xml ($$) {
326 my ($cnx,$packet) = @_;
328 # for some reason, Send does not return anything
330 xmpp_check_result('Send',0,$cnx);
335 # xmpp_send_message: send a message to some xmpp user
336 # input: connection,recipient,subject,msg
338 sub xmpp_send_message ($$$$) {
340 my ($cnx,$rcpt,$subject,$msg) = @_;
342 # for some reason, MessageSend does not return anything
343 $cnx->MessageSend('to' => $rcpt,
344 'subject' => $subject,
347 xmpp_check_result('MessageSend',0,$cnx);
352 # xmpp_send_chatroom_message: send a message to a chatroom
353 # input: connection,resource,subject,recipient,message
355 sub xmpp_send_chatroom_message ($$$$$) {
357 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
360 my $pres = new Net::XMPP::Presence;
361 my $res = $pres->SetTo("$rcpt/$resource");
365 # create/send the message
366 my $groupmsg = new Net::XMPP::Message;
367 $groupmsg->SetMessage(to => $rcpt,
370 type => 'groupchat');
372 $res = $cnx->Send($groupmsg);
373 xmpp_check_result ('Send',$res,$cnx);
376 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
381 # xmpp_logout: log out from the xmpp server
387 # messages may not be received if we log out too quickly...
392 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
398 # xmpp_check_result: check the return value from some xmpp function execution
399 # input: text, result, [connection]
401 sub xmpp_check_result {
403 my ($txt,$res,$cnx)=@_;
405 error_exit ("Error '$txt': result undefined")
406 unless (defined $res);
411 # result can be true or 'ok'
412 } elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
413 debug_print "$txt: " . $$res[0];
414 # otherwise, there is some error
416 my $errmsg = $cnx->GetErrorCode() || '?';
417 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
423 # terminate; exit the program upon TERM sig reception
426 debug_print "caught TERM";
427 xmpp_logout($main::CNX);
433 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
434 # input: [array of strings]
437 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
438 if (@_ && ($DEBUG ||$VERBOSE));
443 # error_exit: print error message and exit the program
444 # logs out if there is a connection
445 # input: error, [connection]
450 print STDERR "$err\n";
459 # usage: print short usage message and exit
464 "sendxmpp version $VERSION, Copyright (c) 2004, 2005 Dirk-Jan C. Binnema\n" .
465 "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
466 "or refer to the the sendxmpp manpage\n";
478 sendxmpp - send xmpp messages from the commandline.
482 sendxmpp [options] <recipient1> [<recipient2> ...]
484 sendxmpp --raw [options]
488 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
489 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
495 =item B<-f>,B<--file> I<file>
497 Use I<file> configuration file instead of F<~/.sendxmpprc>
499 =item B<-u>,B<--username> I<user>
501 Use I<user> instead of the one in the configuration file
503 =item B<-p>,B<--password> I<password>
505 Use I<password> instead of the one in the configuration file
507 =item B<-j>,B<--jserver> I<server>
509 Use jabber I<server> instead of the one in the configuration file.
511 =item B<-r>,B<--resource> I<res>
513 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
517 Connect securely, using TLS
519 =item B<-c>,B<--chatroom>
521 Send the message to a chatroom
523 =item B<-s>,B<--subject> I<subject>
525 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
527 =item B<-m>,B<--message> I<message>
529 Read the message from I<message> (a file) instead of stdin
531 =item B<-i>,B<--interactive>
533 Work in interactive mode, reading lines from stdin and sending the one-at-time
537 Send raw XML message to jabber server
539 =item B<-v>,B<--verbose>
541 Give verbose output about what is happening
543 =item B<-h>,B<--help>,B<--usage>
545 Show a 'Usage' message
547 =item B<-d>,B<--debug>
549 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
553 =head1 CONFIGURATION FILE
555 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
556 xmpp-account, with a line of the format:
560 I<user>@I<server> I<password>
567 alice@jabber.org secret
569 ('#' 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:
571 # account with specific connection host
572 alice@myjabberserver.com;foo.com secret
574 You can also add a I<port> if it is not the standard XMPP port:
576 # account with weird port number
577 alice@myjabberserver.com:1234 secret
579 Of course, you may also mix the two:
581 # account with a specific host and port
582 alice@myjabberserver.com;foo.com:1234 secret
584 B<NOTE>: for your security, sendxmpp demands that the configuration
585 file is owned by you and has file permissions 600.
589 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
591 or to send to a chatroom:
593 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
595 or to send your system logs somewhere, as new lines appear:
597 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
599 NOTE: be careful not the overload public jabber services
603 Documentation for the L<Net::XMPP> module
605 The jabber homepage: L<http://www.jabber.org/>
607 The sendxmpp homepage: L<http://sendxmpp.platon.sk>
611 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
612 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
613 Lubomir Host 'rajo' <rajo AT platon.sk>, L<http://rajo.platon.sk>