]> code.delx.au - offlineimap/blob - head/offlineimap/folder/Base.py
/head: changeset 32
[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 print "Pass 1"
123
124 # Pass 1 -- Look for messages in self with a negative uid.
125 # These are messages in Maildirs that were not added by us.
126 # Try to add them to the dests, and once that succeeds, get the
127 # UID, add it to the others for real, add it to local for real,
128 # and delete the fake one.
129
130 for uid in self.getmessagelist().keys():
131 if uid >= 0:
132 continue
133 print "Uploading new message %d" % uid
134 successobject = None
135 successuid = None
136 message = self.getmessage(uid)
137 flags = self.getmessageflags(uid)
138 for tryappend in applyto:
139 successuid = tryappend.savemessage(uid, message, flags)
140 if successuid > 0:
141 successobject = tryappend
142 break
143 # Did we succeed?
144 if successobject != None:
145 # Copy the message to the other remote servers.
146 for appendserver in [x for x in applyto if x != successobject]:
147 appendserver.savemessage(successuid, message, flags)
148 # Copy it to its new name on the local server and delete
149 # the one without a UID.
150 self.savemessage(successuid, message, flags)
151 self.deletemessage(uid)
152 else:
153 # Did not find any server to take this message. Ignore.
154 pass
155
156 print "Pass 2"
157
158 # Pass 2 -- Look for messages present in self but not in dest.
159 # If any, add them to dest.
160
161 for uid in self.getmessagelist().keys():
162 if uid < 0: # Ignore messages that pass 1 missed.
163 continue
164 if not uid in dest.getmessagelist():
165 print "Uploading new message %d" % uid
166 message = self.getmessage(uid)
167 flags = self.getmessageflags(uid)
168 for object in applyto:
169 newuid = object.savemessage(uid, message, flags)
170 if newuid != uid:
171 # Change the local uid.
172 self.savemessage(newuid, message, flags)
173 self.deletemessage(uid)
174 uid = newuid
175
176 print "Pass 3"
177
178 # Pass 3 -- Look for message present in dest but not in self.
179 # If any, delete them.
180
181 for uid in dest.getmessagelist().keys():
182 if uid < 0:
183 continue
184 if not uid in self.getmessagelist():
185 print "Deleting message %d" % uid
186 for object in applyto:
187 object.deletemessage(uid)
188
189 # Now, the message lists should be identical wrt the uids present.
190 # (except for potential negative uids that couldn't be placed
191 # anywhere)
192
193 print "Pass 4"
194
195 # Pass 4 -- Look for any flag identity issues -- set dest messages
196 # to have the same flags that we have here.
197
198 for uid in self.getmessagelist().keys():
199 if uid < 0: # Ignore messages missed by pass 1
200 continue
201 selfflags = self.getmessageflags(uid)
202 destflags = dest.getmessageflags(uid)
203
204 addflags = [x for x in selfflags if x not in destflags]
205 if len(addflags):
206 print "Adding flags to %d" % uid, addflags
207 for object in applyto:
208 object.addmessageflags(uid, addflags)
209
210 delflags = [x for x in destflags if x not in selfflags]
211 if len(delflags):
212 print "Deleting flags from %d" % uid, delflags
213 for object in applyto:
214 object.deletemessageflags(uid, delflags)
215
216