procmail
[Top] [All Lists]

Re: Brock's size test

1999-12-24 07:43:21
Brock continued under separate cover,

| I realized that we have an all or nothing situation here.
| Either you backup the whole piece of email (when it's under the size
| set) or you get only the headers.
| 
| Any ideas for a way to get it to save just the first x bytes of the
| email? Then we eliminate the testing of size, we just save up to x and
| throw the rest away. Sort of like a "split" command.
| 
| If we have to use the "split" command, then to speed it up we WOULD use a
| size test, because it's pointless to call the "split" command to truncate
| an email to x bytes if the email is already under x -- thus we'd use
| "split" only when it was over x.

Does your version of head support a -b or -c option?  Head would work much
better than split, because it filters from stdin to stdout without creating
another file that has to be removed, and it will stop after the number of
bytes you want to save instead of going on to save more files that you won't
use at all.

So we could try something like this:

 :0
 * ! ^X-Backup-Disable:
 {
  BKPSIZE=some_number # fill it in
  BKP=some_folder

  # if the entire message is smaller or equal to limit, save it all
  :0c: # if $BKP is a directory, don't bother with the local lock
  * $ ! > $BKPSIZE
  $BKP

  # if the head alone meets or exceeds the limit, save the whole head
  :0Ehc: # same comment about local lock
  * $ ! H ?? < $BKPSIZE
  $BKP

  # if we hit the limit somewhere in the body, cut there
  :0Efi
  | head -c $BKPSIZE
   :0A: # same comment about local lock
   $BKP
 }

The reasons I wrote the last part as a filter plus a save rather than as a
pipe like this:

    :0Ei: # same comment about local lock
    | head -c $BKPSIZE >> $BKP

are that (1) $BKP might be a directory rather than a file and (2) if it is a
file, the first form gives procmail a chance to make sure that there will be
a closing newline on the saved copy if it was cut in the middle of a line.

<Prev in Thread] Current Thread [Next in Thread>