Mercurial > libervia-backend
annotate src/plugins/plugin_misc_imap.py @ 258:aac82dee6091
Added a library for custom SàT Exceptions
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 18 Jan 2011 01:04:20 +0100 |
parents | 012c38b56cdd |
children | c8406fe5e81e |
rev | line source |
---|---|
253 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
5 SàT plugin for managing imap server |
253 | 6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org) |
7 | |
8 This program is free software: you can redistribute it and/or modify | |
9 it under the terms of the GNU General Public License as published by | |
10 the Free Software Foundation, either version 3 of the License, or | |
11 (at your option) any later version. | |
12 | |
13 This program is distributed in the hope that it will be useful, | |
14 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 GNU General Public License for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 """ | |
21 | |
22 from logging import debug, info, error | |
23 import warnings | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
24 from twisted.internet import protocol,defer |
253 | 25 from twisted.words.protocols.jabber import error as jab_error |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
26 from twisted.cred import portal,checkers,credentials |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
27 from twisted.cred import error as cred_error |
253 | 28 from twisted.mail import imap4 |
29 from email.parser import Parser | |
30 import email.message | |
31 import os,os.path | |
32 from cStringIO import StringIO | |
33 from twisted.internet import reactor | |
34 import pdb | |
35 | |
36 | |
37 from zope.interface import implements | |
38 | |
39 | |
40 PLUGIN_INFO = { | |
41 "name": "IMAP server Plugin", | |
42 "import_name": "IMAP", | |
43 "type": "Misc", | |
44 "protocols": [], | |
45 "dependencies": ["Maildir"], | |
46 "main": "IMAP_server", | |
47 "handler": "no", | |
48 "description": _("""Create an Imap server that you can use to read your "normal" type messages""") | |
49 } | |
50 | |
51 class IMAP_server(): | |
52 | |
53 params = """ | |
54 <params> | |
55 <general> | |
56 <category name="IMAP Server"> | |
57 <param name="Port" value="10143" type="string" /> | |
58 </category> | |
59 </general> | |
60 </params> | |
61 """ | |
62 | |
63 def __init__(self, host): | |
64 info(_("Plugin Imap Server initialization")) | |
65 self.host = host | |
66 | |
67 #parameters | |
68 host.memory.importParams(self.params) | |
69 | |
70 port = int(self.host.memory.getParamA("Port", "IMAP Server")) | |
71 info(_("Launching IMAP server on port %d"), port) | |
72 | |
73 self.server_factory = ImapServerFactory(self.host) | |
74 reactor.listenTCP(port, self.server_factory) | |
75 | |
76 class Message(): | |
77 implements(imap4.IMessage) | |
78 | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
79 def __init__(self, uid, flags, mess_fp): |
253 | 80 debug('Message Init') |
81 self.uid=uid | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
82 self.flags=flags |
253 | 83 self.mess_fp=mess_fp |
84 self.message=Parser().parse(mess_fp) | |
85 | |
86 def getUID(self): | |
87 """Retrieve the unique identifier associated with this message. | |
88 """ | |
89 debug('getUID (message)') | |
90 return self.uid | |
91 | |
92 def getFlags(self): | |
93 """Retrieve the flags associated with this message. | |
94 @return: The flags, represented as strings. | |
95 """ | |
96 debug('getFlags') | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
97 return self.flags |
253 | 98 |
99 def getInternalDate(self): | |
100 """Retrieve the date internally associated with this message. | |
101 @return: An RFC822-formatted date string. | |
102 """ | |
103 debug('getInternalDate') | |
104 return self.message['Date'] | |
105 | |
106 | |
107 def getHeaders(self, negate, *names): | |
108 """Retrieve a group of message headers. | |
109 @param names: The names of the headers to retrieve or omit. | |
110 @param negate: If True, indicates that the headers listed in names | |
111 should be omitted from the return value, rather than included. | |
112 @return: A mapping of header field names to header field values | |
113 """ | |
114 debug('getHeaders %s - %s' % (negate, names)) | |
115 final_dict={} | |
116 to_check=[name.lower() for name in names] | |
117 for header in self.message.keys(): | |
118 if (negate and not header.lower() in to_check) or \ | |
119 (not negate and header.lower() in to_check): | |
120 final_dict[header]=self.message[header] | |
121 return final_dict | |
122 | |
123 def getBodyFile(self): | |
124 """Retrieve a file object containing only the body of this message. | |
125 """ | |
126 debug('getBodyFile') | |
127 return StringIO(self.message.get_payload()) | |
128 | |
129 def getSize(self): | |
130 """Retrieve the total size, in octets, of this message. | |
131 """ | |
132 debug('getSize') | |
133 self.mess_fp.seek(0,os.SEEK_END) | |
134 return self.mess_fp.tell() | |
135 | |
136 | |
137 def isMultipart(self): | |
138 """Indicate whether this message has subparts. | |
139 """ | |
140 debug('isMultipart') | |
141 return False | |
142 | |
143 def getSubPart(self,part): | |
144 """Retrieve a MIME sub-message | |
145 @param part: The number of the part to retrieve, indexed from 0. | |
146 @return: The specified sub-part. | |
147 """ | |
148 debug('getSubPart') | |
149 return TypeError | |
150 | |
151 | |
152 class SatMailbox: | |
153 implements(imap4.IMailbox) | |
154 | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
155 def __init__(self,host,name,profile): |
253 | 156 self.host = host |
157 self.listeners=set() | |
158 debug ('Mailbox init (%s)', name) | |
159 if name!="INBOX": | |
160 raise imap4.MailboxException("Only INBOX is managed for the moment") | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
161 self.mailbox=self.host.plugins["Maildir"].accessMessageBox(name,self.newMessage, profile) |
253 | 162 |
163 def newMessage(self): | |
164 """Called when a new message is in the mailbox""" | |
165 debug ("newMessage signal received") | |
166 nb_messages=self.getMessageCount() | |
167 for listener in self.listeners: | |
168 listener.newMessages(nb_messages,None) | |
169 | |
170 def getUIDValidity(self): | |
171 """Return the unique validity identifier for this mailbox. | |
172 """ | |
173 debug ('getUIDValidity') | |
174 return 0 | |
175 | |
176 def getUIDNext(self): | |
177 """Return the likely UID for the next message added to this mailbox. | |
178 """ | |
179 debug ('getUIDNext') | |
254
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
180 return self.mailbox.getNextUid() |
253 | 181 |
182 def getUID(self,message): | |
183 """Return the UID of a message in the mailbox | |
184 @param message: The message sequence number | |
185 @return: The UID of the message. | |
186 """ | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
187 debug ('getUID (%i)' % message) |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
188 #return self.mailbox.getUid(message-1) #XXX: it seems that this method get uid and not message sequence number |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
189 return message |
253 | 190 |
191 def getMessageCount(self): | |
192 """Return the number of messages in this mailbox. | |
193 """ | |
194 debug('getMessageCount') | |
195 ret = self.mailbox.getMessageCount() | |
196 debug("count = %i" % ret) | |
197 return ret | |
198 | |
199 def getRecentCount(self): | |
200 """Return the number of messages with the 'Recent' flag. | |
201 """ | |
202 debug('getRecentCount') | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
203 return len(self.mailbox.getMessageIdsWithFlag('\\Recent')) |
253 | 204 |
205 def getUnseenCount(self): | |
206 """Return the number of messages with the 'Unseen' flag. | |
207 """ | |
208 debug('getUnseenCount') | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
209 return self.getMessageCount()-len(self.mailbox.getMessageIdsWithFlag('\\SEEN')) |
253 | 210 |
211 def isWriteable(self): | |
212 """Get the read/write status of the mailbox. | |
213 @return: A true value if write permission is allowed, a false value otherwise. | |
214 """ | |
215 debug('isWriteable') | |
216 return True | |
217 | |
218 def destroy(self): | |
219 """Called before this mailbox is deleted, permanently. | |
220 """ | |
221 debug('destroy') | |
222 | |
223 | |
224 def requestStatus(self, names): | |
225 """Return status information about this mailbox. | |
226 @param names: The status names to return information regarding. | |
227 The possible values for each name are: MESSAGES, RECENT, UIDNEXT, | |
228 UIDVALIDITY, UNSEEN. | |
229 @return: A dictionary containing status information about the | |
230 requested names is returned. If the process of looking this | |
231 information up would be costly, a deferred whose callback will | |
232 eventually be passed this dictionary is returned instead. | |
233 """ | |
234 debug('requestStatus') | |
235 return imap4.statusRequestHelper(self, names) | |
236 | |
237 def addListener(self, listener): | |
238 """Add a mailbox change listener | |
239 | |
240 @type listener: Any object which implements C{IMailboxListener} | |
241 @param listener: An object to add to the set of those which will | |
242 be notified when the contents of this mailbox change. | |
243 """ | |
244 debug('addListener %s' % listener) | |
245 self.listeners.add(listener) | |
246 | |
247 def removeListener(self, listener): | |
248 """Remove a mailbox change listener | |
249 | |
250 @type listener: Any object previously added to and not removed from | |
251 this mailbox as a listener. | |
252 @param listener: The object to remove from the set of listeners. | |
253 | |
254 @raise ValueError: Raised when the given object is not a listener for | |
255 this mailbox. | |
256 """ | |
257 debug('removeListener') | |
258 if listener in self.listeners: | |
259 self.listeners.remove(listener) | |
260 else: | |
261 raise imap4.MailboxException('Trying to remove an unknown listener') | |
262 | |
263 def addMessage(self, message, flags = (), date = None): | |
264 """Add the given message to this mailbox. | |
265 @param message: The RFC822 formatted message | |
266 @param flags: The flags to associate with this message | |
267 @param date: If specified, the date to associate with this | |
268 @return: A deferred whose callback is invoked with the message | |
269 id if the message is added successfully and whose errback is | |
270 invoked otherwise. | |
271 """ | |
272 debug('addMessage') | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
273 raise imap4.MailboxException("Client message addition not implemented yet") |
253 | 274 |
275 def expunge(self): | |
276 """Remove all messages flagged \\Deleted. | |
277 @return: The list of message sequence numbers which were deleted, | |
278 or a Deferred whose callback will be invoked with such a list. | |
279 """ | |
280 debug('expunge') | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
281 self.mailbox.removeDeleted() |
253 | 282 |
283 def fetch(self, messages, uid): | |
284 """Retrieve one or more messages. | |
285 @param messages: The identifiers of messages to retrieve information | |
286 about | |
287 @param uid: If true, the IDs specified in the query are UIDs; | |
288 """ | |
289 debug('fetch (%s, %s)'%(messages,uid)) | |
254
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
290 if uid: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
291 messages.last = self.mailbox.getMaxUid() |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
292 messages.getnext = self.mailbox.getNextExistingUid |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
293 for mess_uid in messages: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
294 if mess_uid == None: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
295 debug ('stopping iteration') |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
296 raise StopIteration |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
297 try: |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
298 yield (mess_uid,Message(mess_uid,self.mailbox.getFlagsUid(mess_uid), self.mailbox.getMessageUid(mess_uid))) |
254
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
299 except IndexError: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
300 continue |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
301 else: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
302 messages.last = self.getMessageCount() |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
303 for mess_idx in messages: |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
304 if mess_idx>self.getMessageCount(): |
9fc32d1d9046
Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents:
253
diff
changeset
|
305 raise StopIteration |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
306 yield (mess_idx,Message(mess_idx,self.mailbox.getFlags(mess_idx),self.mailbox.getMessage(mess_idx-1))) |
253 | 307 |
308 def store(self, messages, flags, mode, uid): | |
309 """Set the flags of one or more messages. | |
310 @param messages: The identifiers of the messages to set the flags of. | |
311 @param flags: The flags to set, unset, or add. | |
312 @param mode: If mode is -1, these flags should be removed from the | |
313 specified messages. If mode is 1, these flags should be added to | |
314 the specified messages. If mode is 0, all existing flags should be | |
315 cleared and these flags should be added. | |
316 @param uid: If true, the IDs specified in the query are UIDs; | |
317 otherwise they are message sequence IDs. | |
318 @return: A dict mapping message sequence numbers to sequences of str | |
319 representing the flags set on the message after this operation has | |
320 been performed, or a Deferred whose callback will be invoked with | |
321 such a dict. | |
322 """ | |
323 debug('store') | |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
324 |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
325 flags=[flag.upper() for flag in flags] |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
326 |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
327 def updateFlags(getF,setF): |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
328 ret = {} |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
329 for mess_id in messages: |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
330 if (uid and mess_id == None) or (not uid and mess_id>self.getMessageCount()): |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
331 break |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
332 _flags=set(getF(mess_id) if mode else []) |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
333 if mode==-1: |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
334 _flags.difference_update(set(flags)) |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
335 else: |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
336 _flags.update(set(flags)) |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
337 new_flags=list(_flags) |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
338 setF(mess_id, new_flags) |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
339 ret[mess_id] = tuple(new_flags) |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
340 return ret |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
341 |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
342 if uid: |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
343 messages.last = self.mailbox.getMaxUid() |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
344 messages.getnext = self.mailbox.getNextExistingUid |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
345 ret = updateFlags(self.mailbox.getFlagsUid,self.mailbox.setFlagsUid) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
346 for listener in self.listeners: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
347 listener.flagsChanged(ret) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
348 return ret |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
349 |
255
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
350 else: |
55b750017b71
plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents:
254
diff
changeset
|
351 messages.last = self.getMessageCount() |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
352 ret = updateFlags(self.mailbox.getFlags,self.mailbox.setFlags) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
353 newFlags={} |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
354 for idx in ret: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
355 #we have to convert idx to uid for the listeners |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
356 newFlags[self.mailbox.getUid(idx)] = ret[idx] |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
357 for listener in self.listeners: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
358 listener.flagsChanged(newFlags) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
359 return ret |
253 | 360 |
361 def getFlags(self): | |
362 """Return the flags defined in this mailbox | |
363 Flags with the \\ prefix are reserved for use as system flags. | |
364 @return: A list of the flags that can be set on messages in this mailbox. | |
365 """ | |
366 debug('getFlags') | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
367 return ['\\SEEN','\\ANSWERED','\\FLAGGED','\\DELETED','\\DRAFT'] #TODO: add '\\RECENT' |
253 | 368 |
369 def getHierarchicalDelimiter(self): | |
370 """Get the character which delimits namespaces for in this mailbox. | |
371 """ | |
372 debug('getHierarchicalDelimiter') | |
373 return '.' | |
374 | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
375 class ImapSatAccount(imap4.MemoryAccount): |
253 | 376 #implements(imap4.IAccount) |
377 | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
378 def __init__(self, host, profile): |
253 | 379 debug("ImapAccount init") |
380 self.host=host | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
381 self.profile=profile |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
382 imap4.MemoryAccount.__init__(self,profile) |
253 | 383 self.addMailbox("Inbox") #We only manage Inbox for the moment |
384 debug ('INBOX added') | |
385 | |
386 def _emptyMailbox(self, name, id): | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
387 return SatMailbox(self.host,name,self.profile) |
253 | 388 |
389 | |
390 class ImapRealm: | |
391 implements(portal.IRealm) | |
392 | |
393 def __init__(self,host): | |
394 self.host = host | |
395 | |
396 def requestAvatar(self, avatarID, mind, *interfaces): | |
397 debug('requestAvatar') | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
398 profile=avatarID.decode('utf-8') |
253 | 399 if imap4.IAccount not in interfaces: |
400 raise NotImplementedError | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
401 return imap4.IAccount, ImapSatAccount(self.host,profile), lambda:None |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
402 |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
403 class SatProfileCredentialChecker: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
404 """ |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
405 This credential checker check against SàT's profile and associated jabber's password |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
406 Check if the profile exists, and if the password is OK |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
407 Return the profile as avatarId |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
408 """ |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
409 implements(checkers.ICredentialsChecker) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
410 credentialInterfaces = (credentials.IUsernamePassword, |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
411 credentials.IUsernameHashedPassword) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
412 |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
413 |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
414 def __init__(self, host): |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
415 self.host = host |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
416 |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
417 def _cbPasswordMatch(self, matched, profile): |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
418 if matched: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
419 return profile.encode('utf-8') |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
420 else: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
421 return failure.Failure(cred_error.UnauthorizedLogin()) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
422 |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
423 def requestAvatarId(self, credentials): |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
424 profiles = self.host.memory.getProfilesList() |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
425 if not credentials.username in profiles: |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
426 return defer.fail(cred_error.UnauthorizedLogin()) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
427 password = self.host.memory.getParamA("Password", "Connection", profile_key=credentials.username) |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
428 return defer.maybeDeferred( |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
429 credentials.checkPassword, |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
430 password).addCallback( |
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
431 self._cbPasswordMatch, credentials.username) |
253 | 432 |
433 class ImapServerFactory(protocol.ServerFactory): | |
434 protocol = imap4.IMAP4Server | |
435 | |
436 def __init__(self, host): | |
437 self.host=host | |
438 | |
439 def startedConnecting(self, connector): | |
440 debug (_("IMAP server connection started")) | |
441 | |
442 def clientConnectionLost(self, connector, reason): | |
443 debug (_("IMAP server connection lost (reason: %s)"), reason) | |
444 | |
445 def buildProtocol(self, addr): | |
446 debug ("Building protocole") | |
447 prot = protocol.ServerFactory.buildProtocol(self, addr) | |
448 prot.portal = portal.Portal(ImapRealm(self.host)) | |
257
012c38b56cdd
plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents:
255
diff
changeset
|
449 prot.portal.registerChecker(SatProfileCredentialChecker(self.host)) |
253 | 450 return prot |