rendered paste bodyclass Bot: def __init__(self, ServerInfo, ChanInfo): self.ServerInfo = ServerInfo self.ChanInfo = ChanInfo self.Allowed = [] file = open("settings/chans.txt") try: for line in file: self.Join(line) finally: file.close() file = open("settings/access.txt") try: for line in file: self.Allowed.append(line) finally: file.close() Output = [] message = {} def __parse(self, input): """Checks what kind of message recieved and gets appropriate response""" if input.startswith(":" + self.ServerInfo["server"]): return None elif input.startswith("PING"): return input.replace("I", "O", 1) else: if input.find(" PRIVMSG ") != -1 and \ input.find(" PRIVMSG ") < input.find(" :"): Pattern = input.split(" PRIVMSG ", 1) self.message["from"] = Pattern[0][1:Pattern[0].find("!")] Pattern = Pattern[1].split(" :", 1) self.message["chan"] = Pattern[0] self.message["message"] = Pattern[1][0:Pattern[1].find("\r\n")] print self.message if self.message["from"] in self.Allowed and \ self.message["chan"].startswith("#"): return self.HandleUserMessage() else: return None def Parse(self, input): """Parses input and stores output string in local Output variable""" self.Output = self.__parse(input) def ParseRet(self, input): """Parses output and returns output string""" return self.__parse(input) def Join(self, chan): self.Output.append("JOIN " + chan + "\r\n") def HandleUserMessage(self): """Gets a message sent by user, extracts the commands and arguments and calls ProcessCommand""" if self.message["message"].find(" ") != -1: Temp = self.message["message"].split(" ", 1) Command = {"command":Temp[0][1:], "args":Temp[1]} else: Command = {"command":self.message["message"][1:], "args":""} print Command return self.ProcessCommand(Command) def ProcessCommand(self, command): """Takes command and argment, and optional message dictionary. Finds appropriate function and sends arguments to it""" Command = getattr(self, command["command"].capitalize() \ + "BotFunc", None) if Command: print command["command"].capitalize() + " " + args " called" return Command(command["args"]) else: print "Command " + command["command"].capitalize() + " not found" return None def ChanString(self, string): string = "PRIVMSG " + self.message["chan"] + " :" + string + "\r\n" return string class RegularBot(Bot): def HelpBotFunc(self, args): """Usage: "!help command". \ Prints the usage of a command""" if args == "": return self.ChanString("""Invalid argument, please \ enter name of command to get help for. \ To get a list of commands type "!list".""") Command = getattr(self, args.capitalize() + "BotFunc", None) if callable(Command): if Command.__doc__: return self.ChanString(Command.__doc__) return self.ChanString("Command " + args + " not found.") def KickBotFunc(self, args): """Usage: "!kick username". \ Kicks specified user""" Thingy = args.split(" ") print Thingy if args != "": return "KICK " + self.message["chan"] + " " + Thingy[0] + \ " :" + " ".join(Thingy[1:]) + "\r\n" else: return self.ChanString("Kick function requires an argument") def OperBotFunc(self, args): """Usage: "!oper username". \ Makes user operator""" if args != "": return "MODE " + self.message["chan"] + " +o " + args + "\r\n" else: return "MODE " + self.message["chan"] + " +o " \ + self.message["from"] + "\r\n" def TopicBotFunc(self, args): """Usage: "!topic text". \ Sets the channel topic to text specified""" if args != "": return "TOPIC " + self.message["chan"] + " :" + args + "\r\n" else: return self.ChanString("Topic function requires an argument") def JoinBotFunc(self, args): """Usage: "!join #room". \ Joins specified chatroom""" return "JOIN " + args + "\r\n" def LeaveBotFunc(self, args): """Quits current channel.""" return "PART " + self.message["chan"] + \ " :Called by " + self.message["from"] + "\r\n" def QuitBotFunc(self, args): """Quits the server, to quit chatroom only use "!leave""" return "QUIT :Called by " + self.message["from"] + "\r\n"