IRC-Bot

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

commit ba6baca59059fc1f41913e8b80782f4e30d2c9fa
parent eac6182ece021138e7cdec4f8084ba56a8dac5ac
Author: Ne02ptzero <l.solofrizzo@gmail.com>
Date:   Tue, 18 Nov 2014 12:40:24 +0100

Base: Done :)

Diffstat:
Mmodules/hi/hi.py | 46++++++++++++++++++++++++++++++++++++++++------
Mmorphux.py | 55++++++++++++++++++++++++++++++++++++++++++++-----------
2 files changed, 84 insertions(+), 17 deletions(-)

diff --git a/modules/hi/hi.py b/modules/hi/hi.py @@ -1,18 +1,39 @@ class Hi: def command(self): - config = { + self.config = { "command": { - "hi": self.hiCommand, - "cake": self.someCake + "hi": { + "function": self.hiCommand, + "usage": "hi", + "help": "Say hi to the bot" + }, + "cake": { + "function": self.someCake, + "usage": "cake <user>", + "help": "Give a piece of cake to someone !" + } }, "onJoin": { 'sayHello' :self.sayHello }, - "onLeave": self.sayGoodbye, - + "onLeave": { + 'sayGoodbye': self.sayGoodbye + }, + "onNickChange": { + 'newNick': self.newNick + }, + "onQuit": { + 'quit': self.quit + }, + "before": { + 'before': self.before + }, + "after": { + 'after': self.after + } } - return config + return self.config def hiCommand(self, Morphux, infos): Morphux.sendMessage("Hello !", infos['nick']) @@ -30,3 +51,16 @@ class Hi: def sayGoodbye(self, Morphux, user): Morphux.sendMessage(user + " is dead...") + + def newNick(self, Morphux, oldNick, newNick): + Morphux.sendMessage(oldNick + " change to " + newNick) + + def quit(self, Morphux, nick): + Morphux.sendMessage(nick + " quit !") + + def before(self, Morphux, line): + #Morphux.sendMessage("Before: " + line) + return 1; + + def after(self, Morphux, line): + print "after YAY " + line diff --git a/morphux.py b/morphux.py @@ -15,7 +15,12 @@ class Morphux: fd = open(path) self.config = json.load(fd) self.currentUsers = {} - self.onJoin = {} + self.join = {} + self.leave = {} + self.quit = {} + self.nickChange = {} + self.before = {} + self.after = {} # Connect the bot to the server and the chan def connect(self): @@ -29,25 +34,32 @@ class Morphux: def loop(self): while 1: line = self.s.getLine() + before = 1 print(line); # Treat Line self.getHeadersLine(line) if ("JOIN" in line): - self.join(line) + self.onJoin(line) elif ("PART" in line): self.onLeave(line) elif ("QUIT" in line): self.onQuit(line) elif ("NICK" in line): - self.nickChange(line) + self.onNickChange(line) elif ("PRIVMSG" in line): infos = self.getInfo(line) - if (infos != False): + for name, function in self.before.items(): + if (function(self, line) == 0): + before = 0 + if (before == 0): + self.s.CorePrint("Aborting treatement"); + elif (infos != False): if (infos["command"] in self.commands): - self.commands[infos["command"]](self, infos) + self.commands[infos["command"]]["function"](self, infos) else: self.sendMessage(self.config["errorMessage"], infos["nick"]) - pprint(self.currentUsers) + for name, function in self.after.items(): + function(self, line) # Send message # @param: string @@ -104,26 +116,40 @@ class Morphux: commands = result["command"] if ("onJoin" in result): for name, function in result["onJoin"].items(): - self.onJoin[name] = function + self.join[name] = function + if ("onLeave" in result): + for name, function in result["onLeave"].items(): + self.leave[name] = function + if ("onQuit" in result): + for name, function in result["onQuit"].items(): + self.quit[name] = function + if ("onNickChange" in result): + for name, function in result["onNickChange"].items(): + self.nickChange[name] = function + if ("before" in result): + for name, function in result["before"].items(): + self.before[name] = function + if ("after" in result): + for name, function in result["after"].items(): + self.after[name] = function for name, function in commands.items(): commands[name] = function self.s.printOk("OK") self.commands = commands - pprint(self.onJoin) # On User Join # @param: string - def join(self, line): + def onJoin(self, line): user = line.split(" ") user[0] = user[0][1:] self.currentUsers[user[0].split("!")[0]] = True - for name, function in self.onJoin.items(): + for name, function in self.join.items(): function(self, user[0].split("!")[0]) # On Nick Change # @param: string - def nickChange(self, line): + def onNickChange(self, line): user = line.split(" ") user[0] = user[0][1:] userName = user[0].split("!") @@ -131,6 +157,9 @@ class Morphux: if (userName[0] in self.currentUsers): del self.currentUsers[userName[0]] self.currentUsers[newNick] = True + for name, function in self.nickChange.items(): + function(self, userName[0], newNick) + # Get Initial list of Users # @param: string @@ -154,6 +183,8 @@ class Morphux: nickName = user.split("!")[0] if (nickName in self.currentUsers): del self.currentUsers[nickName] + for name, function in self.leave.items(): + function(self, nickName) # On User QUIT # @param: string @@ -162,6 +193,8 @@ class Morphux: nickName = user.split("!")[0] if (nickName in self.currentUsers): del self.currentUsers[nickName] + for name, function in self.quit.items(): + function(self, nickName) # If User is connected # @param: string