IRC-Bot

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

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

Add of callbacks onJoin, and Userlist

Diffstat:
Mconfig.json | 3++-
Mmodules/hi/hi.py | 30++++++++++++++++++++++++------
Mmorphux.py | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
3 files changed, 112 insertions(+), 13 deletions(-)

diff --git a/config.json b/config.json @@ -7,5 +7,6 @@ "password": "", "debug": 1, "identify": 0, - "welcomeMessage": "o/" + "welcomeMessage": "o/", + "errorMessage": "Duknow this command !" } diff --git a/modules/hi/hi.py b/modules/hi/hi.py @@ -1,14 +1,32 @@ class Hi: def command(self): - command = {"hi": self.hiCommand, "set": self.hiSet, "show": self.show} - return command + config = { + "command": { + "hi": self.hiCommand, + "cake": self.someCake + }, + "onJoin": { + 'sayHello' :self.sayHello + }, + "onLeave": self.sayGoodbye, + + } + return config def hiCommand(self, Morphux, infos): Morphux.sendMessage("Hello !", infos['nick']) - def hiSet(self, Morphux, infos): - self.i = infos['args'][0] + def someCake(self, Morphux, infos): + if (len(infos['args'][0]) == 0): + Morphux.sendMessage("Kay fine, i eat my cake alone.", infos['nick']); + if (Morphux.userExists(infos['args'][0])): + Morphux.sendMessage(infos['nick'] + " give u some cake :) !" ,infos['args'][0]) + else: + Morphux.sendMessage("Can't find user " + infos['args'][0], infos['nick']); + + def sayHello(self, Morphux, user): + Morphux.sendMessage("Hi :)", user) - def show (self, Morphux, infos): - Morphux.sendMessage(self.i) + def sayGoodbye(self, Morphux, user): + Morphux.sendMessage(user + " is dead...") diff --git a/morphux.py b/morphux.py @@ -4,6 +4,7 @@ # Includes import json from core.Core import Core +from pprint import pprint class Morphux: @@ -13,6 +14,8 @@ class Morphux: def __init__(self, path = "/etc/morphux/configBot.json"): fd = open(path) self.config = json.load(fd) + self.currentUsers = {} + self.onJoin = {} # Connect the bot to the server and the chan def connect(self): @@ -26,14 +29,25 @@ class Morphux: def loop(self): while 1: line = self.s.getLine() + print(line); # Treat Line - if ("PRIVMSG" in line): + self.getHeadersLine(line) + if ("JOIN" in line): + self.join(line) + elif ("PART" in line): + self.onLeave(line) + elif ("QUIT" in line): + self.onQuit(line) + elif ("NICK" in line): + self.nickChange(line) + elif ("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"]) + self.sendMessage(self.config["errorMessage"], infos["nick"]) + pprint(self.currentUsers) # Send message # @param: string @@ -80,13 +94,79 @@ class Morphux: self.modules = res self.getCommands() + # Get modules in dictionnary def getCommands(self): - result = {} + commands = {} for name, klass in self.modules.items(): self.s.CorePrint("Loading '"+ name +"' module...") klass = klass() - commands = klass.command() + result = klass.command() + commands = result["command"] + if ("onJoin" in result): + for name, function in result["onJoin"].items(): + self.onJoin[name] = function for name, function in commands.items(): - result[name] = function + commands[name] = function self.s.printOk("OK") - self.commands = result + self.commands = commands + pprint(self.onJoin) + + + # On User Join + # @param: string + def join(self, line): + user = line.split(" ") + user[0] = user[0][1:] + self.currentUsers[user[0].split("!")[0]] = True + for name, function in self.onJoin.items(): + function(self, user[0].split("!")[0]) + + # On Nick Change + # @param: string + def nickChange(self, line): + user = line.split(" ") + user[0] = user[0][1:] + userName = user[0].split("!") + newNick = user[2][1:] + if (userName[0] in self.currentUsers): + del self.currentUsers[userName[0]] + self.currentUsers[newNick] = True + + # Get Initial list of Users + # @param: string + def getHeadersLine(self, line): + users = line.split(":") + if (len(users) >= 3): + users = users[2] + details = line.split(":")[1].split(" ") + if (len(details) >= 2): + if (details[1] == "353"): + users = users.split(" ") + pprint(users) + for nickName in users: + nickName = nickName.split("\r\n")[0] + self.currentUsers[nickName] = True + + # On User Leave + # @param: string + def onLeave(self, line): + user = line.split(" ")[0][1:] + nickName = user.split("!")[0] + if (nickName in self.currentUsers): + del self.currentUsers[nickName] + + # On User QUIT + # @param: string + def onQuit(self, line): + user = line.split(" ")[0][1:] + nickName = user.split("!")[0] + if (nickName in self.currentUsers): + del self.currentUsers[nickName] + + # If User is connected + # @param: string + def userExists(self, nick): + if (nick in self.currentUsers): + return True + else: + return False