3 #Time-stamp: <2005-05-02 01:00:02 (djcb)>
5 # script to send message using xmpp (aka jabber),
6 # somewhat resembling mail(1)
8 # Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
9 # Copyright (c) 2004,2005 Dirk-Jan C. Binnema
11 # Released under the terms of the GNU General Public License v2
18 sub xmpp_login($$$$$$);
20 sub xmpp_send_message($$$$);
21 sub xmpp_send_chatroom_message($$$$$);
23 sub xmpp_check_result;
27 sub read_config_file($);
34 my $RESOURCE = 'sendxmpp';
46 my $cmdline = parse_cmdline();
48 $| = 1; # no output buffering
50 $DEBUG = 1 if ($$cmdline{'debug'});
51 $VERBOSE = 1 if ($$cmdline{'verbose'});
53 my $config = read_config_file ($$cmdline{'file'})
54 unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});
57 my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'},
58 $$cmdline{'username'} || $$config{'username'},
59 $$cmdline{'password'} || $$config{'password'},
60 $$cmdline{'resource'},
63 or error_exit("cannot login: $!");
66 # read message from STDIN or or from -m/--message parameter
67 if (!$$cmdline{interactive}) {
69 # the non-interactive case
71 my $message = $$cmdline{'message'};
73 open (MSG, "<$message")
74 or error_exit ("cannot open message file '$message': $!");
75 while (<MSG>) {$txt.=$_};
78 $txt.=$_ while (<STDIN>);
81 xmpp_send ($cnx,$cmdline,$txt);
84 # the interactive case, read stdin line by line
88 $SIG{INT}=\&terminate;
93 xmpp_send ($cnx,$cmdline,$_);
104 # read_config_file: read the configuration file
106 # output: hash with 'user', 'jserver' and 'password' keys
108 sub read_config_file ($) {
111 my $cfg_file = shift;
112 error_exit ("cannot read $cfg_file: $!")
113 unless (-r $cfg_file);
114 my $owner = (stat($cfg_file))[4];
115 error_exit ("you must own $cfg_file")
116 unless ($owner == $>);
117 my $mode = (stat($cfg_file))[2] & 07777;
118 error_exit ("$cfg_file must have mode 0600")
119 unless ($mode == 0600);
121 open (CFG,"<$cfg_file")
122 or error_exit("cannot open $cfg_file for reading: $!");
130 next if (/^\s*$/); # ignore empty lines
131 next if (/^\s*\#.*/); # ignore comment lines
133 s/\#.*$//; # ignore comments in lines
135 if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) {
136 %config = ('username' => $1,
141 error_exit ("syntax error in line $line of $cfg_file");
147 error_exit ("no correct config found in $cfg_file")
148 unless (scalar(%config));
150 if ($DEBUG || $VERBOSE) {
151 while (my ($key,$val) = each %config) {
152 debug_print ("config: '$key' => '$val'");
162 # parse_cmdline: parse commandline options
163 # output: hash with commandline options
165 sub parse_cmdline () {
167 usage() unless (scalar(@ARGV));
169 my ($subject,$file,$resource,$jserver,$username,$password,
170 $message,$chatroom,$debug,$tls,$interactive,$help,$verbose);
171 my $res = GetOptions ('subject|s=s' => \$subject,
172 'file|f=s' => \$file,
173 'resource|r=s' => \$resource,
174 'jserver|j=s' => \$jserver,
175 'username|u=s' => \$username,
176 'password|p=s' => \$password,
177 'message|m=s' => \$message,
178 'chatroom|c' => \$chatroom,
180 'interactive|i' => \$interactive,
181 'help|usage|h' => \$help,
182 'debug|d' => \$debug,
183 'verbose|v' => \$verbose);
188 or error_exit "no recipient specified";
190 if ($message && $interactive) {
191 error_exit "cannot have both -m (--message) and -i (--interactive)\n";
194 my %dict = ('subject' => ($subject or ''),
195 'resource' => ($resource or $RESOURCE),
196 'jserver' => ($jserver or ''),
197 'username' => ($username or ''),
198 'password' => ($password or ''),
199 'chatroom' => ($chatroom or 0),
200 'interactive' => ($interactive or 0),
201 'tls' => ($tls or 0),
202 'debug' => ($debug or 0),
203 'verbose' => ($verbose or 0),
204 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
205 'recipient' => $rcpt);
207 if ($DEBUG || $VERBOSE) {
208 while (my ($key,$val) = each %dict) {
209 debug_print ("cmdline: '$key' => '$val'");
218 # xmpp_login: login to the xmpp (jabber) server
219 # input: hostname,username,password,resource,tls,debug
220 # output: an XMPP connection object
222 sub xmpp_login ($$$$$$) {
224 my ($host,$user,$pw,$res,$tls,$debug) = @_;
225 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
226 error_exit "could not create XMPP client object: $!"
229 my @res = $cnx->Connect(hostname=>$host,tls=>$tls);
230 xmpp_check_result("Connect",\@res,$cnx);
233 @res = $cnx->AuthSend('hostname' => $host,
237 xmpp_check_result('AuthSend',\@res,$cnx);
246 # xmmp_send: send the message, determine from cmdline
247 # whether it's to individual or chatroom
249 sub xmpp_send ($$$) {
251 my ($cnx, $cmdline, $txt) = @_;
253 unless ($$cmdline{'chatroom'}) {
254 xmpp_send_message ($cnx,
255 $$cmdline{'recipient'},
256 $$cmdline{'subject'},
259 xmpp_send_chatroom_message ($cnx,
260 $$cmdline{'resource'},
261 $$cmdline{'subject'},
262 $$cmdline{'recipient'},
270 # xmpp_send_message: send a message to some xmpp user
271 # input: connection,recipient,subject,msg
273 sub xmpp_send_message ($$$$) {
275 my ($cnx,$rcpt,$subject,$msg) = @_;
277 # for some reason, MessageSend does not return anything
278 $cnx->MessageSend('to' => $rcpt,
279 'subject' => $subject,
282 xmpp_check_result('MessageSend',0,$cnx);
287 # xmpp_send_chatroom_message: send a message to a chatroom
288 # input: connection,resource,subject,recipient,message
290 sub xmpp_send_chatroom_message ($$$$$) {
292 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
295 my $pres = new Net::XMPP::Presence;
296 my $res = $pres->SetTo("$rcpt/$resource");
300 # create/send the message
301 my $groupmsg = new Net::XMPP::Message;
302 $groupmsg->SetMessage(to => $rcpt,
305 type => 'groupchat');
307 $res = $cnx->Send($groupmsg);
308 xmpp_check_result ('Send',$res,$cnx);
311 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
316 # xmpp_logout: log out from the xmpp server
322 # messages may not be received if we log out too quickly...
327 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
333 # xmpp_check_result: check the return value from some xmpp function execution
334 # input: text, result, [connection]
336 sub xmpp_check_result {
338 my ($txt,$res,$cnx)=@_;
340 error_exit ("Error '$txt': result undefined")
341 unless (defined $res);
346 # result can be true or 'ok'
347 } elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
348 debug_print "$txt: " . $$res[0];
349 # otherwise, there is some error
351 my $errmsg = $cnx->GetErrorCode() || '?';
352 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
358 # terminate; exit the program upon TERM sig reception
361 debug_print "caught TERM";
362 xmpp_logout($main::CNX);
368 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
369 # input: [array of strings]
372 print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
373 if (@_ && ($DEBUG ||$VERBOSE));
378 # error_exit: print error message and exit the program
379 # logs out if there is a connection
380 # input: error, [connection]
385 print STDERR "$err\n";
394 # usage: print short usage message and exit
399 "sendxmpp version $VERSION, Copyright (c) 2004,2005 Dirk-Jan C. Binnema\n" .
400 "usage: sendxmpp [options] <recipient>\n" .
401 "or refer to the the sendxmpp manpage\n";
413 sendxmpp - send xmpp messages from the commandline.
417 sendxmpp [options] <recipient>
421 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
422 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
426 B<-f>,B<--file> <file>
427 use <file> configuration file instead of ~/.sendxmpprc
429 B<-u>,B<--username> <user>
430 use <user> instead of the one in the configuration file
432 B<-p>,B<--password> <password>
433 use <password> instead of the one in the configuration file
435 B<-j>,B<--jserver> <server>
436 use jabber server <server> instead of the one in the configuration file
438 B<-r>,B<--resource> <res>
439 use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
442 connect securely, using TLS
445 send the message to a chatroom
447 B<-s>,B<--subject> <subject>
448 set the subject for the message to <subject> [default: '']; when sending to a chatroom,
449 this will set the subject for the chatroom
451 B<-m>,B<--message> <message>
452 read the message from <message> (a file) instead of stdin
454 B<-i>,B<--interactive>
455 work in interactive mode, reading lines from stdin and sending the one-at-time
458 give verbose output about what is happening
460 B<-h>,B<--help>,B<--usage>
461 show a 'Usage' message
464 show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
466 =head1 CONFIGURATION FILE
468 You may define a '~/.sendxmpprc' file with the necessary data for your
469 xmpp-account, with a line of the format:
471 <user>@<host> <password>
476 alice@jabber.org secret
478 ('#' and newlines are allowed like in shellscripts)
480 B<NOTE>: for your security, sendxmpp demands that the configuration
481 file is owned by you and has file permissions 600.
485 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
487 or to send to a chatroom:
489 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
493 Documentation for the L<Net::XMPP> module
495 The jabber homepage: http://www.jabber.org/
497 The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp
501 sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses
502 the L<Net::XMPP> modules written by Ryan Eatmon.