Imapfilter + Offilineimap + Mutt + New Script = Even Less Pain

Page content

My last article covered by original mail setup after some usage i have tweaked it even more so i have to do even less.
I have now introduced imapfilter that can move messages to the correct mail folder before i run offlineimap.

First you need to setup ~.imapfilter/config.lua

{% codeblock lang:ini %}

– Options –

options.timeout = 120 options.subscribe = true

– Accounts –

Account = IMAP { server = ‘mail.server.com’, username = “username”, password = ‘password’, ssl = ‘ssl3’,}

cron = Account.INBOX:contain_from(‘Cron Daemon’) Account.INBOX:move_messages(Account[‘INBOX/CRON’],cron)

puppet = Account.INBOX:contain_subject(‘Puppet Report’) Account.INBOX:move_messages(Account[‘INBOX/puppet’],puppet)

rancid = Account.INBOX:contain_from(‘RANCID’) Account.INBOX:move_messages(Account[‘INBOX/RANCID’],rancid)

svn = Account.INBOX:contain_subject(‘svn commit’) NOKIA.INBOX:move_messages(Account[‘INBOX/SVN’],svn)

monit = Account.INBOX:contain_subject(‘monit alert’) Account.INBOX:move_messages(Account[‘INBOX/monit’],monit) {% endcodeblock %}

Above is a snippet of the config that i use, it moves messages to the correct folders. This runs before offlineiamp you can find this config in my early blog post.

I also have a new script that no longer runs from cron to call offlineimap and imapfilter. My mail.sh is below:

#!/bin/bash
export DISPLAY=:0.0
stime="90"
ignored="All Mail|Sent Mail|Trash|geekup|uknot|Spam" 

while : ; do
echo "------------------- Running Filter ---------------------------"
imapfilter -v
echo "------------------- Syncing Account1 ----------------------------"
offlineimap -a Account1
echo "------------------- Syncing Account2 ---------------------------"
offlineimap -a Account2

#Alert me about the new mail
newmail=`find /home/sjohnson/Mail -wholename "*/new/*" | egrep -v "$ignored" | awk -F \/ '{print $5"/"$6}' | sort | uniq`

if [ -n "$newmail" ];
then
`notify-send "You have New Mail In" "$newmail" -u low -t 200 -i /usr/share/icons/Humanity/categories/48/applications-mail.svg`
fi

i="0"
while [ $i -lt $stime ]
do
if [ -e ~/Mail/check ] ; then
rm  ~/Mail/check
i=$stime
fi
echo "------------------- Sleeping for $i of $stime --------------------------"
i=$[$i+1]
sleep 1
done
done

The script sets up an endless loop and runs imapfilter then offlineimap for both my accounts, then enters another loop which runs for 90 and then runs the same command again. If ~/Mail/check is present it ends the timed loop and runs the command again, i use this if im waiting on a email and need to call a sync asap. I have added a entry to my .muttrc to allow me to create the file easily

macro generic "\ci" "unset wait_keytouch ~/Mail/checkset wait_key" 'Syncing'
macro index   "\ci" "unset wait_keytouch ~/Mail/checkset wait_key" 'Syncing'
macro pager   "\ci" "unset wait_keytouch ~/Mail/checkset wait_key" 'Syncing'
This basically means when i hit crtl i it creates the file that forces my mail.sh to sync again. In order to run mail.sh i have created a job that runs when i login using the gnome tool

screen -d -m -s ~/utils/mail.sh
This creates a detached screen session so i can connect to it once in a while a see what its doing and kill it easily just i case i need to.