home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / fax.lzh / fax
Encoding:
Text File  |  1990-05-06  |  7.3 KB  |  282 lines

  1. : This is a shar archive.  Extract with sh, not csh.
  2. echo x - README
  3. cat > README << '!Funky!Stuff!'
  4. This is an el-cheapo way to implement outgoing-only text-only e-mail to
  5. fax.
  6.  
  7. We use a $198 fax card ("The Complete FAX") in an old IBM-PC.  The PC
  8. is set up as needed by the fax card, with the AUTOEXEC.BAT file set up
  9. to cd into the CFAX directory (where all the fax stuff is stored),
  10. start the fax background process, and then execute the AUTOFAX.BAT file
  11. included in this distribution.
  12.  
  13. On our Sun mailserver, a dedicated serial line (on /dev/tty0d in our
  14. case) runs from the Sun to the serial port on the PC.
  15.  
  16. When someone sends mail to 12345@fax.ucsd.edu, we run the mail through
  17. the 'faxmailer' shell script, which extracts the phone number (after
  18. applying some sanity and security checks), and then queues it into a
  19. fax job in /var/spool/fax.
  20.  
  21. Every 15 minutes or so (from crontab), we run the 'faxq' script, which
  22. fetches previous fax transmission history files from the PC, examines
  23. them to determine whether the transmission was successful, whether to
  24. requeue the message for retransmission, or to return it to the sender
  25. as undeliverable.
  26.  
  27. It then transfers the outgoing text files to the PC, and then finally
  28. transfers a batch script 'QUEUE.BAT' which performs the actual
  29. transmission and then loops back to the 'AUTOFAX' batch script.
  30.  
  31. For $200 and an old PC, we have the ability for E-mail users to send
  32. mail to Fax machines on campus (at last estimate, there were around
  33. 200-400 or so fax machines around UCSD).
  34.  
  35. This is an incredible hack that I dreamed up one afternoon, but it's
  36. been working fine for about two months, and it has demonstrated that
  37. people want the capability.  We're looking into getting some more
  38. sophisticated FAX capability for our E-mail system, but the low demand
  39. (we handle maybe two to five E-Fax messages a day now) doesn't justify
  40. the expense of the available commercial solutions.
  41.  
  42. You'll need Kermit on both the mail server and the PC.  Or maybe you can
  43. get fancy and use some Ethernet mechanism, but I didn't think it was
  44. worth the cost of putting an Ethernet card into the PC and we had lots
  45. of leftover ports on the Ariel card in our Sun.
  46.  
  47. Enjoy.  Hack on it; it'll do you good.
  48.  
  49. (BTW: this is the only document on this there is.  You're on your own
  50. now, son.)
  51.     - Brian Kantor, UCSD, Nov 1989
  52. !Funky!Stuff!
  53. echo x - autofax.bat
  54. cat > autofax.bat << '!Funky!Stuff!'
  55. del queue.bat
  56. del *.txt
  57. del *.0*
  58. kermit server
  59. queue
  60. !Funky!Stuff!
  61. echo x - faxmailer
  62. cat > faxmailer << '!Funky!Stuff!'
  63. #!/bin/sh
  64. cd /var/spool/fax
  65. # make a copy of the file
  66. echo "==============================================================" > f$$.txt
  67. echo "==============================================================" >> f$$.txt
  68. echo "    Wonderous Electronic Mail to FAX System " `date` >> f$$.txt
  69. echo "==============================================================" >> f$$.txt
  70. echo "==============================================================" >> f$$.txt
  71. echo " " >> f$$.txt
  72. echo " " >> f$$.txt
  73. echo " " >> f$$.txt
  74.  
  75. head -200 >> f$$.txt
  76.  
  77. # find the phone number in the Subject: line
  78. PH=`awk -f /usr/local/lib/faxnumber f$$.txt`
  79.  
  80. if [ $PH ] ; then
  81.     :
  82. else
  83.     echo "No valid fax number found in To: or Subject: line"
  84.     rm f$$.txt
  85.     # ( error 65 is "data error" to sendmail)
  86.     exit 65
  87.     fi
  88.  
  89. # put out the actual command to be run
  90. echo "cfaxsend f$$.txt $PH /x /f > f$$.err" > f$$.q
  91.  
  92. exit 0
  93. !Funky!Stuff!
  94. echo x - faxnumber
  95. cat > faxnumber << '!Funky!Stuff!'
  96. BEGIN    { for (i=1; i <= 5; i++)  number[i] = "" }
  97.  
  98. /^To: / { 
  99.     number[1] = substr($0,5);
  100.         if (q = index(number[1], "@"))
  101.             number[1]=substr(number[1], 1, (q-1));
  102.         }
  103.  
  104. /^Subject: / {
  105.     if (p = index($0, "["))
  106.         {
  107.         if (q = index($0, "]"))
  108.             number[2]=substr($0, p+1, (q-p-1));
  109.         }
  110.     if (p = index($0, "("))
  111.         {
  112.         if (q = index($0, ")"))
  113.             number[3]=substr($0, p+1, (q-p-1));
  114.         }
  115.     if (p = index($0, "<"))
  116.         {
  117.         if (q = index($0, ">"))
  118.             number[4]=substr($0, p+1, (q-p-1));
  119.         }
  120.     if (p = index($0, "{"))
  121.         {
  122.         if (q = index($0, "}"))
  123.             number[5]=substr($0, p+1, (q-p-1));
  124.         }
  125.     }
  126. END    {
  127.     for (i=1; i <= 5; i++)
  128.         {
  129.         p = index(number[i],"%");
  130.         if (p > 1)
  131.             number[i] = substr(number[i], 1, p-1);
  132.         # check for illegal first digit - campus phones start 3,4,5
  133.         fd = substr(number[i],1,1);
  134.         if ( fd != 3 && fd != 4 && fd != 5 )
  135.             number[i] = "";
  136.         }
  137.     print number[1] number[2] number[3] number[4] number[5]
  138.     }
  139. !Funky!Stuff!
  140. echo x - faxq
  141. cat > faxq << '!Funky!Stuff!'
  142. #!/bin/sh
  143. #
  144. # this one dequeues the messages from where they're spooling and
  145. # moves them over to the PC.
  146.  
  147. cd /var/spool/fax
  148.  
  149. # following stuffed in to prevent more than one of these running at a
  150. # time 
  151. # Note that shlock compares
  152. # pids so it doesn't matter if a process dies and leaves a lock sitting
  153. #        - Brian
  154.  
  155. if /usr/lib/news/shlock -p $$ -f FAXLOCK ; then
  156.     :
  157. else
  158.     echo ${pname}: "[$$]" already running "[`cat FAXLOCK`]"
  159.     exit 0
  160. fi
  161.  
  162. nfiles=`find . -name '*.q*' -print | wc -l`
  163. if [ $nfiles = "0" ] ; then
  164.     rm FAXLOCK
  165.     exit 0;
  166. fi
  167.  
  168.  
  169. # zap commands left over from last time
  170. rm queue.bat
  171.  
  172. # following grabs all the error files from the last go-round,
  173. /usr/local/bin/kermit << EOF
  174. set line /dev/tty0d
  175. set baud 9600
  176. get f*.err
  177. quit
  178. EOF
  179.  
  180. # analyze the .err files we got, and add the appropriate
  181. # deletes to the outgoing batch file.  Successful faxes get deleted
  182. # from the current spool directory here too; unsuccessful ones get
  183. # requeued
  184.  
  185. for f in *.err ; do
  186.     if [ ! -r $f ] ; then
  187.         continue;
  188.     fi
  189.     fb=`basename $f .err`
  190.     echo $f $fb ;
  191.  
  192.     # we got the errors file, blow it away on the PC
  193.     echo "DEL $f" >> queue.bat ;
  194.  
  195.     # see if it was sent ok
  196.     if ( egrep -i 'Sent successfully' $f ) ; then
  197.         rm $fb.* ;
  198.         continue ;
  199.     fi
  200.  
  201.     # see if it failed miserably
  202.     if ( egrep -i 'error' $f ) ; then
  203.         frm=`egrep From: $fb.txt | sed -e 's/From: //'` ;
  204.         more FAXERR $fb.* | Mail -s "Fax mail error" $frm brian ;
  205.         rm $fb.* ;
  206.         continue ;
  207.     fi;
  208.  
  209.     # otherwise, requeue and remove the errors file
  210.     cat $fb.q.o >> queue.bat ;
  211.     rm $f ;
  212. done
  213.  
  214. # cat all the new queue files onto the batch file
  215. nq=`find . -name '*.q' -print | wc -l`
  216. if [ $nq -gt 0 ] ; then
  217.     for f in *.q ; do
  218.         cat $f >> queue.bat ;
  219.         mv $f $f.o ;
  220.     done ;
  221.  
  222.     # ship over the current text files
  223.     /usr/local/bin/kermit << EOF
  224. set line /dev/tty0d
  225. set baud 9600
  226. send f*.txt
  227. quit
  228. EOF
  229. fi
  230.  
  231. # make sure that the last thing in the batch restarts the PC script
  232. echo "AUTOFAX" >> queue.bat
  233.  
  234. # ship over batch list of things to do
  235. # 'finish' makes the PC kermit exit and execute the queue.bat file
  236. # we just delivered
  237. /usr/local/bin/kermit << EOF
  238. set line /dev/tty0d
  239. set baud 9600
  240. send queue.bat
  241. finish
  242. quit
  243. EOF
  244.  
  245. echo "===================================================" >> /var/log/fax
  246. date >> /var/log/fax
  247. cat queue.bat >> /var/log/fax
  248. rm -f  FAXLOCK
  249. exit 0
  250. !Funky!Stuff!
  251. echo x - sendmail.stuff
  252. cat > sendmail.stuff << '!Funky!Stuff!'
  253. This goes in Ruleset Zero:
  254.  
  255. # resolve the FAX mailer
  256. R$*<@$*fax>$*        $#FAX$@$2$:$1$3            user@fax
  257. R$*<@$*fax.$D>$*    $#FAX$@$2$:$1$3            user@fax.ucsd.edu
  258.  
  259. And this goes wherever you stuff your mailer definitions
  260.  
  261. ############################################################
  262. ############################################################
  263. #####        FAX Mailer specification
  264. ############################################################
  265. ############################################################
  266.  
  267. Mfax,    P=/usr/local/lib/faxmailer, F=rsDFMmnC, S=17, R=27,
  268.     A=faxmailer $u
  269.  
  270. S17
  271. #null
  272. S27
  273. #null
  274. !Funky!Stuff!
  275. echo x - usr.lib.aliases
  276. cat > usr.lib.aliases << '!Funky!Stuff!'
  277. fax:fax@fax.ucsd.edu
  278. !Funky!Stuff!
  279. exit 0
  280.  
  281.  
  282.