X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=sendxmpp;h=d5546cebaa5e922bb54d2ceaacc3064f59edd581;hb=8e5d36c3b4a16cb6cbccbff5dbf4f7f7cef422bd;hp=4954cec41911a06f79137b17358bf1816b76f721;hpb=4afcfb39adcb9c7c7f080ffcf9937b40fbba7163;p=cascardo%2Fsendxmpp.git diff --git a/sendxmpp b/sendxmpp index 4954cec..d5546ce 100755 --- a/sendxmpp +++ b/sendxmpp @@ -4,30 +4,34 @@ eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' if 0; # not running under some shell # -# script to send message using xmpp (aka jabber), +# script to send message using xmpp (aka jabber), # somewhat resembling mail(1) # -# Author: Dirk-Jan C. Binnema -# Copyright (c) 2004, 2005 Dirk-Jan C. Binnema -# +# Author: Dirk-Jan C. Binnema # Maintainer: Lubomir Host 'rajo' +# Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema +# Copyright (c) 2006 - 2009 Lubomir Host 'rajo' +# # Homepage: http://sendxmpp.platon.sk # # Released under the terms of the GNU General Public License v2 # +# $Platon: sendxmpp/sendxmpp,v 1.15 2008-10-21 21:31:53 rajo Exp $ +# $Id: $ +use Authen::SASL qw(Perl); # authentication broken if Authen::SASL::Cyrus module installed use Net::XMPP; use Getopt::Long; use strict; -use open ':utf8'; -use open ':std'; +use open ':utf8'; +use open ':std'; # subroutines decls -sub xmpp_login($$$$$$$); -sub xmpp_send ($$$); +sub xmpp_login($$$$$$$$); +sub xmpp_send ($$$$); sub xmpp_send_raw_xml($$); -sub xmpp_send_message($$$$); +sub xmpp_send_message($$$$$$); sub xmpp_send_chatroom_message($$$$$); sub xmpp_logout($); sub xmpp_check_result; @@ -40,11 +44,13 @@ sub terminate(); sub main(); my # MakeMaker -$VERSION = '0.0.8'; +$VERSION = [ q$Revision: 1.15 $ =~ m/(\S+)\s*$/g ]->[0]; my $RESOURCE = 'sendxmpp'; my $VERBOSE = 0; my $DEBUG = 0; - +my @suppported_message_types = qw( message chat headline ); +my $message_type = 'message'; # default message type + # start! &main; @@ -54,54 +60,56 @@ my $DEBUG = 0; sub main () { my $cmdline = parse_cmdline(); - + $| = 1; # no output buffering $DEBUG = 1 if ($$cmdline{'debug'}); $VERBOSE = 1 if ($$cmdline{'verbose'}); - + my $config = read_config_file ($$cmdline{'file'}) - unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'}); - + unless ($$cmdline{'jserver'} && $$cmdline{'username'} && $$cmdline{'password'}); + # login to xmpp my $cnx = xmpp_login ($$cmdline{'jserver'} || $$config{'jserver'}, $$cmdline{'port'} || $$config{'port'}, $$cmdline{'username'} || $$config{'username'}, $$cmdline{'password'} || $$config{'password'}, + $$cmdline{'component'}|| $$config{'component'}, $$cmdline{'resource'}, $$cmdline{'tls'}, $$cmdline{'debug'}) 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'}; + my $message = $$cmdline{'message'}; if ($message) { open (MSG, "<$message") or error_exit ("cannot open message file '$message': $!"); - while () {$txt.=$_}; + while () { $txt .= $_ }; close(MSG); - } else { - $txt.=$_ while (); } - - xmpp_send ($cnx,$cmdline,$txt); - + else { + $txt .= $_ while (); + } + + xmpp_send ($cnx,$cmdline,$config,$txt); + } else { # the interactive case, read stdin line by line # deal with TERM - $main::CNX = $cnx; + $main::CNX = $cnx; $SIG{INT}=\&terminate; # line by line... while () { chomp; - xmpp_send ($cnx,$cmdline,$_); + xmpp_send ($cnx,$cmdline,$config,$_); } } @@ -117,60 +125,73 @@ sub main () { # output: hash with 'user', 'jserver' and 'password' keys # sub read_config_file ($) { - + # check permissions my $cfg_file = shift; - error_exit ("cannot read $cfg_file: $!") - unless (-r $cfg_file); - my $owner = (stat($cfg_file))[4]; + error_exit ("cannot read $cfg_file: $!") + unless (-r $cfg_file); + my $owner = (stat _ )[4]; error_exit ("you must own $cfg_file") - unless ($owner == $>); - my $mode = (stat($cfg_file))[2] & 07777; - error_exit ("$cfg_file must have mode 0600") - unless ($mode == 0600); - + unless ($owner == $>); + my $mode = (stat _ )[2] & 07777; + error_exit ("$cfg_file must not be accessible by others") + if ($mode & 0077); + open (CFG,"<$cfg_file") or error_exit("cannot open $cfg_file for reading: $!"); my %config; my $line = 0; - while () { - - ++$line; - - next if (/^\s*$/); # ignore empty lines - next if (/^\s*\#.*/); # ignore comment lines - - s/\#.*$//; # ignore comments in lines - - if (/([-\.\w]+)@([-\.\w:]+)\s+(\S+)\s*$/) { - %config = ('username' => $1, - 'jserver' => $2, - 'port' => 0, - 'password' => $3); - - if ($config{'jserver'} =~ /(.*):(\d+)/) { - $config{'jserver'} = $1; - $config{'port'} = $2; - } - } else { - close CFG; - error_exit ("syntax error in line $line of $cfg_file"); + while () { + + ++$line; + + next if (/^\s*$/); # ignore empty lines + next if (/^\s*\#.*/); # ignore comment lines + + #s/\#.*$//; # ignore comments in lines + + # Hugo van der Kooij has account with '#' as username + if (/([\.\w_#-]+)@([-\.\w:;]+)\s+(\S+)\s*(\S+)?$/) { + %config = ( + 'username' => $1, + 'jserver' => $2, + 'port' => 0, + 'password' => $3, + 'component' => $4, + ); + + } + else { + close CFG; + error_exit ("syntax error in line $line of $cfg_file"); + } + + # account with weird port number + if ($config{'jserver'} =~ /(.*):(\d+)/) { + $config{'jserver'} = $1; + $config{'port'} = $2; + } + + # account with specific connection host + if ($config{'jserver'} =~ /(.*);([-\.\w]+)/) { + $config{'jserver'} = $2; + $config{'username'} .= "\@$1"; + } } - } - + close CFG; - - error_exit ("no correct config found in $cfg_file") - unless (scalar(%config)); + + error_exit ("no correct config found in $cfg_file") + unless (scalar(%config)); if ($DEBUG || $VERBOSE) { while (my ($key,$val) = each %config) { debug_print ("config: '$key' => '$val'"); } - } - - return \%config; + } + + return \%config; } @@ -180,18 +201,21 @@ sub read_config_file ($) { # output: hash with commandline options # sub parse_cmdline () { - + usage() unless (scalar(@ARGV)); - - my ($subject,$file,$resource,$jserver,$port,$username,$password, - $message, $chatroom, $debug, $tls, $interactive, $help, $raw, $verbose); + + my ($subject,$file,$resource,$jserver,$port,$username,$password,$component, + $message, $chatroom, $headline, $debug, $tls, $interactive, $help, $raw, $verbose); my $res = GetOptions ('subject|s=s' => \$subject, 'file|f=s' => \$file, 'resource|r=s' => \$resource, 'jserver|j=s' => \$jserver, + 'component|o=s' => \$component, 'username|u=s' => \$username, 'password|p=s' => \$password, 'message|m=s' => \$message, + 'headline|l' => \$headline, + 'message-type=s' => \$message_type, 'chatroom|c' => \$chatroom, 'tls|t' => \$tls, 'interactive|i' => \$interactive, @@ -199,12 +223,12 @@ sub parse_cmdline () { 'debug|d' => \$debug, 'raw|w' => \$raw, 'verbose|v' => \$verbose); - usage () if ($help); - + usage () if ($help); + my @rcpt = @ARGV; if (defined($raw) && scalar(@rcpt) > 0) { - error_exit "You must give a recipient or --raw (but not both)"; + error_exit("You must give a recipient or --raw (but not both)"); } if ($raw && $subject) { error_exit("You cannot specify a subject in raw XML mode"); @@ -214,8 +238,22 @@ sub parse_cmdline () { } if ($message && $interactive) { - error_exit "Cannot have both -m (--message) and -i (--interactive)"; - } + error_exit("Cannot have both -m (--message) and -i (--interactive)"); + } + + if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) { + error_exit("Unsupported message type '$message_type'"); + } + + if ($headline) { + # --headline withouth --message-type + if ($message_type eq 'message') { + $message_type = 'headline' + } + else { + error_exit("Options --headline and --message-type are mutually exclusive"); + } + } if ($jserver && $jserver =~ /(.*):(\d+)/) { $jserver = $1; @@ -226,10 +264,12 @@ sub parse_cmdline () { 'message' => ($message or ''), 'resource' => ($resource or $RESOURCE), 'jserver' => ($jserver or ''), + 'component' => ($component or ''), 'port' => ($port or 0), 'username' => ($username or ''), 'password' => ($password or ''), 'chatroom' => ($chatroom or 0), + 'message-type' => $message_type, 'interactive' => ($interactive or 0), 'tls' => ($tls or 0), 'debug' => ($debug or 0), @@ -242,9 +282,9 @@ sub parse_cmdline () { while (my ($key,$val) = each %dict) { debug_print ("cmdline: '$key' => '$val'"); } - } - - return \%dict; + } + + return \%dict; } @@ -253,31 +293,43 @@ sub parse_cmdline () { # input: hostname,port,username,password,resource,tls,debug # output: an XMPP connection object # -sub xmpp_login ($$$$$$$) { +sub xmpp_login ($$$$$$$$) { - my ($host,$port,$user,$pw,$res,$tls,$debug) = @_; + my ($host, $port, $user, $pw, $comp, $res, $tls, $debug) = @_; my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0)); error_exit "could not create XMPP client object: $!" - unless ($cnx); + unless ($cnx); my @res; + my $arghash = { + hostname => $host, + tls => $tls, + connectiontype => 'tcpip', + componentname => $comp + }; + $arghash->{port} = $port if ($port); if (!$port) { - @res = $cnx->Connect(hostname => $host, tls => $tls); + @res = $cnx->Connect(%$arghash); error_exit ("Could not connect to server '$host': $@") unless @res; } else { - @res = $cnx->Connect(hostname => $host, port => $port, tls => $tls); + @res = $cnx->Connect(%$arghash); error_exit ("Could not connect to '$host' on port $port: $@") unless @res; } xmpp_check_result("Connect",\@res,$cnx); - @res = $cnx->AuthSend('hostname' => $host, + if ($comp) { + my $sid = $cnx->{SESSION}->{id}; + $cnx->{STREAM}->{SIDS}->{$sid}->{hostname} = $comp + } + + @res = $cnx->AuthSend(#'hostname' => $host, 'username' => $user, 'password' => $pw, 'resource' => $res); xmpp_check_result('AuthSend',\@res,$cnx); - - return $cnx; + + return $cnx; } @@ -287,16 +339,18 @@ sub xmpp_login ($$$$$$$) { # xmmp_send: send the message, determine from cmdline # whether it's to individual or chatroom # -sub xmpp_send ($$$) { - - my ($cnx, $cmdline, $txt) = @_; - +sub xmpp_send ($$$$) { + + my ($cnx, $cmdline, $config, $txt) = @_; + unless ($$cmdline{'chatroom'}) { unless ($$cmdline{'raw'}) { map { xmpp_send_message ($cnx, $_, #$$cmdline{'recipient'}, + $$cmdline{'component'} || $$config{'component'}, $$cmdline{'subject'}, + $$cmdline{'message-type'}, $txt) } @{$$cmdline{'recipient'}}; } @@ -322,9 +376,9 @@ sub xmpp_send ($$$) { # input: connection,packet # sub xmpp_send_raw_xml ($$) { - + my ($cnx,$packet) = @_; - + # for some reason, Send does not return anything $cnx->Send($packet); xmpp_check_result('Send',0,$cnx); @@ -335,19 +389,20 @@ sub xmpp_send_raw_xml ($$) { # xmpp_send_message: send a message to some xmpp user # input: connection,recipient,subject,msg # -sub xmpp_send_message ($$$$) { - - my ($cnx,$rcpt,$subject,$msg) = @_; - +sub xmpp_send_message ($$$$$$) { + + my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_; + # for some reason, MessageSend does not return anything - $cnx->MessageSend('to' => $rcpt, - 'subject' => $subject, - 'body' => $msg); - + $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ), + 'type' => $message_type, + 'subject' => $subject, + 'body' => $msg); + xmpp_check_result('MessageSend',0,$cnx); } - - + + # # xmpp_send_chatroom_message: send a message to a chatroom # input: connection,resource,subject,recipient,message @@ -355,23 +410,22 @@ sub xmpp_send_message ($$$$) { sub xmpp_send_chatroom_message ($$$$$) { my ($cnx,$resource,$subject,$rcpt,$msg) = @_; - + # set the presence my $pres = new Net::XMPP::Presence; my $res = $pres->SetTo("$rcpt/$resource"); - $cnx->Send($pres); + $cnx->Send($pres); # create/send the message my $groupmsg = new Net::XMPP::Message; - $groupmsg->SetMessage(to => $rcpt, + $groupmsg->SetMessage(to => $rcpt, body => $msg, - subject => $subject, type => 'groupchat'); $res = $cnx->Send($groupmsg); - xmpp_check_result ('Send',$res,$cnx); - + xmpp_check_result ('Send',$res,$cnx); + # leave the group $pres->SetPresence (Type=>'unavailable',To=>$rcpt); } @@ -382,11 +436,11 @@ sub xmpp_send_chatroom_message ($$$$$) { # input: connection # sub xmpp_logout($) { - + # HACK # messages may not be received if we log out too quickly... - sleep 1; - + sleep 1; + my $cnx = shift; $cnx->Disconnect(); xmpp_check_result ('Disconnect',0); # well, nothing to check, really @@ -396,26 +450,28 @@ sub xmpp_logout($) { # # xmpp_check_result: check the return value from some xmpp function execution -# input: text, result, [connection] +# input: text, result, [connection] # -sub xmpp_check_result { +sub xmpp_check_result +{ + my ($txt, $res, $cnx)=@_; - my ($txt,$res,$cnx)=@_; - error_exit ("Error '$txt': result undefined") unless (defined $res); - + # res may be 0 - if ($res == 0) { - debug_print "$txt"; - # result can be true or 'ok' - } elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') { - debug_print "$txt: " . $$res[0]; - # otherwise, there is some error - } else { - my $errmsg = $cnx->GetErrorCode() || '?'; - error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx); - } + if ($res == 0) { + debug_print "$txt"; + # result can be true or 'ok' + } + elsif ((@$res == 1 && $$res[0]) || $$res[0] eq 'ok') { + debug_print "$txt: " . $$res[0]; + # otherwise, there is some error + } + else { + my $errmsg = $cnx->GetErrorCode() || '?'; + error_exit ("Error '$txt': " . join (': ',@$res) . "[$errmsg]", $cnx); + } } @@ -441,16 +497,16 @@ sub debug_print { # # error_exit: print error message and exit the program -# logs out if there is a connection +# logs out if there is a connection # input: error, [connection] # sub error_exit { - + my ($err,$cnx) = @_; - print STDERR "$err\n"; - xmpp_logout ($cnx) + print STDERR "$err\n"; + xmpp_logout ($cnx) if ($cnx); - + exit 1; } @@ -459,12 +515,14 @@ sub error_exit { # usage: print short usage message and exit # sub usage () { - - print STDERR - "sendxmpp version $VERSION, Copyright (c) 2004, 2005 Dirk-Jan C. Binnema\n" . + + print STDERR + "sendxmpp version $VERSION\n" . + "Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema\n" . + "Copyright (c) 2006 - 2007 Lubomir Host 'rajo'\n" . "usage: sendxmpp [options] [ ...]\n" . "or refer to the the sendxmpp manpage\n"; - + exit 0; } @@ -473,6 +531,7 @@ sub usage () { # the fine manual # =pod + =head1 NAME sendxmpp - send xmpp messages from the commandline. @@ -508,6 +567,10 @@ Use I instead of the one in the configuration file Use jabber I instead of the one in the configuration file. +=item B<-o>,B<--component> I + +Use componentname in connect call. Seems needed for Google talk. + =item B<-r>,B<--resource> I Use resource I for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias' @@ -516,9 +579,17 @@ Use resource I for the sender [default: 'sendxmpp']; when sending to a chat Connect securely, using TLS +=item B<-l>,B<--headline> + +Backward compatibility option. You should use B<--message-type=headline> instead. Send a headline type message (not stored in offline messages) + +=item B<--messages-type> + +Set type of message. Supported types are: B. Default message type is B. Headline type message can be set also with B<--headline> option, see B<--headline> + =item B<-c>,B<--chatroom> -Send the message to a chatroom +Send the message to a chatroom =item B<-s>,B<--subject> I @@ -552,12 +623,12 @@ Show debugging info while running. B: This will include passwords etc. =head1 CONFIGURATION FILE -You may define a 'F<~/.sendxmpprc>' file with the necessary data for your +You may define a 'F<~/.sendxmpprc>' file with the necessary data for your xmpp-account, with a line of the format: =over -I@I I +I@I I I =back @@ -582,7 +653,7 @@ Of course, you may also mix the two: alice@myjabberserver.com;foo.com:1234 secret B: for your security, sendxmpp demands that the configuration -file is owned by you and has file permissions 600. +file is owned by you and readable only to you (permissions 600). =head1 EXAMPLE @@ -590,14 +661,14 @@ file is owned by you and has file permissions 600. or to send to a chatroom: - $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org + $ echo "Dinner Time" | sendxmpp -r TheCook --chatroom test2@conference.jabber.org or to send your system logs somewhere, as new lines appear: - + $ tail -f /var/log/syslog | sendxmpp -i sysadmin@myjabberserver.com - + NOTE: be careful not the overload public jabber services - + =head1 SEE ALSO Documentation for the L module