Example #1
0
    def _parse_message_body(self, msg_obj, serialized_msg):
        msg = TaoUtils.json_loads(serialized_msg)
        if not msg:
            return

        try:
            sender_id = msg["from"]
            receiver_id = msg["to"]
            msg_ext_info = msg['ext']
            msg_type = self._judge_msg_type(msg_ext_info)

            msg_obj.sender_id = sender_id
            msg_obj.is_sender = 1 if msg_obj.account_id == msg_obj.sender_id else 0
            msg_obj.type = msg_type

            if msg_type == model_im.MESSAGE_CONTENT_TYPE_TEXT:
                msg_obj.content = msg["bodies"][0]['msg']
            elif msg_type == model_im.MESSAGE_CONTENT_TYPE_SYSTEM:
                msg_obj.content = msg["bodies"][0]['msg'] if msg["bodies"][0][
                    'msg'] != "[卡片]" else self._get_card_desc(msg_ext_info)
            elif msg_type == model_im.MESSAGE_CONTENT_TYPE_IMAGE:
                msg_obj.media_path = self._get_image_path(msg_ext_info)
            elif msg_type == model_im.MESSAGE_CONTENT_TYPE_VIDEO:
                msg_obj.media_path = self._get_video_path(msg_ext_info)
            elif msg_type == model_im.MESSAGE_CONTENT_TYPE_VOICE:
                msg_obj.media_path = self._get_audio_path(msg["bodies"][0])
            elif msg_type == model_im.MESSAGE_CONTENT_TYPE_VOIP:
                msg_obj.content = msg["bodies"][0]['msg']

        except Exception as e:
            print(serialized_msg)
            self.logger.error()
Example #2
0
 def _get_image_path(self, msg_ext_info):
     for d in msg_ext_info:
         for k, v in d.items():
             if k == 'images':
                 images_array = TaoUtils.json_loads(v)
                 return [i['imageUrl'] for i in images_array]
     return None
Example #3
0
 def _get_card_desc(self, msg_ext_info):
     for d in msg_ext_info:
         for k, v in d.items():
             if k == 'cardDescs':
                 desc = "".join([w for w in TaoUtils.json_loads(v)])
                 return desc
     return None
Example #4
0
    def _generate_message_table(self):
        chat_db = self._search_file_simple("chat-db$")
        if not chat_db:
            return
        Message.connect(chat_db)
        Friend.connect(chat_db)
        name = {
            member.user_id: member.nickname
            for member in Friend.objects.all
        }

        account_id = self.master_account.account_id
        for msg in Message.objects.all:
            try:
                message = model_im.Message()
                message.deleted = msg.deleted
                message.source = msg.source_path
                message.talker_id = msg.talker_id
                message.account_id = message.talker_id.split('##')[-1]
                message.content = msg.content
                message.msg_id = msg.msg_id
                message.sender_id = msg.sender_id
                message.send_time = TaoUtils.convert_timestamp(msg.ts)
                message.is_sender = 1 if int(account_id) == int(
                    msg.sender_id) else 0
                message.sender_name = name.get(int(message.sender_id), None)
                message.talker_type = model_im.CHAT_TYPE_SHOP
                message.type = model_im.MESSAGE_CONTENT_TYPE_IMAGE if msg.msg_type == 1 \
                    else model_im.MESSAGE_CONTENT_TYPE_TEXT
                message.media_path = msg.media_path
                self.model_im_col.db_insert_table_message(message)
            except Exception as e:
                self.logger.error()
        self.model_im_col.db_commit()
Example #5
0
    def _recover_message_record(self, account_id):
        recover_col = self.message_recover_col
        if not recover_col.is_valid():
            return
        ts = recover_col.get_table(
            "message", {
                "msgid": [FieldType.Text, FieldConstraints.NotNull],
                "msgtime": [FieldType.Int, FieldConstraints.NotNull],
                "conversation": [FieldType.Text, FieldConstraints.NotNull],
                "isread": [FieldType.Int, FieldConstraints.NotNull],
                "status": [FieldType.Int, FieldConstraints.NotNull],
                "msgbody": [FieldType.Text, FieldConstraints.NotNull],
                "msgtype": [FieldType.Int, FieldConstraints.NotNull],
                "bodytype": [FieldType.Int, FieldConstraints.NotNull],
                "servertime": [FieldType.Int, FieldConstraints.NotNull],
            })
        for rec in recover_col.read_deleted_records(ts):
            try:
                message = model_im.Message()
                message.account_id = account_id
                message.source = recover_col.db_path
                message.deleted = 1
                message.msg_id = rec['msgid'].Value
                message.send_time = TaoUtils.convert_timestamp(
                    rec['msgtime'].Value)
                message.status = model_im.MESSAGE_STATUS_READ if rec['isread'].Value == 1 \
                    else model_im.MESSAGE_STATUS_UNREAD
                message.talker_type = model_im.CHAT_TYPE_FRIEND
                message.talker_id = rec['conversation'].Value
                self._parse_message_body(message, rec['msgbody'].Value)
                message.sender_name = self._query_sender_name(
                    message.account_id, message.sender_id)
                message.talker_name = message.sender_name

                if message.type == model_im.MESSAGE_CONTENT_TYPE_IMAGE:
                    image_urls = message.media_path[:]
                    for image_url in image_urls:
                        message.media_path = image_url
                        self.model_im_col.db_insert_table_message(message)
                else:
                    self.model_im_col.db_insert_table_message(message)

            except Exception as e:
                self.logger.error()
        self.model_im_col.db_commit()
Example #6
0
    def _generate_message_table(self):
        """生成消息的表"""
        db_col = self.message_col
        table = db_col.get_table(
            "message", {
                "msgid": [FieldType.Text, FieldConstraints.NotNull],
                "msgtime": [FieldType.Int, FieldConstraints.NotNull],
                "conversation": [FieldType.Text, FieldConstraints.NotNull],
                "isread": [FieldType.Int, FieldConstraints.NotNull],
                "status": [FieldType.Int, FieldConstraints.NotNull],
                "msgbody": [FieldType.Text, FieldConstraints.NotNull],
                "msgtype": [FieldType.Int, FieldConstraints.NotNull],
                "bodytype": [FieldType.Int, FieldConstraints.NotNull],
                "servertime": [FieldType.Int, FieldConstraints.NotNull],
            })
        for rec in db_col.read_records(table):
            try:
                message = model_im.Message()
                message.account_id = os.path.basename(
                    db_col.db_path).split(".")[0]
                message.source = db_col.db_path
                message.deleted = 1 if rec.IsDeleted else 0
                message.msg_id = rec['msgid'].Value
                message.send_time = TaoUtils.convert_timestamp(
                    rec['servertime'].Value)
                message.status = model_im.MESSAGE_STATUS_READ if rec['isread'].Value == 1 \
                    else model_im.MESSAGE_STATUS_UNREAD
                message.talker_type = model_im.CHAT_TYPE_SYSTEM if rec['conversation'].Value == "admin" \
                    else model_im.CHAT_TYPE_FRIEND
                message.talker_id = rec['conversation'].Value
                message.talker_name = message.sender_name
                self._parse_message_body(message, rec['msgbody'].Value)
                if rec['conversation'].Value != "admin":
                    self._friend_array.add(message.talker_id)
                if message.type == model_im.MESSAGE_CONTENT_TYPE_IMAGE:
                    image_urls = message.media_path[:]
                    for image_url in image_urls:
                        message.media_path = image_url
                        self.model_im_col.db_insert_table_message(message)
                else:
                    self.model_im_col.db_insert_table_message(message)

            except Exception as e:
                self.logger.error()
Example #7
0
    def _add_message_record(self, account_id):
        with self.message_col as db_col:
            sql = """SELECT msgid, 
                            msgtime, 
                            msgdirection, 
                            conversation, 
                            isread, 
                            isacked, 
                            isdelivered, 
                            islistened, 
                            status, 
                            msgbody, 
                            msgtype, 
                            bodytype, 
                            servertime 
                        FROM message;"""
            db_col.execute_sql(sql)
            while db_col.has_rest():
                try:
                    message = model_im.Message()
                    message.account_id = account_id
                    message.source = db_col.db_path
                    message.msg_id = db_col.get_string(0)
                    message.send_time = TaoUtils.convert_timestamp(
                        db_col.get_int64(1))
                    message.status = model_im.MESSAGE_STATUS_READ if db_col.get_int64(4) == 1 \
                        else model_im.MESSAGE_STATUS_UNREAD
                    message.talker_type = model_im.CHAT_TYPE_FRIEND
                    message.talker_id = db_col.get_string(3)
                    self._parse_message_body(message, db_col.get_string(9))
                    message.sender_name = message.talker_name = self._query_sender_name(
                        message.account_id, message.sender_id)
                    if message.type == model_im.MESSAGE_CONTENT_TYPE_IMAGE:
                        image_urls = message.media_path[:]
                        for image_url in image_urls:
                            message.media_path = image_url
                            self.model_im_col.db_insert_table_message(message)
                    else:
                        self.model_im_col.db_insert_table_message(message)
                except Exception as e:
                    self.logger.error()

            self.model_im_col.db_commit()
Example #8
0
    def _generate_account_table(self):
        """
        创建account table
        """
        msg_db_col = self.message_col
        account_id = os.path.basename(msg_db_col.db_path).split(".")[0]

        db_col = self.soul_app_col
        table = db_col.get_table(
            "user", {
                "_id": [FieldType.Int, FieldConstraints.NotNull],
                "user": [FieldType.Text, FieldConstraints.NotNull],
            })
        for rec in db_col.read_records(table):
            try:
                if str(rec['_id'].Value) == account_id:
                    account_info = TaoUtils.json_loads(rec['user'].Value)
                    if not account_info:
                        continue

                    account = model_im.Account()
                    account.account_id = rec['_id'].Value
                    account.source = db_col.db_path
                    account.signature = account.username = account.nickname = account_info.get(
                        "signature", None)
                    account.gender = model_im.GENDER_FEMALE if account_info.get("gender", None) == "FEMALE" \
                        else model_im.GENDER_MALE
                    account.birthday = TimeHelper.convert_timestamp(
                        account_info.get("birthday", None))
                    account.email = account_info.get('bindMail', None)
                    account.country = "China" if account_info.get(
                        'area', None) else None
                    account.deleted = 1 if rec.IsDeleted else 0
                else:
                    account = model_im.Account()
                    account.account_id = account.username = account.nickname = account_id
                    account.source = db_col.db_path
                self.model_im_col.db_insert_table_account(account)
            except Exception as e:
                self.logger.error()
        self.model_im_col.db_commit()
Example #9
0
    def _generate_message_table(self):
        chat_db = self._search_file("Chat.sqlite$")
        if not chat_db:
            return

        for account_id in self.history_account_list:
            msg_model = create_message_table(account_id)
            member_model = create_chat_member_table(account_id)

            member_model.connect(chat_db)
            name = {
                member.user_id: member.nickname
                for member in member_model.objects.all
            }

            msg_model.connect(chat_db)

            for msg in msg_model.objects.all:
                try:
                    message = model_im.Message()
                    message.account_id = account_id
                    message.deleted = msg.deleted
                    message.talker_id = message.sender_id = msg.sender_id
                    message.content = msg.content
                    message.msg_id = msg.msg_id
                    message.send_time = TaoUtils.convert_timestamp(msg.ts)
                    message.is_sender = 1 if account_id == msg.sender_id else 0
                    message.sender_name = name.get(int(message.sender_id),
                                                   None)
                    message.talker_type = model_im.CHAT_TYPE_FRIEND
                    message.type = model_im.MESSAGE_CONTENT_TYPE_IMAGE if msg.msg_type == 1 \
                        else model_im.MESSAGE_CONTENT_TYPE_TEXT
                    message.media_path = self._search_file('{}$'.format(
                        message.content))
                    self.model_im_col.db_insert_table_message(message)
                except Exception as e:
                    self.logger.error()
            self.model_im_col.db_commit()
Example #10
0
    def convert_message_content(self, content, msg_obj):
        content = content.strip()
        if not (content.startswith("<") and content.endswith(">")):
            return content
        try:
            hp = PaHtmlParser()
            hp.feed(content)
            hp.close()

            tag_name = hp.first_dom.tag_name
            if tag_name == "deletemember":
                executor = hp.root['deletemember']['initiator'].data
                target = hp.root['deletemember']['target'].data
                return self.assemble_message_delete_member(executor, target)
            elif tag_name == "addmember":
                executor = hp.root['addmember']['initiator'].data
                target = hp.root['addmember']['target'].data
                return self.assemble_message_add_member(executor, target)
            elif tag_name == "ss":
                nodes = hp.root.get_all("ss")
                return self.assemble_message_emoji(nodes)
            elif tag_name == "uriobject":
                if hp.root['uriobject'].get("swift", None):
                    encoded_info = hp.root['uriobject']['swift'].property[
                        'b64']
                    decoded_info = TaoUtils.decode_base64(encoded_info)
                    attachment = json.loads(decoded_info)["attachments"][0]
                    url = attachment["content"]['images'][0]['url']
                    return url
                return hp.root['uriobject']['originalname'].property['v']
            elif tag_name == 'partlist':
                return self.assemble_message_call(hp.root, msg_obj.account_id)
            elif tag_name == 'topicupdate':
                return self.assemble_message_rename_group(hp.root)
            elif tag_name == 'location':
                address = hp.root['location'].property['address']
                latitude = float(
                    hp.root['location'].property['latitude']) / 1000000
                longitude = float(
                    hp.root['location'].property['longitude']) / 1000000
                ts = TimeHelper.convert_timestamp(
                    hp.root['location'].property.get("timestamp", None))
                location = model_im.Location()
                location.account_id = self.using_account.account_id
                location.address = address
                location.latitude = latitude
                location.longitude = longitude
                location.timestamp = ts
                self.model_im_col.db_insert_table_location(location)
                msg_obj.location_id = location.location_id
                msg_obj.type = model_im.MESSAGE_CONTENT_TYPE_LOCATION
                return None
            elif tag_name == 'historydisclosedupdate':
                return self.assemble_message_history_closure(hp.root)
            elif tag_name == 'joiningenabledupdate':
                return self.assemble_message_close_add_memeber(hp.root)
            elif tag_name == "sms":
                return hp.root['sms']['defaults']['content'].data
            elif tag_name == "pictureupdate":
                return self.assemble_message_change_pic_bak(hp.root)
            else:
                return content
        except Exception as e:
            return content