Skip to main content

Ralsina.Me — Roberto Alsina's website

A KDE hack: Faster mail searches in kmail, using mairix

Kmail search­es slow­ly.

I have been a kmail us­er for a cou­ple of years, since I set­tled in my own home of­fice with my own com­put­er.

I like the thing.

How­ev­er, it an­noys me that it takes sooooo long to find a mes­sage in my mail­store. Hey, it's on­ly 13000 mes­sages!

So, while I wait for KDE4 to bring all its search­ing good­ness, I de­cid­ed to see if I could hack some­thing quick­ly.

En­ter mair­ix: a mail in­dex­er/search thing.

Con­vinc­ing mair­ix to in­dex all my mail was rather sim­ple (here is my ~/.­mair­ixr­c):

base=/home/ralsina/
maildir=Mail/*...
maildir=.kde/share/apps/kmail/dimap...
omit=Mail/mairix
database=~/.mairix_db

What does it do?

  • It in­­dex­es mail stored at ~/­­Mail and ev­ery­where in my kmail imap fold­er­s.

  • It stores search re­­sults in ~/­­Mail/­­mair­ix and ig­nores its con­­tents when search­ing. The re­­sults are stored as links, so they waste no disk space.

Af­ter run­ning mair­ix so it builds its DB (took about a min­ute, which is less than most kmail search­es) , you can search for things like this:

[ralsina@monty ~]$ time mairix b:bartleblog
Matched 6 messages

real    0m0.232s
user    0m0.012s
sys     0m0.204s

And the re­sult can be seen in kmail, in the mair­ix fold­er:

mairix1.png

How­ev­er, there is a prob­lem. It will work for the first search, but not for the sec­ond one. On the sec­ond search, you get the same con­tent list­ing, but all mes­sages ap­pear emp­ty.

That's be­cause kmail saves an in­dex file of each fold­er. To work around that, I wrote a lit­tle shell wrap­per, kmair­ix:

#!/bin/sh
rm ~/Mail/.mairix* -f
mairix $*

And you use that in­stead of call­ing mair­ix di­rect­ly.

But there are still im­prove­ments to be done. If your kmail is cur­rent­ly dis­play­ing the mair­ix fold­er, search­ing does­n't up­date the mes­sage list.

DCOP to the res­cue! We can switch to the in­box, then back to mair­ix (ad­just as need­ed for your­self):

dcop kmail KMailIface selectFolder /Local/inbox
dcop kmail KMailIface selectFolder /Local/mairix

Miss­ing pieces:

  • How about switch­ing to the kmail win­­dow? Sad­­ly, the kwin DCOP in­­ter­­face seems in­­­com­­plete. Maybe as­sign­ing kmail a hotkey and work from there? Let me know if you have any ideas.

    UP­­­DATE as sug­­gest­ed by An­no He­im­burg: just call kmail.

  • A GUI (of course!) prob­a­bly with a tray icon...

  • A way to au­­to-up­­date the Mair­ix DB when new mail ar­rives. I am think­ing about do­ing it with in­­cron but have not done it yet.

So, here is the fi­nal ver­sion, put it some­where in your path, and use AL­T+F2 to search your mails :-)

#!/bin/sh
rm ~/Mail/.mairix* -f
mairix $*
dcop kmail KMailIface selectFolder /Local/inbox
dcop kmail KMailIface selectFolder /Local/mairix
kmail
Anno v. Heimburg / 2007-05-16 21:06:

With respect to switching to the kmail window: Why don't you simply call "kmail" in the script? As it is a KUniqueApplication, the running instance will then get the focus instead of starting a new instance.

Roberto Alsina / 2007-05-16 21:11:

Brilliant. Will update it as soon as I get home.

shiny / 2007-05-17 11:02:

Creating simple kommander gui is a matter of 5 minutes of work. And making it hide in system tray is another 1 minute, thanks to kwin magic. I could make one if someone is interested, but I'm not sure its any better than a simple alt+f2.

todd / 2007-05-17 14:16:

I can understand if you don't want to use it as it's something of a resource hog, but Beagle will let you search Kmail, and it uses inotify to automatically update its db so there's no need to use cron as it automatically watches your mailboxes. I don't use KDE, but I just googled 'beagle kde' and found a couple of front-ends for it.

Well, I don't use beagle, either, I switched to Tracker, but I think it only indexes Evolution and Thunderbird mails, and not even local maildir's (yet).

Anonymous / 2007-05-17 16:47:

I get a segmentatio fault as soon as I try to create the index.

Using strace, it looks like it segfaults as soon as it finds an email with a uuencoded file.

Does anyone know why?

Anonymous / 2007-05-19 07:57:

It works great... thank you!

niko sams / 2007-05-21 08:21:

thanks for that tip! works great for me!

although i got the segfault too first, see here my bugreport:
http://bugs.gentoo.org/show...

phone number lookup / 2011-12-03 22:21:

this is really interesting viewpoint on the subject i might add


Contents © 2000-2024 Roberto Alsina