comparison src/plugins/plugin_misc_maildir.py @ 439:866dbb0d7d87

plugin maildir: maildir now use PersistentBinaryDictionary to store profile specific data /!\ profile needs to be connected for maildir/imap/smtp plugins to work, this has to be fixed
author Goffi <goffi@goffi.org>
date Sat, 03 Dec 2011 15:50:09 +0100
parents efbfccfed623
children 1c1d1a4994c4
comparison
equal deleted inserted replaced
438:62145e50eae5 439:866dbb0d7d87
32 import os,os.path 32 import os,os.path
33 from cStringIO import StringIO 33 from cStringIO import StringIO
34 from twisted.internet import reactor 34 from twisted.internet import reactor
35 import pdb 35 import pdb
36 from sat.exceptions import * 36 from sat.exceptions import *
37 from sat.memory.persistent import PersistentBinaryDict
37 38
38 39
39 from zope.interface import implements 40 from zope.interface import implements
40 41
41 42
60 def __init__(self, host): 61 def __init__(self, host):
61 info(_("Plugin Maildir initialization")) 62 info(_("Plugin Maildir initialization"))
62 self.host = host 63 self.host = host
63 64
64 self.__observed={} 65 self.__observed={}
66 self.data={} #list of profile spectific data. key = profile, value = PersistentBinaryDict where key=mailbox name,
67 # and value is a dictionnary with the following value
68 # - cur_idx: value of the current unique integer increment (UID)
69 # - message_id (as returned by MaildirMailbox): a tuple of (UID, [flag1, flag2, ...])
65 pList=host.memory.getProfilesList #shorter :) 70 pList=host.memory.getProfilesList #shorter :)
66 self.__mailboxes=dict(zip(pList(),len(pList())*[{}])) 71 self.__mailboxes={} #key: profile, value: {boxname: MailboxUser instance}
67 self.data=host.memory.getPrivate("MAILDIR_data") or dict(zip(pList(),len(pList())*[{"INBOX":{"cur_idx":0}}])) #Create empty box for each profile
68 #a value in the dictionnary for a mailbox is a dictionnary with the following value
69 # - cur_idx: value of the current unique integer increment (UID)
70 # - message_id (as returned by MaildirMailbox): a tuple of (UID, [flag1, flag2, ...])
71 72
72 #the triggers 73 #the triggers
73 host.trigger.add("MessageReceived", self.messageReceivedTrigger) 74 host.trigger.add("MessageReceived", self.messageReceivedTrigger)
74 host.trigger.add("ProfileCreation", self.newProfileTrigger) 75
75 76 def profileConnected(self, profile):
76 def __del__(self): 77 """Called on profile connection, create profile data"""
77 debug('Destroying MaildirBox') 78 self.data[profile] = PersistentBinaryDict("plugin_maildir", profile)
78 self.host.memory.setPrivate('MAILDIR_data',self.data) 79 self.__mailboxes[profile]={}
80 def dataLoaded(ignore):
81 if not self.data[profile]:
82 #the mailbox is new, we initiate the data
83 self.data[profile]["INBOX"] = {"cur_idx":0}
84 self.data[profile].load().addCallback(dataLoaded)
85
86 def profileDisconnected(self, profile):
87 """Called on profile disconnection, free profile's resources"""
88 del self.__mailboxes[profile]
89 del self.data[profile]
79 90
80 def messageReceivedTrigger(self, message, profile): 91 def messageReceivedTrigger(self, message, profile):
81 """This trigger catch normal message and put the in the Maildir box. 92 """This trigger catch normal message and put the in the Maildir box.
82 If the message is not of "normal" type, do nothing 93 If the message is not of "normal" type, do nothing
83 @param message: message xmlstrem 94 @param message: message xmlstrem
87 mess_type = message['type'] if message.hasAttribute('type') else 'normal' 98 mess_type = message['type'] if message.hasAttribute('type') else 'normal'
88 if mess_type != 'normal': 99 if mess_type != 'normal':
89 return True 100 return True
90 self.accessMessageBox("INBOX", profile_key=profile).addMessage(message) 101 self.accessMessageBox("INBOX", profile_key=profile).addMessage(message)
91 return False 102 return False
92
93 def newProfileTrigger(self, profile):
94 """This trigger create necessary to manage mailbox for every new profile created"""
95 self.__mailboxes[profile] = {}
96 self.data[profile]={"INBOX":{"cur_idx":0}}
97 self.host.memory.setPrivate('MAILDIR_data',self.data)
98 return True
99 103
100 def accessMessageBox(self, boxname, observer=None, profile_key='@DEFAULT@'): 104 def accessMessageBox(self, boxname, observer=None, profile_key='@DEFAULT@'):
101 """Create and return a MailboxUser instance 105 """Create and return a MailboxUser instance
102 @param boxname: name of the box 106 @param boxname: name of the box
103 @param observer: method to call when a NewMessage arrive""" 107 @param observer: method to call when a NewMessage arrive"""