procmail
[Top] [All Lists]

Re: need help with a forwarding script

2000-03-10 13:17:03
Russ Salerno wrote,

| My employer has a single customer service email address which
| is receiving more emails than a single person can handle.  So, they want to
| distribute each incoming email to one of several recipients in round-robin
| fashion (1st email goes to recipient #1, next to #2, next to #3, next to
| #1... and so forth).
|
| I imagine the script can keep track of who got the last email by reading and
| writing a counter to a file.  The actual recipients can be kept in a
| separate file or hard-coded into the script.  The counter is read,
| incremented, saved, the next email recipient is determined therefrom, and
| finally the email gets forwarded (without saving a copy in the original
| recipient account.)
  
Well, let's see.  Assuming we've already determined that the message at hand
needs distribution to the next CS employee up, and therefore we're in an
rcfile or a brace nest specifically for such mail ...

path_to_victimfile is a listing, one address per line, of employees to whom
such mail should be forwarded.  path_to_pointerfile contains

  LASTVICTIM=##

where ## is the line number where the address of the last employee to be
forwarded a message appears in path_to_victimfile.  If you are first starting
the setup, or if you are reinitializing it from scratch, path_to_pointerfile
should read

  LASTVICTIM=0

And now, here's the procmailrc code:

  LOCKFILE=.victims.lock
  INCLUDERC=path_to_pointerfile # either absolute, or relative to $MAILDIR
  TOTALVICTIMS=`wc -l < path_to_victimfile` # or hard-code the number
                                            # if it rarely changes
  :0
  * $ $LASTVICTIM^0
  * 1^0
  { }  
  THISVICTIM = $=
  
  :0 # in case we roll off the end of the list
  * $ $THISVICTIM^0
  * $ -$TOTALVICTIMS^0
  { THISVICTIM = $= }  # Could we safely say "{ THISVICTIM = 1 }" instead?
                       # No, because someone could just have been removed
                       # from the end.
  :0cir
  | echo "LASTVICTIM=$THISVICTIM" > path_to_pointerfile

  LOCKFILE

  :0
  ! `sed -e ${THISVICTIM}q -ed path_to_victimfile`

People can be skipped or hit double when names are added or removed, unless
you lock $MAILDIR/.victim.lock and adjust the pointer as well when you change
the list of names.

You could even set up an editing script like this:

 #!/bin/sh
 cd wherever_$MAILDIR_is
 lockfile .victim.lock
 ${VISUAL:-$EDITOR} path_to_victimfile path_to_pointerfile
 rm -f .victim.lock

... so that you can make sure the pointer is set fairly (remember, it gives
the line number of the last person, not that of the next person, to get sent
a CS message) after names are added or removed.

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