0 | 0 |
#!/usr/bin/perl -w
|
|
1 |
|
|
2 |
eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
|
|
3 |
if 0; # not running under some shell
|
1 | 4 |
#-*-mode:perl-*-
|
2 | |
#Time-stamp: <2005-05-02 01:00:02 (djcb)>
|
|
5 |
#Time-stamp: <2005-05-07 19:24:09 (djcb)>
|
3 | 6 |
|
4 | 7 |
# script to send message using xmpp (aka jabber),
|
5 | 8 |
# somewhat resembling mail(1)
|
|
14 | 17 |
use strict;
|
15 | 18 |
|
16 | 19 |
# subroutines decls
|
17 | |
sub xmpp_login($$$$$$);
|
|
20 |
sub xmpp_login($$$$$$$);
|
18 | 21 |
sub xmpp_send ($$$);
|
19 | 22 |
sub xmpp_send_message($$$$);
|
20 | 23 |
sub xmpp_send_chatroom_message($$$$$);
|
|
29 | 32 |
sub main();
|
30 | 33 |
|
31 | 34 |
my # MakeMaker
|
32 | |
$VERSION = '0.0.7.1';
|
|
35 |
$VERSION = '0.0.8';
|
33 | 36 |
my $RESOURCE = 'sendxmpp';
|
34 | 37 |
my $VERBOSE = 0;
|
35 | 38 |
my $DEBUG = 0;
|
|
54 | 57 |
|
55 | 58 |
# login to xmpp
|
56 | 59 |
my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'},
|
|
60 |
$$cmdline{'port'} || $$config{'port'},
|
57 | 61 |
$$cmdline{'username'} || $$config{'username'},
|
58 | 62 |
$$cmdline{'password'} || $$config{'password'},
|
59 | 63 |
$$cmdline{'resource'},
|
|
131 | 135 |
|
132 | 136 |
s/\#.*$//; # ignore comments in lines
|
133 | 137 |
|
134 | |
if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) {
|
|
138 |
if (/([-\.\w]+)@([-\.\w:]+)\s+(\S+)\s*$/) {
|
135 | 139 |
%config = ('username' => $1,
|
136 | 140 |
'jserver' => $2,
|
|
141 |
'port' => 0,
|
137 | 142 |
'password' => $3);
|
|
143 |
|
|
144 |
if ($config{'jserver'} =~ /(.*):(\d+)/) {
|
|
145 |
$config{'jserver'} = $1;
|
|
146 |
$config{'port'} = $2;
|
|
147 |
}
|
138 | 148 |
} else {
|
139 | 149 |
close CFG;
|
140 | 150 |
error_exit ("syntax error in line $line of $cfg_file");
|
|
165 | 175 |
|
166 | 176 |
usage() unless (scalar(@ARGV));
|
167 | 177 |
|
168 | |
my ($subject,$file,$resource,$jserver,$username,$password,
|
|
178 |
my ($subject,$file,$resource,$jserver,$port,$username,$password,
|
169 | 179 |
$message,$chatroom,$debug,$tls,$interactive,$help,$verbose);
|
170 | 180 |
my $res = GetOptions ('subject|s=s' => \$subject,
|
171 | 181 |
'file|f=s' => \$file,
|
|
186 | 196 |
my $rcpt = $ARGV[0]
|
187 | 197 |
or error_exit "no recipient specified";
|
188 | 198 |
|
189 | |
if ($message && $interactive) {
|
190 | |
error_exit "cannot have both -m (--message) and -i (--interactive)\n";
|
191 | |
}
|
|
199 |
if ($message && $interactive) {
|
|
200 |
error_exit "cannot have both -m (--message) and -i (--interactive)\n";
|
|
201 |
}
|
|
202 |
|
|
203 |
if ($jserver && $jserver =~ /(.*):(\d+)/) {
|
|
204 |
$jserver = $1;
|
|
205 |
$port = $2;
|
|
206 |
}
|
192 | 207 |
|
193 | 208 |
my %dict = ('subject' => ($subject or ''),
|
194 | 209 |
'resource' => ($resource or $RESOURCE),
|
195 | 210 |
'jserver' => ($jserver or ''),
|
|
211 |
'port' => ($port or 0),
|
196 | 212 |
'username' => ($username or ''),
|
197 | 213 |
'password' => ($password or ''),
|
198 | 214 |
'chatroom' => ($chatroom or 0),
|
|
215 | 231 |
|
216 | 232 |
#
|
217 | 233 |
# xmpp_login: login to the xmpp (jabber) server
|
218 | |
# input: hostname,username,password,resource,tls,debug
|
|
234 |
# input: hostname,port,username,password,resource,tls,debug
|
219 | 235 |
# output: an XMPP connection object
|
220 | 236 |
#
|
221 | |
sub xmpp_login ($$$$$$) {
|
222 | |
|
223 | |
my ($host,$user,$pw,$res,$tls,$debug) = @_;
|
|
237 |
sub xmpp_login ($$$$$$$) {
|
|
238 |
|
|
239 |
my ($host,$port,$user,$pw,$res,$tls,$debug) = @_;
|
224 | 240 |
my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
|
225 | 241 |
error_exit "could not create XMPP client object: $!"
|
226 | 242 |
unless ($cnx);
|
227 | 243 |
|
228 | |
my @res = $cnx->Connect(hostname=>$host,tls=>$tls);
|
|
244 |
my @res;
|
|
245 |
if (!$port) {
|
|
246 |
@res = $cnx->Connect(hostname=>$host,tls=>$tls);
|
|
247 |
} else {
|
|
248 |
@res = $cnx->Connect(hostname=>$host,port=>$port,tls=>$tls);
|
|
249 |
}
|
|
250 |
|
229 | 251 |
xmpp_check_result("Connect",\@res,$cnx);
|
230 | 252 |
|
231 | |
|
232 | 253 |
@res = $cnx->AuthSend('hostname' => $host,
|
233 | 254 |
'username' => $user,
|
234 | 255 |
'password' => $pw,
|
|
337 | 358 |
my ($txt,$res,$cnx)=@_;
|
338 | 359 |
|
339 | 360 |
error_exit ("Error '$txt': result undefined")
|
340 | |
unless (defined $res);
|
341 | |
|
|
361 |
unless (defined $res);
|
|
362 |
|
342 | 363 |
# res may be 0
|
343 | 364 |
if ($res == 0) {
|
344 | 365 |
debug_print "$txt";
|
|
432 | 453 |
use <password> instead of the one in the configuration file
|
433 | 454 |
|
434 | 455 |
B<-j>,B<--jserver> <server>
|
435 | |
use jabber server <server> instead of the one in the configuration file
|
|
456 |
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>
|
436 | 457 |
|
437 | 458 |
B<-r>,B<--resource> <res>
|
438 | 459 |
use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
|
|
474 | 495 |
# my account
|
475 | 496 |
alice@jabber.org secret
|
476 | 497 |
|
477 | |
('#' and newlines are allowed like in shellscripts)
|
|
498 |
('#' and newlines are allowed like in shellscripts). You can add :<port> to
|
|
499 |
the <host> if you need an alternative port, ie.
|
|
500 |
|
|
501 |
# account with weird port number
|
|
502 |
alice@myjabberhost.com:1234 secret
|
478 | 503 |
|
479 | 504 |
B<NOTE>: for your security, sendxmpp demands that the configuration
|
480 | 505 |
file is owned by you and has file permissions 600.
|
|
487 | 512 |
|
488 | 513 |
$ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org
|
489 | 514 |
|
|
515 |
or to send your system logs somewhere, as new lines appear:
|
|
516 |
|
|
517 |
$ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com
|
|
518 |
|
|
519 |
NOTE: be careful not the overload public jabber services
|
|
520 |
|
490 | 521 |
=head1 SEE ALSO
|
491 | 522 |
|
492 | 523 |
Documentation for the L<Net::XMPP> module
|