def messagesToConversations(msgs, cnt, ua): if not Config.hasValue('import.conversation_interval'): MAX_TIME = 3600000L else: MAX_TIME = Config.getLong('import.conversation_interval') conversations = [] msgs.sort(key=lambda msg: msg.time) lastTime = msgs[0].time conv = Conversation(0, msgs[0].time, msgs[0].message[0:100], 0, cnt, ua) conv.endTime = lastTime conversations.append(conv) for msg in msgs: if msg.time.getTime() <= lastTime.getTime(): msg.time.setTime(lastTime.getTime() + 1) if math.fabs(lastTime.getTime() - msg.time.getTime()) > MAX_TIME: conv.endTime = lastTime conv = Conversation(0, msg.time, msg.message[0:100], 0, cnt, ua) conversations.append(conv) conv.addMessage(msg) lastTime = msg.time conv.endTime = lastTime return conversations
def getContacts(self, userAccounts): self.contactsLoadProgress = 0 try: self.M.recent() except: self.getUserAccounts() contactAccounts = {} convNums = self.data[0].split() for num in convNums: if self.isAborted(): self.M.close() self.M.logout() return None typ, data = self.M.fetch(num, '(RFC822)') if typ <> 'OK': e = Exception(typ+'') e.message = 'Message '+num+' not found.' raise e email_message = email.message_from_string(data[0][1]) # parse head fromField = decode_header(email_message['From']) if len(fromField) == 2: name = unicode(fromField[0][0], 'utf-8') uid = unicode(fromField[1][0], 'utf-8')[1:-1].lower() else: nameUid = fromField[0][0] if fromField[0][1] <> None and fromField[0][1] <> 'utf-8': nameUid = unicode(nameUid, 'iso-8859-2') m = re.search(r'(?P<name>.*)\s+\<(?P<uid>.*)\>', nameUid) name = m.group('name') uid = m.group('uid').lower() # create contact account if uid in contactAccounts: contactAccount = contactAccounts[uid] else: contactAccount = ContactAccount(0, name, uid, None, None, self.protocol) contactAccounts[contactAccount.uid] = contactAccount # parse body data = self.get_first_text_block(email_message).replace('=\r\n', '') pattern = re.compile('\<con\:conversation(.*)\<\/con\:conversation\>', re.DOTALL) m = pattern.search(data) if m == None: e = Exception() e.message = 'Conversation parsing error [conversation not found].' raise e try: root = ElementTree.XML(m.group(0)) except ExpatError, ex: e = Exception(m.group(0)) e.message = 'Conversation parsing error. ' + ex raise e # create conversation time = Date() time.setTime(long(root[0].find('{google:timestamp}time').get('ms'))) conversation = Conversation(0, time, root[0].find('{jabber:client}body').text, len(root), contactAccount, userAccounts[0]) time.setTime(long(root[-1].find('{google:timestamp}time').get('ms'))) conversation.endTime = time contactAccount.conversations.add(conversation) # parse messages for msg in root: time = Date() time.setTime(long(msg.find('{google:timestamp}time').get('ms'))) if msg.find('{jabber:client}body') == None: txt = "" else: txt = msg.find('{jabber:client}body').text message = Message(0, conversation, txt, time, msg.attrib['to'].find(uid) == -1) conversation.addMessage(message) self.messagesCount += len(root) # set progress self.contactsLoadProgress = int(num) * 100 / len(convNums)