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
, rtime
):
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 getmessagetime(self
, uid
):
156 """Return the received time for the specified message."""
157 raise NotImplementedException
159 def getmessageflags(self
, uid
):
160 """Returns the flags for the specified message."""
161 raise NotImplementedException
163 def savemessageflags(self
, uid
, flags
):
164 """Sets the specified message's flags to the given set."""
165 raise NotImplementedException
167 def addmessageflags(self
, uid
, flags
):
168 """Adds the specified flags to the message's flag set. If a given
169 flag is already present, it will not be duplicated."""
170 newflags
= self
.getmessageflags(uid
)
172 if not flag
in newflags
:
173 newflags
.append(flag
)
175 self
.savemessageflags(uid
, newflags
)
177 def addmessagesflags(self
, uidlist
, flags
):
179 self
.addmessageflags(uid
, flags
)
181 def deletemessageflags(self
, uid
, flags
):
182 """Removes each flag given from the message's flag set. If a given
183 flag is already removed, no action will be taken for that flag."""
184 newflags
= self
.getmessageflags(uid
)
187 newflags
.remove(flag
)
189 self
.savemessageflags(uid
, newflags
)
191 def deletemessagesflags(self
, uidlist
, flags
):
193 self
.deletemessageflags(uid
, flags
)
195 def deletemessage(self
, uid
):
196 raise NotImplementedException
198 def deletemessages(self
, uidlist
):
200 self
.deletemessage(uid
)
202 def syncmessagesto_neguid_msg(self
, uid
, dest
, applyto
, register
= 1):
204 UIBase
.getglobalui().registerthread(self
.getaccountname())
205 UIBase
.getglobalui().copyingmessage(uid
, self
, applyto
)
208 message
= self
.getmessage(uid
)
209 flags
= self
.getmessageflags(uid
)
210 rtime
= self
.getmessagetime(uid
)
211 for tryappend
in applyto
:
212 successuid
= tryappend
.savemessage(uid
, message
, flags
, rtime
)
214 successobject
= tryappend
217 if successobject
!= None:
218 if successuid
: # Only if IMAP actually assigned a UID
219 # Copy the message to the other remote servers.
220 for appendserver
in \
221 [x
for x
in applyto
if x
!= successobject
]:
222 appendserver
.savemessage(successuid
, message
, flags
, rtime
)
223 # Copy to its new name on the local server and delete
224 # the one without a UID.
225 self
.savemessage(successuid
, message
, flags
, rtime
)
226 self
.deletemessage(uid
) # It'll be re-downloaded.
228 # Did not find any server to take this message. Ignore.
232 def syncmessagesto_neguid(self
, dest
, applyto
):
233 """Pass 1 of folder synchronization.
235 Look for messages in self with a negative uid. These are messages in
236 Maildirs that were not added by us. Try to add them to the dests,
237 and once that succeeds, get the UID, add it to the others for real,
238 add it to local for real, and delete the fake one."""
240 uidlist
= [uid
for uid
in self
.getmessagelist().keys() if uid
< 0]
245 usethread
= applyto
[0]
248 if usethread
and usethread
.suggeststhreads():
249 usethread
.waitforthread()
250 thread
= InstanceLimitedThread(\
251 usethread
.getcopyinstancelimit(),
252 target
= self
.syncmessagesto_neguid_msg
,
253 name
= "New msg sync from %s" % self
.getvisiblename(),
254 args
= (uid
, dest
, applyto
))
257 threads
.append(thread
)
259 self
.syncmessagesto_neguid_msg(uid
, dest
, applyto
, register
= 0)
260 for thread
in threads
:
263 def copymessageto(self
, uid
, applyto
, register
= 1):
264 # Sometimes, it could be the case that if a sync takes awhile,
265 # a message might be deleted from the maildir before it can be
266 # synced to the status cache. This is only a problem with
267 # self.getmessage(). So, don't call self.getmessage unless
270 UIBase
.getglobalui().registerthread(self
.getaccountname())
271 UIBase
.getglobalui().copyingmessage(uid
, self
, applyto
)
273 # If any of the destinations actually stores the message body,
275 for object in applyto
:
276 if object.storesmessages():
277 message
= self
.getmessage(uid
)
279 flags
= self
.getmessageflags(uid
)
280 rtime
= self
.getmessagetime(uid
)
281 for object in applyto
:
282 newuid
= object.savemessage(uid
, message
, flags
, rtime
)
283 if newuid
> 0 and newuid
!= uid
:
284 # Change the local uid.
285 self
.savemessage(newuid
, message
, flags
, rtime
)
286 self
.deletemessage(uid
)
290 def syncmessagesto_copy(self
, dest
, applyto
):
291 """Pass 2 of folder synchronization.
293 Look for messages present in self but not in dest. If any, add
297 dest_messagelist
= dest
.getmessagelist()
298 for uid
in self
.getmessagelist().keys():
299 if uid
< 0: # Ignore messages that pass 1 missed.
301 if not uid
in dest_messagelist
:
302 if self
.suggeststhreads():
304 thread
= InstanceLimitedThread(\
305 self
.getcopyinstancelimit(),
306 target
= self
.copymessageto
,
307 name
= "Copy message %d from %s" % (uid
,
308 self
.getvisiblename()),
309 args
= (uid
, applyto
))
312 threads
.append(thread
)
314 self
.copymessageto(uid
, applyto
, register
= 0)
315 for thread
in threads
:
318 def syncmessagesto_delete(self
, dest
, applyto
):
319 """Pass 3 of folder synchronization.
321 Look for message present in dest but not in self.
322 If any, delete them."""
324 self_messagelist
= self
.getmessagelist()
325 for uid
in dest
.getmessagelist().keys():
328 if not uid
in self_messagelist
:
329 deletelist
.append(uid
)
331 UIBase
.getglobalui().deletingmessages(deletelist
, applyto
)
332 for object in applyto
:
333 object.deletemessages(deletelist
)
335 def syncmessagesto_flags(self
, dest
, applyto
):
336 """Pass 4 of folder synchronization.
338 Look for any flag matching issues -- set dest message to have the
339 same flags that we have."""
341 # As an optimization over previous versions, we store up which flags
342 # are being used for an add or a delete. For each flag, we store
343 # a list of uids to which it should be added. Then, we can call
344 # addmessagesflags() to apply them in bulk, rather than one
345 # call per message as before. This should result in some significant
346 # performance improvements.
351 for uid
in self
.getmessagelist().keys():
352 if uid
< 0: # Ignore messages missed by pass 1
354 selfflags
= self
.getmessageflags(uid
)
355 destflags
= dest
.getmessageflags(uid
)
357 addflags
= [x
for x
in selfflags
if x
not in destflags
]
359 for flag
in addflags
:
360 if not flag
in addflaglist
:
361 addflaglist
[flag
] = []
362 addflaglist
[flag
].append(uid
)
364 delflags
= [x
for x
in destflags
if x
not in selfflags
]
365 for flag
in delflags
:
366 if not flag
in delflaglist
:
367 delflaglist
[flag
] = []
368 delflaglist
[flag
].append(uid
)
370 for object in applyto
:
371 for flag
in addflaglist
.keys():
372 UIBase
.getglobalui().addingflags(addflaglist
[flag
], flag
, [object])
373 object.addmessagesflags(addflaglist
[flag
], [flag
])
374 for flag
in delflaglist
.keys():
375 UIBase
.getglobalui().deletingflags(delflaglist
[flag
], flag
, [object])
376 object.deletemessagesflags(delflaglist
[flag
], [flag
])
378 def syncmessagesto(self
, dest
, applyto
= None):
379 """Syncs messages in this folder to the destination.
380 If applyto is specified, it should be a list of folders (don't forget
381 to include dest!) to which all write actions should be applied.
382 It defaults to [dest] if not specified. It is important that
383 the UID generator be listed first in applyto; that is, the other
384 applyto ones should be the ones that "copy" the main action."""
388 self
.syncmessagesto_neguid(dest
, applyto
)
389 self
.syncmessagesto_copy(dest
, applyto
)
390 self
.syncmessagesto_delete(dest
, applyto
)
392 # Now, the message lists should be identical wrt the uids present.
393 # (except for potential negative uids that couldn't be placed
396 self
.syncmessagesto_flags(dest
, applyto
)