home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / admin / 4304 < prev    next >
Encoding:
Internet Message Format  |  1992-07-24  |  9.9 KB

  1. Path: sparky!uunet!gatech!ukma!widener!netnews.upenn.edu!eniac.seas.upenn.edu!chip
  2. From: chip@eniac.seas.upenn.edu (Charles H. Buchholtz)
  3. Newsgroups: comp.unix.admin
  4. Subject: Re: mail expiration program
  5. Message-ID: <84309@netnews.upenn.edu>
  6. Date: 24 Jul 92 12:59:56 GMT
  7. References: <1992Jul23.203951.13691@msuinfo.cl.msu.edu>
  8. Sender: news@netnews.upenn.edu
  9. Organization: University of Pennsylvania
  10. Lines: 419
  11. Nntp-Posting-Host: eniac.seas.upenn.edu
  12.  
  13. wey@convex.cl.msu.edu (Lih-Er Wey) writes:
  14. >I am looking for a program which will ckeck every user's system mail
  15. >box and expires the mail which is older than some certain number of
  16. >days. Any help will be appreciated.
  17.  
  18. [Reformatted to <80 columns, and removed bogus "comp" distribution]
  19.  
  20. We run such a script monthly, and it is a teriffic help in managing
  21. our systems.  It takes 5 parameters:
  22.  
  23. WarnSize
  24. MaxSize
  25. MungeSize
  26. WarnLetter
  27. MungeLetter
  28.  
  29. It checks every file in /usr/spool/mail.  If the file is less than
  30. WarnSize, it does nothing.  If the letter is larger than WarnSize and
  31. less than MaxSize, it send WarnLetter to the owner.  
  32.  
  33. If the file is larger than MaxSize, it splits it into two parts.
  34. The "new" part contains the most recent "MungeSize" messages, or all
  35. messages received during the last month, whichever is larger.  This
  36. part stays in /usr/spool/mail.  The older part (all the rest) is
  37. placed in the owners home directory named "mbox.month-year".  Finally,
  38. MungeLetter is sent to the owner.
  39.  
  40. MungeLetter explains how to read the mail which was shifted to the
  41. home directory.
  42.  
  43. No mail is ever deleted.
  44.  
  45. Scripts to follow signature.
  46.  
  47. Charles H. Buchholtz       Systems Programmer     chip@seas.upenn.edu
  48.           School of Engineering and Applied Science
  49.               University of Pennsylvania
  50.  
  51. This is called "mailcop"
  52.  
  53. #!/bin/sh
  54. #
  55. # Check to see if a user has too much mail - if so, send her/him mail
  56. # written by James F. Kennedy - jfk@ee.upenn.edu, jfk@seas.upenn.edu
  57. # revised by J. Eric Fosler - fosler@seas.upenn.edu
  58. #
  59. # Last revision: Apr 2nd, 1991
  60. # Revised: v2.0 June 13th, 1991 - jfk
  61. #          v2.1 5 Dec 1991 - fosler
  62. #          v2.2 25 Dec 1991 - chip
  63. #             handle mailmunger errors better
  64. #
  65. # Usage: mailcop warnsize maxsize mungesize [warnletter [mungeletter]]
  66. #
  67. # warnsize is the size at which a user is sent a warning letter.  maxsize
  68. # is the size at which the file is munged and mungesize is the size to munge
  69. # the file to.  filename is optional filename of the letter to warn people.
  70. #
  71. # warnsize and maxsize are in blocks while mungesize is in bytes
  72. #
  73. # The default letter
  74. #     default munger letter
  75. #     mail backup file
  76. #
  77. TMUCHL=/usr/local/lib/mailcop/mailcop.letter
  78. MUNGERMAIL=/usr/local/lib/mailcop/mailmunger.letter
  79. MAILBACKUP=/home2/tar/mail.tar
  80. MONTH=`date '+%h-%y'`
  81. #
  82. # our temporary files
  83. #
  84. TMPDIR=/tmp
  85. TLIST=$TMPDIR/mailcop.list.$$
  86. MLIST=$TMPDIR/mailmunger.list.$$
  87. TMPNULL=$TMPDIR/mailcop.null.$$
  88. #
  89. # the mail directory to scan
  90. #
  91. MAILDIR=/var/spool/mail
  92. #
  93. # various commands
  94. #
  95. SORT=/bin/sort
  96. LS="/bin/ls -s"
  97. MAIL=/usr/ucb/mail
  98. MUNGER=/usr/local/etc/mailmunger
  99. RM=/bin/rm
  100. COMPRESS=/usr/ucb/compress
  101. TARADD="/bin/tar -uf"
  102. TARCREATE="/bin/tar -cf"
  103. CAT=/bin/cat
  104. #
  105. # usage statement
  106. #
  107. USAGE="Usage: mailcop warnsize maxsize mungesize [warnletter [mungeletter]]"
  108. #
  109. # other miscellaneous things
  110. #
  111. SUBJECT="Your mail file"
  112. MSUBJECT="Mail File Munged"
  113. #
  114. # set our umask to 600 so no one else can bother us
  115. #
  116. umask 077
  117. #
  118. # make sure that the core file doesn't exist (mailer will barf this one back
  119. # and also emacs backup (*~) files as these aren't valid userids either.
  120. #
  121. rm -f $MAILDIR/core $MAILDIR/*~
  122. #
  123. # Let's see if there's at least one argument, if not - display USAGE statement
  124. # otherwise, set maxsize to be the first argument.
  125. #
  126. if [ $1 ]; then
  127.     WARNSIZE=$1
  128. else
  129.     echo $USAGE
  130.     exit 1
  131. fi
  132. #
  133. # get second argument - maxsize and third arg - mungesize to determine
  134. # at what size and to what size we munge a file.
  135. #
  136. #
  137. if [ $2 ]; then
  138.     MAXSIZE=$2
  139. else
  140.     echo $USAGE
  141.     exit 1
  142. fi
  143.  
  144. if [ $3 ]; then
  145.     MUNGESIZE=$3
  146. else
  147.     echo $USAGE
  148.     exit 1
  149. fi
  150. #
  151. # now for the optional arguments
  152. #
  153. if [ $4 ]; then
  154.     TMUCHL=$4
  155. fi
  156. if [ $5 ]; then
  157.     MUNGERMAIL=$5
  158. fi
  159. #
  160. # Now make sure the files exist
  161. #
  162. if [ -f $TMUCHL ]; then
  163.     :
  164. else
  165.     echo "$0: Warning letter does not exist."
  166.      exit 1
  167. fi
  168. if [ -f $MUNGERMAIL ]; then
  169.     :
  170. else
  171.     echo "$0:Munging letter does not exist."
  172.      exit 1
  173. fi
  174. #
  175. # AWKSCRIPT is an awk program to process "ls -s" listings.  It ignores
  176. # popper files (.*.pop) and the total line, and only prints files
  177. # larger than MAXSIZE - chip@seas
  178. #
  179. AWKSCRIPT='$1 > '$WARNSIZE' && $1 != "total" && $2 !~ /\..*\.pop/ { print $2 }'
  180. MUNGESCRIPT='$1 > '$MAXSIZE' && $1 != "total" && $2 !~ /\..*\.pop/ { print $2 }'
  181. #
  182. # First check for people who should be munged
  183. #
  184. $LS $MAILDIR | awk "$MUNGESCRIPT" > $MLIST
  185. #
  186. # and now munge them and let them know
  187. #
  188. if [ -f "$MAILBACKUP.Z" ]; then
  189.     $RM "$MAILBACKUP.Z"
  190. fi
  191. if [ -f $MLIST ]; then
  192.     export RCPT MONTH
  193.     $CAT /dev/null > $TMPNULL
  194.     $TARCREATE $MAILBACKUP $TMPNULL
  195.     $RM $TMPNULL
  196.     $CAT $MLIST | 
  197.       (while read RCPT; do
  198.         if $TARADD $MAILBACKUP $MAILDIR/$RCPT
  199.         then
  200.           if $MUNGER $MAILDIR/$RCPT $MAILDIR/$RCPT $RCPT $MUNGESIZE
  201.           then
  202.             $MAIL -s "$MSUBJECT" $RCPT < $MUNGERMAIL
  203.             #output from cron jobs will be mailed
  204.             echo "$RCPT: mail file munged."
  205.           fi
  206.         fi
  207.       done)
  208.     $COMPRESS $MAILBACKUP
  209. fi
  210. #
  211. # Do a directory listing of the mail directory and pipe the output
  212. # to awk which will check for all files greater than WARNSIZE blocks
  213. # This awk program will eliminate the "total" line and popper files.
  214. #
  215. $LS $MAILDIR | awk "$AWKSCRIPT" > $TLIST
  216. #
  217. # Now read each line of the temp file and mail the user the letter.
  218. #
  219. if [ -f $TLIST ]; then
  220.     cat $TLIST | (while read RCPT; do
  221.             $MAIL -s "$SUBJECT" $RCPT < $TMUCHL
  222.             # output from cron jobs will be mailed
  223.             echo "$RCPT: warned."
  224.              done)
  225. fi
  226. #
  227. #cleanup
  228. #
  229. rm -f $TLIST $MLIST
  230. exit 0
  231.  
  232.  
  233. This is called "mailmunger":
  234.  
  235. #!/bin/sh
  236. #
  237. # mailmunger - created and written by James F. Kennedy jfk@ee.upenn.edu
  238. #            - revised by J. Eric Fosler - fosler@seas.upenn.edu
  239. #
  240. # Revised: v1.0 June 12th, 1991 -jfk
  241. #          v1.1 5 Dec 1991 - fosler
  242. #       v1.2 25 Dec 1991 - chip
  243. #              correctly determine home directory on any system
  244. #
  245. # mailmunger-
  246. #    truncates <srcfile> to specified <targetsize> and puts the older
  247. #    part of the file in <targetdirectory> and the newer part in <targetfile>
  248. #
  249. # since this is called from tmuch..we assume that RCPT, MUNGERMAIL, MONTH and
  250. # SUBJECT are set in the environment!
  251. #
  252. # define where the binaries are
  253. #
  254. MAIL=/usr/ucb/mail
  255. MV=/bin/mv
  256. CP=/bin/cp
  257. CAT=/bin/cat
  258. COMPRESS=/usr/ucb/compress
  259. WC=/usr/ucb/wc
  260. SPLIT=/bin/csplit
  261. TAIL=/usr/ucb/tail
  262. AWK=/bin/awk
  263. RM=/bin/rm
  264. TOUCH=/bin/touch
  265. CUT=/bin/cut
  266. CHOWN=/etc/chown
  267. CHMOD=/bin/chmod
  268. #
  269. #
  270. # default targetsize is 20000 bytes
  271. #
  272. TARGETSIZE=20000
  273. #
  274. # define a usage string
  275. #
  276. USAGE="Usage: mailmunger <sourcefile> <targetfile> <targetdirectory> <targetsize>"
  277. #
  278. #
  279. # temporary files and other things
  280. #
  281. TMPDIR=/tmp
  282. TMPSRCFILE=$TMPDIR/mungersrc.$$
  283. TMPPREFIX=$TMPDIR/munger$$
  284. PERMS=600
  285. #
  286. # argument one should be the source file
  287. #
  288. if [ $1 ]; then
  289.     if [ -r $1 ]; then
  290.         SRCFILE=$1
  291.     else
  292.         echo "$0: Source file $1 does not exist"
  293.         exit 1
  294.     fi
  295. else
  296.     echo $USAGE
  297.     exit 1
  298. fi
  299. #
  300. # argument 2 is the target file
  301. #
  302. if [ $2 ]; then
  303.     TARGETFILE=$2
  304. else
  305.     echo $USAGE
  306.     exit 1
  307. fi
  308. #
  309. # Find the  target directory and  make sure that this exists
  310. #
  311.  
  312. if [ $3 ]; then
  313.     USER=$3
  314.     TARGETDIR=`egrep "^$3:" /etc/passwd | cut -d: -f6`
  315.     if [ -d $TARGETDIR ]; then
  316.         :
  317.     else
  318.         echo $USAGE
  319.         echo "$0: Target directory $TARGETDIR not a directory"
  320.         exit 1
  321.     fi
  322. else
  323.     echo $USAGE
  324.     exit 1
  325. fi
  326.  
  327. #
  328. # argument 4 is the targetsize
  329. #
  330. if [ $4 ]; then
  331.     TARGETSIZE=$4
  332. fi
  333.  
  334. #
  335. # see if the source file exists and that it is mungable
  336. #
  337. if [ -s $SRCFILE ]; then
  338.     X=`$WC -c $SRCFILE | $CUT -c-8`
  339.     if [ $TARGETSIZE -gt $X ]; then
  340.         echo "$0: File already at or below desired size."
  341.         exit 0
  342.     fi
  343. else
  344.     echo "$0: Cannot munge zero-length file."
  345.     exit 1
  346. fi
  347. #
  348. # now, determine about what line number will be our cut-off point.
  349. #
  350. # first, mv srcfile to tmpsrcfile
  351. #
  352.  
  353. $MV $SRCFILE $TMPSRCFILE
  354. #
  355. # make a backup of the mail file - just in case...
  356. #
  357. $CP $TMPSRCFILE $TMPDIR/mungerback.$$
  358. #
  359. # now, using awk - we will calculate the approximate area of demarcation
  360. #
  361. Y=`$WC $TMPSRCFILE | $AWK '{ N1=($3-'$TARGETSIZE')/$3;N2=N1*$1;printf("%d\n", N2) }'`
  362. #
  363. # once we have the approximate area...we now must find the exact area by 
  364. # looking for the next "From_" (where _ is a space)
  365. #
  366. X=`$TAIL +$Y"l" $TMPSRCFILE | $AWK 'BEGIN {LINES = '$Y'} /^From\ /{ printf("%d\n", LINES); exit } { LINES=LINES+1 }'`
  367. #
  368. # just in case the file can't be munged period we check to see if X contains
  369. # a valid field - if so, we split it using System V csplit (useful utility:)
  370. # otherwise, we simply mv the original mail file back
  371. # after we check for new mail and append that to the mail file.
  372. #
  373. if [ "$X" ]; then
  374.     $SPLIT -f$TMPPREFIX $TMPSRCFILE $X > /dev/null
  375. else
  376. #
  377. # check to see if it's busy
  378. #
  379.     while [ -f "$TARGETFILE.lock" ];
  380.         do
  381.       sleep 1
  382.     done
  383.     $TOUCH "$TARGETFILE.lock"
  384.  
  385.     if [ -f $TARGETFILE ]; then
  386.         $CAT $TARGETFILE >> $TMPSRCFILE
  387.     fi
  388.     $MV $TMPSRCFILE $TARGETFILE
  389.     $CHOWN $RCPT $TARGETFILE
  390.     $CHMOD $PERMS $TARGETFILE
  391.     $RM "$TARGETFILE.lock"
  392. #
  393. # we don't need to notify anyone, so we simply exit
  394. #
  395.     exit 0
  396. fi
  397. #
  398. # otherwise, we take the second file created by csplit and move it to
  399. # the original file, provided we save any new file by appending it
  400. # to the old
  401. #
  402. #
  403. # check to see if it's busy
  404. #
  405. while [ -f "$TARGETFILE.lock" ];
  406. do
  407.   sleep 1
  408. done
  409. $TOUCH "$TARGETFILE.lock"
  410.  
  411. if [ -f $TARGETFILE ]; then
  412.     $CAT $TARGETFILE >> $TMPPREFIX"01"
  413.     $MV $TMPPREFIX"01" $TARGETFILE
  414. else
  415.     $MV $TMPPREFIX"01" $TARGETFILE
  416. fi
  417.  
  418. $RM "$TARGETFILE.lock"
  419. #
  420. # now we compress the munged part and put it in the target directory
  421. #
  422. # $COMPRESS $TMPPREFIX"00"
  423. $MV $TMPPREFIX"00" "$TARGETDIR/mbox-$MONTH"
  424. $CHOWN $RCPT "$TARGETDIR/mbox-$MONTH"
  425. $CHOWN $RCPT $TARGETFILE
  426. $CHMOD $PERMS "$TARGETDIR/mbox-$MONTH" $TARGETFILE
  427. #
  428. # if we've gotten this far, it should be safe to remove all of our
  429. # temps and backups
  430. #
  431. rm -f $TEMPPREFIX"*" $TMPSRCFILE $TMPDIR/mungerback.$$
  432.