procmail
[Top] [All Lists]

Re: help with taking input from body [resent]

1996-10-24 11:05:27
    > I would like to match a subject "search_archive" and take the body  
    > and filter it through a 'for' loop
    > 
    > the format of the body would be like this
    > 
    > somestring
    > someotherword
    > thirdword
    > fourthword
    > 
    > I would like to use the body like this
    > 
    > for i in somestring someotherword thirdword fourthword
    > do
    >   grep $i file
    > done
    > 
    > there could be ANY number of lines in the body of the message
    > 
    > 
    > Is this feasible?
    > 
    > I have no idea where to begin

Please read the "man" page for "fgrep" and "egrep".  Pay special
attention to the '-f' option.

Below is a recipe to search for strings or regexps in a file, depending
upon whether the subject is given as "search_archive" or
"grep_archive", respectively.  The strings are specified in the body of
the current message, and the subject is must be the two words "search"
or "egrep" followed by the word "archive", with a separator of "_",
"-", or " ".  The results are returned in the reply.

    :0
    * ^Subject: *\/(search|fgrep)[-_ ]archive
    { GREP=fgrep QUERY=$MATCH }
    :0E
    * ^Subject: *\/e?grep[-_ ]archive
    { GREP=egrep QUERY=$MATCH }
    :0A                 # process only one of the previous recipes succeeded
    * $!X-Loop: *$LOGNAME(_at_)$HOST
    {  
       ARCHIVE=archive  # name the file to be searched
       SEARCHFILE=search.tmp
                        # use a global lock file to keep the searchfile
                        # intact
       LOCKFILE=$SEARCHFILE.lock
       :0cb             # make a copy of the body in a tmp file
       $SEARCHFILE
       :0ci:            # keep a log of these queries
       | formail -X"From:" -X"Date: -k >>search.log
       :0fh             # replace the headers
       | formail -rt -I"From: $LOGNAME's Archive Server <$LOGNAME>" \
                     -I"Subject: Results of your $QUERY query" \
                     -I"Precedence: bulk" \
                     -I"X-Loop: $LOGNAME(_at_)$HOST"
       :0fbWi           # replace the body with the query results
       | $GREP -f $SEARCHFILE $ARCHIVE
       LOCKFILE         # we don't need the $SEARCHFILE any more
       :0efb
       | echo "I'm sorry, but nothing matched the query below:" ; \
         sed -e 's/^/>>> /'
       :0               # send the reply (and finish)
       ! -t
    }

Try this.  It might need a tweak here or there.

Good luck.
___________________________________________________________
Alan Stebbens <aks(_at_)sgi(_dot_)com>      http://reality.sgi.com/aks

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