home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 18 REXX / 18-REXX.zip / RXSOCK.ZIP / rnr.cmd < prev    next >
OS/2 REXX Batch file  |  1993-06-09  |  6KB  |  225 lines

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