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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
|
# IMAP folder support
# 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 random
import binascii
import re
import time
from sys import exc_info
from offlineimap import imaputil, imaplibutil, OfflineImapError
from offlineimap import globals
from imaplib2 import MonthNames
from .Base import BaseFolder
# Globals
CRLF = '\r\n'
MSGCOPY_NAMESPACE = 'MSGCOPY_'
class IMAPFolder(BaseFolder):
def __init__(self, imapserver, name, repository, decode=True):
# decode the folder name from IMAP4_utf_7 to utf_8 if
# - utf8foldernames is enabled for the *account*
# - the decode argument is given
# (default True is used when the folder name is the result of
# querying the IMAP server, while False is used when creating
# a folder object from a locally available utf_8 name)
# In any case the given name is first dequoted.
name = imaputil.dequote(name)
if decode and repository.account.utf_8_support:
name = imaputil.IMAP_utf8(name)
self.sep = imapserver.delim
super(IMAPFolder, self).__init__(name, repository)
if repository.getdecodefoldernames():
self.visiblename = imaputil.decode_mailbox_name(self.visiblename)
self.idle_mode = False
self.expunge = repository.getexpunge()
self.root = None # imapserver.root
self.imapserver = imapserver
self.randomgenerator = random.Random()
# self.ui is set in BaseFolder.
self.imap_query = ['BODY.PEEK[]']
# number of times to retry fetching messages
self.retrycount = self.repository.getconfint('retrycount', 2)
fh_conf = self.repository.account.getconf('filterheaders', '')
self.filterheaders = [h for h in re.split(r'\s*,\s*', fh_conf) if h]
# self.copy_ignoreUIDs is used by BaseFolder.
self.copy_ignoreUIDs = repository.get_copy_ignore_UIDs(
self.getvisiblename())
if self.repository.getidlefolders():
self.idle_mode = True
def __selectro(self, imapobj, force=False):
"""Select this folder when we do not need write access.
Prefer SELECT to EXAMINE if we can, since some servers
(Courier) do not stabilize UID validity until the folder is
selected.
.. todo: Still valid? Needs verification
:param: Enforce new SELECT even if we are on that folder already.
:returns: raises :exc:`OfflineImapError` severity FOLDER on error"""
try:
imapobj.select(self.getfullIMAPname(), force=force)
except imapobj.readonly:
imapobj.select(self.getfullIMAPname(), readonly=True, force=force)
def getfullIMAPname(self):
name = self.getfullname()
if self.repository.account.utf_8_support:
name = imaputil.utf8_IMAP(name)
return imaputil.foldername_to_imapname(name)
# Interface from BaseFolder
def suggeststhreads(self):
singlethreadperfolder_default = False
if self.idle_mode is True:
singlethreadperfolder_default = True
onethread = self.config.getdefaultboolean(
"Repository %s" % self.repository.getname(),
"singlethreadperfolder", singlethreadperfolder_default)
if onethread is True:
return False
return not globals.options.singlethreading
# Interface from BaseFolder
def waitforthread(self):
self.imapserver.connectionwait()
def getmaxage(self):
if self.config.getdefault("Account %s" %
self.accountname, "maxage", None):
raise OfflineImapError(
"maxage is not supported on IMAP-IMAP sync",
OfflineImapError.ERROR.REPO,
exc_info()[2])
# Interface from BaseFolder
def getinstancelimitnamespace(self):
return MSGCOPY_NAMESPACE + self.repository.getname()
# Interface from BaseFolder
def get_uidvalidity(self):
"""Retrieve the current connections UIDVALIDITY value
UIDVALIDITY value will be cached on the first call.
:returns: The UIDVALIDITY as (long) number."""
if hasattr(self, '_uidvalidity'):
# Use cached value if existing.
return self._uidvalidity
imapobj = self.imapserver.acquireconnection()
try:
# SELECT (if not already done) and get current UIDVALIDITY.
self.__selectro(imapobj)
typ, uidval = imapobj.response('UIDVALIDITY')
assert uidval != [None] and uidval is not None, \
"response('UIDVALIDITY') returned [None]!"
self._uidvalidity = int(uidval[-1])
return self._uidvalidity
finally:
self.imapserver.releaseconnection(imapobj)
# Interface from BaseFolder
def quickchanged(self, statusfolder):
# An IMAP folder has definitely changed if the number of
# messages or the UID of the last message have changed. Otherwise
# only flag changes could have occurred.
retry = True # Should we attempt another round or exit?
imapdata = None
while retry:
retry = False
imapobj = self.imapserver.acquireconnection()
try:
# Select folder and get number of messages.
restype, imapdata = imapobj.select(self.getfullIMAPname(), True,
True)
self.imapserver.releaseconnection(imapobj)
except OfflineImapError as e:
# Retry on dropped connections, raise otherwise.
self.imapserver.releaseconnection(imapobj, True)
if e.severity == OfflineImapError.ERROR.FOLDER_RETRY:
retry = True
else:
raise
except:
# Cleanup and raise on all other errors.
self.imapserver.releaseconnection(imapobj, True)
raise
# 1. Some mail servers do not return an EXISTS response
# if the folder is empty. 2. ZIMBRA servers can return
# multiple EXISTS replies in the form 500, 1000, 1500,
# 1623 so check for potentially multiple replies.
if imapdata == [None]:
return True
maxmsgid = 0
for msgid in imapdata:
maxmsgid = max(int(msgid), maxmsgid)
# Different number of messages than last time?
if maxmsgid != statusfolder.getmessagecount():
return True
return False
def _msgs_to_fetch(self, imapobj, min_date=None, min_uid=None):
"""Determines sequence numbers of messages to be fetched.
Message sequence numbers (MSNs) are more easily compacted
into ranges which makes transactions slightly faster.
Arguments:
- imapobj: instance of IMAPlib
- min_date (optional): a time_struct; only fetch messages newer
than this
- min_uid (optional): only fetch messages with UID >= min_uid
This function should be called with at MOST one of min_date OR
min_uid set but not BOTH.
Returns: range(s) for messages or None if no messages
are to be fetched."""
def search(search_conditions):
"""Actually request the server with the specified conditions.
Returns: range(s) for messages or None if no messages
are to be fetched."""
try:
res_type, res_data = imapobj.search(None, search_conditions)
if res_type != 'OK':
msg = "SEARCH in folder [%s]%s failed. " \
"Search string was '%s'. " \
"Server responded '[%s] %s'" % \
(self.getrepository(), self, search_cond,
res_type, res_data)
raise OfflineImapError(msg, OfflineImapError.ERROR.FOLDER)
except Exception as e:
msg = "SEARCH in folder [%s]%s failed. "\
"Search string was '%s'. Error: %s" % \
(self.getrepository(), self, search_cond, str(e))
raise OfflineImapError(msg, OfflineImapError.ERROR.FOLDER)
"""
In Py2, with IMAP, imaplib2 returned a list of one element string.
['1, 2, 3, ...'] -> in Py3 is [b'1 2 3,...']
In Py2, with Davmail, imaplib2 returned a list of strings.
['1', '2', '3', ...] -> in Py3 should be [b'1', b'2', b'3',...]
In my tests with Py3, I get a list with one element: [b'1 2 3 ...']
Then I convert the values to string and I get ['1 2 3 ...']
With Davmail, it should be [b'1', b'2', b'3',...]
When I convert the values to string, I get ['1', '2', '3',...]
"""
res_data = [x.decode('utf-8') for x in res_data]
# Then, I can do the check in the same way than Python 2
# with string comparison:
if len(res_data) > 0 and (' ' in res_data[0] or res_data[0] == ''):
res_data = res_data[0].split()
# Some servers are broken.
if 0 in res_data:
self.ui.warn("server returned UID with 0; ignoring.")
res_data.remove(0)
return res_data
a = self.getfullIMAPname()
res_type, imapdata = imapobj.select(a, True, True)
# imaplib2 returns the type as string, like "OK" but
# returns imapdata as list of bytes, like [b'0'] so we need decode it
# to use the existing code
imapdata = [x.decode('utf-8') for x in imapdata]
if imapdata == [None] or imapdata[0] == '0':
# Empty folder, no need to populate message list.
return None
conditions = []
# 1. min_uid condition.
if min_uid is not None:
conditions.append("UID %d:*" % min_uid)
# 2. date condition.
elif min_date is not None:
# Find out what the oldest message is that we should look at.
conditions.append("SINCE %02d-%s-%d" % (
min_date[2], MonthNames[min_date[1]], min_date[0]))
# 3. maxsize condition.
maxsize = self.getmaxsize()
if maxsize is not None:
conditions.append("SMALLER %d" % maxsize)
if len(conditions) >= 1:
# Build SEARCH command.
search_cond = "(%s)" % ' '.join(conditions)
search_result = search(search_cond)
return imaputil.uid_sequence(search_result)
# By default consider all messages in this folder.
return '1:*'
# Interface from BaseFolder
def msglist_item_initializer(self, uid):
return {'uid': uid, 'flags': set(), 'time': 0}
# Interface from BaseFolder
def cachemessagelist(self, min_date=None, min_uid=None):
self.ui.loadmessagelist(self.repository, self)
self.dropmessagelistcache()
imapobj = self.imapserver.acquireconnection()
try:
msgsToFetch = self._msgs_to_fetch(
imapobj, min_date=min_date, min_uid=min_uid)
if not msgsToFetch:
return # No messages to sync.
# Get the flags and UIDs for these. single-quotes prevent
# imaplib2 from quoting the sequence.
fetch_msg = "%s" % msgsToFetch
self.ui.debug('imap', "calling imaplib2 fetch command: %s %s" %
(fetch_msg, '(FLAGS UID INTERNALDATE)'))
res_type, response = imapobj.fetch(
fetch_msg, '(FLAGS UID INTERNALDATE)')
if res_type != 'OK':
msg = "FETCHING UIDs in folder [%s]%s failed. "\
"Server responded '[%s] %s'" % \
(self.getrepository(), self, res_type, response)
raise OfflineImapError(msg, OfflineImapError.ERROR.FOLDER)
finally:
self.imapserver.releaseconnection(imapobj)
for messagestr in response:
# Looks like: '1 (FLAGS (\\Seen Old) UID 4807)' or None if no msg.
# Discard initial message number.
if messagestr is None:
continue
messagestr = messagestr.decode('utf-8').split(' ', 1)[1]
options = imaputil.flags2hash(messagestr)
if 'UID' not in options:
self.ui.warn('No UID in message with options %s' %
str(options), minor=1)
else:
uid = int(options['UID'])
self.messagelist[uid] = self.msglist_item_initializer(uid)
flags = imaputil.flagsimap2maildir(options['FLAGS'])
keywords = imaputil.flagsimap2keywords(options['FLAGS'])
rtime = imaplibutil.Internaldate2epoch(
messagestr.encode('utf-8'))
self.messagelist[uid] = {'uid': uid,
'flags': flags,
'time': rtime,
'keywords': keywords}
self.ui.messagelistloaded(self.repository, self, self.getmessagecount())
# Interface from BaseFolder
def getmessage(self, uid):
"""Retrieve message with UID from the IMAP server (incl body).
After this function all CRLFs will be transformed to '\n'.
:returns: the message body or throws and OfflineImapError
(probably severity MESSAGE) if e.g. no message with
this UID could be found.
"""
data = self._fetch_from_imap(str(uid), self.retrycount)
# Data looks now e.g.
# ['320 (17061 BODY[] {2565}',<email.message.EmailMessage object>]
# Is a list of two elements. Message is at [1]
msg = data[1]
if self.ui.is_debugging('imap'):
# Optimization: don't create the debugging objects unless needed
msg_s = msg.as_string(policy=self.policy['8bit-RFC'])
if len(msg_s) > 200:
dbg_output = "%s...%s" % (msg_s[:150], msg_s[-50:])
else:
dbg_output = msg_s
self.ui.debug('imap', "Returned object from fetching %d: '%s'" %
(uid, dbg_output))
return msg
# Interface from BaseFolder
def getmessagetime(self, uid):
return self.messagelist[uid]['time']
# Interface from BaseFolder
def getmessageflags(self, uid):
return self.messagelist[uid]['flags']
# Interface from BaseFolder
def getmessagekeywords(self, uid):
return self.messagelist[uid]['keywords']
def __generate_randomheader(self, msg, policy=None):
"""Returns a unique X-OfflineIMAP header
Generate an 'X-OfflineIMAP' mail header which contains a random
unique value (which is based on the mail content, and a random
number). This header allows us to fetch a mail after APPENDing
it to an IMAP server and thus find out the UID that the server
assigned it.
:returns: (headername, headervalue) tuple, consisting of strings
headername == 'X-OfflineIMAP' and headervalue will be a
random string
"""
headername = 'X-OfflineIMAP'
if policy is None:
output_policy = self.policy['8bit-RFC']
else:
output_policy = policy
# We need a random component too. If we ever upload the same
# mail twice (e.g. in different folders), we would still need to
# get the UID for the correct one. As we won't have too many
# mails with identical content, the randomness requirements are
# not extremly critial though.
# Compute unsigned crc32 of 'msg' (as bytes) into a unique hash.
# NB: crc32 returns unsigned only starting with python 3.0.
headervalue = '{}-{}'.format(
(binascii.crc32(msg.as_bytes(policy=output_policy)) & 0xffffffff),
self.randomgenerator.randint(0, 9999999999))
return headername, headervalue
def __savemessage_searchforheader(self, imapobj, headername, headervalue):
self.ui.debug('imap',
'__savemessage_searchforheader called for %s: %s' %
(headername, headervalue))
# Now find the UID it got.
headervalue = imapobj._quote(headervalue)
try:
matchinguids = imapobj.uid('search', 'HEADER',
headername, headervalue)[1][0]
# Returned value is type bytes
matchinguids = matchinguids.decode('utf-8')
except imapobj.error as err:
# IMAP server doesn't implement search or had a problem.
self.ui.debug('imap',
"__savemessage_searchforheader: got IMAP error '%s' "
"while attempting to UID SEARCH for message with "
"header %s" % (err, headername))
return 0
self.ui.debug('imap',
"__savemessage_searchforheader got initial "
"matchinguids: " + repr(matchinguids))
if matchinguids == '':
self.ui.debug('imap',
"__savemessage_searchforheader: UID SEARCH "
"for message with header %s yielded no results" %
headername)
return 0
matchinguids = matchinguids.split(' ')
self.ui.debug('imap', '__savemessage_searchforheader: matchinguids now '
+ repr(matchinguids))
if len(matchinguids) != 1 or matchinguids[0] is None:
raise OfflineImapError(
"While attempting to find UID for message with "
"header %s, got wrong-sized matchinguids of %s" %
(headername, str(matchinguids)),
OfflineImapError.ERROR.MESSAGE
)
return int(matchinguids[0])
def __savemessage_fetchheaders(self, imapobj, headername, headervalue):
""" We fetch all new mail headers and search for the right
X-OfflineImap line by hand. The response from the server has form:
(
'OK',
[
(
'185 (RFC822.HEADER {1789}',
'... mail headers ...'
),
' UID 2444)',
(
'186 (RFC822.HEADER {1789}',
'... 2nd mail headers ...'
),
' UID 2445)'
]
)
We need to locate the UID just after mail headers containing our
X-OfflineIMAP line.
Returns UID when found, 0 when not found."""
self.ui.debug('imap', '__savemessage_fetchheaders called for %s: %s' %
(headername, headervalue))
# Run "fetch X:* rfc822.header".
# Since we stored the mail we are looking for just recently, it would
# not be optimal to fetch all messages. So we'll find highest message
# UID in our local messagelist and search from there (exactly from
# UID+1). That works because UIDs are guaranteed to be unique and
# ascending.
if self.getmessagelist():
start = 1 + max(self.getmessagelist().keys())
else:
# Folder was empty - start from 1.
start = 1
result = imapobj.uid('FETCH', '%d:*' % start, 'rfc822.header')
if result[0] != 'OK':
msg = 'Error fetching mail headers: %s' % '. '.join(result[1])
raise OfflineImapError(msg, OfflineImapError.ERROR.MESSAGE)
# result is like:
# [
# ('185 (RFC822.HEADER {1789}', '... mail headers ...'),
# ' UID 2444)',
# ('186 (RFC822.HEADER {1789}', '... 2nd mail headers ...'),
# ' UID 2445)'
# ]
result = result[1]
found = None
# item is like:
# ('185 (RFC822.HEADER {1789}', '... mail headers ...'), ' UID 2444)'
for item in result:
if found is None and type(item) == tuple:
# Decode the value
item = [x.decode('utf-8') for x in item]
# Walk just tuples.
if re.search("(?:^|\\r|\\n)%s:\s*%s(?:\\r|\\n)" %
(headername, headervalue),
item[1], flags=re.IGNORECASE):
found = item[0]
elif found is not None:
if isinstance(item, bytes):
item = item.decode('utf-8')
uid = re.search("UID\s+(\d+)", item, flags=re.IGNORECASE)
if uid:
return int(uid.group(1))
else:
# This parsing is for Davmail.
# https://github.com/OfflineIMAP/offlineimap/issues/479
# item is like:
# ')'
# and item[0] stored in "found" is like:
# '1694 (UID 1694 RFC822.HEADER {1294}'
uid = re.search("\d+\s+\(UID\s+(\d+)", found,
flags=re.IGNORECASE)
if uid:
return int(uid.group(1))
self.ui.warn("Can't parse FETCH response, "
"can't find UID in %s" % item)
self.ui.debug('imap', "Got: %s" % repr(result))
else:
self.ui.warn("Can't parse FETCH response, "
"we awaited string: %s" % repr(item))
return 0
def __getmessageinternaldate(self, msg, rtime=None):
"""Parses mail and returns an INTERNALDATE string
It will use information in the following order, falling back as an
attempt fails:
- rtime parameter
- Date header of email
We return None, if we couldn't find a valid date. In this case
the IMAP server will use the server local time when appening
(per RFC).
Note, that imaplib's Time2Internaldate is inherently broken as
it returns localized date strings which are invalid for IMAP
servers. However, that function is called for *every* append()
internally. So we need to either pass in `None` or the correct
string (in which case Time2Internaldate() will do nothing) to
append(). The output of this function is designed to work as
input to the imapobj.append() function.
TODO: We should probably be returning a bytearray rather than a
string here, because the IMAP server will expect plain
ASCII. However, imaplib.Time2INternaldate currently returns a
string so we go with the same for now.
:param rtime: epoch timestamp to be used rather than analyzing
the email.
:returns: string in the form of "DD-Mmm-YYYY HH:MM:SS +HHMM"
(including double quotes) or `None` in case of failure
(which is fine as value for append)."""
if rtime is None:
rtime = self.get_message_date(msg)
if rtime is None:
return None
datetuple = time.localtime(rtime)
try:
# Check for invalid dates.
if datetuple[0] < 1981:
raise ValueError
# Check for invalid dates.
datetuple_check = time.localtime(time.mktime(datetuple))
if datetuple[:2] != datetuple_check[:2]:
raise ValueError
except (ValueError, OverflowError):
# Argh, sometimes it's a valid format but year is 0102
# or something. Argh. It seems that Time2Internaldate
# will rause a ValueError if the year is 0102 but not 1902,
# but some IMAP servers nonetheless choke on 1902.
self.ui.debug('imap', "Message with invalid date %s. "
"Server will use local time." % datetuple)
return None
# Produce a string representation of datetuple that works as
# INTERNALDATE.
num2mon = {1: 'Jan', 2: 'Feb', 3: 'Mar',
4: 'Apr', 5: 'May', 6: 'Jun',
7: 'Jul', 8: 'Aug', 9: 'Sep',
10: 'Oct', 11: 'Nov', 12: 'Dec'}
# tm_isdst coming from email.parsedate is not usable, we still use it
# here, mhh.
if datetuple.tm_isdst == 1:
zone = -time.altzone
else:
zone = -time.timezone
offset_h, offset_m = divmod(zone // 60, 60)
internaldate = '"%02d-%s-%04d %02d:%02d:%02d %+03d%02d"' % \
(datetuple.tm_mday, num2mon[datetuple.tm_mon],
datetuple.tm_year, datetuple.tm_hour,
datetuple.tm_min, datetuple.tm_sec,
offset_h, offset_m)
return internaldate
# Interface from BaseFolder
def savemessage(self, uid, msg, flags, rtime):
"""Save the message on the Server
This backend always assigns a new uid, so the uid arg is ignored.
This function will update the self.messagelist dict to contain
the new message after sucessfully saving it.
See folder/Base for details. Note that savemessage() does not
check against dryrun settings, so you need to ensure that
savemessage is never called in a dryrun mode.
:param uid: Message UID
:param msg: Message Object
:param flags: Message flags
:param rtime: A timestamp to be used as the mail date
:returns: the UID of the new message as assigned by the server. If the
message is saved, but it's UID can not be found, it will
return 0. If the message can't be written (folder is
read-only for example) it will return -1."""
self.ui.savemessage('imap', uid, flags, self)
# Already have it, just save modified flags.
if uid > 0 and self.uidexists(uid):
self.savemessageflags(uid, flags)
return uid
# Filter user requested headers before uploading to the IMAP server
self.deletemessageheaders(msg, self.filterheaders)
# Should just be able to set the policy, to use CRLF in msg output
output_policy = self.policy['8bit-RFC']
# Get the date of the message, so we can pass it to the server.
date = self.__getmessageinternaldate(msg, rtime)
# Message-ID is handy for debugging messages.
msg_id = self.getmessageheader(msg, "message-id")
if not msg_id:
msg_id = '[unknown message-id]'
retry_left = 2 # succeeded in APPENDING?
imapobj = self.imapserver.acquireconnection()
# NB: in the finally clause for this try we will release
# NB: the acquired imapobj, so don't do that twice unless
# NB: you will put another connection to imapobj. If you
# NB: really do need to release connection manually, set
# NB: imapobj to None.
try:
while retry_left:
# XXX: we can mangle message only once, out of the loop
# UIDPLUS extension provides us with an APPENDUID response.
use_uidplus = 'UIDPLUS' in imapobj.capabilities
if not use_uidplus:
# Insert a random unique header that we can fetch later.
(headername, headervalue) = self.__generate_randomheader(
msg)
self.ui.debug('imap', 'savemessage: header is: %s: %s' %
(headername, headervalue))
self.addmessageheader(msg, headername, headervalue)
if self.ui.is_debugging('imap'):
# Optimization: don't create the debugging objects unless needed
msg_s = msg.as_string(policy=output_policy)
if len(msg_s) > 200:
dbg_output = "%s...%s" % (msg_s[:150], msg_s[-50:])
else:
dbg_output = msg_s
self.ui.debug('imap', "savemessage: date: %s, content: '%s'" %
(date, dbg_output))
try:
# Select folder for append and make the box READ-WRITE.
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
# readonly exception. Return original uid to notify that
# we did not save the message. (see savemessage in Base.py)
self.ui.msgtoreadonly(self, uid)
return uid
# Do the APPEND.
try:
(typ, dat) = imapobj.append(
self.getfullIMAPname(),
imaputil.flagsmaildir2imap(flags),
date, msg.as_bytes(policy=output_policy))
# This should only catch 'NO' responses since append()
# will raise an exception for 'BAD' responses:
if typ != 'OK':
# For example, Groupwise IMAP server
# can return something like:
#
# NO APPEND The 1500 MB storage limit \
# has been exceeded.
#
# In this case, we should immediately abort
# the repository sync and continue
# with the next account.
err_msg = \
"Saving msg (%s) in folder '%s', " \
"repository '%s' failed (abort). " \
"Server responded: %s %s\n" % \
(msg_id, self, self.getrepository(), typ, dat)
raise OfflineImapError(err_msg, OfflineImapError.ERROR.REPO)
retry_left = 0 # Mark as success.
except imapobj.abort as e:
# Connection has been reset, release connection and retry.
retry_left -= 1
self.imapserver.releaseconnection(imapobj, True)
imapobj = self.imapserver.acquireconnection()
if not retry_left:
raise OfflineImapError(
"Saving msg (%s) in folder '%s', "
"repository '%s' failed (abort). "
"Server responded: %s\n"
"Message content was: %s" %
(msg_id, self, self.getrepository(),
str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE,
exc_info()[2])
# XXX: is this still needed?
self.ui.error(e, exc_info()[2])
except imapobj.error as e: # APPEND failed
# If the server responds with 'BAD', append()
# raise()s directly. So we catch that too.
# drop conn, it might be bad.
self.imapserver.releaseconnection(imapobj, True)
imapobj = None
raise OfflineImapError(
"Saving msg (%s) folder '%s', repo '%s'"
"failed (error). Server responded: %s\n"
"Message content was: %s" %
(msg_id, self, self.getrepository(),
str(e), dbg_output),
OfflineImapError.ERROR.MESSAGE,
exc_info()[2])
# Checkpoint. Let it write out stuff, etc. Eg searches for
# just uploaded messages won't work if we don't do this.
(typ, dat) = imapobj.check()
assert (typ == 'OK')
# Get the new UID, do we use UIDPLUS?
if use_uidplus:
# Get new UID from the APPENDUID response, it could look
# like OK [APPENDUID 38505 3955] APPEND completed with
# 38505 bein folder UIDvalidity and 3955 the new UID.
# note: we would want to use .response() here but that
# often seems to return [None], even though we have
# data. TODO
resp = imapobj._get_untagged_response('APPENDUID')
if resp == [None] or resp is None:
self.ui.warn("Server supports UIDPLUS but got no APPENDUID "
"appending a message. Got: %s." % str(resp))
return 0
try:
# Convert the UID from [b'4 1532'] to ['4 1532']
s_uid = [x.decode('utf-8') for x in resp]
# Now, read the UID field
uid = int(s_uid[-1].split(' ')[1])
except ValueError:
uid = 0 # Definetly not what we should have.
except Exception:
raise OfflineImapError("Unexpected response: %s" %
str(resp),
OfflineImapError.ERROR.MESSAGE)
if uid == 0:
self.ui.warn("savemessage: Server supports UIDPLUS, but"
" we got no usable UID back. APPENDUID "
"reponse was '%s'" % str(resp))
else:
try:
# We don't use UIDPLUS.
uid = self.__savemessage_searchforheader(imapobj,
headername,
headervalue)
# See docs for savemessage in Base.py for explanation
# of this and other return values.
if uid == 0:
self.ui.debug('imap',
'savemessage: attempt to get new UID '
'UID failed. Search headers manually.')
uid = self.__savemessage_fetchheaders(imapobj,
headername,
headervalue)
self.ui.warn("savemessage: Searching mails for new "
"Message-ID failed. "
"Could not determine new UID on %s." %
self.getname())
# Something wrong happened while trying to get the UID. Explain
# the error might be about the 'get UID' process not necesseraly
# the APPEND.
except Exception:
self.ui.warn("%s: could not determine the UID while we got "
"no error while appending the "
"email with '%s: %s'" %
(self.getname(), headername, headervalue))
raise
finally:
if imapobj:
self.imapserver.releaseconnection(imapobj)
if uid: # Avoid UID FETCH 0 crash happening later on.
self.messagelist[uid] = self.msglist_item_initializer(uid)
self.messagelist[uid]['flags'] = flags
self.ui.debug('imap', 'savemessage: returning new UID %d' % uid)
return uid
def _fetch_from_imap(self, uids, retry_num=1):
"""Fetches data from IMAP server.
Arguments:
- uids: message UIDS (OfflineIMAP3: First UID returned only)
- retry_num: number of retries to make
Returns: data obtained by this query."""
imapobj = self.imapserver.acquireconnection()
try:
query = "(%s)" % (" ".join(self.imap_query))
fails_left = retry_num # Retry on dropped connection.
while fails_left:
try:
imapobj.select(self.getfullIMAPname(), readonly=True)
res_type, data = imapobj.uid('fetch', uids, query)
break
except imapobj.abort as e:
fails_left -= 1
# self.ui.error() will show the original traceback.
if fails_left <= 0:
message = ("%s, while fetching msg %r in folder %r."
" Max retry reached (%d)" %
(e, uids, self.name, retry_num))
raise OfflineImapError(message,
OfflineImapError.ERROR.MESSAGE)
self.ui.error("%s. While fetching msg %r in folder %r."
" Query: %s Retrying (%d/%d)" % (
e, uids, self.name, query,
retry_num - fails_left, retry_num))
# Release dropped connection, and get a new one.
self.imapserver.releaseconnection(imapobj, True)
imapobj = self.imapserver.acquireconnection()
finally:
# The imapobj here might be different than the one created before
# the ``try`` clause. So please avoid transforming this to a nice
# ``with`` without taking this into account.
self.imapserver.releaseconnection(imapobj)
# Ensure to not consider unsolicited FETCH responses caused by flag
# changes from concurrent connections. These appear as strings in
# 'data' (the BODY response appears as a tuple). This should leave
# exactly one response.
if res_type == 'OK':
data = [res for res in data if not isinstance(res, bytes)]
# Could not fetch message. Note: it is allowed by rfc3501 to return any
# data for the UID FETCH command.
if data == [None] or res_type != 'OK' or len(data) != 1:
severity = OfflineImapError.ERROR.MESSAGE
reason = "IMAP server '%s' failed to fetch messages UID '%s'. " \
"Server responded: %s %s" % (self.getrepository(), uids,
res_type, data)
if data == [None] or len(data) < 1:
# IMAP server did not find a message with this UID.
reason = "IMAP server '%s' does not have a message " \
"with UID '%s'" % (self.getrepository(), uids)
raise OfflineImapError(reason, severity)
# JI: In offlineimap, this function returned a tuple of strings for each
# fetched UID, offlineimap3 calls to the imap object return bytes and so
# originally a fixed, utf-8 conversion was done and *only* the first
# response (d[0]) was returned. Note that this alters the behavior
# between code bases. However, it seems like a single UID is the intent
# of this function so retaining the modfication here for now.
#
# TODO: Can we assume the server response containing the meta data is
# always 'utf-8' encoded? Assuming yes for now.
#
# Convert responses, d[0][0], into a 'utf-8' string (from bytes) and
# Convert email, d[0][1], into a message object (from bytes)
ndata0 = data[0][0].decode('utf-8')
ndata1 = self.parser['8bit-RFC'].parsebytes(data[0][1])
ndata = [ndata0, ndata1]
return ndata
def _store_to_imap(self, imapobj, uid, field, data):
"""Stores data to IMAP server
Arguments:
- imapobj: instance of IMAPlib to use
- uid: message UID
- field: field name to be stored/updated
- data: field contents
"""
imapobj.select(self.getfullIMAPname())
res_type, retdata = imapobj.uid('store', uid, field, data)
if res_type != 'OK':
severity = OfflineImapError.ERROR.MESSAGE
reason = "IMAP server '%s' failed to store %s " \
"for message UID '%d'." \
"Server responded: %s %s" % (
self.getrepository(), field, uid, res_type, retdata)
raise OfflineImapError(reason, severity)
return retdata[0]
# Interface from BaseFolder
def savemessageflags(self, uid, flags):
"""Change a message's flags to `flags`.
Note that this function does not check against dryrun settings,
so you need to ensure that it is never called in a
dryrun mode."""
imapobj = self.imapserver.acquireconnection()
try:
result = self._store_to_imap(imapobj, str(uid), 'FLAGS',
imaputil.flagsmaildir2imap(flags))
except imapobj.readonly:
self.ui.flagstoreadonly(self, [uid], flags)
return
finally:
self.imapserver.releaseconnection(imapobj)
if not result:
self.messagelist[uid]['flags'] = flags
else:
flags = imaputil.flags2hash(imaputil.imapsplit(result)[1])['FLAGS']
self.messagelist[uid]['flags'] = imaputil.flagsimap2maildir(flags)
# Interface from BaseFolder
def addmessageflags(self, uid, flags):
self.addmessagesflags([uid], flags)
def __addmessagesflags_noconvert(self, uidlist, flags):
self.__processmessagesflags('+', uidlist, flags)
# Interface from BaseFolder
def addmessagesflags(self, uidlist, flags):
"""This is here for the sake of UIDMaps.py -- deletemessages must
add flags and get a converted UID, and if we don't have noconvert,
then UIDMaps will try to convert it twice."""
self.__addmessagesflags_noconvert(uidlist, flags)
# Interface from BaseFolder
def deletemessageflags(self, uid, flags):
self.deletemessagesflags([uid], flags)
# Interface from BaseFolder
def deletemessagesflags(self, uidlist, flags):
self.__processmessagesflags('-', uidlist, flags)
def __processmessagesflags_real(self, operation, uidlist, flags):
imapobj = self.imapserver.acquireconnection()
try:
try:
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
self.ui.flagstoreadonly(self, uidlist, flags)
return
response = imapobj.uid('store',
imaputil.uid_sequence(uidlist),
operation + 'FLAGS',
imaputil.flagsmaildir2imap(flags))
if response[0] != 'OK':
raise OfflineImapError(
'Error with store: %s' % '. '.join(response[1]),
OfflineImapError.ERROR.MESSAGE)
response = response[1]
finally:
self.imapserver.releaseconnection(imapobj)
# Some IMAP servers do not always return a result. Therefore,
# only update the ones that it talks about, and manually fix
# the others.
needupdate = list(uidlist)
for result in response:
if result is None:
# Compensate for servers that don't return anything from
# STORE.
continue
attributehash = imaputil.flags2hash(imaputil.imapsplit(result)[1])
if not ('UID' in attributehash and 'FLAGS' in attributehash):
# Compensate for servers that don't return a UID attribute.
continue
flagstr = attributehash['FLAGS']
uid = int(attributehash['UID'])
self.messagelist[uid]['flags'] = imaputil.flagsimap2maildir(flagstr)
try:
needupdate.remove(uid)
except ValueError: # Let it slide if it's not in the list.
pass
for uid in needupdate:
if operation == '+':
self.messagelist[uid]['flags'] |= flags
elif operation == '-':
self.messagelist[uid]['flags'] -= flags
def __processmessagesflags(self, operation, uidlist, flags):
# Hack for those IMAP servers with a limited line length.
batch_size = 100
for i in range(0, len(uidlist), batch_size):
self.__processmessagesflags_real(operation,
uidlist[i:i + batch_size], flags)
return
# Interface from BaseFolder
def change_message_uid(self, uid, new_uid):
"""Change the message from existing uid to new_uid
If the backend supports it. IMAP does not and will throw errors."""
raise OfflineImapError('IMAP backend cannot change a messages UID from '
'%d to %d' %
(uid, new_uid), OfflineImapError.ERROR.MESSAGE)
# Interface from BaseFolder
def deletemessage(self, uid):
self.__deletemessages_noconvert([uid])
# Interface from BaseFolder
def deletemessages(self, uidlist):
self.__deletemessages_noconvert(uidlist)
def __deletemessages_noconvert(self, uidlist):
if not len(uidlist):
return
self.__addmessagesflags_noconvert(uidlist, set('T'))
imapobj = self.imapserver.acquireconnection()
try:
try:
imapobj.select(self.getfullIMAPname())
except imapobj.readonly:
self.ui.deletereadonly(self, uidlist)
return
if self.expunge:
assert (imapobj.expunge()[0] == 'OK')
finally:
self.imapserver.releaseconnection(imapobj)
for uid in uidlist:
del self.messagelist[uid]
|