procmail
[Top] [All Lists]

Scanning IMAP mail (first solution)

2009-11-16 04:14:04
OK, I have a working solution now. It's kludgy, and slow, and I wouldn't 
recommend using it, but it is working for me (for certain values of working). 
The heart of it is this bit of procmail that is embedded in the portion of my 
recipes where list mail gets delivered to the IMAP store.

:0
* ! LISTNAME ?? ^^^^
{
   NEWMAIL = "`/usr/local/bin/newimapscan`"
   USER = `/usr/bin/id -un`
   UNREAD = `/usr/bin/tail -1 <<< "$NEWMAIL"`
   MYAD = "syth(_at_)mac(_dot_)com, kremels(_at_)kreme(_dot_)com"

   :0c
   |(echo "Subject: $UNREAD unread mail(s)"; echo; echo "$NEWMAIL")| $SENDMAIL 
$MYAD

   :0
   .$LISTNAME.$MYDATE/
}



The script is quite simple, but I use bash syntax instead of pure /bin/sh 

<<EOF
#!/bin/bash

UNAME=`id -un`
DATE=`date +'%F.%T'`

rm /tmp/$UNAME.* 2>/dev/null

for i in `ls -A $HOME/Maildir/ | grep '[0-9]-*'`;
 do ls −1 $HOME/Maildir/$i/{cur,new}/*, 2>/dev/null >>/tmp/$UNAME.$DATE.$i;
 if [ -s /tmp/$UNAME.$DATE.$i ]; then
   echo -n;
 else
   rm /tmp/$UNAME.$DATE.$i;
 fi
done

wc -l /tmp/$UNAME.$DATE.* 2>/dev/null | sed "s/\/tmp\/$UNAME.$DATE\.//" 

EOF

One note that might be confusing otherwise, I set my list mailboxes to, for 
example, .procmail.2009-11, then when messages are a month old or more, I move 
them into a folder named .procmail.2009, so the final grep on the for line it 
designed to prevent the for loop from traversing into the very large 
year-folders and only process folders that, in my case, are fairly current 
(i.e., could contain new messages).

The "echo -n" was just an empty line place-holder until I got around to 
figuring out bash's if NOT syntax for -s and I never got around to looking it 
up.

It works, however every version of this I've tried sends the file as a single 
line instead of preserving the formatting. If it matters, this is postfix's 
sendmail stub, and not real sendmail.

My only real issue with the solution as it stands now is that it is writing a 
NEW mail every time a message comes in, which would be fine if I could figure 
out how to write it safely and correctly directly into the INBOX of the account 
(and delete the old one). As it is, sending many messages a minute at times to 
my push-enabled me.com account is going to get annoying, so I may just set it 
on a 10 minute cron.

Also, it's just too slow.

For finding 29 messages unread:
real    0m1.426s
user    0m0.323s
sys     0m0.552s


-- 
The trouble with witches is that they'll never run away from things they really 
hate.
And the trouble with small furry animals in a corner is that, just 
occasionally, one of them's a mongoose. --Witches Abroad


____________________________________________________________
procmail mailing list   Procmail homepage: http://www.procmail.org/
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)de
http://mailman.rwth-aachen.de/mailman/listinfo/procmail