def kill(self, nick): """ Disconnects a user from the server Kinda like a channel kick but from the server """ if self.is_oper(): if nick in self.server.users.keys(): self.server.users[nick].on_kill("You were killed.") self.writeline(json.dumps({ "type": "SERVERMSG", "message": "You killed %s" % nick })) self.server.writeline("%s killed %s" % (self.nick, nick)) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s isn't on the server." % nick })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `kill` command" }))
def client_login(self, client, hashedpw): if os.path.exists("accounts/%s.json" % client.nick): user = json.load(open("accounts/%s.json" % client.nick, 'r')) if hashedpw == user["password"]: client.account = user client.writeline(json.dumps({ "type": "SERVERMSG", "message": "You're now logged in!" })) self.writeline("%s logged in" % client.nick) client.on_login() else: client.writeline("ERROR Invalid password for %s" % client.nick) client.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid nick password"), "message": "invalid password for %s" % client.nick })) self.writeline("%s failed to login" % client.nick) else: client.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid account name"), "message": "account %s not found." % client.nick }))
def command_channel_flag(self, chan_name, switch, flag, args=True): """ Sets a flag in a channel chan_name: name of channel you want to set the flag in flag: the flag you want to set args: arguments for that flag """ if args == "true": args = True elif args == "false": args = False elif args.isdigit(): args = int(args) if switch == "add": if chan_name in self.channels: self.channels[chan_name].add_channel_flag(self, flag, args) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You're not in %s" % chan_name })) elif switch == "remove": if chan_name in self.channels: self.channels[chan_name].remove_channel_flag(self, flag) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You're not in %s" % chan_name }))
def sapart(self, nick, channel): """ Force a user to leave (part) a channel """ if self.is_oper(): if nick in self.server.users: if channel in self.server.channels: self.server.channels[channel].on_part( self.server.users[nick], "sapart") self.server.users[nick].on_sapart(channel) self.writeline("You forced %s to leave %s" % (nick, channel)) self.server.writeline("%s forced %s to leave %s" % ( self.nick, nick, channel)) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s isn't on the server." % nick })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s doesn't exist" % channel })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `sapart` command" }))
def set_nick(self, client, nick): """ Sets the clients nick """ self.logout() if len(nick) <= self.server.CONFIG["MAX_NICK_LENGTH"]: if len(nick) > self.server.CONFIG["MIN_NICK_LENGTH"]: for c in nick: # make sure each char in in the set if not c in self.server.CONFIG["NICK_CHAR_SET"]: client.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "Your nick my only contain these chars: %s" % ( self.server.CONFIG["NICK_CHAR_SET"] ) })) self.set_nick(client, client.readline()) return if not self.server.find_nick(nick) and nick not in self.server.CONFIG["RESERVED_NICKS"]: old_nick = str(client.nick) client.nick = str(nick) del self.server.users[old_nick] self.server.users[client.nick] = self self.writeline(json.dumps({ "type": "NICK", "old_nick": old_nick, "new_nick": client.nick })) self.server.writeline( "%s is now known as %s" % (old_nick, nick)) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("nick in use"), "message": "%s is already in use. Please choose a new nick." % nick })) self.set_nick(client, client.readline()) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("nick excecced limit"), "message": "Your nick is too short. Please choose a nick with more than %i chars" % self.server.CONFIG["MIN_NICK_LENGTH"] })) self.set_nick(client, client.readline()) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("nick excecced limit"), "message": "Your nick is too long. Please choose a nick with more than %i chars" % self.server.CONFIG["MAX_NICK_LENGTH"] })) self.set_nick(client, client.readline())
def command_oper_rehash(self): if self.is_oper(): self.server.rehash() else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `announcement` command" }))
def client_whois(self, client, nick): if self.users.get(nick): self.users[nick].on_whois(client) self.writeline("%s used whois on %s" % (client.nick, nick)) else: client.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s isn't on the server" % client.nick }))
def message_channel(self, channel, message): """ Sends a message to a channel on the server """ if channel in self.server.channels.keys(): self.server.channels[channel].on_message(self, message) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "No channel named %s" % channel }))
def command_oper_global_message(self, message): """ Sends a message to all clients connected to the server """ if self.is_oper(): self.server.global_message(message) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `announcement` command" }))
def command_channel_register(self, chan_name): """ Register a channel on the serevr chan_name: name of channel you want to register """ if chan_name in self.channels: self.channels[chan_name].register(self) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You're not in %s" % chan_name }))
def command_channel_unban(self, chan_name, ip): """ Unban an IP from a channel chan_name: Channel name ip: ip you want to unban """ if chan_name in self.channels: self.channels[chan_name].unban_ip(self, ip) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You are not in %s" % chan_name }))
def command_channel_ban(self, chan_name, nick): """ Bad a client from a channel chan_name: Channel name nick: nick of user you want to bad """ if chan_name in self.channels: self.channels[chan_name].ban_user(self, nick) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You are not in %s" % chan_name }))
def sajoin(self, nick, channel): """ Force a user to join a channel this will bypass all restrictions """ if self.is_oper(): if channel in self.server.channels: if nick in self.server.users: self.server.channels[channel].add_client( self.server.users[nick]) self.server.users[nick].on_sajoin(channel) self.writeline("%s was forced to join %s" % (nick, channel)) self.writeline(json.dumps({ "type": "SERVERMSG", "message": "You forced %s to join %s" % (nick, channel) })) self.server.writeline("%s forced %s to join %s" % (self.nick, nick, channel)) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s isn't on the server." % nick })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s doesn't exist" % channel })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `sajoin` command" }))
def command_channel_kick(self, chan_name, nick, message="GTFO"): """ Kick a client from a channel chan_name: Channel name nick: nick of client you want to kick from channel message: reason you're kicking this client """ if chan_name in self.channels: self.channels[chan_name].kick_user(self, nick, message) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You are not in %s" % chan_name }))
def ban_ip(self, ip): """ Adds an ip to the banlist.txt """ if self.is_oper(): self.server.ban_ip(self, ip) self.writeline(json.dumps({ "type": "SERVERMSG", "message": "Banned %s from the server" % ip })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `ban` command" }))
def command_channel_members(self, chan_name): """ Gives a list of all the clients in a channel chan_name: Channel name """ if chan_name in self.channels: self.writeline(json.dumps({ "type": "CHANUSERS", "channel": chan_name, "members": self.channels[chan_name].users.keys() })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You are not in %s" % chan_name }))
def command_channel_badword(self, chan_name, switch, badword): """ Adds a word to the channel badword list chan_name: Channel name switch: add or remove bad word badword: word to add to the badword list """ if chan_name in self.channels: if switch.lower() == "add": self.channels[chan_name].add_badword(self, badword) elif switch.lower() == "remove": self.channels[chan_name].remove_badword(badword) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You're not in %s" % chan_name }))
def message_nick(self, nick, message): """ Sends a message to another client on the server nick: nick of the client you want to message message: the message you want to send to that client """ if nick in self.server.users.keys(): self.server.users[nick].writeline(json.dumps({ "type": "USERMSG", "nick": self.nick, "ip": self.ip, "message": message })) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("invalid channel/nick"), "message": "%s isn't on the server" % self.nick }))
def command_channel_clientflag(self, chan_name, switch, nick, flag): """ Add a flag to a client on a channel chan_name: Channel name switch: add or remove bad word flag: flag you want to add/remove """ if chan_name in self.channels: if switch.lower() == "add": self.channels[chan_name].add_client_flag( self, nick, flag) elif switch.lower() == "remove": self.channels[chan_name].remove_client_flag( self, nick, flag) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not in channel"), "message": "You're not in %s" % chan_name }))
def sanick(self, nick, new_nick): """ Force a user to change their nick """ if self.is_oper(): if self.server.users.get(nick): self.server.users[nick].nick = new_nick self.server.users[nick].on_sanick(new_nick) self.server.users[new_nick] = self.server.users[nick] self.writeline(json.dumps({ "type": "SERVERMSG", "message": "You changed %s nick to %s" % (nick, new_nick) })) self.server.writeline( "%s changed %s's nick to %s" % (self.nick, nick, new_nick)) else: self.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("not an oper"), "message": "You need to be an oper to use the `sanick` command" }))
def register_account(self, client, email, hashedpw): if os.path.exists("accounts/%s.json" % client.nick): client.writeline("This nick is already registered.") client.writeline(json.dumps({ "type": "ERROR", "code": errorcodes.get("nick already registered"), "message": "Nick is already registered" })) else: with open("accounts/%s.json" % client.nick, 'w') as f: f.write(json.dumps({ "email": email, "password": hashedpw, "notes": [], "uuid": client.nick + ':' + str(uuid.uuid4()), "time_registered": int(time.time()) }, sort_keys=True, indent=4, separators=(',', ': '))) self.writeline( "%s created a new account [%s]" % (client.ip, client.nick)) client.writeline(json.dumps({ "type": "SERVERMSG", "message": "Your nick is now registered! You can now login with `login <password>`" }))