def api_log(self, network, user, channel, req, qs, posted): # FIXME: Choose between bots based on network grep = qs.get('grep', [''])[0] after = qs.get('seen', [None])[0] limit = int(qs.get('limit', [0])[0]) timeout = int(qs.get('timeout', [0])[0]) if timeout: timeout += time.time() bot = self.networks[network] rules = bot.irc_parsed_mode(channel) if (rules.get('secret', False) or rules.get('key', False) or rules.get('invite_only', False)): if not (user and channel in user['channels']): # This will hide the channel key and other parameters for r in rules: if rules[r]: rules[r] = True rules['event'] = 'pleasejoin' return 'application/json', HttpdLite.json_encode( [[get_timed_uid(), rules]]) data = [] try: while not data: data = bot.irc_channel_log(channel) if after or grep: data = [ x for x in data if (x[0] > after) and ( not grep or grep in x[1]['nick'].lower() or grep in x[1].get('text', '')) ] if timeout and not data: cond = threading.Condition() ev = self.event_loop.add_sleeper(timeout, cond, 'API request') bot.irc_watch_channel(channel, ev) cond.acquire() cond.wait() cond.release() self.event_loop.remove_sleeper(ev) if time.time() >= timeout: break except SelectAborted: pass if limit: data = data[-limit:] return 'application/json', HttpdLite.json_encode(data)
def api_log(self, network, user, channel, req, qs, posted): # FIXME: Choose between bots based on network grep = qs.get('grep', [''])[0] after = qs.get('seen', [None])[0] limit = int(qs.get('limit', [0])[0]) timeout = int(qs.get('timeout', [0])[0]) if timeout: timeout += time.time() bot = self.networks[network] rules = bot.irc_parsed_mode(channel) if (rules.get('secret', False) or rules.get('key', False) or rules.get('invite_only', False)): if not (user and channel in user['channels']): # This will hide the channel key and other parameters for r in rules: if rules[r]: rules[r] = True rules['event'] = 'pleasejoin' return 'application/json', HttpdLite.json_encode([ [get_timed_uid(), rules] ]) data = [] try: while not data: data = bot.irc_channel_log(channel) if after or grep: data = [x for x in data if (x[0] > after) and (not grep or grep in x[1]['nick'].lower() or grep in x[1].get('text', ''))] if timeout and not data: cond = threading.Condition() ev = self.event_loop.add_sleeper(timeout, cond, 'API request') bot.irc_watch_channel(channel, ev) cond.acquire() cond.wait() cond.release() self.event_loop.remove_sleeper(ev) if time.time() >= timeout: break except SelectAborted: pass if limit: data = data[-limit:] return 'application/json', HttpdLite.json_encode(data)
def api_logout(self, network, user, channel, req, qs, posted): del self.networks[network].users[user.uid] req.setCookie('muid-%s' % network, '', delete=True) sockfd = self.event_loop.fds_by_uid[user.uid] self.event_loop.sendall(sockfd, 'QUIT :Logged off\r\n') return 'application/json', HttpdLite.json_encode(['ok'])
def api_logout(self, network, user, channel, req, qs, posted): del self.networks[network].users[user.uid] req.setCookie('muid-%s' % network, '', delete=True) sockfd = self.event_loop.fds_by_uid[user.uid] self.event_loop.sendall(sockfd, 'QUIT :Logged off\r\n') return 'application/json', HttpdLite.json_encode(['ok'])
def handleUserLogin(self, req, page_prefix, path, qs): state = qs.get('state', ['/'])[0] network, channel = self.get_channel_from_path(state[1:]) provider, token = req.auth_info profile = {'source': provider, 'home': 'The Internet'} # First, pull details from their on-line profile, if possible... try: if provider == 'Facebook': fbp = req.server.auth_handler.getFacebookProfile(token) profile.update({ 'name': fbp.get('name'), 'home': fbp.get('hometown', {}).get('name', 'The Internet'), 'uid': 'fb%s' % fbp.get('id', ''), 'pic': 'https://graph.facebook.com/%s/picture' % fbp.get('id', ''), 'url': 'https://www.facebook.com/%s' % fbp.get('username', fbp.get('id', '')) }) elif provider == 'Google': profile = req.server.auth_handler.getGoogleProfile(token) else: print '*** Not sure how to get profiles from %s' % provider except (IOError, OSError): # Network problems...? pass # Create a nick-name for them nickname = profile.get('name', 'Guest %x' % random.randint(0, 10000)) while (len(nickname) > 15 and ' ' in nickname): nickname = nickname.rsplit(' ', 1)[0] profile['nick'] = self.dumb_down(nickname) # Create an IRC client, start the connection. client = IrcClient().irc_profile(profile).irc_channels([channel]) self.networks[network].users[client.uid] = client self.connect_client(network, client) # Finally, set a cookie with their client's UID. req.setCookie('muid-%s' % network, '%s,pending' % client.uid) print 'Logged in: %s' % HttpdLite.json_encode(profile, indent=2) return req.sendRedirect(page_prefix + state)
def handleUserLogin(self, req, page_prefix, path, qs): state = qs.get('state', ['/'])[0] network, channel = self.get_channel_from_path(state[1:]) provider, token = req.auth_info profile = { 'source': provider, 'home': 'The Internet' } # First, pull details from their on-line profile, if possible... try: if provider == 'Facebook': fbp = req.server.auth_handler.getFacebookProfile(token) profile.update({ 'name': fbp.get('name'), 'home': fbp.get('hometown', {}).get('name', 'The Internet'), 'uid': 'fb%s' % fbp.get('id', ''), 'pic': 'https://graph.facebook.com/%s/picture' % fbp.get('id', ''), 'url': 'https://www.facebook.com/%s' % fbp.get('username', fbp.get('id', '')) }) elif provider == 'Google': profile = req.server.auth_handler.getGoogleProfile(token) else: print '*** Not sure how to get profiles from %s' % provider except (IOError, OSError): # Network problems...? pass # Create a nick-name for them nickname = profile.get('name', 'Guest %x' % random.randint(0, 10000)) while (len(nickname) > 15 and ' ' in nickname): nickname = nickname.rsplit(' ', 1)[0] profile['nick'] = self.dumb_down(nickname) # Create an IRC client, start the connection. client = IrcClient().irc_profile(profile).irc_channels([channel]) self.networks[network].users[client.uid] = client self.connect_client(network, client) # Finally, set a cookie with their client's UID. req.setCookie('muid-%s' % network, '%s,pending' % client.uid) print 'Logged in: %s' % HttpdLite.json_encode(profile, indent=2) return req.sendRedirect(page_prefix + state)
def api_say(self, network, user, channel, req, qs, posted): sockfd = self.event_loop.fds_by_uid[user.uid] privmsg = 'PRIVMSG %s :%s\r\n' % (channel, posted['msg'][0].decode('utf-8')) self.event_loop.sendall(sockfd, privmsg.encode('utf-8')) return 'application/json', HttpdLite.json_encode(['ok'])
} if len(sys.argv) > 1: arg = sys.argv.pop(1) config['irc']['irc']['servers'] = [arg.rsplit('/', 1)[0]] config['irc']['irc']['channels'] = channels = {} for channel in arg.rsplit('/', 1)[1].split(',', 1): channels[channel] = { 'description': 'IRC channel', 'access': 'open' } if len(sys.argv) > 1: arg = sys.argv.pop(1) for channel in channels: channels[channel]['access'] = arg print 'Config is: %s' % HttpdLite.json_encode(config, indent=2) return config if __name__ == "__main__": try: mutiny = Mutiny(Configuration()) except (IndexError, ValueError, OSError, IOError): print '%s\n' % traceback.format_exc() print 'Usage: %s <nick> irc://<server:port>/<channel>' % sys.argv[0] print ' %s <nick> ssl://<server:port>/<channel>' % sys.argv[0] print print 'Logs and settings will be stored here: %s' % config['work_dir'] print sys.exit(1) try:
def api_say(self, network, user, channel, req, qs, posted): sockfd = self.event_loop.fds_by_uid[user.uid] privmsg = 'PRIVMSG %s :%s\r\n' % (channel, posted['msg'][0].decode('utf-8')) self.event_loop.sendall(sockfd, privmsg.encode('utf-8')) return 'application/json', HttpdLite.json_encode(['ok'])
'userinfo': 'Mutiny %s' % VERSION } if len(sys.argv) > 1: arg = sys.argv.pop(1) config['irc']['irc']['servers'] = [ arg.rsplit('/', 1)[0] ] config['irc']['irc']['channels'] = channels = {} for channel in arg.rsplit('/', 1)[1].split(',', 1): channels[channel] = {'description': 'IRC channel', 'access': 'open'} if len(sys.argv) > 1: arg = sys.argv.pop(1) for channel in channels: channels[channel]['access'] = arg print 'Config is: %s' % HttpdLite.json_encode(config, indent=2) return config if __name__ == "__main__": try: mutiny = Mutiny(Configuration()) except (IndexError, ValueError, OSError, IOError): print '%s\n' % traceback.format_exc() print 'Usage: %s <nick> irc://<server:port>/<channel>' % sys.argv[0] print ' %s <nick> ssl://<server:port>/<channel>' % sys.argv[0] print print 'Logs and settings will be stored here: %s' % config['work_dir'] print sys.exit(1) try: