2002-06-19 08:08:59 +02:00
|
|
|
# Maildir folder support
|
2002-06-19 07:55:12 +02:00
|
|
|
# Copyright (C) 2002 John Goerzen
|
|
|
|
# <jgoerzen@complete.org>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
|
|
|
|
from Base import BaseFolder
|
2002-06-21 12:01:10 +02:00
|
|
|
from offlineimap import imaputil
|
2002-06-22 06:32:29 +02:00
|
|
|
import os.path, os, re, time, socket, md5
|
|
|
|
|
|
|
|
timeseq = 0
|
|
|
|
lasttime = long(0)
|
|
|
|
|
|
|
|
def gettimeseq():
|
2002-06-22 06:39:02 +02:00
|
|
|
global lasttime, timeseq
|
2002-06-22 06:32:29 +02:00
|
|
|
thistime = long(time.time())
|
|
|
|
if thistime == lasttime:
|
|
|
|
timeseq += 1
|
|
|
|
return timeseq
|
|
|
|
else:
|
|
|
|
lasttime = long(time.time())
|
|
|
|
timeseq = 0
|
|
|
|
return timeseq
|
2002-06-19 07:55:12 +02:00
|
|
|
|
2002-06-19 08:08:59 +02:00
|
|
|
class MaildirFolder(BaseFolder):
|
|
|
|
def __init__(self, root, name):
|
|
|
|
self.name = name
|
|
|
|
self.root = root
|
|
|
|
self.sep = '.'
|
2002-06-21 12:01:10 +02:00
|
|
|
self.uidfilename = os.path.join(self.getfullname(), "offlineimap.uidvalidity")
|
2002-06-20 05:33:23 +02:00
|
|
|
self.messagelist = None
|
|
|
|
|
2002-06-20 07:14:35 +02:00
|
|
|
def getfullname(self):
|
|
|
|
return os.path.join(self.getroot(), self.getname())
|
|
|
|
|
2002-06-20 05:33:23 +02:00
|
|
|
def getuidvalidity(self):
|
|
|
|
if not os.path.exists(self.uidfilename):
|
|
|
|
return None
|
|
|
|
file = open(self.uidfilename, "rt")
|
2002-06-20 09:40:29 +02:00
|
|
|
retval = long(file.readline().strip())
|
2002-06-20 05:33:23 +02:00
|
|
|
file.close()
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def saveuidvalidity(self, newval):
|
|
|
|
file = open(self.uidfilename, "wt")
|
2002-06-21 06:04:47 +02:00
|
|
|
file.write("%d\n" % newval)
|
2002-06-20 05:33:23 +02:00
|
|
|
file.close()
|
|
|
|
|
|
|
|
def isuidvalidityok(self, remotefolder):
|
|
|
|
myval = self.getuidvalidity()
|
|
|
|
if myval != None:
|
|
|
|
return myval == remotefolder.getuidvalidity()
|
|
|
|
else:
|
|
|
|
self.saveuidvalidity(remotefolder.getuidvalidity())
|
2002-06-21 06:04:47 +02:00
|
|
|
return 1
|
2002-06-20 05:33:23 +02:00
|
|
|
|
2002-07-16 04:26:58 +02:00
|
|
|
def _scanfolder(self):
|
2002-06-20 05:33:23 +02:00
|
|
|
"""Cache the message list. Maildir flags are:
|
|
|
|
R (replied)
|
|
|
|
S (seen)
|
|
|
|
T (trashed)
|
|
|
|
D (draft)
|
|
|
|
F (flagged)
|
|
|
|
and must occur in ASCII order."""
|
2002-07-16 04:26:58 +02:00
|
|
|
retval = {}
|
2002-06-20 05:33:23 +02:00
|
|
|
files = []
|
2002-06-20 06:14:54 +02:00
|
|
|
nouidcounter = -1 # Messages without UIDs get
|
|
|
|
# negative UID numbers.
|
2002-06-20 05:33:23 +02:00
|
|
|
for dirannex in ['new', 'cur']:
|
|
|
|
fulldirname = os.path.join(self.getfullname(), dirannex)
|
2002-06-20 07:14:35 +02:00
|
|
|
files.extend([os.path.join(fulldirname, filename) for
|
2002-06-20 05:33:23 +02:00
|
|
|
filename in os.listdir(fulldirname)])
|
|
|
|
for file in files:
|
|
|
|
messagename = os.path.basename(file)
|
2002-06-22 06:32:29 +02:00
|
|
|
foldermatch = re.search(',FMD5=([0-9a-f]{32})', messagename)
|
|
|
|
if (not foldermatch) or \
|
|
|
|
md5.new(self.getvisiblename()).hexdigest() \
|
|
|
|
!= foldermatch.group(1):
|
|
|
|
# If there is no folder MD5 specified, or if it mismatches,
|
|
|
|
# assume it is a foreign (new) message and generate a
|
|
|
|
# negative uid for it
|
2002-06-20 06:14:54 +02:00
|
|
|
uid = nouidcounter
|
2002-06-22 06:39:02 +02:00
|
|
|
nouidcounter -= 1
|
2002-06-22 06:32:29 +02:00
|
|
|
else: # It comes from our folder.
|
|
|
|
uidmatch = re.search(',U=(\d+)', messagename)
|
|
|
|
uid = None
|
|
|
|
if not uidmatch:
|
|
|
|
uid = nouidcounter
|
|
|
|
nouidcounter -= 1
|
|
|
|
else:
|
|
|
|
uid = long(uidmatch.group(1))
|
2002-06-20 16:09:19 +02:00
|
|
|
flagmatch = re.search(':.*2,([A-Z]+)', messagename)
|
2002-06-20 05:33:23 +02:00
|
|
|
flags = []
|
|
|
|
if flagmatch:
|
2002-06-20 16:09:19 +02:00
|
|
|
flags = [x for x in flagmatch.group(1)]
|
2002-06-20 06:59:57 +02:00
|
|
|
flags.sort()
|
2002-07-11 05:31:39 +02:00
|
|
|
if 'T' in flags:
|
|
|
|
# Message is marked for deletion; just delete it now.
|
|
|
|
# Otherwise, the T flag will be propogated to the IMAP
|
|
|
|
# server, and then expunged there, and then deleted here.
|
|
|
|
# Might as well just delete it now, to help make things
|
|
|
|
# more robust.
|
|
|
|
os.unlink(file)
|
|
|
|
else:
|
2002-07-16 04:26:58 +02:00
|
|
|
retval[uid] = {'uid': uid,
|
|
|
|
'flags': flags,
|
|
|
|
'filename': file}
|
|
|
|
return retval
|
|
|
|
|
|
|
|
def cachemessagelist(self):
|
|
|
|
self.messagelist = self._scanfolder()
|
2002-06-20 05:33:23 +02:00
|
|
|
|
|
|
|
def getmessagelist(self):
|
|
|
|
return self.messagelist
|
2002-06-19 08:08:59 +02:00
|
|
|
|
2002-06-20 06:59:57 +02:00
|
|
|
def getmessage(self, uid):
|
2002-06-20 07:14:35 +02:00
|
|
|
filename = self.getmessagelist()[uid]['filename']
|
2002-06-20 06:59:57 +02:00
|
|
|
file = open(filename, 'rt')
|
|
|
|
retval = file.read()
|
|
|
|
file.close()
|
|
|
|
return retval
|
|
|
|
|
2002-06-21 03:12:52 +02:00
|
|
|
def savemessage(self, uid, content, flags):
|
2002-06-21 03:03:30 +02:00
|
|
|
if uid < 0:
|
|
|
|
# We cannot assign a new uid.
|
|
|
|
return uid
|
2002-06-20 06:59:57 +02:00
|
|
|
if uid in self.getmessagelist():
|
|
|
|
# We already have it.
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2002-06-20 06:59:57 +02:00
|
|
|
newdir = os.path.join(self.getfullname(), 'new')
|
|
|
|
tmpdir = os.path.join(self.getfullname(), 'tmp')
|
|
|
|
messagename = None
|
|
|
|
attempts = 0
|
|
|
|
while 1:
|
|
|
|
if attempts > 15:
|
|
|
|
raise IOError, "Couldn't write to file %s" % messagename
|
2002-06-22 06:32:29 +02:00
|
|
|
messagename = '%d_%d.%d.%s,U=%d,FMD5=%s' % \
|
2002-06-20 09:40:29 +02:00
|
|
|
(long(time.time()),
|
2002-06-22 06:32:29 +02:00
|
|
|
gettimeseq(),
|
2002-06-20 06:59:57 +02:00
|
|
|
os.getpid(),
|
|
|
|
socket.gethostname(),
|
2002-06-22 06:32:29 +02:00
|
|
|
uid,
|
|
|
|
md5.new(self.getvisiblename()).hexdigest())
|
2002-06-20 06:59:57 +02:00
|
|
|
if os.path.exists(os.path.join(tmpdir, messagename)):
|
|
|
|
time.sleep(2)
|
|
|
|
attempts += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
file = open(os.path.join(tmpdir, messagename), "wt")
|
|
|
|
file.write(content)
|
|
|
|
file.close()
|
|
|
|
os.link(os.path.join(tmpdir, messagename),
|
|
|
|
os.path.join(newdir, messagename))
|
|
|
|
os.unlink(os.path.join(tmpdir, messagename))
|
|
|
|
self.messagelist[uid] = {'uid': uid, 'flags': [],
|
|
|
|
'filename': os.path.join(newdir, messagename)}
|
2002-06-21 03:12:52 +02:00
|
|
|
self.savemessageflags(uid, flags)
|
2002-06-21 03:03:30 +02:00
|
|
|
return uid
|
2002-06-20 06:59:57 +02:00
|
|
|
|
2002-06-20 07:14:35 +02:00
|
|
|
def getmessageflags(self, uid):
|
|
|
|
return self.getmessagelist()[uid]['flags']
|
|
|
|
|
|
|
|
def savemessageflags(self, uid, flags):
|
|
|
|
oldfilename = self.getmessagelist()[uid]['filename']
|
|
|
|
newpath, newname = os.path.split(oldfilename)
|
|
|
|
infostr = ':'
|
|
|
|
infomatch = re.search('(:.*)$', newname)
|
|
|
|
if infomatch: # If the info string is present..
|
|
|
|
infostr = infomatch.group(1)
|
|
|
|
newname = newname.split(':')[0] # Strip off the info string.
|
2002-06-21 06:04:47 +02:00
|
|
|
infostr = re.sub('2,[A-Z]*', '', infostr)
|
2002-06-20 07:14:35 +02:00
|
|
|
flags.sort()
|
|
|
|
infostr += '2,' + ''.join(flags)
|
|
|
|
newname += infostr
|
|
|
|
|
|
|
|
newfilename = os.path.join(newpath, newname)
|
|
|
|
if (newfilename != oldfilename):
|
|
|
|
os.rename(oldfilename, newfilename)
|
|
|
|
self.getmessagelist()[uid]['flags'] = flags
|
2002-06-21 06:04:47 +02:00
|
|
|
self.getmessagelist()[uid]['filename'] = newfilename
|
2002-06-20 07:14:35 +02:00
|
|
|
|
|
|
|
def getmessageflags(self, uid):
|
|
|
|
return self.getmessagelist()[uid]['flags']
|
|
|
|
|
|
|
|
def deletemessage(self, uid):
|
2002-06-21 06:30:08 +02:00
|
|
|
if not uid in self.messagelist:
|
|
|
|
return
|
2002-06-20 07:14:35 +02:00
|
|
|
filename = self.getmessagelist()[uid]['filename']
|
2002-07-16 04:26:58 +02:00
|
|
|
try:
|
|
|
|
os.unlink(filename)
|
|
|
|
except IOError:
|
|
|
|
# Can't find the file -- maybe already deleted?
|
|
|
|
newmsglist = self._scanfolder()
|
|
|
|
if uid in newmsglist: # Nope, try new filename.
|
|
|
|
os.unlink(newmsglist[uid]['filename'])
|
|
|
|
# Yep -- return.
|
2002-06-20 07:14:35 +02:00
|
|
|
del(self.messagelist[uid])
|
|
|
|
|