]>
code.delx.au - offlineimap/blob - head/offlineimap/folder/IMAP.py
52e6c0404ce3b104a96b9e05b494c03a4b1c6f84
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
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.
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.
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
19 from Base
import BaseFolder
20 from imapsync
import imaputil
22 from StringIO
import StringIO
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
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])
38 def cachemessagelist(self
):
39 assert(self
.imapobj
.select(self
.getfullname())[0] == 'OK')
41 response
= self
.imapobj
.status(self
.getfullname(), ('MESSAGES'))[1][0]
42 result
= imaputil
.imapsplit(response
)[1]
43 maxmsgid
= long(imaputil
.flags2hash(result
)['MESSAGES'])
45 # No messages? return.
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
}
58 def getmessagelist(self
):
59 return self
.messagelist
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]
66 def getmessageflags(self
, uid
):
67 return self
.getmessagelist()[uid
]['flags']
69 def savemessage(self
, uid
, content
, flags
):
70 # This backend always assigns a new uid, so the uid arg is ignored.
72 # In order to get the new uid, we need to save off the message ID.
74 message
= rfc822
.Message(StringIO(content
))
75 mid
= self
.imapobj
._quote
(message
.getheader('Message-Id'))
76 date
= imaplib
.Time2Internaldate(rfc822
.parsedate(message
.getheader('Date')))
78 assert(self
.imapobj
.append(self
.getfullname(),
79 imaputil
.flagsmaildir2imap(flags
),
80 date
, content
)[0] == 'OK')
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(' ')
87 uid
= long(matchinguids
[-1])
88 self
.messagelist
[uid
] = {'uid': uid
, 'flags': flags
}
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
)
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')