def join(self, channel): try: messages.print_message("Joining " + channel + "...") self.bot.irc.send(utils.encode("JOIN " + channel + "\n")) while True: message = self.bot.irc.recv(self.bot.buffer).decode("UTF-8") message = message.strip('\n\r') if message: messages.print_message(message, messages.MessageType.NOTICE) if message.find("End of /NAMES list.") != null: return True if message.find(":Illegal") != null: raise Exception("Could not join: " + channel + " - Illegal channel name.") if message.find("PING :") != null: commands.pong(self.bot, message) if message.find(":Cannot") != null: raise Exception("Could not join: " + channel + " - No invitation was given.") else: break return True except Exception as error: raise Exception(error)
def run(self, args): params = args[0][0] if len(params) == 0: messages.send_message( self.bot, self.sender + ", '!join' takes a required parameter -- <channel> ", self.sender) else: if len(params) > 1: channel = params[1] try: joined = self.join(channel) if joined: messages.print_message("Joined " + channel, messages.MessageType.SUCCESS) messages.send_message(self.bot, "Joined " + channel, self.sender) except Exception as error: messages.print_message(str(error), messages.MessageType.EXCEPTION) messages.send_message(self.bot, str(error), self.sender) else: messages.send_message( self.bot, self.sender + ", '!join' takes a required parameter -- <channel> ", self.sender)
def join_server(bot): messages.print_message("Connecting to " + bot.server_name + "...", messages.MessageType.GENERAL, True) bot.irc.connect((bot.server_name, bot.server_port)) bot.irc.send( utils.encode("USER " + bot.bot_username + " Ping Pong " + bot.bot_realname + "\n")) bot.irc.send(utils.encode("NICK " + bot.bot_nick + "\n"))
def main(): print_greeting() current = sys.path[0] sys.path.append(current + "/modules") sys.path.append(current + "/plugins") if logging.Logger.verbosity_level > logging.Verbosity.DEFAULT: messages.print_message("Appending system module paths...") for path in sys.path: messages.print_message(("Path: %s" % path), NONE, False) start_resolver() start_bot() wait() # Wait for input since we're multi-threading. exit(0) # End the script gracefully.
def load_plugins(self, path): self.check_plugins(path) plugins = os.listdir(path) for plugin in plugins: if plugin.find(".py") != null: if plugin.find("autumn_") != null: name = plugin[ 7:-3] # Remove "autumn_" and ".py" from the filename command = self.prefix + name loader = utils.Loader(self, name, null, null) module = self.commands.get(command, null) if module != null: # Reload the module try: filename = Path("./plugins/autumn_" + name + ".py") checksum = utils.get_checksum(filename) if checksum != module.checksum: module.checksum = checksum module = import_module("plugins.autumn_" + name) loader.reload_plugin(module) if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message( "Plugin reloaded - " + plugin, messages.MessageType.NOTICE) except: utils.removekey(self.commands, command) if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message( plugin + " has been unloaded", messages.MessageType.NOTICE) else: try: # Make a PluginInfo and toss it into the commands dictionary. checksum = utils.get_checksum("./plugins/autumn_" + name + ".py") info = PluginInfo(name, checksum, loader, null, null) self.commands[command] = info if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message( "Plugin loaded - " + plugin, messages.MessageType.NOTICE) messages.print_message("Checksum: " + checksum) except Exception as error: messages.print_message( plugin + " could not be loaded.", messages.MessageType.WARNING)
def start(self): try: if logging.Logger.verbosity_level >= logging.Verbosity.DEFAULT: messages.print_message( "Running '" + self.plugin + "' for %s" % self.sender, messages.MessageType.NOTICE) self.start_plugin(self.plugin, self.args) except Exception as exception: if self.channel is not None: messages.send_message( self.bot, "That plugin cannot be activated at the moment.", self.channel) else: messages.send_message( self.bot, "That plugin cannot be activated at the moment.", self.sender)
def get_checksum(filename, block=2**20): import hashlib hashing = hashlib.md5() try: file = open(filename, 'rb') while True: data = file.read(block) if not data: break hashing.update(data) except IOError: if logging.Logger.verbosity_level >= logging.Verbosity.DEBUG: messages.print_message("File \'" + filename + "\' not found!", messages.MessageType.NONE, False) return None except Exception as error: return None return hashing.hexdigest()
def process_command(bundle): # Check if we have an acceptable nick. if len(bundle.sender) < 32: # Parse our command. args = bundle.contents.split(" ") c = args[0] # Restart the bot if the master says so. if c.lower() == "!!restart": if bundle.sender == bundle.bot.bot_master: utils.restart_program() # Continue loading the plugin. command = bundle.bot.commands.get(c, null) if command != null: if isinstance(command, str): # Reply to the user or channel if bundle.channel is not None: messages.send_message(bundle.bot, command, bundle.channel) else: messages.send_message(bundle.bot, command, bundle.sender) else: # Create a new loader instance and run the plugin with args. try: name = args[0].strip(bundle.bot.prefix).lower() args[0] = bundle.sender plugin = utils.Loader(bundle.bot, name, bundle.sender, bundle.channel, args) command.loader = plugin command.sender = bundle.sender command.channel = bundle.channel plugin.start() except Exception as error: messages.send_message(bundle.bot, str(error), bundle.sender) if logging.Logger.verbosity_level > logging.Logger.NONE: messages.print_message(str(error), messages.MessageType.EXCEPTION, True)
async def process(time=1): if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message("Connecting to server...") commands.join_server(bot) bot.connected = True while bot.connected: try: # Obtain a message from the server/channel. message = irc.recv(buffer).decode("UTF-8") message = message.strip('\n\r') messages.process_message(bot, message) await asyncio.sleep(time) except Exception as error: # Typically when someone sends an invalid character code. messages.print_message(error, messages.MessageType.EXCEPTION) messages.print_message("Adiós, mi amor. 🌸")
def join_channel(bot, channel): message = null messages.print_message("Joining " + channel + "...", messages.MessageType.GENERAL, True) bot.irc.send(utils.encode("JOIN " + channel + "\n")) while message.find("End of /NAMES list.") == null: message = bot.irc.recv(bot.buffer).decode("UTF-8") message = message.strip('\n\r') if message.find("PING :") != null: if logging.Logger.verbosity_level >= logging.Verbosity.DEBUG: messages.print_message(message, messages.MessageType.NOTICE, True) pong(message) else: messages.print_message(message, messages.MessageType.NOTICE, True)
def print_greeting(): bold = messages.Colors.bold reset = messages.Colors.reset messages.print_message("===================================", NONE, False) messages.print_message("🌙 Welcome to: Autumn! 🌙", NONE, False) messages.print_message("===================================", NONE, False) messages.print_message("⇢ " + bold + "Author" + reset + ": Jason Drawdy", NONE, False) messages.print_message("⇢ " + bold + "Date" + reset + ": 5.3.20", NONE, False) messages.print_message("⇢ " + bold + "Version" + reset + ": 2.0.0 (Mangekyo) 🎃", NONE, False) messages.print_message("===================================", NONE, False)
def start_bot(): if logging.Logger.verbosity_level > logging.Verbosity.DEFAULT: messages.print_message("Loading internal commands...") for command, obj in bot.commands.items(): messages.print_message(("Command: %s" % command), messages.MessageType.NONE, False) start_processor()
def start_resolving(): if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message("Loading plugins...") sentinel = Sentinel(bot, "plugins") asyncio.run(sentinel.refresh())
def start_processing(): if logging.Logger.verbosity_level > logging.Verbosity.TRACE: messages.print_message("Processing messages...") sentinel = Sentinel(bot) asyncio.run(sentinel.process())
def pong(bot, ping): reply = ping.split(':')[1] bot.irc.send(utils.encode("PONG :" + reply + "\n")) if logging.Logger.verbosity_level >= logging.Verbosity.DEBUG: messages.print_message("PONG :" + reply, messages.MessageType.GENERAL, True)