當前位置: 首頁>>代碼示例>>Python>>正文


Python Bus.send_in_message方法代碼示例

本文整理匯總了Python中bus.Bus.send_in_message方法的典型用法代碼示例。如果您正苦於以下問題:Python Bus.send_in_message方法的具體用法?Python Bus.send_in_message怎麽用?Python Bus.send_in_message使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在bus.Bus的用法示例。


在下文中一共展示了Bus.send_in_message方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: Botter

# 需要導入模塊: from bus import Bus [as 別名]
# 或者: from bus.Bus import send_in_message [as 別名]

#.........這裏部分代碼省略.........
        self._conn.send("""PASS {uniquepass}\r\n
        NICK {username}\r\n
        USER {username} testbot testbot :{realname}\r\n""".format(uniquepass=uuid.uuid1().hex,
            username=self.username,
            realname=self.realname))
        self._write_conn = self._conn.dup()
        self._read_conn = self._conn.dup()

    def pong(self, msg):
        answer = msg.strip().split(':')[1]
        self._write_conn.send('PONG %s\r\n' % answer)

    def _parse_message(self, buf):
        LOG.info('Start message parsing')
        messages = []
        for msg in buf.split('\r\n'):
            msg = msg.strip()
            if not msg:
                continue
            LOG.info('Parse: %s' % msg)
            if msg.startswith('PING'):
                self.pong(msg)
                continue
            if 'ERROR :Closing Link:' in msg:
                self.connect(self._con_opts[0], self._con_opts[1])
                return []
            msg_opts = msg.split()
            user_opts = msg_opts[0][1:].split('!')
            if len(user_opts) > 1:
                sender, user_ident = user_opts[0], user_opts[1]
            else:
                sender, user_ident = user_opts[0], None
            receiver = msg_opts[2]
            msg_type = msg_opts[1]
            message = ' '.join(msg_opts[3:])[1:]
            if msg_type == 'NOTICE' and receiver == 'AUTH' and message.startswith('*** You connected'):
                for chan in self._init_channels:
                    self.join_channel(chan)
            if msg_type == 'NOTICE' and sender == 'NickServ' and 'NickServ IDENTIFY' in message:
                self.authorize()
                continue
            messages.append({'sender': sender,
                             'receiver': receiver,
                             'msg_type': msg_type,
                             'message': message,
                             'user_ident': user_ident})
        return messages

    def authorize(self):
        LOG.info('Authorize in nickserv')
        if self.username_password:
            self.bus.send_out_message({'receiver':'NickServ',
                                       'message': 'identify %s' % self.username_password})

    def send_message(self, message):
        if isinstance(message, list):
            for m in message:
                LOG.info('Send "%s" to "%s"' % (m['message'], m['receiver']))
                self._write_conn.send('PRIVMSG %s :%s\r\n' % (m['receiver'], m['message']))
        else:
            LOG.info('Send "%s" to "%s"' % (message['message'], message['receiver']))
            self._write_conn.send('PRIVMSG %s :%s\r\n' % (message['receiver'], message['message']))

    def join_channel(self, channel):
        LOG.info('Join to channel %s' % channel)
        self.joined = True
        if not channel.startswith('#'):
            channel = '#' + channel
        if len(channel.split(':')) > 1:
            channel, password = channel.split(':')
            self._write_conn.send('JOIN %s %s\r\n' % (channel, password))
        else:
            self._write_conn.send('JOIN %s\r\n' % channel)

    def work(self):
        """Start Loader check bus to input messages"""
        receive = gevent.spawn(self._start_recv)
        sender = gevent.spawn(self._start_send)
        while not self.joined:
            gevent.sleep(1)
        self.plugins.work()
        gevent.joinall([ receive, sender ])

    def _start_recv(self):
        buf = ''
        while True:
            try:
                msg = self._read_conn.recv(512)
            except socket.error, e:
                LOG.error('Can\'t send message: %s' % e)
                if 'Broken pipe' in e:
                    LOG.info('Reconnect to server')
                    self.connect(self._con_opts[0], self._con_opts[1])
            buf += msg
            if len(msg) < 512 and msg.endswith('\r\n'):
                messages = self._parse_message(buf)
                buf = ''
                if messages:
                    self.bus.send_in_message(messages)
            gevent.sleep(0.1)
開發者ID:gigimon,項目名稱:pyBotter,代碼行數:104,代碼來源:pybotter.py


注:本文中的bus.Bus.send_in_message方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。