IRC-Bot

morphux IRC bot
Log | Files | Refs | README | LICENSE | git clone https://git.ne02ptzero.me/git/IRC-Bot

commit 5b36235895edd04a5383a8c259f44885d8c31a7d
parent 6b8919280b1cb63969949c1b71229a2fa86dbf5c
Author: Ne02ptzero <l.solofrizzo@gmail.com>
Date:   Tue, 18 Nov 2014 10:38:57 +0100

Let's start !

Diffstat:
A.gitignore | 1+
Aconfig.json | 11+++++++++++
Acore/Core.py | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Acore/__init__.py | 0
Amain.py | 19+++++++++++++++++++
Amodules/__init__.py | 0
Amodules/hi/__init__.py | 0
Amodules/hi/hi.py | 14++++++++++++++
Amorphux.py | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
9 files changed, 214 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1 @@ +*.pyc diff --git a/config.json b/config.json @@ -0,0 +1,11 @@ +{ + "host": "irc.freenode.net", + "chan": "#ne02ptzero", + "port": "6667", + "symbol": "!", + "nick": "Bot2fab4u", + "password": "", + "debug": 1, + "identify": 0, + "welcomeMessage": "o/" +} diff --git a/core/Core.py b/core/Core.py @@ -0,0 +1,77 @@ +# Core functions +# By: Louis <louis@ne02ptzero.me> + +# Includes +import socket +import sys + +class Core: + + # Construct functions + # @param: config file (json.load) + def __init__(self, config): + self.config = config + self.socket = socket.socket() + self.CorePrint("Connection to " + config["host"] + " at port " + config["port"] + "..."); + try: + self.socket.connect((config["host"], int(config["port"]))) + except socket.gaierror: + self.printError("Error at connection.", 1) + self.coreSend("NICK " + config["nick"]) + self.coreSend("USER " + config["nick"] + " " + config["nick"] + " " + config["nick"] + " :Bot") + self.printOk("OK") + + # Print Core Function, without \n + # @param: string + def CorePrint(self, string): + if (self.config["debug"] == 1): + print(string), + + # Print Ok function in green color + # @param: string + def printOk(self, string): + if (self.config["debug"] == 1): + print("\033[0;32m"+ string +"\033[0m"); + + # Print Error function, in red + # @param: int (Optional) + def printError(self, string, die = False): + if (self.config["debug"] == 1): + print("\033[0;31m"+ string +"\033[0m"); + if (die != False): + sys.exit(die) + + # Send a message to server, w/ \r\n + # @param: string + def coreSend(self, string): + self.socket.send(string + "\r\n"); + + # Send a message to channel + # @param: string + def send(self, string): + self.coreSend("PRIVMSG " + self.config["chan"] + " :" + string) + + # Identify bot to the server + def identify(self): + self.coreSend("PRIVMSG NickServ :identify " + self.config["password"]) + + # Kick someone of the channel + # @param: string + # @param: string (Optional) + def kick(self, user, message = ""): + if (message == ""): + message = self.config["banMessage"] + self.coreSend("KICK " + self.config["chan"] + " " + user + " : " + message) + + # Join a channel + def join(self): + self.CorePrint("Join " + self.config["chan"]); + self.coreSend("JOIN " + self.config["chan"]) + self.printOk("OK"); + + def getLine(self): + line = self.socket.recv(4096) + # If that's a ping from server + if ("PING :" in line): + self.coreSend("PONG " + line.split(" ")[1]) + return line.strip("\r\n") diff --git a/core/__init__.py b/core/__init__.py diff --git a/main.py b/main.py @@ -0,0 +1,19 @@ +# IRC MORPHUX BOT v2 +# By: Louis <louis@ne02ptzero.me> +#18:09 @Ne02ptzero: !ascii CL4P_TP +#18:09 @CL4P_TP: _____ _ _ _ _____ _______ _____ +#18:09 @CL4P_TP: / ____| | | || | | __ \ \__ __| __ \ +#18:09 @CL4P_TP: | | | | | || |_| |__) | | | | |__) | +#18:09 @CL4P_TP: | | | | |__ _| ___/ | | | ___/ +#18:09 @CL4P_TP: | |____| |____| | | | | | | | +#18:09 @CL4P_TP: \_____|______|_| |_| |_| |_| +#18:09 @CL4P_TP: ________ +#18:09 @CL4P_TP: |________| + + +from morphux import Morphux + +main = Morphux("config.json") +main.connect() +main.loadModules() +main.loop() diff --git a/modules/__init__.py b/modules/__init__.py diff --git a/modules/hi/__init__.py b/modules/hi/__init__.py diff --git a/modules/hi/hi.py b/modules/hi/hi.py @@ -0,0 +1,14 @@ + +class Hi: + def command(self): + command = {"hi": self.hiCommand, "set": self.hiSet, "show": self.show} + return command + + def hiCommand(self, Morphux, infos): + Morphux.sendMessage("Hello !", infos['nick']) + + def hiSet(self, Morphux, infos): + self.i = infos['args'][0] + + def show (self, Morphux, infos): + Morphux.sendMessage(self.i) diff --git a/morphux.py b/morphux.py @@ -0,0 +1,92 @@ +# Irc Bot Main file +# By: Louis <louis@ne02ptzero.me> + +# Includes +import json +from core.Core import Core + + +class Morphux: + + # Construct function + # @param path of the config file (Optional) + def __init__(self, path = "/etc/morphux/configBot.json"): + fd = open(path) + self.config = json.load(fd) + + # Connect the bot to the server and the chan + def connect(self): + self.s = Core(self.config) + if (self.config["identify"] != 0): + self.s.identify() + self.s.join() + self.s.send(self.config["welcomeMessage"]) + + # Main Loop + def loop(self): + while 1: + line = self.s.getLine() + # Treat Line + if ("PRIVMSG" in line): + infos = self.getInfo(line) + if (infos != False): + if (infos["command"] in self.commands): + self.commands[infos["command"]](self, infos) + else: + self.sendMessage("Don't know this command :(", infos["nick"]) + + # Send message + # @param: string + # @param: string (Optional) + def sendMessage(self, string, user = False): + if (user != False): + self.s.send(user + ": " + string) + else: + self.s.send(string) + + # Get Line Information + # @param: string + def getInfo(self, line): + infos = {} + infos["fullLine"] = line + args = line.split(":", 2)[2] + args = args.split(" ") + args = filter(None, args) + if (args[0][0] != self.config["symbol"]): + return False + args[0] = args[0][1:] + infos["command"] = args[0] + args.remove(args[0]) + infos["args"] = args + user = line.split(":", 2)[1] + infos["fullUser"] = user.split(" ")[0] + infos["nick"] = user.split("!", 2)[0] + return infos; + + # Load Modules + # @param: path (Optional) + def loadModules(self, path = "modules"): + res = {} + import os + lst = os.listdir(path) + dir = [] + for d in lst: + s = os.path.abspath(path) + os.sep + d + if os.path.isdir(s) and os.path.exists(s + os.sep + "__init__.py"): + dir.append(d) + for d in dir: + res[d] = __import__(path + "." + d + "." + d, fromlist = ["*"]) + res[d] = getattr(res[d], d.title()) + self.modules = res + self.getCommands() + + def getCommands(self): + result = {} + for name, klass in self.modules.items(): + self.s.CorePrint("Loading '"+ name +"' module...") + klass = klass() + commands = klass.command() + for name, function in commands.items(): + result[name] = function + self.s.printOk("OK") + self.commands = result