X-Git-Url: http://git.cascardo.eti.br/?a=blobdiff_plain;f=sendxmpp;h=3d870ef826bf82ab34db802617aee039b9b143af;hb=e752831f4d976af4154000f055286747acb04f3f;hp=fd6523e6d3bbc616e864b35796b656cb8f6614f8;hpb=116559255440c02ce649b717b0ed6a8ae6456184;p=cascardo%2Fsendxmpp.git diff --git a/sendxmpp b/sendxmpp index fd6523e..3d870ef 100755 --- a/sendxmpp +++ b/sendxmpp @@ -1,26 +1,37 @@ #!/usr/bin/perl -w eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}' - if 0; # not running under some shell -#-*-mode:perl-*- -#Time-stamp: <2005-05-07 19:24:09 (djcb)> +if 0; # not running under some shell +# # 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 - +# somewhat resembling mail(1) +# +# Author: Dirk-Jan C. Binnema +# Maintainer: Lubomir Host 'rajo' +# Copyright (c) 2004 - 2005 Dirk-Jan C. Binnema +# Copyright (c) 2006 - 2007 Lubomir Host 'rajo' +# +# Homepage: http://sendxmpp.platon.sk +# # Released under the terms of the GNU General Public License v2 +# +# $Platon: sendxmpp/sendxmpp,v 1.14 2008-08-25 09:54:12 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'; + # subroutines decls -sub xmpp_login($$$$$$$); -sub xmpp_send ($$$); -sub xmpp_send_message($$$$); +sub xmpp_login($$$$$$$$); +sub xmpp_send ($$$$); +sub xmpp_send_raw_xml($$); +sub xmpp_send_message($$$$$$); sub xmpp_send_chatroom_message($$$$$); sub xmpp_logout($); sub xmpp_check_result; @@ -33,7 +44,7 @@ sub terminate(); sub main(); my # MakeMaker -$VERSION = '0.0.8'; +$VERSION = [ q$Revision: 1.14 $ =~ m/(\S+)\s*$/g ]->[0]; my $RESOURCE = 'sendxmpp'; my $VERBOSE = 0; my $DEBUG = 0; @@ -61,6 +72,7 @@ sub main () { $$cmdline{'port'} || $$config{'port'}, $$cmdline{'username'} || $$config{'username'}, $$cmdline{'password'} || $$config{'password'}, + $$cmdline{'component'}|| $$config{'component'}, $$cmdline{'resource'}, $$cmdline{'tls'}, $$cmdline{'debug'}) @@ -82,7 +94,7 @@ sub main () { $txt.=$_ while (); } - xmpp_send ($cnx,$cmdline,$txt); + xmpp_send ($cnx,$cmdline,$config,$txt); } else { # the interactive case, read stdin line by line @@ -94,7 +106,7 @@ sub main () { # line by line... while () { chomp; - xmpp_send ($cnx,$cmdline,$_); + xmpp_send ($cnx,$cmdline,$config,$_); } } @@ -115,42 +127,55 @@ sub read_config_file ($) { my $cfg_file = shift; error_exit ("cannot read $cfg_file: $!") unless (-r $cfg_file); - my $owner = (stat($cfg_file))[4]; + 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); + 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; @@ -176,49 +201,64 @@ sub parse_cmdline () { usage() unless (scalar(@ARGV)); - my ($subject,$file,$resource,$jserver,$port,$username,$password, - $message,$chatroom,$debug,$tls,$interactive,$help,$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, 'chatroom|c' => \$chatroom, 'tls|t' => \$tls, 'interactive|i' => \$interactive, 'help|usage|h' => \$help, 'debug|d' => \$debug, + 'raw|w' => \$raw, 'verbose|v' => \$verbose); - usage () - if ($help); - - my $rcpt = $ARGV[0] - or error_exit "no recipient specified"; - - if ($message && $interactive) { - error_exit "cannot have both -m (--message) and -i (--interactive)\n"; - } + usage () if ($help); - if ($jserver && $jserver =~ /(.*):(\d+)/) { - $jserver = $1; - $port = $2; - } - + my @rcpt = @ARGV; + + if (defined($raw) && scalar(@rcpt) > 0) { + 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"); + } + if ($raw && $chatroom) { + error_exit("The chatroom option is pointless in raw XML mode"); + } + + if ($message && $interactive) { + error_exit "Cannot have both -m (--message) and -i (--interactive)"; + } + + if ($jserver && $jserver =~ /(.*):(\d+)/) { + $jserver = $1; + $port = $2; + } + my %dict = ('subject' => ($subject or ''), + '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), + 'headline' => ($headline or 0), 'interactive' => ($interactive or 0), 'tls' => ($tls or 0), 'debug' => ($debug or 0), 'verbose' => ($verbose or 0), + 'raw' => ($raw or 0), 'file' => ($file or ($ENV{'HOME'}.'/.sendxmpprc')), - 'recipient' => $rcpt); + 'recipient' => \@rcpt); if ($DEBUG || $VERBOSE) { while (my ($key,$val) = each %dict) { @@ -235,23 +275,37 @@ 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); my @res; - if (!$port) { - @res = $cnx->Connect(hostname=>$host,tls=>$tls); - } else { - @res = $cnx->Connect(hostname=>$host,port=>$port,tls=>$tls); - } + my $arghash = { + hostname => $host, + tls => $tls, + connectiontype => 'tcpip', + componentname => $comp + }; + $arghash->{port} = $port if (!$port); + if (!$port) { + @res = $cnx->Connect(%$arghash); + error_exit ("Could not connect to server '$host': $@") unless @res; + } else { + @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); @@ -267,38 +321,70 @@ sub xmpp_login ($$$$$$$) { # xmmp_send: send the message, determine from cmdline # whether it's to individual or chatroom # -sub xmpp_send ($$$) { +sub xmpp_send ($$$$) { - my ($cnx, $cmdline, $txt) = @_; + my ($cnx, $cmdline, $config, $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); - } + unless ($$cmdline{'chatroom'}) { + unless ($$cmdline{'raw'}) { + map { + xmpp_send_message ($cnx, + $_, #$$cmdline{'recipient'}, + $$cmdline{'component'} || $$config{'component'}, + $$cmdline{'subject'}, + $$cmdline{'headline'}, + $txt) + } @{$$cmdline{'recipient'}}; + } + else { + xmpp_send_raw_xml ($cnx, $txt); + } + } + else { + map { + xmpp_send_chatroom_message ($cnx, + $$cmdline{'resource'}, + $$cmdline{'subject'}, + $_, # $$cmdline{'recipient'}, + $txt) + } @{$$cmdline{'recipient'}}; + } } +# +# xmpp_send_raw_xml: send a raw XML packet +# 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); +} + + # # xmpp_send_message: send a message to some xmpp user # input: connection,recipient,subject,msg # -sub xmpp_send_message ($$$$) { +sub xmpp_send_message ($$$$$$) { - my ($cnx,$rcpt,$subject,$msg) = @_; + my ($cnx,$rcpt,$comp,$subject,$headline,$msg) = @_; + + my $type = 'message'; + if ($headline) { + $type='headline'; + } # for some reason, MessageSend does not return anything - $cnx->MessageSend('to' => $rcpt, - 'subject' => $subject, - 'body' => $msg); + $cnx->MessageSend('to' => $rcpt . ( $comp ? "\@$comp" : '' ), + 'type' => $type, + 'subject' => $subject, + 'body' => $msg); xmpp_check_result('MessageSend',0,$cnx); } @@ -322,7 +408,6 @@ sub xmpp_send_chatroom_message ($$$$$) { my $groupmsg = new Net::XMPP::Message; $groupmsg->SetMessage(to => $rcpt, body => $msg, - subject => $subject, type => 'groupchat'); $res = $cnx->Send($groupmsg); @@ -354,24 +439,26 @@ sub xmpp_logout($) { # xmpp_check_result: check the return value from some xmpp function execution # input: text, result, [connection] # -sub xmpp_check_result { - - my ($txt,$res,$cnx)=@_; +sub xmpp_check_result +{ + 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); + } } @@ -417,8 +504,10 @@ sub error_exit { sub usage () { print STDERR - "sendxmpp version $VERSION, Copyright (c) 2004,2005 Dirk-Jan C. Binnema\n" . - "usage: sendxmpp [options] \n" . + "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; @@ -435,7 +524,9 @@ sendxmpp - send xmpp messages from the commandline. =head1 SYNOPSIS -sendxmpp [options] +sendxmpp [options] [ ...] + +sendxmpp --raw [options] =head1 DESCRIPTION @@ -444,66 +535,107 @@ unlike L. Messages can be sent both to individual recipients and chatro =head1 OPTIONS -B<-f>,B<--file> -use configuration file instead of ~/.sendxmpprc +=over + +=item B<-f>,B<--file> I + +Use I configuration file instead of F<~/.sendxmpprc> + +=item B<-u>,B<--username> I + +Use I instead of the one in the configuration file + +=item B<-p>,B<--password> I + +Use I instead of the one in the configuration file + +=item B<-j>,B<--jserver> I + +Use jabber I instead of the one in the configuration file. + +=item B<-o>,B<--component> I -B<-u>,B<--username> -use instead of the one in the configuration file +Use componentname in connect call. Seems needed for Google talk. -B<-p>,B<--password> -use instead of the one in the configuration file +=item B<-r>,B<--resource> I -B<-j>,B<--jserver> -use jabber server instead of the one in the configuration file. Note that you can add : to use a non-default port, ie. B<-j myjabber.org:1234> +Use resource I for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias' -B<-r>,B<--resource> -use resource for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias' +=item B<-t>,B<--tls> -B<-t>,B<--tls> -connect securely, using TLS +Connect securely, using TLS -B<-c>,B<--chatroom> -send the message to a chatroom +=item B<-l>,B<--headline> -B<-s>,B<--subject> -set the subject for the message to [default: '']; when sending to a chatroom, -this will set the subject for the chatroom +Send a headline type message (not stored in offline messages) -B<-m>,B<--message> -read the message from (a file) instead of stdin +=item B<-c>,B<--chatroom> -B<-i>,B<--interactive> -work in interactive mode, reading lines from stdin and sending the one-at-time +Send the message to a chatroom -B<-v>,B<--verbose> -give verbose output about what is happening +=item B<-s>,B<--subject> I -B<-h>,B<--help>,B<--usage> -show a 'Usage' message +Set the subject for the message to I [default: '']; when sending to a chatroom, this will set the subject for the chatroom -B<-d>,B<--debug> -show debugging info while running. B: This will include passwords etc. so be careful with the output! +=item B<-m>,B<--message> I + +Read the message from I (a file) instead of stdin + +=item B<-i>,B<--interactive> + +Work in interactive mode, reading lines from stdin and sending the one-at-time + +=item B<-w>,B<--raw> + +Send raw XML message to jabber server + +=item B<-v>,B<--verbose> + +Give verbose output about what is happening + +=item B<-h>,B<--help>,B<--usage> + +Show a 'Usage' message + +=item B<-d>,B<--debug> + +Show debugging info while running. B: This will include passwords etc. so be careful with the output! + +=back =head1 CONFIGURATION FILE -You may define a '~/.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 + +=back e.g.: # my account alice@jabber.org secret -('#' and newlines are allowed like in shellscripts). You can add : to -the if you need an alternative port, ie. +('#' and newlines are allowed like in shellscripts). You can add a I (or IP address) if it is different from the I part of your JID: + + # account with specific connection host + alice@myjabberserver.com;foo.com secret + +You can also add a I if it is not the standard XMPP port: # account with weird port number - alice@myjabberhost.com:1234 secret - + alice@myjabberserver.com:1234 secret + +Of course, you may also mix the two: + + # account with a specific host and port + 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 @@ -523,13 +655,14 @@ file is owned by you and has file permissions 600. Documentation for the L module -The jabber homepage: http://www.jabber.org/ +The jabber homepage: L -The sendxmpp homepage: http://www.djcbsoftware.nl/code/sendxmpp +The sendxmpp homepage: L =head1 AUTHOR sendxmpp has been written by Dirk-Jan C. Binnema , and uses -the L modules written by Ryan Eatmon. +the L modules written by Ryan Eatmon. Current maintainer is +Lubomir Host 'rajo' , L =cut