home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ktfc / ktfc0d5.zip / SCAN0.5 < prev    next >
Text File  |  2000-05-24  |  8KB  |  247 lines

  1. ; File scan0.5, copyright (C) Peter Lyall Easthope, 2000.
  2. ; All rights reserved.
  3.  
  4. take prolog0.5            ; User configuration and procedures for login.
  5.  
  6. ; ============ Begin user configuration section ============
  7. ; Messages older than MsgLife will be deleted from the Mailbox.
  8. asg MsgLife 3 
  9.  
  10. ; "Other" defines a list of directory index vectors to be scanned.
  11. ; Each directory index vector is a sequence of indexes of directories 
  12. ; which enclose the target directory, the last in the sequence.  Each 
  13. ; index vector is the content of a \%d[i].
  14. def Other {
  15.   def dLen 0                      ; Number of directories.
  16.   declare \&d[\m(dLen)]       ; Declare the directory list array
  17. ;  def \&d[1] {3 6}                ; FirstClass Support
  18. ;  def \&d[2] {3 7}                ; FirstClass News
  19.   ; HndlMsg is used in ScanDirs.  
  20.   def HndlMsg {
  21.     if equal {\fsubstr(\m(Title),1,1)} {*} RecvMsg
  22.   }
  23. }
  24. ; ============ End user configuration section ============
  25.  
  26. ; MB defines a directory list containing only the Mailbox.
  27. def MB {
  28.   local string
  29.   asg string {This message has been on the server for at least}
  30.   def dLen 1                      ; Number of directories.
  31.   declare \&d[\m(dLen)]     ; Declare the directory list array
  32.   def \&d[1] {1}                  ; Path from Home to Mailbox.
  33.   def HndlMsg {
  34.     xif equal {\fsubstr(\m(Title),1,1)} {*} { RecvMsg } else {
  35.       GetAge
  36. ;      echo {age of this message is \v(return) days.}
  37.       xif > \v(return) \m(MsgLife) { 
  38.         DelMsg {\m(string) \m(MsgLife) days.}
  39.       }
  40.     }
  41.   }
  42. }
  43.  
  44. ; Beginning of procedures used to receive messages or to tidy the mailbox.
  45.  
  46. def minputJ {
  47.   clear input
  48.   minput \m(WaitTime) {J} {Item number:}
  49.     switch \v(minput) {
  50.       :0, errext {Response after GetDir sends dir. number not recognizable.}
  51.       :1, break
  52.       :2, errext {faulty directory number; check path in User Configuration.}
  53.       :default, errext {Undefined switch value in GetDir}
  54.     }
  55. }
  56.  
  57. ; This procedure takes two arguments.  The second is a string which is the 
  58. ; index of a directory in the current directory.  If the first argument
  59. ; is 1 a listing of the contents of the directory is put in a file named DIR.
  60. ; If the first argument is 0 the directory listing is discarded.
  61. def GetDir {
  62.   local str
  63.   xif equal {\%1}  {1} {
  64.     open write DIR
  65.     if failure errext {Can't open directory file DIR for writing.}
  66.     echo {File DIR opened to write the directory at {\&d[\%f]}.}
  67.   }
  68.   output {\%2\13}
  69.   minputJ
  70.   IInputLine
  71.   SaveLine
  72.   if equal {\%1} {1}  writeln file {\m(InLine)}
  73.   asg str \fsubstr(\m(InLine),\feval(\flength(\m(InLine))-8),9)
  74.   xif equal {\m(str)} {No items.} {
  75. ;    echo {Directory is empty.}
  76.     IInputLine
  77.     IInputLine
  78.     GetPrompt 
  79.   } else {
  80. ;    echo {Directory is not empty.}
  81.     local ExitMB
  82.     def ExitMB 0
  83.     while equal \m(ExitMB) 0 { 
  84.       clear input
  85.       minput \m(WaitTime) {\10} {[More]}
  86.       switch \v(minput) {
  87.         :0, errext {Unrecognized characters when receiving directory}
  88.         :1, SaveLine, InputMB1 {\%1}, break
  89.         :2, InputMB2, break
  90.         :default, errext {Undefined switch in directory reading loop}
  91.       }
  92.     }
  93.   }
  94.   xif equal {\%1} {1}  { close write-file }
  95. }
  96.  
  97. def FindDir {
  98.    ; Initialize index for locating the start of the terminal directory number.
  99.     asg beginD \flength(\&d[\%f])
  100.     ; Step backwards through the path from the end.
  101.     while equal {\fsubstr(\&d[\%f ],\m(beginD),1)}  { }  {
  102.       if = \m(beginD) 1 errext {No directory number found}
  103.       asg beginD \feval(\m(beginD)-1)
  104.     }
  105.     while = 1 1 {
  106. ;      echo {Searching for beginning of directory string}
  107.       if = \m(beginD) 1 break
  108.       asg tmp \feval(\m(beginD)-1)
  109.       if equal {\fsubstr(\&d[\%f],\m(tmp),1)} { } break
  110.       asg beginD \m(tmp)
  111.     }
  112.     ; beginD now contains the index in \&d[\%f] of the start of the 
  113.     ; target directory.
  114. }
  115.  
  116. def TracePath {
  117.     ; Step through the path from the beginning.
  118.     asg begini 1
  119.     while = 1 1 {
  120.       if = \m(begini) \m(beginD) break
  121.       while = 1 1 {
  122.         if not equal {\fsubstr(\&d[\%f],\m(begini),1)} { } break
  123.         asg begini \feval(\m(begini)+1)
  124.       }
  125. ;      echo {begini = \m(begini)}
  126.       if = \m(begini) \m(beginD) break 
  127.       asg endi \feval(\m(begini)+1)
  128.       while = 1 1 {
  129.         if < \flength(\&d[\%f]) \m(endi) break
  130.         if equal {\fsubstr(\&d[\%f],\m(endi),1)} { } break
  131.         asg endi \feval(\m(endi)+1)
  132.       }
  133. ;      echo {endi = \m(endi)}
  134.       asg tmp {\fsubstr(\&d[\%f],\m(begini),\feval(\m(endi)-\m(begini)))}
  135. ;      echo {GetDir is called with argument \m(tmp)}
  136.       GetDir 0 {\m(tmp)}
  137.       asg begini \m(endi)
  138.     }
  139. }
  140.  
  141. ; Days takes the month number and date as arguments and returns 
  142. ; the inclusive number of days from January 1.  Eg. Days {03} {04} 
  143. ; returns 63.  February is assumed to have 28 days.
  144. def Days {
  145.   local tmp
  146.   switch \%1 {
  147.     :01, asg tmp   0, break
  148.     :02, asg tmp  31, break
  149.     :03, asg tmp  59, break
  150.     :04, asg tmp  90, break
  151.     :05, asg tmp 120, break
  152.     :06, asg tmp 151, break
  153.     :07, asg tmp 181, break
  154.     :08, asg tmp 212, break
  155.     :09, asg tmp 243, break
  156.     :10, asg tmp 273, break
  157.     :11, asg tmp 304, break
  158.     :12, asg tmp 334, break
  159.     :default, errext {Undefined switch in Days}
  160.   }
  161.   return \feval(\m(tmp)+\%2)
  162. }
  163.  
  164. def GetAge {
  165. ; Sindx represents the offset of the first / in the date, in a 
  166. ; line from the Mailbox.
  167.   local age year month day Sindx
  168.   asg Sindx 32
  169.   while = 0 0 {
  170.     if equal {\fsubstr(\m(Title),\m(Sindx),1)} {/} break
  171.     asg Sindx \feval(\m(Sindx)+1)
  172.   }
  173.   asg year {\fsubstr(\m(Title),\feval(\m(Sindx)+4),4)}
  174. ;  echo {year = \m(year)}
  175.   if not numeric {\m(year)} return 0
  176. ; Current year - (year of message creation) expressed in days:
  177.   asg age \feval((\m(year)-\fsubstr(\v(ndate),1,4))*365 )
  178. ; Add days from Jan 1 to present.
  179.   asg month {\fsubstr(\v(ndate),5,2)}
  180.   asg day {\fsubstr(\v(ndate),7,2)}
  181. ;  echo {Current month and day = \m(month)  \m(day)}
  182.   Days {\m(month)} {\m(day)}
  183.   asg age \feval(\m(age)+\v(return))
  184. ;  echo {Diff. in years + days from jan 1 to present = \m(age)}
  185. ; Subtract days from Jan1 to message date.
  186.   asg month {\fsubstr(\m(Title),\feval(\m(Sindx)-2),2)}
  187.   if not numeric {\m(month)} return 0
  188.   asg day {\fsubstr(\m(Title),\feval(\m(Sindx)+1),2)}
  189.   if not numeric {\m(day)} return 0
  190. ;  echo {month and day of message = \m(month) \m(day)}
  191.   Days {\m(month)} {\m(day)}
  192.   asg age \feval(\m(age)-\v(return))
  193.   return \m(age)
  194. }
  195.  
  196. ; ScanDirs reads unread messages in a list of directories.  The first 
  197. ; argument receives the number of directories to be scanned.
  198. def ScanDirs {
  199.   local beginD begini endi tmp
  200.   \%1      ; Define the directory list and HndlMsg.
  201.   ; Loop over the directories in the list.
  202.   for \%f  1 \m(dLen) 1 {
  203.     ; Go to the Desktop or Home directory on the server.
  204.     output {home\13}
  205.     GetPrompt
  206.     FindDir
  207.     TracePath
  208.     ; Get the directory into the file DIR.
  209.     GetDir 1 {\fsubstr(\&d[\%f],\m(beginD))}
  210.     echo {DIR written and closed.  Checking each entry.  Patience. ...}
  211.     open read DIR
  212.     if failure errext {can't open DIR for reading}
  213.     read Title
  214.     if failure errext {Nothing in DIR file}
  215.     while = 1 1 { 
  216.       read Title
  217.       if failure break
  218. ;      echo {Checking a line in the directory file ...}
  219.       if < \flength(\m(Title)) 49 continue
  220.       ; This is a message; otherwise it is a directory.
  221.       GetMsgNum
  222.       if = \m(MsgNum) 0 continue
  223.       HndlMsg
  224.     }
  225.     close read-file
  226.     echo {Finished with this directory.  DIR closed.}
  227.     echo {}
  228.   }
  229. }
  230.  
  231. ; End of procedures used to receive messages or to tidy the mailbox.
  232.  
  233. def Communicate {
  234.   Login
  235.   ScanDirs MB           ; Scan the Mailbox.
  236.   ScanDirs Other        ; Scan directories other than the Mailbox.
  237. ;  echo {}
  238.   output {logout y\13}
  239.   hangup
  240.   close session                   ; Close session log
  241.   quit
  242. }
  243.  
  244. ; Finished macro definitions.  
  245.  
  246. Communicate                       ; Make it happen.
  247.