/head: changeset 37

Updated
This commit is contained in:
jgoerzen 2002-06-21 08:25:24 +01:00
parent 75dcb0dd16
commit 8171027aa1
7 changed files with 121 additions and 69 deletions

View File

@ -35,6 +35,12 @@ metadata = ~/.imapsync
accounts = Test accounts = Test
# You can have imapsync continue running indefinately, automatically
# syncing your mail periodically. If you want that, specify how
# frequently to do that (in minutes) here.
# autorefresh = 5
################################################## ##################################################
# Mailbox name recorder # Mailbox name recorder
################################################## ##################################################

View File

@ -45,71 +45,88 @@ remoterepos = None
localrepos = None localrepos = None
mailboxes = [] mailboxes = []
for accountname in accounts: def syncitall():
ui.acct(accountname) for accountname in accounts:
accountmetadata = os.path.join(metadatadir, accountname) ui.acct(accountname)
if not os.path.exists(accountmetadata): accountmetadata = os.path.join(metadatadir, accountname)
os.mkdir(accountmetadata, 0700) if not os.path.exists(accountmetadata):
host = config.get(accountname, "remotehost") os.mkdir(accountmetadata, 0700)
user = config.get(accountname, "remoteuser") host = config.get(accountname, "remotehost")
port = None user = config.get(accountname, "remoteuser")
if config.has_option(accountname, "remoteport"): port = None
port = config.getint(accountname, "remoteport") if config.has_option(accountname, "remoteport"):
password = None port = config.getint(accountname, "remoteport")
if config.has_option(accountname, "remotepass"): password = None
password = config.get(accountname, "remotepass") if config.has_option(accountname, "remotepass"):
else: password = config.get(accountname, "remotepass")
password = ui.getpass(accountname, host, port, user) else:
ssl = config.getboolean(accountname, "ssl") password = ui.getpass(accountname, host, port, user)
# Save it for future reference.
config.set(accountname, "remotepass", password)
ssl = config.getboolean(accountname, "ssl")
# Connect to the remote server. # Connect to the remote server.
server = imapserver.IMAPServer(user, password, host, port, ssl) server = imapserver.IMAPServer(user, password, host, port, ssl)
remoterepos = repository.IMAP.IMAPRepository(config, accountname, server) remoterepos = repository.IMAP.IMAPRepository(config, accountname, server)
# Connect to the Maildirs. # Connect to the Maildirs.
localrepos = repository.Maildir.MaildirRepository(os.path.expanduser(config.get(accountname, "localfolders"))) localrepos = repository.Maildir.MaildirRepository(os.path.expanduser(config.get(accountname, "localfolders")))
# Connect to the local cache. # Connect to the local cache.
statusrepos = repository.LocalStatus.LocalStatusRepository(accountmetadata) statusrepos = repository.LocalStatus.LocalStatusRepository(accountmetadata)
ui.syncfolders(remoterepos, localrepos)
remoterepos.syncfoldersto(localrepos)
for remotefolder in remoterepos.getfolders(): ui.syncfolders(remoterepos, localrepos)
mailboxes.append({'accountname': accountname, remoterepos.syncfoldersto(localrepos)
'foldername': remotefolder.getvisiblename()})
# Load local folder. for remotefolder in remoterepos.getfolders():
localfolder = localrepos.getfolder(remotefolder.getvisiblename()) mailboxes.append({'accountname': accountname,
if not localfolder.isuidvalidityok(remotefolder): 'foldername': remotefolder.getvisiblename()})
ui.validityproblem(remotefolder) # Load local folder.
continue localfolder = localrepos.getfolder(remotefolder.getvisiblename())
ui.syncingfolder(remoterepos, remotefolder, localrepos, localfolder) if not localfolder.isuidvalidityok(remotefolder):
ui.loadmessagelist(localrepos, localfolder) ui.validityproblem(remotefolder)
localfolder.cachemessagelist() continue
ui.messagelistloaded(localrepos, localfolder, len(localfolder.getmessagelist().keys())) ui.syncingfolder(remoterepos, remotefolder, localrepos, localfolder)
ui.loadmessagelist(localrepos, localfolder)
localfolder.cachemessagelist()
ui.messagelistloaded(localrepos, localfolder, len(localfolder.getmessagelist().keys()))
# Load remote folder.
ui.loadmessagelist(remoterepos, remotefolder)
remotefolder.cachemessagelist()
ui.messagelistloaded(remoterepos, remotefolder,
len(remotefolder.getmessagelist().keys()))
# Load status folder.
statusfolder = statusrepos.getfolder(remotefolder.getvisiblename())
statusfolder.cachemessagelist()
if not statusfolder.isnewfolder():
ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder)
localfolder.syncmessagesto(statusfolder, [remotefolder, statusfolder])
# Synchronize remote changes.
ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder)
remotefolder.syncmessagesto(localfolder)
# Make sure the status folder is up-to-date.
ui.syncingmessages(localrepos, localfolder, statusrepos, statusfolder)
localfolder.syncmessagesto(statusfolder)
statusfolder.save()
server.close()
mbnames.genmbnames(config, mailboxes)
syncitall()
if config.has_option('general', 'autorefresh'):
refreshperiod = config.getint('general', 'autorefresh') * 60
while 1:
sleepamount = refreshperiod
abortsleep = 0
while sleepamount > 0 and not abortsleep:
abortsleep = ui.sleeping(1, sleepamount)
sleepamount -= 1
ui.sleeping(0, 0) # Done sleeping.
syncitall()
# Load remote folder.
ui.loadmessagelist(remoterepos, remotefolder)
remotefolder.cachemessagelist()
ui.messagelistloaded(remoterepos, remotefolder,
len(remotefolder.getmessagelist().keys()))
# Load status folder.
statusfolder = statusrepos.getfolder(remotefolder.getvisiblename())
statusfolder.cachemessagelist()
if not statusfolder.isnewfolder():
ui.syncingmessages(localrepos, localfolder, remoterepos, remotefolder)
localfolder.syncmessagesto(statusfolder, [remotefolder, statusfolder])
# Synchronize remote changes.
ui.syncingmessages(remoterepos, remotefolder, localrepos, localfolder)
remotefolder.syncmessagesto(localfolder)
# Make sure the status folder is up-to-date.
ui.syncingmessages(localrepos, localfolder, statusrepos, statusfolder)
localfolder.syncmessagesto(statusfolder)
statusfolder.save()
mbnames.genmbnames(config, mailboxes)

View File

@ -64,7 +64,7 @@ class IMAPFolder(BaseFolder):
def getmessage(self, uid): def getmessage(self, uid):
assert(self.imapobj.select(self.getfullname())[0] == 'OK') assert(self.imapobj.select(self.getfullname())[0] == 'OK')
return self.imapobj.uid('fetch', '%d' % uid, '(RFC822)')[1][0][1].replace("\r\n", "\n") return self.imapobj.uid('fetch', '%d' % uid, '(BODY.PEEK[])')[1][0][1].replace("\r\n", "\n")
def getmessageflags(self, uid): def getmessageflags(self, uid):
return self.getmessagelist()[uid]['flags'] return self.getmessagelist()[uid]['flags']

View File

@ -67,4 +67,6 @@ class IMAPServer:
self.imapobj = imapobj self.imapobj = imapobj
return imapobj return imapobj
def close(self):
self.imapobj.logout()
self.imapobj = None

View File

@ -50,5 +50,6 @@ class IMAPRepository(BaseRepository):
continue continue
retval.append(folder.IMAP.IMAPFolder(self.imapserver, name, retval.append(folder.IMAP.IMAPFolder(self.imapserver, name,
self.nametrans(imaputil.dequote(name)))) self.nametrans(imaputil.dequote(name))))
retval.sort()
self.folders = retval self.folders = retval
return retval return retval

View File

@ -1,5 +1,6 @@
from UIBase import UIBase from UIBase import UIBase
from getpass import getpass from getpass import getpass
import select, sys
class TTYUI(UIBase): class TTYUI(UIBase):
def __init__(self, verbose = 0): def __init__(self, verbose = 0):
@ -23,3 +24,18 @@ class TTYUI(UIBase):
def messagelistloaded(s, repos, folder, count): def messagelistloaded(s, repos, folder, count):
if s.verbose: if s.verbose:
UIBase.messagelistloaded(s, repos, folder, count) UIBase.messagelistloaded(s, repos, folder, count)
def sleeping(s, sleepsecs, remainingsecs):
if remainingsecs > 0:
sys.stdout.write("Next sync in %d:%02d (press Enter to sync now) \r" % \
(remainingsecs / 60, remainingsecs % 60))
sys.stdout.flush()
else:
sys.stdout.write("Wait done, proceeding with sync.... ")
if sleepsecs > 0:
if len(select.select([sys.stdin], [], [], sleepsecs)[0]):
return 1
return 0

View File

@ -17,7 +17,7 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
from imapsync import repository from imapsync import repository
import re import re, time
class UIBase: class UIBase:
################################################## UTILS ################################################## UTILS
@ -95,12 +95,22 @@ class UIBase:
def addingflags(s, uid, flags, destlist): def addingflags(s, uid, flags, destlist):
ds = s.folderlist(destlist) ds = s.folderlist(destlist)
s._msg("Adding flags %s to message %d on %s" % \ s._msg("Adding flags %s to message %d on %s" % \
(flags.join(", "), uid, ds)) (", ".join(flags), uid, ds))
def deletingflags(s, uid, flags, destlist): def deletingflags(s, uid, flags, destlist):
ds = s.folderlist(destlist) ds = s.folderlist(destlist)
s._msg("Deleting flags %s to message %d on %s" % \ s._msg("Deleting flags %s to message %d on %s" % \
(flags.join(", "), uid, ds)) (", ".join(flags), uid, ds))
################################################## Other
def sleeping(s, sleepsecs, remainingsecs):
"""Sleep for sleepsecs, remainingsecs to go.
If sleepsecs is 0, indicates we're done sleeping.
Return 0 for normal sleep, or 1 to indicate a request
to sync immediately."""
s._msg("Next refresh in %d seconds" % remainingsec)
if sleepsecs > 0:
time.sleep(sleepsecs)
return 0