<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>noorul islam :: my thoughts &#187; mail</title>
	<atom:link href="http://noorul.com/blog/category/mail/feed/" rel="self" type="application/rss+xml" />
	<link>http://noorul.com/blog</link>
	<description>gnu/linux, emacs and random thoughts</description>
	<lastBuildDate>Wed, 01 Aug 2012 02:24:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.1</generator>
		<item>
		<title>Filtering IMAP mails using imapfilter</title>
		<link>http://noorul.com/blog/2010/07/09/filtering-imap-mails-using-imapfilter/</link>
		<comments>http://noorul.com/blog/2010/07/09/filtering-imap-mails-using-imapfilter/#comments</comments>
		<pubDate>Fri, 09 Jul 2010 02:57:11 +0000</pubDate>
		<dc:creator>noorul</dc:creator>
				<category><![CDATA[debian]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[mail]]></category>

		<guid isPermaLink="false">http://noorul.com/blog/?p=59</guid>
		<description><![CDATA[I use gnus to read my mails. Gnus is a very powerful emacs tool. Basically we don&#8217;t require a client side filtering system if one is provided by the server like Google Mail. But we do require one if the server is not supporting filtering mechanism. Initially I was using inbuilt filtering system from gnus. [...]]]></description>
			<content:encoded><![CDATA[<p>I use gnus to read my mails. <a href="http://gnus.org">Gnus</a> is a very powerful emacs tool. Basically we don&#8217;t require a client side filtering system if one is provided by the server like <a href="http://gmail.com">Google Mail</a>. But we do require one if the server is not supporting filtering mechanism.</p>
<p>Initially I was using inbuilt filtering system from gnus. Since I run <a href="http://www.gnu.org/software/emacs/">emacs</a> all the time on my machine and the filtering mechanism was interfering with my editing task, I thought of decoupling the same from Gnus. Google helped me to find <a href="http://imapfilter.hellug.gr/">imapfilter</a> which is a very powerful tool.</p>
<h3>Getting imapfiler</h3>
<p>You can download the software from here <a href="http://imapfilter.hellug.gr/">http://imapfilter.hellug.gr/</a>.</p>
<h3>Installing</h3>
<p>You can compile and install the program from the sources by typing:</p>
<pre>tar -zxvf imapfilter-x.y.z.tar.gz
cd imapfilter-x.y.z
make
make install</pre>
<h3>Configuring</h3>
<p>imapfilter uses lua as extension language. Here is an example ~/.imapfilter/config.lua<br />
You can configure multiple mail accounts. Since I have only one<br />
non-gmail account I have not tried that. The syntax is self explanatory.</p>
<ul>
<li> Define an account like this.</li>
<pre>    account1 = IMAP {
    server = 'your.exchage.server.com',
    username = 'username',
    password = 'reallysecret',
    ssl = 'ssl3'
}
</pre>
</ul>
<ul>
<li> Filter messages based on different header fields like this.</li>
<pre>msgs = account1.INBOX:contain_field('To', 'x-mailing-list@mail.com')
msgs = account1.INBOX:contain_field('Subject', 'Joke')
msgs = account1.INBOX:contain_field('From', 'support@bank.com')</pre>
<p>You can use complex filters like</p>
<pre>       msgs = account1.INBOX:contain_field('To', 'y-mailing-list@mail.com') +
       account1.INBOX:contain_field('Cc', 'y-mailing-list@mail.com')</pre>
</ul>
<ul>
<li> Perform an action on the filters messages</li>
<pre>account1.INBOX:move_messages(account1['x-mailing-list'], msgs)</pre>
</ul>
<ul>
<li> If we combine everything into a function.</li>
<pre>    function sortMail()
    options.timeout = 120
    options.subscribe = true                                                    

    account1 = IMAP {
        server = 'your.exchage.server.com',
        username = 'username',
        password = 'reallysecret',
        ssl = 'ssl3'
    }                                                                           

    msgs = account1.INBOX:contain_field('To', 'x-mailing-list@mail.com')
    account1.INBOX:move_messages(account1['x-mailing-list'], msgs)
    msgs = account1.INBOX:contain_field('Subject', 'Joke')
    account1.INBOX:move_messages(account1['jokes'], msgs)
    msgs = account1.INBOX:contain_field('From', 'support@bank.com')
    account1.INBOX:move_messages(account1['bank'], msgs)

    msgs = account1.INBOX:contain_field('To', 'y-mailing-list@mail.com') +
           account1.INBOX:contain_field('Cc', 'y-mailing-list@mail.com')
    account1.INBOX:move_messages(account1['y-mailing-list'], msgs)
end                                                                             

become_daemon(300, sortMail)</pre>
</ul>
<p>Now we have a simple complete imapfilter config.lua. This file should<br />
reside in ~/.imapfilter/ folder. This is the default location.</p>
<h3>Executing imapfilter</h3>
<p>Just run the following command. The option -l is used to specify log file.</p>
<pre>imapfilter -l /tmp/imapfilter.log</pre>
<h3>Trouble</h3>
<p>I had one issue with imapfilter so far. Sometimes the program terminates because of some protocol handling issue with the exchange server. So I have a script watch_imapfilter.sh added to crontab to monitor imapfilter process.</p>
<pre>#!/bin/sh                                                                       

result=`pgrep imapfilter`
if [ $result ]
then
        echo "imapfilter is already running"
else
        echo "imapfilter is not running"
        echo "Starting imapfilter...."
        /usr/local/bin/imapfilter -l /tmp/imapfilter.log
fi</pre>
<p>And the crontab entry looks like this.</p>
<pre># m h  dom mon dow   command
*/1 * * * *          /home/noorul/bin/watch_imapfilter.sh &gt;/dev/null 2&gt;&amp;1</pre>
<h3>Further reading</h3>
<p>The <a href="http://imapfilter.hellug.gr/imapfilter.1.txt">imapfilter</a> and <a  href="http://imapfilter.hellug.gr/imapfilter_config.5.txt">imapfilter_config</a> manual pages are available online.</p>
]]></content:encoded>
			<wfw:commentRss>http://noorul.com/blog/2010/07/09/filtering-imap-mails-using-imapfilter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
