2 # Copyright (C) 2002 John Goerzen
3 # <jgoerzen@complete.org>
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 from threading
import *
20 from offlineimap
import threadutil
21 from offlineimap
.threadutil
import InstanceLimitedThread
22 from offlineimap
.ui
import UIBase
33 def suggeststhreads(self
):
34 """Returns true if this folder suggests using threads for actions;
35 false otherwise. Probably only IMAP will return true."""
38 def waitforthread(self
):
39 """For threading folders, waits until there is a resource available
40 before firing off a thread. For all others, returns immediately."""
43 def getcopyinstancelimit(self
):
44 """For threading folders, returns the instancelimitname for
45 InstanceLimitedThreads."""
46 raise NotImplementedException
48 def storesmessages(self
):
49 """Should be true for any backend that actually saves message bodies.
50 (Almost all of them). False for the LocalStatus backend. Saves
51 us from having to slurp up messages just for localstatus purposes."""
54 def getvisiblename(self
):
57 def getrepository(self
):
58 """Returns the repository object that this folder is within."""
59 return self
.repository
62 """Returns the root of the folder, in a folder-specific fashion."""
66 """Returns the separator for this folder type."""
69 def getfullname(self
):
71 return self
.getroot() + self
.getsep() + self
.getname()
75 def getfolderbasename(self
):
76 foldername
= self
.getname()
77 foldername
= foldername
.replace(self
.repository
.getsep(), '.')
78 foldername
= re
.sub('/\.$', '/dot', foldername
)
79 foldername
= re
.sub('^\.$', 'dot', foldername
)
82 def isuidvalidityok(self
):
83 if self
.getsaveduidvalidity() != None:
84 return self
.getsaveduidvalidity() == self
.getuidvalidity()
86 self
.saveuidvalidity()
89 def _getuidfilename(self
):
90 return os
.path
.join(self
.repository
.getuiddir(),
91 self
.getfolderbasename())
93 def getsaveduidvalidity(self
):
94 if hasattr(self
, '_base_saved_uidvalidity'):
95 return self
._base
_saved
_uidvalidity
96 uidfilename
= self
._getuidfilename
()
97 if not os
.path
.exists(uidfilename
):
98 self
._base
_saved
_uidvalidity
= None
100 file = open(uidfilename
, "rt")
101 self
._base
_saved
_uidvalidity
= long(file.readline().strip())
103 return self
._base
_saved
_uidvalidity
105 def saveuidvalidity(self
):
106 newval
= self
.getuidvalidity()
107 uidfilename
= self
._getuidfilename
()
108 self
.uidlock
.acquire()
110 file = open(uidfilename
+ ".tmp", "wt")
111 file.write("%d\n" % newval
)
113 os
.rename(uidfilename
+ ".tmp", uidfilename
)
114 self
._base
_saved
_uidvalidity
= newval
116 self
.uidlock
.release()
118 def getuidvalidity(self
):
119 raise NotImplementedException
121 def cachemessagelist(self
):
122 """Reads the message list from disk or network and stores it in
123 memory for later use. This list will not be re-read from disk or
124 memory unless this function is called again."""
125 raise NotImplementedException
127 def getmessagelist(self
):
128 """Gets the current message list.
129 You must call cachemessagelist() before calling this function!"""
130 raise NotImplementedException
132 def getmessage(self
, uid
):
133 """Returns the content of the specified message."""
134 raise NotImplementedException
136 def savemessage(self
, uid
, content
, flags
):
137 """Writes a new message, with the specified uid.
138 If the uid is < 0, the backend should assign a new uid and return it.
140 If the backend cannot assign a new uid, it returns the uid passed in
141 WITHOUT saving the message.
143 If the backend CAN assign a new uid, but cannot find out what this UID
144 is (as is the case with many IMAP servers), it returns 0 but DOES save
147 IMAP backend should be the only one that can assign a new uid.
149 If the uid is > 0, the backend should set the uid to this, if it can.
150 If it cannot set the uid to that, it will save it anyway.
151 It will return the uid assigned in any case.
153 raise NotImplementedException
155 def getmessageflags(self
, uid
):
156 """Returns the flags for the specified message."""
157 raise NotImplementedException
159 def savemessageflags(self
, uid
, flags
):
160 """Sets the specified message's flags to the given set."""
161 raise NotImplementedException
163 def addmessageflags(self
, uid
, flags
):
164 """Adds the specified flags to the message's flag set. If a given
165 flag is already present, it will not be duplicated."""
166 newflags
= self
.getmessageflags(uid
)
168 if not flag
in newflags
:
169 newflags
.append(flag
)
171 self
.savemessageflags(uid
, newflags
)
173 def addmessagesflags(self
, uidlist
, flags
):
175 self
.addmessageflags(uid
, flags
)
177 def deletemessageflags(self
, uid
, flags
):
178 """Removes each flag given from the message's flag set. If a given
179 flag is already removed, no action will be taken for that flag."""
180 newflags
= self
.getmessageflags(uid
)
183 newflags
.remove(flag
)
185 self
.savemessageflags(uid
, newflags
)
187 def deletemessagesflags(self
, uidlist
, flags
):
189 self
.deletemessageflags(uid
, flags
)
191 def deletemessage(self
, uid
):
192 raise NotImplementedException
194 def deletemessages(self
, uidlist
):
196 self
.deletemessage(uid
)
198 def syncmessagesto_neguid_msg(self
, uid
, dest
, applyto
, register
= 1):
200 UIBase
.getglobalui().registerthread(self
.getaccountname())
201 UIBase
.getglobalui().copyingmessage(uid
, self
, applyto
)
204 message
= self
.getmessage(uid
)
205 flags
= self
.getmessageflags(uid
)
206 for tryappend
in applyto
:
207 successuid
= tryappend
.savemessage(uid
, message
, flags
)
209 successobject
= tryappend
212 if successobject
!= None:
213 if successuid
: # Only if IMAP actually assigned a UID
214 # Copy the message to the other remote servers.
215 for appendserver
in \
216 [x
for x
in applyto
if x
!= successobject
]:
217 appendserver
.savemessage(successuid
, message
, flags
)
218 # Copy to its new name on the local server and delete
219 # the one without a UID.
220 self
.savemessage(successuid
, message
, flags
)
221 self
.deletemessage(uid
) # It'll be re-downloaded.
223 # Did not find any server to take this message. Ignore.
227 def syncmessagesto_neguid(self
, dest
, applyto
):
228 """Pass 1 of folder synchronization.
230 Look for messages in self with a negative uid. These are messages in
231 Maildirs that were not added by us. Try to add them to the dests,
232 and once that succeeds, get the UID, add it to the others for real,
233 add it to local for real, and delete the fake one."""
235 uidlist
= [uid
for uid
in self
.getmessagelist().keys() if uid
< 0]
240 usethread
= applyto
[0]
243 if usethread
and usethread
.suggeststhreads():
244 usethread
.waitforthread()
245 thread
= InstanceLimitedThread(\
246 usethread
.getcopyinstancelimit(),
247 target
= self
.syncmessagesto_neguid_msg
,
248 name
= "New msg sync from %s" % self
.getvisiblename(),
249 args
= (uid
, dest
, applyto
))
252 threads
.append(thread
)
254 self
.syncmessagesto_neguid_msg(uid
, dest
, applyto
, register
= 0)
255 for thread
in threads
:
258 def copymessageto(self
, uid
, applyto
, register
= 1):
259 # Sometimes, it could be the case that if a sync takes awhile,
260 # a message might be deleted from the maildir before it can be
261 # synced to the status cache. This is only a problem with
262 # self.getmessage(). So, don't call self.getmessage unless
265 UIBase
.getglobalui().registerthread(self
.getaccountname())
266 UIBase
.getglobalui().copyingmessage(uid
, self
, applyto
)
268 # If any of the destinations actually stores the message body,
270 for object in applyto
:
271 if object.storesmessages():
272 message
= self
.getmessage(uid
)
274 flags
= self
.getmessageflags(uid
)
275 for object in applyto
:
276 newuid
= object.savemessage(uid
, message
, flags
)
277 if newuid
> 0 and newuid
!= uid
:
278 # Change the local uid.
279 self
.savemessage(newuid
, message
, flags
)
280 self
.deletemessage(uid
)
284 def syncmessagesto_copy(self
, dest
, applyto
):
285 """Pass 2 of folder synchronization.
287 Look for messages present in self but not in dest. If any, add
291 dest_messagelist
= dest
.getmessagelist()
292 for uid
in self
.getmessagelist().keys():
293 if uid
< 0: # Ignore messages that pass 1 missed.
295 if not uid
in dest_messagelist
:
296 if self
.suggeststhreads():
298 thread
= InstanceLimitedThread(\
299 self
.getcopyinstancelimit(),
300 target
= self
.copymessageto
,
301 name
= "Copy message %d from %s" % (uid
,
302 self
.getvisiblename()),
303 args
= (uid
, applyto
))
306 threads
.append(thread
)
308 self
.copymessageto(uid
, applyto
, register
= 0)
309 for thread
in threads
:
312 def syncmessagesto_delete(self
, dest
, applyto
):
313 """Pass 3 of folder synchronization.
315 Look for message present in dest but not in self.
316 If any, delete them."""
318 self_messagelist
= self
.getmessagelist()
319 for uid
in dest
.getmessagelist().keys():
322 if not uid
in self_messagelist
:
323 deletelist
.append(uid
)
325 UIBase
.getglobalui().deletingmessages(deletelist
, applyto
)
326 for object in applyto
:
327 object.deletemessages(deletelist
)
329 def syncmessagesto_flags(self
, dest
, applyto
):
330 """Pass 4 of folder synchronization.
332 Look for any flag matching issues -- set dest message to have the
333 same flags that we have."""
335 # As an optimization over previous versions, we store up which flags
336 # are being used for an add or a delete. For each flag, we store
337 # a list of uids to which it should be added. Then, we can call
338 # addmessagesflags() to apply them in bulk, rather than one
339 # call per message as before. This should result in some significant
340 # performance improvements.
345 for uid
in self
.getmessagelist().keys():
346 if uid
< 0: # Ignore messages missed by pass 1
348 selfflags
= self
.getmessageflags(uid
)
349 destflags
= dest
.getmessageflags(uid
)
351 addflags
= [x
for x
in selfflags
if x
not in destflags
]
353 for flag
in addflags
:
354 if not flag
in addflaglist
:
355 addflaglist
[flag
] = []
356 addflaglist
[flag
].append(uid
)
358 delflags
= [x
for x
in destflags
if x
not in selfflags
]
359 for flag
in delflags
:
360 if not flag
in delflaglist
:
361 delflaglist
[flag
] = []
362 delflaglist
[flag
].append(uid
)
364 for object in applyto
:
365 for flag
in addflaglist
.keys():
366 UIBase
.getglobalui().addingflags(addflaglist
[flag
], flag
, [object])
367 object.addmessagesflags(addflaglist
[flag
], [flag
])
368 for flag
in delflaglist
.keys():
369 UIBase
.getglobalui().deletingflags(delflaglist
[flag
], flag
, [object])
370 object.deletemessagesflags(delflaglist
[flag
], [flag
])
372 def syncmessagesto(self
, dest
, applyto
= None):
373 """Syncs messages in this folder to the destination.
374 If applyto is specified, it should be a list of folders (don't forget
375 to include dest!) to which all write actions should be applied.
376 It defaults to [dest] if not specified. It is important that
377 the UID generator be listed first in applyto; that is, the other
378 applyto ones should be the ones that "copy" the main action."""
382 self
.syncmessagesto_neguid(dest
, applyto
)
383 self
.syncmessagesto_copy(dest
, applyto
)
384 self
.syncmessagesto_delete(dest
, applyto
)
386 # Now, the message lists should be identical wrt the uids present.
387 # (except for potential negative uids that couldn't be placed
390 self
.syncmessagesto_flags(dest
, applyto
)