From ec0e9a3485e5812e048386182d4ffd881aa8518b Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Tue, 2 Feb 2016 09:18:20 -0500 Subject: [PATCH] Added simple Plugin functionality Added very basic Plugin and reimporting functionality; Password is now sent after NickServ informs you that your nick is registered; Bugfix: cfgParser will now convert the string "false" to a boolean; --- core/config.py | 2 +- core/irclib.py | 12 +++---- core/main.py | 91 +++++++++++++++++++++++++++++++++++++++++++++----- settings.conf | 1 + 4 files changed, 90 insertions(+), 16 deletions(-) diff --git a/core/config.py b/core/config.py index a96f63a..d9fdeb2 100644 --- a/core/config.py +++ b/core/config.py @@ -107,7 +107,7 @@ def cfgParser(cfgFile): if settings[x].lower() == "true": settings[x] = True elif settings[x].lower() == "false": - settings[x] == False + settings[x] = False # Conversion for positive integers elif re.search("^[0-9]+$", settings[x]) != None: diff --git a/core/irclib.py b/core/irclib.py index 40cfcc5..fa6cda6 100644 --- a/core/irclib.py +++ b/core/irclib.py @@ -20,12 +20,12 @@ class irc: debug = False sock = socket.socket() - # IRC Parser. Parses by line + # IRC Parser. Parses by line def Parser(self, line): packet = {"nick":None, "ident":None, "host":None, "cmd":None, "params":[], "text":None} - + if len(line) > 0: if(line[0] == ':'): part, line = line.split(' ', 1) @@ -92,8 +92,11 @@ class irc: def Mode(self, nick, mode): self.msg("MODE "+ nick + " "+mode) + def PrivMsg(self, user, message): + self.msg("PRIVMSG "+user+" :"+message) + def Identify(self, password): - self.msg("NickServ IDENTIFY "+password) + self.PrivMsg("NickServ", "IDENTIFY "+password) def Join(self, channel): self.msg("JOIN "+channel) @@ -104,9 +107,6 @@ class irc: def Notice(self, message, user): self.msg("NOTICE "+user+" :"+message) - def PrivMsg(self, user, message): - self.msg("PRIVMSG "+user+" :"+message) - def Part(self, channel, message): self.msg("PART "+channel+" "+message) diff --git a/core/main.py b/core/main.py index ff87818..71483a4 100644 --- a/core/main.py +++ b/core/main.py @@ -18,12 +18,83 @@ from core.config import * import core.irclib as irclib # Built-in to Python 2.7 +import __builtin__ import socket import ssl +import sys +import os + + +# Allows reimporting modules +class ImportRollback: + def __init__(self): + # Dictionary of loaded modules + self.curMods = sys.modules.copy() + self.newImport = __builtin__.__import__ + + # Directory of plugins + self.plugins = os.getcwd()+"/plugins/" + + # Add the plugins location to the path variable + # Helps the system find the plugin modules + sys.path.append(self.plugins) + + # Override builtin import function with install() + __builtin__.__import__ = self.install + self.newMods = {} + + # Import modules + def install(self, mod, globals=None, locals=None, fromlist=[]): + self.newMods[mod] = 1 + return apply(self.newImport, (mod, globals, locals, fromlist)) + + # Delete modules + def reset(self): + for mod in self.newMods.keys(): + if not self.curMods.has_key(mod): + del(sys.modules[mod]) + + __builtin__.__import__ = self.newImport + + +def PluginsImport(): + # Get root of Flea + current = os.getcwd() + # Path to /plugins/ under /Flea/ + plugins = current+"/plugins/" + + # If /plugins/ exists change directory to it + if os.path.exists(plugins): + os.chdir(plugins) + + # Go through every item in /plugins/ + for item in os.listdir(plugins): + + # Only import directory plugins (no single files) + if os.path.isdir(plugins+item): + print "[Plugins] Initializing "+item + __import__(item+".main") + + else: + return False + + os.chdir(current) + return True + def main(): + + # Parse main settings.conf file config = cfgParser("settings.conf") + # Keep track of modules for a rollback + importctrl = ImportRollback() + + # Import /plugins/ + if config["plugins"]: + if not PluginsImport(): + print "[Plugins] Failed to load." + # Create irclib irc object irc = irclib.irc() @@ -64,7 +135,7 @@ def main(): if len(tmpdata) < 4096: break - + # If no incoming data exists then connection has closed if len(tmpdata) == 0: input("Connection closed.") @@ -82,7 +153,7 @@ def main(): # Print line, parse it and respond print line pack = irc.Parser(line) - + # Ping Pong, keep the connection alive. if pack["cmd"] == "PING": irc.Pong(pack["text"]) @@ -91,12 +162,14 @@ def main(): elif pack["cmd"] == "001": irc.Mode(config["nick"], config["mode"]) - # Send password after End of MOTD - elif pack["cmd"] == "376": - irc.Identify(config["password"]) - # Temp test join - irc.Join("#Flea") - + elif pack["cmd"] == "NOTICE": + if pack["ident"] == "NickServ": + # Send password after NickServ informs you + # that your nick is registered + pattern = r"[Tt]his nickname is registered" + if re.search(pattern, pack["text"]): + irc.Identify(config["password"]) + irc.Join("#Flea") + main() - diff --git a/settings.conf b/settings.conf index a6965c7..59a5f7b 100644 --- a/settings.conf +++ b/settings.conf @@ -1,4 +1,5 @@ debug = true +plugins = false host = irc.example.net port = 7001