The Haiku/BeOS Tip Server
Tips and tricks for Haiku/BeOS users

BeMail: Building filters

Contributed by: Bill Barthelmy
This tip is valid for: Both BeOS and Haiku

Have you ever wanted to filter your BeMail into a given folder after downloading?

Although Be’s built-in Find function is powerful and can be run from the command line (see Queries from Terminal), the problem with querying BeMail is that you can’t query all the attibutes you might like (e.g., "CC:").

The following BASH script queries for my new mail and then greps it for a search word, in this case, "beusertalk." Then it moves the results to a target folder, in this example, into the "New" folder (/boot/home/mail/BeUserTalk/New/). Although it is far from being fool-proof, it more or less gets the job done.

The second problem I ran into was automating the process, so that the filter ran when I got new mail. After a number of unsatisfactory attempts, I finally decided to on the following solution.

I named this script "mailbox" and placed it in my "mail" folder, /boot/home/mail/. (You probably have a simlink in there now called "mailbox" that links to the "in" folder). Now whenever I double-click on the mailbox in the Deskbar, it runs the script instead of opening my "in" folder. To complete the process, if it moved new beusertalk mail into the New folder it opens it up and then the last line of the script opens my in box.

targetfolder=$HOME/mail/BeUserTalk/New/
searchword=beusertalk

movedmail=no


newmail=$(query
 "((MAIL:status=='*[nN][eE][wW]*')&&(BEOS:TYPE=='text/x-email'))")




for i in ${newmail// /\}; do
	# replace backlashes with spaces again
	file="${i//\/ }"

	# if file contain the searched-for word, then move it to target folder
	if grep -ic "$searchword" "$file"; then
		filename="${file##/*/}"
		if [ ! -e "$targetfolder$filename" ]; then
			mv "$file" "$targetfolder"
			movedmail=yes
		fi
	fi
done


/boot/beos/system/Tracker $HOME/mail/in


if [ "$movedmail" = yes ]; then
	/boot/beos/system/Tracker "$targetfolder"
fi

One word of warning: If this script ends up moving a bunch of files, it might take a second or two before your "in" folder actually opens. You can move this line to the beginning of the script, if you don’t mind watching your email disappear.

Posted in Applications


(comments are closed).