2 """This is a notmuch implementation in python. It's goal is to allow running the test suite on the cnotmuch python bindings.
4 This "binary" honors the NOTMUCH_CONFIG environmen variable for reading a user's
5 notmuch configuration (e.g. the database path)
7 This code is licensed under the GNU GPL v3+."""
8 from __future__ import with_statement # This isn't required in Python 2.6
9 import sys, os, re, logging
10 from subprocess import call
11 from cnotmuch.notmuch import Database, Query
12 PREFIX=re.compile('(\w+):(.*$)')
13 #TODO Handle variable: NOTMUCH-CONFIG
15 #-------------------------------------------------------------------------
16 def get_user_email_addresses():
17 """ Reads a user's notmuch config and returns his email addresses as list (name, primary_address, other_address1,...)"""
19 from ConfigParser import SafeConfigParser
20 config = SafeConfigParser()
21 conf_f = os.getenv('NOTMUCH_CONFIG',
22 os.path.expanduser('~/.notmuch-config'))
24 if not config.has_option('user','name'): name = ""
25 else:name = config.get('user','name')
27 if not config.has_option('user','primary_email'): mail = ""
28 else:mail = config.get('user','primary_email')
30 if not config.has_option('user','other_email'): other = []
31 else:other = config.get('user','other_email').rstrip(';').split(';')
36 #-------------------------------------------------------------------------
37 HELPTEXT="""The notmuch mail system.
39 Usage: notmuch <command> [args...]
41 Where <command> and [args...] are as follows:
43 setup Interactively setup notmuch for first use.
47 Find and import new messages to the notmuch database.
49 search [options...] <search-terms> [...]
51 Search for messages matching the given search terms.
53 show <search-terms> [...]
55 Show all messages matching the search terms.
57 count <search-terms> [...]
59 Count messages matching the search terms.
61 reply [options...] <search-terms> [...]
63 Construct a reply template for a set of messages.
65 tag +<tag>|-<tag> [...] [--] <search-terms> [...]
67 Add/remove tags for all messages matching the search terms.
71 Create a plain-text dump of the tags for each message.
75 Restore the tags from the given dump file (see 'dump').
77 search-tags [<search-terms> [...] ]
79 List all tags found in the database or matching messages.
83 This message, or more detailed help for the named command.
85 Use "notmuch help <command>" for more details on each command.
86 And "notmuch help search-terms" for the common search-terms syntax.
88 #-------------------------------------------------------------------------
89 #TODO: replace the dynamic pieces
90 USAGE="""Notmuch is configured and appears to have a database. Excellent!
92 At this point you can start exploring the functionality of notmuch by
93 using commands such as:
95 notmuch search tag:inbox
97 notmuch search to:"Sebastian Spaeth"
99 notmuch search from:"Sebastian@SSpaeth.de"
101 notmuch search subject:"my favorite things"
103 See "notmuch help search" for more details.
105 You can also use "notmuch show" with any of the thread IDs resulting
106 from a search. Finally, you may want to explore using a more sophisticated
107 interface to notmuch such as the emacs interface implemented in notmuch.el
108 or any other interface described at http://notmuchmail.org
110 And don't forget to run "notmuch new" whenever new mail arrives.
112 Have fun, and may your inbox never have much mail.
114 #-------------------------------------------------------------------------
115 def quote_reply(oldbody ,date, from_address):
116 """Transform a mail body into a quote text starting with On blah, x wrote:
117 :param body: a str with a mail body
118 :returns: The new payload of the email.message()
120 from cStringIO import StringIO
122 #we get handed a string, wrap it in a file-like object
123 oldbody = StringIO(oldbody)
126 newbody.write("On %s, %s wrote:\n" % (date, from_address))
129 newbody.write("> " + line)
131 return newbody.getvalue()
133 def format_reply(msgs):
134 """Gets handed Messages() and displays the reply to them"""
138 f = open(msg.get_filename(),"r")
139 reply = email.message_from_file(f)
141 #handle the easy non-multipart case:
142 if not reply.is_multipart():
143 reply.set_payload(quote_reply(reply.get_payload(),
144 reply['date'],reply['from']))
146 #handle the tricky multipart case
148 """A string describing which nontext attachements have been deleted"""
150 """A list of payload indices to be deleted"""
152 payloads = reply.get_payload()
154 for i, part in enumerate(payloads):
156 mime_main = part.get_content_maintype()
157 if mime_main not in ['multipart', 'message', 'text']:
158 deleted += "Non-text part: %s\n" % (part.get_content_type())
159 payloads[i].set_payload("Non-text part: %s" % (part.get_content_type()))
160 payloads[i].set_type('text/plain')
161 delpayloads.append(i)
163 # payloads[i].set_payload("Text part: %s" % (part.get_content_type()))
164 payloads[i].set_payload(quote_reply(payloads[i].get_payload(),reply['date'],reply['from']))
167 # Delete those payloads that we don't need anymore
168 for i in reversed(sorted(delpayloads)):
171 #Back to single- and multipart handling
173 my_addresses = get_user_email_addresses()
175 # filter our email addresses from all to: cc: and bcc: fields
176 # if we find one of "my" addresses being used,
177 # it is stored in used_address
178 for header in ['To', 'CC', 'Bcc']:
179 if not header in reply:
180 #only handle fields that exist
182 addresses = email.utils.getaddresses(reply.get_all(header,[]))
184 for name, mail in addresses:
185 if mail in my_addresses[1:]:
186 used_address = email.utils.formataddr((my_addresses[0],mail))
188 purged_addr.append(email.utils.formataddr((name,mail)))
191 reply.replace_header(header, ", ".join(purged_addr))
193 #we deleted all addresses, delete the header
196 # Use our primary email address to the From
197 # (save original from line, we still need it)
198 orig_from = reply['From']
200 reply['From'] = used_address if used_address \
201 else email.utils.formataddr((my_addresses[0],my_addresses[1]))
203 #reinsert the Subject after the From
204 orig_subject = reply['Subject']
206 reply['Subject'] = 'Re: ' + orig_subject
208 # Calculate our new To: field
210 # add all remaining original 'To' addresses
212 new_to += ", " + reply['To']
214 reply.add_header('To', new_to)
216 # Add our primary email address to the BCC
217 new_bcc = my_addresses[1]
218 if reply.has_key('Bcc'):
219 new_bcc += ', ' + reply['Bcc']
221 reply['Bcc'] = new_bcc
223 # Set replies 'In-Reply-To' header to original's Message-ID
224 if reply.has_key('Message-ID') :
225 del reply['In-Reply-To']
226 reply['In-Reply-To'] = reply['Message-ID']
228 #Add original's Message-ID to replies 'References' header.
229 if reply.has_key('References'):
230 ref = reply['References'] + ' ' +reply['Message-ID']
232 ref = reply['Message-ID']
233 del reply['References']
234 reply['References'] = ref
236 # Delete the original Message-ID.
237 del(reply['Message-ID'])
239 # filter all existing headers but a few and delete them from 'reply'
240 delheaders = filter(lambda x: x not in ['From','To','Subject','CC','Bcc',
241 'In-Reply-To', 'References',
242 'Content-Type'],reply.keys())
243 map(reply.__delitem__, delheaders)
246 From: Sebastian Spaeth <Sebastian@SSpaeth.de>
247 Subject: Re: Template =?iso-8859-1?b?Zvxy?= das Kochrezept
248 In-Reply-To: <4A6D55F9.6040405@SSpaeth.de>
249 References: <4A6D55F9.6040405@SSpaeth.de>
251 #return without Unixfrom
252 return reply.as_string(False)
253 #-------------------------------------------------------------------------
254 def quote_query_line(argv):
255 #mangle arguments wrapping terms with spaces in quotes
256 for i in xrange(0,len(argv)):
257 if argv[i].find(' ') >= 0:
258 #if we use prefix:termWithSpaces, put quotes around term
259 m = PREFIX.match(argv[i])
261 argv[i] = '%s:"%s"' % (m.group(1), m.group(2))
263 argv[i] = '"'+argv[i]+'"'
264 return ' '.join(argv)
266 if __name__ == '__main__':
268 # Handle command line options
269 #-------------------------------------
270 # No option given, print USAGE and exit
271 if len(sys.argv) == 1:
273 #-------------------------------------
274 elif sys.argv[1] == 'setup':
275 """Interactively setup notmuch for first use."""
276 print "Not implemented."
277 #-------------------------------------
278 elif sys.argv[1] == 'new':
279 """ Interactively setup notmuch for first use. """
280 #print "Not implemented. We cheat by calling the proper notmuch"
281 call(['notmuch new'],shell=True)
282 #-------------------------------------
283 elif sys.argv[1] == 'help':
284 if len(sys.argv) == 2: print HELPTEXT
285 else: print "Not implemented"
286 #-------------------------------------
287 elif sys.argv[1] == 'search':
289 if len(sys.argv) == 2:
290 #no further search term
293 #mangle arguments wrapping terms with spaces in quotes
294 querystr = quote_query_line(sys.argv[2:])
295 logging.debug("search "+querystr)
296 t = Query(db,querystr).search_threads()
299 #-------------------------------------
300 elif sys.argv[1] == 'show':
302 if len(sys.argv) == 2:
303 #no further search term
306 #mangle arguments wrapping terms with spaces in quotes
307 querystr = quote_query_line(sys.argv[2:])
308 logging.debug("show "+querystr)
309 m = Query(db,querystr).search_messages()
311 print(msg.format_as_text())
313 #-------------------------------------
314 elif sys.argv[1] == 'reply':
316 if len(sys.argv) == 2:
317 #no search term. abort
318 print("Error: notmuch reply requires at least one search term.")
321 #mangle arguments wrapping terms with spaces in quotes
322 querystr = quote_query_line(sys.argv[2:])
323 logging.debug("reply "+querystr)
324 msgs = Query(db,querystr).search_messages()
325 print (format_reply(msgs))
327 #-------------------------------------
328 elif sys.argv[1] == 'count':
329 if len(sys.argv) == 2:
330 #no further search term, count all
333 #mangle arguments wrapping terms with spaces in quotes
334 querystr = quote_query_line(sys.argv[2:])
335 print(Database().create_query(querystr).count_messages())
337 #-------------------------------------
338 elif sys.argv[1] == 'tag':
339 #build lists of tags to be added and removed
341 while not sys.argv[2]=='--' and \
342 (sys.argv[2].startswith('+') or sys.argv[2].startswith('-')):
343 if sys.argv[2].startswith('+'):
344 #append to add list without initial +
345 add.append(sys.argv.pop(2)[1:])
347 #append to remove list without initial -
348 remove.append(sys.argv.pop(2)[1:])
350 if sys.argv[2]=='--': sys.argv.pop(2)
351 #the rest is search terms
352 querystr = quote_query_line(sys.argv[2:])
353 logging.debug("tag search-term "+querystr)
354 db = Database(mode=Database.MODE.READ_WRITE)
355 m = Query(db,querystr).search_messages()
357 #actually add and remove all tags
358 map(msg.add_tag, add)
359 map(msg.remove_tag, remove)
360 #-------------------------------------
361 elif sys.argv[1] == 'search-tags':
362 if len(sys.argv) == 2:
363 #no further search term
364 print("\n".join(Database().get_all_tags()))
366 #mangle arguments wrapping terms with spaces in quotes
367 querystr = quote_query_line(sys.argv[2:])
368 logging.debug("search-term "+querystr)
370 m = Query(db,querystr).search_messages()
371 print("\n".join([t for t in m.collect_tags()]))
372 #-------------------------------------
373 elif sys.argv[1] == 'dump':
374 #TODO: implement "dump <filename>"
375 if len(sys.argv) == 2:
378 f = open(sys.argv[2],"w")
381 q.set_sort(Query.SORT.MESSAGE_ID)
382 m = q.search_messages()
384 f.write("%s (%s)\n" % (msg.get_message_id(), msg.get_tags()))
385 #-------------------------------------
386 elif sys.argv[1] == 'restore':
388 if len(sys.argv) == 2:
389 print("No filename given. Reading dump from stdin.")
392 f = open(sys.argv[2],"r")
393 #split the msg id and the tags
394 MSGID_TAGS = re.compile("(\S+)\s\((.*)\)$")
395 db = Database(mode=Database.MODE.READ_WRITE)
397 #read each line of the dump file
399 m = MSGID_TAGS.match(line)
401 sys.stderr.write("Warning: Ignoring invalid input line: %s" %
404 # split line in components and fetch message
406 new_tags= set(m.group(2).split())
407 msg = db.find_message(msg_id)
411 "Warning: Cannot apply tags to missing message: %s\n" % id)
414 #do nothing if the old set of tags is the same as the new one
415 old_tags = set(msg.get_tags())
416 if old_tags == new_tags: continue
420 #only remove tags if the new ones are not a superset anyway
421 if not (new_tags > old_tags): msg.remove_all_tags()
422 for tag in new_tags: msg.add_tag(tag)
425 #-------------------------------------
428 print "Error: Unknown command '%s' (see \"notmuch help\")" % sys.argv[1]
435 show <search-terms> [...]
436 reply [options...] <search-terms> [...]