Skip to content

Instantly share code, notes, and snippets.

@mirhec
Last active January 2, 2016 19:39
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 mirhec/1e677ee1bd42a119ef29 to your computer and use it in GitHub Desktop.
Save mirhec/1e677ee1bd42a119ef29 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2.7
import os
import sys
try:
authorized = False
directory = None
addr = None
with open('moveto.log', 'w') as f:
f.write('start ...\n')
if len(sys.argv) < 2:
f.write('No main address specified. Please set the authorized email address as argument.\n')
os._exit(02)
f.write('Main address is "%s"\n' % sys.argv[1])
while 1:
line = sys.stdin.readline()
f.write(' -> %s' % line)
if not line or line is '':
break
if line in ("\r\n", "\n"):
break
if 'From: ' in line and sys.argv[1] in line:
authorized = True
if 'To: moveto-' in line:
directory = line[line.index('To: moveto-') + 11:]
directory = directory[0:directory.index('@')]
if 'Subject: ' in line:
addr = line[line.index('Subject: ') + 9:-1].split(',')
if (not directory and not addr) or not authorized:
f.write('Could not find moveto mail address or subject address.\n')
else:
f.write('Move all mails from %s to %s' % (str(addr), directory,))
with open('mailrules.cfg', 'a') as f2:
for adr in addr:
f2.write('\n%s %s' % (adr, directory,))
except Exception as e:
with open('moveto.log', 'w') as f:
f.write('Error occured: %s' % str(e))
os._exit(81) # internal error
|./.moveto.py your@email-address.tld
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment