Import Debian changes 1.22-1
[cascardo/sendxmpp.git] / sendxmpp
index d5546ce..b78fbc2 100755 (executable)
--- a/sendxmpp
+++ b/sendxmpp
@@ -16,7 +16,7 @@ if 0; # not running under some shell
 #
 # 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 $
+# $Platon: sendxmpp/sendxmpp,v 1.22 2010-10-03 19:36:35 rajo Exp $
 # $Id: $
 
 use Authen::SASL qw(Perl); # authentication broken if Authen::SASL::Cyrus module installed
@@ -28,7 +28,7 @@ use open ':utf8';
 use open ':std';
 
 # subroutines decls
-sub xmpp_login($$$$$$$$);
+sub xmpp_login($$$$$$$$$);
 sub xmpp_send ($$$$);
 sub xmpp_send_raw_xml($$);
 sub xmpp_send_message($$$$$$);
@@ -44,12 +44,13 @@ sub terminate();
 sub main();
 
 my # MakeMaker
-$VERSION       = [ q$Revision: 1.15 $ =~ m/(\S+)\s*$/g ]->[0];
+$VERSION       = [ q$Revision: 1.22 $ =~ 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
+# http://tools.ietf.org/html/rfc3921#section-2  section 2.1.1 - Types of Message
+my @suppported_message_types   = qw( chat error groupchat headline );
+my $message_type                               = 'chat'; # default message type
 
 # start!
 &main;
@@ -71,12 +72,13 @@ sub main () {
 
     # login to xmpp
     my $cnx =  xmpp_login ($$cmdline{'jserver'}  || $$config{'jserver'},
-                          $$cmdline{'port'}     || $$config{'port'},
+                          $$cmdline{'port'}     || $$config{'port'} || ($$cmdline{'ssl'} ? 5223 : 5222),
                           $$cmdline{'username'} || $$config{'username'},
                           $$cmdline{'password'} || $$config{'password'},
                           $$cmdline{'component'}|| $$config{'component'},
                           $$cmdline{'resource'},
                           $$cmdline{'tls'},
+                          $$cmdline{'ssl'},
                           $$cmdline{'debug'})
       or error_exit("cannot login: $!");
 
@@ -176,7 +178,7 @@ sub read_config_file ($) {
                # account with specific connection host
                if ($config{'jserver'}  =~ /(.*);([-\.\w]+)/) {
                        $config{'jserver'}      = $2;
-                       $config{'username'}     .= "\@$1";
+                       $config{'username'}     .= "\@$1" unless $config{'component'};
                }
        }
 
@@ -205,7 +207,7 @@ sub parse_cmdline () {
     usage() unless (scalar(@ARGV));
 
        my ($subject,$file,$resource,$jserver,$port,$username,$password,$component,
-       $message, $chatroom, $headline, $debug, $tls, $interactive, $help, $raw, $verbose);
+       $message, $chatroom, $headline, $debug, $tls, $ssl, $interactive, $help, $raw, $verbose);
     my $res = GetOptions ('subject|s=s'    => \$subject,
                          'file|f=s'       => \$file,
                          'resource|r=s'   => \$resource,
@@ -218,6 +220,7 @@ sub parse_cmdline () {
                          'message-type=s' => \$message_type,
                          'chatroom|c'     => \$chatroom,
                          'tls|t'          => \$tls,
+                         'ssl|e'          => \$ssl,
                          'interactive|i'  => \$interactive,
                          'help|usage|h'   => \$help,
                          'debug|d'        => \$debug,
@@ -244,6 +247,10 @@ sub parse_cmdline () {
        if (scalar(grep { $message_type eq $_ } @suppported_message_types) == 0) {
                error_exit("Unsupported message type '$message_type'");
        }
+       
+       if ($ssl && $tls) {
+           error_exit("Connect securely wether using -e (--ssl) or -t (--tls)");
+       }
 
        if ($headline) {
                # --headline withouth --message-type
@@ -272,6 +279,7 @@ sub parse_cmdline () {
                'message-type'    => $message_type,
                'interactive' => ($interactive or 0),
                'tls'         => ($tls or 0),
+               'ssl'         => ($ssl or 0),
                'debug'       => ($debug or 0),
                'verbose'     => ($verbose or 0),
                'raw'         => ($raw or 0),
@@ -290,12 +298,12 @@ sub parse_cmdline () {
 
 #
 # xmpp_login: login to the xmpp (jabber) server
-# input: hostname,port,username,password,resource,tls,debug
+# input: hostname,port,username,password,resource,tls,ssl,debug
 # output: an XMPP connection object
 #
-sub xmpp_login ($$$$$$$$) {
+sub xmpp_login ($$$$$$$$$) {
 
-    my ($host, $port, $user, $pw, $comp, $res, $tls, $debug) = @_;
+    my ($host, $port, $user, $pw, $comp, $res, $tls, $ssl, $debug) = @_;
     my $cnx = new Net::XMPP::Client(debuglevel=>($debug?2:0));
     error_exit "could not create XMPP client object: $!"
        unless ($cnx);
@@ -303,17 +311,20 @@ sub xmpp_login ($$$$$$$$) {
     my @res;
        my $arghash = {
                hostname                => $host,
+               port            => $port,
                tls                             => $tls,
+               ssl             => $ssl,
                connectiontype  => 'tcpip',
                componentname   => $comp
        };
-       $arghash->{port} = $port if ($port);
-       if (!$port) {
+
+       delete $arghash->{port} unless $port; 
+       if ($arghash->{port}) {
                @res = $cnx->Connect(%$arghash);
-               error_exit ("Could not connect to server '$host': $@") unless @res;
+               error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
        } else {
                @res = $cnx->Connect(%$arghash);
-               error_exit ("Could not connect to '$host' on port $port: $@") unless @res;
+               error_exit ("Could not connect to server '$host': $@") unless @res;
        }
 
     xmpp_check_result("Connect",\@res,$cnx);
@@ -394,7 +405,8 @@ sub xmpp_send_message ($$$$$$) {
     my ($cnx, $rcpt, $comp, $subject, $message_type, $msg) = @_;
 
     # for some reason, MessageSend does not return anything
-    $cnx->MessageSend('to'      => $rcpt . ( $comp ? "\@$comp" : '' ),
+       # mimeit01@xmpp.hs-esslingen.de: if $comp IS set, AND the rcpt DOESN'T contain an @, then @comp is added
+    $cnx->MessageSend('to'      => $rcpt . ( ($comp && index($rcpt, "@") == -1) ? "\@$comp" : '' ),
                'type'          => $message_type,
                'subject'       => $subject,
                'body'          => $msg);
@@ -579,6 +591,10 @@ Use resource I<res> for the sender [default: 'sendxmpp']; when sending to a chat
 
 Connect securely, using TLS
 
+=item B<-e>,B<--ssl>
+
+Connect securely, using SSL
+
 =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)