]> code.delx.au - offlineimap/blob - head/offlineimap/folder/Maildir.py
/head: changeset 13
[offlineimap] / head / offlineimap / folder / Maildir.py
1 # Maildir 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 from Base import BaseFolder
20 from imapsync import imaputil
21 import os.path, os, re
22
23 class MaildirFolder(BaseFolder):
24 def __init__(self, root, name):
25 self.name = name
26 self.root = root
27 self.sep = '.'
28 self.uidfilename = os.path.join(self.getfullname(), "imapsync.uidvalidity")
29 self.messagelist = None
30
31 def getuidvalidity(self):
32 if not os.path.exists(self.uidfilename):
33 return None
34 file = open(self.uidfilename, "rt")
35 retval = int(file.readline().strip())
36 file.close()
37 return retval
38
39 def saveuidvalidity(self, newval):
40 file = open(self.uidfilename, "wt")
41 file.write("%d\n", newval)
42 file.close()
43
44 def isuidvalidityok(self, remotefolder):
45 myval = self.getuidvalidity()
46 if myval != None:
47 return myval == remotefolder.getuidvalidity()
48 else:
49 self.saveuidvalidity(remotefolder.getuidvalidity())
50
51 def cachemessagelist(self):
52 """Cache the message list. Maildir flags are:
53 R (replied)
54 S (seen)
55 T (trashed)
56 D (draft)
57 F (flagged)
58 and must occur in ASCII order."""
59 self.messagelist = {}
60 files = []
61 nouidcounter = -1 # Messages without UIDs get
62 # negative UID numbers.
63 for dirannex in ['new', 'cur']:
64 fulldirname = os.path.join(self.getfullname(), dirannex)
65 files.append([os.path.join(fulldirname, filename) for
66 filename in os.listdir(fulldirname)])
67 for file in files:
68 messagename = os.path.basename(file)
69 uidmatch = re.search(',U=(\d+)', messagename)
70 uid = None
71 if not uidmatch:
72 uid = nouidcounter
73 nouidcounter -= 1
74 else:
75 uid = int(uidmatch.group(1))
76 flagmatch = re.search(':.*2,([A-Z]+)')
77 flags = []
78 if flagmatch:
79 flags = [f for x in flagmatch.group(1)]
80 self.messagelist[uid] = {'uid': uid,
81 'flags': flags,
82 'filename': messagename}
83
84 def getmessagelist(self):
85 return self.messagelist
86
87