Created
June 11, 2009 08:47
-
-
Save suttree/127789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
# Wikemail.py - import your emails into dokuwiki to create | |
# a searchable, wiki-able archive of you mail - Duncan Gough 23/10/04 | |
import getpass, imaplib, re, rfc822, commands, email, os | |
#from TaskThread import TaskThread | |
import threading | |
class ImapWiki(threading.Thread): | |
def __init__(self): | |
threading.Thread.__init__(self) | |
# self._finished = threading.Event() | |
# self._interval = 15.0 | |
# def setInterval(self, interval): | |
# """Set the number of seconds we sleep between executing our task""" | |
# self._interval = interval | |
# def shutdown(self): | |
# """Stop this thread""" | |
# self._finished.set() | |
# def run(self): | |
# while 1: | |
# if self._finished.isSet(): return | |
# self.task() | |
# | |
# # sleep for interval or until shutdown | |
# self._finished.wait(self._interval) | |
def task(self): | |
from_pattern = re.compile('From: (.*)') | |
subject_pattern = re.compile('Subject: (.*)') | |
to_pattern = re.compile('To: (.*)') | |
date_pattern = re.compile('Date: (.*)') | |
body_pattern = re.compile('^\n\s*?',re.MULTILINE) | |
newline_pattern = re.compile('\n', re.MULTILINE) | |
star_pattern = re.compile('\*', re.MULTILINE) | |
M = imaplib.IMAP4('imap.to.infn.it') | |
M.login('fenoglio','') | |
M.select() | |
typ, data = M.search(None, '(UNSEEN UNDELETED)' or '(UNDELETED)') | |
for num in data[0].split(): | |
typ, data = M.fetch(num, '(RFC822)') | |
M.store(num, 'FLAGS', '(UNSEEN)') | |
email_headers, email_body = re.split(r'\n\r?\n', data[0][1], 1) | |
# Munge the email into a usable, wiki friendly format | |
email_from = from_pattern.findall( data[0][1] )[0] | |
email_from = rfc822.parseaddr( email_from )[1] | |
try: | |
email_to = to_pattern.findall( data[0][1] )[0] | |
except: | |
email_to = 'NOMAIL' | |
email_to = rfc822.parseaddr( email_to )[1] | |
try: | |
email_subject = subject_pattern.findall( data[0][1] )[0] | |
except: | |
email_subject = 'NOSUBJECT' | |
rfc822_date = date_pattern.findall( data[0][1] )[0] | |
email_date = rfc822.parsedate( rfc822_date.strip() ) | |
email_from = email_from.strip().lower() | |
try: | |
email_to = email_to.strip().lower() | |
except: | |
email_to = 'NOMAIL' | |
email_subject = email_subject.strip() | |
wiki_date = '%(year)04d %(month)02d %(day)02d' % {'year': email_date[0], 'month': email_date[1], 'day': email_date[2]} | |
wiki_date = str(wiki_date) | |
#wiki_date = str(email_date[0]) + ' ' + str(email_date[1]) + ' ' + str(email_date[2]) | |
wiki_from = email_from.replace( '@', '_at_' ) | |
wiki_to = email_to | |
wiki_filename = email_subject.lower() | |
wiki_filename = wiki_filename.replace( 're: ', '' ) | |
wiki_filename = wiki_filename.replace( 'fwd: ', '' ) | |
wiki_filename = wiki_filename.replace( ' ', '_' ) | |
wiki_filename = wiki_filename.replace( '/', '_' ) | |
email_body = email_body.strip() | |
email_body = body_pattern.sub('', email_body) | |
email_body = star_pattern.sub('-', email_body) | |
message = '' | |
message += '===== ' + email_subject.upper() + '=====\n\n' | |
message += 'From: [[email:' + wiki_date + ':from:' + wiki_from + ']] (' + email_from + ')\n\n' | |
message += 'To: ' + email_to + '\n\n' | |
message += 'Date: [[email:' + wiki_date + ':combined|' + wiki_date + ']]\n\n' | |
message += '==Message body==\n\n' + email_body + "\n\n" | |
# Store it under the subject, from email and date | |
try: | |
os.stat('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/subject/') | |
except: | |
os.makedirs('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/subject/') | |
try: | |
os.stat('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/from/') | |
except: | |
os.makedirs('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/from/') | |
f = open('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/subject/' + wiki_filename + '.txt', 'a' ) | |
f.write(message) | |
f.close | |
f = open('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/from/' + wiki_from + '.txt', 'a' ) | |
f.write(message) | |
f.close | |
f = open('/mnt/disk/www/dokuwiki-2009-02-14/data/pages/email/' + wiki_date.replace( ' ', '_' ) + '/combined.txt', 'a' ) | |
f.write(message) | |
f.close | |
M.logout() | |
if __name__ == '__main__': | |
wiki = ImapWiki() | |
# wiki.run() | |
wiki.task() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment