]> code.delx.au - offlineimap/blob - head/offlineimap/folder/Base.py
c6afcad251d610810a7c8797afd72b8a570831a5
[offlineimap] / head / offlineimap / folder / Base.py
1 # Base folder support
2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
19 class BaseFolder:
20 def getname(self):
21 """Returns name"""
22 return self.name
23
24 def getroot(self):
25 """Returns the root of the folder, in a folder-specific fashion."""
26 return self.root
27
28 def getsep(self):
29 """Returns the separator for this folder type."""
30 return self.sep
31
32 def getfullname(self):
33 if self.getroot():
34 return self.getroot() + self.getsep() + self.getname()
35 else:
36 return self.getname()
37
38 def isuidvalidityok(self, remotefolder):
39 raise NotImplementedException
40
41 def getuidvalidity(self):
42 raise NotImplementedException
43
44 def saveuidvalidity(self, newval):
45 raise NotImplementedException
46
47 def cachemessagelist(self):
48 """Reads the message list from disk or network and stores it in
49 memory for later use. This list will not be re-read from disk or
50 memory unless this function is called again."""
51 raise NotImplementedException
52
53 def getmessagelist(self):
54 """Gets the current message list.
55 You must call cachemessagelist() before calling this function!"""
56 raise NotImplementedException
57
58 def getmessage(self, uid):
59 """Returns the content of the specified message."""
60 raise NotImplementedException
61
62 def savemessage(self, uid, content, flags):
63 """Writes a new message, with the specified uid.
64 If the uid is < 0, the backend should assign a new uid and return it.
65
66 If the backend cannot assign a new uid, it returns the uid passed in
67 WITHOUT saving the message.
68
69 IMAP backend should be the only one that can assign a new uid.
70
71 If the uid is > 0, the backend should set the uid to this, if it can.
72 If it cannot set the uid to that, it will save it anyway.
73 It will return the uid assigned in any case.
74 """
75 raise NotImplementedException
76
77 def getmessageflags(self, uid):
78 """Returns the flags for the specified message."""
79 raise NotImplementedException
80
81 def savemessageflags(self, uid, flags):
82 """Sets the specified message's flags to the given set."""
83 raise NotImplementedException
84
85 def addmessageflags(self, uid, flags):
86 """Adds the specified flags to the message's flag set. If a given
87 flag is already present, it will not be duplicated."""
88 newflags = self.getmessageflags(uid)
89 for flag in flags:
90 if not flag in newflags:
91 newflags.append(flag)
92 newflags.sort()
93 self.savemessageflags(uid, newflags)
94
95 def deletemessageflags(self, uid, flags):
96 """Removes each flag given from the message's flag set. If a given
97 flag is already removed, no action will be taken for that flag."""
98 newflags = self.getmessageflags(uid)
99 for flag in flags:
100 if flag in newflags:
101 newflags.remove(flag)
102 newflags.sort()
103 self.savemessageflags(uid, newflags)
104
105 def deletemessage(self, uid):
106 raise NotImplementedException
107
108 def deletemessages(self, uidlist):
109 for uid in uidlist:
110 self.deletemessage(uid)
111
112 def syncmessagesto(self, dest, applyto = None):
113 """Syncs messages in this folder to the destination.
114 If applyto is specified, it should be a list of folders (don't forget
115 to include dest!) to which all write actions should be applied.
116 It defaults to [dest] if not specified. It is important that
117 the UID generator be listed first in applyto; that is, the other
118 applyto ones should be the ones that "copy" the main action."""
119 if applyto == None:
120 applyto = [dest]
121
122 # Pass 1 -- Look for messages in self with a negative uid.
123 # These are messages in Maildirs that were not added by us.
124 # Try to add them to the dests, and once that succeeds, get the
125 # UID, add it to the others for real, add it to local for real,
126 # and delete the fake one.
127
128 for uid in self.getmessagelist().keys():
129 if uid >= 0:
130 continue
131 successobject = None
132 successuid = None
133 message = self.getmessage(uid)
134 flags = self.getmessageflags(uid)
135 for tryappend in applyto:
136 successuid = tryappend.savemessage(uid, message, flags)
137 if successuid > 0:
138 successobject = tryappend
139 break
140 # Did we succeed?
141 if successobject != None:
142 # Copy the message to the other remote servers.
143 for appendserver in [x for x in applyto if x != successobject]:
144 appendserver.savemessage(successuid, message, flags)
145 # Copy it to its new name on the local server and delete
146 # the one without a UID.
147 self.savemessage(successuid, message, flags)
148 self.deletemessage(uid)
149 else:
150 # Did not find any server to take this message. Ignore.
151 pass
152
153 # Pass 2 -- Look for messages present in self but not in dest.
154 # If any, add them to dest.
155
156 for uid in self.getmessagelist().keys():
157 if uid < 0: # Ignore messages that pass 1 missed.
158 continue
159 if not uid in dest.getmessagelist():
160 message = self.getmessage(uid)
161 flags = self.getmessageflags(uid)
162 for object in applyto:
163 newuid = object.savemessage(uid, message, flags)
164 if newuid != uid:
165 # Change the local uid.
166 self.savemessage(newuid, message, flags)
167 self.deletemessage(uid)
168 uid = newuid
169
170 # Pass 3 -- Look for message present in dest but not in self.
171 # If any, delete them.
172
173 for uid in dest.getmessagelist().keys():
174 if uid < 0:
175 continue
176 if not uid in self.getmessagelist():
177 for object in applyto:
178 object.deletemessage(uid)
179
180 # Now, the message lists should be identical wrt the uids present.
181 # (except for potential negative uids that couldn't be placed
182 # anywhere)
183
184 # Pass 3 -- Look for any flag identity issues -- set dest messages
185 # to have the same flags that we have here.
186
187 for uid in self.getmessagelist().keys():
188 if uid < 0: # Ignore messages missed by pass 1
189 continue
190 selfflags = self.getmessageflags(uid)
191 destflags = dest.getmessageflags(uid)
192
193 addflags = [x for x in selfflags if x not in destflags]
194 if len(addflags):
195 for object in applyto:
196 object.addmessageflags(uid, addflags)
197
198 delflags = [x for x in destflags if x not in selfflags]
199 if len(delflags):
200 for object in applyto:
201 object.deletemessageflags(uid, delflags)
202
203