def __ParseFetchReply(self, fetch_reply): s = stringscanner.StringScanner(fetch_reply) message_infos = [] while s.Peek(): current_message_info = messageinfo.MessageInfo() message_infos.append(current_message_info) # The sequence ID is first, with all the data in parentheses sequence_id = s.ReadUntil(" ") s.ConsumeAll(" ") s.ConsumeChar("(") while s.Peek() != ")": s.ConsumeAll(" ") name = s.ReadUntil(" ") s.ConsumeAll(" ") value = s.ConsumeValue() current_message_info.PopulateField(name, value) s.ConsumeChar(")") return message_infos
def GetMessageInfos(self): """ Return a list of MessageInfo objects, one per message If the current mailbox has been selected, only mails from that mailbox are selected, otherwise all mails are returned. """ if self.__current_mailbox and self.mailboxes.has_key(self.__current_mailbox): boxes = [self.__current_mailbox] else: boxes = self.GetMailboxes() info = [] for mbox in boxes: #logging.debug("GetMessages from Mailbox: %s", mbox) for path in self.mailboxes[mbox]: #logging.debug("ReadMessage: %s", path) mi = messageinfo.MessageInfo() try: fd = open(path, "r") fd.readline() headers = StringIO() bytes_read = 0 while True: chunk = fd.read(1024) if chunk == '': break bytes_read += len(chunk) headers.write(chunk) if '\n\n' in chunk or bytes_read >= 100000: break fd.close() msg = email.message_from_string(headers.getvalue()) # FIXME: there has to be a better way to do this # e.g. Just read the headers and stat the file mi.PopulateField('RFC822.SIZE', os.stat(path).st_size) mi.PopulateField('RFC822.HEADER', self.__BuildHeader(msg)) if "Date" not in msg: #logging.info("ERROR: Unable to parse file: %s", path) continue date = email.utils.parsedate_tz(msg["Date"]) t = time.gmtime(email.utils.mktime_tz(date)) maildate = time.strftime("%d-%b-%Y %H:%M:%S", t) + ' %+05d' % (date[9] / 3600) mi.PopulateField('INTERNALDATE', maildate) info.append(mi) except: #logging.info("ERROR: Unable to parse file: %s", path) pass return info
def GetMessageInfos(self): """ Return a list of MessageInfo objects, one per message If the current mailbox has been selected, only mails from that mailbox are selected, otherwise all mails are returned. """ if self.__current_mailbox and self.mailboxes.has_key(self.__current_mailbox): boxes = [self.__current_mailbox] else: boxes = self.GetMailboxes() info = [] for mbox in boxes: #logging.info("GetMessages from Mailbox: %s",mbox) for path in self.mailboxes[mbox]: #logging.info("ReadMessage: %s",path) mi = messageinfo.MessageInfo() try: fd = open(path,"r") fd.readline() msg = email.message_from_file(fd) fd.close() # FIXME: there has to be a better way to do this # e.g. Just read the headers and stat the file mi.PopulateField('RFC822.SIZE', os.stat(path).st_size) mi.PopulateField('RFC822.HEADER', self.__BuildHeader(msg) ) if not msg.has_key("Date"): logging.info("ERROR: Unable to parse file: %s",path) continue date = email.utils.parsedate_tz(msg["Date"]) t = time.gmtime(email.utils.mktime_tz(date)) maildate = time.strftime("%d-%b-%Y %H:%M:%S", t) + ' %+05d' % (date[9]/3600) mi.PopulateField('INTERNALDATE', maildate) info.append(mi) except: logging.info("ERROR: Unable to parse file: %s",path) return info