3 #Time-stamp: <2004-12-01 14:52:47 (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, Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
11 # released under the terms of the GNU General Public License v2
19 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';
48 my $cmdline = parse_cmdline();
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 unless ($$cmdline{'chatroom'}) {
67 xmpp_send_message ($cnx,
68 $$cmdline{'recipient'},
70 $$cmdline{'message'});
72 xmpp_send_chatroom_message ($cnx,
73 $$cmdline{'resource'},
75 $$cmdline{'recipient'},
76 $$cmdline{'message'});
85 # read_config_file: read the configuration file
87 # output: hash with 'user', 'jserver' and 'password' keys
89 sub read_config_file ($) {
93 error_exit ("cannot read $cfg_file: $!")
94 unless (-r $cfg_file);
95 my $owner = (stat($cfg_file))[4];
96 error_exit ("you must own $cfg_file")
97 unless ($owner == $>);
98 my $mode = (stat($cfg_file))[2] & 07777;
99 error_exit ("$cfg_file must have mode 0600")
100 unless ($mode == 0600);
102 open (CFG,"<$cfg_file")
103 or error_exit("cannot open $cfg_file for reading: $!");
111 next if (/^\s*$/); # ignore empty lines
112 next if (/^\s*\#.*/); # ignore comment lines
114 s/\#.*$//; # ignore comments in lines
116 if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) {
117 %config = ('username'=>$1,
123 error_exit ("syntax error in line $line of $cfg_file");
129 error_exit ("no correct config found in $cfg_file")
130 unless (scalar(%config));
132 if ($DEBUG || $VERBOSE) {
133 while (my ($key,$val) = each %config) {
134 debug_print ("config: '$key' => '$val'");
144 # parse_cmdline: parse commandline options
145 # output: hash with commandline options
147 sub parse_cmdline () {
149 usage() unless (scalar(@ARGV));
151 my ($subject,$file,$resource,$jserver,$username,$password,
152 $message,$chatroom,$debug,$tls,$help,$verbose);
153 my $res = GetOptions ('subject|s=s' => \$subject,
154 'file|f=s' => \$file,
155 'resource|r=s' => \$resource,
156 'jserver|j=s' => \$jserver,
157 'username|u=s' => \$username,
158 'password|p=s' => \$password,
159 'message|m=s' => \$message,
160 'chatroom|c' => \$chatroom,
162 'help|usage|h' => \$help,
163 'debug|d' => \$debug,
164 'verbose|v' => \$verbose);
169 or error_exit("no recipient specified");
171 # read message from STDIN or or from -m/--message parameter
174 open (MSG, "<$message")
175 or error_exit ("cannot open message file '$message': $!");
176 while (<MSG>) {$txt.=$_};
179 while (<STDIN>) {$txt.=$_};
182 my %dict = ('subject' => ($subject or ''),
183 'resource' => ($resource or $RESOURCE),
184 'jserver' => ($jserver or ''),
185 'username' => ($username or ''),
186 'password' => ($password or ''),
187 'chatroom' => ($chatroom or 0),
188 'tls' => ($tls or 0),
189 'debug' => ($debug or 0),
190 'message' => ($txt or ''),
191 'verbose' => ($verbose or 0),
192 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
193 'recipient' => $rcpt);
195 if ($DEBUG || $VERBOSE) {
196 while (my ($key,$val) = each %dict) {
197 debug_print ("cmdline: '$key' => '$val'");
207 # xmpp_login: login to the xmmp (jabber) server
208 # input: hostname,username,password,resource,tls,debug
209 # output: an XMPP connection object
211 sub xmpp_login ($$$$$$) {
213 my ($host,$user,$pw,$res,$tls,$debug) = @_;
214 my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
215 error_exit ("could not create XMPP client object: $!")
218 my @res = $cnx->Connect(hostname=>$host,tls=>$tls);
219 xmpp_check_result("Connect",\@res,$cnx);
222 @res = $cnx->AuthSend('hostname'=>$host,
226 xmpp_check_result('AuthSend',\@res,$cnx);
228 #@res = $cnx->PresenceSend(type=>'unavailable');
229 #mpp_check_result("PresenceSend",\@res,$cnx);
236 # xmmp_send_message: send a message to some xmmp user
237 # input: connection,recipient,subject,msg
239 sub xmpp_send_message ($$$$) {
241 my ($cnx,$rcpt,$subject,$msg) = @_;
243 # for some reason, MessageSend does not return anything
244 $cnx->MessageSend('to'=>$rcpt,
248 xmpp_check_result('MessageSend',0,$cnx);
253 # xmpp_send_chatroom_message: send a message to a chatroom
254 # input: connection,resource,subject,recipient,message
256 sub xmpp_send_chatroom_message ($$$$$) {
258 my ($cnx,$resource,$subject,$rcpt,$msg) = @_;
261 my $pres = new Net::XMPP::Presence;
262 my $res = $pres->SetTo("$rcpt/$resource");
266 # create/send the message
267 my $groupmsg = new Net::XMPP::Message;
268 $groupmsg->SetMessage(to=>$rcpt,
273 $res = $cnx->Send($groupmsg);
274 xmpp_check_result ('Send',$res,$cnx);
277 $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
282 # xmmp_logout: log out from the xmpp server
288 # messages may not be received if we log out too quickly...
293 xmpp_check_result ('Disconnect',0); # well, nothing to check, really
299 # xmpp_check_result: check the return value from some xmpp function execution
300 # input: text, result, [connection]
302 sub xmpp_check_result {
304 my ($txt,$res,$cnx)=@_;
306 error_exit ("Error '$txt': result undefined")
307 unless (defined $res);
312 # result can be true or 'ok'
313 } elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') {
314 debug_print "$txt: " . $$res[0];
315 # otherwise, there is some error
317 my $errmsg = $cnx->GetErrorCode() || '?';
318 error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx);
324 # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
325 # input: [array of strings]
328 print STDERR "sendxmpp: " . (join ' ',@_) . "\n"
329 if (@_ && ($DEBUG ||$VERBOSE));
334 # error_exit: print error message and exit the program
335 # logs out if there is a connection
336 # input: error, [connection]
341 print STDERR "$err\n";
350 # usage: print short usage message and exit
355 "sendxmpp version $VERSION, (c) 2004 Dirk-Jan C. Binnema\n" .
356 "usage: sendxmpp [options] <recipient>\n" .
357 "or refer to the the sendxmpp manpage\n";
369 sendxmpp - send xmpp messages from the commandline.
373 sendxmpp [options] <recipient>
377 sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not
378 unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms.
380 the recipient is either another jabber-account or a jabber-chatroom (use '-c' to tell
381 sendxmpp it's a chatroom)
385 B<-f>,B<--file> <file>
386 use <file> configuration file instead of ~/.sendxmpprc
388 B<-u>,B<--username> <user>
389 use <user> instead of the one in the configuration file
391 B<-p>,B<--password> <password>
392 use <password> instead of the one in the configuration file
394 B<-j>,B<--jserver> <server>
395 use jabber server <server> instead of the one in the configuration file
397 B<-r>,B<--resource> <res>
398 use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
401 connect securely, using TLS
404 send the message to a chatroom
406 B<-s>,B<--subject> <subject>
407 set the subject for the message to <subject> [default: '']; when sending to a chatroom,
408 this will set the subject for the chatroom
410 B<-m>,B<--message> <message>
411 read the message from <message> (a file) instead of stdin
414 give verbose output about what is happening
416 B<-h>,B<--help>,B<--usage>
417 show a 'Usage' message
420 show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
422 =head1 CONFIGURATION FILE
424 You may define a '~/.sendxmpprc' file with the necessary data for your
425 xmmp-account, with a line of the format:
427 <user>@<host> <password>
432 alice@jabber.org secret
434 ('#' and newlines are allowed like in shellscripts)
436 B<NOTE>: for your security, sendxmpp demands that the configuration
437 file is owned by you and has file permissions 600.
441 $ echo "hello bob!" | sendxmpp -s hello someone@jabber.org
443 or to send to a chatroom:
445 $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
449 Documentation for the L<Net::XMPP> module
451 The jabber homepage: http://www.jabber.org/
453 The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmmp (the xmmp homepage)
457 sendxmpp has been written by Dirk-Jan C. Binnema <dirk-jan@djcbsoftware.nl>, and uses
458 the L<Net::XMPP> modules written by Ryan Eatmon.