Ejemplo n.º 1
0
 def assets_update(self, asset_info, jms_info):
     jms_info_asset = [x for x in jms_info
                       if x['id'] == asset_info['id']][0]
     if (jms_info_asset['hostname'] == asset_info['hostname']
             and jms_info_asset['ip'] == asset_info['ip']
             and jms_info_asset['comment'] == asset_info['comment']):
         Log.debug('The assets %s is no change' % asset_info['hostname'])
     else:
         rst = requests.put(url=self.url +
                            '/api/v1/assets/assets/%s/' % asset_info['id'],
                            headers=self.headers,
                            data=json.dumps(asset_info))
         Log.info('Update the assent %s: %s' %
                  (rst.status_code, asset_info['hostname']))
Ejemplo n.º 2
0
 def admin_user_create(self, admin_user):
     __data = {
         'id': str(uuid.uuid3(uuid.NAMESPACE_DNS, admin_user)),
         'name': admin_user
     }
     if admin_user in [x['name'] for x in self.admin_user_list()]:
         Log.info('The Admin User %s is exist' % admin_user)
     else:
         requests.post(url=self.url + '/api/v1/assets/admin-users/',
                       headers=self.headers,
                       data=json.dumps(__data))
         Log.info('Create admin user: %s' % admin_user)
     return [x for x in self.admin_user_list()
             if x['name'] == admin_user][0]['id']
Ejemplo n.º 3
0
 def node_child_create(self, node_uuid, node_child):
     rst_child_nodes = self.node_child_list(node_uuid)
     if isinstance(node_child, list):
         for each_child_node in node_child:
             if each_child_node not in [
                     x['value'] for x in rst_child_nodes
             ]:
                 child_data = {'value': each_child_node}
                 requests.post(
                     url=self.url +
                     '/api/v1/assets/nodes/%s/children/' % str(node_uuid),
                     headers=self.headers,
                     data=json.dumps(child_data))
                 Log.info('Create child node %s' % each_child_node)
     return [x['id'] for x in rst_child_nodes if x['value'] in node_child]
Ejemplo n.º 4
0
 def node_create(self, node_name):
     __data = {
         'id': str(uuid.uuid3(uuid.NAMESPACE_DNS, node_name)),
         'value': node_name
     }
     if node_name in [
             i['value'] for i in json.loads(self.nodes_list())
             if i['value'] == node_name
     ]:
         Log.info('The Group %s is exist' % node_name)
     else:
         requests.post(url=self.url + '/api/v1/assets/nodes/',
                       headers=self.headers,
                       data=json.dumps(__data)).text()
         Log.info('Create node %s' % node_name)
     return str(uuid.uuid3(uuid.NAMESPACE_DNS, node_name))
Ejemplo n.º 5
0
    def domain_create(self, domain_name):
        __data = {
            'id': str(uuid.uuid3(uuid.NAMESPACE_DNS, domain_name)),
            'name': domain_name,
            'assets': []
        }
        if domain_name in [x['name'] for x in self.domain_list()]:
            Log.info('The domain %s is exist' % domain_name)
        else:
            requests.post(url=self.url + '/api/v1/assets/domains/',
                          headers=self.headers,
                          data=json.dumps(__data))
            Log.info('Create domain %s' % domain_name)

        return [x for x in self.domain_list()
                if x['name'] == domain_name][0]['id']
Ejemplo n.º 6
0
    def assets_create(self, data):
        f_uuid = self.node_create(data[1])
        d_uuid = self.domain_create(data[2])
        a_uuid = self.admin_user_create(data[3])
        jms_assets = self.assets_list()

        for each_asset in data[0]:
            each_asset['nodes'] = self.node_child_create(
                f_uuid, each_asset['group'])
            each_asset['domain'] = d_uuid
            each_asset['admin_user'] = a_uuid
            if each_asset['id'] in [x['id'] for x in jms_assets]:
                self.assets_update(each_asset, jms_assets)
            else:
                rst = requests.post(url=self.url + '/api/v1/assets/assets/',
                                    headers=self.headers,
                                    data=json.dumps(each_asset))
                if str(rst.status_code).startswith('2'):
                    Log.info('create asset %s %s' %
                             (each_asset['hostname'], rst.status_code))
                else:
                    Log.error('create asset faild %s: %s' %
                              (rst.status_code, rst.text))
Ejemplo n.º 7
0
    def handle_msg(self, r):
        """
        @brief      Recover from snapshot data.
        @param      r  Dict: message json
        """
        Log.debug('handle message')
        if self.msg_handler:
            self.msg_handler.handle_wxsync(r)

        n = len(r['AddMsgList'])
        if n == 0:
            return

        if self.log_mode:
            echo(Constant.LOG_MSG_NEW_MSG % n)

        for msg in r['AddMsgList']:

            msgType = msg['MsgType']
            msgId = msg['MsgId']
            content = msg['Content'].replace('&lt;', '<').replace('&gt;', '>')
            raw_msg = None

            if msgType == self.wx_conf['MSGTYPE_TEXT']:
                # 地理位置消息
                if content.find('pictype=location') != -1:
                    location = content.split('<br/>')[1][:-1]
                    raw_msg = {
                        'raw_msg': msg,
                        'location': location,
                        'log': Constant.LOG_MSG_LOCATION % location
                    }
                # 普通文本消息
                else:
                    text = content.split(':<br/>')[-1]
                    raw_msg = {
                        'raw_msg': msg,
                        'text': text,
                        'log': text.replace('<br/>', '\n')
                    }
            elif msgType == self.wx_conf['MSGTYPE_IMAGE']:
                data = self.webwxgetmsgimg(msgId)
                fn = 'img_' + msgId + '.jpg'
                dir = self.save_data_folders['webwxgetmsgimg']
                path = save_file(fn, data, dir)
                raw_msg = {
                    'raw_msg': msg,
                    'image': path,
                    'log': Constant.LOG_MSG_PICTURE % path
                }
            elif msgType == self.wx_conf['MSGTYPE_VOICE']:
                data = self.webwxgetvoice(msgId)
                fn = 'voice_' + msgId + '.mp3'
                dir = self.save_data_folders['webwxgetvoice']
                path = save_file(fn, data, dir)
                raw_msg = {
                    'raw_msg': msg,
                    'voice': path,
                    'log': Constant.LOG_MSG_VOICE % path
                }
            elif msgType == self.wx_conf['MSGTYPE_SHARECARD']:
                info = msg['RecommendInfo']
                card = Constant.LOG_MSG_NAME_CARD % (
                    info['NickName'], info['Alias'], info['Province'],
                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']])
                namecard = '%s %s %s %s %s' % (
                    info['NickName'], info['Alias'], info['Province'],
                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']])
                raw_msg = {'raw_msg': msg, 'namecard': namecard, 'log': card}
            elif msgType == self.wx_conf['MSGTYPE_EMOTICON']:
                url = search_content('cdnurl', content)
                raw_msg = {
                    'raw_msg': msg,
                    'emoticon': url,
                    'log': Constant.LOG_MSG_EMOTION % url
                }
            elif msgType == self.wx_conf['MSGTYPE_APP']:
                card = ''
                # 链接, 音乐, 微博
                if msg['AppMsgType'] in [
                        self.wx_conf['APPMSGTYPE_AUDIO'],
                        self.wx_conf['APPMSGTYPE_URL'],
                        self.wx_conf['APPMSGTYPE_OPEN']
                ]:
                    card = Constant.LOG_MSG_APP_LINK % (
                        Constant.LOG_MSG_APP_LINK_TYPE[msg['AppMsgType']],
                        msg['FileName'], search_content('des', content, 'xml'),
                        msg['Url'], search_content('appname', content, 'xml'))
                    raw_msg = {'raw_msg': msg, 'link': msg['Url'], 'log': card}
                # 图片
                elif msg['AppMsgType'] == self.wx_conf['APPMSGTYPE_IMG']:
                    data = self.webwxgetmsgimg(msgId)
                    fn = 'img_' + msgId + '.jpg'
                    dir = self.save_data_folders['webwxgetmsgimg']
                    path = save_file(fn, data, dir)
                    card = Constant.LOG_MSG_APP_IMG % (
                        path, search_content('appname', content, 'xml'))
                    raw_msg = {'raw_msg': msg, 'image': path, 'log': card}
                else:
                    raw_msg = {
                        'raw_msg': msg,
                        'log':
                        Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)
                    }
            elif msgType == self.wx_conf['MSGTYPE_STATUSNOTIFY']:
                Log.info(Constant.LOG_MSG_NOTIFY_PHONE)
            elif msgType == self.wx_conf['MSGTYPE_MICROVIDEO']:
                data = self.webwxgetvideo(msgId)
                fn = 'video_' + msgId + '.mp4'
                dir = self.save_data_folders['webwxgetvideo']
                path = save_file(fn, data, dir)
                raw_msg = {
                    'raw_msg': msg,
                    'video': path,
                    'log': Constant.LOG_MSG_VIDEO % path
                }
            elif msgType == self.wx_conf['MSGTYPE_RECALLED']:
                recall_id = search_content('msgid', content, 'xml')
                text = Constant.LOG_MSG_RECALL
                raw_msg = {
                    'raw_msg': msg,
                    'text': text,
                    'recall_msg_id': recall_id,
                    'log': text
                }
            elif msgType == self.wx_conf['MSGTYPE_SYS']:
                raw_msg = {
                    'raw_msg': msg,
                    'sys_notif': content,
                    'log': content
                }
            elif msgType == self.wx_conf['MSGTYPE_VERIFYMSG']:
                name = search_content('fromnickname', content)
                raw_msg = {
                    'raw_msg': msg,
                    'log': Constant.LOG_MSG_ADD_FRIEND % name
                }
            else:
                raw_msg = {
                    'raw_msg': msg,
                    'log': Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)
                }

            isGroupMsg = '@@' in msg['FromUserName'] + msg['ToUserName']
            if self.msg_handler and raw_msg:
                if isGroupMsg:
                    # handle group messages
                    g_msg = self.make_group_msg(raw_msg)
                    self.msg_handler.handle_group_msg(g_msg)
                else:
                    # handle personal messages
                    self.msg_handler.handle_user_msg(raw_msg)

            if self.log_mode:
                self.show_msg(raw_msg)
Ejemplo n.º 8
0
def echo(str):
    Log.info(str[:-1])
    sys.stdout.write(str)
    sys.stdout.flush()
Ejemplo n.º 9
0
def echo(str):
    Log.info(str[:-1])
    sys.stdout.write(str)
    sys.stdout.flush()
Ejemplo n.º 10
0
    def handle_msg(self, r):
        """
        @brief      Recover from snapshot data.
        @param      r  Dict: message json
        """
        Log.debug('handle message')
        if self.msg_handler:
            self.msg_handler.handle_wxsync(r)

        n = len(r['AddMsgList'])
        if n == 0:
            return

        if self.log_mode:
            echo(Constant.LOG_MSG_NEW_MSG % n)

        for msg in r['AddMsgList']:

            msgType = msg['MsgType']
            msgId = msg['MsgId']
            content = msg['Content'].replace('&lt;', '<').replace('&gt;', '>')
            raw_msg = None

            if msgType == self.wx_conf['MSGTYPE_TEXT']:
                # 地理位置消息
                if content.find('pictype=location') != -1:
                    location = content.split('<br/>')[1][:-1]
                    raw_msg = {
                        'raw_msg': msg,
                        'location': location,
                        'log': Constant.LOG_MSG_LOCATION % location
                    }
                # 普通文本消息
                else:
                    text = content.split(':<br/>')[-1]
                    raw_msg = {
                        'raw_msg': msg,
                        'text': text,
                        'log': text.replace('<br/>', '\n')
                    }
            elif msgType == self.wx_conf['MSGTYPE_IMAGE']:
                data = self.webwxgetmsgimg(msgId)
                fn = 'img_' + msgId + '.jpg'
                dir = self.save_data_folders['webwxgetmsgimg']
                path = save_file(fn, data, dir)
                raw_msg = {'raw_msg': msg,
                           'image': path,
                           'log': Constant.LOG_MSG_PICTURE % path}
            elif msgType == self.wx_conf['MSGTYPE_VOICE']:
                data = self.webwxgetvoice(msgId)
                fn = 'voice_' + msgId + '.mp3'
                dir = self.save_data_folders['webwxgetvoice']
                path = save_file(fn, data, dir)
                raw_msg = {'raw_msg': msg,
                           'voice': path,
                           'log': Constant.LOG_MSG_VOICE % path}
            elif msgType == self.wx_conf['MSGTYPE_SHARECARD']:
                info = msg['RecommendInfo']
                card = Constant.LOG_MSG_NAME_CARD % (
                    info['NickName'],
                    info['Alias'],
                    info['Province'], info['City'],
                    Constant.LOG_MSG_SEX_OPTION[info['Sex']]
                )
                namecard = '%s %s %s %s %s' % (
                    info['NickName'], info['Alias'], info['Province'],
                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']]
                )
                raw_msg = {
                    'raw_msg': msg,
                    'namecard': namecard,
                    'log': card
                }
            elif msgType == self.wx_conf['MSGTYPE_EMOTICON']:
                url = search_content('cdnurl', content)
                raw_msg = {'raw_msg': msg,
                           'emoticon': url,
                           'log': Constant.LOG_MSG_EMOTION % url}
            elif msgType == self.wx_conf['MSGTYPE_APP']:
                card = ''
                # 链接, 音乐, 微博
                if msg['AppMsgType'] in [
                    self.wx_conf['APPMSGTYPE_AUDIO'],
                    self.wx_conf['APPMSGTYPE_URL'],
                    self.wx_conf['APPMSGTYPE_OPEN']
                ]:
                    card = Constant.LOG_MSG_APP_LINK % (
                        Constant.LOG_MSG_APP_LINK_TYPE[msg['AppMsgType']],
                        msg['FileName'],
                        search_content('des', content, 'xml'),
                        msg['Url'],
                        search_content('appname', content, 'xml')
                    )
                    raw_msg = {
                        'raw_msg': msg,
                        'link': msg['Url'],
                        'log': card
                    }
                # 图片
                elif msg['AppMsgType'] == self.wx_conf['APPMSGTYPE_IMG']:
                    data = self.webwxgetmsgimg(msgId)
                    fn = 'img_' + msgId + '.jpg'
                    dir = self.save_data_folders['webwxgetmsgimg']
                    path = save_file(fn, data, dir)
                    card = Constant.LOG_MSG_APP_IMG % (
                        path,
                        search_content('appname', content, 'xml')
                    )
                    raw_msg = {
                        'raw_msg': msg,
                        'image': path,
                        'log': card
                    }
                else:
                    raw_msg = {
                        'raw_msg': msg,
                        'log': Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)
                    }
            elif msgType == self.wx_conf['MSGTYPE_STATUSNOTIFY']:
                Log.info(Constant.LOG_MSG_NOTIFY_PHONE)
            elif msgType == self.wx_conf['MSGTYPE_MICROVIDEO']:
                data = self.webwxgetvideo(msgId)
                fn = 'video_' + msgId + '.mp4'
                dir = self.save_data_folders['webwxgetvideo']
                path = save_file(fn, data, dir)
                raw_msg = {'raw_msg': msg,
                           'video': path,
                           'log': Constant.LOG_MSG_VIDEO % path}
            elif msgType == self.wx_conf['MSGTYPE_RECALLED']:
                recall_id = search_content('msgid', content, 'xml')
                text = Constant.LOG_MSG_RECALL
                raw_msg = {
                    'raw_msg': msg,
                    'text': text,
                    'recall_msg_id': recall_id,
                    'log': text
                }
            elif msgType == self.wx_conf['MSGTYPE_SYS']:
                raw_msg = {
                    'raw_msg': msg,
                    'sys_notif': content,
                    'log': content
                }
            elif msgType == self.wx_conf['MSGTYPE_VERIFYMSG']:
                name = search_content('fromnickname', content)
                raw_msg = {
                    'raw_msg': msg,
                    'log': Constant.LOG_MSG_ADD_FRIEND % name
                }
            else:
                raw_msg = {
                    'raw_msg': msg,
                    'log': Constant.LOG_MSG_UNKNOWN_MSG % (msgType, content)
                }

            isGroupMsg = '@@' in msg['FromUserName']+msg['ToUserName']
            if self.msg_handler and raw_msg:
                if isGroupMsg:
                    # handle group messages
                    g_msg = self.make_group_msg(raw_msg)
                    self.msg_handler.handle_group_msg(g_msg)
                else:
                    # handle personal messages
                    self.msg_handler.handle_user_msg(raw_msg)

            if self.log_mode:
                self.show_msg(raw_msg)
Ejemplo n.º 11
0

if __name__ == '__main__':
    # Built-in libraries.
    import argparse

    parser = argparse.ArgumentParser(
        prog='Random Policy Search',
        usage='python3 random_policy.py -n=500',
        description='Get the best score of n random policies',
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)

    parser.add_argument('-n',
                        type=int,
                        default=500,
                        help='How many random policies to generate.')
    parser.add_argument('--env',
                        type=str,
                        default=env_names.Atari.MS_PACMAN,
                        help='Name of env. See `env.names`')

    args = parser.parse_args()

    Log.info(f'{"="*30}')
    Log.info(f'{"Options":<15}\t{"Default":<15}')
    Log.info(f'{"="*30}')
    for k, v in vars(args).items():
        Log.info(f'{k:<15}\t{v:<15}')

    main(args=args)
Ejemplo n.º 12
0
 def getObservation(self):
     Log.info("getObservation not implemented for GridMap!")
     return self
Ejemplo n.º 13
0
class Plugin(Process):
    '''
    The base class of plugin.
    Input:
        timeout: the timeout of getting model data from queue
        unique: whether to drop the dumplicate model data
        log: whether to record log
    Usage:
        class XXXPlugin(Plugin):
            def handle(self, data):
                result = doSomeThing(data)
                self.outQueue.put(result)
    Example: 
        plugin = (DNSTrans(timeout=5) + DomainBrute(dictlist) + GoogleHackingPlugin(engine='baidu')) | HttpRecognize() | DataSavePlugin(mod="database") whill return a pluginObject
        plugin.dostart(startData)       
    '''

    def __init__(self, timeout=1, unique=True, log=True):
        Process.__init__(self)

        self.timeout = timeout
        self.unique = unique
        self._ins = list()
        self._outs = list()

        #addlist will record all the plugin object when use '+' or '|' operator
        self._addList = list()
        #orlist will record all the plugin object when use '+' operator
        self._orList = list()
        self.addAppend(self)
        self.orAppend(self)

        self._dataSet = list()

        self.log = True if log else None


    def addAppend(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right object is not plugin")
        self._addList.append(pluginObj)

    def orAppend(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right object is not plugin")
        self._orList.append(pluginObj)


    def __add__(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right parameter is not plugin")

        for obj in pluginObj._addList:
            self.addAppend(obj)
        self.orAppend(pluginObj)

        return self


    def __or__(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right parameter is not plugin")

        for obj in self._addList:
            pluginObj.addAppend(obj)

        inLen = len(self._orList)
        for inObj in self._orList:
            for outObj in pluginObj._orList:
                queue = RTD.taskManager.list()
                inObj._outs.append(queue)
                outObj._ins.append(queue)
        return pluginObj


    def __contains__(self, obj):
        for data in self._dataSet:
            if data == obj:
                return True
        return False


    def get(self):
        '''
        Get data from input queues.
        '''
        if not self._ins:
            raise PluginExit()

        gotData = False
        for i,queue in enumerate(self._ins):
            try:
                data = queue.pop()
            except IndexError:
                continue
            else:
                if not data:
                    del self._ins[i]
                    continue
                else:
                    if self.unique:
                        if data in self:
                            continue
                        else:
                            gotData = True
                            self._dataSet.append(data)
                            break
                    else:
                        gotData = True
                        break

        if not gotData:
            raise QueueEmpty()
        else:
            if self.log:
                self.log.info("plugin '{0}' got model {1}".format(self.__class__.__name__, data))
            return data


    def put(self, data):
        '''
        Put data to output queue.
        '''
        if self.log:
            self.log.info("plugin '{0}' put model {1}".format(self.__class__.__name__, data))

        for queue in self._outs:
            queue.insert(0,data)


    def quit(self):
        '''
        Quit process. if the process finish a task, it sends an empty object.
        '''
        if self._outs:
            self.put(Model())


    def dostart(self, startData):
        '''
        Start plugins.
        '''
        for obj in self._addList:
            if not obj._ins:
                queue = RTD.taskManager.list()
                for data in startData:
                    queue.insert(0,data)
                queue.insert(0,Model())
                obj._ins.append(queue)

        for plugin in self._addList:
            time.sleep(0.5)
            plugin.start()

    
    def run(self):
        '''
        Start process, the subclass must rewrite this function or 'handle' function
        when all the father processes quits, then break to quit
        '''

        if self.log:
            self.log = Log(self.name, toFile="plugin")
            self.log.info("plugin {0} start, ins:{1}, outs:{2}".format(self.name, self._ins, self._outs))
        else:
            self.log = False

        while True:
            try:
                data = self.get()
                #print "debug:", "plugin ", self.name, "getting", "ins<<<<<<<<", [str(x) for x in self._ins]
            except QueueEmpty:
                continue
            except IOError:
                break
            except EOFError:
                break
            except PluginExit:
                self.quit()
                #print "debug:", "plugin ", self.name, "quit"
                if self.log:
                    self.log.info("plugin {0} quit".format(self.name))
                break
            else:
                #print "debug:", "plugin ", self.name, "got", data
                self.handle(data)
            finally:
                time.sleep(self.timeout)


    def handle(self, data):
        '''
        Handle data, the subclass must rewrite this function or 'run' function
        '''
        pass

        
Ejemplo n.º 14
0
    def handle_group_msg(self, msg):
        """
        @brief      Recieve group messages
        @param      msg  Dict: packaged msg
        """
        # rename media files
        for k in ['image', 'video', 'voice']:
            if msg[k]:
                t = time.localtime(float(msg['timestamp']))
                time_str = time.strftime("%Y%m%d%H%M%S", t)
                # format: 时间_消息ID_群名
                file_name = '/%s_%s_%s.' % (time_str, msg['msg_id'],
                                            msg['group_name'])
                new_name = re.sub(r'\/\w+\_\d+\.', file_name, msg[k])
                Log.debug('rename file to %s' % new_name)
                os.rename(msg[k], new_name)
                msg[k] = new_name

        if msg['msg_type'] == 10000:
            # record member enter in group
            m = re.search(r'邀请(.+)加入了群聊', msg['sys_notif'])
            if m:
                name = m.group(1)
                col_enter_group = (
                    msg['msg_id'],
                    msg['group_name'],
                    msg['from_user_name'],
                    msg['to_user_name'],
                    name,
                    msg['time'],
                )
                self.db.insert(Constant.TABLE_RECORD_ENTER_GROUP,
                               col_enter_group)

            # record rename group
            n = re.search(r'(.+)修改群名为“(.+)”', msg['sys_notif'])
            if n:
                people = n.group(1)
                to_name = n.group(2)
                col_rename_group = (
                    msg['msg_id'],
                    msg['group_name'],
                    to_name,
                    people,
                    msg['time'],
                )
                self.db.insert(Constant.TABLE_RECORD_RENAME_GROUP,
                               col_rename_group)

                # upadte group in GroupList
                for g in self.wechat.GroupList:
                    if g['UserName'] == msg['from_user_name']:
                        g['NickName'] = to_name
                        break

        # normal group message
        col = (msg['msg_id'], msg['group_owner_uin'], msg['group_name'],
               msg['group_count'], msg['from_user_name'], msg['to_user_name'],
               msg['user_attrstatus'], msg['user_display_name'],
               msg['user_nickname'], msg['msg_type'], msg['emoticon'],
               msg['text'], msg['image'], msg['video'], msg['voice'],
               msg['link'], msg['namecard'], msg['location'],
               msg['recall_msg_id'], msg['sys_notif'], msg['time'],
               msg['timestamp'])
        self.db.insert(Constant.TABLE_GROUP_MSG_LOG, col)

        try:
            groupName = msg['group_name']
            Log.debug(msg['group_name'])
            Log.info(msg['user_nickname'])
            if groupName.strip() == "微信机器人测试" or groupName.strip(
            ) == "沉迷学习,日渐消瘦":
                Log.debug(msg['text'])
                if msg['raw_msg']['FromUserName'][:2] == '@@':
                    dst = msg['raw_msg']['FromUserName']
                elif msg['raw_msg']['ToUserName'][:2] == '@@':
                    dst = msg['raw_msg']['ToUserName']

                msg_info = self.wechat.bot.handleGroupMsg(
                    msg['text'], msg['user_nickname'])
                self.wechat.webwxsendmsg(msg_info, dst)
        except Exception, e:
            Log.debug(str(e))
            pass
Ejemplo n.º 15
0
class Plugin(Process):
    '''
    The base class of plugin.
    Input:
        timeout: the timeout of getting model data from queue
        unique: whether to drop the dumplicate model data
        log: whether to record log
    Usage:
        class XXXPlugin(Plugin):
            def handle(self, data):
                result = doSomeThing(data)
                self.outQueue.put(result)
    Example: 
        plugin = (DNSTrans(timeout=5) + DomainBrute(dictlist) + GoogleHackingPlugin(engine='baidu')) | HttpRecognize() | DataSavePlugin(mod="database") whill return a pluginObject
        plugin.dostart(startData)       
    '''
    def __init__(self, timeout=1, unique=True, log=True):
        Process.__init__(self)

        self.timeout = timeout
        self.unique = unique
        self._ins = list()
        self._outs = list()

        #addlist will record all the plugin object when use '+' or '|' operator
        self._addList = list()
        #orlist will record all the plugin object when use '+' operator
        self._orList = list()
        self.addAppend(self)
        self.orAppend(self)

        self._dataSet = list()

        self.log = True if log else None

    def addAppend(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right object is not plugin")
        self._addList.append(pluginObj)

    def orAppend(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right object is not plugin")
        self._orList.append(pluginObj)

    def __add__(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right parameter is not plugin")

        for obj in pluginObj._addList:
            self.addAppend(obj)
        self.orAppend(pluginObj)

        return self

    def __or__(self, pluginObj):
        if not isinstance(pluginObj, Plugin):
            raise PluginError("the right parameter is not plugin")

        for obj in self._addList:
            pluginObj.addAppend(obj)

        inLen = len(self._orList)
        for inObj in self._orList:
            for outObj in pluginObj._orList:
                queue = RTD.taskManager.list()
                inObj._outs.append(queue)
                outObj._ins.append(queue)
        return pluginObj

    def __contains__(self, obj):
        for data in self._dataSet:
            if data == obj:
                return True
        return False

    def get(self):
        '''
        Get data from input queues.
        '''
        if not self._ins:
            raise PluginExit()

        gotData = False
        for i, queue in enumerate(self._ins):
            try:
                data = queue.pop()
            except IndexError:
                continue
            else:
                if not data:
                    del self._ins[i]
                    continue
                else:
                    if self.unique:
                        if data in self:
                            continue
                        else:
                            gotData = True
                            self._dataSet.append(data)
                            break
                    else:
                        gotData = True
                        break

        if not gotData:
            raise QueueEmpty()
        else:
            if self.log:
                self.log.info("plugin '{0}' got model {1}".format(
                    self.__class__.__name__, data))
            return data

    def put(self, data):
        '''
        Put data to output queue.
        '''
        if self.log:
            self.log.info("plugin '{0}' put model {1}".format(
                self.__class__.__name__, data))

        for queue in self._outs:
            queue.insert(0, data)

    def quit(self):
        '''
        Quit process. if the process finish a task, it sends an empty object.
        '''
        if self._outs:
            self.put(Model())

    def dostart(self, startData):
        '''
        Start plugins.
        '''
        for obj in self._addList:
            if not obj._ins:
                queue = RTD.taskManager.list()
                for data in startData:
                    queue.insert(0, data)
                queue.insert(0, Model())
                obj._ins.append(queue)

        for plugin in self._addList:
            time.sleep(0.5)
            plugin.start()

    def run(self):
        '''
        Start process, the subclass must rewrite this function or 'handle' function
        when all the father processes quits, then break to quit
        '''

        if self.log:
            self.log = Log(self.name, toFile="plugin")
            self.log.info("plugin {0} start, ins:{1}, outs:{2}".format(
                self.name, self._ins, self._outs))
        else:
            self.log = False

        while True:
            try:
                data = self.get()
                #print "debug:", "plugin ", self.name, "getting", "ins<<<<<<<<", [str(x) for x in self._ins]
            except QueueEmpty:
                continue
            except IOError:
                break
            except EOFError:
                break
            except PluginExit:
                self.quit()
                #print "debug:", "plugin ", self.name, "quit"
                if self.log:
                    self.log.info("plugin {0} quit".format(self.name))
                break
            else:
                #print "debug:", "plugin ", self.name, "got", data
                self.handle(data)
            finally:
                time.sleep(self.timeout)

    def handle(self, data):
        '''
        Handle data, the subclass must rewrite this function or 'run' function
        '''
        pass
Ejemplo n.º 16
0
    def handle_msg(self, r):
        Log.debug('handle message')

        n = len(r['AddMsgList'])
        if n == 0:
            # 权宜之计
            time.sleep(self.time_out)
            return

        if self.log_mode:
            echo(Constant.LOG_MSG_NEW_MSG % n)

        for raw_msg in r['AddMsgList']:

            msgType = raw_msg['MsgType']
            msgId = raw_msg['MsgId']
            content = raw_msg['Content'].replace('&lt;',
                                                 '<').replace('&gt;', '>')
            content = trans_coding(content).encode('utf-8')

            rmsg = {}
            reply_flag = False

            # 获取收发人信息
            from_id = raw_msg['FromUserName']
            to_id = raw_msg['ToUserName']
            if from_id[0:2] == '@@':
                from_user = self.get_group_by_id(from_id)
                if re.search(":<br/>", content, re.IGNORECASE):
                    who_id = content.split(':<br/>')[0]
                    from_who = self.get_group_user_by_id(who_id, from_id)
                    rmsg['FromWho'] = from_who
                to_user = self.get_group_user_by_id(to_id, from_id)
                content_use = ':<br/>'.join(content.split(':<br/>')[1:])
                reply_flag = True
            elif to_id[0:2] == '@@':
                from_user = self.get_group_user_by_id(from_id, to_id)
                to_user = self.get_group_by_id(to_id)
                echo(content + '\n')
                content_use = ':<br/>'.join(content.split(':<br/>')[1:])
            else:
                from_user = self.get_user_by_id(from_id)
                to_user = self.get_user_by_id(to_id)
                content_use = content
                if from_id != self.User['UserName']:
                    reply_flag = True

            rmsg['raw_msg'] = raw_msg
            rmsg['FromUser'] = from_user
            rmsg['ToUser'] = to_user

            # 消息内容分类获取
            if msgType == self.wx_conf['MSGTYPE_TEXT']:
                # 地理位置消息
                if content_use.find('pictype=location') != -1:
                    location = content_use.split(':<br/>')[0]
                    rmsg['location'] = location
                    rmsg['text'] = location
                    rmsg['log'] = Constant.LOG_MSG_LOCATION % location
                # 普通文本消息
                else:
                    rmsg['text'] = content_use
                    rmsg['log'] = content_use

                # 文字信息分类处理
                self.add_operate_list(rmsg, reply_flag)

            elif msgType == self.wx_conf['MSGTYPE_IMAGE']:
                data = self.webwxgetmsgimg(msgId)
                fn = 'img_' + msgId + '.jpg'
                dir = self.save_data_folders['webwxgetmsgimg']
                path = save_file(fn, data, dir)
                rmsg['text'] = '[图片]'
                rmsg['image'] = path
                rmsg['log'] = Constant.LOG_MSG_PICTURE % path
            elif msgType == self.wx_conf['MSGTYPE_VOICE']:
                data = self.webwxgetvoice(msgId)
                fn = 'voice_' + msgId + '.mp3'
                dir = self.save_data_folders['webwxgetvoice']
                path = save_file(fn, data, dir)
                rmsg['text'] = '[音频]'
                rmsg['voice'] = path
                rmsg['log'] = Constant.LOG_MSG_VOICE % path
            elif msgType == self.wx_conf['MSGTYPE_SHARECARD']:
                info = raw_msg['RecommendInfo']
                card = Constant.LOG_MSG_NAME_CARD % (
                    info['NickName'], info['Alias'], info['Province'],
                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']])
                namecard = '%s %s %s %s %s' % (
                    info['NickName'], info['Alias'], info['Province'],
                    info['City'], Constant.LOG_MSG_SEX_OPTION[info['Sex']])
                rmsg['text'] = '[名片]' + trans_coding(namecard).encode('utf-8')
                rmsg['namecard'] = namecard
                rmsg['log'] = card
            elif msgType == self.wx_conf['MSGTYPE_EMOTICON']:
                url = search_content('cdnurl', content_use)
                rmsg['text'] = '[表情]'
                rmsg['emoticon'] = url
                rmsg['log'] = Constant.LOG_MSG_EMOTION % url
            elif msgType == self.wx_conf['MSGTYPE_APP']:
                card = ''
                # 链接, 音乐, 微博
                if raw_msg['AppMsgType'] in [
                        self.wx_conf['APPMSGTYPE_AUDIO'],
                        self.wx_conf['APPMSGTYPE_URL'],
                        self.wx_conf['APPMSGTYPE_OPEN']
                ]:
                    card = Constant.LOG_MSG_APP_LINK % (
                        Constant.LOG_MSG_APP_LINK_TYPE[raw_msg['AppMsgType']],
                        raw_msg['FileName'],
                        search_content('des', content_use,
                                       'xml'), raw_msg['Url'],
                        search_content('appname', content_use, 'xml'))
                    rmsg['text'] = '[分享链接]'
                    rmsg['link'] = raw_msg['Url']
                    rmsg['log'] = card
                # 图片
                elif raw_msg['AppMsgType'] == self.wx_conf['APPMSGTYPE_IMG']:
                    data = self.webwxgetmsgimg(msgId)
                    fn = 'img_' + msgId + '.jpg'
                    dir = self.save_data_folders['webwxgetmsgimg']
                    path = save_file(fn, data, dir)
                    card = Constant.LOG_MSG_APP_IMG % (
                        path, search_content('appname', content_use, 'xml'))
                    rmsg['text'] = '[图片]'
                    rmsg['image'] = path
                    rmsg['log'] = card
                else:
                    rmsg['text'] = ''
                    rmsg['log'] = Constant.LOG_MSG_UNKNOWN_MSG % (msgType,
                                                                  content_use)
            elif msgType == self.wx_conf['MSGTYPE_STATUSNOTIFY']:
                Log.info(Constant.LOG_MSG_NOTIFY_PHONE)
                rmsg['text'] = '[状态通知]'
                rmsg['log'] = Constant.LOG_MSG_NOTIFY_PHONE[:-1]
            elif msgType == self.wx_conf['MSGTYPE_MICROVIDEO']:
                data = self.webwxgetvideo(msgId)
                fn = 'video_' + msgId + '.mp4'
                dir = self.save_data_folders['webwxgetvideo']
                path = save_file(fn, data, dir)
                rmsg['text'] = '[小视频]'
                rmsg['video'] = path
                rmsg['log'] = Constant.LOG_MSG_VIDEO % path
            elif msgType == self.wx_conf['MSGTYPE_RECALLED']:
                recall_id = search_content('msgid', content_use, 'xml')
                text = Constant.LOG_MSG_RECALL
                rmsg['text'] = text
                rmsg['recall_msg_id'] = recall_id
                rmsg['log'] = text
            elif msgType == self.wx_conf['MSGTYPE_SYS']:
                rmsg['text'] = content_use
                rmsg['sys_notif'] = content_use
                rmsg['log'] = content_use
            elif msgType == self.wx_conf['MSGTYPE_VERIFYMSG']:
                name = search_content('fromnickname', content_use)
                rmsg['text'] = '[添加好友请求]'
                rmsg['log'] = Constant.LOG_MSG_ADD_FRIEND % name

                # 好友自动同意在此处添加
                count = len(self.AddUserList)
                add_user = {
                    'Order': count + 1,
                    'UserName': raw_msg['RecommendInfo']['UserName'],
                    'NickName': raw_msg['RecommendInfo']['NickName'],
                    'Ticket': raw_msg['RecommendInfo']['Ticket']
                }
                self.AddUserList.append(add_user)
                # 先添加与列表中,之后可根据命令提示来允许添加谁

            elif msgType == self.wx_conf['MSGTYPE_VIDEO']:
                # 暂时无法对该类型进行处理,即视频信息
                rmsg['text'] = '[视频消息]'
                rmsg['log'] = Constant.LOG_MSG_UNKNOWN_MSG % (msgType,
                                                              content_use)
            else:
                rmsg['text'] = ''
                rmsg['log'] = Constant.LOG_MSG_UNKNOWN_MSG % (msgType,
                                                              content_use)

            if self.log_mode:
                self.show_msg(rmsg)

        try:
            if self.msg_handler:
                self.msg_handler.save_into_db(self.DBStoreMSGList)
                self.msg_handler.handle_commands(self.CommandList)
                self.msg_handler.get_bot_reply(self.GroupNeedReplyList,
                                               self.UserNeedReplyList)
                self.msg_handler.auto_reply(self.ReplyList)
                self.msg_handler.save_into_db(self.DBStoreBOTReplyList)
        except:
            traceback.print_exc()
            Log.error(traceback.format_exc())
        finally:
            self.CommandList = []
            self.DBStoreMSGList = []
            self.GroupNeedReplyList = []
            self.UserNeedReplyList = []
            self.ReplyList = []
            self.DBStoreBOTReplyList = []