summaryrefslogtreecommitdiffstats
path: root/mutt-notmuch.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2014-08-17 20:27:44 -0400
committerThibaut Horel <thibaut.horel@gmail.com>2014-08-17 20:27:44 -0400
commitcb2e7c09daefafad97618763f43c991baf8e9365 (patch)
tree339bec874ce6b0b6efec33943b93325db63de153 /mutt-notmuch.py
downloadmutt-helpers-cb2e7c09daefafad97618763f43c991baf8e9365.tar.gz
Initial commit
Diffstat (limited to 'mutt-notmuch.py')
-rwxr-xr-xmutt-notmuch.py66
1 files changed, 66 insertions, 0 deletions
diff --git a/mutt-notmuch.py b/mutt-notmuch.py
new file mode 100755
index 0000000..fbfbd19
--- /dev/null
+++ b/mutt-notmuch.py
@@ -0,0 +1,66 @@
+#! /usr/bin/python2
+import sys
+import email
+import readline
+from mailbox import Maildir
+import os
+from os.path import basename, join, expanduser
+from ConfigParser import RawConfigParser, NoSectionError, NoOptionError
+
+from notmuch import Database, Query
+
+CONFIG_FILE = expanduser(os.getenv("NOTMUCH_CONFIG") or "~/.notmuch-config")
+HIST_FILE = expanduser(os.getenv("MUTT_NM_HIST") or "~/.mutt-notmuch-history")
+
+
+def make_query(query):
+ parser = RawConfigParser()
+ parser.read(CONFIG_FILE)
+ try:
+ tags = parser.get("search", "exclude_tags").split(";")
+ except (NoSectionError, NoOptionError):
+ tags = []
+ q = Database().create_query(query)
+ map(q.exclude_tag, tags)
+ return q
+
+
+def search(query):
+ q = make_query(query)
+ q.set_sort(Query.SORT.NEWEST_FIRST)
+ for message in q.search_messages():
+ yield message.get_filename()
+
+
+def thread(id):
+ q = make_query("id:" + id)
+ for message in q.search_messages():
+ q2 = make_query("thread:" + message.get_thread_id())
+ for m in q2.search_messages():
+ yield m.get_filename()
+
+
+def process(gen, output):
+ box = Maildir(output)
+ box.clear()
+ for filename in gen:
+ os.symlink(filename, join(box._path, "cur", basename(filename)))
+
+
+def main():
+ output = sys.argv[1]
+ if sys.argv[2] == "search":
+ try:
+ readline.read_history_file(HIST_FILE)
+ except IOError:
+ open(HIST_FILE, "a").close()
+ query = raw_input("Query: ")
+ readline.write_history_file(HIST_FILE)
+ process(search(query), output)
+ elif sys.argv[2] == "thread":
+ message = email.message_from_file(sys.stdin)
+ process(thread(message["message-id"][1:-1]), output)
+
+
+if __name__ == "__main__":
+ main()