From 7748de52fbf8495de1948d280ef2d40dc3aaf535 Mon Sep 17 00:00:00 2001 From: Mart Lubbers Date: Tue, 7 Sep 2021 09:38:10 +0200 Subject: [PATCH] Communicate syncmode to synchooks This patch sets the environment variable OFFLINEIMAPSYNCMODE to either full, quick or idle depending on the context of the pre- and postsynchook. Adding the context as an argument was considered but this would break existing configurations and it makes calling a program directly more cumbersome. Some programs (e.g. imapfilter) may not know what to do with this extra argument. Signed-off-by: Mart Lubbers --- offlineimap.conf | 3 +++ offlineimap/accounts.py | 33 ++++++++++++++++++--------------- offlineimap/imapserver.py | 4 ++-- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/offlineimap.conf b/offlineimap.conf index 6d01fe7..7e257c7 100644 --- a/offlineimap.conf +++ b/offlineimap.conf @@ -320,6 +320,9 @@ remoterepository = RemoteExample # # The pre sync script has to complete before a sync to the account will start. # +# The environment variable OFFLINEIMAPSYNCMODE will be set to either full, +# quick or idle to communicate the syncmode to the external command. +# #presynchook = imapfilter -c someotherconfig.lua #postsynchook = notifysync.sh diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index 4cb15ab..86e5b88 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -338,18 +338,6 @@ class SyncableAccount(Account): folderthreads = [] - hook = self.getconf('presynchook', '') - self.callhook(hook) - - if self.utf_8_support and self.remoterepos.getdecodefoldernames(): - raise OfflineImapError("Configuration mismatch in account " + - "'%s'. " % self.getname() + - "\nAccount setting 'utf8foldernames' and repository " + - "setting 'decodefoldernames'\nmay not be used at the " + - "same time. This account has not been synchronized.\n" + - "Please check the configuration and documentation.", - OfflineImapError.ERROR.REPO) - quickconfig = self.getconfint('quick', 0) if quickconfig < 0: quick = True @@ -363,6 +351,19 @@ class SyncableAccount(Account): else: quick = False + + hook = self.getconf('presynchook', '') + self.callhook(hook, "quick" if quick else "full") + + if self.utf_8_support and self.remoterepos.getdecodefoldernames(): + raise OfflineImapError("Configuration mismatch in account " + + "'%s'. " % self.getname() + + "\nAccount setting 'utf8foldernames' and repository " + + "setting 'decodefoldernames'\nmay not be used at the " + + "same time. This account has not been synchronized.\n" + + "Please check the configuration and documentation.", + OfflineImapError.ERROR.REPO) + try: startedThread = False remoterepos = self.remoterepos @@ -444,9 +445,9 @@ class SyncableAccount(Account): remoterepos.holdordropconnections() hook = self.getconf('postsynchook', '') - self.callhook(hook) + self.callhook(hook, "quick" if quick else "full") - def callhook(self, cmd): + def callhook(self, cmd, syncmode): # Check for CTRL-C or SIGTERM and run postsynchook. if Account.abort_NOW_signal.is_set(): return @@ -456,9 +457,11 @@ class SyncableAccount(Account): self.ui.callhook("Calling hook: " + cmd) if self.dryrun: return + environ = os.environ.copy() + environ['OFFLINEIMAPSYNCMODE'] = syncmode p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, - close_fds=True) + close_fds=True, env=environ) stdout, stderr = p.communicate() self.ui.callhook("Hook stdout: %s\nHook stderr:%s\n" % (stdout.decode('utf-8'), stderr.decode('utf-8'))) diff --git a/offlineimap/imapserver.py b/offlineimap/imapserver.py index 6acb331..0102083 100644 --- a/offlineimap/imapserver.py +++ b/offlineimap/imapserver.py @@ -832,10 +832,10 @@ class IdleThread: remotefolder = remoterepos.getfolder(self.folder, decode=False) hook = account.getconf('presynchook', '') - account.callhook(hook) + account.callhook(hook, "idle") offlineimap.accounts.syncfolder(account, remotefolder, quick=False) hook = account.getconf('postsynchook', '') - account.callhook(hook) + account.callhook(hook, "idle") ui = getglobalui() ui.unregisterthread(currentThread()) # syncfolder registered the thread