"Fossies" - the Fresh Open Source Software Archive

Member "sendpage-1.001001/snpp" (3 Jan 2008, 5148 Bytes) of package /linux/privat/old/sendpage-1.001001.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #!/usr/bin/perl
    2 #
    3 # quick script for sending SNPP messages
    4 #
    5 # $Id: snpp 205 2005-07-22 17:38:10Z keescook $
    6 #
    7 # Copyright (C) 2000-2004 Kees Cook
    8 # kees@outflux.net, http://outflux.net/
    9 #
   10 # This program is free software; you can redistribute it and/or
   11 # modify it under the terms of the GNU General Public License
   12 # as published by the Free Software Foundation; either version 2
   13 # of the License, or (at your option) any later version.
   14 #
   15 # This program is distributed in the hope that it will be useful,
   16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18 # GNU General Public License for more details.
   19 #
   20 # You should have received a copy of the GNU General Public License
   21 # along with this program; if not, write to the Free Software
   22 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   23 # http://www.gnu.org/copyleft/gpl.html
   24 
   25 =head1 NAME
   26 
   27 snpp - send pages via SNPP
   28 
   29 =head1 SYNOPSIS
   30 
   31 snpp [OPTIONS] recipient...
   32 
   33 =head1 OPTIONS
   34 
   35 =over 4
   36 
   37 =item -s SERVER[:PORT]
   38 
   39 Connect to the specified SERVER (and PORT, if given).  Default is
   40 "localhost:444".
   41 
   42 =item -f USER
   43 
   44 Show that the sent page is coming from USER.  Default is the current user.
   45 
   46 =item -m MESSAGE
   47 
   48 Send the given MESSAGE instead of reading text from stdin.
   49 
   50 =item -n
   51 
   52 Do not notify the 'from' user about the status of the page.
   53 
   54 =item -d
   55 
   56 Display SNPP session debugging.
   57 
   58 =item -C CONF
   59 
   60 Read CONF instead of /etc/snpp.conf for server default.
   61 
   62 =item -h
   63 
   64 Display a summary of all the available command line options.
   65 
   66 =back
   67 
   68 =head1 DESCRIPTION
   69 
   70 This tool is used to send a page via the Simple Network Paging Protocol
   71 (level 2).  It is designed to be used with 'sendpage', but should work
   72 with any other SNPP servers as well.
   73 
   74 The /etc/snpp.conf file can contain a single line in the form of
   75 
   76     server:ADDRESS[:PORT]
   77 
   78 where ADDRESS and PORT are the defaults for snpp.
   79 
   80 =head1 AUTHOR
   81 
   82 Kees Cook <kees@outflux.net>
   83 
   84 =head1 BUGS
   85 
   86 Not much happening in this tool, but I bet the use of "CALLer id" is
   87 not standard, and other SNPP server may require this tool run with
   88 the '-n' option all the time.
   89 
   90 =head1 COPYRIGHT
   91 
   92 snpp is free software; it can be used under the terms of the GNU General
   93 Public License.
   94 
   95 =head1 SEE ALSO
   96 
   97 perl(1), sendpage(1), Net::SNPP(3)
   98 
   99 =cut
  100 
  101 use Getopt::Std;
  102 use Net::SNPP;
  103 use Sys::Hostname::Long;
  104 use IO::File;
  105 
  106 my %opts;
  107 my $VERSION="0.1";
  108 
  109 sub Usage {
  110         die "Usage: $0 [OPTIONS] pin1 [pin2 [...]]
  111 
  112 version $VERSION
  113 
  114 -s SERVER[:PORT]  SNPP server to use (default is 'localhost')
  115 -f USER           force page to be from user USER (default is current user)
  116 -m MESSAGE    message to send (reads from stdin by default)
  117 -n                no email carboning to 'from'
  118 -d                turn debug on
  119 -C CONF       read CONF instead of /etc/snpp.conf
  120 -h                you're reading it.  :)
  121 
  122 ";
  123 }
  124 
  125 
  126 # get our options
  127 if (!getopts('hdns:f:m:C:',\%opts) || $opts{h} || !@ARGV) {
  128         Usage();
  129 }
  130 
  131 ### DEFAULTS
  132 # set config file
  133 $opts{C}="/etc/snpp.conf" unless ($opts{C});
  134 # set server
  135 my $server="localhost";
  136 
  137 
  138 my $fh = new IO::File $opts{C}, "r";
  139 if (!defined($fh)) {
  140         warn "Cannot read '$opts{C}' file (using defaults): $!\n";
  141 }
  142 else {
  143     my $num=0;
  144     my $line;
  145 
  146     foreach $line (<$fh>) {
  147             chomp($line);
  148             $num++;
  149 
  150             # skip comments and blanks
  151             next if ($line =~ /^\s*#/ || $line =~ /^\s*$/);
  152 
  153             ($cmd,$arg)=split(/:/,$line,2);
  154 
  155             if ($cmd eq "server") {
  156             $server=$arg;
  157             }
  158             else {
  159                     warn "unknown command '$cmd' in '$opts{C}', line ".
  160                 "$num\n";
  161             }
  162     }
  163     undef $fh;
  164 }
  165 
  166 # override config file and defaults
  167 $opts{s}=$server unless ($opts{s});
  168 
  169 # verify that there is a "SNPP" service (weakness in Net::SNPP...)
  170 $proto=getprotobyname("tcp");
  171 if (!defined($proto))
  172 {
  173     die "Could not resolve 'tcp' protocol into a protocol number!\nPlease check /etc/protocols\n";
  174 }
  175 else
  176 {
  177     warn "'tcp' is proto $proto\n" if ($opts{d});   
  178 }
  179 $port=getservbyname("snpp","tcp");
  180 if (!defined($port))
  181 {
  182     die "Could not resolve 'snpp' service into a port number!\nPlease check /etc/services\n";
  183 }
  184 else
  185 {
  186     warn "'snpp' is service $port\n" if ($opts{d}); 
  187 }
  188 $snpp = Net::SNPP->new($opts{s}, Debug => $opts{d} );
  189 if (!defined($snpp)) {
  190     die "Could not connect to SNPP server '$opts{s}'\n";
  191 }
  192 
  193 # get the pins
  194 my @pins = @ARGV;
  195 
  196 # get the page text
  197 undef $/;
  198 my $text = $opts{m} ? $opts{m} : <STDIN>;
  199 $/="\n";
  200 
  201 # who is it from?
  202 if (!defined($opts{f}) && !$opts{n}) {
  203     $opts{f}=scalar(getpwuid($<))."\@";
  204     $opts{f}.=hostname_long();
  205 }
  206 
  207 # send pins
  208 foreach $pin (@pins) {
  209     if (!$snpp->pager_id($pin)) {
  210         warn $snpp->message();
  211         if ($snpp->status()==4) {
  212             exit($snpp->code()-400);
  213         }
  214     }
  215 }
  216 
  217 # send 'caller id', if we need to
  218 if (!$opts{n}) {
  219     if (!$snpp->caller_id($opts{f})) {
  220         warn $snpp->message();
  221         if ($snpp->status()==4) {
  222             exit($snpp->code()-400);
  223         }
  224     }
  225 }
  226 
  227 # send text
  228 if (!$snpp->data($text)) {
  229     warn $snpp->message();
  230     if ($snpp->status()==4) {
  231         exit($snpp->code()-400);
  232     }
  233 }
  234 
  235 # issue the send
  236 if (!$snpp->send()) {
  237     warn $snpp->message();
  238     exit($snpp->code()%100);
  239 }
  240 
  241 $snpp->quit;
  242 
  243 undef $snpp;