Calculate sieve file
This commit is contained in:
parent
1a56840e1d
commit
671282a8b0
1 changed files with 23 additions and 14 deletions
|
@ -100,8 +100,8 @@ class ImapFilter:
|
|||
imap_user=mailbox['imap_user'],
|
||||
imap_password_file=mailbox['imap_password_file'],
|
||||
)
|
||||
if mailbox['mailbox'] == 'INBOX' and self.config['sieve_scripts_path']:
|
||||
self._create_sieve_script(mailbox['filters'])
|
||||
if mailbox['mailbox'] == 'INBOX':
|
||||
self._create_sieve_script(mailbox['filters'], mailbox['imap_user'])
|
||||
else:
|
||||
self._log.debug(
|
||||
"Mailbox '%s' won't be processed to create Sieve scripts",
|
||||
|
@ -175,16 +175,23 @@ class ImapFilter:
|
|||
)
|
||||
return True
|
||||
|
||||
def _create_sieve_script(self, filters):
|
||||
def _create_sieve_script(self, filters, user_email):
|
||||
owner = None
|
||||
group = None
|
||||
if os.path.exists(self.config['sieve_scripts_path']):
|
||||
mail_domain, mail_user = user_email.split('@')
|
||||
sieve_scripts_path = os.path.join(
|
||||
self.config['mail_home_path'],
|
||||
mail_domain,
|
||||
mail_user,
|
||||
'.dovecot.sieve'
|
||||
)
|
||||
if os.path.exists(sieve_scripts_path):
|
||||
self._log.debug(
|
||||
"Updating Sieve script in '%s'...",
|
||||
self.config['sieve_scripts_path']
|
||||
sieve_scripts_path
|
||||
)
|
||||
if os.geteuid() == 0:
|
||||
file_path = Path(self.config['sieve_scripts_path'])
|
||||
file_path = Path(sieve_scripts_path)
|
||||
owner = file_path.owner()
|
||||
group = file_path.group()
|
||||
self._log.debug(
|
||||
|
@ -192,12 +199,12 @@ class ImapFilter:
|
|||
owner,
|
||||
group
|
||||
)
|
||||
with open(self.config['sieve_scripts_path'], 'r', encoding='UTF-8') as sieve_script:
|
||||
with open(sieve_scripts_path, 'r', encoding='UTF-8') as sieve_script:
|
||||
content = sieve_script.read()
|
||||
else:
|
||||
self._log.debug(
|
||||
"Will create Sieve script in '%s'...",
|
||||
self.config['sieve_scripts_path']
|
||||
sieve_scripts_path
|
||||
)
|
||||
content = '# Managed by imap_filter. Some lines might get replaced if the match with rule name from imap_filter'
|
||||
content += 'require ["fileinto"];'
|
||||
|
@ -258,16 +265,16 @@ class ImapFilter:
|
|||
content += f"{replacement}\n"
|
||||
self._log.debug(
|
||||
"Writting sieve script '%s' with %s bytes",
|
||||
self.config['sieve_scripts_path'],
|
||||
sieve_scripts_path,
|
||||
len(content)
|
||||
)
|
||||
with open(self.config['sieve_scripts_path'], 'w', encoding='UTF-8') as sieve_script:
|
||||
with open(sieve_scripts_path, 'w', encoding='UTF-8') as sieve_script:
|
||||
sieve_script.write(content)
|
||||
if os.geteuid() == 0:
|
||||
if owner and group:
|
||||
os.chown(self.config['sieve_scripts_path'], owner, group)
|
||||
os.chown(sieve_scripts_path, owner, group)
|
||||
os.chmod(
|
||||
self.config['sieve_scripts_path'],
|
||||
sieve_scripts_path,
|
||||
0o777
|
||||
)
|
||||
return True
|
||||
|
@ -535,10 +542,12 @@ class ImapFilter:
|
|||
help='JSON or YAML file containing a list of dictionaries with the filter rules.'
|
||||
)
|
||||
@click.option(
|
||||
'--sieve-scripts-path',
|
||||
'--mail-home-path',
|
||||
'-P',
|
||||
help='Path to create sieve scripts.'
|
||||
default='/home/vmail',
|
||||
help='Path to mail files in dovecot.'
|
||||
)
|
||||
|
||||
@click_config_file.configuration_option()
|
||||
def __main__(**kwargs):
|
||||
return ImapFilter(**kwargs)
|
||||
|
|
Loading…
Reference in a new issue