def connect(self): """Convenience method that creates Server instance""" print('connect irc', self.addr) self.irc_client = IrcClient(self.addr, blocking=False) self.irc_client.socket.send_callback = log_send self.irc_client.connect()
class IrcBot(BotMixin): def __init__(self, host, port, config): self.name = config.get('name', None) self.last_ping = 0 self.bot_plugins = [] self.irc_client = None self.addr = (host, port) self.plugin = 'irc' self.config = config self.relay_outs = [] assert self.config def connect(self): """Convenience method that creates Server instance""" print('connect irc', self.addr) self.irc_client = IrcClient(self.addr, blocking=False) self.irc_client.socket.send_callback = log_send self.irc_client.connect() def init(self): self.connect() self.load_plugins() def process(self): self.irc_client.runloop_unit() while True: message = self.irc_client.dispatch(raw=True) if message: self.input(message) self.crons() self.output() # self.autoping() if not self.irc_client.dispatchable(): break time.sleep(0.1) def input(self, data): data = Message(data) function_name = "process_" + data.type.lower() dbg("got {}".format(function_name)) for plugin in self.bot_plugins: plugin.register_jobs() plugin.do(function_name, data) def autoping(self): # hardcode the interval to 3 seconds now = int(time.time()) if now > self.last_ping + 30: self.irc_client.send_line('ping :dontkillme') self.last_ping = now def send_item(self, data): self.irc_client(data) time.sleep(.2) def relay(self, bot, relay_ins): def channel_for(channel_id): return bot.slack_client.server.channels.find(channel_id) def name(channel): if self.name: if not channel.name.startswith(self.name): return None return channel.name.split('-', 1)[1] else: return channel.name for data in relay_ins: if 'channel' in data: channel = channel_for(data['channel']) if channel is None: continue if data['type'] == 'join': line = u'join #{}'.format(name(channel)) self.irc_client.send_line(line) elif data['type'] == 'message': print('do?', data) message = data.get('text', '') try: import html message = html.unescape(message) except ImportError: pass message = message.replace('\r', ' ').replace('\n', r' ') user_id = data.get('user', None) if user_id: user = bot.slack_client.server.users.find(user_id) else: user = None user # usable, but not yet if message: line = u'privmsg #{} :{}'.format(name(channel), message) self.irc_client.send_line(line) else: line = u'privmsg #{} :{}'.format(self.config['irc'].get('nick', 'slairck'), unicode(data)) self.irc_client.send_line(line)
class IrcBot(BotMixin): def __init__(self, host, port, config): self.name = config.get('name', None) self.last_ping = 0 self.bot_plugins = [] self.irc_client = None self.addr = (host, port) self.plugin = 'irc' self.config = config self.relay_outs = [] assert self.config def connect(self): """Convenience method that creates Server instance""" print('connect irc', self.addr) self.irc_client = IrcClient(self.addr, blocking=False) self.irc_client.socket.send_callback = log_send self.irc_client.connect() def init(self): self.connect() self.load_plugins() def process(self): self.irc_client.runloop_unit() while True: message = self.irc_client.dispatch(raw=True) if message: self.input(message) self.crons() self.output() # self.autoping() if not self.irc_client.dispatchable(): break time.sleep(0.1) def input(self, data): data = Message(data) function_name = "process_" + data.type.lower() dbg("got {}".format(function_name)) for plugin in self.bot_plugins: plugin.register_jobs() plugin.do(function_name, data) def autoping(self): # hardcode the interval to 3 seconds now = int(time.time()) if now > self.last_ping + 30: self.irc_client.send_line('ping :dontkillme') self.last_ping = now def send_item(self, data): self.irc_client(data) time.sleep(.2) def relay(self, bot, relay_ins): def channel_for(channel_id): return bot.slack_client.server.channels.find(channel_id) def name(channel): if self.name: if not channel.name.startswith(self.name): return None return channel.name.split('-', 1)[1] else: return channel.name for data in relay_ins: if 'channel' in data: channel = channel_for(data['channel']) if channel is None: continue if data['type'] == 'join': line = u'join #{}'.format(name(channel)) self.irc_client.send_line(line) elif data['type'] == 'message': print('do?', data) message = data.get('text', '') try: import html message = html.unescape(message) except ImportError: pass message = message.replace('\r', ' ').replace('\n', r' ') user_id = data.get('user', None) if user_id: user = bot.slack_client.server.users.find(user_id) else: user = None user # usable, but not yet if message: line = u'privmsg #{} :{}'.format(name(channel), message) self.irc_client.send_line(line) else: line = u'privmsg #{} :{}'.format( self.config['irc'].get('nick', 'slairck'), unicode(data)) self.irc_client.send_line(line)