GmailMaildir.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # Maildir folder support with labels
  2. # Copyright (C) 2002-2016 John Goerzen & contributors.
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. import os
  18. from sys import exc_info
  19. import offlineimap.accounts
  20. from offlineimap import OfflineImapError
  21. from offlineimap import imaputil
  22. from .Maildir import MaildirFolder
  23. class GmailMaildirFolder(MaildirFolder):
  24. """Folder implementation to support adding labels to messages in a Maildir."""
  25. def __init__(self, root, name, sep, repository):
  26. super(GmailMaildirFolder, self).__init__(root, name, sep, repository)
  27. # The header under which labels are stored.
  28. self.labelsheader = self.repository.account.getconf('labelsheader',
  29. 'X-Keywords')
  30. # Enables / disables label sync.
  31. self.synclabels = self.repository.account.getconfboolean('synclabels', 0)
  32. # If synclabels is enabled, add a 4th pass to sync labels.
  33. if self.synclabels:
  34. self.syncmessagesto_passes.append(self.syncmessagesto_labels)
  35. def quickchanged(self, statusfolder):
  36. """Returns True if the Maildir has changed.
  37. Checks uids, flags and mtimes"""
  38. if self._utime_from_header is True:
  39. raise Exception("GmailMaildir does not support quick mode"
  40. " when 'utime_from_header' is enabled.")
  41. self.cachemessagelist()
  42. # Folder has different uids than statusfolder => TRUE.
  43. if sorted(self.getmessageuidlist()) != \
  44. sorted(statusfolder.getmessageuidlist()):
  45. return True
  46. # Check for flag changes, it's quick on a Maildir.
  47. for (uid, message) in list(self.getmessagelist().items()):
  48. if message['flags'] != statusfolder.getmessageflags(uid):
  49. return True
  50. # check for newer mtimes. it is also fast
  51. for (uid, message) in list(self.getmessagelist().items()):
  52. if message['mtime'] > statusfolder.getmessagemtime(uid):
  53. return True
  54. return False # Nope, nothing changed.
  55. # Interface from BaseFolder
  56. def msglist_item_initializer(self, uid):
  57. return {'flags': set(), 'labels': set(), 'labels_cached': False,
  58. 'filename': '/no-dir/no-such-file/', 'mtime': 0}
  59. def cachemessagelist(self, min_date=None, min_uid=None):
  60. if self.ismessagelistempty():
  61. self.messagelist = self._scanfolder(min_date=min_date,
  62. min_uid=min_uid)
  63. # Get mtimes
  64. if self.synclabels:
  65. for uid, msg in list(self.messagelist.items()):
  66. filepath = os.path.join(self.getfullname(), msg['filename'])
  67. msg['mtime'] = int(os.stat(filepath).st_mtime)
  68. def getmessagelabels(self, uid):
  69. # Labels are not cached in cachemessagelist because it is too slow.
  70. if not self.messagelist[uid]['labels_cached']:
  71. filename = self.messagelist[uid]['filename']
  72. filepath = os.path.join(self.getfullname(), filename)
  73. if not os.path.exists(filepath):
  74. return set()
  75. fd = open(filepath, 'rb')
  76. msg = self.parser['8bit'].parse(fd)
  77. fd.close()
  78. self.messagelist[uid]['labels'] = set()
  79. for hstr in self.getmessageheaderlist(msg, self.labelsheader):
  80. self.messagelist[uid]['labels'].update(
  81. imaputil.labels_from_header(self.labelsheader, hstr))
  82. self.messagelist[uid]['labels_cached'] = True
  83. return self.messagelist[uid]['labels']
  84. def getmessagemtime(self, uid):
  85. if 'mtime' not in self.messagelist[uid]:
  86. return 0
  87. else:
  88. return self.messagelist[uid]['mtime']
  89. def savemessage(self, uid, msg, flags, rtime):
  90. """Writes a new message, with the specified uid.
  91. See folder/Base for detail. Note that savemessage() does not
  92. check against dryrun settings, so you need to ensure that
  93. savemessage is never called in a dryrun mode."""
  94. if not self.synclabels:
  95. return super(GmailMaildirFolder, self).savemessage(uid, msg,
  96. flags, rtime)
  97. labels = set()
  98. for hstr in self.getmessageheaderlist(msg, self.labelsheader):
  99. labels.update(imaputil.labels_from_header(self.labelsheader, hstr))
  100. # TODO - Not sure why the returned uid is stored early as ret here?
  101. ret = super(GmailMaildirFolder, self).savemessage(uid, msg, flags,
  102. rtime)
  103. # Update the mtime and labels.
  104. filename = self.messagelist[uid]['filename']
  105. filepath = os.path.join(self.getfullname(), filename)
  106. self.messagelist[uid]['mtime'] = int(os.stat(filepath).st_mtime)
  107. self.messagelist[uid]['labels'] = labels
  108. return ret
  109. def savemessagelabels(self, uid, labels, ignorelabels=None):
  110. """Change a message's labels to `labels`.
  111. Note that this function does not check against dryrun settings,
  112. so you need to ensure that it is never called in a dryrun mode."""
  113. if ignorelabels is None:
  114. ignorelabels = set()
  115. filename = self.messagelist[uid]['filename']
  116. filepath = os.path.join(self.getfullname(), filename)
  117. fd = open(filepath, 'rb')
  118. msg = self.parser['8bit'].parse(fd)
  119. fd.close()
  120. oldlabels = set()
  121. for hstr in self.getmessageheaderlist(msg, self.labelsheader):
  122. oldlabels.update(imaputil.labels_from_header(self.labelsheader,
  123. hstr))
  124. labels = labels - ignorelabels
  125. ignoredlabels = oldlabels & ignorelabels
  126. oldlabels = oldlabels - ignorelabels
  127. # Nothing to change.
  128. if labels == oldlabels:
  129. return
  130. # Change labels into content.
  131. labels_str = imaputil.format_labels_string(self.labelsheader,
  132. sorted(labels | ignoredlabels))
  133. # First remove old labels header, and then add the new one.
  134. self.deletemessageheaders(msg, self.labelsheader)
  135. self.addmessageheader(msg, self.labelsheader, labels_str)
  136. mtime = int(os.stat(filepath).st_mtime)
  137. # Write file with new labels to a unique file in tmp.
  138. messagename = self.new_message_filename(uid, set())
  139. tmpname = self.save_to_tmp_file(messagename, msg)
  140. tmppath = os.path.join(self.getfullname(), tmpname)
  141. # Move to actual location.
  142. try:
  143. os.rename(tmppath, filepath)
  144. except OSError as e:
  145. raise OfflineImapError("Can't rename file '%s' to '%s': %s" %
  146. (tmppath, filepath, e.errno),
  147. OfflineImapError.ERROR.FOLDER,
  148. exc_info()[2])
  149. # If utime_from_header=true, we don't want to change the mtime.
  150. if self._utime_from_header and mtime:
  151. os.utime(filepath, (mtime, mtime))
  152. # save the new mtime and labels
  153. self.messagelist[uid]['mtime'] = int(os.stat(filepath).st_mtime)
  154. self.messagelist[uid]['labels'] = labels
  155. def copymessageto(self, uid, dstfolder, statusfolder, register=1):
  156. """Copies a message from self to dst if needed, updating the status
  157. Note that this function does not check against dryrun settings,
  158. so you need to ensure that it is never called in a
  159. dryrun mode.
  160. :param uid: uid of the message to be copied.
  161. :param dstfolder: A BaseFolder-derived instance
  162. :param statusfolder: A LocalStatusFolder instance
  163. :param register: whether we should register a new thread."
  164. :returns: Nothing on success, or raises an Exception."""
  165. # Check if we are really copying.
  166. realcopy = uid > 0 and not dstfolder.uidexists(uid)
  167. # First copy the message.
  168. super(GmailMaildirFolder, self).copymessageto(uid, dstfolder,
  169. statusfolder, register)
  170. # Sync labels and mtime now when the message is new (the embedded labels
  171. # are up to date, and have already propagated to the remote server. For
  172. # message which already existed on the remote, this is useless, as later
  173. # the labels may get updated.
  174. if realcopy and self.synclabels:
  175. try:
  176. labels = dstfolder.getmessagelabels(uid)
  177. statusfolder.savemessagelabels(uid, labels,
  178. mtime=self.getmessagemtime(uid))
  179. # dstfolder is not GmailMaildir.
  180. except NotImplementedError:
  181. return
  182. def syncmessagesto_labels(self, dstfolder, statusfolder):
  183. """Pass 4: Label Synchronization (Gmail only)
  184. Compare label mismatches in self with those in statusfolder. If
  185. msg has a valid UID and exists on dstfolder (has not e.g. been
  186. deleted there), sync the labels change to both dstfolder and
  187. statusfolder.
  188. Also skips messages whose mtime remains the same as statusfolder, as the
  189. contents have not changed.
  190. This function checks and protects us from action in ryrun mode.
  191. """
  192. # For each label, we store a list of uids to which it should be
  193. # added. Then, we can call addmessageslabels() to apply them in
  194. # bulk, rather than one call per message.
  195. addlabellist = {}
  196. dellabellist = {}
  197. uidlist = []
  198. try:
  199. # Filter uids (fast).
  200. for uid in self.getmessageuidlist():
  201. # Bail out on CTRL-C or SIGTERM.
  202. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  203. break
  204. # Ignore messages with negative UIDs missed by pass 1 and
  205. # don't do anything if the message has been deleted remotely.
  206. if uid < 0 or not dstfolder.uidexists(uid):
  207. continue
  208. selfmtime = self.getmessagemtime(uid)
  209. if statusfolder.uidexists(uid):
  210. statusmtime = statusfolder.getmessagemtime(uid)
  211. else:
  212. statusmtime = 0
  213. if selfmtime > statusmtime:
  214. uidlist.append(uid)
  215. self.ui.collectingdata(uidlist, self)
  216. # This can be slow if there is a lot of modified files.
  217. for uid in uidlist:
  218. # Bail out on CTRL-C or SIGTERM.
  219. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  220. break
  221. selflabels = self.getmessagelabels(uid)
  222. if statusfolder.uidexists(uid):
  223. statuslabels = statusfolder.getmessagelabels(uid)
  224. else:
  225. statuslabels = set()
  226. addlabels = selflabels - statuslabels
  227. dellabels = statuslabels - selflabels
  228. for lb in addlabels:
  229. if lb not in addlabellist:
  230. addlabellist[lb] = []
  231. addlabellist[lb].append(uid)
  232. for lb in dellabels:
  233. if lb not in dellabellist:
  234. dellabellist[lb] = []
  235. dellabellist[lb].append(uid)
  236. for lb, uids in list(addlabellist.items()):
  237. # Bail out on CTRL-C or SIGTERM.
  238. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  239. break
  240. self.ui.addinglabels(uids, lb, dstfolder)
  241. if self.repository.account.dryrun:
  242. continue # Don't actually add in a dryrun.
  243. dstfolder.addmessageslabels(uids, set([lb]))
  244. statusfolder.addmessageslabels(uids, set([lb]))
  245. for lb, uids in list(dellabellist.items()):
  246. # Bail out on CTRL-C or SIGTERM.
  247. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  248. break
  249. self.ui.deletinglabels(uids, lb, dstfolder)
  250. if self.repository.account.dryrun:
  251. continue # Don't actually remove in a dryrun.
  252. dstfolder.deletemessageslabels(uids, set([lb]))
  253. statusfolder.deletemessageslabels(uids, set([lb]))
  254. # Update mtimes on StatusFolder. It is done last to be safe. If
  255. # something els fails and the mtime is not updated, the labels will
  256. # still be synced next time.
  257. mtimes = {}
  258. for uid in uidlist:
  259. # Bail out on CTRL-C or SIGTERM.
  260. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  261. break
  262. if self.repository.account.dryrun:
  263. continue # Don't actually update statusfolder.
  264. filename = self.messagelist[uid]['filename']
  265. filepath = os.path.join(self.getfullname(), filename)
  266. mtimes[uid] = int(os.stat(filepath).st_mtime)
  267. # Finally, update statusfolder in a single DB transaction.
  268. statusfolder.savemessagesmtimebulk(mtimes)
  269. except NotImplementedError:
  270. self.ui.warn("Can't sync labels. You need to configure a remote "
  271. "repository of type Gmail.")