pipeto.lib 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # deliver mail to a local file
  24. fn spool {
  25. if(~ $#* 0)
  26. _mbox=$MBOX
  27. if not
  28. _mbox=$1
  29. $BIN/deliver $RECIP $D/from $_mbox < $D/raw || exit $status
  30. }
  31. # spool but change the subject line to note spam
  32. fn spool-tagged-spam {
  33. if(~ $#* 0)
  34. _mbox=$MBOX
  35. if not
  36. _mbox=$1
  37. {
  38. cat $D/rawheader | sed 's/^[Ss][Uu][Bb][Jj][Ee][Cc][Tt]:/& SPAM:/'
  39. if(! grep -si '^subject:' $D/rawheader)
  40. echo 'Subject: SPAM: '
  41. echo
  42. cat $D/rawbody
  43. } | $BIN/deliver $RECIP $D/from $_mbox || exit $status
  44. }
  45. # forward mail to a list of addresses
  46. fn forward {
  47. upasname=`{awk '{print $2}' $D/unixheader} cat $D/raw | upas/send $* || exit $status
  48. }
  49. # pipe mail through a command
  50. fn pipe {
  51. if(~ $#* 0)
  52. exit 'bad pipe command'
  53. {cat $D/unixheader $D/raw; echo} | $* || exit $status
  54. }
  55. # add @domain to all unqualified addresses in the message
  56. fn qualify {
  57. if(! ~ $#* 1){
  58. echo 'usage: qualify domain' >[1=2]
  59. exit bad-qualify
  60. }
  61. {
  62. sed 1q $TMP.msg
  63. cat $TMP.msg | sed 1d | upas/smtp -fh $1 a b
  64. } >$TMP.msg2 || exit $status
  65. mv $TMP.msg2 $TMP.msg || exit $status
  66. unmount /mail/fs
  67. upas/fs -f $TMP.msg || exit $status
  68. }
  69. # classify message according to token-based white list
  70. fn tokenfilter {
  71. if($BIN/list check $PF $D/from $D/sender $D/replyto)
  72. echo match
  73. if not if(~ $status *!match*)
  74. echo !match
  75. if not if($BIN/token $KEY $D/subject)
  76. echo token
  77. if not
  78. echo new
  79. }
  80. # reject a message due to the token-based white list
  81. fn tokenreply {
  82. TOKEN=`{upas/token $KEY}
  83. if(! ~ $#MAILTO 1)
  84. exit 'bad token reply: no MAILTO variable'
  85. {
  86. cat /mail/lib/token.msg |
  87. sed 's/TOKEN/'$TOKEN'/g;s/USER/'$USER'/g;s/MAILTO/'$MAILTO'/g'
  88. cat $D/raw
  89. } | upasname=/dev/null mail `{cat $D/replyto}
  90. }
  91. # add addresses in message to white list
  92. fn listupdate {
  93. $BIN/list add $PF $D/from $D/to $D/cc $D/sender
  94. }
  95. # microsoft virus going around 9/22/2003
  96. fn isvirus {
  97. virus=no
  98. if(grep -s '\.(exe|scr|bat|com)' $D/2/2/mimeheader >[2]/dev/null){
  99. s=`{ls -l $D/2/2/raw | awk '{print $6}'}
  100. if(~ $s 1440??) virus=yes
  101. }
  102. if(~ $virus yes)
  103. status=''
  104. if not
  105. status='not this virus'
  106. }
  107. # bayesian spam filter. alternative to token. see /mail/lib/setup.bayes
  108. fn isspam {
  109. for(i in _prof.mbox _prof.spam _bounced){
  110. if(! test -f $i){
  111. echo 'need '^$i >[1=2]
  112. exit 'need '^$i
  113. }
  114. }
  115. {
  116. echo '# hash table'
  117. upas/msgcat $TMP.msg | upas/msgtok |
  118. grep -v '^....................(.*) ' |
  119. sed 's/$/ 1/'
  120. } >$TMP.tok
  121. x=`{upas/bayes -k _prof.mbox _prof.spam ~ $TMP.tok | sed 's/_prof.//'}
  122. where=$x(1)
  123. prob=$x(2)
  124. prob1000=`{echo $prob '*1000' | hoc | sed 's/\..*//'}
  125. echo `{sed 's/^From ([^ ]+) (.*)/\2 from \1/' $D/unixheader} $x >>_bounced
  126. if(~ $where spam && test $prob1000 -lt 999)
  127. where=mbox
  128. upas/addhash -o _prof.$where _prof.$where 1 $TMP.tok 1
  129. if(~ $where spam)
  130. where=''
  131. status=$where
  132. }
  133. # save and parse the mail file
  134. sed '/^$/,$ s/^From / From /' >$TMP.msg
  135. upas/fs -f $TMP.msg || exit $status