annotate src/plugins/plugin_misc_maildir.py @ 329:be9f682c53a5

QuickApp: autoplay deactivated in Tarot game
author Goffi <goffi@goffi.org>
date Mon, 23 May 2011 01:01:44 +0200
parents 27bc5d7732a3
children 6dcdc4cf8622
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
5 SàT plugin for managing Maildir type mail boxes
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
6 Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, error
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 import warnings
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 warnings.filterwarnings('ignore','the MimeWriter',DeprecationWarning,'twisted' ) #FIXME: to be removed, see http://twistedmatrix.com/trac/ticket/4038
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.internet import protocol
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.cred import portal,checkers
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.mail import imap4,maildir
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from email.parser import Parser
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30 import email.message
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from email.charset import Charset
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import os,os.path
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from cStringIO import StringIO
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34 from twisted.internet import reactor
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 import pdb
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
36 from sat.exceptions import *
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 from zope.interface import implements
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 PLUGIN_INFO = {
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43 "name": "Maildir Plugin",
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 "import_name": "Maildir",
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "type": "Misc",
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
46 "protocols": [],
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "dependencies": [],
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "main": "MaildirBox",
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "handler": "no",
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "description": _("""Intercept "normal" type messages, and put them in a Maildir type box""")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 }
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 MAILDIR_PATH = "Maildir"
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 class MaildirError(Exception):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 pass
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 class MaildirBox():
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __init__(self, host):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61 info(_("Plugin Maildir initialization"))
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 self.host = host
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.__observed={}
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
65 pList=host.memory.getProfilesList #shorter :)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
66 self.__mailboxes=dict(zip(pList(),len(pList())*[{}]))
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
67 self.data=host.memory.getPrivate("MAILDIR_data") or dict(zip(pList(),len(pList())*[{"INBOX":{"cur_idx":0}}])) #Create empty box for each profile
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
68 #a value in the dictionnary for a mailbox is a dictionnary with the following value
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
69 # - cur_idx: value of the current unique integer increment (UID)
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
70 # - message_id (as returned by MaildirMailbox): a tuple of (UID, [flag1, flag2, ...])
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71
259
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
72 #the triggers
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
73 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
74 host.trigger.add("ProfileCreation", self.newProfileTrigger)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
76 def __del__(self):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
77 debug('Destroying MaildirBox')
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
78 self.host.memory.setPrivate('MAILDIR_data',self.data)
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
79
259
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
80 def messageReceivedTrigger(self, message, profile):
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
81 """This trigger catch normal message and put the in the Maildir box.
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
82 If the message is not of "normal" type, do nothing
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
83 @param message: message xmlstrem
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
84 @return: False if it's a normal message, True else"""
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
85 for e in message.elements():
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
86 if e.name == "body":
264
27bc5d7732a3 core: fixed default message type
Goffi <goffi@goffi.org>
parents: 259
diff changeset
87 mess_type = message['type'] if message.hasAttribute('type') else 'normal'
27bc5d7732a3 core: fixed default message type
Goffi <goffi@goffi.org>
parents: 259
diff changeset
88 if mess_type != 'normal':
259
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
89 return True
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
90 self.accessMessageBox("INBOX", profile_key=profile).addMessage(message)
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
91 return False
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
92
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
93 def newProfileTrigger(self, profile):
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
94 """This trigger create necessary to manage mailbox for every new profile created"""
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
95 self.__mailboxes[profile] = {}
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
96 self.data[profile]={"INBOX":{"cur_idx":0}}
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
97 self.host.memory.setPrivate('MAILDIR_data',self.data)
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
98 return True
11f71187d5e4 core, plugin Maildir: added "ProfileCreation" Trigger
Goffi <goffi@goffi.org>
parents: 257
diff changeset
99
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
100 def accessMessageBox(self, boxname, observer=None, profile_key='@DEFAULT@'):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
101 """Create and return a MailboxUser instance
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 @param boxname: name of the box
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 @param observer: method to call when a NewMessage arrive"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
104 profile = self.host.memory.getProfileName(profile_key)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
105 if not profile:
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
106 raise ProfileUnknownError
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
107 if not self.__mailboxes[profile].has_key(boxname):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
108 self.__mailboxes[profile][boxname]=MailboxUser(self, boxname, observer, profile=profile)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
109 else:
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 if observer:
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
111 self.addObserver(observer, profile, boxname)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
112 return self.__mailboxes[profile][boxname]
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
114 def _getProfilePath(self, profile):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
115 """Return a unique path for profile's mailbox
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
116 The path must be unique, usable as a dir name, and bijectional"""
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
117 return profile.replace('/','_').replace('..','_') #FIXME: this is too naive to work well, must be improved
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
118
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
119 def _removeBoxAccess(self, boxname, mailboxUser, profile):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
120 """Remove a reference to a box
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
121 @param name: name of the box
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
122 @param mailboxUser: MailboxUser instance"""
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
123 if not self.__mailboxes.has_key(boxname):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
124 err_msg=_("Trying to remove an mailboxUser not referenced")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
125 error(_("INTERNAL ERROR: ") + err_msg)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
126 raise MaildirError(err_msg)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
127 assert self.__mailboxes[profile][boxname]==mailboxUser
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
128 del __mailboxes[profile][boxname]
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
129
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
130 def _checkBoxReference(self, boxname, profile):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
131 """Check if there is a reference on a box, and return it
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @param boxname: name of the box to check
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
133 @return: MailboxUser instance or None"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
134 if self.__mailboxes.has_key(profile):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
135 if self.__mailboxes[profile].has_key(boxname):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
136 return self.__mailboxes[profile][boxname]
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
137
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
138 def __getBoxData(self, boxname, profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
139 """Return the date of a box"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
140 try:
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
141 return self.data[profile][boxname] #the boxname MUST exist in the data
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
142 except KeyError:
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
143 err_msg=_("Boxname doesn't exist in internal data")
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
144 error(_("INTERNAL ERROR: ") + err_msg)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
145 raise MaildirError(err_msg)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
146
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
147 def getUid(self, boxname, message_id, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
148 """Return an unique integer, always ascending, for a message
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
149 This is mainly needed for the IMAP protocol
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
150 @param boxname: name of the box where the message is
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
151 @param message_id: unique id of the message as given by MaildirMailbox
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
152 @return: Integer UID"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
153 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
154 if box_data.has_key(message_id):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
155 ret = box_data[message_id][0]
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
156 else:
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
157 box_data['cur_idx']+=1
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
158 box_data[message_id]=[box_data['cur_idx'],[]]
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
159 ret = box_data[message_id]
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
160 self.host.memory.setPrivate('MAILDIR_data',self.data)
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
161 return ret
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
163 def getNextUid(self, boxname, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
164 """Return next unique integer that will generated
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
165 This is mainly needed for the IMAP protocol
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
166 @param boxname: name of the box where the message is
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
167 @return: Integer UID"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
168 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
169 return box_data['cur_idx']+1
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
170
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
171 def getNextExistingUid(self, boxname, uid, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
172 """Give the next uid of existing message
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
173 @param boxname: name of the box where the message is
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
174 @param uid: uid to start from
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
175 @return: uid or None if the is no more message"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
176 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
177 idx=uid+1
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
178 while self.getIdFromUid(boxname, idx, profile) == None: #TODO: this is highly inefficient because getIdfromUid is inefficient, fix this
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
179 idx+=1
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
180 if idx>box_data['cur_idx']:
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
181 return None
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
182 return idx
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
183
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
184 def getMaxUid(self, boxname, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
185 """Give the max existing uid
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
186 @param boxname: name of the box where the message is
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
187 @return: uid"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
188 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
189 return box_data['cur_idx']
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
190
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
191 def getIdFromUid(self, boxname, message_uid, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
192 """Return the message unique id from it's integer UID
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
193 @param boxname: name of the box where the message is
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
194 @param message_uid: unique integer identifier
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
195 @return: unique id of the message as given by MaildirMailbox or None if not found"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
196 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
197 for message_id in box_data.keys(): #TODO: this is highly inefficient on big mailbox, must be replaced in the future
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
198 if message_id == 'cur_idx':
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
199 continue
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
200 if box_data[message_id][0] == message_uid:
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
201 return message_id
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
202 return None
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
203
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
204 def getFlags(self, boxname, mess_id, profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
205 """Return the messages flags
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
206 @param boxname: name of the box where the message is
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
207 @param message_idx: message id as given by MaildirMailbox
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
208 @return: list of strings"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
209 box_data = self.__getBoxData(boxname, profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
210 if not box_data.has_key(mess_id):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
211 raise MailboxException("Trying to get flags from an unexisting message")
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
212 return box_data[mess_id][1]
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
213
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
214 def setFlags(self, boxname, mess_id, flags, profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
215 """Change the flags of the message
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
216 @param boxname: name of the box where the message is
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
217 @param message_idx: message id as given by MaildirMailbox
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
218 @param flags: list of strings
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
219 """
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
220 box_data = self.__getBoxData(boxname, profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
221 assert(type(flags)==list)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
222 flags=[flag.upper() for flag in flags] #we store every flag UPPERCASE
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
223 if not box_data.has_key(mess_id):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
224 raise MailboxException("Trying to set flags for an unexisting message")
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
225 box_data[mess_id][1]=flags
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
226 self.host.memory.setPrivate('MAILDIR_data',self.data)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
227
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
228 def getMessageIdsWithFlag(self, boxname, flag, profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
229 """Return ids of messages where a flag is set
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
230 @param boxname: name of the box where the message is
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
231 @param flag: flag to check
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
232 @return: list of id (as given by MaildirMailbox)"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
233 box_data = self.__getBoxData(boxname, profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
234 assert(isinstance(flag,basestring))
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
235 flag=flag.upper()
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
236 result = []
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
237 for key in box_data:
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
238 if key=='cur_idx':
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
239 continue
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
240 if flag in box_data[key][1]:
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
241 result.append(key)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
242 return result
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
243
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
244 def purgeDeleted(self, boxname, profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
245 """Remove data for messages with flag "\\Deleted"
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
246 @param boxname: name of the box where the message is
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
247 """
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
248 box_data = self.__getBoxData(boxname, profile)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
249 for mess_id in self.getMessageIdsWithFlag(boxname,"\\Deleted", profile):
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
250 del(box_data[mess_id])
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
251 self.host.memory.setPrivate('MAILDIR_data',self.data)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
252
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
253 def cleanTable(self, boxname, existant_id, profile):
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
254 """Remove mails which no longuer exist from the table
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
255 @param boxname: name of the box to clean
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
256 @param existant_id: list of id which actually exist"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
257 box_data = self.__getBoxData(boxname, profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
258 to_remove=[]
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
259 for key in box_data:
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
260 if key not in existant_id and key!="cur_idx":
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
261 to_remove.append(key)
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
262 for key in to_remove:
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
263 del box_data[key]
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
264
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
265 def addObserver(self, callback, profile, boxname, signal="NEW_MESSAGE"):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
266 """Add an observer for maildir box changes
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
267 @param callback: method to call when the the box is updated
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
268 @param boxname: name of the box to observe
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
269 @param signal: which signal is observed by the caller"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
270 if not self.__observed.has_key((profile,boxname)):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
271 self.__observed[(profile,boxname)]={}
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
272 if not self.__observed[(profile,boxname)].has_key(signal):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
273 self.__observed[(profile,boxname)][signal]=set()
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
274 self.__observed[(profile,boxname)][signal].add(callback)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
275
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
276 def removeObserver(self, callback, profile, boxname, signal="NEW_MESSAGE"):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
277 """Remove an observer of maildir box changes
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
278 @param callback: method to remove from obervers
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
279 @param boxname: name of the box which was observed
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
280 @param signal: which signal was observed by the caller"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
281 if not self.__observed.has_key((profile,boxname)):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
282 err_msg=_("Trying to remove an observer for an inexistant mailbox")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
283 error(_("INTERNAL ERROR: ") + err_msg)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
284 raise MaildirError(err_msg)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
285 if not self.__observed[(profile,boxname)].has_key(signal):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
286 err_msg=_("Trying to remove an inexistant observer, no observer for this signal")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
287 error(_("INTERNAL ERROR: ") + err_msg)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
288 raise MaildirError(err_msg)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
289 if not callback in self.__observed[(profile,boxname)][signal]:
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
290 err_msg=_("Trying to remove an inexistant observer")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
291 error(_("INTERNAL ERROR: ") + err_msg)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
292 raise MaildirError(err_msg)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
293 self.__observed[(profile,boxname)][signal].remove(callback)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
294
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
295 def emitSignal(self, profile, boxname, signal_name):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
296 """Emit the signal to observer"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
297 debug('emitSignal %s %s %s' %(profile, boxname, signal_name))
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
298 try:
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
299 for observer_cb in self.__observed[(profile, boxname)][signal_name]:
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
300 observer_cb()
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
301 except KeyError:
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
302 pass
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
303
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
304
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
305 class MailboxUser:
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
306 """This class is used to access a mailbox"""
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
307
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
308 def xmppMessage2mail(self, message):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
309 """Convert the XMPP's XML message to a basic rfc2822 message
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
310 @param xml: domish.Element of the message
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
311 @return: string email"""
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
312 mail = email.message.Message()
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
313 mail['MIME-Version'] = "1.0"
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
314 mail['Content-Type'] = "text/plain; charset=UTF-8; format=flowed"
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
315 mail['Content-Transfer-Encoding'] = "8bit"
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
316 mail['From'] = message['from'].encode('utf-8')
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
317 mail['To'] = message['to'].encode('utf-8')
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
318 mail['Date'] = email.utils.formatdate().encode('utf-8')
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
319 #TODO: save thread id
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
320 for e in message.elements():
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
321 if e.name == "body":
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
322 mail.set_payload(e.children[0].encode('utf-8'))
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
323 elif e.name == "subject":
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
324 mail['Subject'] = e.children[0].encode('utf-8')
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
325 return mail.as_string()
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
326
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
327 def __init__(self, _maildir, name, observer=None, profile="@NONE@"):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
328 """@param _maildir: the main MaildirBox instance
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
329 @param name: name of the mailbox
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
330 @param profile: real profile (ie not a profile_key)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
331 THIS OBJECT MUST NOT BE USED DIRECTLY: use MaildirBox.accessMessageBox instead"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
332 if _maildir._checkBoxReference(name, profile):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
333 error ("INTERNAL ERROR: MailboxUser MUST NOT be instancied directly")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
334 raise MailboxException('double MailboxUser instanciation')
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
335 if name!="INBOX":
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
336 raise NotImplementedError
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
337 self.name=name
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
338 self.profile=profile
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
339 self.maildir=_maildir
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
340 profile_path = self.maildir._getProfilePath(profile)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
341 full_profile_path = os.path.join(os.path.expanduser(self.maildir.host.get_const('local_dir')),profile_path)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
342 if not os.path.exists(full_profile_path):
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
343 os.makedirs(full_profile_path,0700)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
344 mailbox_path = os.path.join(full_profile_path, MAILDIR_PATH)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
345 self.mailbox_path=mailbox_path
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
346 self.mailbox = maildir.MaildirMailbox(mailbox_path)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
347 self.observer=observer
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
348 self.__uid_table_update()
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
349
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
350 if observer:
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
351 debug("adding observer for %s (%s)" % (name,profile))
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
352 self.maildir.addObserver(observer, profile, name, "NEW_MESSAGE")
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
353
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
354 def __uid_table_update(self):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
355 existant_id=[]
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
356 for mess_idx in range (self.getMessageCount()):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
357 #we update the uid table
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
358 existant_id.append(self.getId(mess_idx))
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
359 self.getUid(mess_idx)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
360 self.maildir.cleanTable(self.name, existant_id, profile=self.profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
361
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
362
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
363 def __del__(self):
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
364 if observer:
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
365 debug("removing observer for %s" % self.name)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
366 self._maildir.removeObserver(observer, self.name, "NEW_MESSAGE")
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
367 self.maildir._removeBoxAccess(self.name, self, profile=self.profile)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
368
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
369 def addMessage(self, message):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
370 """Add a message to the box
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
371 @param message: XMPP XML message"""
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
372 self.mailbox.appendMessage(self.xmppMessage2mail(message)).addCallback(self.emitSignal, "NEW_MESSAGE")
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
373
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
374 def emitSignal(self, ignore, signal):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
375 """Emit the signal to the observers"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
376 if signal=="NEW_MESSAGE":
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
377 self.getUid(self.getMessageCount()-1) #XXX: we make an uid for the last message added
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
378 self.maildir.emitSignal(self.profile, self.name, signal)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
379
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
380 def getId(self, mess_idx):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
381 """Return the Unique ID of the message
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
382 @mess_idx: message index"""
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
383 return self.mailbox.getUidl(mess_idx)
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
384
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
385 def getUid(self, mess_idx):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
386 """Return a unique interger id for the message, always ascending"""
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
387 mess_id=self.getId(mess_idx)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
388 return self.maildir.getUid(self.name,mess_id, profile=self.profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
389
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
390 def getNextUid(self):
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
391 return self.maildir.getNextUid(self.name, profile=self.profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
392
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
393 def getNextExistingUid(self, uid):
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
394 return self.maildir.getNextExistingUid(self.name, uid, profile=self.profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
395
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
396 def getMaxUid(self):
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
397 return self.maildir.getMaxUid(self.name, profile=self.profile)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
398
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
399 def getMessageCount(self):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
400 """Return number of mails present in this box"""
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
401 return len(self.mailbox.list)
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
402
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
403 def getMessageIdx(self, mess_idx):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
404 """Return the full message
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
405 @mess_idx: message index"""
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
406 return self.mailbox.getMessage(mess_idx)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
407
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
408 def getIdxFromUid(self, mess_uid):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
409 """Return the message index from the uid
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
410 @param mess_uid: message unique identifier
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
411 @return: message index, as managed by MaildirMailbox"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
412 for mess_idx in range (self.getMessageCount()):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
413 if self.getUid(mess_idx) == mess_uid:
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
414 return mess_idx
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
415 raise IndexError
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
416
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
417 def getIdxFromId(self, mess_id):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
418 """Return the message index from the unique index
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
419 @param mess_id: message unique index as given by MaildirMailbox
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
420 @return: message sequence index"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
421 for mess_idx in range (self.getMessageCount()):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
422 if self.mailbox.getUidl(mess_idx) == mess_id:
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
423 return mess_idx
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
424 raise IndexError
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
425
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
426 def getMessage(self, mess_idx):
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
427 """Return the full message
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
428 @param mess_idx: message index"""
253
f45ffbf211e9 MAILDIR + IMAP plugins: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
429 return self.mailbox.getMessage(mess_idx)
254
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
430
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
431 def getMessageUid(self, mess_uid):
9fc32d1d9046 Plugin IMAP, plugin MAILDIR: added IMAP's UID management, mailbox data persistence
Goffi <goffi@goffi.org>
parents: 253
diff changeset
432 """Return the full message
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
433 @param mess_idx: message unique identifier"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
434 return self.mailbox.getMessage(self.getIdxFromUid(mess_uid))
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
435
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
436 def getFlags(self, mess_idx):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
437 """Return the flags of the message
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
438 @param mess_idx: message index
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
439 @return: list of strings"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
440 id = self.getId(mess_idx)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
441 return self.maildir.getFlags(self.name, id, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
442
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
443 def getFlagsUid(self, mess_uid):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
444 """Return the flags of the message
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
445 @param mess_uid: message unique identifier
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
446 @return: list of strings"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
447 id = self.maildir.getIdFromUid(self.name,mess_uid, profile=self.profile)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
448 return self.maildir.getFlags(self.name, id, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
449
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
450 def setFlags(self, mess_idx, flags):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
451 """Change the flags of the message
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
452 @param mess_idx: message index
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
453 @param flags: list of strings
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
454 """
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
455 id = self.getId(mess_idx)
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
456 self.maildir.setFlags(self.name, id, flags, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
457
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
458 def setFlagsUid(self, mess_uid, flags):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
459 """Change the flags of the message
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
460 @param mess_uid: message unique identifier
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
461 @param flags: list of strings
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
462 """
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
463 id = self.maildir.getIdFromUid(self.name,mess_uid, profile=self.profile)
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
464 return self.maildir.setFlags(self.name, id, flags, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
465
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
466 def getMessageIdsWithFlag(self, flag):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
467 """Return ids of messages where a flag is set
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
468 @param flag: flag to check
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
469 @return: list of id (as given by MaildirMailbox)"""
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
470 return self.maildir.getMessageIdsWithFlag(self.name,flag, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
471
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
472 def removeDeleted(self):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
473 """Actually delete message flagged "\\Deleted"
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
474 Also purge the internal data of these messages
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
475 """
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
476 for mess_id in self.getMessageIdsWithFlag("\\Deleted"):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
477 print ("Deleting %s", mess_id)
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
478 self.mailbox.deleteMessage(self.getIdxFromId(mess_id))
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
479 self.mailbox = maildir.MaildirMailbox(self.mailbox_path) #We need to reparse the dir to have coherent indexing
257
012c38b56cdd plugin IMAP, plugin Maildir: profile management
Goffi <goffi@goffi.org>
parents: 255
diff changeset
480 self.maildir.purgeDeleted(self.name, profile=self.profile)
255
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
481
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
482 def emptyTrash(self):
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
483 """Delete everything in the .Trash dir"""
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
484 import shutils
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
485 pdb.set_trace()
55b750017b71 plugin IMAP, plugin Maildir: added flag, IMAP's uid management
Goffi <goffi@goffi.org>
parents: 254
diff changeset
486