From 5b5e6d0ef8cbb9f397c4d9df204c1d1d13572609 Mon Sep 17 00:00:00 2001 From: jgoerzen Date: Thu, 20 Jun 2002 05:59:57 +0100 Subject: [PATCH] /head: changeset 15 Added savemessage --- head/offlineimap/folder/Maildir.py | 43 ++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/head/offlineimap/folder/Maildir.py b/head/offlineimap/folder/Maildir.py index e90b8a0..acb543b 100644 --- a/head/offlineimap/folder/Maildir.py +++ b/head/offlineimap/folder/Maildir.py @@ -18,7 +18,7 @@ from Base import BaseFolder from imapsync import imaputil -import os.path, os, re +import os.path, os, re, time, socket class MaildirFolder(BaseFolder): def __init__(self, root, name): @@ -77,6 +77,7 @@ class MaildirFolder(BaseFolder): flags = [] if flagmatch: flags = [f for x in flagmatch.group(1)] + flags.sort() self.messagelist[uid] = {'uid': uid, 'flags': flags, 'filename': messagename} @@ -84,4 +85,42 @@ class MaildirFolder(BaseFolder): def getmessagelist(self): return self.messagelist - + def getmessage(self, uid): + filename = self.getmessagelist(uid)['filename'] + file = open(filename, 'rt') + retval = file.read() + file.close() + return retval + + def savemessage(self, uid, content): + if uid in self.getmessagelist(): + # We already have it. + return + 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 + messagename = '%d.%d.%s,U=%d' % \ + (int(time.time()), + os.getpid(), + socket.gethostname(), + uid) + 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)} + + +