pipeto.lib 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # create a /tmp for here documents
  2. rfork en
  3. bind -c /mail/tmp /tmp
  4. # caller should set KEY and USER
  5. if(! ~ $#USER 1)
  6. USER=`{echo $1 | sed 's/local!//;s/[+\-].*//'}
  7. if(! ~ $#KEY 1)
  8. KEY=plan9
  9. cd /mail/box/$USER
  10. RECIP=$1
  11. MBOX=$2
  12. PF=/mail/box/$USER/_pattern
  13. TMP=/mail/tmp/$pid.$sysname
  14. BIN=/bin/upas
  15. D=/mail/fs/mbox/1
  16. # clean up files on exit
  17. fn sigexit {
  18. rm -f $TMP.*
  19. }
  20. fn log {
  21. if(~ $#USER 1)
  22. echo `{date} $* >>/mail/box/$USER/_log >[2]/dev/null
  23. }
  24. # deliver mail to a local file
  25. fn spool {
  26. if(~ $#* 0)
  27. _mbox=$MBOX
  28. if not
  29. _mbox=$1
  30. $BIN/deliver $RECIP $D/from $_mbox < $D/raw || exit $status
  31. }
  32. # forward mail to a list of addresses
  33. fn forward {
  34. upasname=`{awk '{print $2}' $D/unixheader} cat $D/raw | upas/send $* || exit $status
  35. }
  36. # pipe mail through a command
  37. fn pipe {
  38. if(~ $#* 0)
  39. exit 'bad pipe command'
  40. {cat $D/unixheader $D/raw; echo} | $* || exit $status
  41. }
  42. # add @domain to all unqualified addresses in the message
  43. fn qualify {
  44. if(! ~ $#* 1){
  45. echo 'usage: qualify domain' >[1=2]
  46. exit bad-qualify
  47. }
  48. {
  49. sed 1q $TMP.msg
  50. cat $TMP.msg | sed 1d | upas/smtp -fh $1 a b
  51. } >$TMP.msg2 || exit $status
  52. mv $TMP.msg2 $TMP.msg || exit $status
  53. unmount /mail/fs
  54. upas/fs -f $TMP.msg || exit $status
  55. }
  56. # classify message according to token-based white list
  57. fn tokenfilter {
  58. if($BIN/list check $PF $D/from $D/sender $D/replyto)
  59. echo match
  60. if not if(~ $status *!match*)
  61. echo !match
  62. if not if($BIN/token $KEY $D/subject)
  63. echo token
  64. if not
  65. echo new
  66. }
  67. # reject a message due to the token-based white list
  68. fn tokenreply {
  69. TOKEN=`{upas/token $KEY}
  70. if(! ~ $#MAILTO 1)
  71. exit 'bad token reply: no MAILTO variable'
  72. {
  73. cat /mail/lib/token.msg |
  74. sed 's/TOKEN/'$TOKEN'/g;s/USER/'$USER'/g;s/MAILTO/'$MAILTO'/g'
  75. cat $D/raw
  76. } | upasname=/dev/null mail `{cat $D/replyto}
  77. }
  78. # add addresses in message to white list
  79. fn listupdate {
  80. $BIN/list add $PF $D/from $D/to $D/cc $D/sender
  81. }
  82. # save and parse the mail file
  83. sed '/^$/,$ s/^From / From /' >$TMP.msg
  84. upas/fs -f $TMP.msg || exit $status