Beispiel #1
0
 def assertTextsEqual(self, t1, t2):
     warns = core.compareTexts(t1, t2,
             throw_errors=True,
             required_attrs=['num', 'incoming', 'body'])
     self.assertEqual(long(t1.date/1000), long(t2.date/1000))
     if 'date' in warns: warns.remove('date')
     if warns: core.warning("text differ with %s" % (warns))
Beispiel #2
0
    def parse_cursor(self, cursor):
        i = 0
        texts = []
        contactLookup = {}
        query = cursor.execute(
            'SELECT is_madrid, madrid_handle, address, date, text, madrid_date_read, flags \
            FROM message; ')
        for row in query:
            if row[0]:
                txt = core.Text(num=row[1],
                                date=long((row[3] + 978307200) * 1000),
                                incoming=(row[5] == 0),
                                body=row[4])
            else:
                from_me = row[6] & 0x01
                txt = core.Text(num=row[2],
                                date=long(row[3] * 1000),
                                incoming=(from_me == 1),
                                body=row[4])
            if not txt.num:
                txt.num = "unknown"
                core.warning("extracted text without number. row: %s" %
                             str(row))

            lookup_num = str(txt.num)[-10:]
            if not lookup_num in contactLookup:
                contactLookup[lookup_num] = i
            txt.cid = contactLookup[lookup_num]
            texts.append(txt)
            i += 1
        return texts
Beispiel #3
0
 def assertTextsEqual(self, t1, t2):
     warns = core.compareTexts(t1, t2,
             throw_errors=True,
             required_attrs=['num', 'incoming', 'body'])
     self.assertEqual(long(t1.date/1000), long(t2.date/1000))
     if 'date' in warns: warns.remove('date')
     if warns: core.warning("text differ with %s" % (warns))
Beispiel #4
0
    def parse_cursor(self, cursor):
        i=0
        texts = []
        contactLookup = {}
        query = cursor.execute(
            'SELECT is_madrid, madrid_handle, address, date, text, madrid_date_read, flags \
            FROM message; ')
        for row in query:
            if row[0]:
                txt = core.Text( num=row[1], date=long((row[3] + 978307200)*1000), incoming=(row[5]==0), body=row[4])
            else:
                from_me = row[6] & 0x01
                txt = core.Text( num=row[2], date=long(row[3]*1000), incoming=(from_me==1), body=row[4])
            if not txt.num:
                txt.num = "unknown"
                core.warning("extracted text without number. row: %s" % str(row))

            lookup_num = str(txt.num)[-10:]
            if not lookup_num in contactLookup:
                contactLookup[lookup_num] = i
            txt.cid = contactLookup[lookup_num]
            texts.append(txt)
            i+=1
        return texts
Beispiel #5
0
    def write_cursor(self, texts, cursor):

        if (cursor.execute("SELECT Count() FROM message").fetchone()[0] > 0):
            raise sms_exceptions.NonEmptyStartDBError("Output DB has existing messages!")

        ## First populate the 'handle' table with each contact
        handles_lookup = {} # cleaned # -> handle ROWID
        chat_lookup = {} # chat_key -> chat ROWID
        chat_participants = {} # chat_key -> [cleaned1, cleaned2]
        for txt in texts:
            try:
                clean_number = core.cleanNumber(txt.num)
                chat_key = txt.chatroom if txt.chatroom else txt.num

                ## Create the handle table (effectively a contacts table)
                if (clean_number) and (not clean_number in handles_lookup):
                    cursor.execute( "INSERT INTO handle ('id', service, uncanonicalized_id ) \
                        VALUES (?,?,?)", [txt.num,"SMS",clean_number])
                    handles_lookup[clean_number] = cursor.lastrowid

                if not chat_key:
                    core.warning("no txt chat_key [%s] for %s" % (chat_key, txt))
                ## Create the chat table (effectively a threads table)
                if not chat_key in chat_lookup:
                    guid = ("SMS;+;%s" % txt.chatroom) if txt.chatroom else ("SMS;-;%s" % txt.num)
                    style = 43 if txt.chatroom else 45
                    cursor.execute( "INSERT INTO chat (guid, style, state, chat_identifier, service_name, room_name ) \
                        VALUES (?,?,?,?,?,?)", [guid, style, 3, chat_key, 'SMS', txt.chatroom])
                    chat_lookup[chat_key] = cursor.lastrowid

                ## Create the chat_handle_join table (represents participants in all threads)
                if not chat_key in chat_participants:
                    chat_participants[chat_key] = set()
                if not clean_number in chat_participants[chat_key]:
                    chat_participants[chat_key].add(clean_number)
                    chat_id = chat_lookup[chat_key]
                    try:
                        handle_id = handles_lookup[clean_number]
                        cursor.execute( "INSERT INTO chat_handle_join (chat_id, handle_id ) \
                            VALUES (?,?)", [chat_id, handle_id])
                    except: pass #don't add handle joins for unknown contacts.
            except:
                print core.term.red("something failed at: %s") % (txt)
                raise

        print "built handles table with %i, chat with %i, chat_handle_join with %i entries" \
            % (len(handles_lookup), len(chat_lookup), len(chat_participants))


        for txt in texts:
            chat_key = txt.chatroom if txt.chatroom else txt.num
            handle_i = handles_lookup[core.cleanNumber(txt.num)] if core.cleanNumber(txt.num) in handles_lookup else 0
            idate = long( (float(txt.date)/1000) - 978307200)
            from_me = 0 if txt.incoming else 1
            guid = str(uuid.uuid1())

            cursor.execute( "INSERT INTO message \
                ('text', guid, handle_id, version, type, service, 'date', is_finished, is_from_me, is_sent, is_read ) \
                VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                [txt.body, guid, handle_i, 1, txt.chatroom != None, 'SMS', idate, 1, from_me, from_me, (1 - from_me)])
            message_id = cursor.lastrowid

            chat_id = chat_lookup[chat_key]
            cursor.execute( "INSERT INTO chat_message_join (chat_id, message_id) \
                VALUES (?,?)", [chat_id, message_id])

        print "built messages table with %i entries" % len(texts)
Beispiel #6
0
    def write_cursor(self, texts, cursor):

        if (cursor.execute("SELECT Count() FROM message").fetchone()[0] > 0):
            raise sms_exceptions.NonEmptyStartDBError("Output DB has existing messages!")

        ## First populate the 'handle' table with each contact
        handles_lookup = {} # cleaned # -> handle ROWID
        chat_lookup = {} # chat_key -> chat ROWID
        chat_participants = {} # chat_key -> [cleaned1, cleaned2]
        for txt in texts:
            try:
                clean_number = core.cleanNumber(txt.num)
                chat_key = txt.chatroom if txt.chatroom else txt.num

                ## Create the handle table (effectively a contacts table)
                if (clean_number) and (not clean_number in handles_lookup):
                    cursor.execute( "INSERT INTO handle ('id', service, uncanonicalized_id ) \
                        VALUES (?,?,?)", [txt.num,"SMS",clean_number])
                    handles_lookup[clean_number] = cursor.lastrowid

                if not chat_key:
                    core.warning("no txt chat_key [%s] for %s" % (chat_key, txt))
                ## Create the chat table (effectively a threads table)
                if not chat_key in chat_lookup:
                    guid = ("SMS;+;%s" % txt.chatroom) if txt.chatroom else ("SMS;-;%s" % txt.num)
                    style = 43 if txt.chatroom else 45
                    cursor.execute( "INSERT INTO chat (guid, style, state, chat_identifier, service_name, room_name ) \
                        VALUES (?,?,?,?,?,?)", [guid, style, 3, chat_key, 'SMS', txt.chatroom])
                    chat_lookup[chat_key] = cursor.lastrowid

                ## Create the chat_handle_join table (represents participants in all threads)
                if not chat_key in chat_participants:
                    chat_participants[chat_key] = set()
                if not clean_number in chat_participants[chat_key]:
                    chat_participants[chat_key].add(clean_number)
                    chat_id = chat_lookup[chat_key]
                    try:
                        handle_id = handles_lookup[clean_number]
                        cursor.execute( "INSERT INTO chat_handle_join (chat_id, handle_id ) \
                            VALUES (?,?)", [chat_id, handle_id])
                    except: pass #don't add handle joins for unknown contacts.
            except:
                print(core.term.red("something failed at: %s") % (txt))
                raise

        print("built handles table with %i, chat with %i, chat_handle_join with %i entries" \
            % (len(handles_lookup), len(chat_lookup), len(chat_participants)))


        for txt in texts:
            chat_key = txt.chatroom if txt.chatroom else txt.num
            handle_i = handles_lookup[core.cleanNumber(txt.num)] if core.cleanNumber(txt.num) in handles_lookup else 0
            idate = long( (float(txt.date)/1000) - 978307200)
            from_me = 0 if txt.incoming else 1
            guid = str(uuid.uuid1())

            cursor.execute( "INSERT INTO message \
                ('text', guid, handle_id, version, type, service, 'date', is_finished, is_from_me, is_sent, is_read ) \
                VALUES (?,?,?,?,?,?,?,?,?,?,?)",
                [txt.body, guid, handle_i, 1, txt.chatroom != None, 'SMS', idate, 1, from_me, from_me, (1 - from_me)])
            message_id = cursor.lastrowid

            chat_id = chat_lookup[chat_key]
            cursor.execute( "INSERT INTO chat_message_join (chat_id, message_id) \
                VALUES (?,?)", [chat_id, message_id])

        print("built messages table with %i entries" % len(texts))