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 <mart@martlubbers.net>
This commit is contained in:
Mart Lubbers 2021-09-07 09:38:10 +02:00
parent 4ca9c75c6f
commit 7748de52fb
3 changed files with 23 additions and 17 deletions

View File

@ -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

View File

@ -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')))

View File

@ -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