2002-06-21 07:54:27 +02:00
|
|
|
#!/usr/bin/python2.2
|
2002-06-19 04:21:27 +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
|
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
from offlineimap import imaplib, imaputil, imapserver, repository, folder, mbnames, threadutil, version
|
2002-07-05 04:34:39 +02:00
|
|
|
from offlineimap.threadutil import InstanceLimitedThread, ExitNotifyThread
|
2002-06-21 12:01:10 +02:00
|
|
|
import re, os, os.path, offlineimap, sys
|
2002-06-20 04:55:24 +02:00
|
|
|
from ConfigParser import ConfigParser
|
2002-07-04 02:02:10 +02:00
|
|
|
from threading import *
|
2002-07-12 09:51:52 +02:00
|
|
|
from getopt import getopt
|
2002-06-19 04:21:27 +02:00
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
options = {}
|
|
|
|
if '--help' in sys.argv[1:]:
|
|
|
|
sys.stdout.write(version.cmdhelp + "\n")
|
|
|
|
sys.exit(0)
|
|
|
|
|
2002-07-23 03:48:15 +02:00
|
|
|
for optlist in getopt(sys.argv[1:], 'P:1oa:c:du:h')[0]:
|
2002-07-12 09:51:52 +02:00
|
|
|
options[optlist[0]] = optlist[1]
|
2002-07-12 06:08:27 +02:00
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-d' in options:
|
2002-07-05 13:46:55 +02:00
|
|
|
imaplib.Debug = 5
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-h' in options:
|
|
|
|
sys.stdout.write(version.cmdhelp)
|
|
|
|
sys.stdout.write("\n")
|
|
|
|
sys.exit(0)
|
|
|
|
configfilename = os.path.expanduser("~/.offlineimaprc")
|
|
|
|
if '-c' in options:
|
|
|
|
configfilename = options['-c']
|
2002-07-23 03:48:15 +02:00
|
|
|
if '-P' in options:
|
|
|
|
if not '-1' in options:
|
|
|
|
sys.stderr.write("FATAL: profile mode REQUIRES -1\n")
|
|
|
|
sys.exit(100)
|
|
|
|
profiledir = options['-P']
|
|
|
|
os.mkdir(profiledir)
|
|
|
|
threadutil.setprofiledir(profiledir)
|
|
|
|
sys.stderr.write("WARNING: profile mode engaged;\n{otentially large data will be created in " + profiledir + "\n")
|
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
|
2002-06-22 15:04:55 +02:00
|
|
|
|
2002-06-20 04:55:24 +02:00
|
|
|
config = ConfigParser()
|
2002-06-21 07:54:27 +02:00
|
|
|
if not os.path.exists(configfilename):
|
|
|
|
sys.stderr.write(" *** Config file %s does not exist; aborting!\n" % configfilename)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
config.read(configfilename)
|
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-u' in options:
|
|
|
|
ui = offlineimap.ui.detector.getUImod(options['-u'])()
|
|
|
|
else:
|
|
|
|
ui = offlineimap.ui.detector.findUI(config)
|
2002-07-12 06:08:27 +02:00
|
|
|
ui.init_banner()
|
|
|
|
|
2002-07-16 01:50:52 +02:00
|
|
|
if '-o' in options and config.has_option("general", "autorefresh"):
|
|
|
|
config.remove_option("general", "autorefresh")
|
2002-07-12 06:08:27 +02:00
|
|
|
|
2002-06-20 04:55:24 +02:00
|
|
|
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")
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-a' in options:
|
|
|
|
accounts = options['-a']
|
2002-06-20 04:55:24 +02:00
|
|
|
accounts = accounts.replace(" ", "")
|
|
|
|
accounts = accounts.split(",")
|
2002-06-19 07:22:21 +02:00
|
|
|
|
2002-06-20 08:26:28 +02:00
|
|
|
server = None
|
|
|
|
remoterepos = None
|
|
|
|
localrepos = None
|
2002-07-04 02:02:10 +02:00
|
|
|
passwords = {}
|
2002-07-05 08:36:34 +02:00
|
|
|
tunnels = {}
|
2002-07-04 05:59:19 +02:00
|
|
|
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-1' in options:
|
|
|
|
threadutil.initInstanceLimit("ACCOUNTLIMIT", 1)
|
|
|
|
else:
|
|
|
|
threadutil.initInstanceLimit("ACCOUNTLIMIT",
|
|
|
|
config.getint("general", "maxsyncaccounts"))
|
2002-07-04 02:02:10 +02:00
|
|
|
|
|
|
|
# We have to gather passwords here -- don't want to have two threads
|
|
|
|
# asking for passwords simultaneously.
|
|
|
|
|
|
|
|
for account in accounts:
|
2002-07-05 08:36:34 +02:00
|
|
|
if config.has_option(account, "preauthtunnel"):
|
|
|
|
tunnels[account] = config.get(account, "preauthtunnel")
|
|
|
|
elif config.has_option(account, "remotepass"):
|
2002-07-04 03:58:12 +02:00
|
|
|
passwords[account] = config.get(account, "remotepass")
|
2002-07-05 07:47:52 +02:00
|
|
|
elif config.has_option(account, "remotepassfile"):
|
2002-07-17 00:36:33 +02:00
|
|
|
passfile = open(os.path.expanduser(config.get(account, "remotepassfile")))
|
2002-07-05 07:47:52 +02:00
|
|
|
passwords[account] = passfile.readline().strip()
|
|
|
|
passfile.close()
|
2002-07-04 02:02:10 +02:00
|
|
|
else:
|
2002-07-04 03:58:12 +02:00
|
|
|
passwords[account] = ui.getpass(account, config)
|
2002-07-04 05:59:19 +02:00
|
|
|
for instancename in ["FOLDER_" + account, "MSGCOPY_" + account]:
|
2002-07-12 09:51:52 +02:00
|
|
|
if '-1' in options:
|
|
|
|
threadutil.initInstanceLimit(instancename, 1)
|
|
|
|
else:
|
|
|
|
threadutil.initInstanceLimit(instancename,
|
|
|
|
config.getint(account, "maxconnections"))
|
2002-06-20 08:26:28 +02:00
|
|
|
|
2002-07-04 03:35:05 +02:00
|
|
|
mailboxes = []
|
2002-07-10 13:18:07 +02:00
|
|
|
servers = {}
|
|
|
|
|
2002-07-04 03:58:12 +02:00
|
|
|
def syncaccount(accountname, *args):
|
2002-07-04 03:35:05 +02:00
|
|
|
# We don't need an account lock because syncitall() goes through
|
|
|
|
# each account once, then waits for all to finish.
|
|
|
|
try:
|
2002-06-21 09:25:24 +02:00
|
|
|
ui.acct(accountname)
|
|
|
|
accountmetadata = os.path.join(metadatadir, accountname)
|
|
|
|
if not os.path.exists(accountmetadata):
|
|
|
|
os.mkdir(accountmetadata, 0700)
|
|
|
|
|
2002-07-05 08:36:34 +02:00
|
|
|
server = None
|
2002-07-10 13:18:07 +02:00
|
|
|
if accountname in servers:
|
|
|
|
server = servers[accountname]
|
2002-07-05 08:36:34 +02:00
|
|
|
else:
|
2002-07-10 13:18:07 +02:00
|
|
|
server = imapserver.ConfigedIMAPServer(config, accountname, passwords)
|
|
|
|
servers[accountname] = server
|
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
remoterepos = repository.IMAP.IMAPRepository(config, accountname, server)
|
|
|
|
|
|
|
|
# Connect to the Maildirs.
|
|
|
|
localrepos = repository.Maildir.MaildirRepository(os.path.expanduser(config.get(accountname, "localfolders")))
|
|
|
|
|
|
|
|
# Connect to the local cache.
|
|
|
|
statusrepos = repository.LocalStatus.LocalStatusRepository(accountmetadata)
|
|
|
|
|
|
|
|
ui.syncfolders(remoterepos, localrepos)
|
|
|
|
remoterepos.syncfoldersto(localrepos)
|
2002-07-24 23:00:02 +02:00
|
|
|
ui.acct(accountname)
|
2002-06-21 09:25:24 +02:00
|
|
|
|
2002-07-04 03:58:12 +02:00
|
|
|
folderthreads = []
|
2002-06-21 09:25:24 +02:00
|
|
|
for remotefolder in remoterepos.getfolders():
|
2002-07-04 05:59:19 +02:00
|
|
|
thread = InstanceLimitedThread(\
|
|
|
|
instancename = 'FOLDER_' + accountname,
|
|
|
|
target = syncfolder,
|
2002-07-11 12:55:32 +02:00
|
|
|
name = "Folder sync %s[%s]" % \
|
2002-07-04 05:59:19 +02:00
|
|
|
(accountname, remotefolder.getvisiblename()),
|
|
|
|
args = (accountname, remoterepos, remotefolder, localrepos,
|
|
|
|
statusrepos))
|
2002-07-05 04:34:39 +02:00
|
|
|
thread.setDaemon(1)
|
2002-07-04 03:58:12 +02:00
|
|
|
thread.start()
|
|
|
|
folderthreads.append(thread)
|
|
|
|
threadutil.threadsreset(folderthreads)
|
2002-07-10 13:18:07 +02:00
|
|
|
if not (config.has_option(accountname, 'holdconnectionopen') and \
|
|
|
|
config.getboolean(accountname, 'holdconnectionopen')):
|
|
|
|
server.close()
|
2002-07-04 03:35:05 +02:00
|
|
|
finally:
|
2002-07-04 05:59:19 +02:00
|
|
|
pass
|
2002-06-21 09:25:24 +02:00
|
|
|
|
2002-07-04 03:58:12 +02:00
|
|
|
def syncfolder(accountname, remoterepos, remotefolder, localrepos,
|
|
|
|
statusrepos):
|
|
|
|
# Load local folder.
|
2002-07-05 02:07:02 +02:00
|
|
|
localfolder = localrepos.\
|
|
|
|
getfolder(remotefolder.getvisiblename().\
|
|
|
|
replace(remoterepos.getsep(), localrepos.getsep()))
|
2002-07-17 00:36:33 +02:00
|
|
|
# Write the mailboxes
|
|
|
|
mailboxes.append({'accountname': accountname,
|
|
|
|
'foldername': localfolder.getvisiblename()})
|
2002-07-12 13:46:36 +02:00
|
|
|
# Load local folder
|
2002-07-04 03:58:12 +02:00
|
|
|
ui.syncingfolder(remoterepos, remotefolder, localrepos, localfolder)
|
|
|
|
ui.loadmessagelist(localrepos, localfolder)
|
|
|
|
localfolder.cachemessagelist()
|
|
|
|
ui.messagelistloaded(localrepos, localfolder, len(localfolder.getmessagelist().keys()))
|
|
|
|
|
2002-07-12 13:46:36 +02:00
|
|
|
# Load status folder.
|
|
|
|
statusfolder = statusrepos.getfolder(remotefolder.getvisiblename().\
|
|
|
|
replace(remoterepos.getsep(),
|
|
|
|
statusrepos.getsep()))
|
|
|
|
statusfolder.cachemessagelist()
|
|
|
|
|
|
|
|
|
|
|
|
# If either the local or the status folder has messages and
|
|
|
|
# there is a UID validity problem, warn and abort.
|
|
|
|
# If there are no messages, UW IMAPd loses UIDVALIDITY.
|
|
|
|
# But we don't really need it if both local folders are empty.
|
|
|
|
# So, in that case, save it off.
|
|
|
|
if (len(localfolder.getmessagelist()) or \
|
|
|
|
len(statusfolder.getmessagelist())) and \
|
|
|
|
not localfolder.isuidvalidityok(remotefolder):
|
|
|
|
ui.validityproblem(remotefolder)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
localfolder.saveuidvalidity(remotefolder.getuidvalidity())
|
|
|
|
|
2002-07-04 03:58:12 +02:00
|
|
|
# Load remote folder.
|
|
|
|
ui.loadmessagelist(remoterepos, remotefolder)
|
|
|
|
remotefolder.cachemessagelist()
|
|
|
|
ui.messagelistloaded(remoterepos, remotefolder,
|
|
|
|
len(remotefolder.getmessagelist().keys()))
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
if not statusfolder.isnewfolder():
|
|
|
|
# Delete local copies of remote messages. This way,
|
|
|
|
# if a message's flag is modified locally but it has been
|
|
|
|
# deleted remotely, we'll delete it locally. Otherwise, we
|
|
|
|
# try to modify a deleted message's flags! This step
|
|
|
|
# need only be taken if a statusfolder is present; otherwise,
|
|
|
|
# there is no action taken *to* the remote repository.
|
|
|
|
|
|
|
|
remotefolder.syncmessagesto_delete(localfolder, [localfolder,
|
|
|
|
statusfolder])
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
2002-07-04 03:35:05 +02:00
|
|
|
def syncitall():
|
2002-07-04 10:35:44 +02:00
|
|
|
global mailboxes
|
2002-07-04 03:35:05 +02:00
|
|
|
mailboxes = [] # Reset.
|
|
|
|
threads = []
|
2002-07-04 08:44:51 +02:00
|
|
|
for accountname in accounts:
|
2002-07-04 05:59:19 +02:00
|
|
|
thread = InstanceLimitedThread(instancename = 'ACCOUNTLIMIT',
|
|
|
|
target = syncaccount,
|
2002-07-11 12:55:32 +02:00
|
|
|
name = "Account sync %s" % accountname,
|
2002-07-04 05:59:19 +02:00
|
|
|
args = (accountname,))
|
2002-07-05 04:34:39 +02:00
|
|
|
thread.setDaemon(1)
|
2002-07-04 03:35:05 +02:00
|
|
|
thread.start()
|
|
|
|
threads.append(thread)
|
|
|
|
# Wait for the threads to finish.
|
2002-07-04 03:58:12 +02:00
|
|
|
threadutil.threadsreset(threads)
|
2002-06-21 09:25:24 +02:00
|
|
|
mbnames.genmbnames(config, mailboxes)
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
def sync_with_timer():
|
|
|
|
currentThread().setExitMessage('SYNC_WITH_TIMER_TERMINATE')
|
|
|
|
syncitall()
|
|
|
|
if config.has_option('general', 'autorefresh'):
|
|
|
|
refreshperiod = config.getint('general', 'autorefresh') * 60
|
|
|
|
while 1:
|
2002-07-10 13:18:07 +02:00
|
|
|
# Set up keep-alives.
|
|
|
|
kaevents = {}
|
|
|
|
kathreads = {}
|
|
|
|
for accountname in accounts:
|
|
|
|
if config.has_option(accountname, 'holdconnectionopen') and \
|
|
|
|
config.getboolean(accountname, 'holdconnectionopen') and \
|
|
|
|
config.has_option(accountname, 'keepalive'):
|
|
|
|
event = Event()
|
|
|
|
kaevents[accountname] = event
|
|
|
|
thread = ExitNotifyThread(target = servers[accountname].keepalive,
|
2002-07-11 12:55:32 +02:00
|
|
|
name = "Keep alive " + accountname,
|
2002-07-10 13:18:07 +02:00
|
|
|
args = (config.getint(accountname, 'keepalive'), event))
|
|
|
|
thread.setDaemon(1)
|
|
|
|
thread.start()
|
|
|
|
kathreads[accountname] = thread
|
2002-07-05 04:34:39 +02:00
|
|
|
if ui.sleep(refreshperiod) == 2:
|
2002-07-10 13:18:07 +02:00
|
|
|
# Cancel keep-alives, but don't bother terminating threads
|
|
|
|
for event in kaevents.values():
|
|
|
|
event.set()
|
2002-07-05 04:34:39 +02:00
|
|
|
break
|
|
|
|
else:
|
2002-07-10 13:18:07 +02:00
|
|
|
# Cancel keep-alives and wait for threads to terminate.
|
|
|
|
for event in kaevents.values():
|
|
|
|
event.set()
|
|
|
|
for thread in kathreads.values():
|
|
|
|
thread.join()
|
2002-07-05 04:34:39 +02:00
|
|
|
syncitall()
|
2002-06-20 04:55:24 +02:00
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
def threadexited(thread):
|
|
|
|
if thread.getExitCause() == 'EXCEPTION':
|
2002-07-11 15:25:53 +02:00
|
|
|
if isinstance(thread.getExitException(), SystemExit):
|
|
|
|
# Bring a SystemExit into the main thread.
|
|
|
|
# Do not send it back to UI layer right now.
|
|
|
|
# Maybe later send it to ui.terminate?
|
|
|
|
raise SystemExit
|
2002-07-05 04:34:39 +02:00
|
|
|
ui.threadException(thread) # Expected to terminate
|
|
|
|
sys.exit(100) # Just in case...
|
|
|
|
os._exit(100)
|
|
|
|
elif thread.getExitMessage() == 'SYNC_WITH_TIMER_TERMINATE':
|
|
|
|
ui.terminate()
|
|
|
|
# Just in case...
|
|
|
|
sys.exit(100)
|
|
|
|
os._exit(100)
|
|
|
|
else:
|
|
|
|
ui.threadExited(thread)
|
|
|
|
|
|
|
|
threadutil.initexitnotify()
|
2002-07-11 15:25:53 +02:00
|
|
|
t = ExitNotifyThread(target=sync_with_timer,
|
|
|
|
name='Sync Runner')
|
2002-07-05 04:34:39 +02:00
|
|
|
t.setDaemon(1)
|
|
|
|
t.start()
|
|
|
|
try:
|
|
|
|
threadutil.exitnotifymonitorloop(threadexited)
|
2002-07-05 04:59:08 +02:00
|
|
|
except SystemExit:
|
|
|
|
raise
|
2002-07-05 04:34:39 +02:00
|
|
|
except:
|
|
|
|
ui.mainException() # Also expected to terminate.
|