Base.py 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. """
  2. Base folder support
  3. Copyright (C) 2002-2016 John Goerzen & contributors
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. """
  16. import email
  17. import os.path
  18. import re
  19. import time
  20. from sys import exc_info
  21. from email import policy
  22. from email.parser import BytesParser
  23. from email.utils import parsedate_tz, mktime_tz
  24. from email.charset import Charset
  25. from offlineimap import threadutil
  26. from offlineimap.ui import getglobalui
  27. from offlineimap.error import OfflineImapError
  28. import offlineimap.accounts
  29. # This is wrapper to workaround for:
  30. # - https://bugs.python.org/issue32330
  31. class EmailMessage(email.message.EmailMessage):
  32. def set_payload(self, payload, charset=None):
  33. if hasattr(payload, 'encode') and charset is not None:
  34. if not isinstance(charset, Charset):
  35. charset = Charset(charset)
  36. payload = payload.encode(charset.output_charset, 'replace')
  37. charset = None
  38. super().set_payload(payload, charset)
  39. class BaseFolder:
  40. """
  41. Base Folder Class
  42. """
  43. __hash__ = None
  44. def __init__(self, name, repository):
  45. """
  46. :param name: Path & name of folder minus root or reference
  47. :param repository: Repository() in which the folder is.
  48. """
  49. self.ui = getglobalui()
  50. self.messagelist = {}
  51. # Use the built-in email libraries
  52. # Establish some policies
  53. default_policy = policy.default.clone(message_factory=EmailMessage)
  54. self.policy = {
  55. '7bit':
  56. default_policy.clone(cte_type='7bit', utf8=False, refold_source='none'),
  57. '7bit-RFC':
  58. default_policy.clone(cte_type='7bit', utf8=False, refold_source='none', linesep='\r\n'),
  59. '8bit':
  60. default_policy.clone(cte_type='8bit', utf8=True, refold_source='none'),
  61. '8bit-RFC':
  62. default_policy.clone(cte_type='8bit', utf8=True, refold_source='none', linesep='\r\n'),
  63. }
  64. # Parsers
  65. self.parser = {}
  66. for key in self.policy:
  67. self.parser[key] = BytesParser(policy=self.policy[key])
  68. # Save original name for folderfilter operations.
  69. self.ffilter_name = name
  70. # Top level dir name is always ''.
  71. self.root = None
  72. self.name = name if not name == self.getsep() else ''
  73. self.newmail_hook = None
  74. # Only set the newmail_hook if the IMAP folder is named 'INBOX'.
  75. if self.name == 'INBOX':
  76. self.newmail_hook = repository.newmail_hook
  77. self.have_newmail = False
  78. self.copy_ignoreUIDs = None # List of UIDs to ignore.
  79. self.repository = repository
  80. self.visiblename = repository.nametrans(name)
  81. # In case the visiblename becomes '.' or '/' (top-level) we use
  82. # '' as that is the name that e.g. the Maildir scanning will
  83. # return for the top-level dir.
  84. if self.visiblename == self.getsep():
  85. self.visiblename = ''
  86. self.repoconfname = "Repository " + repository.name
  87. self.config = repository.getconfig()
  88. # Do we need to use mail timestamp for filename prefix?
  89. filename_use_mail_timestamp_global = self.config.getdefaultboolean(
  90. "general", "filename_use_mail_timestamp", False)
  91. self._filename_use_mail_timestamp = self.config.getdefaultboolean(
  92. self.repoconfname,
  93. "filename_use_mail_timestamp",
  94. filename_use_mail_timestamp_global)
  95. self._sync_deletes = self.config.getdefaultboolean(
  96. self.repoconfname, "sync_deletes", True)
  97. self._dofsync = self.config.getdefaultboolean("general", "fsync", True)
  98. # Determine if we're running static or dynamic folder filtering
  99. # and check filtering status.
  100. self._dynamic_folderfilter = self.config.getdefaultboolean(
  101. self.repoconfname, "dynamic_folderfilter", False)
  102. self._sync_this = repository.should_sync_folder(self.ffilter_name)
  103. if self._dynamic_folderfilter:
  104. self.ui.debug('', "Running dynamic folder filtering on '%s'[%s]" %
  105. (self.ffilter_name, repository))
  106. elif not self._sync_this:
  107. self.ui.debug('', "Filtering out '%s'[%s] due to folderfilter" %
  108. (self.ffilter_name, repository))
  109. # Passes for syncmessagesto.
  110. self.syncmessagesto_passes = [
  111. self.__syncmessagesto_copy,
  112. self.__syncmessagesto_delete,
  113. self.__syncmessagesto_flags,
  114. ]
  115. def getname(self):
  116. """Returns name"""
  117. return self.name
  118. def __str__(self):
  119. # FIMXE: remove calls of this. We have getname().
  120. return self.name
  121. def __unicode__(self):
  122. # NOTE(sheeprine): Implicit call to this by UIBase deletingflags() which
  123. # fails if the str is utf-8
  124. return self.name.decode('utf-8')
  125. def __enter__(self):
  126. """Starts a transaction. This will postpone (guaranteed) saving to disk
  127. of all messages saved inside this transaction until its committed."""
  128. pass
  129. def __exit__(self, exc_type, exc_val, exc_tb):
  130. """Commits a transaction, all messages saved inside this transaction
  131. will only now be persisted to disk."""
  132. pass
  133. @property
  134. def accountname(self):
  135. """Account name as string"""
  136. return self.repository.accountname
  137. @property
  138. def sync_this(self):
  139. """Should this folder be synced or is it e.g. filtered out?"""
  140. if not self._dynamic_folderfilter:
  141. return self._sync_this
  142. else:
  143. return self.repository.should_sync_folder(self.ffilter_name)
  144. def dofsync(self):
  145. """
  146. Call and returns _dofsync()
  147. Returns: Call and returns _dofsync()
  148. """
  149. return self._dofsync
  150. def suggeststhreads(self):
  151. """Returns True if this folder suggests using threads for actions.
  152. Only IMAP returns True. This method must honor any CLI or configuration
  153. option."""
  154. return False
  155. def waitforthread(self):
  156. """Implements method that waits for thread to be usable.
  157. Should be implemented only for folders that suggest threads."""
  158. raise NotImplementedError
  159. def quickchanged(self, statusfolder):
  160. """ Runs quick check for folder changes and returns changed
  161. status: True -- changed, False -- not changed.
  162. :param statusfolder: keeps track of the last known folder state.
  163. """
  164. return True
  165. def getinstancelimitnamespace(self):
  166. """For threading folders, returns the instancelimitname for
  167. InstanceLimitedThreads."""
  168. raise NotImplementedError
  169. def storesmessages(self):
  170. """Should be true for any backend that actually saves message bodies.
  171. (Almost all of them). False for the LocalStatus backend. Saves
  172. us from having to slurp up messages just for localstatus purposes."""
  173. return 1
  174. def getvisiblename(self):
  175. """The nametrans-transposed name of the folder's name."""
  176. return self.visiblename
  177. def getexplainedname(self):
  178. """Name that shows both real and nametrans-mangled values."""
  179. if self.name == self.visiblename:
  180. return self.name
  181. else:
  182. return "%s [remote name %s]" % (self.visiblename, self.name)
  183. def getrepository(self):
  184. """Returns the repository object that this folder is within."""
  185. return self.repository
  186. def getroot(self):
  187. """Returns the root of the folder, in a folder-specific fashion."""
  188. return self.root
  189. def getsep(self):
  190. """Returns the separator for this folder type."""
  191. return self.sep
  192. def getfullname(self):
  193. """
  194. Returns the folder full name, using the getname(). If getroot() is set
  195. their value is concatenated to getname() using the separator
  196. Returns: The folder full name
  197. """
  198. if self.getroot():
  199. return self.getroot() + self.getsep() + self.getname()
  200. else:
  201. return self.getname()
  202. def getfolderbasename(self):
  203. """Return base file name of file to store Status/UID info in."""
  204. if not self.name:
  205. basename = '.'
  206. else: # Avoid directory hierarchies and file names such as '/'.
  207. basename = self.name.replace('/', '.')
  208. # Replace with literal 'dot' if final path name is '.' as '.' is
  209. # an invalid file name.
  210. basename = re.sub(r'(^|\/)\.$', '\\1dot', basename)
  211. return basename
  212. def check_uidvalidity(self):
  213. """Tests if the cached UIDVALIDITY match the real current one
  214. If required it saves the UIDVALIDITY value. In this case the
  215. function is not threadsafe. So don't attempt to call it from
  216. concurrent threads.
  217. :returns: Boolean indicating the match. Returns True in case it
  218. implicitely saved the UIDVALIDITY."""
  219. if self.get_saveduidvalidity() is not None:
  220. return self.get_saveduidvalidity() == self.get_uidvalidity()
  221. else:
  222. self.save_uidvalidity()
  223. return True
  224. def _getuidfilename(self):
  225. """provides UIDVALIDITY cache filename for class internal purposes."""
  226. return os.path.join(self.repository.getuiddir(),
  227. self.getfolderbasename())
  228. def get_saveduidvalidity(self):
  229. """Return the previously cached UIDVALIDITY value
  230. :returns: UIDVALIDITY as (long) number or None, if None had been
  231. saved yet."""
  232. if hasattr(self, '_base_saved_uidvalidity'):
  233. return self._base_saved_uidvalidity
  234. uidfilename = self._getuidfilename()
  235. if not os.path.exists(uidfilename):
  236. self._base_saved_uidvalidity = None
  237. else:
  238. file = open(uidfilename, "rt")
  239. self._base_saved_uidvalidity = int(file.readline().strip())
  240. file.close()
  241. return self._base_saved_uidvalidity
  242. def save_uidvalidity(self):
  243. """Save the UIDVALIDITY value of the folder to the cache
  244. This function is not threadsafe, so don't attempt to call it
  245. from concurrent threads."""
  246. newval = self.get_uidvalidity()
  247. uidfilename = self._getuidfilename()
  248. with open(uidfilename + ".tmp", "wt") as uidfile:
  249. uidfile.write("%d\n" % newval)
  250. # This is weird, os.rename on Windows raises an exception,
  251. # But not in Linux. In linux the file is overwritten.
  252. try:
  253. os.rename(uidfilename + ".tmp", uidfilename)
  254. except WindowsError:
  255. os.remove(uidfilename)
  256. os.rename(uidfilename + ".tmp", uidfilename)
  257. self._base_saved_uidvalidity = newval
  258. def get_uidvalidity(self):
  259. """Retrieve the current connections UIDVALIDITY value
  260. This function needs to be implemented by each Backend
  261. :returns: UIDVALIDITY as a (long) number."""
  262. raise NotImplementedError
  263. def cachemessagelist(self):
  264. """Cache the list of messages.
  265. Reads the message list from disk or network and stores it in memory for
  266. later use. This list will not be re-read from disk or memory unless
  267. this function is called again."""
  268. raise NotImplementedError
  269. def ismessagelistempty(self):
  270. """Is the list of messages empty."""
  271. if len(list(self.messagelist.keys())) < 1:
  272. return True
  273. return False
  274. def dropmessagelistcache(self):
  275. """Empty everythings we know about messages."""
  276. self.messagelist = {}
  277. def getmessagelist(self):
  278. """Gets the current message list.
  279. You must call cachemessagelist() before calling this function!"""
  280. return self.messagelist
  281. def msglist_item_initializer(self, uid):
  282. """Returns value for empty messagelist element with given UID.
  283. This function must initialize all fields of messagelist item
  284. and must be called every time when one creates new messagelist
  285. entry to ensure that all fields that must be present are present.
  286. :param uid: Message UID"""
  287. raise NotImplementedError
  288. def uidexists(self, uid):
  289. """Returns True if uid exists."""
  290. return uid in self.getmessagelist()
  291. def getmessageuidlist(self):
  292. """Gets a list of UIDs.
  293. You may have to call cachemessagelist() before calling this function!"""
  294. return sorted(self.getmessagelist().keys())
  295. def getmessagecount(self):
  296. """Gets the number of messages."""
  297. return len(self.getmessagelist())
  298. def getmessage(self, uid):
  299. """Returns an email message object."""
  300. raise NotImplementedError
  301. def getmaxage(self):
  302. """Return maxage.
  303. maxage is allowed to be either an integer or a date of the form
  304. YYYY-mm-dd. This returns a time_struct."""
  305. maxagestr = self.config.getdefault("Account %s" %
  306. self.accountname, "maxage", None)
  307. if maxagestr is None:
  308. return None
  309. # Is it a number?
  310. try:
  311. maxage = int(maxagestr)
  312. if maxage < 1:
  313. raise OfflineImapError("invalid maxage value %d" % maxage,
  314. OfflineImapError.ERROR.MESSAGE)
  315. return time.gmtime(time.time() - 60 * 60 * 24 * maxage)
  316. except ValueError:
  317. pass # Maybe it was a date.
  318. # Is it a date string?
  319. try:
  320. date = time.strptime(maxagestr, "%Y-%m-%d")
  321. if date[0] < 1900:
  322. raise OfflineImapError("maxage led to year %d. "
  323. "Abort syncing." % date[0],
  324. OfflineImapError.ERROR.MESSAGE)
  325. if (time.mktime(date) - time.mktime(time.localtime())) > 0:
  326. raise OfflineImapError("maxage led to future date %s. "
  327. "Abort syncing." % maxagestr,
  328. OfflineImapError.ERROR.MESSAGE)
  329. return date
  330. except ValueError:
  331. raise OfflineImapError("invalid maxage value %s" % maxagestr,
  332. OfflineImapError.ERROR.MESSAGE)
  333. def getmaxsize(self):
  334. """
  335. Get the maxsize for account name. If not found, returns None.
  336. Returns: A string with the maxise of the account name
  337. """
  338. return self.config.getdefaultint("Account %s" %
  339. self.accountname, "maxsize", None)
  340. def getstartdate(self):
  341. """ Retrieve the value of the configuration option startdate """
  342. datestr = self.config.getdefault("Repository " + self.repository.name,
  343. 'startdate', None)
  344. try:
  345. if not datestr:
  346. return None
  347. date = time.strptime(datestr, "%Y-%m-%d")
  348. if date[0] < 1900:
  349. raise OfflineImapError("startdate led to year %d. "
  350. "Abort syncing." % date[0],
  351. OfflineImapError.ERROR.MESSAGE)
  352. if (time.mktime(date) - time.mktime(time.localtime())) > 0:
  353. raise OfflineImapError("startdate led to future date %s. "
  354. "Abort syncing." % datestr,
  355. OfflineImapError.ERROR.MESSAGE)
  356. return date
  357. except ValueError:
  358. raise OfflineImapError("invalid startdate value %s",
  359. OfflineImapError.ERROR.MESSAGE)
  360. def get_min_uid_file(self):
  361. """
  362. Get the min UID file name. Create it if not found.
  363. Returns: Min UID file name.
  364. """
  365. startuiddir = os.path.join(self.config.getmetadatadir(),
  366. 'Repository-' + self.repository.name,
  367. 'StartUID')
  368. if not os.path.exists(startuiddir):
  369. os.mkdir(startuiddir, 0o700)
  370. return os.path.join(startuiddir, self.getfolderbasename())
  371. def save_min_uid(self, min_uid):
  372. """
  373. Save the min UID in the min uid file
  374. Args:
  375. min_uid: min_uid to save
  376. Returns: None
  377. """
  378. uidfile = self.get_min_uid_file()
  379. fd = open(uidfile, 'wt')
  380. fd.write(str(min_uid) + "\n")
  381. fd.close()
  382. def retrieve_min_uid(self):
  383. """
  384. Retrieve the min UID file
  385. Returns: min UID of file
  386. """
  387. uidfile = self.get_min_uid_file()
  388. if not os.path.exists(uidfile):
  389. return None
  390. try:
  391. fd = open(uidfile, 'rt')
  392. min_uid = int(fd.readline().strip())
  393. fd.close()
  394. return min_uid
  395. except:
  396. raise IOError("Can't read %s" % uidfile)
  397. def savemessage(self, uid, msg, flags, rtime):
  398. """Writes a new message, with the specified uid.
  399. If the uid is < 0: The backend should assign a new uid and
  400. return it. In case it cannot assign a new uid, it returns
  401. the negative uid passed in WITHOUT saving the message.
  402. If the backend CAN assign a new uid, but cannot find out what
  403. this UID is (as is the case with some IMAP servers), it
  404. returns 0 but DOES save the message.
  405. IMAP backend should be the only one that can assign a new
  406. uid.
  407. If the uid is > 0, the backend should set the uid to this, if it can.
  408. If it cannot set the uid to that, it will save it anyway.
  409. It will return the uid assigned in any case.
  410. Note that savemessage() does not check against dryrun settings,
  411. so you need to ensure that savemessage is never called in a
  412. dryrun mode."""
  413. raise NotImplementedError
  414. def getmessagetime(self, uid):
  415. """Return the received time for the specified message."""
  416. raise NotImplementedError
  417. def getmessagemtime(self, uid):
  418. """Returns the message modification time of the specified message."""
  419. raise NotImplementedError
  420. def getmessageflags(self, uid):
  421. """Returns the flags for the specified message."""
  422. raise NotImplementedError
  423. def getmessagekeywords(self, uid):
  424. """Returns the keywords for the specified message."""
  425. raise NotImplementedError
  426. def savemessageflags(self, uid, flags):
  427. """Sets the specified message's flags to the given set.
  428. Note that this function does not check against dryrun settings,
  429. so you need to ensure that it is never called in a
  430. dryrun mode."""
  431. raise NotImplementedError
  432. def addmessageflags(self, uid, flags):
  433. """Adds the specified flags to the message's flag set.
  434. If a given flag is already present, it will not be duplicated.
  435. Note that this function does not check against dryrun settings,
  436. so you need to ensure that it is never called in a
  437. dryrun mode.
  438. :param uid: Message UID
  439. :param flags: A set() of flags"""
  440. newflags = self.getmessageflags(uid) | flags
  441. self.savemessageflags(uid, newflags)
  442. def addmessagesflags(self, uidlist, flags):
  443. """Note that this function does not check against dryrun settings,
  444. so you need to ensure that it is never called in a
  445. dryrun mode."""
  446. for uid in uidlist:
  447. if self.uidexists(uid):
  448. self.addmessageflags(uid, flags)
  449. def deletemessageflags(self, uid, flags):
  450. """Removes each flag given from the message's flag set.
  451. Note that this function does not check against dryrun settings,
  452. so you need to ensure that it is never called in a
  453. dryrun mode.
  454. If a given flag is already removed, no action will be taken for that
  455. flag."""
  456. newflags = self.getmessageflags(uid) - flags
  457. self.savemessageflags(uid, newflags)
  458. def deletemessagesflags(self, uidlist, flags):
  459. """
  460. Note that this function does not check against dryrun settings,
  461. so you need to ensure that it is never called in a
  462. dryrun mode."""
  463. for uid in uidlist:
  464. self.deletemessageflags(uid, flags)
  465. def getmessagelabels(self, uid):
  466. """Returns the labels for the specified message."""
  467. raise NotImplementedError
  468. def savemessagelabels(self, uid, labels, ignorelabels=None, mtime=0):
  469. """Sets the specified message's labels to the given set.
  470. Note that this function does not check against dryrun settings,
  471. so you need to ensure that it is never called in a
  472. dryrun mode."""
  473. """
  474. If this function is implemented,
  475. then it should include this code:
  476. if ignorelabels is None:
  477. ignorelabels = set()
  478. """
  479. raise NotImplementedError
  480. def addmessagelabels(self, uid, labels):
  481. """Adds the specified labels to the message's labels set. If a given
  482. label is already present, it will not be duplicated.
  483. Note that this function does not check against dryrun settings,
  484. so you need to ensure that it is never called in a
  485. dryrun mode.
  486. :param uid: Message UID
  487. :param labels: A set() of labels"""
  488. newlabels = self.getmessagelabels(uid) | labels
  489. self.savemessagelabels(uid, newlabels)
  490. def addmessageslabels(self, uidlist, labels):
  491. """Note that this function does not check against dryrun settings,
  492. so you need to ensure that it is never called in a
  493. dryrun mode.
  494. :param uidlist: Message UID
  495. :param labels: Labels to add"""
  496. for uid in uidlist:
  497. self.addmessagelabels(uid, labels)
  498. def deletemessagelabels(self, uid, labels):
  499. """Removes each label given from the message's label set.
  500. If a given label is already removed, no action will be taken for that
  501. label.
  502. Note that this function does not check against dryrun settings,
  503. so you need to ensure that it is never called in a
  504. dryrun mode.
  505. :param uid: Message uid
  506. :param labels: Labels to delete"""
  507. newlabels = self.getmessagelabels(uid) - labels
  508. self.savemessagelabels(uid, newlabels)
  509. def deletemessageslabels(self, uidlist, labels):
  510. """
  511. Note that this function does not check against dryrun settings,
  512. so you need to ensure that it is never called in a
  513. dryrun mode."""
  514. for uid in uidlist:
  515. self.deletemessagelabels(uid, labels)
  516. def addmessageheader(self, msg, headername, headervalue):
  517. """Adds new header to the provided message.
  518. Arguments:
  519. - msg: message object
  520. - headername: name of the header to add
  521. - headervalue: value of the header to add
  522. Returns: None
  523. """
  524. self.ui.debug('', 'addmessageheader: called to add %s: %s' %
  525. (headername, headervalue))
  526. msg.add_header(headername, headervalue)
  527. return
  528. def getmessageheader(self, msg, headername):
  529. """Return the value of an undefined occurence of the given header.
  530. Header name is case-insensitive.
  531. Arguments:
  532. - msg: message object
  533. - headername: name of the header to be searched
  534. Returns: header value or None if no such header was found.
  535. """
  536. self.ui.debug('', 'getmessageheader: called to get %s' % headername)
  537. return msg.get(headername)
  538. def getmessageheaderlist(self, msg, headername):
  539. """Return a list of values for the given header.
  540. Header name is case-insensitive.
  541. Arguments:
  542. - msg: message object
  543. - headername: name of the header to be searched
  544. Returns: list of header values or empty list if no such header was
  545. found.
  546. """
  547. self.ui.debug('', 'getmessageheaderlist: called to get %s' % headername)
  548. return msg.get_all(headername, [])
  549. def deletemessageheaders(self, msg, header_list):
  550. """Deletes headers in the given list from the message.
  551. Arguments:
  552. - msg: message object
  553. - header_list: list of headers to be deleted or just the header name
  554. """
  555. if type(header_list) != type([]):
  556. header_list = [header_list]
  557. self.ui.debug('',
  558. 'deletemessageheaders: called to delete %s' % header_list)
  559. for h in header_list:
  560. del msg[h]
  561. return
  562. def get_message_date(self, msg, header="Date"):
  563. """Returns the Unix timestamp of the email message, derived from the
  564. Date field header by default.
  565. Arguments:
  566. - msg: message object
  567. - header: header to extract the date from
  568. Returns: timestamp or `None` in the case of failure.
  569. """
  570. datetuple = parsedate_tz(msg.get(header))
  571. if datetuple is None:
  572. return None
  573. return mktime_tz(datetuple)
  574. def change_message_uid(self, uid, new_uid):
  575. """Change the message from existing uid to new_uid.
  576. If the backend supports it (IMAP does not).
  577. :param uid: Message UID
  578. :param new_uid: (optional) If given, the old UID will be changed
  579. to a new UID. This allows backends efficient renaming of
  580. messages if the UID has changed."""
  581. raise NotImplementedError
  582. def deletemessage(self, uid):
  583. """Note that this function does not check against dryrun settings,
  584. so you need to ensure that it is never called in a
  585. dryrun mode."""
  586. raise NotImplementedError
  587. def deletemessages(self, uidlist):
  588. """Note that this function does not check against dryrun settings,
  589. so you need to ensure that it is never called in a
  590. dryrun mode."""
  591. for uid in uidlist:
  592. self.deletemessage(uid)
  593. def copymessageto(self, uid, dstfolder, statusfolder, register=1):
  594. """Copies a message from self to dst if needed, updating the status
  595. Note that this function does not check against dryrun settings,
  596. so you need to ensure that it is never called in a
  597. dryrun mode.
  598. :param uid: uid of the message to be copied.
  599. :param dstfolder: A BaseFolder-derived instance
  600. :param statusfolder: A LocalStatusFolder instance
  601. :param register: whether we should register a new thread."
  602. :returns: Nothing on success, or raises an Exception."""
  603. # Sometimes, it could be the case that if a sync takes awhile,
  604. # a message might be deleted from the maildir before it can be
  605. # synced to the status cache. This is only a problem with
  606. # self.getmessage(). So, don't call self.getmessage unless
  607. # really needed.
  608. if register: # Output that we start a new thread.
  609. self.ui.registerthread(self.repository.account)
  610. try:
  611. message = None
  612. flags = self.getmessageflags(uid)
  613. rtime = self.getmessagetime(uid)
  614. # If any of the destinations actually stores the message body,
  615. # load it up.
  616. if dstfolder.storesmessages():
  617. message = self.getmessage(uid)
  618. # Succeeded? -> IMAP actually assigned a UID. If newid
  619. # remained negative, no server was willing to assign us an
  620. # UID. If newid is 0, saving succeeded, but we could not
  621. # retrieve the new UID. Ignore message in this case.
  622. new_uid = dstfolder.savemessage(uid, message, flags, rtime)
  623. if new_uid > 0:
  624. if new_uid != uid:
  625. # Got new UID, change the local uid to match the new one.
  626. self.change_message_uid(uid, new_uid)
  627. statusfolder.deletemessage(uid)
  628. # Got new UID, change the local uid.
  629. # Save uploaded status in the statusfolder.
  630. statusfolder.savemessage(new_uid, message, flags, rtime)
  631. # Check whether the mail has been seen.
  632. if 'S' not in flags:
  633. self.have_newmail = True
  634. elif new_uid == 0:
  635. # Message was stored to dstfolder, but we can't find it's UID
  636. # This means we can't link current message to the one created
  637. # in IMAP. So we just delete local message and on next run
  638. # we'll sync it back
  639. # XXX This could cause infinite loop on syncing between two
  640. # IMAP servers ...
  641. self.deletemessage(uid)
  642. else:
  643. msg = "Trying to save msg (uid %d) on folder " \
  644. "%s returned invalid uid %d" % \
  645. (uid, dstfolder.getvisiblename(), new_uid)
  646. raise OfflineImapError(msg, OfflineImapError.ERROR.MESSAGE)
  647. except KeyboardInterrupt: # Bubble up CTRL-C.
  648. raise
  649. except OfflineImapError as e:
  650. if e.severity > OfflineImapError.ERROR.MESSAGE:
  651. raise # Bubble severe errors up.
  652. self.ui.error(e, exc_info()[2])
  653. except Exception as e:
  654. self.ui.error(e, exc_info()[2],
  655. msg="Copying message %s [acc: %s]" %
  656. (uid, self.accountname))
  657. raise # Raise on unknown errors, so we can fix those.
  658. def _extract_message_id(self, raw_msg_bytes):
  659. """Extract the Message-ID from a bytes object containing a raw message.
  660. This function attempts to find the Message-ID for a message that has not
  661. been processed by the built-in email library, and is therefore NOT an
  662. email object. If parsing the message fails (or is otherwise not
  663. needed), this utility can be useful to help provide a (hopefully) unique
  664. identifier in log messages to facilitate locating the message on disk.
  665. :param raw_msg_bytes: bytes object containing the raw email message.
  666. :returns: A tuple containing the contents of the Message-ID header if
  667. found (or <Unknown Message-ID> if not found) and a flag which is True if
  668. the Message-ID was in proper RFC format or False if it contained
  669. defects.
  670. """
  671. msg_header = re.split(b'[\r]?\n[\r]?\n', raw_msg_bytes)[0]
  672. try:
  673. msg_id = re.search(br"\nmessage-id:[\s]+(<[A-Za-z0-9!#$%&'*+-/=?^_`{}|~.@ ]+>)",
  674. msg_header, re.IGNORECASE).group(1)
  675. except AttributeError:
  676. # No match - Likely not following RFC rules. Try and find anything
  677. # that looks like it could be the Message-ID but flag it.
  678. _start_pos = msg_header.find(b'\nMessage-ID:')
  679. if _start_pos > 0:
  680. _end_pos = msg_header.find(b'\n',_start_pos+15)
  681. msg_id = msg_header[_start_pos+12:_end_pos].strip()
  682. return (msg_id, False)
  683. else:
  684. return (b"<Unknown Message-ID>", False)
  685. return (msg_id, True)
  686. def _quote_boundary_fix(self, raw_msg_bytes):
  687. """Modify a raw message to quote the boundary separator for multipart messages.
  688. This function quotes only the first occurrence of the boundary field in
  689. the email header, and quotes any boundary value. Improperly quoted
  690. boundary fields can give the internal python email library issues.
  691. :param raw_msg_bytes: bytes object containing the raw email message.
  692. :returns: The raw byte stream containing the quoted boundary
  693. """
  694. # Use re.split to extract just the header, and search for the boundary in
  695. # the context-type header and extract just the boundary and characters per
  696. # RFC 2046 ( see https://tools.ietf.org/html/rfc2046#section-5.1.1 )
  697. # We don't cap the length to 70 characters, because we are just trying to
  698. # soft fix this message to resolve the python library looking for properly
  699. # quoted boundaries.
  700. try: boundary_field = \
  701. re.search(b"content-type:.*(boundary=[\"]?[A-Za-z0-9'()+_,-./:=? ]+[\"]?)",
  702. re.split(b'[\r]?\n[\r]?\n', raw_msg_bytes)[0],
  703. (re.IGNORECASE|re.DOTALL)).group(1)
  704. except AttributeError:
  705. # No match
  706. return raw_msg_bytes
  707. # get the boundary field, and strip off any trailing ws (against RFC rules, leading ws is OK)
  708. # if it was already quoted, well then there was nothing to fix
  709. boundary, value = boundary_field.split(b'=', 1)
  710. value = value.rstrip()
  711. # ord(b'"') == 34
  712. if value[0] == value[-1] == 34:
  713. # Sanity Check - Do not requote if already quoted.
  714. # A quoted boundary was the end goal so return the original
  715. #
  716. # No need to worry about if the original email did something like:
  717. # boundary="ahahah " as the email library will trim the ws for us
  718. return raw_msg_bytes
  719. else:
  720. new_field = b''.join([boundary, b'="', value, b'"'])
  721. return(raw_msg_bytes.replace(boundary_field, new_field, 1))
  722. def __syncmessagesto_copy(self, dstfolder, statusfolder):
  723. """Pass1: Copy locally existing messages not on the other side.
  724. This will copy messages to dstfolder that exist locally but are
  725. not in the statusfolder yet. The strategy is:
  726. 1) Look for messages present in self but not in statusfolder.
  727. 2) invoke copymessageto() on those which:
  728. - If dstfolder doesn't have it yet, add them to dstfolder.
  729. - Update statusfolder.
  730. This function checks and protects us from action in dryrun mode."""
  731. # We have no new mail yet.
  732. self.have_newmail = False
  733. threads = []
  734. copylist = [uid for uid in self.getmessageuidlist()
  735. if not statusfolder.uidexists(uid)]
  736. num_to_copy = len(copylist)
  737. # Honor 'copy_ignore_eval' configuration option.
  738. if self.copy_ignoreUIDs is not None:
  739. for uid in self.copy_ignoreUIDs:
  740. if uid in copylist:
  741. copylist.remove(uid)
  742. self.ui.ignorecopyingmessage(uid, self, dstfolder)
  743. if num_to_copy > 0 and self.repository.account.dryrun:
  744. self.ui.info("[DRYRUN] Copy {} messages from {}[{}] to {}".format(
  745. num_to_copy, self, self.repository, dstfolder.repository)
  746. )
  747. return
  748. with self:
  749. for num, uid in enumerate(copylist):
  750. # Bail out on CTRL-C or SIGTERM.
  751. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  752. break
  753. if uid == 0:
  754. msg = "Assertion that UID != 0 failed; ignoring message."
  755. self.ui.warn(msg)
  756. continue
  757. if uid > 0 and dstfolder.uidexists(uid):
  758. # dstfolder has message with that UID already,
  759. # only update status.
  760. flags = self.getmessageflags(uid)
  761. rtime = self.getmessagetime(uid)
  762. statusfolder.savemessage(uid, None, flags, rtime)
  763. continue
  764. self.ui.copyingmessage(uid, num + 1, num_to_copy, self,
  765. dstfolder)
  766. # Exceptions are caught in copymessageto().
  767. if self.suggeststhreads():
  768. self.waitforthread()
  769. thread = threadutil.InstanceLimitedThread(
  770. self.getinstancelimitnamespace(),
  771. target=self.copymessageto,
  772. name="Copy message from %s:%s" % (self.repository,
  773. self),
  774. args=(uid, dstfolder, statusfolder)
  775. )
  776. thread.start()
  777. threads.append(thread)
  778. else:
  779. self.copymessageto(uid, dstfolder, statusfolder, register=0)
  780. for thread in threads:
  781. thread.join() # Block until all "copy" threads are done.
  782. # Execute new mail hook if we have new mail.
  783. if self.have_newmail:
  784. if self.newmail_hook is not None:
  785. self.newmail_hook()
  786. def __syncmessagesto_delete(self, dstfolder, statusfolder):
  787. """Pass 2: Remove locally deleted messages on dst.
  788. Get all UIDs in statusfolder but not self. These are messages
  789. that were deleted in 'self'. Delete those from dstfolder and
  790. statusfolder.
  791. This function checks and protects us from action in dryrun mode.
  792. """
  793. # The list of messages to delete. If sync of deletions is disabled we
  794. # still remove stale entries from statusfolder (neither in local nor
  795. # remote).
  796. deletelist = [uid for uid in statusfolder.getmessageuidlist()
  797. if uid >= 0 and
  798. not self.uidexists(uid) and
  799. (self._sync_deletes or not dstfolder.uidexists(uid))]
  800. if len(deletelist):
  801. # Delete in statusfolder first to play safe. In case of abort, we
  802. # won't lose message, we will just unneccessarily retransmit some.
  803. # Delete messages from statusfolder that were either deleted by the
  804. # user, or not being tracked (e.g. because of maxage).
  805. if not self.repository.account.dryrun:
  806. statusfolder.deletemessages(deletelist)
  807. # Filter out untracked messages.
  808. deletelist = [uid for uid in deletelist if dstfolder.uidexists(uid)]
  809. if len(deletelist):
  810. self.ui.deletingmessages(deletelist, [dstfolder])
  811. if not self.repository.account.dryrun:
  812. dstfolder.deletemessages(deletelist)
  813. def combine_flags_and_keywords(self, uid, dstfolder):
  814. """Combine the message's flags and keywords using the mapping for the
  815. destination folder."""
  816. # Take a copy of the message flag set, otherwise
  817. # __syncmessagesto_flags() will fail because statusflags is actually a
  818. # reference to selfflags (which it should not, but I don't have time to
  819. # debug THAT).
  820. selfflags = set(self.getmessageflags(uid))
  821. try:
  822. keywordmap = dstfolder.getrepository().getkeywordmap()
  823. if keywordmap is None:
  824. return selfflags
  825. knownkeywords = set(keywordmap.keys())
  826. selfkeywords = self.getmessagekeywords(uid)
  827. if not knownkeywords >= selfkeywords:
  828. # Some of the message's keywords are not in the mapping, so
  829. # skip them.
  830. skipped_keywords = list(selfkeywords - knownkeywords)
  831. selfkeywords &= knownkeywords
  832. msg = "Unknown keywords skipped: %s\n" \
  833. "You may want to change your configuration to include " \
  834. "those\n" % skipped_keywords
  835. self.ui.warn(msg)
  836. keywordletterset = set([keywordmap[keyw] for keyw in selfkeywords])
  837. # Add the mapped keywords to the list of message flags.
  838. selfflags |= keywordletterset
  839. except NotImplementedError:
  840. pass
  841. return selfflags
  842. def __syncmessagesto_flags(self, dstfolder, statusfolder):
  843. """Pass 3: Flag synchronization.
  844. Compare flag mismatches in self with those in statusfolder. If
  845. msg has a valid UID and exists on dstfolder (has not e.g. been
  846. deleted there), sync the flag change to both dstfolder and
  847. statusfolder.
  848. This function checks and protects us from action in ryrun mode.
  849. """
  850. # For each flag, we store a list of uids to which it should be
  851. # added. Then, we can call addmessagesflags() to apply them in
  852. # bulk, rather than one call per message.
  853. addflaglist = {}
  854. delflaglist = {}
  855. for uid in self.getmessageuidlist():
  856. # Ignore messages with negative UIDs missed by pass 1 and
  857. # don't do anything if the message has been deleted remotely
  858. if uid < 0 or not dstfolder.uidexists(uid):
  859. continue
  860. if statusfolder.uidexists(uid):
  861. statusflags = statusfolder.getmessageflags(uid)
  862. else:
  863. statusflags = set()
  864. selfflags = self.combine_flags_and_keywords(uid, dstfolder)
  865. addflags = selfflags - statusflags
  866. delflags = statusflags - selfflags
  867. for flag in addflags:
  868. if flag not in addflaglist:
  869. addflaglist[flag] = []
  870. addflaglist[flag].append(uid)
  871. for flag in delflags:
  872. if flag not in delflaglist:
  873. delflaglist[flag] = []
  874. delflaglist[flag].append(uid)
  875. for flag, uids in list(addflaglist.items()):
  876. self.ui.addingflags(uids, flag, dstfolder)
  877. if self.repository.account.dryrun:
  878. continue # Don't actually add in a dryrun.
  879. dstfolder.addmessagesflags(uids, set(flag))
  880. statusfolder.addmessagesflags(uids, set(flag))
  881. for flag, uids in list(delflaglist.items()):
  882. self.ui.deletingflags(uids, flag, dstfolder)
  883. if self.repository.account.dryrun:
  884. continue # Don't actually remove in a dryrun.
  885. dstfolder.deletemessagesflags(uids, set(flag))
  886. statusfolder.deletemessagesflags(uids, set(flag))
  887. def syncmessagesto(self, dstfolder, statusfolder):
  888. """Syncs messages in this folder to the destination dstfolder.
  889. This is the high level entry for syncing messages in one direction.
  890. Syncsteps are:
  891. Pass1: Copy locally existing messages
  892. Copy messages in self, but not statusfolder to dstfolder if not
  893. already in dstfolder. dstfolder might assign a new UID (e.g. if
  894. uploading to IMAP). Update statusfolder.
  895. Pass2: Remove locally deleted messages
  896. Get all UIDS in statusfolder but not self. These are messages
  897. that were deleted in 'self'. Delete those from dstfolder and
  898. statusfolder.
  899. After this pass, the message lists should be identical wrt the
  900. uids present (except for potential negative uids that couldn't
  901. be placed anywhere).
  902. Pass3: Synchronize flag changes
  903. Compare flag mismatches in self with those in statusfolder. If
  904. msg has a valid UID and exists on dstfolder (has not e.g. been
  905. deleted there), sync the flag change to both dstfolder and
  906. statusfolder.
  907. Pass4: Synchronize label changes (Gmail only)
  908. Compares label mismatches in self with those in statusfolder.
  909. If msg has a valid UID and exists on dstfolder, syncs the labels
  910. to both dstfolder and statusfolder.
  911. :param dstfolder: Folderinstance to sync the msgs to.
  912. :param statusfolder: LocalStatus instance to sync against.
  913. """
  914. for action in self.syncmessagesto_passes:
  915. # Bail out on CTRL-C or SIGTERM.
  916. if offlineimap.accounts.Account.abort_NOW_signal.is_set():
  917. break
  918. try:
  919. action(dstfolder, statusfolder)
  920. except KeyboardInterrupt:
  921. raise
  922. except OfflineImapError as e:
  923. if e.severity > OfflineImapError.ERROR.FOLDER:
  924. raise
  925. msg = "while syncing %s [account %s]" % (self, self.accountname)
  926. self.ui.error(e, exc_info()[2], msg)
  927. except Exception as e:
  928. msg = "while syncing %s [account %s]" % (self, self.accountname)
  929. self.ui.error(e, exc_info()[2], msg)
  930. raise # Raise unknown Exceptions so we can fix them.
  931. def __eq__(self, other):
  932. """Comparisons work either on string comparing folder names or
  933. on the same instance.
  934. MailDirFolder('foo') == 'foo' --> True
  935. a = MailDirFolder('foo'); a == b --> True
  936. MailDirFolder('foo') == 'moo' --> False
  937. MailDirFolder('foo') == IMAPFolder('foo') --> False
  938. MailDirFolder('foo') == MaildirFolder('foo') --> False
  939. """
  940. if isinstance(other, str):
  941. return other == self.name
  942. return id(self) == id(other)
  943. def __ne__(self, other):
  944. return not self.__eq__(other)