home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Spezial / SPEZIAL2_97.zip / SPEZIAL2_97.iso / ANWEND / ONLINE / IMPRO / AUTORPLY.ZIP / autoreply.cmd next >
OS/2 REXX Batch file  |  1997-03-15  |  6KB  |  212 lines

  1. /* AutoReply Rexx Script */
  2.  
  3. /*
  4.  * This script will send an automatic reply based on
  5.  * the username passed to it.
  6.  *
  7.  */
  8.  
  9. /* Date: 15 March 1997 */
  10.  
  11. uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  12. lowercase = 'abcdefghijklmnopqrstuvwxyz'
  13. Env = 'OS2ENVIRONMENT'
  14. FALSE = 0
  15. TRUE = 1
  16. HeadFrom = ''
  17. HeadTo = ''
  18. HeadReplyTo = ''
  19. HeadSubject = ''
  20. HeadDate = ''
  21. HeadCc = ''
  22. HeadSender = ''
  23. HeadEmail = ''
  24. ReplyTo = ''                   /* Point to an address for more info */
  25. Mailer = 'hmailer'
  26. WhereAmI = 'example.com'       /* Define to your local hostname */
  27. Sender = 'AutoReply'           /* Where errors will go */
  28.  
  29. /* The external functions we need */
  30. call RxFuncAdd 'SysTempFileName', 'RexxUtil', 'SysTempFileName'
  31. call RxFuncAdd 'SysFileDelete', 'RexxUtil', 'SysFileDelete'
  32.  
  33. /* start main function */
  34. /* The first arg is who the message was sent to.
  35.  * The second is the filename. We're responsible
  36.  * for cleaning up the file if needed.
  37.  */
  38. parse arg UserName MsgFile
  39.  
  40. /* remove the domain part of the address */
  41. parse var UserName UserName '@' Domain
  42.  
  43. /* Find out where the files are */
  44. AutoReply = value('autoreply',,Env)  /* comment this line to not use the environment variable */
  45. /*AutoReply = 'd:\beta\autoreply'*/  /* uncomment this line to specify a directory */
  46.  
  47. /* change to the directory for autoreply files */
  48. Junk = directory(AutoReply)
  49.  
  50. /* Find out who the message is from */
  51. rc = stream(MsgFile, 'c', 'open read')
  52. call ParseHeaders
  53. rc = stream(MsgFile, 'c', 'close')
  54.  
  55. /* Figure out who to send mail back to */
  56. if HeadReplyTo <> '' then
  57.   HeadEmail = HeadReplyTo
  58. else
  59.   HeadEmail = HeadFrom
  60. /* now clean up the email address */
  61. HeadEmail = NormalizeEmail(HeadEmail)
  62. HeadEmail = translate(HeadEmail, lowercase, uppercase)
  63.  
  64. if HeadEmail = '' then do
  65.   say 'No valid return address found.'
  66.   exit
  67.   end
  68.  
  69. /* create a temp file for the outgoing message */
  70. OutFile = SysTempFileName('f?????.tmp', '?');
  71. rc = stream(OutFile, 'C', 'OPEN WRITE')  /* open the file for writing */
  72. call WriteHeaders /* the headers for the outgoing message */
  73. rc = stream(OutFile, 'C', 'close')       /* close the file */
  74.  
  75. ReplyFile = AutoReply'\'UserName'.txt'   /* append the reply file to the */
  76. rc = AppendLock(ReplyFile OutFile)      /* headers file */
  77.  
  78. /* the request is processed, now send the message back */
  79. /* first create a file with the email address in it */
  80. EmailFile = SysTempFileName('e?????.tmp', '?')
  81. rc = stream(EmailFile, 'C', 'OPEN WRITE')  /* open the file for writing */
  82. rc = lineout(EmailFile, HeadEmail, )
  83. rc = stream(EmailFile, 'C', 'CLOSE')
  84.  
  85. say 'Calling mailer'
  86. /* now start the mailer */
  87. Mailer Sender'@'WhereAmI EmailFile OutFile
  88.  
  89. /* Remove the request file from the user */
  90. rc = SysFileDelete(MsgFile)
  91.  
  92. return
  93.  
  94. /* ------------------------------------------------------------------ */
  95. /*
  96.  * Normalize the email address into a SMTP form
  97.  *
  98.  */
  99.  
  100. NormalizeEmail: procedure
  101.  
  102. parse arg All
  103.  
  104. rc = pos('<', All, )
  105. if rc = 0 then
  106.   do
  107.   /* in case some mailers use () instead of <> */
  108.   All = translate(All, '<', '(')
  109.   All = translate(All, '>', ')')
  110.   end
  111.  
  112. parse var All Part1 '<' Part2 '>' Part3
  113.  
  114. rc = pos('@', Part1, )
  115. if rc <> 0 then 
  116.   do
  117.   Part1 = strip(Part1, 'B', )  /* we must strip any blanks leftover */
  118.   if Part2 <> '' then Author = Part2
  119.   else if Part3 <> '' then Author = Part3
  120.   else Author = Part1
  121.   return Part1
  122.   end
  123.  
  124. rc = pos('@', Part2, )
  125. if rc <> 0 then
  126.   do
  127.   Part2 = strip(Part2, 'B', )
  128.   if Part1 <> '' then Author = Part1
  129.   else if Part3 <> '' then Author = Part3
  130.   else Author = Part2
  131.   return Part2
  132.   end
  133.  
  134. rc = pos('@', Part3, )
  135. if rc <> 0 then
  136.   do
  137.   Part3 = strip(Part3, 'B', )
  138.   if Part2 <> '' then Author = Part2
  139.   else if Part1 <> '' then Author = Part1
  140.   else Author = Part3
  141.   return Part3
  142.   end
  143.  
  144. return ''  /* error finding SMTP email address */
  145.  
  146. /* ------------------------------------------------------------------ */
  147. /*
  148.  * Parse RFC822 headers
  149.  *
  150.  */
  151.  
  152. ParseHeaders: procedure expose HeadTo HeadFrom HeadReplyTo MsgFile HeadSubject ,
  153.               lowercase uppercase HeadDate HeadCc HeadSender FALSE TRUE
  154.  
  155. say 'ParseHeaders starting'
  156.  
  157. Line = linein(MsgFile)                /* get a line of the file */
  158. do while Line <> ''                   /* until end of headers */
  159.   parse var Line Key ':' Val          /* separate out the components */
  160.   Key = translate(Key, lowercase, uppercase)
  161.   select
  162.     when Key = 'to' then
  163.       HeadTo = Val
  164.     when Key = 'reply-to' then
  165.       HeadReplyTo = Val
  166.     when Key = 'from' then
  167.       HeadFrom = Val
  168.     when Key = 'subject' then
  169.       HeadSubject = Val
  170.     when Key = 'date' then
  171.       HeadDate = Val
  172.     when Key = 'cc' then
  173.       HeadCc = Val
  174.     when Key = 'sender' then
  175.       HeadSender = Val
  176.     otherwise nop
  177.     end   /* select */
  178.   Line = linein(MsgFile)
  179. end       /* do while */
  180.  
  181. return
  182.  
  183. /* ------------------------------------------------------------------ */
  184. /*
  185.  * Write out our standard headers for a message
  186.  *
  187.  */
  188.  
  189. WriteHeaders:  /* note that we have full access to all globals here */
  190.  
  191. TimeZone = value( 'TZ', , Env)
  192. TmpTime = time('N')
  193. DayOfWeek = date('W')
  194. DayOfWeek = left(DayOfWeek, 3)
  195. TmpDate = date('N')
  196. /* we put in the local time for date so that posts are chronological */
  197. rc = lineout(OutFile, 'Date:' DayOfWeek',' TmpDate TmpTime TimeZone, )
  198. rc = lineout(OutFile, 'Sender:' Sender '<'Sender'@'WhereAmI'>', )
  199. rc = lineout(OutFile, 'From:' HeadTo, )
  200. if ReplyTo <> '' then
  201.   rc = lineout(OutFile, 'Reply-To:' ReplyTo, )
  202. rc = lineout(OutFile, 'To:' HeadEmail, )
  203. rc = lineout(OutFile, 'Subject: Re:' HeadSubject, )
  204. rc = lineout(OutFile, '', )
  205.  
  206. return
  207.  
  208. /* ------------------------------------------------------------------ */
  209.  
  210.  
  211.  
  212.