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 <lubomir.host@gmail.com>
12 # Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema
13 # Copyright (c) 2006 - 2014 Lubomir Host
15 # Homepage: http://sendxmpp.hostname.sk
17 # Released under the terms of the GNU General Public License v2
20 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($);
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{'sso'} || ($$cmdline{'username'} && $$cmdline{'password'}));
74 my $cnx = xmpp_login (
75 $$cmdline{'jserver'} || $$config{'jserver'},
76 $$cmdline{'port'} || $$config{'port'} || ($$cmdline{'ssl'} ? 5223 : 5222),
77 $$cmdline{'username'} || $$config{'username'},
78 $$cmdline{'password'} || $$config{'password'},
79 $$cmdline{'component'}|| $$config{'component'},
80 $$cmdline{'resource'},
81 $$cmdline{'tls'} || $$config{'tls'} || 0,
82 $$cmdline{'no-tls-verify'} || $$config{'no-tls-verify'},
83 $$cmdline{'tls-ca-path'} || $$config{'tls-ca-path'} || '',
87 ) or error_exit("cannot login: $!");
90 # read message from STDIN or from -m/--message parameter
91 if (!$$cmdline{interactive}) {
92 # the non-interactive case
94 my $message = $$cmdline{'message'};
96 open (MSG, "<$message")
97 or error_exit ("cannot open message file '$message': $!");
98 while (<MSG>) { $txt .= $_ };
102 $txt .= $_ while (<STDIN>);
105 xmpp_send ($cnx,$cmdline,$config,$txt);
109 # the interactive case, read stdin line by line
113 $SIG{INT}=\&terminate;
118 xmpp_send ($cnx,$cmdline,$config,$_);
127 # read_config_file: read the configuration file
129 # output: hash with 'user', 'jserver' and 'password' keys
131 sub read_config_file ($) { # {{{
134 my $cfg_file = shift;
135 error_exit ("cannot read $cfg_file: $!")
136 unless (-r $cfg_file);
137 my $owner = (stat _ )[4];
138 error_exit ("you must own $cfg_file")
139 unless ($owner == $>);
140 my $mode = (stat _ )[2] & 07777;
141 error_exit ("$cfg_file must not be accessible by others")
144 open (CFG,"<$cfg_file")
145 or error_exit("cannot open $cfg_file for reading: $!");
153 next if (/^\s*$/); # ignore empty lines
154 next if (/^\s*\#.*/); # ignore comment lines
156 #s/\#.*$//; # ignore comments in lines
158 if (/^([a-z]+):\s*(.*)$/) {
161 # Hugo van der Kooij <hvdkooij AT vanderkooij.org> has account with '#' as username
162 elsif (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) {
174 error_exit ("syntax error in line $line of $cfg_file");
177 # account with weird port number
178 if (defined($config{'jserver'}) and $config{'jserver'} =~ /(.*):(\d+)/) {
179 $config{'jserver'} = $1;
180 $config{'port'} = $2;
183 # account with specific connection host
184 if (defined($config{'jserver'}) and $config{'jserver'} =~ /(.*);([-\.\w]+)/) {
185 $config{'jserver'} = $2;
186 $config{'username'} .= "\@$1" unless $config{'component'};
192 error_exit ("no correct config found in $cfg_file")
193 unless (scalar(%config));
195 if ($DEBUG || $VERBOSE) {
196 while (my ($key,$val) = each %config) {
197 debug_print ("config: '$key' => '$val'");
205 # parse_cmdline: parse commandline options
206 # output: hash with commandline options
208 sub parse_cmdline () { # {{{
210 usage() unless (scalar(@ARGV));
212 my ($subject, $file, $resource, $jserver, $port, $username, $password, $sso, $component,
213 $message, $chatroom, $headline, $debug, $tls, $ssl,
214 $no_tls_verify, $tls_ca_path,
215 $interactive, $help, $raw, $verbose
218 my $res = GetOptions (
219 'subject|s=s' => \$subject,
220 'file|f=s' => \$file,
221 'resource|r=s' => \$resource,
222 'jserver|j=s' => \$jserver,
223 'component|o=s' => \$component,
224 'username|u=s' => \$username,
225 'password|p=s' => \$password,
227 'message|m=s' => \$message,
228 'headline|l' => \$headline,
229 'message-type=s' => \$message_type,
230 'chatroom|c' => \$chatroom,
232 'no-tls-verify|n' => \$no_tls_verify,
233 'tls-ca-path|a=s' => \$tls_ca_path,
235 'interactive|i' => \$interactive,
236 'help|usage|h' => \$help,
237 'debug|d:i' => sub { $debug = $_[1] ? $_[1] : $debug + 1 },
239 'verbose|v' => \$verbose
246 if (defined($raw) && scalar(@rcpt) > 0) {
247 error_exit("You must give a recipient or --raw (but not both)");
249 if ($raw && $subject) {
250 error_exit("You cannot specify a subject in raw XML mode");
252 if ($raw && $chatroom) {
253 error_exit("The chatroom option is pointless in raw XML mode");
256 if ($message && $interactive) {
257 error_exit("Cannot have both -m (--message) and -i (--interactive)");
260 if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
261 error_exit("Unsupported message type '$message_type'");
265 error_exit("Connect securely wether using -e (--ssl) or -t (--tls)");
268 if ($sso && $username) {
269 error_exit("When using --sso, user should not be specified");
273 # --headline withouth --message-type
274 if ($message_type eq 'message' or $message_type eq 'chat') {
275 $message_type = 'headline'
278 error_exit("Options --headline and --message-type are mutually exclusive");
282 if ($jserver && $jserver =~ /(.*):(\d+)/) {
288 'subject' => ($subject or ''),
289 'message' => ($message or ''),
290 'resource' => ($resource or $RESOURCE),
291 'jserver' => ($jserver or ''),
292 'component' => ($component or ''),
293 'port' => ($port or 0),
294 'username' => ($username or ''),
295 'password' => ($password or ''),
296 'sso' => ($sso or 0),
297 'chatroom' => ($chatroom or 0),
298 'message-type' => $message_type,
299 'interactive' => ($interactive or 0),
300 'tls' => ($tls or 0),
301 'no-tls-verify' => ($no_tls_verify or 0),
302 'tls-ca-path' => ($tls_ca_path or ''),
303 'ssl' => ($ssl or 0),
304 'debug' => ($debug or 0),
305 'verbose' => ($verbose or 0),
306 'raw' => ($raw or 0),
307 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
308 'recipient' => \@rcpt
311 if ($DEBUG || $VERBOSE) {
312 while (my ($key,$val) = each %dict) {
313 debug_print ("cmdline: '$key' => '$val'");
321 # xmpp_login: login to the xmpp (jabber) server
322 # input: hostname,port,username,password,resource,tls,ssl,debug
323 # output: an XMPP connection object
325 sub xmpp_login ($$$$$$$$$$$$) { # {{{
327 my ($host, $port, $user, $pw, $comp, $res, $tls, $no_tls_verify, $tls_ca_path, $ssl, $debug, $sso) = @_;
328 my $cnx = new Net::XMPP::Client(debuglevel=>$debug);
329 error_exit "could not create XMPP client object: $!"
332 my $ssl_verify = 0x01;
333 if ($no_tls_verify) { $ssl_verify = 0x00; }
334 debug_print "ssl_verify: $ssl_verify";
336 debug_print "tls_ca_path: $tls_ca_path";
343 ssl_verify => $ssl_verify,
344 ssl_ca_path => $tls_ca_path,
346 connectiontype => 'tcpip',
347 componentname => $comp
351 $user = join('@', scalar getpwuid($<), Net::Domain::hostdomain());
352 debug_print "using SSO user $user";
355 # use the xmpp domain as the host and enable SRV lookups
357 if ($user =~ /@(.*)/) {
358 $arghash->{hostname} = $host = $1;
360 debug_print "enabling SRV lookups";
363 error_exit "unable to determine a host to connect to (no cmdline, no config, no SRV possible)";
368 delete $arghash->{port} unless $port;
369 if ($arghash->{port}) {
370 @res = $cnx->Connect(%$arghash);
371 error_exit ("Could not connect to '$host' on port $port: ".($cnx->GetErrorCode()||$@)) unless @res;
373 @res = $cnx->Connect(%$arghash);
374 error_exit ("Could not connect to server '$host': ".($cnx->GetErrorCode()||$@)) unless @res;
377 xmpp_check_result("Connect",\@res,$cnx);
380 my $sid = $cnx->{SESSION}->{id};
381 $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp
384 @res = $cnx->AuthSend(#'hostname' => $host,
388 xmpp_check_result('AuthSend',\@res,$cnx);
394 # xmmp_send: send the message, determine from cmdline
395 # whether it's to individual or chatroom
397 sub xmpp_send ($$$$) { # {{{
399 my ($cnx, $cmdline, $config, $txt) = @_;
401 unless ($$cmdline{'chatroom'}) {
402 unless ($$cmdline{'raw'}) {
404 xmpp_send_message ($cnx,
405 $_, #$$cmdline{'recipient'},
406 $$cmdline{'component'} || $$config{'component'},
407 $$cmdline{'subject'},
408 $$cmdline{'message-type'},
410 } @{$$cmdline{'recipient'}};
413 xmpp_send_raw_xml ($cnx, $txt);
418 xmpp_send_chatroom_message ($cnx,
419 $$cmdline{'resource'},
420 $$cmdline{'subject'},
421 $_, # $$cmdline{'recipient'},
423 } @{$$cmdline{'recipient'}};
428 # xmpp_send_raw_xml: send a raw XML packet
429 # input: connection,packet
431 sub xmpp_send_raw_xml ($$) { # {{{
433 my ($cnx,$packet) = @_;
435 # for some reason, Send does not return anything
437 xmpp_check_result('Send',0,$cnx);
441 # xmpp_send_message: send a message to some xmpp user
442 # input: connection,recipient,subject,msg
444 sub xmpp_send_message ($$$$$$) { # {{{
446 my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
448 # for some reason, MessageSend does not return anything
449 # mimeit01@xmpp.hs-esslingen.de: if $comp IS set, AND the rcpt DOESN'T contain an @, then @comp is added
450 $cnx->MessageSend('to' => $rcpt . ( ($comp && index($rcpt, "@") == -1) ? "\@$comp" : '' ),
451 'type' => $message_type,
452 'subject' => $subject,
455 xmpp_check_result('MessageSend',0,$cnx);
459 # xmpp_send_chatroom_message: send a message to a chatroom
460 # input: connection,resource,subject,recipient,message
462 sub xmpp_send_chatroom_message ($$$$$) { # {{{
464 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
467 my $pres = new Net::XMPP::Presence;
468 my $res = $pres->SetTo("$rcpt/$resource");
472 # create/send the message
473 my $groupmsg = new Net::XMPP::Message;
474 $groupmsg->SetMessage(to => $rcpt,
476 type => 'groupchat');
478 $res = $cnx->Send($groupmsg);
479 xmpp_check_result ('Send',$res,$cnx);
482 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
486 # xmpp_logout: log out from the xmpp server
489 sub xmpp_logout($) { # {{{
492 # messages may not be received if we log out too quickly...
497 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
501 # xmpp_check_result: check the return value from some xmpp function execution
502 # input: text, result, [connection]
504 sub xmpp_check_result { # {{{
505 my ($txt, $res, $cnx)=@_;
507 error_exit ("Error '$txt': result undefined")
508 unless (defined $res);
513 # result can be true or 'ok'
515 elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
516 debug_print "$txt: " . $$res[0];
517 # otherwise, there is some error
520 my $errmsg = $cnx->GetErrorCode() || '?';
521 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
526 # terminate; exit the program upon TERM sig reception
528 sub terminate () { # {{{
529 debug_print "caught TERM";
530 xmpp_logout($main::CNX);
535 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
536 # input: [array of strings]
538 sub debug_print { # {{{
539 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
540 if (@_ && ($DEBUG ||$VERBOSE));
544 # error_exit: print error message and exit the program
545 # logs out if there is a connection
546 # input: error, [connection]
548 sub error_exit { # {{{
551 print STDERR "$err\n";
559 # usage: print short usage message and exit
564 "sendxmpp version $VERSION\n" .
565 "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" .
566 "Copyright (c) 2006 - 2014 Lubomir Host\n" .
567 "usage: sendxmpp [options] <recipient1> [<recipient2> ...]\n" .
568 "or refer to the the sendxmpp manpage\n";
580 sendxmpp - send xmpp messages from the commandline.
584 sendxmpp [options] <recipient1> [<recipient2> ...]
586 sendxmpp --raw [options]
590 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
591 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
597 =item B<-f>,B<--file> I<file>
599 Use I<file> configuration file instead of F<~/.sendxmpprc>
601 =item B<-u>,B<--username> I<user>
603 Use I<user> instead of the one in the configuration file
605 =item B<-p>,B<--password> I<password>
607 Use I<password> instead of the one in the configuration file
611 Instead of specifying username or password, attempt to use system level SSO (e.g. kerberos) if supported.
613 =item B<-j>,B<--jserver> I<server>
615 Use jabber I<server> instead of the one in the configuration file.
617 =item B<-o>,B<--component> I<componentname>
619 Use componentname in connect call. Seems needed for Google talk.
621 =item B<-r>,B<--resource> I<res>
623 Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
627 Connect securely, using TLS
631 Connect securely, using SSL
633 =item B<-n>,B<--no-tls-verify>
635 Deactivate the verification of SSL certificates. Better way is to use parameter B<--tls-ca-path> with the needed path to CA certificates.
637 =item B<-a>,B<--tls-ca-path>
639 Path to your custom CA certificates, so you can verificate SSL certificates during connecting.
641 =item B<-l>,B<--headline>
643 Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages)
645 =item B<--messages-type>
647 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>
649 =item B<-c>,B<--chatroom>
651 Send the message to a chatroom
653 =item B<-s>,B<--subject> I<subject>
655 Set the subject for the message to I<subject> [default: '']; when sending to a chatroom, this will set the subject for the chatroom
657 =item B<-m>,B<--message> I<message>
659 Read the message from I<message> (a file) instead of stdin
661 =item B<-i>,B<--interactive>
663 Work in interactive mode, reading lines from stdin and sending the one-at-time
667 Send raw XML message to jabber server
669 =item B<-v>,B<--verbose>
671 Give verbose output about what is happening
673 =item B<-h>,B<--help>,B<--usage>
675 Show a 'Usage' message
677 =item B<-d>,B<--debug>
679 Show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output! Specify multiple times to increase debug level.
683 =head1 CONFIGURATION FILE
685 You may define a 'F<~/.sendxmpprc>' file with the necessary data for your
686 xmpp-account. Since version 1.24 the following format is supported:
688 username: I<your_username>
689 jserver: I<jabber_server>
691 password: I<your_jabber_password>
692 component: I<optional_component_name>
695 Example for Google Talk servers:
697 username: I<lubomir.host>
698 jserver: I<talk.google.com>
699 password: I<my-secure-password>
700 component: I<gmail.com>
702 With version 1.23 and older only one-line format is supported:
706 I<user>@I<server> I<password> I<componentname>
713 alice@jabber.org secret
715 ('#' 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:
717 # account with specific connection host
718 alice@myjabberserver.com;foo.com secret
720 You can also add a I<port> if it is not the standard XMPP port:
722 # account with weird port number
723 alice@myjabberserver.com:1234 secret
725 Of course, you may also mix the two:
727 # account with a specific host and port
728 alice@myjabberserver.com;foo.com:1234 secret
730 B<NOTE>: for your security, sendxmpp demands that the configuration
731 file is owned by you and readable only to you (permissions 600).
735 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
737 or to send to a chatroom:
739 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
741 or to send your system logs somewhere, as new lines appear:
743 $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
745 NOTE: be careful not the overload public jabber services
749 Documentation for the L<Net::XMPP> module
751 The jabber homepage: L<http://www.jabber.org/>
753 The sendxmpp homepage: L<http://sendxmpp.hostname.sk>
757 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
758 the L<Net::XMPP> modules written by Ryan Eatmon. Current maintainer is
759 Lubomir Host <lubomir.host@gmail.com>, L<http://blog.hostname.sk>
762 # vim: fdm=marker fdl=0 fdc=3