2002-06-21 07:29:12 +02:00
|
|
|
# UI base class
|
2011-09-29 17:22:02 +02:00
|
|
|
# Copyright (C) 2002-2011 John Goerzen & contributors
|
2002-06-21 07:29:12 +02:00
|
|
|
#
|
|
|
|
# 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
|
2003-04-16 21:23:45 +02:00
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
2002-06-21 07:29:12 +02:00
|
|
|
#
|
|
|
|
# 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
|
2006-08-12 06:15:55 +02:00
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-03-06 20:03:06 +01:00
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import threading
|
2011-08-11 12:22:34 +02:00
|
|
|
from Queue import Queue
|
2011-01-25 12:22:26 +01:00
|
|
|
import offlineimap
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-05-01 20:18:28 +02:00
|
|
|
debugtypes = {'':'Other offlineimap related sync messages',
|
|
|
|
'imap': 'IMAP protocol debugging',
|
2003-06-02 21:06:18 +02:00
|
|
|
'maildir': 'Maildir repository debugging',
|
|
|
|
'thread': 'Threading debugging'}
|
2002-08-08 22:03:36 +02:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
globalui = None
|
|
|
|
def setglobalui(newui):
|
2011-05-02 17:11:40 +02:00
|
|
|
"""Set the global ui object to be used for logging"""
|
2007-07-05 15:49:54 +02:00
|
|
|
global globalui
|
|
|
|
globalui = newui
|
|
|
|
def getglobalui():
|
2011-05-02 17:11:40 +02:00
|
|
|
"""Return the current ui object"""
|
2007-07-05 15:49:54 +02:00
|
|
|
global globalui
|
|
|
|
return globalui
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
class UIBase:
|
2002-07-25 00:20:42 +02:00
|
|
|
def __init__(s, config, verbose = 0):
|
2002-07-22 07:40:29 +02:00
|
|
|
s.verbose = verbose
|
2002-07-25 00:20:42 +02:00
|
|
|
s.config = config
|
2002-08-08 22:03:36 +02:00
|
|
|
s.debuglist = []
|
2007-07-05 15:49:54 +02:00
|
|
|
s.debugmessages = {}
|
|
|
|
s.debugmsglen = 50
|
2003-01-05 12:50:01 +01:00
|
|
|
s.threadaccounts = {}
|
2011-09-29 15:43:01 +02:00
|
|
|
"""dict linking active threads (k) to account names (v)"""
|
2011-09-29 17:22:02 +02:00
|
|
|
s.acct_startimes = {}
|
|
|
|
"""linking active accounts with the time.time() when sync started"""
|
2003-06-02 21:06:18 +02:00
|
|
|
s.logfile = None
|
2011-08-11 12:22:34 +02:00
|
|
|
s.exc_queue = Queue()
|
|
|
|
"""saves all occuring exceptions, so we can output them at the end"""
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## UTILS
|
|
|
|
def _msg(s, msg):
|
|
|
|
"""Generic tool called when no other works."""
|
2003-06-02 21:06:18 +02:00
|
|
|
s._log(msg)
|
|
|
|
s._display(msg)
|
|
|
|
|
|
|
|
def _log(s, msg):
|
|
|
|
"""Log it to disk. Returns true if it wrote something; false
|
|
|
|
otherwise."""
|
|
|
|
if s.logfile:
|
|
|
|
s.logfile.write("%s: %s\n" % (threading.currentThread().getName(),
|
|
|
|
msg))
|
2003-06-02 21:52:33 +02:00
|
|
|
return 1
|
|
|
|
return 0
|
2003-06-02 21:06:18 +02:00
|
|
|
|
|
|
|
def setlogfd(s, logfd):
|
|
|
|
s.logfile = logfd
|
2006-10-19 03:04:28 +02:00
|
|
|
logfd.write("This is %s %s\n" % \
|
2010-12-22 12:35:41 +01:00
|
|
|
(offlineimap.__productname__,
|
|
|
|
offlineimap.__version__))
|
2003-06-02 21:52:33 +02:00
|
|
|
logfd.write("Python: %s\n" % sys.version)
|
|
|
|
logfd.write("Platform: %s\n" % sys.platform)
|
|
|
|
logfd.write("Args: %s\n" % sys.argv)
|
2003-06-02 21:06:18 +02:00
|
|
|
|
|
|
|
def _display(s, msg):
|
|
|
|
"""Display a message."""
|
2002-07-22 08:02:24 +02:00
|
|
|
raise NotImplementedError
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-10-30 05:26:49 +01:00
|
|
|
def warn(s, msg, minor = 0):
|
|
|
|
if minor:
|
|
|
|
s._msg("warning: " + msg)
|
|
|
|
else:
|
|
|
|
s._msg("WARNING: " + msg)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-08-11 12:22:34 +02:00
|
|
|
def error(self, exc, exc_traceback=None, msg=None):
|
|
|
|
"""Log a message at severity level ERROR
|
|
|
|
|
|
|
|
Log Exception 'exc' to error log, possibly prepended by a preceding
|
|
|
|
error "msg", detailing at what point the error occurred.
|
|
|
|
|
|
|
|
In debug mode, we also output the full traceback that occurred
|
2011-08-14 10:42:21 +02:00
|
|
|
if one has been passed in via sys.info()[2].
|
2011-08-11 12:22:34 +02:00
|
|
|
|
|
|
|
Also save the Exception to a stack that can be output at the end
|
|
|
|
of the sync run when offlineiamp exits. It is recommended to
|
|
|
|
always pass in exceptions if possible, so we can give the user
|
|
|
|
the best debugging info.
|
|
|
|
|
|
|
|
One example of such a call might be:
|
|
|
|
|
2011-08-14 10:42:21 +02:00
|
|
|
ui.error(exc, sys.exc_info()[2], msg="While syncing Folder %s in "
|
2011-08-11 12:22:34 +02:00
|
|
|
"repo %s")
|
|
|
|
"""
|
|
|
|
if msg:
|
2011-09-19 19:59:39 +02:00
|
|
|
self._msg("ERROR: %s\n %s" % (msg, exc))
|
2011-08-11 12:22:34 +02:00
|
|
|
else:
|
2011-09-19 19:59:39 +02:00
|
|
|
self._msg("ERROR: %s" % (exc))
|
2011-08-11 12:22:34 +02:00
|
|
|
|
|
|
|
if not self.debuglist:
|
|
|
|
# only output tracebacks in debug mode
|
|
|
|
exc_traceback = None
|
|
|
|
# push exc on the queue for later output
|
|
|
|
self.exc_queue.put((msg, exc, exc_traceback))
|
|
|
|
if exc_traceback:
|
|
|
|
self._msg(traceback.format_tb(exc_traceback))
|
|
|
|
|
2011-09-29 15:43:01 +02:00
|
|
|
def registerthread(self, account):
|
|
|
|
"""Register current thread as being associated with an account name"""
|
|
|
|
cur_thread = threading.currentThread()
|
|
|
|
if cur_thread in self.threadaccounts:
|
|
|
|
# was already associated with an old account, update info
|
|
|
|
self.debug('thread', "Register thread '%s' (previously '%s', now "
|
|
|
|
"'%s')" % (cur_thread.getName(),
|
|
|
|
self.getthreadaccount(cur_thread), account))
|
|
|
|
else:
|
|
|
|
self.debug('thread', "Register new thread '%s' (account '%s')" %\
|
|
|
|
(cur_thread.getName(), account))
|
|
|
|
self.threadaccounts[cur_thread] = account
|
|
|
|
|
|
|
|
def unregisterthread(self, thr):
|
|
|
|
"""Unregister a thread as being associated with an account name"""
|
|
|
|
if self.threadaccounts.has_key(thr):
|
|
|
|
del self.threadaccounts[thr]
|
|
|
|
self.debug('thread', "Unregister thread '%s'" % thr.getName())
|
|
|
|
|
|
|
|
def getthreadaccount(self, thr = None):
|
|
|
|
"""Get name of account for a thread (current if None)"""
|
2003-01-05 13:01:17 +01:00
|
|
|
if not thr:
|
|
|
|
thr = threading.currentThread()
|
2011-09-29 15:43:01 +02:00
|
|
|
if thr in self.threadaccounts:
|
|
|
|
return self.threadaccounts[thr]
|
|
|
|
return '*Control' # unregistered thread is '*Control'
|
2003-01-05 12:50:01 +01:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def debug(s, debugtype, msg):
|
|
|
|
thisthread = threading.currentThread()
|
|
|
|
if s.debugmessages.has_key(thisthread):
|
|
|
|
s.debugmessages[thisthread].append("%s: %s" % (debugtype, msg))
|
|
|
|
else:
|
|
|
|
s.debugmessages[thisthread] = ["%s: %s" % (debugtype, msg)]
|
|
|
|
|
|
|
|
while len(s.debugmessages[thisthread]) > s.debugmsglen:
|
|
|
|
s.debugmessages[thisthread] = s.debugmessages[thisthread][1:]
|
|
|
|
|
2003-06-02 21:52:33 +02:00
|
|
|
if debugtype in s.debuglist:
|
|
|
|
if not s._log("DEBUG[%s]: %s" % (debugtype, msg)):
|
2003-06-02 21:06:18 +02:00
|
|
|
s._display("DEBUG[%s]: %s" % (debugtype, msg))
|
2002-08-08 22:03:36 +02:00
|
|
|
|
|
|
|
def add_debug(s, debugtype):
|
|
|
|
global debugtypes
|
|
|
|
if debugtype in debugtypes:
|
|
|
|
if not debugtype in s.debuglist:
|
|
|
|
s.debuglist.append(debugtype)
|
|
|
|
s.debugging(debugtype)
|
|
|
|
else:
|
|
|
|
s.invaliddebug(debugtype)
|
|
|
|
|
|
|
|
def debugging(s, debugtype):
|
|
|
|
global debugtypes
|
|
|
|
s._msg("Now debugging for %s: %s" % (debugtype, debugtypes[debugtype]))
|
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def invaliddebug(s, debugtype):
|
|
|
|
s.warn("Invalid debug type: %s" % debugtype)
|
|
|
|
|
2003-01-30 02:19:53 +01:00
|
|
|
def locked(s):
|
2003-04-29 03:48:55 +02:00
|
|
|
raise Exception, "Another OfflineIMAP is running with the same metadatadir; exiting."
|
2003-01-30 02:19:53 +01:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def getnicename(s, object):
|
2011-05-05 15:59:25 +02:00
|
|
|
"""Return the type of a repository or Folder as string
|
|
|
|
|
|
|
|
(IMAP, Gmail, Maildir, etc...)"""
|
|
|
|
prelimname = object.__class__.__name__.split('.')[-1]
|
2007-07-05 15:49:54 +02:00
|
|
|
# Strip off extra stuff.
|
|
|
|
return re.sub('(Folder|Repository)', '', prelimname)
|
|
|
|
|
2002-07-12 03:38:36 +02:00
|
|
|
def isusable(s):
|
|
|
|
"""Returns true if this UI object is usable in the current
|
|
|
|
environment. For instance, an X GUI would return true if it's
|
|
|
|
being run in X with a valid DISPLAY setting, and false otherwise."""
|
|
|
|
return 1
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## INPUT
|
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def getpass(s, accountname, config, errmsg = None):
|
2002-07-22 08:02:24 +02:00
|
|
|
raise NotImplementedError
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-06-21 08:51:21 +02:00
|
|
|
def folderlist(s, list):
|
|
|
|
return ', '.join(["%s[%s]" % (s.getnicename(x), x.getname()) for x in list])
|
|
|
|
|
2002-08-08 03:57:17 +02:00
|
|
|
################################################## WARNINGS
|
2007-07-05 15:49:54 +02:00
|
|
|
def msgtoreadonly(s, destfolder, uid, content, flags):
|
2007-07-07 03:30:42 +02:00
|
|
|
if not (s.config.has_option('general', 'ignore-readonly') and s.config.getboolean("general", "ignore-readonly")):
|
2002-08-08 03:57:17 +02:00
|
|
|
s.warn("Attempted to synchronize message %d to folder %s[%s], but that folder is read-only. The message will not be copied to that folder." % \
|
2007-07-05 15:49:54 +02:00
|
|
|
(uid, s.getnicename(destfolder), destfolder.getname()))
|
2002-08-08 03:57:17 +02:00
|
|
|
|
2007-07-05 15:49:54 +02:00
|
|
|
def flagstoreadonly(s, destfolder, uidlist, flags):
|
2007-07-07 03:30:42 +02:00
|
|
|
if not (s.config.has_option('general', 'ignore-readonly') and s.config.getboolean("general", "ignore-readonly")):
|
2002-08-08 03:57:17 +02:00
|
|
|
s.warn("Attempted to modify flags for messages %s in folder %s[%s], but that folder is read-only. No flags have been modified for that message." % \
|
|
|
|
(str(uidlist), s.getnicename(destfolder), destfolder.getname()))
|
|
|
|
|
|
|
|
def deletereadonly(s, destfolder, uidlist):
|
2007-07-07 03:30:42 +02:00
|
|
|
if not (s.config.has_option('general', 'ignore-readonly') and s.config.getboolean("general", "ignore-readonly")):
|
2002-08-08 03:57:17 +02:00
|
|
|
s.warn("Attempted to delete messages %s in folder %s[%s], but that folder is read-only. No messages have been deleted in that folder." % \
|
|
|
|
(str(uidlist), s.getnicename(destfolder), destfolder.getname()))
|
|
|
|
|
2002-06-21 07:29:12 +02:00
|
|
|
################################################## MESSAGES
|
|
|
|
|
|
|
|
def init_banner(s):
|
2002-07-12 03:38:36 +02:00
|
|
|
"""Called when the UI starts. Must be called before any other UI
|
|
|
|
call except isusable(). Displays the copyright banner. This is
|
|
|
|
where the UI should do its setup -- TK, for instance, would
|
|
|
|
create the application window here."""
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
2010-12-22 12:35:41 +01:00
|
|
|
s._msg(offlineimap.banner)
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-25 06:46:27 +02:00
|
|
|
def connecting(s, hostname, port):
|
2011-09-29 17:46:46 +02:00
|
|
|
"""Log 'Establishing connection to'"""
|
|
|
|
if s.verbose < 0: return
|
|
|
|
displaystr = ''
|
|
|
|
hostname = hostname if hostname else ''
|
|
|
|
port = "%d" % port if port else ''
|
|
|
|
if hostname:
|
|
|
|
displaystr = ' to %s:%s' % (hostname, port)
|
|
|
|
s._msg("Establishing connection%s" % displaystr)
|
2002-07-25 06:46:27 +02:00
|
|
|
|
2011-09-29 17:22:02 +02:00
|
|
|
def acct(self, account):
|
|
|
|
"""Output that we start syncing an account (and start counting)"""
|
|
|
|
self.acct_startimes[account] = time.time()
|
|
|
|
if self.verbose >= 0:
|
|
|
|
self._msg("*** Processing account %s" % account)
|
|
|
|
|
|
|
|
def acctdone(self, account):
|
|
|
|
"""Output that we finished syncing an account (in which time)"""
|
|
|
|
sec = time.time() - self.acct_startimes[account]
|
|
|
|
del self.acct_startimes[account]
|
|
|
|
self._msg("*** Finished account '%s' in %d:%02d" %
|
|
|
|
(account, sec // 60, sec % 60))
|
2003-01-04 05:57:46 +01:00
|
|
|
|
2011-09-30 09:21:03 +02:00
|
|
|
def syncfolders(self, src_repo, dst_repo):
|
|
|
|
"""Log 'Copying folder structure...'"""
|
|
|
|
if self.verbose < 0: return
|
|
|
|
self.debug('', "Copying folder structure from %s to %s" % \
|
|
|
|
(src_repo, dst_repo))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Folder syncing
|
|
|
|
def syncingfolder(s, srcrepos, srcfolder, destrepos, destfolder):
|
|
|
|
"""Called when a folder sync operation is started."""
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("Syncing %s: %s -> %s" % (srcfolder.getname(),
|
|
|
|
s.getnicename(srcrepos),
|
|
|
|
s.getnicename(destrepos)))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
Daniel Jacobowitz patches
fixes deb#433732
Date: Sun, 30 Sep 2007 13:54:56 -0400
From: Daniel Jacobowitz <drow@false.org>
To: offlineimap@complete.org
Subject: Assorted patches
Here's the result of a lazy Sunday hacking on offlineimap. Sorry for
not breaking this into multiple patches. They're mostly logically
independent so just ask if that would make a difference.
First, a new -q (quick) option. The quick option means to only update
folders that seem to have had significant changes. For Maildir, any
change to any message UID or flags is significant, because checking
the flags doesn't add a significant cost. For IMAP, only a change to
the total number of messages or a change in the UID of the most recent
message is significant. This should catch everything except for
flags changes.
The difference in bandwidth is astonishing: a quick sync takes 80K
instead of 5.3MB, and 28 seconds instead of 90.
There's a configuration variable that lets you say every tenth sync
should update flags, but let all the intervening ones be lighter.
Second, a fix to the UID validity problems many people have been
reporting with Courier. As discussed in Debian bug #433732, I changed
the UID validity check to use SELECT unless the server complains that
the folder is read-only. This avoids the Courier bug (see the Debian
log for more details). This won't fix existing validity errors, you
need to remove the local status and validity files by hand and resync.
Third, some speedups in Maildir checking. It's still pretty slow
due to a combination of poor performance in os.listdir (never reads
more than 4K of directory entries at a time) and some semaphore that
leads to lots of futex wake operations, but at least this saves
20% or so of the CPU time running offlineimap on a single folder:
Time with quick refresh and md5 in loop: 4.75s user 0.46s system 12%
cpu 41.751 total
Time with quick refresh and md5 out of loop: 4.38s user 0.50s system
14% cpu 34.799 total
Time using string compare to check folder: 4.11s user 0.47s system 13%
cpu 34.788 total
And fourth, some display fixes for Curses.Blinkenlights. I made
warnings more visible, made the new quick sync message cyan, and
made all not explicitly colored messages grey. That last one was
really bugging me. Any time OfflineIMAP printed a warning in
this UI, it had even odds of coming out black on black!
Anyway, I hope these are useful. I'm happy to revise them if you see
a problem.
--
Daniel Jacobowitz
CodeSourcery
2007-10-01 23:20:37 +02:00
|
|
|
def skippingfolder(s, folder):
|
|
|
|
"""Called when a folder sync operation is started."""
|
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg("Skipping %s (not changed)" % folder.getname())
|
|
|
|
|
2007-03-15 05:41:43 +01:00
|
|
|
def validityproblem(s, folder):
|
|
|
|
s.warn("UID validity problem for folder %s (repo %s) (saved %d; got %d); skipping it" % \
|
|
|
|
(folder.getname(), folder.getrepository().getname(),
|
|
|
|
folder.getsaveduidvalidity(), folder.getuidvalidity()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def loadmessagelist(s, repos, folder):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Loading message list for %s[%s]" % (s.getnicename(repos),
|
|
|
|
folder.getname()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def messagelistloaded(s, repos, folder, count):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Message list for %s[%s] loaded: %d messages" % \
|
|
|
|
(s.getnicename(repos), folder.getname(), count))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
############################## Message syncing
|
|
|
|
|
|
|
|
def syncingmessages(s, sr, sf, dr, df):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose > 0:
|
2002-07-22 07:38:06 +02:00
|
|
|
s._msg("Syncing messages %s[%s] -> %s[%s]" % (s.getnicename(sr),
|
|
|
|
sf.getname(),
|
|
|
|
s.getnicename(dr),
|
|
|
|
df.getname()))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-09-29 16:51:48 +02:00
|
|
|
def copyingmessage(self, uid, num, num_to_copy, src, destfolder):
|
2011-08-15 09:58:46 +02:00
|
|
|
"""Output a log line stating which message we copy"""
|
2011-09-29 16:51:48 +02:00
|
|
|
if self.verbose < 0: return
|
|
|
|
self._msg("Copy message %s (%d of %d) %s:%s -> %s" % (uid, num,
|
|
|
|
num_to_copy, src.repository, src, destfolder.repository))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
|
|
|
def deletingmessage(s, uid, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Deleting message %d in %s" % (uid, ds))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2002-07-03 07:05:49 +02:00
|
|
|
def deletingmessages(s, uidlist, destlist):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
|
|
|
ds = s.folderlist(destlist)
|
|
|
|
s._msg("Deleting %d messages (%s) in %s" % \
|
|
|
|
(len(uidlist),
|
2011-08-22 12:21:12 +02:00
|
|
|
offlineimap.imaputil.uid_sequence(uidlist),
|
2002-07-22 08:55:27 +02:00
|
|
|
ds))
|
2002-07-03 07:05:49 +02:00
|
|
|
|
2011-03-06 20:03:07 +01:00
|
|
|
def addingflags(s, uidlist, flags, dest):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
2011-03-06 20:03:07 +01:00
|
|
|
s._msg("Adding flag %s to %d messages on %s" % \
|
|
|
|
(", ".join(flags), len(uidlist), dest))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-03-06 20:03:07 +01:00
|
|
|
def deletingflags(s, uidlist, flags, dest):
|
2002-07-22 08:55:27 +02:00
|
|
|
if s.verbose >= 0:
|
2011-03-06 20:03:07 +01:00
|
|
|
s._msg("Deleting flag %s from %d messages on %s" % \
|
|
|
|
(", ".join(flags), len(uidlist), dest))
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-06-30 15:18:03 +02:00
|
|
|
def serverdiagnostics(self, repository, type):
|
|
|
|
"""Connect to repository and output useful information for debugging"""
|
|
|
|
conn = None
|
|
|
|
self._msg("%s repository '%s': type '%s'" % (type, repository.name,
|
|
|
|
self.getnicename(repository)))
|
|
|
|
try:
|
|
|
|
if hasattr(repository, 'gethost'): # IMAP
|
|
|
|
self._msg("Host: %s Port: %s SSL: %s" % (repository.gethost(),
|
|
|
|
repository.getport(),
|
|
|
|
repository.getssl()))
|
|
|
|
try:
|
|
|
|
conn = repository.imapserver.acquireconnection()
|
|
|
|
except OfflineImapError, e:
|
|
|
|
self._msg("Failed to connect. Reason %s" % e)
|
|
|
|
else:
|
|
|
|
if 'ID' in conn.capabilities:
|
|
|
|
self._msg("Server supports ID extension.")
|
|
|
|
#TODO: Debug and make below working, it hangs Gmail
|
|
|
|
#res_type, response = conn.id((
|
|
|
|
# 'name', offlineimap.__productname__,
|
|
|
|
# 'version', offlineimap.__version__))
|
|
|
|
#self._msg("Server ID: %s %s" % (res_type, response[0]))
|
|
|
|
self._msg("Server welcome string: %s" % str(conn.welcome))
|
|
|
|
self._msg("Server capabilities: %s\n" % str(conn.capabilities))
|
|
|
|
repository.imapserver.releaseconnection(conn)
|
|
|
|
if type != 'Status':
|
|
|
|
folderfilter = repository.getconf('folderfilter', None)
|
|
|
|
if folderfilter:
|
|
|
|
self._msg("folderfilter= %s\n" % folderfilter)
|
|
|
|
folderincludes = repository.getconf('folderincludes', None)
|
|
|
|
if folderincludes:
|
|
|
|
self._msg("folderincludes= %s\n" % folderincludes)
|
|
|
|
nametrans = repository.getconf('nametrans', None)
|
|
|
|
if nametrans:
|
|
|
|
self._msg("nametrans= %s\n" % nametrans)
|
|
|
|
|
|
|
|
folders = repository.getfolders()
|
|
|
|
foldernames = [(f.name, f.getvisiblename()) for f in folders]
|
|
|
|
folders = []
|
|
|
|
for name, visiblename in foldernames:
|
|
|
|
if name == visiblename: folders.append(name)
|
|
|
|
else: folders.append("%s -> %s" % (name, visiblename))
|
|
|
|
self._msg("Folderlist: %s\n" % str(folders))
|
|
|
|
finally:
|
|
|
|
if conn: #release any existing IMAP connection
|
|
|
|
repository.imapserver.close()
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
################################################## Threads
|
|
|
|
|
2002-10-17 01:27:27 +02:00
|
|
|
def getThreadDebugLog(s, thread):
|
|
|
|
if s.debugmessages.has_key(thread):
|
|
|
|
message = "\nLast %d debug messages logged for %s prior to exception:\n"\
|
|
|
|
% (len(s.debugmessages[thread]), thread.getName())
|
|
|
|
message += "\n".join(s.debugmessages[thread])
|
|
|
|
else:
|
|
|
|
message = "\nNo debug messages were logged for %s." % \
|
|
|
|
thread.getName()
|
|
|
|
return message
|
|
|
|
|
|
|
|
def delThreadDebugLog(s, thread):
|
|
|
|
if s.debugmessages.has_key(thread):
|
|
|
|
del s.debugmessages[thread]
|
|
|
|
|
|
|
|
def getThreadExceptionString(s, thread):
|
|
|
|
message = "Thread '%s' terminated with exception:\n%s" % \
|
|
|
|
(thread.getName(), thread.getExitStackTrace())
|
|
|
|
message += "\n" + s.getThreadDebugLog(thread)
|
|
|
|
return message
|
|
|
|
|
2002-07-05 04:34:39 +02:00
|
|
|
def threadException(s, thread):
|
|
|
|
"""Called when a thread has terminated with an exception.
|
|
|
|
The argument is the ExitNotifyThread that has so terminated."""
|
2002-10-17 01:27:27 +02:00
|
|
|
s._msg(s.getThreadExceptionString(thread))
|
|
|
|
s.delThreadDebugLog(thread)
|
2002-07-05 04:34:39 +02:00
|
|
|
s.terminate(100)
|
|
|
|
|
2011-08-11 12:22:34 +02:00
|
|
|
def terminate(self, exitstatus = 0, errortitle = None, errormsg = None):
|
2002-07-05 04:34:39 +02:00
|
|
|
"""Called to terminate the application."""
|
2011-08-11 12:22:34 +02:00
|
|
|
#print any exceptions that have occurred over the run
|
|
|
|
if not self.exc_queue.empty():
|
|
|
|
self._msg("\nERROR: Exceptions occurred during the run!")
|
|
|
|
while not self.exc_queue.empty():
|
|
|
|
msg, exc, exc_traceback = self.exc_queue.get()
|
|
|
|
if msg:
|
|
|
|
self._msg("ERROR: %s\n %s" % (msg, exc))
|
2006-12-01 11:54:12 +01:00
|
|
|
else:
|
2011-08-11 12:22:34 +02:00
|
|
|
self._msg("ERROR: %s" % (exc))
|
|
|
|
if exc_traceback:
|
|
|
|
self._msg("\nTraceback:\n%s" %"".join(
|
|
|
|
traceback.format_tb(exc_traceback)))
|
|
|
|
|
|
|
|
if errormsg and errortitle:
|
|
|
|
sys.stderr.write('ERROR: %s\n\n%s\n'%(errortitle, errormsg))
|
|
|
|
elif errormsg:
|
2006-12-01 11:54:12 +01:00
|
|
|
sys.stderr.write('%s\n' % errormsg)
|
2002-07-05 04:34:39 +02:00
|
|
|
sys.exit(exitstatus)
|
|
|
|
|
|
|
|
def threadExited(s, thread):
|
|
|
|
"""Called when a thread has exited normally. Many UIs will
|
|
|
|
just ignore this."""
|
2002-10-17 01:27:27 +02:00
|
|
|
s.delThreadDebugLog(thread)
|
2003-01-05 12:50:01 +01:00
|
|
|
s.unregisterthread(thread)
|
2002-07-05 04:34:39 +02:00
|
|
|
|
2008-10-01 07:03:04 +02:00
|
|
|
################################################## Hooks
|
|
|
|
|
|
|
|
def callhook(s, msg):
|
|
|
|
if s.verbose >= 0:
|
|
|
|
s._msg(msg)
|
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
################################################## Other
|
2002-06-21 07:29:12 +02:00
|
|
|
|
2011-05-07 17:40:32 +02:00
|
|
|
def sleep(s, sleepsecs, account):
|
2002-07-03 08:50:31 +02:00
|
|
|
"""This function does not actually output anything, but handles
|
|
|
|
the overall sleep, dealing with updates as necessary. It will,
|
|
|
|
however, call sleeping() which DOES output something.
|
|
|
|
|
2011-05-07 17:40:32 +02:00
|
|
|
:returns: 0/False if timeout expired, 1/2/True if there is a
|
|
|
|
request to cancel the timer.
|
|
|
|
"""
|
|
|
|
abortsleep = False
|
2002-07-03 08:50:31 +02:00
|
|
|
while sleepsecs > 0 and not abortsleep:
|
2011-05-07 17:40:32 +02:00
|
|
|
if account.get_abort_event():
|
|
|
|
abortsleep = True
|
|
|
|
else:
|
2010-12-05 15:35:01 +01:00
|
|
|
abortsleep = s.sleeping(10, sleepsecs)
|
2011-05-07 17:40:32 +02:00
|
|
|
sleepsecs -= 10
|
|
|
|
s.sleeping(0, 0) # Done sleeping.
|
2002-07-03 08:50:31 +02:00
|
|
|
return abortsleep
|
|
|
|
|
2002-06-21 09:25:24 +02:00
|
|
|
def sleeping(s, sleepsecs, remainingsecs):
|
2010-12-05 15:35:01 +01:00
|
|
|
"""Sleep for sleepsecs, display remainingsecs to go.
|
2002-06-21 09:25:24 +02:00
|
|
|
|
2010-12-05 15:35:01 +01:00
|
|
|
Does nothing if sleepsecs <= 0.
|
2011-09-29 17:58:07 +02:00
|
|
|
Display a message on the screen if we pass a full minute.
|
2010-12-05 15:35:01 +01:00
|
|
|
|
|
|
|
This implementation in UIBase does not support this, but some
|
|
|
|
implementations return 0 for successful sleep and 1 for an
|
|
|
|
'abort', ie a request to sync immediately.
|
|
|
|
"""
|
2002-06-21 09:25:24 +02:00
|
|
|
if sleepsecs > 0:
|
2011-09-29 17:58:07 +02:00
|
|
|
if remainingsecs//60 != (remainingsecs-sleepsecs)//60:
|
|
|
|
s._msg("Next refresh in %.1f minutes" % (remainingsecs/60.0))
|
2002-06-21 09:25:24 +02:00
|
|
|
time.sleep(sleepsecs)
|
|
|
|
return 0
|