def main(): dbpool.runQuery(SQL_CREATE_CAR_TABLE) factory = protocol.ServerFactory() factory.protocol = CarCenter reactor.listenTCP(8000, factory) reactor.run()
def render_GET(self, request, session): """Channel history""" def render_messages(l): request.write(json.dumps(l)) request.finish() msg_sql = """SELECT events.id, source_identities.token, events.args[0:1], events.kind, to_char(events.timestamp, 'IYYY:MM:DD-HH24:MI:SS') FROM identities RIGHT OUTER JOIN events on (events.target = identities.id) JOIN identities as source_identities on (events.source = source_identities.id) WHERE (identities.token ILIKE %s OR (events.target IS NULL AND %s ILIKE ANY(events.args))) AND events.observer_email = %s """ query_args = [self.name, self.name, session.email] before_sql = "\nAND events.id < %s" count_sql = "\nORDER BY events.id DESC LIMIT %s;" if "before" in request.args: before = int(request.args["before"][0]) msg_sql += before_sql query_args.append(before) if "count" in request.args: count = max(0, min(int(request.args["count"][0]), 1000)) else: count = 100 msg_sql += count_sql query_args.append(count) d = dbpool.runQuery(msg_sql, query_args) d.addCallback(render_messages) return server.NOT_DONE_YET
def render_GET(self, request, session): def render_topic(l): request.write(json.dumps(l[0][0])) request.finish() topic_sql = """SELECT topic FROM channels WHERE name ILIKE %s;""" d = dbpool.runQuery(topic_sql, (self.name,)) d.addCallback(render_topic) return server.NOT_DONE_YET
def start(self): """Initialize the IRC client. Load existing configuration and join all clients.""" start_sql = """SELECT user_email, hostname, port, ssl, nick FROM servers JOIN server_configs ON (servers.id = server_configs.server_id) WHERE server_configs.enabled = true; """ d = dbpool.runQuery(start_sql) d.addCallback(self.server_init) return d
def render_GET(self, request, session): """List of channels by default""" def render_channels(l): request.write(json.dumps(l)) request.finish() chan_sql = """SELECT name FROM channels JOIN servers ON channels.server_id = servers.id WHERE user_email = %s AND hostname = %s ORDER BY name;""" d = dbpool.runQuery(chan_sql, (session.email,self.name)); d.addCallback(render_channels) return server.NOT_DONE_YET
def __init__(self, network, identity, email): self.network = network self.identity = identity self.email = email event_id_sql = """SELECT max(events.id) FROM events JOIN servers ON (servers.id = events.network_id) WHERE events.observer_email = %s AND servers.hostname = %s; """ # Deferred for finishing init self.init = dbpool.runQuery(event_id_sql, (self.email, self.network)) def finish(l): self.current_id = (l[0][0] or 0) self.init.addCallback(finish)
def signedOn(self): self.channels = list() self.publish = RemoteEventPublisher(self.network, self.nickname, self.email) self.publish.event("signedOn", self.nickname) def initial_join(l): for channel in l: self.join(channel[0], channel[1] or None) # Join channels join_sql = """SELECT name, key FROM channels JOIN servers ON (servers.id = channels.server_id) WHERE enabled = true AND user_email = %s AND servers.hostname = %s; """ d = dbpool.runQuery(join_sql, (self.email, self.network)) d.addCallback(initial_join)
def messageReceived(self, message): """Protocol is this: Servers control: {user} global {command} {arguments...} Connection control: {user} {server} {command} {arguments...} """ print("Controller message: {0}".format(message)) user, server = message[:2] command = message[2] if len(message) >= 3: command_args = message[3:] else: command_args = None subject = self.hashi.clients[user] # If there's a server specified, pick that server here if server != "global" and command != "connect": subject = subject[server] # Otherwise, the iterable subject means for all servers if command == 'connect': hostname, nick = command_args start_sql = """SELECT hostname, port, ssl FROM servers WHERE hostname = %s;""" d = dbpool.runQuery(start_sql, (hostname,)) d.addCallback(self.connectCallback, user, nick) elif command == 'join': # Arguments are 'channel [key]' subject.join(*command_args[:2]) elif command == 'msg': # Arguments are 'target message ...' target = command_args[0] msg = command_args[1] subject.msg(target, msg, 500 - len(target)) subject.publish.event("privmsg", subject.nickname, target, msg) elif command == 'action': target = command_args[0] msg = command_args[1] # IRCClient.me prepends # erroneously subject.ctcpMakeQuery(target, [('ACTION', command_args[1])]) subject.publish.event("action", subject.nickname, target, msg) elif command == 'names': channel = command_args[0] subject.names(channel).addCallback(subject.got_names, channel)
def render_GET(self, request, session): list_sql = """SELECT user_email, hostname, port, ssl, nick FROM servers LEFT JOIN server_configs ON (servers.id = server_configs.server_id);""" d = dbpool.runQuery(list_sql) d.addCallback(self.list_servers, request) return server.NOT_DONE_YET