home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / rxpop123.zip / ChkPOP.cmd next >
OS/2 REXX Batch file  |  1997-12-13  |  7KB  |  252 lines

  1. /*------------------------------------------------------------------
  2.  * chkpop.cmd :
  3.  *------------------------------------------------------------------
  4.  * 08-09-92 rnr.cmd published originally by Patrick J. Mueller
  5.  * 24-01-95 adapted as chkpop.cmd by Christoph Lechleitner
  6.  * 15-12-96 fixed for more RFC-compatibility by C. Lechleitner
  7.  *------------------------------------------------------------------*/
  8.  
  9. trace off
  10.  
  11. parse arg server user password
  12.  
  13. if (server = "") | (user='') | (password='') then
  14.    do
  15.    say " Error: Expecting a pop server name, a user and a password as parameters."
  16.    exit 1
  17.    end
  18. say ' '
  19.  
  20. /*------------------------------------------------------------------
  21.  * initialize socket function package
  22.  *------------------------------------------------------------------*/
  23. parse source os .
  24.  
  25. if (os = "OS/2") then
  26.    do
  27.    if RxFuncQuery("SockLoadFuncs") then
  28.       do
  29.       rc = RxFuncAdd("SockLoadFuncs","RxSock","SockLoadFuncs")
  30.       rc = SockLoadFuncs()
  31.       end
  32.    end
  33.  
  34. if (os = "AIX/6000") then
  35.    do
  36.    rc = SysAddFuncPkg("rxsock.dll")
  37.    end
  38.  
  39. /*------------------------------------------------------------------
  40.  * get address of server
  41.  *------------------------------------------------------------------*/
  42. rc = SockGetHostByName(server,"host.!")
  43. if (rc = 0) then
  44.    do
  45.    say "Error: Unable to resolve server name" server
  46.    exit
  47.    end
  48.  
  49. server = host.!addr
  50.  
  51. /*------------------------------------------------------------------
  52.  * open socket
  53.  *------------------------------------------------------------------*/
  54. sock = SockSocket("AF_INET","SOCK_STREAM","IPPROTO_TCP")
  55. if (sock = -1) then
  56.    do
  57.    say "Error opening socket:" errno
  58.    exit
  59.    end
  60.  
  61. /*------------------------------------------------------------------
  62.  * connect socket
  63.  *------------------------------------------------------------------*/
  64. server.!family = "AF_INET"
  65. server.!port   = 110
  66. server.!addr   = server
  67.  
  68. rc = SockConnect(sock,"server.!")
  69. if (rc = -1) then
  70.    Error(sock,rc,"Error connecting to popserver :" errno)
  71.  
  72.    trc = GetResponse(sock)
  73.  
  74.    trc = SendMessage(sock,'USER 'user)
  75.    trc = GetResponse(sock)
  76.    parse var line.1 status rest
  77.    if status <> '+OK' then
  78.      do
  79.        say ' Error: User' user 'unknown on' server '.'
  80.        qrc = SendMessage(sock,'QUIT')
  81.        qrc = SockSoclose(sock)
  82.        exit
  83.      end
  84.  
  85.    trc = SendMessage(sock,'PASS 'password)
  86.    trc = GetResponse(sock)
  87.    parse var line.1 status rest
  88.    if status <> '+OK' then
  89.      do
  90.        say ' Error: Password wrong for' user ' on 'server'.'
  91.        qrc = SendMessage(sock,'QUIT')
  92.        qrc = SockSoclose(sock)
  93.        exit
  94.      end
  95.    else
  96.      do 
  97.        trc = SendMessage(sock,'LIST')
  98.        trc = GetResponse(sock)
  99.        messages = 0
  100.        parse var line.1 status rest
  101.        if status = '+OK' then
  102.          msginfo = GetResponseLine(sock)
  103.          do while msginfo <> '.'
  104.            messages = messages + 1
  105.            msginfo = GetResponseLine(sock)
  106.          end
  107.        if messages = 0 
  108.          then say ' There is no message waiting for you.'
  109.          else
  110.            do 
  111.              say ' There are' messages 'messages waiting for you.'
  112.              trc = SendMessage(sock,'LIST')
  113.              trc = GetResponse(sock)
  114.              msginfo = GetResponseLine(sock)
  115.              do while msginfo <> '.'
  116.                parse var msginfo number size
  117.                say ' Message' number 'has' size 'bytes.'
  118.                msginfo = GetResponseLine(sock)
  119.              end /* do while */
  120.            end /* do */
  121.      end /* do */
  122.  
  123.    trc = SendMessage(sock,'QUIT')
  124.    trc = GetResponse(sock)
  125.  
  126. /*------------------------------------------------------------------
  127.  * quittin' time!
  128.  *------------------------------------------------------------------*/
  129. /* rc = SendMessage(sock,"quit") */
  130. rc = SockSoclose(sock)
  131. exit
  132.  
  133.  
  134. /*------------------------------------------------------------------
  135.  * help
  136.  *------------------------------------------------------------------*/
  137. Help: procedure
  138.    say "commands:"
  139.    say
  140.    say "quit    - to quit"
  141.    say "group   - to change to a particular group"
  142.    say "article - to see an article"
  143.    say
  144.    return ""
  145.  
  146. /*------------------------------------------------------------------
  147.  * get a response from the server
  148.  *------------------------------------------------------------------*/
  149. GetResponse:     procedure expose !. line.
  150.    sock = arg(1)
  151.  
  152.    moreids = "100 215 220 221 222 223 230 231"
  153.  
  154.    line.0 = 1
  155.    line.1 = GetResponseLine(sock)
  156.  
  157.    parse var line.1 rid .
  158.  
  159.    if (wordpos(rid,moreids) = 0) then
  160.       return ""
  161.  
  162.    say ' getting further lines '
  163.  
  164.    do forever
  165.       o = line.0 + 1
  166.  
  167.       line.o = GetResponseLine(sock)
  168.  
  169.       if (line.o = ".") then
  170.          return ""
  171.  
  172.       line.0 = o
  173.    end
  174.  
  175.    return ""
  176.  
  177. /*------------------------------------------------------------------
  178.  * get a line from the server
  179.  *------------------------------------------------------------------*/
  180. GetResponseLine: procedure expose !.
  181.    sock = arg(1)
  182.  
  183.    crlf = d2c(13) || d2c(10)
  184.  
  185.    if (symbol('!.buff') = "LIT") then
  186.       !.buff = ""
  187.  
  188.    do while (pos(crlf,!.buff) = 0)
  189.       rc = SockRecv(sock,"data",8000)
  190.       !.buff = !.buff || data
  191.    end
  192.  
  193.    /* say ' got data "' data '"' */
  194.    /* say ' buff = "' !.buff '"' */
  195.  
  196.  
  197.    p = pos(crlf,!.buff)
  198.  
  199.    line = substr(!.buff,1,p-1)
  200.    !.buff = substr(!.buff,p+2)
  201.  
  202.    return line
  203.  
  204. /*------------------------------------------------------------------
  205.  * send a string to the server
  206.  *------------------------------------------------------------------*/
  207. SendMessage:     procedure expose !.
  208.    sock = arg(1)
  209.    data = arg(2) || d2c(13) || d2c(10)
  210.  
  211.    /* say 'Sending "'data'" to server.' */
  212.    len = length(data)
  213.    do while (len > 0)
  214.  
  215.       len = SockSend(sock,data);
  216.       /* say 'Returncode: ' len   */
  217.       /* say 'Errorcode:  ' errno */
  218.       /*
  219.       if (errno <> 0) then
  220.          Error(-1,rc,"Error sending data to server.")
  221.       */
  222.       if (len <= 0) then
  223.          Error(sock,100,"Server closed the connection.")
  224.       
  225.       data = substr(data,len+1)
  226.       len  = length(data)
  227.    end
  228.  
  229.    return i
  230.  
  231. /*------------------------------------------------------------------
  232.  * halting ...
  233.  *------------------------------------------------------------------*/
  234. Halting:
  235.    Error(sock,1,"error on line" sigl)
  236.  
  237. /*------------------------------------------------------------------
  238.  * exit with a message and return code
  239.  *------------------------------------------------------------------*/
  240. Error: procedure
  241.    sock = arg(1)
  242.    retc = arg(2)
  243.    msg  = arg(3)
  244.  
  245.    if (sock <> -1) then
  246.       rc = SockSoClose(sock)
  247.  
  248.    say msg
  249.  
  250.    exit retc
  251.  
  252.