pipeto.lib 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. # create a /tmp for here documents
  2. rfork en
  3. bind -c /mail/tmp /tmp
  4. # caller should set KEY
  5. USER=`{echo $1 | sed 's/local!//;s/[+\-].*//'}
  6. if(! ~ $#KEY 1)
  7. KEY=plan9
  8. cd /mail/box/$USER
  9. RECIP=$1
  10. MBOX=$2
  11. PF=/mail/box/$USER/_pattern
  12. TMP=/mail/tmp/$pid.$sysname
  13. BIN=/bin/upas
  14. D=/mail/fs/mbox/1
  15. # clean up files on exit
  16. fn sigexit {
  17. rm -f $TMP.*
  18. }
  19. fn log {
  20. if(~ $#USER 1)
  21. echo `{date} $* >>/mail/box/$USER/_log >[2]/dev/null
  22. }
  23. fn gonefishing {
  24. if (test -e gone.fishing) {
  25. MAILTO=`{cat $D/replyto}
  26. grep '^'$"MAILTO'$' gone.addrs >/dev/null >[2=1] || {
  27. echo $MAILTO >>gone.addrs
  28. message=gone.msg
  29. if (! test -e $message)
  30. message=/mail/lib/gone.msg
  31. # only respond if $USER is mentioned in To: or cc:
  32. # header. this avoids autoresponding to mailing lists.
  33. tohdr=`{cat $D/to}
  34. cchdr=`{cat $D/cc}
  35. if (~ $"tohdr $USER@* *!$USER $USER ||
  36. ~ $"cchdr $USER@* *!$USER $USER)
  37. mail $MAILTO <$message
  38. if not
  39. status=''
  40. }
  41. }
  42. if not
  43. status='' # ensure good exit status
  44. }
  45. # deliver mail to a local file
  46. fn spool {
  47. if(~ $#* 0)
  48. _mbox=$MBOX
  49. if not
  50. _mbox=$1
  51. $BIN/deliver $RECIP $D/from $_mbox < $D/raw || exit $status
  52. gonefishing
  53. }
  54. # spool but change the subject line to note spam
  55. fn spool-tagged-spam {
  56. if(~ $#* 0)
  57. _mbox=$MBOX
  58. if not
  59. _mbox=$1
  60. {
  61. cat $D/rawheader | sed 's/^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:/& SPAM:/'
  62. if(! grep -si '^subject:' $D/rawheader)
  63. echo 'Subject: SPAM: '
  64. echo
  65. cat $D/rawbody
  66. } | $BIN/deliver $RECIP $D/from $_mbox || exit $status
  67. gonefishing
  68. }
  69. # forward mail to a list of addresses
  70. fn forward {
  71. upasname=`{awk '{print $2}' $D/unixheader} cat $D/raw | upas/send $* || exit $status
  72. }
  73. # pipe mail through a command
  74. fn pipe {
  75. if(~ $#* 0)
  76. exit 'bad pipe command'
  77. {cat $D/unixheader $D/raw; echo} | $* || exit $status
  78. }
  79. # add @domain to all unqualified addresses in the message
  80. fn qualify {
  81. if(! ~ $#* 1){
  82. echo 'usage: qualify domain' >[1=2]
  83. exit bad-qualify
  84. }
  85. {
  86. sed 1q $TMP.msg
  87. cat $TMP.msg | sed 1d | upas/smtp -fh $1 a b
  88. } >$TMP.msg2 || exit $status
  89. mv $TMP.msg2 $TMP.msg || exit $status
  90. unmount /mail/fs
  91. upas/fs -pf $TMP.msg || exit $status
  92. }
  93. # classify message according to token-based white list
  94. fn tokenfilter {
  95. if($BIN/list check $PF $D/from $D/sender $D/replyto)
  96. echo match
  97. if not if(~ $status *!match*)
  98. echo !match
  99. if not if($BIN/token $KEY $D/subject)
  100. echo token
  101. if not
  102. echo new
  103. }
  104. # reject a message due to the token-based white list
  105. fn tokenreply {
  106. TOKEN=`{upas/token $KEY}
  107. if(! ~ $#MAILTO 1)
  108. exit 'bad token reply: no MAILTO variable'
  109. {
  110. cat /mail/lib/token.msg |
  111. sed 's/TOKEN/'$TOKEN'/g;s/USER/'$USER'/g;s/MAILTO/'$MAILTO'/g'
  112. cat $D/raw
  113. } | upasname=/dev/null mail `{cat $D/replyto}
  114. }
  115. # add addresses in message to white list
  116. fn listupdate {
  117. $BIN/list add $PF $D/from $D/to $D/cc $D/sender
  118. }
  119. # microsoft virus going around 9/22/2003
  120. fn isvirus {
  121. virus=no
  122. if(grep -s '\.(exe|scr|bat|com)' $D/2/2/mimeheader >[2]/dev/null){
  123. s=`{ls -l $D/2/2/raw | awk '{print $6}'}
  124. if(~ $s 1440??) virus=yes
  125. }
  126. if(~ $virus yes)
  127. status=''
  128. if not
  129. status='not this virus'
  130. }
  131. fn hasgifattachment {
  132. gif=no
  133. if(grep -s '\.gif' $D/2/mimeheader >[2]/dev/null){
  134. gif=yes
  135. }
  136. if(~ $gif yes)
  137. status=''
  138. if not
  139. status='not this gif'
  140. }
  141. # bayesian spam filter. alternative to token. see /mail/lib/setup.bayes
  142. fn isspam {
  143. for(i in _prof.mbox _prof.spam _bounced){
  144. if(! test -f $i){
  145. echo 'need '^$i >[1=2]
  146. exit 'need '^$i
  147. }
  148. }
  149. {
  150. echo '# hash table'
  151. upas/msgcat $TMP.msg | upas/msgtok |
  152. grep -v '^....................(.*) ' |
  153. sed 's/$/ 1/'
  154. } >$TMP.tok
  155. x=`{upas/bayes -k _prof.mbox _prof.spam ~ $TMP.tok | sed 's/_prof.//'}
  156. where=$x(1)
  157. prob=$x(2)
  158. prob1000=`{echo $prob '*1000' | hoc | sed 's/\..*//'}
  159. echo `{sed 's/^From ([^ ]+) (.*)/\2 from \1/' $D/unixheader} $x >>_bounced
  160. if(~ $where spam && test $prob1000 -lt 999)
  161. where=mbox
  162. upas/addhash -o _prof.$where _prof.$where 1 $TMP.tok 1
  163. if(~ $where spam)
  164. where=''
  165. status=$where
  166. }
  167. # save and parse the mail file
  168. sed '/^$/,$ s/^From / From /' >$TMP.msg
  169. upas/fs -pf $TMP.msg || exit $status