Commit bfd1c0b6 authored by Markus Kötter's avatar Markus Kötter
Browse files

run - move messaged based on the validation result

parent b1b5637e
......@@ -2,7 +2,34 @@
POC: diagnostic autoresponder for smime multipart/signed
### Installation
#### Requirements
* Python3 (>= 3.5 probably)
* M2Crypto
* asn1crypto
* OpenSSL
* cryptography
* jinja2
###
```commandline
python3.5 -m venv ~/venv-msv/
~/venv-msv/bin/pip install --upgrade pip
# apt-get build-dep m2crypto
# apt-get install python3-dev libpython3-dev
~/venv-msv/bin/pip --requirement requirements.txt
```
### Configuration
```commandline
cp conf/example.ini conf/msv.ini
chmod 0600 conf/msv.conf
# edit conf/msv.ini
```
#### Running
##### debug
```commandline
......
......@@ -7,5 +7,9 @@ password = X
[cmd_run]
mailbox = INBOX
subject = Bitte validieren
action = see
action = move
# see delete move
# move moves the mail to another directory
move_on_error = processed/error
move_on_good = processed/good
move_on_bad = processed/bad
......@@ -336,7 +336,7 @@ def respond(cfg, body, old):
s.sendmail(old['To'], [old['From']], msg.as_string())
def process(cfg, m, data, action):
def process(cfg, m, data, action, moveto=None):
for msgid in filter(lambda x: x != '', data[0].decode('utf-8').split(' ')):
typ, msg = m.fetch(msgid, '(RFC822)')
for part in msg:
......@@ -348,33 +348,41 @@ def process(cfg, m, data, action):
date = datetime.datetime.fromtimestamp(time.mktime(email.utils.parsedate(mail['Date'])))
log.info('Processing {date} "{From}" "{To}" "{Subject}"'.format(date=date, **mail))
# valid is 3 state
# True False None, None is error
try:
valid, p7, data, micalg, digest = validate(mail)
except Exception as e:
log.exception(e)
valid = None
else:
try:
response = render(p7, data, micalg, digest)
# respond(cfg, response, mail)
except Exception as e:
log.exception(e)
valid = None
finally:
if valid is None:
# format error?
pass
elif valid is True:
# valid
pass
else:
# invalid
pass
response = render(p7, data, micalg, digest)
respond(cfg, response, mail)
if action == 'see':
log.debug("Marked as Seen")
m.store(msgid, '+FLAGS', '\Seen')
elif action == 'delete':
m.store(msgid, '+FLAGS', '\Deleted')
m.expunge()
log.debug("Deleted Message")
if action == 'move':
# move
assert valid in moveto
target = moveto[valid]
log.info('Moving to {}'.format(target))
state, _ = m.uid('COPY', msgid, target)
assert state == 'OK'
state, _ = m.store(msgid, '+FLAGS', '\Deleted')
assert state == 'OK'
m.expunge()
elif action == 'see':
log.debug("Marked as Seen")
m.store(msgid, '+FLAGS', '\Seen')
elif action == 'delete':
m.store(msgid, '+FLAGS', '\Deleted')
m.expunge()
log.debug("Deleted Message")
# this msg is processed
break
def main():
......@@ -449,7 +457,7 @@ def main():
t, data = m.search(None, pattern)
assert state == 'OK'
process(cfg, m, data, args.extension, args.directory, args.action)
process(cfg, m, data, args.extension, args.directory, args.action, moveto=None)
except Exception as e:
log.exception(e)
......@@ -473,24 +481,27 @@ def main():
cmd = sub.add_parser('run')
def cmd_run(args):
assert cfg.get('cmd_run', 'action') in ['see','delete']
action = cfg.get('cmd_run', 'action')
assert action in ['see','delete','move']
moveto = {}
if action == 'move':
for k,v in {True:'good',False:'bad',None:'error'}.items():
moveto[k] = cfg.get('cmd_run', 'move_on_{}'.format(v))
pattern = '(UNSEEN) '
pattern += '(SUBJECT "{}") '.format(cfg.get('cmd_run', 'subject'))
action = cfg.get('cmd_run', 'action')
while True:
time.sleep(10)
try:
m = login(cfg)
state, data = m.select(cfg.get('cmd_run','mailbox'), readonly=False)
assert state == 'OK'
while True:
state, data = m.search(None, pattern)
process(cfg, m, data, action)
process(cfg, m, data, action, moveto)
time.sleep(10)
except Exception as e:
log.exception(e)
time.sleep(10)
cmd.set_defaults(func=cmd_run)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment