home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / comm / mail / YAMscripts.lha / WholeStory.rexx < prev    next >
OS/2 REXX Batch file  |  1997-02-18  |  5KB  |  186 lines

  1. /*
  2. ** WholeStory.rexx 1.3 - 18-Feb-96 by Kai.Nikulainen@utu.fi
  3. **
  4. ** I belong to a X-Files fanfic mailing list and long stories are usually posted
  5. ** in parts.  This script will find and join all parts.
  6. **
  7. ** New in 1.3:
  8. **  -now hadles both (n/m) and n/m numbering correctly
  9. ** New in 1.2:
  10. **  -script now displays a requester with message subjects 
  11. **  -messages are marked deleted AFTER they have been copied to the file
  12. **  -an end message tells you when everything is done
  13. ** Send all comments, bug reports, suggestions and X-mas cards to knikulai@utu.fi
  14. */
  15.  
  16. options results
  17. call addlib('rexxsupport.library',0,-30)
  18. call addlib('rexxreqtools.library',0,-30)
  19.  
  20.  defdir='work:home'
  21.    body='I found these parts in this order:'
  22. buttons='_Save them|_Quit'
  23.   title='WholeStory 1.2 © knikulai@utu.fi'
  24. mc=0 
  25. used=0
  26. NL='0a'x
  27.  
  28. address 'YAM'
  29. 'GetMailInfo From'
  30. server=result
  31. 'GetMailInfo Subject'
  32. subj=result
  33. 'GetMailInfo Active'
  34. active=result
  35. 'GetFolderInfo Max'
  36. n=result
  37.  
  38. /* 
  39. ** First scan all messages in the folder.
  40. ** Search for messages with same sender and at most 4 different chars in subject 
  41. */
  42.  
  43. do m=0 to n-1
  44.     'SetMail' m
  45.     'GetMailInfo From'
  46.     if result=server then do
  47.        'GetMailInfo Subject'
  48.        if SubDiff(upper(result),upper(subj))<2 then do
  49.                call AddMsg(result)
  50.         part.mc=m
  51.         end
  52.     end /* if result=server then do */
  53. end /* do m=1 to n */
  54.  
  55. /*
  56. ** Then sort the messages.  Hopefully part 10 comes after 9 and not after 1....
  57. */
  58.  
  59. call SortMessages
  60.  
  61. /*
  62. ** Do you really want to save them?
  63. */
  64. do i=1 to mc
  65.     body=body NL subject.i
  66.     end
  67.  
  68. sel=rtezrequest(body,buttons,title,'rtez_defaultresponse=1')
  69. if sel=0 then exit
  70.  
  71. /*
  72. ** Lets write the messages into one file
  73. */
  74. storyname=left(compress(subject.1,' /:()'),25,'')
  75. if upper(left(storyname,3))='NEW' then storyname=substr(storyname,4)
  76. if upper(left(storyname,6))='REPOST' then storyname=substr(storyname,7)
  77. outfile=rtfilerequest(defdir,storyname,'Select story name')
  78. if outfile='' then exit
  79.  
  80. call open(out,outfile,'w')
  81. call writeln(out,'This file contains the following messages:')
  82. do i=1 to mc
  83.         call writeln(out,subject.i)
  84.         end /* do i=1 to mc */
  85. call writeln(out,'')
  86. address 'YAM' 
  87. do i=1 to mc
  88.         if ~open(inp,filename.i,'r') then do
  89.         'Request "Can not read part*n'subject.i'" "Ok"' 
  90.         exit
  91.         end
  92.     do until eof(inp) | r='' /* Skip headers */
  93.         r=readln(inp)
  94.         end
  95.     do until eof(inp)
  96.         r=readln(inp)
  97.         call writeln(out,r)
  98.         end
  99.     call close(inp)
  100.     'SetMail' part.i
  101.     'MailDelete'
  102.         end /* do i=1 to mc */
  103. call close(out)
  104. 'SetMail' active  /* Go back to the originally selected message */
  105. 'Request "All parts are saved and marked as deleted!" "_Ok"'
  106.  
  107. exit    /* It's the end of the script as we know it... */
  108.  
  109. FindStart:
  110.   found_it=0
  111.   if used=0 then do
  112.   /* 
  113.   ** When called the first time, this branch checks which delimiters are used 
  114.   ** Following calls use else branch
  115.   */
  116.      do until eof(1) | found_it
  117.          rivi=readln(1)
  118.          do d=1 to delimiters
  119.              if pos(startline.d,rivi)=1 then do
  120.               found_it=1
  121.               used=d
  122.           end /* if */
  123.               end /* do d=1 */
  124.          end /*do until */
  125.      if used=3 then call writeln(tmp,rivi)  /* Save uuencode first line */
  126.      end /* ifused=0 */
  127.   else do
  128.      do until eof(1) | found_it
  129.          rivi=readln(1)
  130.          found_it=pos(startline.used,rivi)
  131.          end /*do until */  
  132.      end /* else do */
  133. return found_it
  134.  
  135. SubDiff: procedure
  136.   /* Differences between strings are calculate by word */
  137.   parse arg s1,s2
  138.   diff=0
  139.   do i=1 to words(s1)
  140.     diff=diff+ (word(s1,i)~=word(s2,i))
  141.   end
  142. return diff
  143.  
  144.  
  145. AddMsg:
  146. /*
  147. ** This changes the subject's (1/n) to (01/n) to allow sorting
  148. */
  149.   parse arg s
  150.   mc=mc+1
  151.   bra=lastpos('(',s)
  152.   if bra>0 then
  153.       slash=pos('/',s,bra)
  154.   else do
  155.       slash=lastpos('/',s)
  156.       bra=lastpos(' ',s,slash)
  157.       end
  158.   if slash-bra=2 then s=left(s,bra)'00'substr(s,bra+1)
  159.   if slash-bra=3 then s=left(s,bra)'0'substr(s,bra+1)
  160.   subject.mc=s
  161.   'GetMailInfo File'
  162.   filename.mc=result
  163. return
  164.  
  165. SortMessages:
  166. /* 
  167. ** A simple algorithm is fastest with relatively few items.
  168. ** There should be no need for something fancy like quicksort :-) 
  169. */
  170.   do i=2 to mc
  171.      do j=1 to i-1
  172.         if upper(subject.j)>upper(subject.i) then do/* let's swap stuff... */
  173.         temp=subject.j
  174.         subject.j=subject.i
  175.         subject.i=temp
  176.         temp=filename.j
  177.         filename.j=filename.i
  178.         filename.i=temp
  179.         temp=part.j
  180.         part.j=part.i
  181.         part.i=temp
  182.         end /* if */
  183.      end /* do j */
  184.   end /* do i */
  185. return  /* Everything should be in order now... */
  186.