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

  1. #!/usr/local/bin/kermit +
  2.  
  3. ; http://www.faqs.org/rfcs/rfc1939.html
  4.  
  5. ; p o p 3  --  POP3 mail retrieval
  6. ;
  7. ; This kermit script retrieves mail from a POP3 server and appends it
  8. ; to a local mailbox file.  The protocol is defined in rfc 1939.
  9.  
  10. ; It accepts one optional argument which is the word "delete".  If
  11. ; "delete" is specified, the server is asked to delete the messages
  12. ; as they are retrieved.  Otherwise, the server is not asked to delete
  13. ; them and they will be retained.
  14.  
  15. ; Any errors or unexpected responses from the server will cause the
  16. ; script to quit with an appropriate message.  If the script had
  17. ; succeeded in logging in to the server, it will request the server
  18. ; to abandon any changes before quitting.
  19.  
  20. ; If no errors occur, the script reports the number of messages
  21. ; retrieved and exits normally.
  22.  
  23. ; in this version, the identity of the POP3 server is built in,
  24. ; the local mailbox is taken from the environment, the userid is
  25. ; taken from the kermit variable and the password is requested
  26. ; interactively.  Of course, any of these things could be done
  27. ; differently depending on specific needs.
  28.  
  29. ; This script precedes each appended message with a "From " line
  30. ; containing the current date and time and a hyphen for the source.
  31. ; this seems compatible with most mail user programs.
  32.  
  33. ; Mark Sapiro, San Francisco Bay Area, California, September 2001
  34.  
  35. .pophost = mail.value.net
  36. .mailbox := \$(MAIL)
  37.  
  38. ; check kermit version
  39. if < \v(version) 700000 -
  40.   exit 1 \%0: C-Kermit 7.0 or later required.
  41.  
  42. if not writeable \m(mailbox) exit 1 \%0: \m(mailbox) not writeable.
  43.  
  44. ; macro to check the status response from the POP3 server
  45. ;  2 args, \%1 is a string saying what we're doing used in error message
  46. ;          \%2 is a label to go to if there is an error
  47. ;  The server's response is in \v(input)
  48. define ckstat {
  49.     .popstat := \fsubst(\v(input),1,3)
  50.  
  51.     switch \m(popstat) {
  52.         :+OK, break
  53.         :-ER, echo Error response to \%1: \v(input), goto \%2
  54.         :default, echo Unknown response to \%1: \v(input), goto \%2
  55.     }
  56. }
  57.  
  58. .delsw = 0    ; assume no delete, then check
  59. if defined \%2 exit 1 Usage: \%0 [delete]
  60. if defined \%1 {
  61.     if equal {\%1} {delete} .delsw = 1
  62.         else exit 1 Usage: \%0 [delete]
  63. }
  64.  
  65. fopen /append \%c \m(mailbox)  ; open mailbox first
  66. if fail exit 1 Can't open \m(mailbox) for appending
  67.  
  68. askq passwd {Password for \v(userid) on \m(pophost):}
  69.  
  70. ;  format a date-time for the From line, e.g. {Sat Sep 29 16:42:06 2001}
  71. if > \fword(\v(date),1) 9 {
  72.     .datime := {\v(day) \fword(\v(date),2) -  ; one space, 2 digits
  73. \fword(\v(date),1) \v(time) \fword(\v(date),3)}
  74. } else {
  75.     .datime := {\v(day) \fword(\v(date),2)  - ; two spaces, one digit
  76. \feval(\fword(\v(date),1)) \v(time) \fword(\v(date),3)}
  77. ; \feval above removes possible leading zero from one digit "day of month"
  78. }
  79.  
  80. set host \m(pophost) pop3 /raw
  81. if fail exit 1 Can't open \m(pophost)
  82.  
  83. set input echo off
  84.  
  85. clear input
  86. input -1 \13\10
  87. ckstat connect giveup
  88.  
  89. output USER \v(userid)\13\10
  90.  
  91. clear input
  92. input -1 \13\10
  93. ckstat USER giveup
  94.  
  95. output PASS \m(passwd)\13\10
  96.  
  97. clear input
  98. input -1 \13\10
  99. ckstat PASS giveup
  100.  
  101. ;  Now in Transaction state
  102.  
  103. output STAT\13\10    ; get message count
  104.  
  105. clear input
  106. input -1 \13\10
  107. ckstat STAT reset
  108.  
  109. .msgcnt := \fword(\v(input),2)
  110.  
  111. if = \m(msgcnt) 0 goto fini
  112.  
  113. for \%m 1 \m(msgcnt) 1 {
  114.     output RETR \%m\13\10
  115.  
  116.     clear input
  117.     input -1 \13\10
  118.     ckstat RETR reset
  119.  
  120.     fwrite /line \%c {From - \m(datime)}    ; write a From line
  121.     if fail {echo fwrite failed, goto reset}
  122.  
  123.     clear input
  124.     input -1 \13\10
  125.  
  126.     while not equal {\v(input)} {.\13\10} {
  127.         .line := \ftrim(\v(input),\13\10)
  128.         .\%l := \flength(\m(line))
  129.  
  130.         ;    strip leading "." from ".." lines
  131.  
  132.         if > \%l 1 {
  133.             if equal {.} {\fsubstring(\m(line),1,1)} {
  134.                 .line := \fsubstring(\m(line),2,\%l - 1)
  135.             }
  136.         }
  137.         fwrite /line \%c \m(line)
  138.         if fail {echo fwrite failed, goto reset}
  139.         clear input
  140.         input -1 \13\10
  141.     }
  142.     if = \m(delsw) 1 {
  143.         output DELE \%m\13\10
  144.  
  145.         clear input
  146.         input -1 \13\10
  147.         ckstat DELE reset
  148.     }
  149. }
  150.  
  151. :fini
  152. fclose \%c
  153. output QUIT\13\10
  154. input -1 \13\10
  155. close connection
  156. exit 0 Normal Termination: \m(msgcnt) messages
  157.  
  158. :reset
  159. output RSET\13\10
  160. input -1 \13\10
  161. goto giveup
  162.  
  163. :giveup
  164. output QUIT\13\10
  165. input -1 \13\10
  166. close connection
  167. exit 1 Quitting
  168.