3 eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
6 #Time-stamp: <2005-05-07 19:24:09 (djcb)>
8 # script to send message using xmpp (aka jabber),
9 # somewhat resembling mail(1)
11 # Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
12 # Copyright (c) 2004,2005 Dirk-Jan C. Binnema
14 # Released under the terms of the GNU General Public License v2
21 sub xmpp_login($$$$$$$);
23 sub xmpp_send_message($$$$);
24 sub xmpp_send_chatroom_message($$$$$);
26 sub xmpp_check_result;
30 sub read_config_file($);
37 my $RESOURCE = 'sendxmpp';
49 my $cmdline = parse_cmdline();
51 $| = 1; # no output buffering
53 $DEBUG = 1 if ($$cmdline{'debug'});
54 $VERBOSE = 1 if ($$cmdline{'verbose'});
56 my $config = read_config_file ($$cmdline{'file'})
57 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
60 my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'},
61 $$cmdline{'port'} || $$config{'port'},
62 $$cmdline{'username'} || $$config{'username'},
63 $$cmdline{'password'} || $$config{'password'},
64 $$cmdline{'resource'},
67 or error_exit("cannot login: $!");
70 # read message from STDIN or or from -m/--message parameter
71 if (!$$cmdline{interactive}) {
73 # the non-interactive case
75 my $message = $$cmdline{'message'};
77 open (MSG, "<$message")
78 or error_exit ("cannot open message file '$message': $!");
79 while (<MSG>) {$txt.=$_};
82 $txt.=$_ while (<STDIN>);
85 xmpp_send ($cnx,$cmdline,$txt);
88 # the interactive case, read stdin line by line
92 $SIG{INT}=\&terminate;
97 xmpp_send ($cnx,$cmdline,$_);
108 # read_config_file: read the configuration file
110 # output: hash with 'user', 'jserver' and 'password' keys
112 sub read_config_file ($) {
115 my $cfg_file = shift;
116 error_exit ("cannot read $cfg_file: $!")
117 unless (-r $cfg_file);
118 my $owner = (stat($cfg_file))[4];
119 error_exit ("you must own $cfg_file")
120 unless ($owner == $>);
121 my $mode = (stat($cfg_file))[2] & 07777;
122 error_exit ("$cfg_file must have mode 0600")
123 unless ($mode == 0600);
125 open (CFG,"<$cfg_file")
126 or error_exit("cannot open $cfg_file for reading: $!");
134 next if (/^\s*$/); # ignore empty lines
135 next if (/^\s*\#.*/); # ignore comment lines
137 s/\#.*$//; # ignore comments in lines
139 if (/([-\.\w]+)@([-\.\w:]+)\s+(\S+)\s*$/) {
140 %config = ('username' => $1,
145 if ($config{'jserver'} =~ /(.*):(\d+)/) {
146 $config{'jserver'} = $1;
147 $config{'port'} = $2;
151 error_exit ("syntax error in line $line of $cfg_file");
157 error_exit ("no correct config found in $cfg_file")
158 unless (scalar(%config));
160 if ($DEBUG || $VERBOSE) {
161 while (my ($key,$val) = each %config) {
162 debug_print ("config: '$key' => '$val'");
172 # parse_cmdline: parse commandline options
173 # output: hash with commandline options
175 sub parse_cmdline () {
177 usage() unless (scalar(@ARGV));
179 my ($subject,$file,$resource,$jserver,$port,$username,$password,
180 $message,$chatroom,$debug,$tls,$interactive,$help,$verbose);
181 my $res = GetOptions ('subject|s=s' => \$subject,
182 'file|f=s' => \$file,
183 'resource|r=s' => \$resource,
184 'jserver|j=s' => \$jserver,
185 'username|u=s' => \$username,
186 'password|p=s' => \$password,
187 'message|m=s' => \$message,
188 'chatroom|c' => \$chatroom,
190 'interactive|i' => \$interactive,
191 'help|usage|h' => \$help,
192 'debug|d' => \$debug,
193 'verbose|v' => \$verbose);
198 or error_exit "no recipient specified";
200 if ($message && $interactive) {
201 error_exit "cannot have both -m (--message) and -i (--interactive)\n";
204 if ($jserver && $jserver =~ /(.*):(\d+)/) {
209 my %dict = ('subject' => ($subject or ''),
210 'resource' => ($resource or $RESOURCE),
211 'jserver' => ($jserver or ''),
212 'port' => ($port or 0),
213 'username' => ($username or ''),
214 'password' => ($password or ''),
215 'chatroom' => ($chatroom or 0),
216 'interactive' => ($interactive or 0),
217 'tls' => ($tls or 0),
218 'debug' => ($debug or 0),
219 'verbose' => ($verbose or 0),
220 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
221 'recipient' => $rcpt);
223 if ($DEBUG || $VERBOSE) {
224 while (my ($key,$val) = each %dict) {
225 debug_print ("cmdline: '$key' => '$val'");
234 # xmpp_login: login to the xmpp (jabber) server
235 # input: hostname,port,username,password,resource,tls,debug
236 # output: an XMPP connection object
238 sub xmpp_login ($$$$$$$) {
240 my ($host,$port,$user,$pw,$res,$tls,$debug) = @_;
241 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
242 error_exit "could not create XMPP client object: $!"
247 @res = $cnx->Connect(hostname=>$host,tls=>$tls);
249 @res = $cnx->Connect(hostname=>$host,port=>$port,tls=>$tls);
252 xmpp_check_result("Connect",\@res,$cnx);
254 @res = $cnx->AuthSend('hostname' => $host,
258 xmpp_check_result('AuthSend',\@res,$cnx);
267 # xmmp_send: send the message, determine from cmdline
268 # whether it's to individual or chatroom
270 sub xmpp_send ($$$) {
272 my ($cnx, $cmdline, $txt) = @_;
274 unless ($$cmdline{'chatroom'}) {
275 xmpp_send_message ($cnx,
276 $$cmdline{'recipient'},
277 $$cmdline{'subject'},
280 xmpp_send_chatroom_message ($cnx,
281 $$cmdline{'resource'},
282 $$cmdline{'subject'},
283 $$cmdline{'recipient'},
291 # xmpp_send_message: send a message to some xmpp user
292 # input: connection,recipient,subject,msg
294 sub xmpp_send_message ($$$$) {
296 my ($cnx,$rcpt,$subject,$msg) = @_;
298 # for some reason, MessageSend does not return anything
299 $cnx->MessageSend('to' => $rcpt,
300 'subject' => $subject,
303 xmpp_check_result('MessageSend',0,$cnx);
308 # xmpp_send_chatroom_message: send a message to a chatroom
309 # input: connection,resource,subject,recipient,message
311 sub xmpp_send_chatroom_message ($$$$$) {
313 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
316 my $pres = new Net::XMPP::Presence;
317 my $res = $pres->SetTo("$rcpt/$resource");
321 # create/send the message
322 my $groupmsg = new Net::XMPP::Message;
323 $groupmsg->SetMessage(to => $rcpt,
326 type => 'groupchat');
328 $res = $cnx->Send($groupmsg);
329 xmpp_check_result ('Send',$res,$cnx);
332 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
337 # xmpp_logout: log out from the xmpp server
343 # messages may not be received if we log out too quickly...
348 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
354 # xmpp_check_result: check the return value from some xmpp function execution
355 # input: text, result, [connection]
357 sub xmpp_check_result {
359 my ($txt,$res,$cnx)=@_;
361 error_exit ("Error '$txt': result undefined")
362 unless (defined $res);
367 # result can be true or 'ok'
368 } elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
369 debug_print "$txt: " . $$res[0];
370 # otherwise, there is some error
372 my $errmsg = $cnx->GetErrorCode() || '?';
373 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
379 # terminate; exit the program upon TERM sig reception
382 debug_print "caught TERM";
383 xmpp_logout($main::CNX);
389 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
390 # input: [array of strings]
393 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
394 if (@_ && ($DEBUG ||$VERBOSE));
399 # error_exit: print error message and exit the program
400 # logs out if there is a connection
401 # input: error, [connection]
406 print STDERR "$err\n";
415 # usage: print short usage message and exit
420 "sendxmpp version $VERSION, Copyright (c) 2004,2005 Dirk-Jan C. Binnema\n" .
421 "usage: sendxmpp [options] <recipient>\n" .
422 "or refer to the the sendxmpp manpage\n";
434 sendxmpp - send xmpp messages from the commandline.
438 sendxmpp [options] <recipient>
442 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
443 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
447 B<-f>,B<--file> <file>
448 use <file> configuration file instead of ~/.sendxmpprc
450 B<-u>,B<--username> <user>
451 use <user> instead of the one in the configuration file
453 B<-p>,B<--password> <password>
454 use <password> instead of the one in the configuration file
456 B<-j>,B<--jserver> <server>
457 use jabber server <server> instead of the one in the configuration file. Note that you can add :<port> to use a non-default port, ie. B<-j myjabber.org:1234>
459 B<-r>,B<--resource> <res>
460 use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
463 connect securely, using TLS
466 send the message to a chatroom
468 B<-s>,B<--subject> <subject>
469 set the subject for the message to <subject> [default: '']; when sending to a chatroom,
470 this will set the subject for the chatroom
472 B<-m>,B<--message> <message>
473 read the message from <message> (a file) instead of stdin
475 B<-i>,B<--interactive>
476 work in interactive mode, reading lines from stdin and sending the one-at-time
479 give verbose output about what is happening
481 B<-h>,B<--help>,B<--usage>
482 show a 'Usage' message
485 show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
487 =head1 CONFIGURATION FILE
489 You may define a '~/.sendxmpprc' file with the necessary data for your
490 xmpp-account, with a line of the format:
492 <user>@<host> <password>
497 alice@jabber.org secret
499 ('#' and newlines are allowed like in shellscripts). You can add :<port> to
500 the <host> if you need an alternative port, ie.
502 # account with weird port number
503 alice@myjabberhost.com:1234 secret
505 B<NOTE>: for your security, sendxmpp demands that the configuration
506 file is owned by you and has file permissions 600.
510 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
512 or to send to a chatroom:
514 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
516 or to send your system logs somewhere, as new lines appear:
518 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
520 NOTE: be careful not the overload public jabber services
524 Documentation for the L<Net::XMPP> module
526 The jabber homepage: http://www.jabber.org/
528 The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp
532 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
533 the L<Net::XMPP> modules written by Ryan Eatmon.