home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / malphapage < prev    next >
Text File  |  2020-01-01  |  11KB  |  361 lines

  1. #!/usr/local/bin/kermit8
  2. ; --------------------------------------------------------------------
  3. ;
  4. ; cook2.ksc
  5. ;
  6. ;
  7. ; Author
  8. ; ------
  9. ; R. M. Almeria, Anteon Corp., 2003
  10. ;
  11. ;
  12. ; Purpose
  13. ; -------
  14. ; This kermit script is designed to take 1 to N times two arguments 
  15. ; (a pager-id and a message) and to connect to Arch's paging service 
  16. ; via a modem using their special service number.
  17. ;
  18. ; So you can basically connect to the service and issue several pages 
  19. ; or just one.  You could send a message to someone's pager like so:
  20. ;
  21. ;    cook2.ksc 4441111 "hello world"
  22. ;
  23. ; or you could send multiple messages to multiple pagers like so:
  24. ;
  25. ;    cook2.ksc 4441111 "test1" 4442222 "test2" 4443333 "test3"
  26. ;
  27. ;
  28. ; Special Remarks
  29. ; ---------------
  30. ; This program implements the TAP (Telocator Alphanumeric Protocol)
  31. ; for pagers.
  32. ;
  33. ;
  34. ; Language
  35. ; --------
  36. ; This program is written in Kermit script.  Please read the book
  37. ; "Using C-KERMIT" by Frank da Cruz et al., for more details.
  38. ;
  39. ; Revision History
  40. ; ----------------
  41. ; v1.1 -- February 5, 2003 @ 3:48 PM 
  42. ;    Added copious debug statements that are toggled (0/1) via
  43. ;    the DEBUG constant (it is defined in the initialization section).
  44. ;
  45. ; v1.0 -- January 28, 2003 @ 5:55 PM 
  46. ;    Initial revision.
  47. ;
  48. ; --------------------------------------------------------------------
  49.  
  50. ; --------------------------------------------------------------------
  51. ; Check the number of command line arguments and exit if insufficient. 
  52. ; --------------------------------------------------------------------
  53.   xif < \v(args) 4 {
  54.     echo \10This kermit8 paging script accepts unlimited alphanumeric-
  55.  pager-id/message pairs.\10
  56.     echo {SYNTAX:\10\9cook2.ksc pagerid1 message1 pagerid2 message2-
  57.  pagerid3 message3 . . . \10}
  58.     echo {    EX:\10\9./cook2.ksc 9992322 "Hello World!"\10}
  59.     exit
  60.   }
  61.  
  62. ; --------------------------------------------------------------------
  63. ; Define the SendBlock function which sends a message (block) to the
  64. ; paging service.  This function is also called initially to send the
  65. ; first message.
  66. ; --------------------------------------------------------------------
  67. def SENDBLOCK {
  68.   local \%b \%c \%s blk
  69.  
  70.   ; Create a string composed of the pager-id and message with
  71.   ; appropriate control chars.
  72.   assign \%b \2\&p[\%m]\13\&m[\%m]\13\3
  73.  
  74.   ; Calculate the checksum required for TAP and add to the final
  75.   ; message string that will be sent to the paging service.
  76.   assign \%c \fchecksum(\%b)
  77.   assign blk \%b\fchar(\fmod(\%c/256,16)+48)\fchar(\fmod(\%c/16,16)+48)\fchar(\fmod(\%c,16)+48)\13
  78.  
  79.   assign \%s \flength(blk)
  80.  
  81.   ; Make sure that the size of the block is less than MaxBlockSize.
  82.   xif < \%s \%y {
  83.     if \m(DEBUG) echo {DEBUG: {SENDBLOCK}  Sending '\&m[\%m]' to '\&p[\%m]'}
  84.     output \m(blk)
  85.     return 1 
  86.   } else {
  87.     return 0
  88.   }
  89.  
  90. }
  91.  
  92. ; --------------------------------------------------------------------
  93. ; Define the SendNextBlock function which sends the second thru last 
  94. ; messages (blocks) to the paging service (one block sent per 
  95. ; function call).  This function calls SendBlock to send its messages.
  96. ; --------------------------------------------------------------------
  97. def SENDNEXTBLOCK {
  98.  
  99.   xif < \%m+1 \%n {
  100.  
  101.     ; There are still messages to send so send them.
  102.  
  103.     assign \%q 0    ; Reset the retries counter.
  104.     assign \%m \feval(\%m + 1)
  105.    
  106.     sendblock
  107.     return \v(return)
  108.  
  109.   } else {
  110.  
  111.     ; We are done with the paging service so disconnect.
  112.  
  113.     output \4\13    ; Send <EOT><CR>
  114.     if \m(DEBUG) echo {DEBUG: {SENDNEXTBLOCK} Sending <EOT><CR>.}
  115.     input 3 {\27\4\13}    ; Look for <ESC><EOT><CR>
  116.     assign \%f 1    ; Set Finished to true.
  117.     return 0
  118.   }
  119.  
  120. }
  121.  
  122. ; --------------------------------------------------------------------
  123. ; Define the TAP (Telocator Alphanumeric Protocol) function.
  124. ; --------------------------------------------------------------------
  125. def SENDTAPMSG {
  126.  
  127.   assign \%o \6\13    ; OK         = <ACK><CR>
  128.   assign \%t \21\13    ; TRY AGAIN    = <NAK><CR>
  129.   assign \%a \30\13    ; ABANDON    = <RS><CR>
  130.   assign \%d \27\4\13    ; DISCONNECT    = <ESC><EOT><CR>
  131.   
  132.   assign \%x 0        ; Initialize the loop variable.
  133.  
  134.   assign \%f 0        ; Initialize Finished flag to false.
  135.   assign \%w 5        ; Initialize MaxIDAttempts.
  136.  
  137. :dowhile
  138.  
  139.   ; Look for 'ID=' string for at least MaxIDAttempts before exiting.
  140.   xif = \%f 0 {
  141.     xif < \%x \%w {
  142.       output \13
  143.       input 3 {ID=}
  144.       xif success {
  145.         assign \%f 1
  146.       } else {
  147.         assign \%x \feval(\%x + 1)
  148.       }
  149.       goto dowhile
  150.     }
  151.   } 
  152.  
  153.   ; Exit if we didn't get the 'ID=' string.
  154.   xif = \%f 0 {
  155.     assign \%e 0  
  156.     end
  157.   }
  158.  
  159.   assign \%q 0        ; Initialize protocol Retries and NOT dialing retries.
  160.   assign \%u 1        ; Initialize First to true.
  161.   assign \%f 0        ; Reset Finished to false.
  162.   assign \%v 0        ; Initialize SentBlock to false.
  163.   assign \%y 233    ; Define MaxBlockSize.  Typically 256 according to the
  164.             ; protocol but Cook limits this to 233.
  165.   assign \%z 3        ; Define MaxRetries.
  166.  
  167.   while = \%f 0 {
  168.     xif = \%u 1 {
  169.       xif < \%q \%z {
  170.         if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Sending <ESC>PG1<CR>.}
  171.  
  172.     ; Tell the paging service that we want to send an
  173.         ; alphanumeric page.
  174.         output {\27PG1\13}    ; Sending <ESC>PG1<CR>    
  175.  
  176.         ; Wait for a response from the paging service...
  177.         ; either OK, TRY AGAIN, DISCONNECT, 'ID=' or
  178.         ; TIMEOUT.
  179.         minput 3 {\%o} {\%t} {\%d} {ID=}
  180.  
  181.         switch \v(minput) {
  182.           :0, break        ; TIMEOUT 
  183.           :1,                   ; OK         = <ACK><CR> 
  184.               if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Received OK.}
  185.               if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Waiting for <ESC>[p<CR>.}
  186.  
  187.               ; Wait for the go-ahead from the paging service.
  188.               input 3 {\27[p\13}
  189.               xif success {
  190.                 msleep 500
  191.                 xif < \%m \%n {
  192.                   if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Calling SENDBLOCK.}
  193.                   sendblock    ; Send first block.
  194.                   assign \%v \v(return)
  195.                   assign \%u 0
  196.                 } else {
  197.                   assign \%f 1    ; Set Finished to true.
  198.                 }
  199.               } else {
  200.                 assign \%f 1    ; Set Finished to true.
  201.               }
  202.               break        
  203.           :2,                   ; TRY AGAIN  = <NAK><CR> 
  204.               if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Received TRY AGAIN.}
  205.               break
  206.           :3,                   ; DISCONNECT = <ESC><EOT><CR> 
  207.               if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} Received DISCONNECT.}
  208.               assign \%f 1    ; Set Finished to true.
  209.               break        
  210.           :4, break        ; Received 'ID=' send <ESC>PG1<CR> again
  211.         }
  212.  
  213.         ; Increment the Retries counter.
  214.         assign \%q \feval(\%q + 1)
  215.  
  216.       } else {
  217.         assign \%f 1        ; Set Finished to true.
  218.       }
  219.     } else {
  220.  
  221.       ; Look for the response to the first and succeeding blocks that
  222.       ; were sent to the paging service.  We are waiting for an OK,
  223.       ; ABANDON, TRY AGAIN, DISCONNECT or TIMEOUT response.
  224.       minput 3 {\%o} {\%a} {\%t} {\%d}
  225.       switch \v(minput) {
  226.         :0, break        ; TIMEOUT 
  227.         :1,                     ; OK         = <ACK><CR> 
  228.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Received OK.}
  229.             msleep 500   
  230.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Calling SENDNEXTBLOCK.}
  231.             sendnextblock
  232.             assign \%v \v(return)
  233.             break
  234.         :2,                     ; ABANDON    = <RS><CR> 
  235.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Received ABANDON.}
  236.             msleep 500   
  237.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Calling SENDNEXTBLOCK.}
  238.             sendnextblock
  239.             assign \%v \v(return)
  240.             break
  241.         :3,                     ; TRY AGAIN  = <NAK><CR>
  242.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Received TRY AGAIN.}
  243.             msleep 500
  244.             xif = \%v 1 {
  245.               xif < \%q \%z {
  246.                 sendblock
  247.                 assign \%v \v(return)
  248.  
  249.         ; Increment the retries counter.
  250.                 assign \%q \feval(\%q + 1) 
  251.               } else {
  252.                 sendnextblock
  253.                 assign \%v \v(return)
  254.               }
  255.             } else {
  256.               sendnextblock
  257.               assign \%v \v(return)
  258.             }
  259.             break
  260.         :4,                     ; DISCONNECT = <ESC><EOT><CR>
  261.             if \m(DEBUG) echo {DEBUG: {SENDTAPMSG} - Received DISCONNECT.}
  262.             assign \%f 1    ; Set Finished to true.
  263.             break
  264.       }
  265.     }
  266.   }
  267.   
  268.   xif < \%m+1 \%n {
  269.     assign \%e 0        ; Return false (Didn't send all messages due to errors).
  270.   } else {
  271.     assign \%e 1        ; Return true (We sent all of the messages).
  272.   }
  273. }
  274.  
  275.  
  276. ; --------------------------------------------------------------------
  277. ; Initialization.
  278. ; --------------------------------------------------------------------
  279.  
  280.   assign DEBUG 1
  281.  
  282.   ; Calculate the number of messages to send.
  283.   assign \%n \feval( (\v(args) - 2)/2)    
  284.  
  285.   declare \&p[\%n]        ; Declare the pager-id array.
  286.   declare \&m[\%n]        ; Declare the message array.
  287.  
  288.   ; Populate the pager-id and message arrays with the args.
  289.   for \%x 0 \%n-1 1 {
  290.     assign \%i \feval(2 + (\%x * 2))    ; Compute the pager-id index.
  291.     assign \%j \feval(3 + (\%x * 2))    ; Compute the message index.
  292.  
  293.     assign \&p[\%x] \&@[\%i]        ; Add pager-id to pager-id array.
  294.     assign \&m[\%x] \&@[\%j]        ; Add message to message array.
  295.   }
  296.  
  297.   assign \%m 0            ; Initialize message index (MIndex).
  298.  
  299.   assign \%r 0            ; Initialize the Reconnects counter.
  300.   assign \%s 3            ; Define MaxReconnects.
  301.  
  302.   assign COOKPDX 9,5031234567   ; Set to local paging service phone number.
  303.  
  304.   assign \%e 0            ; Initialize TAP Error Flag to false.
  305.  
  306. ; --------------------------------------------------------------------
  307. ; Try to connect to the paging service.
  308. ; --------------------------------------------------------------------
  309. :restart
  310.  
  311.   if \m(DEBUG) echo {\10DEBUG: RESTART NO. \%r}
  312.   set modem hangup-method modem-command 
  313.   if \m(DEBUG) echo {DEBUG: Issuing initial HANGUP.}
  314.   hangup
  315.  
  316.   if \m(DEBUG) echo {DEBUG: Setting modem type}
  317.   set modem type att-dataport
  318.   if \m(DEBUG) echo {DEBUG: Setting the port to '/dev/cua/4'}
  319.   set port /dev/cua/4
  320.   xif failure {
  321.     echo {ERROR: I could not connect to /dev/cua/4}
  322.     goto terminate 
  323.   } 
  324.  
  325.   if \m(DEBUG) echo {DEBUG: Setting up the terminal.}
  326.   set speed 2400
  327.   set parity even
  328.   set stop-bits 1
  329.   set flow-control xon/xoff
  330.   set modem flow none
  331.   set modem error-correction off
  332.   set modem data-compression off
  333.   set modem speed-matching off
  334.   ;set carrier-watch off
  335.   set output pacing 200
  336.   set dial retries 5
  337.   set dial interval 1
  338.  
  339.   if \m(DEBUG) echo {DEBUG: DIALING....}
  340.   dial \m(COOKPDX)
  341.   xif success {
  342.     if \m(DEBUG) echo {DEBUG: Calling SENDTAPMSG.}
  343.     sendtapmsg
  344.     xif = \%e 0 {
  345.       xif < \%r \%s {
  346.         xif < \%m \%n {
  347.           assign \%r \feval(\%r + 1)
  348.           goto restart
  349.         }
  350.       }
  351.     } 
  352.   }
  353.  
  354. :terminate
  355.  
  356.   if \m(DEBUG) echo {DEBUG: Hanging up.}
  357.   hangup
  358.   if \m(DEBUG) echo {DEBUG: Exiting.}
  359.   exit
  360.