Skip to content

Instantly share code, notes, and snippets.

@tai2
Created February 12, 2016 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tai2/d186222bc9755c943e6f to your computer and use it in GitHub Desktop.
Save tai2/d186222bc9755c943e6f to your computer and use it in GitHub Desktop.
#!/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