mirror of
				https://github.com/krislamo/Flea
				synced 2025-10-31 01:58:34 +00:00 
			
		
		
		
	Refactored a lot....PEP8-ifying, logical modularity, etc....
This commit is contained in:
		
							
								
								
									
										3
									
								
								Flea.py
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								Flea.py
									
									
									
									
									
								
							| @@ -18,7 +18,7 @@ import traceback | |||||||
| import sys | import sys | ||||||
|  |  | ||||||
| try: | try: | ||||||
|     import core.main |     from core.main import * | ||||||
|  |  | ||||||
| except KeyboardInterrupt: | except KeyboardInterrupt: | ||||||
|     print "\nGoodbye!" |     print "\nGoodbye!" | ||||||
| @@ -39,3 +39,4 @@ except: | |||||||
|     raw_input() |     raw_input() | ||||||
|     sys.exit(0) |     sys.exit(0) | ||||||
|  |  | ||||||
|  | main() | ||||||
|   | |||||||
| @@ -19,14 +19,14 @@ import socket | |||||||
| import re | import re | ||||||
|  |  | ||||||
| class irc: | class irc: | ||||||
|  |     # TODO: Wrap in __init__ | ||||||
|     debug = False |     debug = False | ||||||
|     log = False |     log = False | ||||||
|     config = {} |     config = {} | ||||||
|     pack = {} |     pack = {} | ||||||
|     sock = socket.socket() |     sock = socket.socket() | ||||||
|  |  | ||||||
|     # IRC Parser. Parses by line |     # IRC Parser. Parses by line Functions are lowercase, classes uppercase | ||||||
|     def Parser(self, line): |     def Parser(self, line): | ||||||
|         packet = {"nick":None, "ident":None, |         packet = {"nick":None, "ident":None, | ||||||
|               "host":None, "cmd":None, |               "host":None, "cmd":None, | ||||||
|   | |||||||
							
								
								
									
										152
									
								
								core/main.py
									
									
									
									
									
								
							
							
						
						
									
										152
									
								
								core/main.py
									
									
									
									
									
								
							| @@ -28,16 +28,14 @@ import re | |||||||
|  |  | ||||||
| # Allows reimporting modules | # Allows reimporting modules | ||||||
| class ImportRollback: | class ImportRollback: | ||||||
|     def __init__(self): |     def __init__(self, plugins_folder="/plugins"): | ||||||
|         # Dictionary of loaded modules |         # Dictionary of loaded modules | ||||||
|         self.curMods = sys.modules.copy() |         self.curMods = sys.modules.copy() | ||||||
|         self.newImport = __builtin__.__import__ |         self.newImport = __builtin__.__import__ | ||||||
|  |  | ||||||
|         # Directory of plugins |         self.plugins = os.path.join(os.getcwd(), plugins_folder) | ||||||
|         self.plugins = os.getcwd()+"/plugins/" |  | ||||||
|  |  | ||||||
|         # Add the plugins location to the path variable |         # Add the plugins location to the path variable | ||||||
|         # Helps the system find the plugin modules |  | ||||||
|         sys.path.append(self.plugins) |         sys.path.append(self.plugins) | ||||||
|  |  | ||||||
|         # Override builtin import function with install() |         # Override builtin import function with install() | ||||||
| @@ -59,94 +57,88 @@ class ImportRollback: | |||||||
|  |  | ||||||
|  |  | ||||||
| # Print and log to logfile | # Print and log to logfile | ||||||
| def prntlog(message, logfile): | def printlog(message, log=None): | ||||||
|     print message |     print message | ||||||
|     if logfile: |     if log is not None: | ||||||
|         logfile.write(message+"\n") |         log.write(message+"\n") | ||||||
|  |  | ||||||
|  |  | ||||||
| def PluginsImport(log=False): | def PluginsImport(log=None, plugins_folder="/plugins"): | ||||||
|     # Get root of Flea |     plugins = os.path.join(os.getcwd(), plugins_folder) | ||||||
|     current = os.getcwd() |  | ||||||
|     # Path to /plugins/ under /Flea/ |  | ||||||
|     plugins = current+"/plugins/" |  | ||||||
|  |  | ||||||
|     # List of plugins |  | ||||||
|     plugin_list = [] |     plugin_list = [] | ||||||
|  |  | ||||||
|     # If /plugins/ exists change directory to it |  | ||||||
|     if os.path.exists(plugins): |     if os.path.exists(plugins): | ||||||
|         os.chdir(plugins) |         os.chdir(plugins) | ||||||
|  |  | ||||||
|         # Go through every item in /plugins/ |  | ||||||
|         for item in os.listdir(plugins): |         for item in os.listdir(plugins): | ||||||
|  |             if os.path.isdir(os.path.join(plugins, item)): | ||||||
|             # Only import directory plugins (no single files) |                 printlog("[Plugins] Initializing " + item, log) | ||||||
|             if os.path.isdir(plugins+item): |  | ||||||
|                 prntlog("[Plugins] Initializing "+item, log) |  | ||||||
|                 plugin = __import__(item+".main") |                 plugin = __import__(item+".main") | ||||||
|                 plugin_list.append(plugin) |                 plugin_list.append(plugin) | ||||||
|  |  | ||||||
|     else: |     else: | ||||||
|         return False |         return None | ||||||
|  |  | ||||||
|     os.chdir(current) |     os.chdir(os.getcwd()) | ||||||
|     return plugin_list |     return plugin_list | ||||||
|  |  | ||||||
|  |  | ||||||
| def main(): | def init_connection(config_file="settings.conf"): | ||||||
|  |     irc_conn = irclib.irc() | ||||||
|  |     irc_conn.config = cfgParser(config_file) | ||||||
|  |  | ||||||
|     # Create irclib irc object |     if irc_conn.config["logging"]: | ||||||
|     irc = irclib.irc() |  | ||||||
|  |  | ||||||
|     # Parse main settings.conf file |  | ||||||
|     irc.config = cfgParser("settings.conf") |  | ||||||
|  |  | ||||||
|     # If logging is enabled, open log file. |  | ||||||
|     if irc.config["logging"]: |  | ||||||
|         log = open("log.txt", 'a') |         log = open("log.txt", 'a') | ||||||
|         irc.log = log |         irc_conn.log = log | ||||||
|     else: |     else: | ||||||
|         log = False |         log = None | ||||||
|  |  | ||||||
|  |     irc_conn.debug = irc_conn.config["debug"] | ||||||
|  |  | ||||||
|     # Keep track of modules for a rollback |     # Keep track of modules for a rollback | ||||||
|     importctrl = ImportRollback() |     importctrl = ImportRollback() | ||||||
|  |  | ||||||
|     # Import /plugins/ |     # Import /plugins | ||||||
|     if irc.config["plugins"]: |     if irc_conn.config["plugins"]: | ||||||
|         plugins = PluginsImport(log) |         plugins = PluginsImport(log) | ||||||
|  |     else: | ||||||
|  |         plugins = None | ||||||
|  |  | ||||||
|         if not plugins: |     if plugins is not None: | ||||||
|             prntlog("[Plugins] Failed to load.", log) |         # TODO: Add more debugging messages, sporadatic. | ||||||
|  |         printlog("[Plugins] Failed to load.", log) | ||||||
|  |  | ||||||
|     # Set debug to true/false inside irc() object |     # Create socket object and wrap with SSL object, then connect. | ||||||
|     irc.debug = irc.config["debug"] |     irc_conn.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||||||
|  |     irc_conn.sock = ssl.wrap_socket(irc_conn.sock) | ||||||
|  |  | ||||||
|     # Create socket object |     server = (irc_conn.config["host"], irc_conn.config["port"]) | ||||||
|     irc.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |     try: | ||||||
|  |         printlog("Connecting to " + server[0] + ':' + str(server[1])) | ||||||
|  |         irc_conn.sock.connect(server[0], server[1]) | ||||||
|  |     except: | ||||||
|  |         printlog("Connection failed.", log) | ||||||
|  |         return (None, None) | ||||||
|  |  | ||||||
|     # Wrap socket object to create SSLSocket object |     # Display SSL inforation to the user | ||||||
|     irc.sock = ssl.wrap_socket(irc.sock) |     ssl_info = irc_conn.sock.cipher() | ||||||
|  |     if ssl_info is not None: | ||||||
|  |         printlog("[SSL] Cipher: " + ssl_info[0], log) | ||||||
|  |         printlog("[SSL] Version: " + ssl_info[1], log) | ||||||
|  |         printlog("[SSL] Bits: " + str(ssl_info[2]), log) | ||||||
|  |  | ||||||
|     # Connect to IRC server |     # Establish identity on server | ||||||
|     host = irc.config["host"] |     identity = (irc_conn.config["ident"], irc_conn.config["mode"], irc_conn.config["unused"], irc_conn.config["realname"]) | ||||||
|     port = irc.config["port"] |     irc_conn.User(identity) | ||||||
|  |     irc_conn.Nick(irc_conn.config["nick"]) | ||||||
|  |  | ||||||
|     irc.sock.connect((host, port)) |     return (irc_conn, plugins) | ||||||
|     prntlog("Connecting to "+host+':'+str(port), log) |  | ||||||
|  |  | ||||||
|     # Display SSL information to the user |  | ||||||
|     ssl_info = irc.sock.cipher() |  | ||||||
|     if ssl_info != None: |  | ||||||
|         prntlog("[SSL] Cipher: "+ssl_info[0], log) |  | ||||||
|         prntlog("[SSL] Version: "+ssl_info[1], log) |  | ||||||
|         prntlog("[SSL] Bits: "+str(ssl_info[2]), log) |  | ||||||
|  |  | ||||||
|     # Send User/Nick message to establish user on the server | def client_loop(irc_conn, plugins, log=None): | ||||||
|     irc.User(irc.config["ident"], irc.config["mode"], |     if irc_conn is None: | ||||||
|             irc.config["unused"], irc.config["realname"]) |         print "No connection established." | ||||||
|  |         sys.exit(0) | ||||||
|     irc.Nick(irc.config["nick"]) |  | ||||||
|  |  | ||||||
|     while True: |     while True: | ||||||
|         # Buffer to store data from server |         # Buffer to store data from server | ||||||
| @@ -154,7 +146,7 @@ def main(): | |||||||
|  |  | ||||||
|         while True: |         while True: | ||||||
|             # Receive data from connection |             # Receive data from connection | ||||||
|             tmpdata = irc.sock.recv(4096) |             tmpdata = irc_conn.sock.recv(4096) | ||||||
|             data = data + tmpdata |             data = data + tmpdata | ||||||
|  |  | ||||||
|             if len(tmpdata) < 4096: |             if len(tmpdata) < 4096: | ||||||
| @@ -176,46 +168,50 @@ def main(): | |||||||
|             if len(line) > 0: |             if len(line) > 0: | ||||||
|  |  | ||||||
|                 # Print/log line, parse it and respond |                 # Print/log line, parse it and respond | ||||||
|                 prntlog(line, log) |                 printlog(line, log) | ||||||
|                 irc.pack = irc.Parser(line) |                 irc_conn.pack = irc_conn.Parser(line) | ||||||
|  |  | ||||||
|                 # Run all plugins main() function |                 # Run all plugins main() function | ||||||
|                 wait = '' |                 wait = '' | ||||||
|                 if irc.config["plugins"]: |                 if irc_conn.config["plugins"]: | ||||||
|                     for plugin in plugins: |                     for plugin in plugins: | ||||||
|                         wait = plugin.main.main(irc) |                         wait = plugin.main.main(irc_conn) | ||||||
|                         if wait == "QUIT": |                         if wait == "QUIT": | ||||||
|                             break |                             break | ||||||
|  |  | ||||||
|                 # Ping Pong, keep the connection alive. |                 # Ping Pong, keep the connection alive. | ||||||
|                 if irc.pack["cmd"] == "PING": |                 if irc_conn.pack["cmd"] == "PING": | ||||||
|                     irc.Pong(irc.pack["text"]) |                     irc_conn.Pong(irc_conn.pack["text"]) | ||||||
|  |  | ||||||
|                 # Send user mode message after command 001 |                 # Send user mode message after command 001 | ||||||
|                 elif irc.pack["cmd"] == "001": |                 elif irc_conn.pack["cmd"] == "001": | ||||||
|                     irc.Mode(irc.config["nick"], irc.config["mode"]) |                     irc_conn.Mode(irc_conn.config["nick"], irc_conn.config["mode"]) | ||||||
|  |  | ||||||
|                 elif irc.pack["cmd"] == "NOTICE": |                 elif irc_conn.pack["cmd"] == "NOTICE": | ||||||
|                     if irc.pack["ident"] == "NickServ": |                     if irc_conn.pack["ident"] == "NickServ": | ||||||
|                         # Send password after NickServ informs you |                         # Send password after NickServ informs you | ||||||
|                         # that your nick is registered |                         # that your nick is registered | ||||||
|                         pattern = r"[Tt]his nickname is registered" |                         pattern = r"[Tt]his nickname is registered" | ||||||
|                         if re.search(pattern, irc.pack["text"]): |                         if re.search(pattern, irc_conn.pack["text"]): | ||||||
|                             irc.Identify(irc.config["password"]) |                             irc_conn.Identify(irc_conn.config["password"]) | ||||||
|                             irc.Join(irc.config["channel"]) |                             irc_conn.Join(irc_conn.config["channel"]) | ||||||
|  |  | ||||||
|         if log: log.flush() |         if log: | ||||||
|  |             log.flush() | ||||||
|  |  | ||||||
|         # Wait for QUIT to be returned from any plugin's main() function |         # Wait for QUIT to be returned from any plugin's main() function | ||||||
|         if wait == "QUIT": |         if wait == "QUIT": | ||||||
|             # Quit, close connection and logfile. |             # Quit, close connection and logfile. | ||||||
|             irc.Quit("Fleabot https://github.com/Kris619/Flea") |             irc_conn.Quit("Fleabot https://github.com/Kris619/Flea") | ||||||
|             irc.sock.close() |             irc_conn.sock.close() | ||||||
|             if log: log.close() |             if log: | ||||||
|  |                 log.close() | ||||||
|  |  | ||||||
|             print "Press the [ENTER] key to close." |             print "Press the [ENTER] key to close." | ||||||
|             raw_input() |             raw_input() | ||||||
|             sys.exit(0) |             sys.exit(0) | ||||||
|  |  | ||||||
|  |  | ||||||
| main() | def main(): | ||||||
|  |     (irc_conn, plugins) = init_connection() | ||||||
|  |     client_loop(irc_conn, plugins) | ||||||
|   | |||||||
| @@ -2,15 +2,15 @@ debug = true | |||||||
| logging = false | logging = false | ||||||
| plugins = true | plugins = true | ||||||
|  |  | ||||||
| host = irc.example.net | host = irc.foonetic.net | ||||||
| port = 7001 | port = 7001 | ||||||
|  |  | ||||||
| nick = Flea | nick = FleaTest | ||||||
| password = your_password | password = | ||||||
| ident = Fleabot | ident = | ||||||
| mode = +B | mode = +B | ||||||
| unused = * | unused = * | ||||||
| realname = your_name | realname = your_name | ||||||
|  |  | ||||||
| control = your_nick | control = graygoose124 | ||||||
| channel = #MyChannel | channel = #freesoftware | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user