]>
code.delx.au - offlineimap/blob - head/offlineimap/folder/Base.py
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 """Returns the root of the folder, in a folder-specific fashion."""
29 """Returns the separator for this folder type."""
32 def getfullname(self
):
34 return self
.getroot() + self
.getsep() + self
.getname()
38 def isuidvalidityok(self
, remotefolder
):
39 raise NotImplementedException
41 def getuidvalidity(self
):
42 raise NotImplementedException
44 def saveuidvalidity(self
, newval
):
45 raise NotImplementedException
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
53 def getmessagelist(self
):
54 """Gets the current message list.
55 You must call cachemessagelist() before calling this function!"""
56 raise NotImplementedException
58 def getmessage(self
, uid
):
59 """Returns the content of the specified message."""
60 raise NotImplementedException
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.
66 If the backend cannot assign a new uid, it returns the uid passed in
67 WITHOUT saving the message.
69 IMAP backend should be the only one that can assign a new uid.
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.
75 raise NotImplementedException
77 def getmessageflags(self
, uid
):
78 """Returns the flags for the specified message."""
79 raise NotImplementedException
81 def savemessageflags(self
, uid
, flags
):
82 """Sets the specified message's flags to the given set."""
83 raise NotImplementedException
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
)
90 if not flag
in newflags
:
93 self
.savemessageflags(uid
, newflags
)
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
)
101 newflags
.remove(flag
)
103 self
.savemessageflags(uid
, newflags
)
105 def deletemessage(self
, uid
):
106 raise NotImplementedException
108 def deletemessages(self
, uidlist
):
110 self
.deletemessage(uid
)
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."""
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.
130 for uid
in self
.getmessagelist().keys():
133 print "Uploading new message %d" % uid
136 message
= self
.getmessage(uid
)
137 flags
= self
.getmessageflags(uid
)
138 for tryappend
in applyto
:
139 successuid
= tryappend
.savemessage(uid
, message
, flags
)
141 successobject
= tryappend
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
)
153 # Did not find any server to take this message. Ignore.
158 # Pass 2 -- Look for messages present in self but not in dest.
159 # If any, add them to dest.
161 for uid
in self
.getmessagelist().keys():
162 if uid
< 0: # Ignore messages that pass 1 missed.
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
)
171 # Change the local uid.
172 self
.savemessage(newuid
, message
, flags
)
173 self
.deletemessage(uid
)
178 # Pass 3 -- Look for message present in dest but not in self.
179 # If any, delete them.
181 for uid
in dest
.getmessagelist().keys():
184 if not uid
in self
.getmessagelist():
185 print "Deleting message %d" % uid
186 for object in applyto
:
187 object.deletemessage(uid
)
189 # Now, the message lists should be identical wrt the uids present.
190 # (except for potential negative uids that couldn't be placed
195 # Pass 4 -- Look for any flag identity issues -- set dest messages
196 # to have the same flags that we have here.
198 for uid
in self
.getmessagelist().keys():
199 if uid
< 0: # Ignore messages missed by pass 1
201 selfflags
= self
.getmessageflags(uid
)
202 destflags
= dest
.getmessageflags(uid
)
204 addflags
= [x
for x
in selfflags
if x
not in destflags
]
206 print "Adding flags to %d" % uid
, addflags
207 for object in applyto
:
208 object.addmessageflags(uid
, addflags
)
210 delflags
= [x
for x
in destflags
if x
not in selfflags
]
212 print "Deleting flags from %d" % uid
, delflags
213 for object in applyto
:
214 object.deletemessageflags(uid
, delflags
)