Thatbytes

Mostly Interesting

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
---------------
--  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)

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/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

1
2
3
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

1
2
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.

Comments