class AsyncCommands(object): """Async commands example. This is what it's look like on irc:: <gawel> !get <gawel> !put item <irc3> items added to queue <irc3> item """ def __init__(self, bot): self.bot = bot self.queue = Queue() @command def put(self, mask, target, args): """Put items in queue %%put <words>... """ for w in args['<words>']: self.queue.put_nowait(w) yield 'items added to queue' @command async def get(self, mask, target, args): """Async get items from the queue %%get """ messages = [] message = await self.queue.get() messages.append(message) while not self.queue.empty(): message = await self.queue.get() messages.append(message) return messages
def __init__(self, bot): maxsize = int(bot.config.get('ctcp_max_replies', 3)) self.queue = Queue(loop=bot.loop, maxsize=maxsize) self.handle = None self.event = irc3.event(irc3.rfc.CTCP, self.on_ctcp) bot.attach_events(self.event) self.bot = bot
class CTCP: """ctcp replies""" def __init__(self, bot): maxsize = int(bot.config.get('ctcp_max_replies', 3)) self.queue = Queue(loop=bot.loop, maxsize=maxsize) self.handle = None self.event = irc3.event(irc3.rfc.CTCP, self.on_ctcp) bot.attach_events(self.event) self.bot = bot def clear_queue(self): self.bot.log.info('CTCP clear queue') while not self.queue.empty(): self.queue.get_nowait() self.handle = None def handle_flood(self): self.bot.log.warn('CTCP Flood detected. ' 'Ignoring requests for 30s') # ignore events for 30s self.bot.detach_events(self.event) self.bot.loop.call_later(30, self.bot.attach_events, self.event) def on_ctcp(self, mask=None, target=None, ctcp=None, **kw): lctcp = ctcp.split(' ')[0].lower() if lctcp in self.bot.config.ctcp: try: self.queue.put_nowait((mask.nick, lctcp)) except QueueFull: self.handle_flood() else: if self.handle is None: self.handle = self.bot.loop.call_later(1, self.clear_queue) data = self.bot.config.ctcp[lctcp].format(now=datetime.now(), **self.bot.config) self.bot.ctcp_reply(mask.nick, '%s %s' % (lctcp.upper(), data))
def handle_flood(self): self.bot.log.warn('CTCP Flood detected. ' 'Ignoring requests for 30s') # reset queue self.queue = Queue(loop=self.bot.loop) # cancel handle to ignore current queue if self.handle is not None: self.handle.cancel() self.handle = None # ignore events for 30s self.bot.detach_events(self.event) self.bot.loop.call_later(30, self.bot.attach_events, self.event)
class CTCP(object): """ctcp replies""" def __init__(self, bot): maxsize = int(bot.config.get('ctcp_max_replies', 3)) self.queue = Queue(loop=bot.loop, maxsize=maxsize) self.handle = None self.event = irc3.event(irc3.rfc.CTCP, self.on_ctcp) bot.attach_events(self.event) self.bot = bot def send_replies(self): replies = [] while not self.queue.empty(): target, ctcp = self.queue.get_nowait() data = self.bot.config.ctcp[ctcp].format(now=datetime.now(), **self.bot.config) replies.append((target, '%s %s' % (ctcp.upper(), data))) if replies: self.bot.call_many(self.bot.ctcp_reply, replies) self.handle = None def handle_flood(self): self.bot.log.warn('CTCP Flood detected. ' 'Ignoring requests for 30s') # reset queue self.queue = Queue(loop=self.bot.loop) # cancel handle to ignore current queue if self.handle is not None: self.handle.cancel() self.handle = None # ignore events for 30s self.bot.detach_events(self.event) self.bot.loop.call_later(30, self.bot.attach_events, self.event) def on_ctcp(self, mask=None, target=None, ctcp=None, **kw): lctcp = ctcp.split(' ')[0].lower() if lctcp in self.bot.config.ctcp: try: self.queue.put_nowait((mask.nick, lctcp)) except QueueFull: self.handle_flood() else: if self.handle is None: self.handle = self.bot.loop.call_later( 2, self.send_replies) if not self.bot.config.async: # used for testing self.send_replies()
def connection_made(self, transport): self.closed = False self.transport = transport self.queue = Queue() self.uuid = self.transport.get_extra_info('peername') self.factory.clients[self.uuid] = self self.encoding = self.factory.encoding self.data = {} self.modes = set() self.channels = set() t = time.time() self.data.update( uuid=self.uuid, host=self.uuid[0], connection_made=t, data_received=t, srv=self.factory.config.servername, version=self.factory.config.version)
class AsyncCommands(object): """Async commands example. This is what it's look like on irc:: <gawel> !get <gawel> !put item <irc3> items added to queue <irc3> item """ def __init__(self, bot): self.bot = bot self.queue = Queue() @command def put(self, mask, target, args): """Put items in queue %%put <words>... """ for w in args['<words>']: self.queue.put_nowait(w) yield 'items added to queue' @command @asyncio.coroutine def get(self, mask, target, args): """Async get items from the queue %%get """ messages = [] message = yield from self.queue.get() messages.append(message) while not self.queue.empty(): message = yield from self.queue.get() messages.append(message) return messages
class IrcClient(asyncio.Protocol): """asyncio protocol to handle an irc connection""" def connection_made(self, transport): self.closed = False self.transport = transport self.queue = Queue() self.uuid = self.transport.get_extra_info('peername') self.factory.clients[self.uuid] = self self.encoding = self.factory.encoding self.data = {} self.modes = set() self.channels = set() t = time.time() self.data.update( uuid=self.uuid, host=self.uuid[0], connection_made=t, data_received=t, srv=self.factory.config.servername, version=self.factory.config.version) def __getattr__(self, attr): return self.data[attr] def get_nick(self): return self.data.get('nick') def set_nick(self, nick): self.data['nick'] = nick self.data['mask'] = utils.IrcString( '{nick}!{username}@{uuid[0]}'.format(**self.data)) nick = property(get_nick, set_nick) @property def registered(self): return bool('nick' in self.data and 'username' in self.data) def decode(self, data): # pragma: no cover """Decode data with bot's encoding""" encoding = getattr(self, 'encoding', 'ascii') return data.decode(encoding, 'ignore') def data_received(self, data): # pragma: no cover self.data['data_received'] = time.time() data = self.decode(data) if not self.queue.empty(): data = self.queue.get_nowait() + data lines = data.split('\r\n') self.queue.put_nowait(lines.pop(-1)) for line in lines: self.factory.dispatch(line, client=self) def fwrite(self, messages, **kwargs): kwargs['c'] = self if not isinstance(messages, (list, tuple)): fmt = getattr(messages, 'tpl', messages) else: fmt = '\r\n'.join([getattr(m, 'tpl', m) for m in messages]) self.write(fmt.format(**kwargs)) def write(self, data): if data is not None: self.factory.dispatch(data, client=self, iotype='out') data = data.encode(self.encoding) if not data.endswith(b'\r\n'): data = data + b'\r\n' self.transport.write(data) def connection_lost(self, exc): self.factory.log.critical('connection lost (%s): %r', id(self.transport), exc) self.factory.notify('connection_lost', client=self) del self.factory.clients[self.uuid] if not self.closed: self.closed = True self.close() def close(self): # pragma: no cover if not self.closed: self.factory.log.critical('closing old transport (%r)', id(self.transport)) try: self.transport.close() finally: self.closed = True def __str__(self): if 'nick' in self.data: return '{nick}'.format(**self.data) else: return 'unknown' __repr__ = __str__
def __init__(self, bot): self.bot = bot self.queue = Queue()
def __init__(self, bot): self.bot = bot self.queue = Queue() self.HSL = HSL()
class IrcClient(asyncio.Protocol): """asyncio protocol to handle an irc connection""" def connection_made(self, transport): self.closed = False self.transport = transport self.queue = Queue() self.uuid = self.transport.get_extra_info('peername') self.factory.clients[self.uuid] = self self.encoding = self.factory.encoding self.data = {} self.modes = set() self.channels = set() t = time.time() self.data.update(uuid=self.uuid, host=self.uuid[0], connection_made=t, data_received=t, srv=self.factory.config.servername, version=self.factory.config.version) def __getattr__(self, attr): return self.data[attr] def get_nick(self): return self.data.get('nick') def set_nick(self, nick): self.data['nick'] = nick self.data['mask'] = utils.IrcString( '{nick}!{username}@{uuid[0]}'.format(**self.data)) nick = property(get_nick, set_nick) @property def registered(self): return bool('nick' in self.data and 'username' in self.data) def decode(self, data): # pragma: no cover """Decode data with bot's encoding""" encoding = getattr(self, 'encoding', 'ascii') return data.decode(encoding, 'ignore') def data_received(self, data): # pragma: no cover self.data['data_received'] = time.time() data = self.decode(data) if not self.queue.empty(): data = self.queue.get_nowait() + data lines = data.split('\r\n') self.queue.put_nowait(lines.pop(-1)) for line in lines: self.factory.dispatch(line, client=self) def fwrite(self, messages, **kwargs): kwargs['c'] = self if not isinstance(messages, (list, tuple)): fmt = getattr(messages, 'tpl', messages) else: fmt = '\r\n'.join([getattr(m, 'tpl', m) for m in messages]) self.write(fmt.format(**kwargs)) def write(self, data): if data is not None: self.factory.dispatch(data, client=self, iotype='out') data = data.encode(self.encoding) if not data.endswith(b'\r\n'): data = data + b'\r\n' self.transport.write(data) def connection_lost(self, exc): self.factory.log.critical('connection lost (%s): %r', id(self.transport), exc) self.factory.notify('connection_lost', client=self) del self.factory.clients[self.uuid] if not self.closed: self.closed = True self.close() def close(self): # pragma: no cover if not self.closed: self.factory.log.critical('closing old transport (%r)', id(self.transport)) try: self.transport.close() finally: self.closed = True def __str__(self): if 'nick' in self.data: return '{nick}'.format(**self.data) else: return 'unknown' __repr__ = __str__
class IrcClient(asyncio.Protocol): """asyncio protocol to handle an irc connection""" def connection_made(self, transport): self.closed = False self.transport = transport self.queue = Queue() self.uuid = self.transport.get_extra_info("peername") self.factory.clients[self.uuid] = self self.encoding = self.factory.encoding self.data = {} self.modes = set() self.channels = set() t = time.time() self.data.update( uuid=self.uuid, host=self.uuid[0], connection_made=t, data_received=t, srv=self.factory.config.servername, version=self.factory.config.version, ) def __getattr__(self, attr): return self.data[attr] def get_nick(self): return self.data.get("nick") def set_nick(self, nick): self.data["nick"] = nick self.data["mask"] = utils.IrcString("{nick}!{username}@{uuid[0]}".format(**self.data)) nick = property(get_nick, set_nick) @property def registered(self): return bool("nick" in self.data and "username" in self.data) def decode(self, data): # pragma: no cover """Decode data with bot's encoding""" encoding = getattr(self, "encoding", "ascii") return data.decode(encoding, "ignore") def data_received(self, data): # pragma: no cover self.data["data_received"] = time.time() data = self.decode(data) if not self.queue.empty(): data = self.queue.get_nowait() + data lines = data.split("\r\n") self.queue.put_nowait(lines.pop(-1)) for line in lines: self.factory.dispatch(line, client=self) def encode(self, data): """Encode data with bot's encoding""" if isinstance(data, text_type): # pragma: no cover data = data.encode(self.encoding) return data def fwrite(self, messages, **kwargs): kwargs["c"] = self if not isinstance(messages, (list, tuple)): fmt = getattr(messages, "tpl", messages) else: fmt = "\r\n".join([getattr(m, "tpl", m) for m in messages]) self.write(fmt.format(**kwargs)) def write(self, data): if data is not None: self.factory.dispatch(data, client=self, iotype="out") data = self.encode(data) if not data.endswith(b"\r\n"): data = data + b"\r\n" self.transport.write(data) def connection_lost(self, exc): self.factory.log.critical("connection lost (%s): %r", id(self.transport), exc) self.factory.notify("connection_lost", client=self) del self.factory.clients[self.uuid] if not self.closed: self.closed = True self.close() def close(self): # pragma: no cover if not self.closed: self.factory.log.critical("closing old transport (%r)", id(self.transport)) try: self.transport.close() finally: self.closed = True def __str__(self): if "nick" in self.data: return "{nick}".format(**self.data) else: return "unknown" __repr__ = __str__