当前位置: 首页>>代码示例>>Python>>正文


Python Bus.send_out_message方法代码示例

本文整理汇总了Python中bus.Bus.send_out_message方法的典型用法代码示例。如果您正苦于以下问题:Python Bus.send_out_message方法的具体用法?Python Bus.send_out_message怎么用?Python Bus.send_out_message使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bus.Bus的用法示例。


在下文中一共展示了Bus.send_out_message方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Botter

# 需要导入模块: from bus import Bus [as 别名]
# 或者: from bus.Bus import send_out_message [as 别名]
class Botter(object):
    def __init__(self, username, username_password=None, realname=None, init_channels=None):
        self._conn = None
        self._write_conn = None
        self._read_conn = None

        self.username = username
        self.username_password = username_password
        if not realname:
            self.realname = username
        else:
            self.realname = realname
        self.bus = Bus()
        self.plugins = Loader(self.bus)
        self.plugins.load_plugins()

        self.joined = False

        if init_channels is None:
            self._init_channels = []
        else:
            self._init_channels = init_channels

    def connect(self, server, port):
        LOG.info('Connect to %s:%s' % (server, port))
        self._con_opts = (server, port)
        self._conn = socket.create_connection((server, port))
        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)
#.........这里部分代码省略.........
开发者ID:gigimon,项目名称:pyBotter,代码行数:103,代码来源:pybotter.py


注:本文中的bus.Bus.send_out_message方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。