Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/env python
import sys
import os
import re
import subprocess
def get_message_path(msgid):
cmd = ['notmuch', 'search', '--output=files', 'id:' + msgid]
return subprocess.check_output(cmd).rstrip()
def report_spam(folder_path):
prog = re.compile('^message-id:\s*<([^>]+)>', re.I)
msgid = None
in_header = True
for line in sys.stdin:
if in_header:
if line == '\r\n' or line == '\n':
in_header = False
else:
m = prog.match(line)
if m:
msgid = m.group(1)
if msgid:
msg_path = get_message_path(msgid)
filename = os.path.basename(msg_path)
dst_path = os.path.join(folder_path, 'spam', 'cur', filename)
os.rename(msg_path, dst_path)
subprocess.check_call(['spamassassin', '--report', dst_path])
subprocess.check_call(['notmuch', 'new', '--no-hooks'])
else:
sys.exit(1)
if __name__ == '__main__':
print 'spam reporting...'
report_spam(sys.argv[1])
#!/usr/bin/env python
import sys
import os
import re
import subprocess
def get_message_path(msgid):
cmd = ['notmuch', 'search', '--output=files', 'id:' + msgid]
return subprocess.check_output(cmd).rstrip()
def get_message_tags(msgid):
cmd = ['notmuch', 'search', '--output=tags', 'id:' + msgid]
return subprocess.check_output(cmd).rstrip().split('\n')
def get_delivered_dir(tags):
prog = re.compile('^delivered-to-(.+)$')
for tag in tags:
m = prog.match(tag)
if m:
return m.group(1)
return None
def revoke_spam(folder_path):
prog = re.compile('^message-id:\s*<([^>]+)>', re.I)
msgid = None
in_header = True
for line in sys.stdin:
if in_header:
if line == '\r\n' or line == '\n':
in_header = False
else:
m = prog.match(line)
if m:
msgid = m.group(1)
if msgid:
msg_path = get_message_path(msgid)
msg_tags = get_message_tags(msgid)
delivered = get_delivered_dir(msg_tags)
filename = os.path.basename(msg_path)
dst_path = os.path.join(folder_path, delivered, 'cur', filename)
subprocess.check_call('spamassassin --remove-markup %s > %s' % (msg_path, dst_path), shell=True)
subprocess.check_call(['spamassassin', '--revoke', dst_path])
os.remove(msg_path)
subprocess.check_call(['notmuch', 'new', '--no-hooks'])
else:
sys.exit(1)
if __name__ == '__main__':
print 'spam revoking...'
revoke_spam(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment