home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / pmmidx02.zip / pmmidx.cmd next >
OS/2 REXX Batch file  |  1998-02-24  |  5KB  |  202 lines

  1. /* Re-index a PMMail 1.92 folder v0.2 */
  2. /* Author: Kovacs Istvan <kofa@math.bme.hu> */
  3.  
  4. /*trace intermediates*/
  5.  
  6. parse arg addresses
  7. if addresses='' then
  8. do
  9.   say 'Usage: pmmidx your_address(es)'
  10.   exit
  11. end
  12.  
  13. /* Load REXX utility functions */
  14. call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
  15. call SysLoadFuncs
  16.  
  17. /* Get data of message files */
  18. rc=SysFileTree('*.msg',messages,'FT')
  19.  
  20. /* status values */
  21. read=1
  22. sent=3
  23.  
  24. /* Month names */
  25. months='JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC'
  26.  
  27. to_txt='To: '
  28. from_txt='From: '
  29. date_txt='Date: '
  30. subject_txt='Subject: '
  31. content_type_txt='Content-Type: '
  32.  
  33. /* Backup old folder.bag */
  34. '@del folder.bak 2>nul'
  35. '@ren folder.bag folder.bak'
  36.  
  37. signal on error name error
  38.  
  39. /* Loop through message files */
  40. do i=1 to messages.0
  41.   /* Set default values - mainly for debugging */
  42.   to_email='(null)'
  43.   to_fullname='(null)'
  44.   from_email='(null)'
  45.   from_fullname='(null)'
  46.   subject='(null)'
  47.   parse value word(messages.i,1) with yy '/' m_m '/' dd '/' hh '/' mm
  48.   msgdate=yy'-'m_m'-'dd
  49.   msgtime=hh':'mm':00'
  50.   attachments=0
  51.   msgstatus=read
  52.   /* Calculate size in kB */
  53.   msglen=trunc(word(messages.i,2)/1024+0.5)
  54.   filename=filespec('n',word(messages.i,4))
  55.  
  56.   /* Read and process the header */
  57.   do until l=''
  58.     l=linein(filename)
  59.     if pos(to_txt,l)=1 then
  60.     do
  61.       call process_to
  62.       iterate
  63.     end
  64.     if pos(from_txt,l)=1 then
  65.     do
  66.       call process_from
  67.       iterate
  68.     end
  69.     if pos(date_txt,l)=1 then
  70.     do
  71.       call process_date
  72.       iterate
  73.     end
  74.     if pos(subject_txt,l)=1 then
  75.     do
  76.       call process_subject
  77.       iterate
  78.     end
  79.     
  80.     if pos(content_type_txt,l)=1 then
  81.       call process_attachments
  82.   end
  83.  
  84.   /* Close file */
  85.   rc=lineout(filename)
  86.  
  87. /* Assemble a line of FOLDER.BAG Structure:
  88. STAUS▐ATTACHMENT▐YY-MM-DD▐HH:MM:SS▐SUBJECT▐TO_EMIAL▐TO_FULLNAME▐FROM_EMAIL▐FROM_FULLNAME▐FILENAME.MSG▐
  89. */
  90.   rc=lineout('folder.bag',msgstatus'▐'attachments'▐'msgdate'▐'msgtime'▐'subject'▐'to_email'▐'to_fullname'▐'from_email'▐'from_fullname'▐'msglen'K▐'filename'▐')
  91. end
  92. rc=lineout('folder.bag')
  93. exit
  94.  
  95. process_to:
  96. /* Try to figure out to_email and to_fullname */
  97.  
  98.   /* Extract addressee data */
  99.   parse var l to_txt addr
  100.  
  101.   /* Look for email address */
  102.   /* If it's the first word, the rest is the full name, and vice versa */
  103.   if pos('@',word(addr,1))\=0 then
  104.   do
  105.     to_email=word(addr,1)
  106.     /* If full name is present, use it, otherwise use email address as full name */
  107.     if words(addr)>1 then        
  108.       to_fullname=substr(addr,wordindex(addr,2))
  109.     else
  110.       to_fullname=to_email
  111.   end
  112.   else
  113.   do
  114.     to_email=word(addr,words(addr))
  115.     to_fullname=left(addr,max(length(addr)-length(to_email)-1,1))
  116.   end
  117.   to_email=stripstring(to_email)
  118.   to_fullname=stripstring(to_fullname)
  119. return
  120.  
  121. process_from:
  122. /* Try to figure out from_email and from_fullname
  123.    No comments, same method as in process_to */
  124.  
  125.   parse var l from_txt addr
  126.   if pos('@',word(addr,1))\=0 then
  127.   do
  128.     from_email=word(addr,1)
  129.     if words(addr)>1 then 
  130.       from_fullname=substr(addr,wordindex(addr,2))
  131.     else
  132.       from_fullname=from_email
  133.   end
  134.   else
  135.   do
  136.     from_email=word(addr,words(addr))
  137.     from_fullname=left(addr,max(length(addr)-length(from_email)-1,1))
  138.   end
  139.   from_email=stripstring(from_email)
  140.   from_fullname=stripstring(from_fullname)
  141.  
  142.   /* If sender address is one of the addresses supplied, mark message as sent */
  143.   do j=1 to words(addresses)
  144.     if word(addresses,j)=from_email then
  145.     do
  146.       msgstatus=sent
  147.       return
  148.     end
  149.   end
  150. return
  151.  
  152. process_subject:
  153. /* Extract subject info */
  154.  
  155.   if words(l)>1 then
  156.     parse value space(l) with subject_txt subject
  157.   else
  158.     subject='(null)'
  159. return
  160.  
  161. process_date:
  162. /* Get the date in YY-MM-DD and time in HH:MM:SS format */
  163.  
  164.   /* If name of day is missing, don't try to parse it */
  165.   if datatype(word(l,2),'n') then
  166.     parse var l 'Date:' dd monthname yyyy hh ':' mm ':' ss rest
  167.   else
  168.     /* We've got the name of the day */
  169.     parse var l date_txt dayname ',' dd monthname yyyy hh ':' mm ':' ss rest
  170.  
  171.   /* Format things, add leading zero, if needed */
  172.   yy=right(yyyy//100,2,'0')
  173.   m_m=right(wordpos(translate(monthname),months),2,'0')
  174.   dd=right(dd,2,'0')
  175.   hh=right(hh,2,'0')
  176.   mm=right(mm,2,'0')
  177.   ss=right(ss,2,'0')
  178.   msgdate=yy'-'m_m'-'dd
  179.   msgtime=hh':'mm':'ss
  180. return
  181.  
  182. process_attachments:
  183. /* If content type is multipart, mark message as one having attachments */
  184.   if pos('multipart',l)\=0 then
  185.     attachments=1
  186. return
  187.  
  188. stripstring: procedure
  189. /* Strip different kinds of begin-end markers */
  190.   parse arg s
  191.   markers='"<>()'
  192.   do i=1 to length(markers)
  193.     if left(s,1)=substr(markers,i,1) then
  194.       s=substr(s,2)
  195.     else if right(s,1)=substr(markers,i,1) then
  196.       s=left(s,length(s)-1)
  197.   end
  198. return s
  199.  
  200. error:
  201.   say 'Error while processing' filename
  202.