@@ -0,0 +1,2368 @@
+#!/usr/bin/python3%0A# Maintain a database of email messages that were seen in an account folder%0A%0Aimport imaplib%0Aimport time%0Aimport re%0Aimport Database%0Aimport Config%0A%0Aclass PyArchive:%0A%0A  def __init__(self):%0A    self.db = Database.Database()%0A    self.id_begin_re = re.compile(%22Message-ID:%5Cs%3C%22, re.IGNORECASE)%0A    self.id_end_re = re.compile(%22%3E%22)%0A    self.logfile = open(Config.log,'a')%0A    self.log(%22PyArchive executing%22)%0A%0A  def update(self):%0A    self.log(%22Updating archives...%22)%0A    M = imaplib.IMAP4_SSL(host=Config.server,port=Config.port)%0A    M.login(Config.email,Config.password)%0A    insert_count = 0%0A    dup_count = 0%0A    got_emails = %5B%5D%0A    for folder in Config.folders:%0A      M.select(mailbox=folder,readonly=True)%0A      typ, data = M.search(None,%22ALL%22)%0A      if typ==%22NO%22:%0A        self.log(%22Server rejected our request to search %7B%7D.%22.format(folder))%0A      for num in data%5B0%5D.split():%0A        _, data = M.fetch(num, '(BODY%5BHEADER.FIELDS (MESSAGE-ID)%5D)')%0A        idstr = str(data%5B0%5D%5B1%5D, encoding=%22ASCII%22)%0A        start = self.id_begin_re.search(idstr)%0A        end = self.id_end_re.search(idstr)%0A        if not start or not end:%0A          self.log(%22Failed to locate the message-id field%22)%0A        else:%0A          start = start.end()%0A          end = end.start()%0A          idstr = idstr%5Bstart:end%5D%0A                    if not self.db.in_database(idstr):%0A            _, full_message = M.fetch(num, '(RFC822)')%0A            full_message = str(full_message%5B0%5D%5B1%5D,encoding='UTF-8',errors='replace')%0A            got_emails.append(full_message)%0A            insert_count += 1%0A            self.log(%22Got new message %7B%7D%22.format(idstr))%0A          else:%0A            dup_count += 1%0A    self.db.insert_emails(got_emails)%0A    M.close()%0A    M.logout()%0A    self.log(%22Got %7B%7D new messages, observed %7B%7D already in database.%22.format(insert_count,dup_count))%0A    self.log(%22%7B%7D emails in the database.%22.format(self.db.count_emails()))%0A    self.log(%22Update complete.%22)%0A%0A  def log(self,msg):%0A    header = %22PyArchive %22 + time.strftime(%22%25m-%25d-%25Y %25H:%25M:%25S%22) + %22: %22%0A    msg = header + msg%0A    print(msg)%0A    self.logfile.write(msg + %22%5Cn%22)%0A%0A  def close(self):%0A    self.log(%22Shutting down...%22)%0A    self.logfile.close()%0A    self.db.close()%0A%0Aif __name__==%22__main__%22:%0A  print(%22Initializing...%22)%0A  try:%0A    arch = PyArchive()%0A    arch.update()%0A    arch.close()%0A  except Exception as err:%0A    arch.log(err)%0A

Create PyArchive.py