import base64
import sys

# Usage: email_decompress.py mailfile keyword
# e.g. email_decompress.py noa/Mail/inbox/1 angry

infile = sys.argv[1]

f=open(infile,'r').read()
xbtoa = False
xbtoa_str = ''
for line in f.split('\n'):
    line=line.strip()
    if 'xbtoa End' in line:
        xbtoa = False
    if xbtoa:
        xbtoa_str += line
    if 'xbtoa Begin' in line:
        xbtoa = True
        
decoded = bytearray(base64.a85decode(xbtoa_str))

keyword = sys.argv[2]+'\x00'

for i in range(len(decoded)):
    keychar = keyword[i%len(keyword)]
    decoded[i] ^= ord(keychar)




f2=open(infile.replace('.mail','')+'.tar.Z','wb')
f2.write(decoded)
f2.close()
