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

  1. /*
  2. ** Join.rexx 1.1 - 18-Jan-97 by Kai.Nikulainen@utu.fi
  3. **
  4. ** Joins and uudecodes messages from ftp-mailservers.  The problem with this
  5. ** script is there are almost as many formats of messages as there are servers.
  6. ** So, if you are having problems with your server send me mail and I'll try
  7. ** to fix the problem.
  8. **
  9. ** This script only decodes uuencoded files now.  Base64 coding will be added
  10. ** if there are requests for it.  I have tested the script with following servers
  11. ** (which all have different message formats...)
  12. **  -mailserver@nic.funet.fi
  13. **  -mailserver@leo.org
  14. **  -ftp-mail@uni-paderborn.de
  15. **  -Anything mailed with SplitNMail.rexx should work
  16. **
  17. ** Send a message to any of the above addresses with word HELP in message body and
  18. ** you will receive instructions on how to use them.
  19. **
  20. ** Installation: 
  21. **  - Copy the script to yam:rexx
  22. **  - Check that variables tempfile, uudecode and del_temp have suitable values.
  23. **    Especially uudecode may need to be changed, depending on which uudecoder
  24. **    you are using.
  25. **
  26. ** Usage:
  27. **  - Just select a message and start the script.  The script will find all messages
  28. **    in the current folder from the same sender with almost the same subject, sort
  29. **    them and write the uucoded part to a file and then execute the uudecode command
  30. **  - If something goes wrong, check t:uu.log for a possible explanation.
  31. **
  32. ** Send all comments, bug reports, suggestions and X-mas cards to knikulai@utu.fi
  33. */
  34.  
  35. options results
  36. call addlib('rexxsupport.library',0,-30)
  37.  
  38.  
  39.    tempfile='t:join.tmp'    /* All parts are joined to this file */
  40.    uudecode='c:uudecode >>t:uu.log' tempfile 'path ram:'
  41.    del_temp='NO'    /* set to yes, if you want to delete the joined ASCII file */
  42.  
  43. startline.1='BEGIN -----'
  44.  stopline.1='END -----'
  45. startline.2='BEGIN PART'
  46.  stopline.2='END PART'
  47. startline.3='begin '
  48.  stopline.3='include '
  49. delimiters=3
  50.  
  51. mc=0 
  52. used=0
  53.  
  54. address 'YAM'
  55. 'GetMailInfo From'
  56. server=result
  57. 'GetMailInfo Subject'
  58. subj=result
  59. 'GetMailInfo Active'
  60. active=result
  61. 'GetFolderInfo Max'
  62. n=result
  63.  
  64. /* 
  65. ** First scan all messages in the folder.
  66. ** Search for messages with same sender and at most 4 different chars in subject 
  67. */
  68.  
  69. do m=0 to n-1
  70.     'SetMail' m
  71.     'GetMailInfo From'
  72.     if result=server then do
  73.        'GetMailInfo Subject'
  74.        if SubDiff(result,subj)<5 then call AddMsg(result)
  75.     end /* if result=server then do */
  76. end /* do m=1 to n */
  77.  
  78. /*
  79. ** Then sort the messages.  Hopefully part 10 comes after 9 and not after 1....
  80. */
  81.  
  82. call SortMessages
  83.  
  84. /*
  85. ** Lets write the messages into one file
  86. */
  87. if open(tmp,tempfile,'w') then do
  88.     do i=1 to mc
  89.         call open(1,filename.i,'r')
  90.     if findstart() then do
  91.         find_end=0
  92.         /* 
  93.         ** Next copy lines to tempfile until the end of file,
  94.         ** stopline is found or an empty line.  The last line is
  95.         ** not written to the tempfile.
  96.         */
  97.         do until find_end | eof(1) | rivi=''
  98.             rivi=readln(1)
  99.         find_end=pos(stopline.used,rivi)=1
  100.         if find_end=0 then call writeln(tmp,rivi)
  101.         end /* do until */
  102.         end /* if FindStart then */
  103.     else do 
  104.         /*
  105.         ** There should not be need to read every part twice, but I'm
  106.         ** supposing someone did the posting manually and the start
  107.         ** and stoplines were left out by mistake from some parts.
  108.         ** Reading the message avoids screwing things up if some
  109.         ** parts have delimiters and some doesn't.
  110.         */
  111.         call close(1) 
  112.         call open(1,filename.i,'r') /* Let's try again with better luck... */
  113.         rivi=readln(1)
  114.         /* Find first empty line */
  115.         do until rivi='' | eof(1) 
  116.             rivi=readln(1)
  117.         end
  118.         /* Find first non blank line */
  119.         do until rivi~='' | eof(1)
  120.                 rivi=readln(1)
  121.             end
  122.         /* Copy lines until the next blank line */
  123.         do until rivi='' | eof(1)
  124.             call writeln(tmp,rivi)
  125.                 rivi=readln(1)
  126.             end
  127.         end /* else */
  128.     call close(1)    
  129.         end /* do i=1 to mc */
  130.     end /* if open(*/
  131. else
  132.     'Request "Can not open' tempfile '" "_Ok"'
  133.  
  134. 'SetMail' active        /* Go back to the originally selected message */
  135. call close(tmp)            /* File must be closed before it can be read  */
  136. address command uudecode    /* Do some magic */
  137.  
  138. if upper(del_temp)='YES' then delete(tempfile)
  139.  
  140. exit    /* It's the end of the script as we know it... */
  141.  
  142. FindStart:
  143.   found_it=0
  144.   if used=0 then do
  145.   /* 
  146.   ** When called the first time, this branch checks which delimiters are used 
  147.   ** Following calls use else branch
  148.   */
  149.      do until eof(1) | found_it
  150.          rivi=readln(1)
  151.          do d=1 to delimiters
  152.              if pos(startline.d,rivi)=1 then do
  153.               found_it=1
  154.               used=d
  155.           end /* if */
  156.               end /* do d=1 */
  157.          end /*do until */
  158.      if used=3 then call writeln(tmp,rivi)  /* Save uuencode first line */
  159.      end /* ifused=0 */
  160.   else do
  161.      do until eof(1) | found_it
  162.          rivi=readln(1)
  163.          found_it=pos(startline.used,rivi)
  164.          end /*do until */  
  165.      end /* else do */
  166. return found_it
  167.  
  168. SubDiff: procedure
  169.   /* Differences between strings are calculate by word */
  170.   parse arg s1,s2
  171.   diff=0
  172.   do i=1 to words(s1)
  173.     diff=diff+ChrDiff( word(s1,i) , word(s2,i) )
  174.   end
  175. return diff
  176.  
  177. ChrDiff: procedure
  178.   parse arg s1,s2
  179.   if length(s1)+1=length(s2) then s1='0's1
  180.   if length(s1)-1=length(s2) then s2='0's2
  181.   diff=abs(length(s1)-length(s2))
  182.   do i=1 to length(s1)
  183.     if substr(s1,i,1)~=substr(s2,i,1) then diff=diff+1
  184.   end
  185. return diff
  186.  
  187. AddMsg:
  188. /*
  189. ** This changes the subject's 'part n' to 'part 0n' to allow sorting
  190. */
  191.   parse arg s
  192.   mc=mc+1
  193.   l=pos('part ',s)
  194.   if l>0 then
  195.      subject.mc=left(s,l-1) || 'part 0' || substr(s,l+5)
  196.   else
  197.      subject.mc=s
  198.   'GetMailInfo File'
  199.   filename.mc=result
  200. return
  201.  
  202. SortMessages:
  203. /* 
  204. ** A simple algorithm is fastest with relatively few items.
  205. ** There should be no need for something fancy like quicksort :-) 
  206. */
  207.   do i=2 to mc
  208.      do j=1 to i-1
  209.         if subject.j>subject.i then do/* let's swap stuff... */
  210.         temp=subject.j
  211.         subject.j=subject.i
  212.         subject.i=temp
  213.         temp=filename.j
  214.         filename.j=filename.i
  215.         filename.i=temp
  216.         end /* if */
  217.      end /* do j */
  218.   end /* do i */
  219. return  /* Everything should be in order now... */
  220.