procmail
[Top] [All Lists]

Re: archiving all but 100 newest messages to other folder

2001-04-18 12:32:45
Ben followed up,

| The messages that need to stay would be the bottom 100.

OK.  Here's the situation: procmail is good at appending incoming messages to
folders but it needs help to prepend them.  On the other hand, formail is
good at counting messages from the top of a folder but helpless to count them
from the bottom.  So holding the most recent N messages of those in a folder
means either making procmail put new ones at the top so that formail can
easily count N of them or finding some way for formail to know when it is N
from the bottom.  Either way, one program gets to do its normal thing while
the other needs a shell invoked so that a program called within .procmailrc
can read the entire existing contents of the mbox.

The advantage to appending messages at doing the extra work to tell formail
where it will be N messages from the end over doing the extra work to prepend
messages and letting formail just count N messages down is that you don't
have to reverse the message order inside the mbox when you first institute
the system.

Anyhow, the code is going to be something like this; I'm assuming that your
procmail is new enough to honor SWITCHRC, as that greatly uncomplicates the
nesting;

 :0 condition-related flags
 * conditions for appending to that mbox
 {
  # set these four variables to suitable values
  to_hold=100  foldername=whatever  archive=wherever  tolerance=5

  LOCKFILE=$foldername$LOCKEXT # regional lockfile

  :0c
  $foldername

  :0e # if save failed
  {
   LOCKFILE # clear regional lockfile
   SWITCHRC=/dev/null # treat as if fallen off end of rcfile
  }

  :0i # generates one `x' per message in mbox; thank you, Stephen
  xes=|formail -I'From x' -x'From ' < $foldername
 
  :0 # see if number of messages (one per `x') exceeds number to hold
  * 1^1 xes ?? x
  * $ -$to_hold^0
  { excess = $= }

  # archive only if $excess is greater than $tolerance; if $tolerance is
  # null or unset, assume it's 0 (i.e., archive if even one extra message)
  :0
  * $ $excess^0
  * $ -${tolerance:-0}^0
  {
   :0
   * SHELL ?? csh^^
   { SHELL=/bin/sh }

   :0wi
   | formail -${excess}s < $foldername >> $archive \
     && mv $foldername /tmp/$foldername \
     && formail +${excess}s < /tmp/$foldername > $foldername \
     && rm -f /tmp/$foldername
  }

  :0 # if no archiving was needed, or if archiving failed, bail
  /dev/null
 }

_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail