OfflineIMAP / offlineimap

Read/sync your IMAP mailboxes (python2) [LEGACY: move to offlineimap3]
http://www.offlineimap.org
Other
1.78k stars 360 forks source link

How can I pass -f %(foldername) variable, specified in cmd, to postsynchook? #636

Closed 7abbix closed 5 years ago

7abbix commented 5 years ago

General informations

Configuration file offlineimaprc

[general]
accounts = Alerts

[Account Alerts]
foldername = TEST
localrepository = AlertsLocal
remoterepository = AlertsRemote
postsynchook = /srv/system/check_alerts.sh "%(foldername)"

[Repository AlertsRemote]
type = IMAP
remotehost = imap.gmail.com
remoteuser = alerts@contoso.com

## remove Gmail prefix on IMAP folders
nametrans = lambda f: f.replace('[Gmail]/', '') if f.startswith('[Gmail]/') else f

[Repository AlertsLocal]
type = Maildir
localfolders = /var/spool/mail/alerts
restoreatime = no
folderfilter = lambda foldername: foldername not in ['[Gmail]/All Mail']

pythonfile (if any)

REMOVE PRIVATE DATA.

Logs, error

Account sync Alerts:
 ERROR: While attempting to sync account 'Alerts'
  bad interpolation variable reference '%(foldername)"'
 *** Finished account 'Alerts' in 0:03
ERROR: Exceptions occurred during the run!
ERROR: While attempting to sync account 'Alerts'
  bad interpolation variable reference '%(foldername)"'

Traceback:
  File "/usr/lib/python2.7/site-packages/offlineimap/accounts.py", line 283, in syncrunner
    self.__sync()
  File "/usr/lib/python2.7/site-packages/offlineimap/accounts.py", line 431, in __sync
    hook = self.getconf('postsynchook', '')
  File "/usr/lib/python2.7/site-packages/offlineimap/CustomConfig.py", line 237, in getconf
    self.getconfig().get)
  File "/usr/lib/python2.7/site-packages/offlineimap/CustomConfig.py", line 203, in _confighelper_runner
    return defaultfunc(*(lst + list(args)))
  File "/usr/lib/python2.7/site-packages/offlineimap/CustomConfig.py", line 38, in getdefault
    return self.get(*(section, option) + args, **kwargs)
  File "/usr/lib64/python2.7/ConfigParser.py", line 623, in get
    return self._interpolate(section, option, value, d)
  File "/usr/lib64/python2.7/ConfigParser.py", line 691, in _interpolate
    self._interpolate_some(option, L, rawval, section, vars, 1)
  File "/usr/lib64/python2.7/ConfigParser.py", line 716, in _interpolate_some
    "bad interpolation variable reference %r" % rest)

Steps to reproduce the error

I tried to use "%(foldername)", "%folder" and "%(folderfilter)" and none of these worked.

Is there a way to achieve this?

What comes to mind, is that "-f", specified in cmd arguments, should override folderfilter and hence "%(folderfilter)" used in config file should reflect overridden change, "%(folderfilter)" doesn't expand at all.

But even test variable expansion "%(foldername)" doesn't work at all. No matter it is specified in [DEFFAULT] section or in the section, where it is used.

I've found lots of offlineimaprc configuration examples on the internet, that show things like:

postsynchook = /srv/system/check_alerts.sh "%(foldername)" but even variable from config file doesn't work.

Anything I'm missing to enable variable usage?

nicolas33 commented 5 years ago

The best workaround I can come with now is to write a wrapper to offlineimap and store the requested foldername(s) in a file which is read by the postsynchook.

7abbix commented 5 years ago

The best workaround I can come with now is to write a wrapper to offlineimap and store the requested foldername(s) in a file which is read by the postsynchook.

Hi, thanks for reply. Could you please explain, why this interpolation functionality doesn't work? I can see here https://github.com/OfflineIMAP/offlineimap/blob/master/offlineimap.conf starting at line 27, that interpolation supposed to be supported. Is this some kind of exception specifically for hooks ?

As you can see in my configuration example, I have there defined the following:

[Account Alerts] foldername = TEST

This supposed to expand %(foldername) as TEST, however offlineimap fails with error "bad interpolation variable".

7abbix commented 5 years ago

Also, this official documentation demonstrates exactly the case I tried to implement: http://www.offlineimap.org/doc/ConfigurationFactorization.html

Using interpolation It’s possible to combine values from others.

[Account Example] localrepository = LocalExample remoterepository = RemoteExample

mailfilter = imapfilter -c presynchook = %(mailfilter) someconfig.lua

Is this documentation outdated and this functionality doesn't work anymore or am I missing something in my configuration?

7abbix commented 5 years ago

So far I have found that this configuration:

[DEFAULT]
testvar = ABCD

[general]
accounts = Alerts

[Account Alerts]
localrepository = AlertsLocal
remoterepository = AlertsRemote
postsynchook = /srv/system/test_hook.sh %(testvar)s

Works! I get the following output:

Account sync Alerts:
 Calling hook: /srv/system/test_hook.sh ABCD
 Hook stdout: Supplied Argument: ABCD

Hook stderr:

 Hook return code: 0
 *** Finished account 'Alerts' in 0:01

It is mandatory to add the trailing "s" in interpolated variable expansion. This was not clear from docs, however.

7abbix commented 5 years ago

I got also working overriding variable via command like "-k" switch. Everything works fine. The issue can be closed.

nicolas33 commented 5 years ago

Oh, sorry. I didn't get that the interpolation default variable was in your configuration file.