コード例 #1
0
ファイル: sample.py プロジェクト: knitori/shanghai-doll
class IRCHandler:

    def __init__(self):
        self.network = Network('Shanghai|Doll')

    async def on_connected(self, prot):
        print('\033[32;1mConnected to {}:{}\033[0m'
              .format(prot.host, prot.port))
        prot.sendline('NICK {}'.format(self.network.mynick))
        prot.sendline('USER doll * * :Shanghai Doll')

    async def on_disconnect(self, prot):
        print('\033[31;1mDisconnected from {}:{}\033[0m'
              .format(prot.host, prot.port))
        loop = asyncio.get_event_loop()
        loop.call_later(30, self.reconnect, prot.host, prot.port)

    def reconnect(self, host, port):
        self.__init__()
        asyncio.ensure_future(IRCProtocol(host, port, self).run())

    async def on_message(self, prot: IRCProtocol, msg: Message):
        self.network.feed_message(msg)

        if msg.isnumeric('001'):
            prot.sendline('JOIN #botted')
            prot.loop.call_later(
                15, prot.sendline,
                'PRIVMSG #botted :waited for '
                '15 seconds to say something! o/')
        if isinstance(msg, Privmsg):
            if msg.message == '+test':
                prot.sendline('QUIT :quitting you bastard!'
                              .format(msg.sender))
            elif msg.message == '+cycle':
                if is_channel(msg.target):
                    prot.sendline('PART {} :Cycling! Because I can!'
                                  .format(msg.target))
                    prot.sendline('JOIN {}'.format(msg.target))
            elif msg.message.startswith('+'):
                return

            if is_channel(msg.target) and \
                    ('http://' in msg.message or 'https://' in msg.message)\
                    and not msg.message.startswith('+'):
                def replace_url(match):
                    the_url, = match.groups('url')
                    return '\x0304\x02{}\x0F'.format(the_url)
                text = utils.url_pattern.sub(replace_url, msg.message)
                prot.sendline('PRIVMSG {} :Matched: {}'
                              .format(msg.target, text))
        if isinstance(msg, CtcpRequest):
            if msg.ctcp_command == 'VERSION':
                prot.sendline('NOTICE {} :\x01VERSION {}\x01'
                              .format(msg.sender, 'Shanghai Doll 0.1dev'))
コード例 #2
0
ファイル: sample.py プロジェクト: knitori/shanghai-doll
 def __init__(self):
     self.network = Network('Shanghai|Doll')