<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <HTML ><HEAD ><link rel='stylesheet' type='text/css' href='manpage.css'> <!-- $Id: maildropex.sgml,v 1.5 2004/07/27 01:36:41 mrsam Exp $ --> <!-- Copyright 1998 - 2001 Double Precision, Inc. See COPYING for --> <!-- distribution information. --> <meta name="MSSmartTagsPreventParsing" content="TRUE"> <link rel="icon" href="icon.gif" type="image/gif" /> <TITLE >maildropex</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"></HEAD ><BODY CLASS="REFENTRY" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><H1 ><A NAME="MAILDROPEX" ></A >maildropex</H1 ><DIV CLASS="REFNAMEDIV" ><A NAME="AEN10" ></A ><H2 >Name</H2 >maildropex -- maildrop filtering language examples</DIV ><DIV CLASS="REFSYNOPSISDIV" ><A NAME="AEN13" ></A ><H2 >Synopsis</H2 ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN14" ></A ><P ><TT CLASS="FILENAME" >$HOME/.mailfilter</TT >, <TT CLASS="FILENAME" >$HOME/.mailfilters/*</TT ></P ><P ></P ></DIV ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN18" ></A ><H2 >DESCRIPTION</H2 ><P >If <TT CLASS="FILENAME" >$HOME/.mailfilter</TT > exists, filtering instructions in this file will be carried out prior to delivering the message. The filtering instructions may instruct <B CLASS="COMMAND" >maildrop</B > to discard the message, save the message in a different mailbox, or forward the message to another address. If <TT CLASS="FILENAME" >$HOME/.mailfilter</TT > does not exist, or does not provide explicit delivery instructions, <B CLASS="COMMAND" >maildrop</B > delivers the message to the user's system mailbox.</P ><P >The files in <TT CLASS="FILENAME" >$HOME/.mailfilters</TT > are used when <B CLASS="COMMAND" >maildrop</B > is invoked in embedded mode.</P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN28" ></A ><H2 >EXAMPLES</H2 ><P >Take all mail that's sent to the 'auto' mailing list, and save it in <TT CLASS="FILENAME" >Mail/auto</TT >. The 'auto' mailing list software adds a "<TT CLASS="LITERAL" >Delivered-To: auto@domain.com</TT >" header to all messages: <A NAME="AEN33" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN34" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >if (/^Delivered-To: *auto@domain\.com$/) to Mail/auto</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE > After the <B CLASS="COMMAND" >to</B > command delivers the message, <B CLASS="COMMAND" >maildrop</B > automatically stops filtering and terminates without executing the subsequent instructions in the <SPAN CLASS="SYSTEMITEM" >filter file</SPAN >.</P ><P >Take all mail from <CODE CLASS="EMAIL" ><<A HREF="mailto:boss@domain.com" >boss@domain.com</A >></CODE > about the current project status, save it in <TT CLASS="FILENAME" >Mail/project</TT >, then forward a copy to John: <A NAME="AEN42" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN43" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >if (/^From: *boss@domain\.com/ \ && /^Subject:.*[:wbreak:]project status[:wbreak:]/) { cc "!john" to Mail/project }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE > Note that it is necessary to use a backslash in order to continue the <B CLASS="COMMAND" >if</B > statement on the next line.</P ><P >Keep copies of the last 50 messages that you received in the <TT CLASS="FILENAME" >maildir</TT > directory 'backup'. NOTE: 'backup' must be a <TT CLASS="FILENAME" >maildir</TT > directory, not a mailbox. You can create a <TT CLASS="FILENAME" >maildir</TT > using the <B CLASS="COMMAND" >maildirmake</B > command. <A NAME="AEN51" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN52" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >cc backup `cd backup/new && rm -f dummy \`ls -t | sed -e 1,50d\``</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE > Put this at the beginning of your filter file, before any other filtering instructions. This is a good idea to have when you are learning <B CLASS="COMMAND" >maildrop</B >. If you make a mistake and accidentally delete a message, you can recover it from the backup/new subdirectory.</P ><P >Save messages that are at least 100 lines long (approximately) into <TT CLASS="FILENAME" >Mail/IN.Large:</TT >: <A NAME="AEN57" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN58" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" > if ( $LINES > 100 ) to Mail/IN.Large</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></P ><P >Send messages from the auto mailing list to the program 'archive', using a lock file to make sure that only one instance of the archive program will be running at the same time: <A NAME="AEN61" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN62" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" > if (/^Delivered-To: *auto@domain\.com$/) dotlock "auto.lock" { to "|archive" }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></P ><P >Check if the <TT CLASS="LITERAL" >Message-ID:</TT > header in the message is identical to the same header that was recently seen. Discard the message if it is, otherwise continue to filter the message: <A NAME="AEN66" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN67" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >`reformail -D 8000 duplicate.cache` if ( $RETURNCODE == 0 ) exit</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE > The <A HREF="reformail.html" TARGET="_top" >reformail</A > command maintains a list of recently seen Message-IDs in the file <TT CLASS="FILENAME" >duplicate.cache</TT >.</P ><P >Here's a more complicated example. This fragment is intended to go right after the message has been filtered according to your regular rules, and just before the message should be saved in your mailbox: <A NAME="AEN72" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN73" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >cc $DEFAULT xfilter "reformail -r -t" /^To:.*/ getaddr($MATCH) =~ /^.*/; MATCH=tolower($MATCH) flock "vacation.lock" { `fgrep -iqx "$MATCH" vacation.lst 2>/dev/null || { \ echo "$MATCH" >>vacation.lst ; \ exit 1 ; \ } ` } if ( $RETURNCODE == 0 ) exit to "| ( cat - ; echo ''; cat vacation.msg) | $SENDMAIL"</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></P ><P >This code maintains a list of everyone who sent you mail in the file called <TT CLASS="FILENAME" >vacation.lst</TT >. When a message is received from anyone that is not already on the list, the address is added to the list, and the contents of the file <TT CLASS="FILENAME" >vacation.msg</TT > are mailed back to the sender. This is intended to reply notify people that you will not be answering mail for a short period of time.</P ><P >The first statement saves the original message in your regular mailbox. Then, <A HREF="maildropfilter.html#xfilter" TARGET="_top" ><B CLASS="COMMAND" >xfilter<</B ></A > is used to generate an autoreply header to the sender. The <TT CLASS="LITERAL" >To:</TT > header in the autoreply - which was the sender of the original message - is extracted, and the <A HREF="maildropfilter.html#getaddr" TARGET="_top" ><B CLASS="COMMAND" >getaddr</B ></A > function is used to strip the person's name, leaving the address only. The file <TT CLASS="FILENAME" >vacation.lst</TT > is checked, using a lock file to guarantee atomic access and update (overkill, probably). Note that the backslashes are required.</P ><P >If the address is already in the file, <B CLASS="COMMAND" >maildrop</B > exits, otherwise the contents of <TT CLASS="FILENAME" >vacation.msg</TT > are appended to the autoreply header, and mailed out.</P ><P >Here's a version of the vacation script that uses a GDBM database file instead. The difference between this script and the previous script is that the previous script will send a vacation message to a given E-mail address only once. The following script will store the time that the vacation message was sent in the GDBM file. If it's been at least a week since the vacation message has been sent to the given address, another vacation message will be sent.</P ><P >Even though a GDBM database file is used, locking is still necessary because the GDBM library does not allow more than one process to open the same database file for writing: <A NAME="AEN90" ></A ><BLOCKQUOTE CLASS="BLOCKQUOTE" ><DIV CLASS="INFORMALEXAMPLE" ><P ></P ><A NAME="AEN91" ></A ><TABLE BORDER="0" BGCOLOR="#E0E0E0" WIDTH="100%" ><TR ><TD ><PRE CLASS="PROGRAMLISTING" >cc $DEFAULT xfilter "reformail -r -t" /^To:.*/ getaddr($MATCH) =~ /^.*/; MATCH=tolower($MATCH) flock "vacation.lock" { current_time=time; if (gdbmopen("vacation.dat", "C") == 0) { if ( (prev_time=gdbmfetch($MATCH)) ne "" && \ $prev_time >= $current_time - 60 * 60 * 24 * 7) { exit } gdbmstore($MATCH, $current_time) gdbmclose } } to "| ( cat - ; echo ''; cat vacation.msg) | $SENDMAIL"</PRE ></TD ></TR ></TABLE ><P ></P ></DIV ></BLOCKQUOTE ></P ><P >This script requires that <B CLASS="COMMAND" >maildrop</B > must be compiled with GDBM support enabled, which is done by default if GDBM libraries are present.</P ><P >After you return from vacation, you can use a simple Perl script to obtain a list of everyone who sent you mail (of course, that can also be determined by examining your mailbox).</P ></DIV ><DIV CLASS="REFSECT1" ><A NAME="AEN96" ></A ><H2 >SEE ALSO</H2 ><P ><A HREF="maildrop.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >maildrop</SPAN >(1)</SPAN ></A >, <A HREF="maildropfilter.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >maildropfilter</SPAN >(7)</SPAN ></A >, <A HREF="reformail.html" TARGET="_top" ><SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >reformail</SPAN >(1)</SPAN ></A >, <SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >egrep</SPAN >(1)</SPAN >, <SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >grep</SPAN >(1)</SPAN >, <SPAN CLASS="CITEREFENTRY" ><SPAN CLASS="REFENTRYTITLE" >sendmail</SPAN >(8)</SPAN >.</P ></DIV ></BODY ></HTML >