unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* notmuch vs gmail filters
@ 2010-04-01  8:01 Anthony Towns
  0 siblings, 0 replies; only message in thread
From: Anthony Towns @ 2010-04-01  8:01 UTC (permalink / raw)
  To: notmuch

[-- Attachment #1: Type: text/plain, Size: 1466 bytes --]

Hey *,

I'm currently using gmail for my mail and trying to port its behaviour
to notmuch. I had a go at writing a python script to convert my gmails
to notmuch tag commands along the lines of Carl's post [0]. It uses
the Gmail labs feature to export filters (to an XML file), and then
goes from XML stanzas like:

        <entry>
                <category term='filter'></category>
                <title>Mail Filter</title>
                <id>tag:mail.google.com,2008:filter:1270085835242</id>
                <updated>2010-04-01T05:33:40Z</updated>
                <content></content>
                <apps:property name='from' value='updates@linkedin.com'/>
                <apps:property name='label' value='flyers'/>
                <apps:property name='shouldArchive' value='true'/>
        </entry>

(which says to tag messages from updates@linked.com with "flyers" and
skip the inbox) into a notmuch command like:

    notmuch tag -inbox +flyers -- from:updates@linkedin.com '(' not
tag:flyers or tag:inbox ')'

The bit in brackets makes sure it doesn't tag messages that already
have exactly the right tags -- this coule probably be implied in any
invocation of notmuch tag really...

Anyway, attached. It converts gmail's list: match to a notmuch to:
match, and just gives up on deliveredto: matches. But maybe someone
else finds it useful/interesting too :)

Cheers,
aj

[0] id:87r5o8stbj.fsf@yoom.home.cworth.org

-- 
Anthony Towns <aj@erisian.com.au>

[-- Attachment #2: gmail2notmuch.py --]
[-- Type: text/x-python, Size: 1981 bytes --]

#!/usr/bin/env python

# Copyright 2010 Anthony Towns <aj@erisian.com.au>
# GPLv2+

from xml.dom import minidom
import pipes

mf = minidom.parse(open("mailFilters.xml"))

def goog2notmuch(term, value):
    if term == "list":
	if value[0]+value[-1] in ['()', '""']:
	    value = value[1:-1]
        return "to:" + value
    return "tag:omg-i-got-confused"

for e in mf.childNodes[0].getElementsByTagName("entry"):
    addtags = []
    deltags = []
    search = []
    for a in e.getElementsByTagName("apps:property"):
        n,v = a.getAttribute("name"), a.getAttribute("value")
        if n == "label":
            addtags.append(v)
        elif n == "shouldArchive" and v == "true":
            deltags.append("inbox")
        elif n == "shouldTrash" and v == "true":
            addtags.append("deleted")
        elif n == "shouldMarkAsRead" and v == "true":
            deltags.append("unread")
        elif n == "from":
            search.append("from:%s" % v)
        elif n == "subject":
            search.append("subject:%s" % v)
        elif n == "doesNotHaveTheWord" and ':' in v:
            search.append("not")
	    search.append(goog2notmuch(*v.split(':',1)))
        elif n == "hasTheWord" and ':' in v:
	    search.append(goog2notmuch(*v.split(':',1)))
        elif n == "hasTheWord":
            search.append(v)
        elif n == "doesNotHaveTheWord":
            search.append("not")
            search.append(v)
        else:
            print "XXX %s : %s" % (n,v)
    tags = ["-"+x for x in deltags] + ["+"+x for x in addtags]
    if tags == []:
	print "WTF?? NO TAGS"
	continue
    tagsearch = sum([ ["not", "tag:"+x, "or"] for x in addtags ], [])
    tagsearch += sum([ ["tag:"+x, "or"] for x in deltags ], [])
    tagsearch = tagsearch[:-1]
    if len(tags) > 1:
        tagsearch = ["("] + tagsearch + [")"]
    search += tagsearch
    print "notmuch tag %s -- %s" % (
	" ".join(pipes.quote(x) for x in tags),
	" ".join(pipes.quote(x) for x in search))


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2010-04-01  8:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-04-01  8:01 notmuch vs gmail filters Anthony Towns

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).