version 1.1.1.3, 2006/10/04 21:05:27 |
version 1.1.1.4, 2006/10/04 21:05:53 |
|
|
#!/usr/bin/perl -w |
#!/usr/bin/perl -w |
#-*-mode:perl-*- |
#-*-mode:perl-*- |
#Time-stamp: <2004-12-01 14:52:47 (djcb)> |
#Time-stamp: <2005-05-02 01:00:02 (djcb)> |
|
|
# script to send message using xmpp (aka jabber), |
# script to send message using xmpp (aka jabber), |
# somewhat resembling mail(1) |
# somewhat resembling mail(1) |
|
|
# author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> |
# Author: Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> |
# copyright (c) 2004, Dirk-Jan C. Binnema <djcb@djcbsoftware.nl> |
# Copyright (c) 2004,2005 Dirk-Jan C. Binnema |
# |
|
# released under the terms of the GNU General Public License v2 |
# Released under the terms of the GNU General Public License v2 |
|
|
use Net::XMPP; |
use Net::XMPP; |
use Getopt::Long; |
use Getopt::Long; |
use strict; |
use strict; |
|
|
|
|
# subroutines decls |
# subroutines decls |
sub xmpp_login($$$$$$); |
sub xmpp_login($$$$$$); |
|
sub xmpp_send ($$$); |
sub xmpp_send_message($$$$); |
sub xmpp_send_message($$$$); |
sub xmpp_send_chatroom_message($$$$$); |
sub xmpp_send_chatroom_message($$$$$); |
sub xmpp_logout($); |
sub xmpp_logout($); |
|
|
sub debug_print; |
sub debug_print; |
sub read_config_file($); |
sub read_config_file($); |
sub push_hash($$); |
sub push_hash($$); |
sub main (); |
sub terminate(); |
|
sub main(); |
|
|
my # MakeMaker |
my # MakeMaker |
$VERSION = '0.0.6'; |
$VERSION = '0.0.7'; |
my $RESOURCE = 'sendxmpp'; |
my $RESOURCE = 'sendxmpp'; |
my $VERBOSE = 0; |
my $VERBOSE = 0; |
my $DEBUG = 0; |
my $DEBUG = 0; |
|
|
|
|
# start! |
# start! |
&main; |
&main; |
|
|
|
|
# |
# |
# main: main routine |
# main: main routine |
# |
# |
|
|
|
|
my $cmdline = parse_cmdline(); |
my $cmdline = parse_cmdline(); |
|
|
|
$| = 1; # no output buffering |
|
|
$DEBUG = 1 if ($$cmdline{'debug'}); |
$DEBUG = 1 if ($$cmdline{'debug'}); |
$VERBOSE = 1 if ($$cmdline{'verbose'}); |
$VERBOSE = 1 if ($$cmdline{'verbose'}); |
|
|
my $config = read_config_file ($$cmdline{'file'}) |
my $config = read_config_file ($$cmdline{'file'}) |
unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'}); |
unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'}); |
|
|
|
|
$$cmdline{'resource'}, |
$$cmdline{'resource'}, |
$$cmdline{'tls'}, |
$$cmdline{'tls'}, |
$$cmdline{'debug'}) |
$$cmdline{'debug'}) |
or error_exit("cannot login: $!"); |
or error_exit("cannot login: $!"); |
|
|
|
|
|
# read message from STDIN or or from -m/--message parameter |
|
if (!$$cmdline{interactive}) { |
|
|
|
# the non-interactive case |
|
my $txt; |
|
my $message = $$cmdline{'message'}; |
|
if ($message) { |
|
open (MSG, "<$message") |
|
or error_exit ("cannot open message file '$message': $!"); |
|
while (<MSG>) {$txt.=$_}; |
|
close(MSG); |
|
} else { |
|
$txt.=$_ while (<STDIN>); |
|
} |
|
|
|
xmmp_send ($cnx,$cmdline,$txt); |
|
|
unless ($$cmdline{'chatroom'}) { |
|
xmpp_send_message ($cnx, |
|
$$cmdline{'recipient'}, |
|
$$cmdline{'subject'}, |
|
$$cmdline{'message'}); |
|
} else { |
} else { |
xmpp_send_chatroom_message ($cnx, |
# the interactive case, read stdin line by line |
$$cmdline{'resource'}, |
|
$$cmdline{'subject'}, |
# deal with TERM |
$$cmdline{'recipient'}, |
$main::CNX = $cnx; |
$$cmdline{'message'}); |
$SIG{INT}=\&terminate; |
|
|
|
# line by line... |
|
while (<STDIN>) { |
|
chomp; |
|
xmpp_send ($cnx,$cmdline,$_); |
|
} |
} |
} |
|
|
xmpp_logout($cnx); |
xmpp_logout($cnx); |
exit(0); |
exit 0; |
} |
} |
|
|
|
|
|
|
# |
# |
# read_config_file: read the configuration file |
# read_config_file: read the configuration file |
# input: filename |
# input: filename |
Line 94 sub read_config_file ($) { |
|
Line 113 sub read_config_file ($) { |
|
unless (-r $cfg_file); |
unless (-r $cfg_file); |
my $owner = (stat($cfg_file))[4]; |
my $owner = (stat($cfg_file))[4]; |
error_exit ("you must own $cfg_file") |
error_exit ("you must own $cfg_file") |
unless ($owner == $>); |
unless ($owner == $>); |
my $mode = (stat($cfg_file))[2] & 07777; |
my $mode = (stat($cfg_file))[2] & 07777; |
error_exit ("$cfg_file must have mode 0600") |
error_exit ("$cfg_file must have mode 0600") |
unless ($mode == 0600); |
unless ($mode == 0600); |
|
|
open (CFG,"<$cfg_file") |
open (CFG,"<$cfg_file") |
or error_exit("cannot open $cfg_file for reading: $!"); |
or error_exit("cannot open $cfg_file for reading: $!"); |
|
|
my %config; |
my %config; |
my $line = 0; |
my $line = 0; |
Line 114 sub read_config_file ($) { |
|
Line 133 sub read_config_file ($) { |
|
s/\#.*$//; # ignore comments in lines |
s/\#.*$//; # ignore comments in lines |
|
|
if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) { |
if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) { |
%config = ('username'=>$1, |
%config = ('username' => $1, |
'jserver'=>$2, |
'jserver' => $2, |
'password'=>$3); |
'password' => $3); |
|
|
} else { |
} else { |
close(CFG); |
close CFG; |
error_exit ("syntax error in line $line of $cfg_file"); |
error_exit ("syntax error in line $line of $cfg_file"); |
} |
} |
} |
} |
|
|
close(CFG); |
close CFG; |
|
|
error_exit ("no correct config found in $cfg_file") |
error_exit ("no correct config found in $cfg_file") |
unless (scalar(%config)); |
unless (scalar(%config)); |
|
|
if ($DEBUG || $VERBOSE) { |
if ($DEBUG || $VERBOSE) { |
while (my ($key,$val) = each %config) { |
while (my ($key,$val) = each %config) { |
debug_print ("config: '$key' => '$val'"); |
debug_print ("config: '$key' => '$val'"); |
} |
} |
} |
} |
|
|
return \%config; |
return \%config; |
} |
} |
|
|
Line 146 sub read_config_file ($) { |
|
Line 164 sub read_config_file ($) { |
|
# |
# |
sub parse_cmdline () { |
sub parse_cmdline () { |
|
|
usage() unless (scalar(@ARGV)); |
usage() unless (scalar(@ARGV)); |
|
|
my ($subject,$file,$resource,$jserver,$username,$password, |
my ($subject,$file,$resource,$jserver,$username,$password, |
$message,$chatroom,$debug,$tls,$help,$verbose); |
$message,$chatroom,$debug,$tls,$interactive,$help,$verbose); |
my $res = GetOptions ('subject|s=s' => \$subject, |
my $res = GetOptions ('subject|s=s' => \$subject, |
'file|f=s' => \$file, |
'file|f=s' => \$file, |
'resource|r=s' => \$resource, |
'resource|r=s' => \$resource, |
Line 159 sub parse_cmdline () { |
|
Line 177 sub parse_cmdline () { |
|
'message|m=s' => \$message, |
'message|m=s' => \$message, |
'chatroom|c' => \$chatroom, |
'chatroom|c' => \$chatroom, |
'tls|t' => \$tls, |
'tls|t' => \$tls, |
|
'interactive|i' => \$interactive, |
'help|usage|h' => \$help, |
'help|usage|h' => \$help, |
'debug|d' => \$debug, |
'debug|d' => \$debug, |
'verbose|v' => \$verbose); |
'verbose|v' => \$verbose); |
usage () |
usage () |
if ($help); |
if ($help); |
|
|
my $rcpt = $ARGV[0] |
|
or error_exit("no recipient specified"); |
|
|
|
# read message from STDIN or or from -m/--message parameter |
|
my $txt; |
|
if ($message) { |
|
open (MSG, "<$message") |
|
or error_exit ("cannot open message file '$message': $!"); |
|
while (<MSG>) {$txt.=$_}; |
|
close(MSG); |
|
} else { |
|
while (<STDIN>) {$txt.=$_}; |
|
} |
|
|
|
my %dict = ('subject' => ($subject or ''), |
my $rcpt = $ARGV[0] |
'resource' => ($resource or $RESOURCE), |
or error_exit "no recipient specified"; |
'jserver' => ($jserver or ''), |
|
'username' => ($username or ''), |
if ($message && $interactive) { |
'password' => ($password or ''), |
error_exit "cannot have both -m (--message) and -i (--interactive)\n"; |
'chatroom' => ($chatroom or 0), |
} |
'tls' => ($tls or 0), |
|
'debug' => ($debug or 0), |
my %dict = ('subject' => ($subject or ''), |
'message' => ($txt or ''), |
'resource' => ($resource or $RESOURCE), |
'verbose' => ($verbose or 0), |
'jserver' => ($jserver or ''), |
'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')), |
'username' => ($username or ''), |
'recipient' => $rcpt); |
'password' => ($password or ''), |
|
'chatroom' => ($chatroom or 0), |
|
'interactive' => ($interactive or 0), |
|
'tls' => ($tls or 0), |
|
'debug' => ($debug or 0), |
|
'verbose' => ($verbose or 0), |
|
'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')), |
|
'recipient' => $rcpt); |
|
|
if ($DEBUG || $VERBOSE) { |
if ($DEBUG || $VERBOSE) { |
while (my ($key,$val) = each %dict) { |
while (my ($key,$val) = each %dict) { |
Line 202 sub parse_cmdline () { |
|
Line 214 sub parse_cmdline () { |
|
} |
} |
|
|
|
|
|
|
# |
# |
# xmpp_login: login to the xmpp (jabber) server |
# xmpp_login: login to the xmpp (jabber) server |
# input: hostname,username,password,resource,tls,debug |
# input: hostname,username,password,resource,tls,debug |
Line 212 sub xmpp_login ($$$$$$) { |
|
Line 223 sub xmpp_login ($$$$$$) { |
|
|
|
my ($host,$user,$pw,$res,$tls,$debug) = @_; |
my ($host,$user,$pw,$res,$tls,$debug) = @_; |
my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0)); |
my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0)); |
error_exit ("could not create XMPP client object: $!") |
error_exit "could not create XMPP client object: $!" |
unless ($cnx); |
unless ($cnx); |
|
|
my @res = $cnx->Connect(hostname=>$host,tls=>$tls); |
my @res = $cnx->Connect(hostname=>$host,tls=>$tls); |
xmpp_check_result("Connect",\@res,$cnx); |
xmpp_check_result("Connect",\@res,$cnx); |
|
|
|
|
@res = $cnx->AuthSend('hostname'=>$host, |
@res = $cnx->AuthSend('hostname' => $host, |
'username'=>$user, |
'username' => $user, |
'password'=>$pw, |
'password' => $pw, |
'resource'=>$res); |
'resource' => $res); |
xmpp_check_result('AuthSend',\@res,$cnx); |
xmpp_check_result('AuthSend',\@res,$cnx); |
|
|
#@res = $cnx->PresenceSend(type=>'unavailable'); |
|
#mpp_check_result("PresenceSend",\@res,$cnx); |
|
|
|
return $cnx; |
return $cnx; |
} |
} |
|
|
|
|
|
|
|
|
|
# |
|
# xmmp_send: send the message, determine from cmdline |
|
# whether it's to individual or chatroom |
|
# |
|
sub xmpp_send ($$$) { |
|
|
|
my ($cnx, $cmdline, $txt) = @_; |
|
|
|
unless ($$cmdline{'chatroom'}) { |
|
xmpp_send_message ($cnx, |
|
$$cmdline{'recipient'}, |
|
$$cmdline{'subject'}, |
|
$txt); |
|
} else { |
|
xmpp_send_chatroom_message ($cnx, |
|
$$cmdline{'resource'}, |
|
$$cmdline{'subject'}, |
|
$$cmdline{'recipient'}, |
|
$txt); |
|
} |
|
} |
|
|
|
|
|
|
# |
# |
# xmpp_send_message: send a message to some xmpp user |
# xmpp_send_message: send a message to some xmpp user |
# input: connection,recipient,subject,msg |
# input: connection,recipient,subject,msg |
Line 241 sub xmpp_send_message ($$$$) { |
|
Line 275 sub xmpp_send_message ($$$$) { |
|
my ($cnx,$rcpt,$subject,$msg) = @_; |
my ($cnx,$rcpt,$subject,$msg) = @_; |
|
|
# for some reason, MessageSend does not return anything |
# for some reason, MessageSend does not return anything |
$cnx->MessageSend('to'=>$rcpt, |
$cnx->MessageSend('to' => $rcpt, |
'subject'=>$subject, |
'subject' => $subject, |
'body'=>$msg); |
'body' => $msg); |
|
|
xmpp_check_result('MessageSend',0,$cnx); |
xmpp_check_result('MessageSend',0,$cnx); |
} |
} |
Line 265 sub xmpp_send_chatroom_message ($$$$$) { |
|
Line 299 sub xmpp_send_chatroom_message ($$$$$) { |
|
|
|
# create/send the message |
# create/send the message |
my $groupmsg = new Net::XMPP::Message; |
my $groupmsg = new Net::XMPP::Message; |
$groupmsg->SetMessage(to=>$rcpt, |
$groupmsg->SetMessage(to => $rcpt, |
body=>$msg, |
body => $msg, |
subject=>$subject, |
subject => $subject, |
type=>'groupchat'); |
type => 'groupchat'); |
|
|
$res = $cnx->Send($groupmsg); |
$res = $cnx->Send($groupmsg); |
xmpp_check_result ('Send',$res,$cnx); |
xmpp_check_result ('Send',$res,$cnx); |
Line 321 sub xmpp_check_result { |
|
Line 355 sub xmpp_check_result { |
|
|
|
|
|
# |
# |
|
# terminate; exit the program upon TERM sig reception |
|
# |
|
sub terminate () { |
|
debug_print "caught TERM"; |
|
xmpp_logout($main::CNX); |
|
exit 0; |
|
} |
|
|
|
|
|
# |
# debug_print: print the data if defined and DEBUG || VERBOSE is TRUE |
# debug_print: print the data if defined and DEBUG || VERBOSE is TRUE |
# input: [array of strings] |
# input: [array of strings] |
# |
# |
sub debug_print { |
sub debug_print { |
print STDERR "sendxmpp: " . (join ' ',@_) . "\n" |
print STDERR "sendxmpp: " . (join ' ', @_) . "\n" |
if (@_ && ($DEBUG ||$VERBOSE)); |
if (@_ && ($DEBUG ||$VERBOSE)); |
} |
} |
|
|
Line 351 sub error_exit { |
|
Line 395 sub error_exit { |
|
# |
# |
sub usage () { |
sub usage () { |
|
|
print |
print STDERR |
"sendxmpp version $VERSION, (c) 2004 Dirk-Jan C. Binnema\n" . |
"sendxmpp version $VERSION, Copyright (c) 2004,2005 Dirk-Jan C. Binnema\n" . |
"usage: sendxmpp [options] <recipient>\n" . |
"usage: sendxmpp [options] <recipient>\n" . |
"or refer to the the sendxmpp manpage\n"; |
"or refer to the the sendxmpp manpage\n"; |
|
|
|
|
} |
} |
|
|
|
|
|
|
# |
# |
# the fine manual |
# the fine manual |
# |
# |
|
=pod |
=head1 NAME |
=head1 NAME |
|
|
sendxmpp - send xmpp messages from the commandline. |
sendxmpp - send xmpp messages from the commandline. |
Line 377 sendxmpp [options] <recipient> |
|
Line 421 sendxmpp [options] <recipient> |
|
sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not |
sendxmpp is a program to send XMPP (Jabber) messages from the commandline, not |
unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms. |
unlike L<mail(1)>. Messages can be sent both to individual recipients and chatrooms. |
|
|
the recipient is either another jabber-account or a jabber-chatroom (use '-c' to tell |
|
sendxmpp it's a chatroom) |
|
|
|
=head1 OPTIONS |
=head1 OPTIONS |
|
|
B<-f>,B<--file> <file> |
B<-f>,B<--file> <file> |
Line 410 this will set the subject for the chatro |
|
Line 451 this will set the subject for the chatro |
|
B<-m>,B<--message> <message> |
B<-m>,B<--message> <message> |
read the message from <message> (a file) instead of stdin |
read the message from <message> (a file) instead of stdin |
|
|
|
B<-i>,B<--interactive> |
|
work in interactive mode, reading lines from stdin and sending the one-at-time |
|
|
B<-v>,B<--verbose> |
B<-v>,B<--verbose> |
give verbose output about what is happening |
give verbose output about what is happening |
|
|
Line 450 Documentation for the L<Net::XMPP> modul |
|
Line 494 Documentation for the L<Net::XMPP> modul |
|
|
|
The jabber homepage: http://www.jabber.org/ |
The jabber homepage: http://www.jabber.org/ |
|
|
The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp (the xmpp homepage) |
The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp |
|
|
=head1 AUTHOR |
=head1 AUTHOR |
|
|
sendxmpp has been written by Dirk-Jan C. Binnema <dirk-jan@djcbsoftware.nl>, and uses |
sendxmpp has been written by Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>, and uses |
the L<Net::XMPP> modules written by Ryan Eatmon. |
the L<Net::XMPP> modules written by Ryan Eatmon. |
|
|
=cut |
=cut |
|
|