Platon Technologies
not logged in Login Registration
EnglishSlovak
open source software development celebrating 10 years of open source development! Friday, March 29, 2024

Diff for sendxmpp/sendxmpp between version 1.1.1.1 and 1.1.1.2

version 1.1.1.1, 2006/10/04 21:03:46 version 1.1.1.2, 2006/10/04 21:04:33
Line 1 
Line 1 
 #!/usr/bin/perl -w  #!/usr/bin/perl -w
 #-*-mode:perl-*-  #-*-mode:perl-*-
 #Time-stamp: <2004-11-21 09:59:27 (djcb)>  #Time-stamp: <2004-12-01 14:52:47 (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>
 # released under the terms of the GNU General Public License  # copyright (c) 2004, Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
   #
   # 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  
   # subroutines decls
 sub xmpp_login($$$$$$);  sub xmpp_login($$$$$$);
 sub xmpp_send_message($$$$);  sub xmpp_send_message($$$$);
 sub xmpp_send_group_message($$$$);  sub xmpp_send_chatroom_message($$$$$);
 sub xmpp_logout($);  sub xmpp_logout($);
 sub xmpp_check_result;  sub xmpp_check_result;
 sub parse_cmdline();  sub parse_cmdline();
 sub error_exit;  sub error_exit;
   sub debug_print;
 sub read_config_file($);  sub read_config_file($);
 sub push_hash($$);  sub push_hash($$);
 sub main ();  sub main ();
   
   
 my # MakeMaker  my # MakeMaker
 $VERSION = '0.0.4';  $VERSION     = '0.0.5';
 my $RESOURCE= 'sendxmpp';  my $RESOURCE = 'sendxmpp';
 my $DEBUG = 0;  my $VERBOSE  = 0;
   my $DEBUG    = 0;
   
   
 # start!  # start!
 &main;  &main;
   
   
   #
   # main: main routine
   #
 sub main () {  sub main () {
   
     my $cmdline = parse_cmdline();      my $cmdline = parse_cmdline();
   
     $DEBUG = 1 if ($$cmdline{'debug'});      $DEBUG   = 1 if ($$cmdline{'debug'});
       $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'});
Line 57  sub main () {
Line 69  sub main () {
                            $$cmdline{'subject'},                             $$cmdline{'subject'},
                            $$cmdline{'message'});                             $$cmdline{'message'});
     } else {      } else {
         xmpp_send_group_message ($cnx,          xmpp_send_chatroom_message ($cnx,
                                  $$cmdline{'resource'},                                   $$cmdline{'resource'},
                                    $$cmdline{'subject'},
                                  $$cmdline{'recipient'},                                   $$cmdline{'recipient'},
                                  $$cmdline{'message'});                                   $$cmdline{'message'});
     }      }
Line 68  sub main () {
Line 81  sub main () {
 }  }
   
   
   #
   # read_config_file: read the configuration file
   # input: filename
   # output: hash with 'user', 'jserver' and 'password' keys
   #
 sub read_config_file ($) {  sub read_config_file ($) {
   
       # check permissions
     my $cfg_file = shift;      my $cfg_file = shift;
     error_exit ("cannot read $cfg_file: $!")      error_exit ("cannot read $cfg_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);
Line 89  sub read_config_file ($) {
Line 105  sub read_config_file ($) {
     my %config;      my %config;
     my $line = 0;      my $line = 0;
     while (<CFG>) {      while (<CFG>) {
   
         ++$line;          ++$line;
   
         next if (/^\s*$/);     # ignore empty lines          next if (/^\s*$/);     # ignore empty lines
         next if (/^\s*\#.*/);  # ignore comment lines          next if (/^\s*\#.*/);  # ignore comment lines
         if (/([-\.\w]+)@([-\.\w]+)\s+([-\w\.]+)/) {  
             %config = (username=>$1,          s/\#.*$//; # ignore comments in lines
                        jserver=>$2,  
                        password=>$3);          if (/([-\.\w]+)@([-\.\w]+)\s+(\S+)\s*$/) {
         } else {            close(CFG);              %config = ('username'=>$1,
                          'jserver'=>$2,
                          'password'=>$3);
   
           } else {
               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) {
           while (my ($key,$val) = each %config) {
               debug_print ("config: '$key' => '$val'");
           }
       }
   
     return \%config;      return \%config;
 }  }
   
   
   
   #
   # parse_cmdline: parse commandline options
   # output: hash with commandline options
   #
 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);          $message,$chatroom,$debug,$tls,$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 125  sub parse_cmdline () {
Line 160  sub parse_cmdline () {
                           'chatroom|c'     => \$chatroom,                            'chatroom|c'     => \$chatroom,
                           'tls|t'          => \$tls,                            'tls|t'          => \$tls,
                           'help|usage|h'   => \$help,                            'help|usage|h'   => \$help,
                           'debug|d'        => \$debug);                            'debug|d'        => \$debug,
     usage () if ($help);                            'verbose|v'      => \$verbose);
       usage ()
           if ($help);
   
     my $rcpt = $ARGV[0]      my $rcpt = $ARGV[0]
         or error_exit("no recipient specified");          or error_exit("no recipient specified");
Line 151  sub parse_cmdline () {
Line 188  sub parse_cmdline () {
                 'tls'        => ($tls or 0),                  'tls'        => ($tls or 0),
                 'debug'      => ($debug or 0),                  'debug'      => ($debug or 0),
                 'message'    => ($txt or ''),                  'message'    => ($txt or ''),
                   'verbose'    => ($verbose or 0),
                 'file'       => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),                  'file'       => ($file or ($ENV{'HOME'}.'/.sendxmpprc')),
                 'recipient'  => $rcpt);                  'recipient'  => $rcpt);
   
      if ($DEBUG || $VERBOSE) {
          while (my ($key,$val) = each %dict) {
              debug_print ("cmdline: '$key' => '$val'");
          }
      }
   
     return \%dict;     return \%dict;
 }  }
   
   
   
   #
   # xmpp_login: login to the xmmp (jabber) server
   # input: hostname,username,password,resource,tls,debug
   # output: an XMPP connection object
   #
 sub xmpp_login ($$$$$$) {  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: $!")
           unless ($cnx);
   
     my @res = $cnx->Connect(hostname=>$host,tls=>$tls);      my @res = $cnx->Connect(hostname=>$host,tls=>$tls);
     xmpp_check_result("Connect",\@res);      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);      xmpp_check_result('AuthSend',\@res,$cnx);
   
     @res = $cnx->PresenceSend();      #@res = $cnx->PresenceSend(type=>'unavailable');
     xmpp_check_result("PresenceSend",\@res);      #mpp_check_result("PresenceSend",\@res,$cnx);
   
     return $cnx;      return $cnx;
 }  }
   
   
   #
   # xmmp_send_message: send a message to some xmmp user
   # input: connection,recipient,subject,msg
   #
 sub xmpp_send_message ($$$$) {  sub xmpp_send_message ($$$$) {
   
     my ($cnx,$rcpt,$subject,$body) = @_;      my ($cnx,$rcpt,$subject,$msg) = @_;
     my @res = $cnx->MessageSend(to=>$rcpt,  
                                 subject=>$subject,      # for some reason, MessageSend does not return anything
                                 body=>$body);      $cnx->MessageSend('to'=>$rcpt,
     xmpp_check_result(@res);                        'subject'=>$subject,
 }                        'body'=>$msg);
   
       xmpp_check_result('MessageSend',0,$cnx);
 sub xmpp_send_group_message ($$$$) {  }
   
   
   #
   # xmpp_send_chatroom_message: send a message to a chatroom
   # input: connection,resource,subject,recipient,message
   #
   sub xmpp_send_chatroom_message ($$$$$) {
   
     my ($cnx,$resource,$rcpt,$msg) =  @_;      my ($cnx,$resource,$subject,$rcpt,$msg) =  @_;
   
     # set the presence      # set the presence
     my $pres = new Net::XMPP::Presence;      my $pres = new Net::XMPP::Presence;
     my @res = $pres->SetTo("$rcpt/$resource");      my $res = $pres->SetTo("$rcpt/$resource");
     xmpp_check_result('SetTo',\@res);  
     @res = $cnx->Send($pres);      $cnx->Send($pres);
     xmpp_check_result('Send',\@res);  
   
     # create/send the message      # create/send the message
     my $groupmsg = new Net::XMPP::Message;      my $groupmsg = new Net::XMPP::Message;
     @res = $groupmsg->SetMessage(to=>$rcpt,      $groupmsg->SetMessage(to=>$rcpt,
                                  body=>$msg,                            body=>$msg,
                                  type=>'groupchat');                            subject=>$subject,
     xmpp_check_result('SetMessage',\@res);                            type=>'groupchat');
   
     @res = $cnx->Send($groupmsg);  
     xmpp_check_result('Send',\@res);  
   
       $res = $cnx->Send($groupmsg);
       xmpp_check_result ('Send',$res,$cnx);
   
     # leave the group      # leave the group
     @res = $pres->SetPresence (Type=>'unavailable',To=>$rcpt);      $pres->SetPresence (Type=>'unavailable',To=>$rcpt);
     xmpp_check_result('SetPresence',\@res);  
 }  }
   
   
   #
   # xmmp_logout: log out from the xmpp server
   # input: connection
   #
 sub xmpp_logout($) {  sub xmpp_logout($) {
   
     # HACK      # HACK
Line 222  sub xmpp_logout($) {
Line 290  sub xmpp_logout($) {
   
     my $cnx = shift;      my $cnx = shift;
     $cnx->Disconnect();      $cnx->Disconnect();
       xmpp_check_result ('Disconnect',0); # well, nothing to check, really
 }  }
   
   
   
   #
   # xmpp_check_result: check the return value from some xmpp function execution
   # input: text, result, [connection]
   #
 sub xmpp_check_result {  sub xmpp_check_result {
   
     my ($txt,$res,$cnx)=@_;      my ($txt,$res,$cnx)=@_;
   
     return unless (defined $res && defined @$res && scalar @$res);      error_exit ("Error '$txt': result undefined")
           unless (defined $res);
   
     if (scalar @$res == 1 || $$res[0] eq 'ok') {      # res may be 0
         print STDERR "$txt: " . $$res[0] . "\n"      if ($res == 0) {
             if ($DEBUG);          debug_print "$txt";
     } else {      # result can be true or 'ok'
         error_exit ("Error '$txt': " . join (': ',@$res) . "\n", $cnx);      } 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);
     }      }
 }  }
   
   
   #
   # debug_print: print the data if defined and DEBUG || VERBOSE is TRUE
   # input: [array of strings]
   #
   sub debug_print {
       print STDERR "sendxmpp: " . (join ' ',@_) . "\n"
           if (@_ && ($DEBUG ||$VERBOSE));
   }
   
   
   #
   # error_exit: print error message and exit the program
   #             logs out if there is a connection
   # input: error, [connection]
   #
 sub error_exit {  sub error_exit {
   
     my ($err,$cnx) = @_;      my ($err,$cnx) = @_;
     print STDERR "$err\n";      print STDERR "$err\n";
       xmpp_logout ($cnx)
     xmpp_logout ($cnx) if ($cnx);          if ($cnx);
   
     exit 1;      exit 1;
 }  }
   
   
   #
   # usage: print short usage message and exit
   #
 sub usage () {  sub usage () {
   
     print      print
Line 256  sub usage () {
Line 356  sub usage () {
         "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";
   
     exit(0);      exit 0;
 }  }
   
 # the manpage  
   
   
   #
   # the fine manual
   #
 =head1 NAME  =head1 NAME
   
 sendxmpp - send xmpp messages from the commandline.  sendxmpp - send xmpp messages from the commandline.
Line 292  B<-j>,B<--jserver> <server>
Line 395  B<-j>,B<--jserver> <server>
 use jabber server <server> instead of the one in the configuration file  use jabber server <server> instead of the one in the configuration file
   
 B<-r>,B<--resource> <res>  B<-r>,B<--resource> <res>
 use resource <res> for the sender [default: 'sendxmpp']  use resource <res> for the sender [default: 'sendxmpp']; when sending to a chatroom, this determines the 'alias'
   
 B<-t>,B<--tls>  B<-t>,B<--tls>
 connect securely, using TLS  connect securely, using TLS
Line 301  B<-c>,B<--chatroom>
Line 404  B<-c>,B<--chatroom>
 send the message to a chatroom  send the message to a chatroom
   
 B<-s>,B<--subject> <subject>  B<-s>,B<--subject> <subject>
 set the subject for the message to <subject> [default: '']  set the subject for the message to <subject> [default: '']; when sending to a chatroom,
   this will set the subject for the chatroom
   
 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<-v>,B<--verbose>
   give verbose output about what is happening
   
 B<-h>,B<--help>,B<--usage>  B<-h>,B<--help>,B<--usage>
 show a 'Usage' message  show a 'Usage' message
   
 B<-d>,B<--debug>  B<-d>,B<--debug>
 show debugging info while running  show debugging info while running. B<WARNING>: This will include passwords etc. so be careful with the output!
   
 =head1 CONFIGURATION FILE  =head1 CONFIGURATION FILE
   

Legend:
Removed from v.1.1.1.1  
changed lines
  Added in v.1.1.1.2

Platon Group <platon@platon.org> http://platon.org/
Copyright © 2002-2006 Platon Group
Site powered by Metafox CMS
Go to Top