home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2219 < prev    next >
Internet Message Format  |  1990-12-28  |  11KB

  1. From: mediratb@p4.cs.man.ac.uk
  2. Newsgroups: alt.sources
  3. Subject: Mail Spooler
  4. Message-ID: <9012061228.AA00435@t4g.cs.man.ac.uk>
  5. Date: 6 Dec 90 12:28:16 GMT
  6.  
  7. From: mediratb@p4.cs.man.ac.uk (Bharat Mediratta)
  8. Organization: University of Manchester Computer Science Department
  9. Subject: A mail spooling program
  10.  
  11. The following shell script allows you to spool large quantities of files
  12. to be mailed on a daily basis without any hassle.  This is a first release
  13. of the shell script and it might have bugs in it.  It is very easy to use.
  14.  
  15. 1) Create a subdirectory in your home directory called 'spool'
  16. 2) Inside this directory, create a subdirectory for each person you want
  17.    spooled mail sent to.  This subdirectory should be the pathname of the
  18.    person the mail is being sent to.  It is usually best if this is a mail
  19.    alias, although this is not necessary.
  20. 3) Inside the ^/spool/<name> directory place the files you wish to be mailed.
  21.  
  22. Example Directories:
  23. % ls -l spool
  24. total 4
  25. drwx------  4 mediratb      512 Dec  3 11:48 ./
  26. drwxr-xr-x 15 mediratb     1024 Dec  5 13:01 ../
  27. drwx------  2 mediratb      512 Dec  5 12:55 alistair/
  28. drwx------  2 mediratb     1024 Dec  5 12:56 jeff/
  29. % ls -l spool/alistair
  30. total 3
  31. -rw-------  1 mediratb      908 Dec  3 11:50 simpsons
  32. -rw-------  1 mediratb      288 Dec  3 11:51 smoking_prohibited
  33. -rw-------  1 mediratb     1200 Dec  3 11:51 software_development.M
  34. % grep alistair ^/.mailrc
  35. alias alistair acampbell%his-site@relay.etc
  36. %
  37.  
  38. To run the program you can either stick it in cron, which is difficult for
  39. some people, or put a line in your .login so that it runs every time you
  40. log in.  Right now, the program is set to exit if you try to run it more than
  41. once per day.  So if you stick it in your .login, it won't send off mail every
  42. time you log in - just once a day.  If you don't want to see the output, use
  43. a line like this:
  44.  
  45. ((nice +10 ^mediratb/bin/cmd/spoolmail) >/dev/null &) >/dev/null
  46.  
  47. It is important to nice the job - especially on a workstation - or there will
  48. be considerable slowdown...
  49. It will be difficult to reach me to give me bug reports because I am finishing
  50. a semester in Manchester and am about to begin a semester in Wales, for which
  51. I don't know my e-mail address.  You can send email to my account back in the
  52. states (bmediratta@colgateu.bitnet) and I'll read it in June.  Alternatively,
  53. you can send mail here and I will try to log on from time to time and check it.
  54.  
  55. Enjoy!
  56.   -Bharat
  57.  
  58. +------------------+------------------------------------------------------+
  59. | Bharat Mediratta | mediratb@p4.cs.man.ac.uk                             |
  60. +------------------+------------------------------------------------------+
  61. | "When the going gets weird, the weird turn pro" - Hunter S. Thompson    |
  62. | "You want it all, but you can't have it" - Faith No More (Epic)         |
  63. | "On a clear disk you can seek forever..."                               |
  64. |                                                                         |
  65. |   (@@@@)    /-----------------------\                                   |
  66. |  (@@@@@@)   | I'm Bharat Simpson!   |                                   |
  67. |  (@@@@@@)   | Don't Eat a Cow, Dude!|                                   |
  68. |  |      |   \-----------------------/                                   |
  69. |  | (o)(o)   /                                                           |
  70. |  C      _) /                                                            |
  71. |   | ,___| -                                                             |
  72. |   |   /                                                                 |
  73. |  /____\                                                                 |
  74. | /      \                                                                |
  75. +-------------------------------------------------------------------------+
  76.  
  77. ------------- CUT HERE ---------------- CUT HERE --------------------
  78. #!/bin/csh -f
  79. #                                  SpoolMail
  80. # Description:
  81. #   Allows you to send large quantities of files over the network without
  82. #   boosting the bandwith.  The program sends out a chunk (preset to 50K)
  83. #   a day.
  84. #
  85. # Author:
  86. #   Bharat Mediratta, Colgate '92
  87. #   Written at the Univ. of  Manchester when I wanted to ship all
  88. #   of my neat stuff back to Colgate after a year abroad - without
  89. #   killing the bandwith.
  90. #
  91. # Usage:
  92. #   Create a spool directory in your home directory (you can adjust
  93. #   this by changing the variables set below.)  Inside the spool
  94. #   directory, create a directory for each person you wish to have
  95. #   mail spooled to.  The name of this directory should be the pathname
  96. #   you wish the mail sent to.  It is probably good to use an alias
  97. #   that you define in your .mailrc file, etc. although this is not
  98. #   necessary.  Any file put inside this directory will be sent to the
  99. #   appropriate person and then either a) removed or b) renamed to
  100. #   <file>.M depending on the option you define below.  Optional logging
  101. #   can be toggled below also.  When logging is on, you will receive a
  102. #   complete report of all files mailed that day via mail itself.
  103. #
  104. # Dependencies:
  105. #   csh, dc, awk, grep, ls, mail, date
  106. #
  107. # Files:
  108. #   $HOME/.spooldate    - keep track of the last time the program was run
  109. #   /tmp/spoollog.<pid> - temporary file for the spool report
  110. #
  111. # Bugs:
  112. #   An annoying tendency to split the files in such a way as to create
  113. #   a last file with only a few K in it.  Then it goes on to put a header
  114. #   at the top of the file saying something like 'File: Part 7 out of 6'
  115. #   Solution?  Use a better file splitting program and a smart algorithm.
  116. #
  117. # Variables
  118. set rmormv = mv                 # Remove or Move?  rm|mv
  119. set logging = on                #  on|off
  120. set log = /tmp/spoollog.$$      # Temporary file for log
  121. set spool = $HOME/spool         # Spool directory
  122. set datefile = $HOME/.spooldate # Date file to keep track of last run
  123. set MAX = 51200                 # Maximum (bytes) sent to one person
  124.  
  125. # First, check to see if the program has been run in the last day.  If it
  126. # has been, simply exit - we only want to mail packets once a day.
  127. if (-e $datefile) then
  128.     set last = `awk ' print $3 ' < $datefile`
  129.     set today = `date | awk ' print $3 '`
  130.     if ($last == $today) then
  131.         exit 0
  132.     endif
  133. endif
  134.  
  135. # Update the date executed file...
  136. date > $datefile
  137.  
  138. # For each person in the spool directory, mail them MAX worth of files
  139. # without splitting any of them up.  If a file in the spool directory
  140. # is too large to be sent without being split, split it up into approximately
  141. # 25K parts and then try again.
  142. cd $spool
  143. foreach foo (`/bin/ls`)
  144.     set total = 0           # Total = number of bytes actually sent
  145.     echo Checking $foo >> $log
  146.     cd $foo
  147.     set dir = `/bin/ls`     # for some stupid reason, csh won't let me
  148.     if ("$dir" != "") then  # combine these two lines! :-(
  149.  
  150.         # First, check to see if any file in the directory exceeds
  151.         # $MAX, if so, split it up into roughly MAX/2 K files
  152.         foreach bar (`/bin/ls -s | sort -r | awk ' if (NR != 1) print $2 '`)
  153.             if ("`echo $bar | grep .M`" == "") then
  154.                 set size = `ls -l $bar | awk ' print $4 '`
  155.                 set ok = `echo "$size $MAX <" | dc`
  156.                 if ("$ok" != "") then
  157.                     # The file is too big!
  158.                     echo $bar \($size\) is over $MAX >> $log
  159.                     set howmany = `echo "$size $MAX 2 / / p" | dc`
  160.                     set linetotal = `wc -l $bar | awk ' print $1 '`
  161.                     set linespersplit = `echo "$linetotal $howmany / p" | dc`
  162.                     echo Splitting $bar into $howmany pieces >> $log
  163.                     split -$linespersplit $bar $bar.
  164.  
  165.                     # See if the sum of the parts = the whole
  166.                     echo $bar \($size\) is now: >> $log
  167.                     set parttotal = 0
  168.                     foreach bletch (`/bin/ls $bar.*`)
  169.                         set partsize = `ls -l $bletch | awk ' print $4 '`
  170.                         echo $bletch \($partsize\) >> $log
  171.                         set parttotal = `echo "$partsize $parttotal + p" | dc`
  172.                     end
  173.  
  174.                     # If so, remove the whole, else remove the parts
  175.                     if ($parttotal != $size) then
  176.                         echo The parts don\'t add up\! >> $log
  177.                         echo Removing `/bin/ls $bar.*` >> $log
  178.                         rm $bar.*
  179.                     else
  180.                         echo Removing $bar... >> $log
  181.                         rm $bar
  182.                         # Stick a note in each file saying which one it is
  183.                         # and how many are coming total.
  184.                         set count = 1
  185.                         foreach bletch (`/bin/ls $bar.*`)
  186.                             echo "--- $bar Part $count out of $howmany ---"\
  187.                                      >/tmp/$bletch
  188.                             cat $bletch >> /tmp/$bletch
  189.                             cp /tmp/$bletch $bletch
  190.                             rm /tmp/$bletch
  191.                             set count = `echo "$count 1 + p" | dc`
  192.                         end
  193.                     endif
  194.                 else
  195.                     # We have found a file that is smaller than MAX.  Since
  196.                     # the files were sorted in descending order, that means
  197.                     # all the rest are smaller than MAX so skip them...
  198.                     break
  199.                 endif
  200.             endif
  201.         end
  202.  
  203.         # Then attempt to send each file, without exceeding the size limit
  204.         # Start with the largest file, and work down to the smallest
  205.         foreach bar (`/bin/ls -s | sort -r | awk ' if (NR != 1) print $2 '`)
  206.             if ("`echo $bar | grep .M`" == "") then
  207.                 set size = `ls -l $bar | awk ' print $4 '`
  208.                 set ok = `echo "$size $total + $MAX <" | dc`
  209.                 if ("$ok" == "") then
  210.                     set total = `echo "$size $total + p" | dc`
  211.                     echo Sending $bar \($size\), Total: $total >> $log
  212.                     mail -s "$bar" $foo < $bar
  213.                     if ("$rmormv" == "mv") then
  214.                         echo $bar is now $bar.M >> $log
  215.                         mv $bar $bar.M
  216.                     else
  217.                         echo Removing $bar >> $log
  218.                         rm $bar
  219.                     endif
  220.                 endif
  221.             endif
  222.         end
  223.         if ($total == 0) then
  224.             echo Sent nothing from $foo >> $log
  225.         endif
  226.     else
  227.         echo $foo is empty >> $log
  228.     endif
  229.     cd $spool
  230. end
  231.  
  232. # Mail a copy of the log to the user (if he wants it)
  233. if ("$logging" == "on") then
  234.     mail -s "Spool Log" $user < $log
  235.     rm $log
  236. endif
  237.