Import Debian changes 0.0.7.1-1
[cascardo/sendxmpp.git] / sendxmpp
1 #!/usr/bin/perl -w
2 #-*-mode:perl-*-
3 #Time-stamp: <2005-05-02 01:00:02 (djcb)>
4
5 # script to send message using xmpp (aka jabber), 
6 #   somewhat resembling mail(1)
7
8 # Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
9 # Copyright (c) 2004,2005 Dirk-Jan C. Binnema
10
11 # Released under the terms of the GNU General Public License v2
12
13 use Net::XMPP;
14 use Getopt::Long;
15 use strict;
16
17 # subroutines decls
18 sub xmpp_login($$$$$$);
19 sub xmpp_send ($$$);
20 sub xmpp_send_message($$$$);
21 sub xmpp_send_chatroom_message($$$$$);
22 sub xmpp_logout($);
23 sub xmpp_check_result;
24 sub parse_cmdline();
25 sub error_exit;
26 sub debug_print;
27 sub read_config_file($);
28 sub push_hash($$);
29 sub terminate();
30 sub main();
31
32 my # MakeMaker
33 $VERSION     = '0.0.7.1';
34 my $RESOURCE = 'sendxmpp';
35 my $VERBOSE  = 0;
36 my $DEBUG    = 0;
37              
38 # start!
39 &main;
40
41 #
42 # main: main routine
43 #
44 sub main () {
45
46     my $cmdline = parse_cmdline();
47     
48     $| = 1; # no output buffering
49
50     $DEBUG   = 1 if ($$cmdline{'debug'});
51     $VERBOSE = 1 if ($$cmdline{'verbose'});
52     
53     my $config = read_config_file ($$cmdline{'file'})
54         unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'});        
55     
56     # login to xmpp
57     my $cnx =  xmpp_login ($$cmdline{'jserver'}  || $$config{'jserver'},
58                            $$cmdline{'username'} || $$config{'username'},
59                            $$cmdline{'password'} || $$config{'password'},
60                            $$cmdline{'resource'},
61                            $$cmdline{'tls'},
62                            $$cmdline{'debug'})
63       or error_exit("cannot login: $!");
64     
65    
66     # read message from STDIN or or from -m/--message parameter
67     if (!$$cmdline{interactive}) {
68         
69         # the non-interactive case
70         my $txt;
71         my $message = $$cmdline{'message'}; 
72         if ($message) {
73             open (MSG, "<$message")
74               or error_exit ("cannot open message file '$message': $!");
75             while (<MSG>) {$txt.=$_};
76             close(MSG);
77         }  else  {
78             $txt.=$_ while (<STDIN>);
79         }
80         
81         xmpp_send ($cnx,$cmdline,$txt);
82     
83     } else {
84         # the interactive case, read stdin line by line
85
86         # deal with TERM
87         $main::CNX = $cnx;  
88         $SIG{INT}=\&terminate;
89
90         # line by line...
91         while (<STDIN>) {
92             chomp;
93             xmpp_send ($cnx,$cmdline,$_);
94         }
95     }
96
97     xmpp_logout($cnx);
98     exit 0;
99 }
100
101
102
103 #
104 # read_config_file: read the configuration file
105 # input: filename
106 # output: hash with 'user', 'jserver' and 'password' keys
107 #
108 sub read_config_file ($) {
109    
110     # check permissions
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);
120     
121     open (CFG,"<$cfg_file")
122       or error_exit("cannot open $cfg_file for reading: $!");
123
124     my %config;
125     my $line = 0;
126     while (<CFG>) {
127         
128         ++$line;
129         
130         next if (/^\s*$/);     # ignore empty lines
131         next if (/^\s*\#.*/);  # ignore comment lines
132         
133         s/\#.*$//; # ignore comments in lines
134         
135         if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) {
136             %config = ('username' => $1,
137                        'jserver'  => $2, 
138                        'password' => $3);
139         } else {
140             close CFG;
141             error_exit ("syntax error in line $line of $cfg_file");
142         }
143     }
144     
145     close CFG;
146     
147     error_exit ("no correct config found in $cfg_file") 
148       unless (scalar(%config));       
149
150     if ($DEBUG || $VERBOSE) {
151         while (my ($key,$val) = each %config) {
152             debug_print ("config: '$key' => '$val'");
153         }
154     }       
155     
156     return \%config;               
157 }
158
159
160
161 #
162 # parse_cmdline: parse commandline options
163 # output: hash with commandline options
164 #
165 sub parse_cmdline () {
166     
167     usage() unless (scalar(@ARGV));
168     
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,
179                           'tls|t'          => \$tls,
180                           'interactive|i'  => \$interactive,
181                           'help|usage|h'   => \$help,
182                           'debug|d'        => \$debug,
183                           'verbose|v'      => \$verbose);
184     usage () 
185       if ($help);   
186     
187     my $rcpt = $ARGV[0]
188       or error_exit "no recipient specified";
189  
190    if ($message && $interactive) {
191        error_exit "cannot have both -m (--message) and -i (--interactive)\n";
192    } 
193         
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);
206
207    if ($DEBUG || $VERBOSE) {
208        while (my ($key,$val) = each %dict) {
209            debug_print ("cmdline: '$key' => '$val'");
210        }
211    }        
212     
213    return \%dict;    
214 }
215
216
217 #
218 # xmpp_login: login to the xmpp (jabber) server
219 # input: hostname,username,password,resource,tls,debug
220 # output: an XMPP connection object
221 #
222 sub xmpp_login ($$$$$$) {
223
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: $!"
227         unless ($cnx);    
228
229     my @res = $cnx->Connect(hostname=>$host,tls=>$tls);
230     xmpp_check_result("Connect",\@res,$cnx);
231
232     
233     @res = $cnx->AuthSend('hostname' => $host,
234                           'username' => $user,
235                           'password' => $pw,
236                           'resource' => $res);
237     xmpp_check_result('AuthSend',\@res,$cnx);
238     
239     return $cnx;    
240 }
241
242
243
244
245 #
246 # xmmp_send: send the message, determine from cmdline
247 # whether it's to individual or chatroom
248 #
249 sub xmpp_send ($$$) {
250     
251     my ($cnx, $cmdline, $txt) = @_;
252     
253     unless ($$cmdline{'chatroom'}) {
254         xmpp_send_message ($cnx,
255                            $$cmdline{'recipient'},
256                            $$cmdline{'subject'},
257                            $txt);
258     } else {
259         xmpp_send_chatroom_message ($cnx,
260                                     $$cmdline{'resource'},
261                                     $$cmdline{'subject'},
262                                     $$cmdline{'recipient'},
263                                     $txt);
264     }
265 }
266
267
268
269 #
270 # xmpp_send_message: send a message to some xmpp user
271 # input: connection,recipient,subject,msg
272 #
273 sub xmpp_send_message ($$$$) {
274     
275     my ($cnx,$rcpt,$subject,$msg) = @_;
276  
277     # for some reason, MessageSend does not return anything
278     $cnx->MessageSend('to'      => $rcpt,
279                       'subject' => $subject,
280                       'body'    => $msg);
281     
282     xmpp_check_result('MessageSend',0,$cnx);
283 }
284     
285     
286 #
287 # xmpp_send_chatroom_message: send a message to a chatroom
288 # input: connection,resource,subject,recipient,message
289 #
290 sub xmpp_send_chatroom_message ($$$$$) {
291
292     my ($cnx,$resource,$subject,$rcpt,$msg) =  @_;
293     
294     # set the presence
295     my $pres = new Net::XMPP::Presence;
296     my $res = $pres->SetTo("$rcpt/$resource");
297
298     $cnx->Send($pres); 
299
300     # create/send the message
301     my $groupmsg = new Net::XMPP::Message;
302     $groupmsg->SetMessage(to      => $rcpt, 
303                           body    => $msg,
304                           subject => $subject,
305                           type    => 'groupchat');
306
307     $res = $cnx->Send($groupmsg);
308     xmpp_check_result ('Send',$res,$cnx); 
309     
310     # leave the group
311     $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
312 }
313
314
315 #
316 # xmpp_logout: log out from the xmpp server
317 # input: connection
318 #
319 sub xmpp_logout($) {
320     
321     # HACK
322     # messages may not be received if we log out too quickly...
323     sleep 1; 
324     
325     my $cnx = shift;
326     $cnx->Disconnect();
327     xmpp_check_result ('Disconnect',0); # well, nothing to check, really
328 }
329
330
331
332 #
333 # xmpp_check_result: check the return value from some xmpp function execution
334 # input: text, result, [connection]                   
335 #
336 sub xmpp_check_result {
337
338     my ($txt,$res,$cnx)=@_;
339     
340     error_exit ("Error '$txt': result undefined")
341         unless (defined $res);  
342
343     # res may be 0
344     if ($res == 0) {
345         debug_print "$txt";
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
350     } else {    
351         my $errmsg = $cnx->GetErrorCode() || '?';
352         error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
353     }
354 }
355
356
357 #
358 # terminate; exit the program upon TERM sig reception
359 #
360 sub terminate () {
361     debug_print "caught TERM";
362     xmpp_logout($main::CNX);
363     exit 0;
364 }
365
366
367 #
368 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
369 # input: [array of strings]
370 #
371 sub debug_print {
372     print STDERR "sendxmpp: " . (join ' ', @_) . "\n"
373         if (@_ && ($DEBUG ||$VERBOSE));
374 }
375
376
377 #
378 # error_exit: print error message and exit the program
379 #             logs out if there is a connection 
380 # input: error, [connection]
381 #
382 sub error_exit {
383     
384     my ($err,$cnx) = @_;
385     print STDERR "$err\n";   
386     xmpp_logout ($cnx) 
387         if ($cnx);
388  
389     exit 1;
390 }
391
392
393 #
394 # usage: print short usage message and exit
395 #
396 sub usage () {
397    
398     print STDERR 
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";
402     
403     exit 0;
404 }
405
406
407 #
408 # the fine manual
409 #
410 =pod
411 =head1 NAME
412
413 sendxmpp - send xmpp messages from the commandline.
414
415 =head1 SYNOPSIS
416
417 sendxmpp [options] <recipient>
418
419 =head1 DESCRIPTION
420
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.
423
424 =head1 OPTIONS
425
426 B<-f>,B<--file> <file>
427 use <file> configuration file instead of ~/.sendxmpprc
428
429 B<-u>,B<--username> <user>
430 use <user> instead of the one in the configuration file
431
432 B<-p>,B<--password> <password>
433 use <password> instead of the one in the configuration file
434
435 B<-j>,B<--jserver> <server>
436 use jabber server <server> instead of the one in the configuration file
437
438 B<-r>,B<--resource> <res>
439 use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
440
441 B<-t>,B<--tls>
442 connect securely, using TLS
443
444 B<-c>,B<--chatroom>
445 send the message to a chatroom 
446
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
450
451 B<-m>,B<--message> <message>
452 read the message from <message> (a file) instead of stdin
453
454 B<-i>,B<--interactive>
455 work in interactive mode, reading lines from stdin and sending the one-at-time
456
457 B<-v>,B<--verbose>
458 give verbose output about what is happening
459
460 B<-h>,B<--help>,B<--usage>
461 show a 'Usage' message
462
463 B<-d>,B<--debug>
464 show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
465
466 =head1 CONFIGURATION FILE
467
468 You may define a '~/.sendxmpprc' file with the necessary data for your 
469 xmpp-account, with a line of the format:
470
471    <user>@<host> <password>
472
473 e.g.:
474
475     # my account
476     alice@jabber.org  secret
477
478 ('#' and newlines are allowed like in shellscripts)
479     
480 B<NOTE>: for your security, sendxmpp demands that the configuration
481 file is owned by you and has file permissions 600.
482
483 =head1 EXAMPLE
484
485    $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
486
487      or to send to a chatroom:
488
489    $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org    
490
491 =head1 SEE ALSO
492
493 Documentation for the L<Net::XMPP> module
494
495 The jabber homepage: http://www.jabber.org/
496
497 The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp
498
499 =head1 AUTHOR
500
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.
503
504 =cut