docker-offlineimap/head/offlineimap.py

105 lines
3.9 KiB
Python
Raw Normal View History

2002-06-19 04:21:27 +02:00
#!/usr/bin/python2.2 -i
# 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 imapsync import imaplib, imaputil, imapserver, repository, folder
2002-06-20 04:55:24 +02:00
import re, getpass, os, os.path
from ConfigParser import ConfigParser
2002-06-19 04:21:27 +02:00
2002-06-20 04:55:24 +02:00
config = ConfigParser()
config.read("imapsync.conf")
metadatadir = os.path.expanduser(config.get("general", "metadata"))
if not os.path.exists(metadatadir):
os.mkdir(metadatadir, 0700)
2002-06-19 04:21:27 +02:00
2002-06-20 04:55:24 +02:00
accounts = config.get("general", "accounts")
accounts = accounts.replace(" ", "")
accounts = accounts.split(",")
server = None
remoterepos = None
localrepos = None
2002-06-20 04:55:24 +02:00
for accountname in accounts:
print "Processing account " + accountname
accountmetadata = os.path.join(metadatadir, accountname)
if not os.path.exists(accountmetadata):
os.mkdir(accountmetadata, 0700)
host = config.get(accountname, "remotehost")
user = config.get(accountname, "remoteuser")
port = None
if config.has_option(accountname, "remoteport"):
port = config.getint(accountname, "remoteport")
password = None
if config.has_option(accountname, "remotepass"):
password = config.get(accountname, "remotepass")
else:
password = getpass.getpass("Password for %s: " % accountname)
ssl = config.getboolean(accountname, "ssl")
# Connect to the remote server.
2002-06-20 04:55:24 +02:00
server = imapserver.IMAPServer(user, password, host, port, ssl)
remoterepos = repository.IMAP.IMAPRepository(server)
# Connect to the Maildirs.
2002-06-20 04:55:24 +02:00
localrepos = repository.Maildir.MaildirRepository(os.path.expanduser(config.get(accountname, "localfolders")))
# Connect to the local cache.
statusrepos = repository.LocalStatus.LocalStatusRepository(accountmetadata)
2002-06-20 04:55:24 +02:00
print "Synchronizing folder list..."
remoterepos.syncfoldersto(localrepos)
print "Done."
for remotefolder in remoterepos.getfolders():
print "*** SYNCHRONIZING FOLDER %s" % remotefolder.getname()
2002-06-21 04:31:05 +02:00
# Load local folder.
2002-06-20 04:55:24 +02:00
localfolder = localrepos.getfolder(remotefolder.getname())
2002-06-21 04:31:05 +02:00
if not localfolder.isuidvalidityok(remotefolder):
print 'UID validity is a problem for this folder; skipping.'
continue
print "Reading local message list...",
localfolder.cachemessagelist()
print len(localfolder.getmessagelist().keys()), "messages."
2002-06-21 04:31:05 +02:00
# Load remote folder.
print "Reading remote message list...",
remotefolder.cachemessagelist()
print len(remotefolder.getmessagelist().keys()), "messages."
# Load status folder.
statusfolder = statusrepos.getfolder(remotefolder.getname())
2002-06-21 06:04:47 +02:00
statusfolder.cachemessagelist()
2002-06-21 04:31:05 +02:00
if statusfolder.isnewfolder():
print "Local status folder is new; ignoring."
else:
print "Synchronizing local changes."
localfolder.syncmessagesto(statusfolder, [remotefolder, statusfolder])
# Synchronize remote changes.
2002-06-20 16:17:20 +02:00
print "Synchronizing remote to local..."
2002-06-21 06:04:47 +02:00
remotefolder.syncmessagesto(localfolder)
2002-06-21 04:31:05 +02:00
# Make sure the status folder is up-to-date.
print "Updating local status cache..."
localfolder.syncmessagesto(statusfolder)
statusfolder.save()
2002-06-20 04:55:24 +02:00