def savemessage(self, uid, content, flags, rtime): self.ui.savemessage('gridfs', uid, flags, self) if uid < 0: # as per maildir implementation, we cannot create new uids return uid if uid in self.messagelist: # already have message, just update flags self.savemessageflags(uid, flags) return uid #: @type: email.Message msg = Parser().parsestr(content, headersonly=True) fromAddress = self.addressToList(msg['From'])[0] if (msg.has_key('To') and msg['To'] is not None): toAddress = self.addressToList(msg['To']) else: toAddress = None if msg.has_key('CC'): ccAddress = self.addressToList(msg['CC']) else: ccAddress = None sent = None sent_date = msg['Date'] if msg.has_key('Date') else None messageid = msg['Message-Id'] if msg.has_key('Message-Id') else None if sent_date is not None: try: sent = dateutil.parser.parse(sent_date, fuzzy=True) except Exception: self.ui.warn("Processing message %s [acc: %s]:\n Date not valid, value: %s" %\ (uid, self.accountname, sent_date)) obj = { 'uid': uid, 'type': 'M', 'flags': [flag for flag in flags], GridFSFolder.MODIFY_DATE_KEY: datetime.utcnow(), 'filename': self.format_file_name(uid), 'path': self.name, 'accountname': self.accountname, 'remotePath' : self.name, # used to determine if message has been moved folders # mail specific metadata 'mailHeaders' : { 'subject': msg['Subject'], 'from': fromAddress, 'date': sent, 'to': toAddress, 'cc': ccAddress, 'messageid':messageid } } id = self._gfs.put(content, **obj) self.messagelist[uid] = { 'flags': flags, '_id' : id } self.ui.debug('gridfs', 'savemessage: returning uid %d' % uid) return uid
""" Sorts the mail! """ if __name__ == "__main__": # Gets a list of all e-mail to be processed. email_files = os.listdir(ORIGIN_MAILDIR) counter = 0 for file_name in email_files: # Opens and parses the file. full_path_to_file = os.path.join(ORIGIN_MAILDIR, file_name) with open(full_path_to_file) as fp: message = Parser().parse(fp, headersonly=True) # Checks for the header containing GMail labels and parses the header. if message.has_key("X-GMAIL-LABELS"): labels = message["X-GMAIL-LABELS"] label_list = shlex.split(labels) ## print label_list # Removes labels that are to be ignored. for label in IGNORED_LABELS: if label in label_list: label_list.remove(label) # If there's only one label, sorts the e-mail into the folder corresponding to that label. if len(label_list) == 1: os.rename(full_path_to_file, os.path.join(maildir_path(label_list[0]), file_name)) ## print 'Moving file %s to directory %s' % (full_path_to_file, os.path.join(maildir_path(label_list[0]), file_name)) # If there are multiple labels, checks the precedence order in settings and sorts the e-mail into the appropriate folder. else: for label in LABEL_ORDER: if label in label_list:
import os, glob from email.parser import Parser from gmailsort_settings import CHAT_DEST_MAILDIR, CHAT_ORIGIN_MAILDIR if __name__ == '__main__': for directory in ['cur', 'tmp', 'new']: path_to_directory = os.path.join(CHAT_DEST_MAILDIR, directory) if not os.path.exists(path_to_directory): os.makedirs(path_to_directory) print 'Making directory %s' % path_to_directory for path in glob.glob(CHAT_ORIGIN_MAILDIR): if path != os.path.join(CHAT_DEST_MAILDIR, 'cur'): email_files = os.listdir(path) for file_name in email_files: full_path_to_file = os.path.join(path, file_name) with open(full_path_to_file) as fp: message = Parser().parse(fp, headersonly=True) if message.has_key('Message-ID') and message['Message-ID'].endswith('*****@*****.**>'): os.rename(full_path_to_file, os.path.join(CHAT_DEST_MAILDIR, 'cur/%s' % file_name)) print 'Moving e-mail "%s" to Chats!' % message['Subject'] else: print 'Did not move e-mail "%s"' % message['Subject']