home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / sendip.zip / sendipad.cmd < prev    next >
OS/2 REXX Batch file  |  1996-06-08  |  19KB  |  582 lines

  1. /*-----------------------------------------------------------------*/
  2. /* This program assumes that a connection to TCP/IP has already    */
  3. /* been established before its execution.                          */
  4. /*-----------------------------------------------------------------*/
  5.  
  6. FROM_NAME = 'Gary L. Robinson <grobin@erinet.com>'
  7. fileout = 'ipadr.doc'
  8.  
  9. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'; call SysLoadFuncs
  10.  
  11. '@hostname.exe | RxQueue'   /* The command whose output is being trapped. */
  12. Do While Queued() > 0
  13.   Parse Pull hostname
  14.   End
  15. say 'My hostname is' hostname
  16. '@set hostname=' || hostname
  17.  
  18. '@netstat -a | RxQueue'   /* The command whose output is being trapped. */
  19. Do While Queued() > 0
  20.   Parse Pull ifdef
  21.   parse var ifdef 'addr' ipaddr 'interface' ifnum . 'broadcast' ipdest
  22.   if ifnum>9 then do
  23.     ipaddr = strip(ipaddr)
  24.     ipdest = strip(ipdest)
  25.     say 'For interface' ifnum', my address is' ipaddr
  26.     say '         destination address is' ipdest
  27.     end
  28.   end
  29.  
  30.  
  31. call LINEOUT fileout, 'From:' FROM_NAME, 1
  32. call LINEOUT fileout, 'Subject: Ping! This is my current I.P. address'
  33. call LINEOUT fileout, 'X-Mailer: Send IP address Utility'
  34. call LINEOUT fileout, ''
  35. call LINEOUT fileout, 'Ping! Ping! Ping! Ping! Ping! Ping! Ping!'
  36. call LINEOUT fileout, 'If you are on the net call me on Intercom .....'
  37. call LINEOUT fileout, ''
  38. call LINEOUT fileout, 'my IP address is : 'ipaddr
  39. call LINEOUT fileout
  40.  
  41. call SysDropFuncs
  42.  
  43.  Call READ_DEFAULTS_FILE "defaults.txt"
  44.  
  45.  ret_code     = 0
  46.  defserver    = def.SMTP_SERVER
  47.  defuser      = def.TARGET_ADDRESS
  48.  deffile      = def.NOTE_FILE
  49.  
  50.  port         = def.SMTP_PORT
  51.  reconnect    = def.RECONNECT_AFTER
  52.  timezone     = def.TIME_ZONE
  53.  connecttime  = def.CONNECT_TIME
  54.  downloadtime = def.DOWNLOAD_TIME
  55.  idletime     = def.IDLE_TIME
  56.  
  57.  Parse Arg args
  58.  
  59.  /*----------------------------------------------------------*/
  60.  /* load the needed functions and check the input parameters */
  61.  /*----------------------------------------------------------*/
  62.  ret_code = Init_Cmd(args) 
  63.  if ret_code = 0 then do
  64.    /*----------------------------------*/
  65.    /* Call a routine to connect to the */
  66.    /* SMTP mail server                 */
  67.    /*----------------------------------*/
  68.    socket = CONNECT_TO_SMTP(server,port,connecttime,idletime)
  69.    if socket > 0 then do
  70.      /*----------------------------------------------------------*/
  71.      /* the connection (socket) number must be saved and used    */
  72.      /* each time you wish to issue a command to the TCP/IP host */
  73.      /*----------------------------------------------------------*/
  74.   
  75.      /*-----------------------------------------*/
  76.      /* determine how many "send to"s there are */
  77.      /* i.e. is the submitted value an id or a  */
  78.      /* list of userids.                        */
  79.      /*-----------------------------------------*/
  80.      if GET_SEND_TO_LIST(userid) > 0 then do
  81.        /*----------------------------------------*/
  82.        /* Read the note text file plucking out   */
  83.        /* the "From:" address and assigning      */
  84.        /* the note date value.                   */
  85.        /*----------------------------------------*/
  86.        if READ_NOTE_FILE(appfile,timezone) > 0 then do
  87.          /*-------------------------------------------------*/
  88.          /* loop thru the sendto userids and send the notes */
  89.          /*-------------------------------------------------*/
  90.          do i = 1 to sendto.0
  91.            userid = sendto.i
  92.  
  93.            Call RxIpConsSay 'Working on ('i' of 'sendto.0')...'userid
  94.  
  95.            /*---------------------------------------*/
  96.            /* call a rexx function to send the note */
  97.            /*---------------------------------------*/
  98.            ret_code = SEND_NOTE(socket,userid,idletime)
  99.  
  100.            if ret_code <> 0 | i // reconnect = 0 then do
  101.              /*-----------------------------------------*/
  102.              /* close the current socket and re-connect */
  103.              /* to avoid SMTP problems                  */
  104.              /*-----------------------------------------*/
  105.              socket = CONNECT_TO_SMTP(server,port,connecttime,idletime)
  106.              if socket <= 0 then LEAVE
  107.            end
  108.          end /* do */
  109.        end /* if */
  110.      end /* if */
  111.      else do
  112.        Say 'No valid "send to" userids were found.'
  113.      end
  114.  
  115.      /*-------------------------------*/
  116.      /* quit the mail server session. */
  117.      /*-------------------------------*/
  118.      Say
  119.      Call RxIpPutStr socket,"QUIT"
  120.      str = RxIpGetStrWait(socket,idletime)
  121.      if datatype(str) <> 'NUM' then say str
  122.  
  123.      say
  124.      say 'SMTP Mail process completed...'
  125.      
  126.      /*--------------------------------------*/
  127.      /* close the open TCP/IP connection.    */
  128.      /* This does not disconnect your modem  */
  129.      /* connection                           */
  130.      /*--------------------------------------*/
  131.      if socket > 0 then ret_code = RxIpClose(socket)
  132.    end /* if */
  133.  end
  134.  Say ''
  135.  Say 'Press any key .....'
  136.  CharIn()
  137.  Exit(ret_code)
  138.  
  139. /*--------------------------------------------------------------------*/
  140. /* Load some of the external functions                                */
  141. /*--------------------------------------------------------------------*/
  142. Init_Cmd:Procedure Expose server userid appfile,
  143.                    defserver deffile defuser 
  144.  Parse Arg args
  145.  
  146.  ret_code = 0
  147.  
  148.  Say '------------------------------------------------------------'
  149.  Say 'SENDNOTE - Send an SMTP note to a user or list of users'
  150.  Say
  151.  Say 'format: SENDNOTE <server> <note file> <userid/list>'
  152.  Say '------------------------------------------------------------'
  153.  if args = '?' | translate(args) = '/H' | translate(args) = '-H' then
  154.    ret_code = 1
  155.  
  156.  if ret_code = 0 then do
  157.    Say 'Initializing library functions...'
  158.  
  159.    If RxFuncQuery('SysLoadFuncs') then do
  160.      /*----------------------------------*/
  161.      /* load extended REXX functions     */
  162.      /*----------------------------------*/
  163.      If RxFuncAdd('SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs') then do
  164.        ret_code = 1
  165.        say "Error: Couldn't load RexxUtil library"
  166.      end
  167.      call SysLoadFuncs
  168.    end
  169.  end
  170.  
  171.  if ret_code = 0 then do
  172.    if RxFuncQuery('RxIpInit') then do
  173.      /*------------------------------------*/
  174.      /* Load the SurfRexx TCP/IP functions */
  175.      /*------------------------------------*/
  176.      if RxFuncAdd('RxIpInit','SurfRexx','RxIpInit') then do
  177.        say "Error: Couln't load SurfRexx library"
  178.        ret_code = 1
  179.      end
  180.      Call RxIpInit
  181.    end
  182.  end
  183.  
  184.  if ret_code = 0 then do
  185.    Say RxIpVersion()
  186.  
  187.    /*--------------------------------------------*/
  188.    /* check input parameters and assign defaults */
  189.    /*--------------------------------------------*/
  190.    Say 'Checking input values...'
  191.    Say
  192.  
  193.    Parse Var args server appfile userid
  194.  
  195.    if server = '.' then do
  196.      server = defserver
  197.    end
  198.    else if server = '' then do
  199.      Say 'Please enter an SMTP mail server: (default = 'defserver')'
  200.      parse pull server
  201.      if strip(server) = '' then server = defserver
  202.    end
  203.  
  204.    if appfile = '.' then do
  205.      appfile = deffile
  206.    end
  207.    else if appfile = '' then do
  208.      Say 'Please enter your note text filename: (default = 'deffile')'
  209.      parse pull appfile
  210.      if strip(appfile) = '' then appfile = deffile
  211.    end
  212.  
  213.    if userid = '.' then do
  214.      userid = defuser
  215.    end
  216.    else if userid = '' then do
  217.      Say 'Please enter a target userid/list of ids: (default = 'defuser')'
  218.      parse pull userid
  219.      if strip(userid) = '' then userid = defuser
  220.    end
  221.  
  222.    if server = '' | userid = '' | appfile = '' then ret_code = 1
  223.    Say
  224.  end
  225.  
  226.  return(ret_code)
  227.  
  228. /*-----------------------------------------------------------------------*/
  229. /* Connect to the given SMTP server                                      */
  230. /*-----------------------------------------------------------------------*/
  231. CONNECT_TO_SMTP:Procedure 
  232.   Parse Arg server,port,connecttime,idletime
  233.  
  234.   /*----------------------------------*/
  235.   /* connect to the SMTP mail server  */
  236.   /*----------------------------------*/
  237.   say 'Connecting to SMTP mail server....'server
  238.   say '                          port....('port')'
  239.  
  240.   /*------------------------------------------------------*/
  241.   /* the function RxIpConnectWait establishes a TCP/IP    */
  242.   /* connection (socket) with the given host at the given */
  243.   /* port number or gives up after x seconds of trying.   */
  244.   /*------------------------------------------------------*/
  245.   socket = RxIpConnectWait(server,port,connecttime)
  246.   if socket > 0 then do
  247.     str = RxIpGetStrWait(socket,idletime)
  248.     if datatype(str) <> 'NUM' then do
  249.       if pos('220',str) = 1 then do
  250.         Say str
  251.         Say 'Connection was successfully...'server
  252.  
  253.         /*----------------------------------*/
  254.         /* the HELO command is a requirment */
  255.         /* of most SMTP servers.            */
  256.         /*----------------------------------*/
  257.         rc = RxIpPutStr(socket,'HELO 'server)
  258.         if rc <> 0 then do
  259.           Say 'SurfRexx Error('rc') sending information to the server.'
  260.           Say RxIpErrorText(rc)
  261.         end
  262.  
  263.         /*-------------------------------------------------*/
  264.         /* retrieve any other logon messages that the SMTP */
  265.         /* site may have for us.                           */
  266.         /*-------------------------------------------------*/
  267.         rc = ""
  268.         do while datatype(rc) <> 'NUM'
  269.           rc = RxIpGetStrWait(socket,5)
  270.           if datatype(rc) <> 'NUM' then say rc
  271.         end
  272.       end
  273.       else do
  274.         Say 'SMTP Error connecting to the server.'
  275.         RxIpClose(socket);
  276.         socket = 0
  277.       end
  278.     end
  279.     else do
  280.       Say 'SurfRexx Error('str') reading information from the server.'
  281.       Say RxIpErrorText(str)
  282.     end
  283.   end
  284.   else do
  285.     /*--------------------*/
  286.     /* connection failed  */
  287.     /*--------------------*/
  288.     say 'SurfRexx Error('socket') connecting to mail server.'
  289.     say RxIpErrorText(socket)
  290.   end
  291.  
  292.   RETURN(socket)
  293.  
  294. /*------------------------------------------------------------------------*/
  295. /* Create a list of send to userids                                       */
  296. /*------------------------------------------------------------------------*/
  297. GET_SEND_TO_LIST:Procedure Expose sendto.
  298.   Parse Arg userid
  299.  
  300.   sendto. = 0
  301.   cnt     = 0
  302.  
  303.   if pos('@',userid) > 0 & pos('.',userid) > 0 & length(userid) > 12 then do
  304.     cnt        = cnt + 1
  305.     sendto.cnt = userid
  306.   end
  307.   else do
  308.     /*-----------------------------------*/
  309.     /* Must be a file so read its values */
  310.     /*-----------------------------------*/
  311.     file = userid
  312.     Do While Lines(file) > 0
  313.       line = LineIn(file)
  314.       /*---------------------------------------------*/
  315.       /* anything after an asterisk '*' is a comment */
  316.       /* and can be ignored.                         */
  317.       /*---------------------------------------------*/
  318.       Parse Var line userid'*'.
  319.       if strip(userid) <> '' then do
  320.         cnt        = cnt + 1
  321.         sendto.cnt = userid
  322.       end  
  323.     end /* do */
  324.     /*-----------------------------------*/
  325.     /* Make sure that the file is closed */
  326.     /*-----------------------------------*/
  327.     Call LineOut file
  328.   end /* else */
  329.  
  330.   sendto.0 = cnt
  331.  
  332.   RETURN(sendto.0)
  333.  
  334. /*-------------------------------------------------------------------------*/
  335. /* Read the file to be sent as the note text                               */
  336. /*-------------------------------------------------------------------------*/
  337. READ_NOTE_FILE:Procedure Expose note.
  338.   Parse Arg file,timezone
  339.  
  340.   Say 'Reading note file...'file
  341.  
  342.   note. = 0
  343.   Do While Lines(file) > 0
  344.     line     = LineIn(file)
  345.  
  346.     if pos('FROM:',translate(line)) = 1 & note.FROM = 0 then do
  347.       note.FROM = strip(delstr(line,1,5))
  348.     end
  349.  
  350.     note.0   = note.0 + 1
  351.     tot      = note.0
  352.     note.tot = line
  353.   end
  354.  
  355.   if note.0 = 0 then do
  356.     Say '*********'
  357.     Say 'Error *** Reading note file ('file') ***'
  358.     Say '*********'
  359.   end
  360.   else if note.FROM = 0 then do
  361.     Say '*********'
  362.     Say 'Error *** No "From:" line was found in the note file ('file') ***'
  363.     Say '*********'
  364.     note. = 0
  365.   end
  366.   else do
  367.     /*--------------------------------------*/
  368.     /* Assign a date to the current note(s) */
  369.     /*--------------------------------------*/
  370.     note.DATE = GET_INTERNET_DATE(timezone)
  371.   end
  372.  
  373.   RETURN(note.0)
  374.  
  375. /*------------------------------------------------------------------------*/
  376. /* Return the current date in internet format                             */
  377. /*------------------------------------------------------------------------*/
  378. GET_INTERNET_DATE:Procedure
  379.   Parse Arg timezone
  380.  
  381.   days.  = 0
  382.  
  383.   mons.  = 0
  384.   mons.1 = "Jan";mons.2  = "Feb";mons.3  = "Mar";mons.4  = "Apr"
  385.   mons.5 = "May";mons.6  = "Jun";mons.7  = "Jul";mons.8  = "Aug"
  386.   mons.9 = "Sep";mons.10 = "Oct";mons.11 = "Nov";mons.12 = "Dec"
  387.  
  388.   Parse Value date('SORTED') With 1 yr 5 mm 7 dd
  389.  
  390.   dd = dd + 1
  391.   mm = mm + 1
  392.  
  393.   date = substr(date('WEEKDAY'),1,3)',' dd mons.mm yr time() timezone
  394.  
  395.   RETURN(date)
  396.  
  397. /*------------------------------------------------------------------------*/
  398. /* Send the current note text to the given user                           */
  399. /*------------------------------------------------------------------------*/
  400. SEND_NOTE:Procedure Expose note.
  401.   Parse Arg socket,tuserid,idletime
  402.  
  403.   ret_code = 0
  404.  
  405.   tuser = tuserid
  406.   if pos('<',tuser) > 0 then do
  407.     Parse Var tuser tname '<'tuser'>'
  408.   end
  409.   else tname = ''
  410.   tname = strip(tname)
  411.   tuser = strip(tuser)
  412.  
  413.   fuserid  = note.FROM
  414.   fuser    = fuserid
  415.   if pos('<',fuser) > 0 then do
  416.     Parse Var fuser fname '<'fuser'>'
  417.   end
  418.   else fname = ''
  419.   fname = strip(fname)
  420.   fuser = strip(fuser)
  421.  
  422.   /*-------------------------------*/
  423.   /* Tell SMTP to start a new note */
  424.   /*-------------------------------*/
  425.   rc = RxIpPutStr(socket,'MAIL FROM:<'fuser'>');
  426.   if rc = 0 then do
  427.     str = RxIpGetStrWait(socket,idletime)
  428.     if datatype(str) <> 'NUM' then do
  429.       if pos('250',str) <> 1 then do
  430.         ret_code = 1
  431.         Say 'SMTP Error sending note to ('tuserid')'
  432.         Say str
  433.       end
  434.     end
  435.     else do
  436.       ret_code = 1
  437.       Say 'SurfRexx Error('str') reading information from the server.'
  438.       Say RxIpErrorText(str)
  439.     end
  440.   end
  441.   else do
  442.     ret_code = 1
  443.     Say 'SurfRexx Error('rc') sending information to the server.'
  444.     Say RxIpErrorText(rc)
  445.   end
  446.  
  447.   if ret_code = 0 then do
  448.     /*------------------------------------*/
  449.     /* Tell SMTP who the note is going to */
  450.     /*------------------------------------*/
  451.     rc = RxIpPutStr(socket,'RCPT TO:<'tuser'>');
  452.     if rc = 0 then do
  453.       str = RxIpGetStrWait(socket,idletime)
  454.       if datatype(str) <> 'NUM' then do
  455.         if pos('250',str) <> 1 then do
  456.           ret_code = 1
  457.           Say 'SMTP Error sending note to ('tuserid')'
  458.           Say str
  459.         end
  460.       end
  461.       else do
  462.         ret_code = 1
  463.         Say 'SurfRexx Error('str') reading information from the server.'
  464.         Say RxIpErrorText(str)
  465.       end
  466.     end
  467.     else do
  468.       ret_code = 1
  469.       Say 'SurfRexx Error('rc') sending information to the server.'
  470.       Say RxIpErrorText(rc)
  471.     end
  472.   end
  473.  
  474.   if ret_code = 0 then do
  475.     /*-----------------------------------------------*/
  476.     /* Tell SMTP that the note body is being entered */
  477.     /*-----------------------------------------------*/
  478.     rc = RxIpPutStr(socket,'DATA');
  479.     if rc = 0 then do
  480.       str = RxIpGetStrWait(socket,idletime)
  481.       if datatype(str) <> 'NUM' then do
  482.         if pos('354',str) <> 1 then do
  483.           ret_code = 1
  484.           Say 'SMTP Error sending note to ('tuserid')'
  485.           Say str
  486.         end
  487.       end
  488.       else do
  489.         ret_code = 1
  490.         Say 'SurfRexx Error('str') reading information from the server.'
  491.         Say RxIpErrorText(str)
  492.       end
  493.     end
  494.     else do
  495.       ret_code = 1
  496.       Say 'SurfRexx Error('rc') sending information to the server.'
  497.       Say RxIpErrorText(rc)
  498.     end
  499.   end
  500.  
  501.   if ret_code = 0 then do
  502.     /*---------------------*/
  503.     /* enter the note body */
  504.     /*---------------------*/
  505.     do i = -2 to note.0
  506.       Select
  507.         When i = -2 then line = "X-Mailer: InnoVal's Surf'n Rexx"
  508.         When i = -1 then line = "To: "tuserid
  509.         When i =  0 then line = "Date: "note.DATE
  510.         Otherwise        line = note.i
  511.       end /* select */
  512.  
  513.       if line = '.' then line = '..'
  514.  
  515.       rc = RxIpPutStr(socket,line);
  516.       if rc <> 0 then do
  517.         ret_code = 1
  518.         Say 'SurfRexx Error('rc') sending information to the server.'
  519.         Say RxIpErrorText(rc)
  520.         LEAVE
  521.       end /* if */
  522.     end /* do...i */
  523.   end
  524.  
  525.   if ret_code = 0 then do
  526.     /*--------------*/
  527.     /* end the note */
  528.     /*--------------*/
  529.     rc = RxIpPutStr(socket,'.')
  530.     if rc = 0 then do
  531.       str = RxIpGetStrWait(socket,idletime)
  532.       if datatype(str) <> 'NUM' then do
  533.         if pos('250',str) <> 1 then do
  534.           ret_code = 1
  535.           Say 'SMTP Error sending note to ('tuserid')'
  536.           Say str
  537.         end
  538.       end
  539.       else do
  540.         ret_code = 1
  541.         Say 'SurfRexx Error('str') reading information from the server.'
  542.         Say RxIpErrorText(str)
  543.       end
  544.     end
  545.     else do
  546.       ret_code = 1
  547.       Say 'SurfRexx Error('rc') sending information to the server.'
  548.       Say RxIpErrorText(rc)
  549.     end
  550.   end
  551.   RETURN(ret_code)
  552.  
  553. /**************************************************************************/
  554. /* Read a set of defaults from a file                                     */
  555. /**************************************************************************/
  556. READ_DEFAULTS_FILE:Procedure Expose def.
  557.   Parse Arg file
  558.  
  559.   def. = 0
  560.  
  561.   if Lines(file) > 0 then do
  562.     do while Lines(file) > 0
  563.       line = strip(LineIn(file))
  564.       if line <> '' & substr(line,1,1) <> '*' then do
  565.         Parse Var line ':'term'.'value
  566.         term  = strip(translate(term))
  567.         value = strip(value)
  568.         if value <> '' then def.term = value
  569.       end
  570.     end /* do...while */
  571.     /*------------------------------*/
  572.     /* Make sure the file is closed */
  573.     /*------------------------------*/
  574.     Call LineOut file
  575.   end 
  576.   else do
  577.     Say '******************************'
  578.     Say 'Error reading defaults file...'file
  579.     Say '******************************'
  580.   end
  581.   RETURN
  582.