]> code.delx.au - offlineimap/blob - head/offlineimap/folder/IMAP.py
52e6c0404ce3b104a96b9e05b494c03a4b1c6f84
[offlineimap] / head / offlineimap / folder / IMAP.py
1 # IMAP folder support
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 from Base import BaseFolder
20 from imapsync import imaputil
21 import rfc822
22 from StringIO import StringIO
23
24 class IMAPFolder(BaseFolder):
25 def __init__(self, imapserver, name):
26 self.name = imaputil.dequote(name)
27 self.root = imapserver.root
28 self.sep = imapserver.delim
29 self.imapserver = imapserver
30 self.imapobj = self.imapserver.makeconnection()
31 self.messagelist = None
32
33 def getuidvalidity(self):
34 x = self.imapobj.status(self.getfullname(), ('UIDVALIDITY'))[1][0]
35 uidstring = imaputil.imapsplit(x)[1]
36 return long(imaputil.flagsplit(uidstring)[1])
37
38 def cachemessagelist(self):
39 assert(self.imapobj.select(self.getfullname())[0] == 'OK')
40 self.messagelist = {}
41 response = self.imapobj.status(self.getfullname(), ('MESSAGES'))[1][0]
42 result = imaputil.imapsplit(response)[1]
43 maxmsgid = long(imaputil.flags2hash(result)['MESSAGES'])
44 if (maxmsgid < 1):
45 # No messages? return.
46 return
47
48 # Now, get the flags and UIDs for these.
49 response = self.imapobj.fetch('1:%d' % maxmsgid, '(FLAGS UID)')[1]
50 for messagestr in response:
51 # Discard the message number.
52 messagestr = imaputil.imapsplit(messagestr)[1]
53 options = imaputil.flags2hash(messagestr)
54 uid = long(options['UID'])
55 flags = imaputil.flagsimap2maildir(options['FLAGS'])
56 self.messagelist[uid] = {'uid': uid, 'flags': flags}
57
58 def getmessagelist(self):
59 return self.messagelist
60
61 def getmessage(self, uid):
62 print "***************** GETMESSAGE %d" % uid
63 assert(self.imapobj.select(self.getfullname())[0] == 'OK')
64 return self.imapobj.uid('fetch', '%d' % uid, '(RFC822)')[1][0][1]
65
66 def getmessageflags(self, uid):
67 return self.getmessagelist()[uid]['flags']
68
69 def savemessage(self, uid, content, flags):
70 # This backend always assigns a new uid, so the uid arg is ignored.
71
72 # In order to get the new uid, we need to save off the message ID.
73
74 message = rfc822.Message(StringIO(content))
75 mid = self.imapobj._quote(message.getheader('Message-Id'))
76 date = imaplib.Time2Internaldate(rfc822.parsedate(message.getheader('Date')))
77
78 assert(self.imapobj.append(self.getfullname(),
79 imaputil.flagsmaildir2imap(flags),
80 date, content)[0] == 'OK')
81
82 # Now find the UID it got.
83 matchinguids = self.imapobj.uid('search', None,
84 '(HEADER Message-Id %s)' % mid)[1][0]
85 matchinguids = matchinguids.split(' ')
86 matchinguids.sort()
87 uid = long(matchinguids[-1])
88 self.messagelist[uid] = {'uid': uid, 'flags': flags}
89 return uid
90
91 def savemessageflags(self, uid, flags):
92 assert(self.imapobj.select(self.getfullname())[0] == 'OK')
93 result = self.imapobj.uid('store', '%d' % uid, 'FLAGS',
94 imaputil.flagsmaildir2imap(flags))[1][0]
95 flags = imaputil.flags2hash(imaputil.imapsplit(result)[1])['FLAGS']
96 self.messagelist[uid]['flags'] = imaputil.flagsimap2maildir(flags)
97
98 def deletemessage(self, uid):
99 self.addmessagesflags(uid, ['T'])
100 assert(self.imapobj.select(self.getfullname())[0] == 'OK')
101 assert(self.imapobj.expunge()[0] == 'OK')
102
103