comparison src/plugins/plugin_misc_imap.py @ 255:55b750017b71

plugin IMAP, plugin Maildir: added flag, IMAP's uid management
author Goffi <goffi@goffi.org>
date Mon, 17 Jan 2011 21:26:16 +0100
parents 9fc32d1d9046
children 012c38b56cdd
comparison
equal deleted inserted replaced
254:9fc32d1d9046 255:55b750017b71
73 reactor.listenTCP(port, self.server_factory) 73 reactor.listenTCP(port, self.server_factory)
74 74
75 class Message(): 75 class Message():
76 implements(imap4.IMessage) 76 implements(imap4.IMessage)
77 77
78 def __init__(self, uid, mess_fp): 78 def __init__(self, uid, flags, mess_fp):
79 debug('Message Init') 79 debug('Message Init')
80 self.uid=uid 80 self.uid=uid
81 self.flags=flags
81 self.mess_fp=mess_fp 82 self.mess_fp=mess_fp
82 self.message=Parser().parse(mess_fp) 83 self.message=Parser().parse(mess_fp)
83 84
84 def getUID(self): 85 def getUID(self):
85 """Retrieve the unique identifier associated with this message. 86 """Retrieve the unique identifier associated with this message.
91 def getFlags(self): 92 def getFlags(self):
92 """Retrieve the flags associated with this message. 93 """Retrieve the flags associated with this message.
93 @return: The flags, represented as strings. 94 @return: The flags, represented as strings.
94 """ 95 """
95 debug('getFlags') 96 debug('getFlags')
96 return [] 97 return self.flags
97 98
98 def getInternalDate(self): 99 def getInternalDate(self):
99 """Retrieve the date internally associated with this message. 100 """Retrieve the date internally associated with this message.
100 @return: An RFC822-formatted date string. 101 @return: An RFC822-formatted date string.
101 """ 102 """
182 def getUID(self,message): 183 def getUID(self,message):
183 """Return the UID of a message in the mailbox 184 """Return the UID of a message in the mailbox
184 @param message: The message sequence number 185 @param message: The message sequence number
185 @return: The UID of the message. 186 @return: The UID of the message.
186 """ 187 """
187 debug ('getUID') 188 debug ('getUID (%i)' % message)
188 return self.mailbox.getUid(message) 189 #return self.mailbox.getUid(message-1) #XXX: it seems that this method get uid and not message sequence number
190 return message
189 191
190 def getMessageCount(self): 192 def getMessageCount(self):
191 """Return the number of messages in this mailbox. 193 """Return the number of messages in this mailbox.
192 """ 194 """
193 debug('getMessageCount') 195 debug('getMessageCount')
197 199
198 def getRecentCount(self): 200 def getRecentCount(self):
199 """Return the number of messages with the 'Recent' flag. 201 """Return the number of messages with the 'Recent' flag.
200 """ 202 """
201 debug('getRecentCount') 203 debug('getRecentCount')
202 return 0 204 return len(self.mailbox.getMessageIdsWithFlag('\\Recent'))
203 205
204 def getUnseenCount(self): 206 def getUnseenCount(self):
205 """Return the number of messages with the 'Unseen' flag. 207 """Return the number of messages with the 'Unseen' flag.
206 """ 208 """
207 debug('getUnseenCount') 209 debug('getUnseenCount')
208 return 1 210 return self.getMessageCount()-len(self.mailbox.getMessageIdsWithFlag('\\Seen'))
209 211
210 def isWriteable(self): 212 def isWriteable(self):
211 """Get the read/write status of the mailbox. 213 """Get the read/write status of the mailbox.
212 @return: A true value if write permission is allowed, a false value otherwise. 214 @return: A true value if write permission is allowed, a false value otherwise.
213 """ 215 """
275 """Remove all messages flagged \\Deleted. 277 """Remove all messages flagged \\Deleted.
276 @return: The list of message sequence numbers which were deleted, 278 @return: The list of message sequence numbers which were deleted,
277 or a Deferred whose callback will be invoked with such a list. 279 or a Deferred whose callback will be invoked with such a list.
278 """ 280 """
279 debug('expunge') 281 debug('expunge')
280 raise NotImplementedError 282 self.mailbox.removeDeleted()
281 283
282 def fetch(self, messages, uid): 284 def fetch(self, messages, uid):
283 """Retrieve one or more messages. 285 """Retrieve one or more messages.
284 @param messages: The identifiers of messages to retrieve information 286 @param messages: The identifiers of messages to retrieve information
285 about 287 about
292 for mess_uid in messages: 294 for mess_uid in messages:
293 if mess_uid == None: 295 if mess_uid == None:
294 debug ('stopping iteration') 296 debug ('stopping iteration')
295 raise StopIteration 297 raise StopIteration
296 try: 298 try:
297 debug ('yielding (%s,%s)' % (mess_uid,Message(mess_uid,self.mailbox.getMessageUid(mess_uid)))) 299 yield (mess_uid,Message(mess_uid,self.mailbox.getFlagsUid(mess_uid), self.mailbox.getMessageUid(mess_uid)))
298 yield (mess_uid,Message(mess_uid,self.mailbox.getMessageUid(mess_uid)))
299 except IndexError: 300 except IndexError:
300 continue 301 continue
301 else: 302 else:
302 messages.last = self.getMessageCount() 303 messages.last = self.getMessageCount()
303 for mess_idx in messages: 304 for mess_idx in messages:
304 if mess_idx>self.getMessageCount(): 305 if mess_idx>self.getMessageCount():
305 raise StopIteration 306 raise StopIteration
306 yield (mess_idx,Message(mess_idx,self.mailbox.getMessage(mess_idx-1))) 307 yield (mess_idx,Message(mess_idx,self.mailbox.getFlags(mess_idx),self.mailbox.getMessage(mess_idx-1)))
307 308
308 def store(self, messages, flags, mode, uid): 309 def store(self, messages, flags, mode, uid):
309 """Set the flags of one or more messages. 310 """Set the flags of one or more messages.
310 @param messages: The identifiers of the messages to set the flags of. 311 @param messages: The identifiers of the messages to set the flags of.
311 @param flags: The flags to set, unset, or add. 312 @param flags: The flags to set, unset, or add.
319 representing the flags set on the message after this operation has 320 representing the flags set on the message after this operation has
320 been performed, or a Deferred whose callback will be invoked with 321 been performed, or a Deferred whose callback will be invoked with
321 such a dict. 322 such a dict.
322 """ 323 """
323 debug('store') 324 debug('store')
324 raise NotImplementedError 325
326 def updateFlags(getF,setF):
327 ret = {}
328 for mess_id in messages:
329 if (uid and mess_id == None) or (not uid and mess_id>self.getMessageCount()):
330 break
331 _flags=set(getF(mess_id) if mode else [])
332 if mode==-1:
333 _flags.difference_update(set(flags))
334 else:
335 _flags.update(set(flags))
336 new_flags=list(_flags)
337 setF(mess_id, new_flags)
338 ret[mess_id] = new_flags
339 return ret
340
341 if uid:
342 messages.last = self.mailbox.getMaxUid()
343 messages.getnext = self.mailbox.getNextExistingUid
344 return updateFlags(self.mailbox.getFlagsUid,self.mailbox.setFlagsUid)
345 else:
346 messages.last = self.getMessageCount()
347 return updateFlags(self.mailbox.getFlags,self.mailbox.setFlags)
325 348
326 def getFlags(self): 349 def getFlags(self):
327 """Return the flags defined in this mailbox 350 """Return the flags defined in this mailbox
328 Flags with the \\ prefix are reserved for use as system flags. 351 Flags with the \\ prefix are reserved for use as system flags.
329 @return: A list of the flags that can be set on messages in this mailbox. 352 @return: A list of the flags that can be set on messages in this mailbox.
330 """ 353 """
331 debug('getFlags') 354 debug('getFlags')
332 #return ['\Seen','\Answered','\Flagged','\Deleted','\Draft', '\Recent'] 355 return ['\Seen','\Answered','\Flagged','\Deleted','\Draft'] #TODO: add '\Recent'
333 return []
334 356
335 def getHierarchicalDelimiter(self): 357 def getHierarchicalDelimiter(self):
336 """Get the character which delimits namespaces for in this mailbox. 358 """Get the character which delimits namespaces for in this mailbox.
337 """ 359 """
338 debug('getHierarchicalDelimiter') 360 debug('getHierarchicalDelimiter')