def start_prog(self): self.name = str(self.makename("".join(self.name.split()))) #to prevent unicode names, repeated names, and names with spaces self.log = Log() self.log.open(self.name) self.server.add(self.data_dict['ip'], self.name) self.server.named_users[self.name] = self prog = Prog(self) self.server.reactor.spawnProcess(prog, self.server.PRG, [], path = self.server.CWD ) self.server.progs.append(prog) self.prog = prog out.write(self.name, "had connected. Users:", len(self.server.users)) self.transport.write("Hello, %s.\n" % self.name) self.prog.transport.write(self.name + "\n") if self.name.lower().startswith("sex"): def boobs(reactor): self.transport.write("BOOBS") reactor.callLater(0, boobs, reactor) self.server.reactor.callLater(0, boobs, self.server.reactor) self.state = 1
def startup(self): out.open(stdout, suffix="\n>>> ") sys.stderr = out out.write("Loading JSON files...") self.stats = json.load(open("stats.json")) self.IPs = json.load(open("IPs.json")) self.state = 1
def stringReceived(self, string): if self.state is 0 and not self.data_dict: #If are not running and have no data try: self.data_dict = json.loads(string) except ValueError: out.write('Someone sent "%s" instead of an respectable ID' % string) self.transport.write("Bad IDing, you fool!\n") self.transport.loseConnection() return None self.ip = self.data_dict["ip"] try: self.name = self.server.IPs[self.ip] self.start_prog() except KeyError: #if this IP has no registered name self.transport.write("What do you wish to be called? [no spaces] ") elif self.state is 0 and self.data_dict: #if we are not running and have data name = self.sanitize(string) if not name == string: #If it was changed name = "Cracker" out.write("User at", self.data_dict["ip"], "tried to name himself", string) self.name = name self.start_prog() elif self.state is 1 and self.prog.state is 1: #If running line = ' '.join(self.sanitize(string).split()) #normalize spacing and remove potentially malicious charecters self.log.write("User:"******"\n")
def shutdown(self): with open("stats.json", "w") as statfile: json.dump(self.stats, statfile, indent = 4) statfile.flush() with open("IPs.json", "w") as IPfile: json.dump(self.IPs, IPfile, indent = 4) IPfile.flush() out.write("\nJSON files dumped") self.state = 2 out.close()
def processEnded(self, reason): self.state = 2 if self.user.state is 1: if reason.check(error.ProcessDone): #Is it done? quitmsg = 'cleanly' else: quitmsg = 'with errors!' reason.printDetailedTraceback(self.user.log) self.user.transport.write('You seem to of have crashed your ' 'game, you insensitive clod!') self.user.log.write("Ended", quitmsg) out.write(self.user.name + "'s process has ended", quitmsg) self.user.state = 2
def onecmd(self, line): cmd, rest, line = self.parseline(line) if not line: #If the line is blank, do nothing return if cmd is None: #If there was no command return self.default(rest) self.lastcmd = line if cmd == '': return self.default(rest) else: try: func = getattr(self, 'do_' + cmd) except AttributeError: return self.default(line) func_args = getargspec(func).args args = rest.split() firstarg = dict(enumerate(func_args)).get(1, None) #Gets first arg, defaults to None. Lists don't support defaults. if firstarg not in ("line", "arg"): #if it's not a command like help for i, func_arg in enumerate(func_args[1:]): #We don't want to think about the first arg, self try: arg = args[i] if "parse_" + func_arg in dir(self): arg = getattr(self, "parse_" + func_arg)(arg) args[i] = arg except ValueError as e: #if the checker doesn't like something return out.write(e.message) except IndexError: return out.write("Omitted argument", func_arg) return func(*args) else: return func(rest)
def connectionLost(self, reason): """ Called when a user quits. If we are running, shuts down everything that we opened. """ self.server.users.remove(self) users = len(self.server.users) if self.state: #If it is a true state, like one or two try: del self.server.named_users[self.name] except KeyError: out.write(self.name, "connected twice, or somehow removed his " "name from the system..") out.write(self.name, 'has quit. Users:', users) self.log.close() if self.prog.state is not 2: #if it's not dead already self.prog.transport.signalProcess('KILL') self.prog.state = 2 else: #if we've not started yet out.write("Someone without ID quit. Users:", users) self.state = 2
def run(self): out.write("Running on", self.PORT) self.reactor.run() return None
def do_whois(self, user): "Gives a user's IP" out.write(user.name + ":", user.ip)
def do_send(self, user, *msgs): "Send a message to a user" user.send("\nAdmin sez: " + " ".join(msgs)) out.write("Message sent.")
def do_echo(self, msg): """Simple echo command""" out.write(msg)
def do_stop(self): """Stops the server""" out.write("Stopping..") self.server.reactor.stop() self.stop__ = True
def errReceived(self, data): """ Errors should be logged and outputed """ self.user.log.write("ERROR: " + data) out.write("{}'s process has errored: {}".format(self.user.name, data))