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