Alias module: add !alias to explain register alias, !unalias to remove alias; variables are now replaced in the Response

Variables are words starting by $. It is possible to escape a variable by prefixing the $ by a \
This commit is contained in:
Némunaire 2012-11-02 20:28:48 +01:00
parent 771e3997c4
commit 21f2af1cad
3 changed files with 87 additions and 44 deletions

10
bot.py
View File

@ -86,7 +86,7 @@ class Bot:
"""Response to CLIENTINFO CTCP message"""
return _ctcp_response(msg.sndr,
" ".join(self.ctcp_capabilities.keys()))
def _ctcp_dcc(self, srv, msg):
"""Response to DCC CTCP message"""
ip = self.srv.toIP(int(msg.cmds[3]))
@ -329,7 +329,7 @@ class Bot:
def treat_pre(self, msg):
"""Treat a message before all other treatment"""
for h, lvl, store in self.create_cache("all_pre"):
h.run(msg)
h.run(self.create_cache, msg)
self.check_rest_times(store, h)
@ -349,8 +349,8 @@ class Bot:
# First, treat simple hook
cmd_hook = self.create_cache("cmd_hook")
if msg.cmd[0] in cmd_hook:
(hks, lvl, store) = cmd_hook[msg.cmd[0]]
if msg.cmds[0] in cmd_hook:
(hks, lvl, store) = cmd_hook[msg.cmds[0]]
for h in hks:
res = h.run(msg)
if res is not None and res != False:
@ -360,7 +360,7 @@ class Bot:
# Then, treat regexp based hook
cmd_rgxp = self.create_cache("cmd_rgxp")
for hook, lvl, store in cmd_rgxp:
if hook.is_matching(msg.cmd[0], msg.channel):
if hook.is_matching(msg.cmds[0], msg.channel):
res = hook.run(msg)
if res is not None and res != False:
treated.append(res)

View File

@ -41,9 +41,8 @@ def save():
class Message:
def __init__ (self, srv, line, timestamp, private = False):
def __init__ (self, line, timestamp, private = False):
self.raw = line
self.srv = srv
self.time = timestamp
self.channel = None
self.content = b''
@ -75,14 +74,6 @@ class Message:
# Check for CTCP request
self.ctcp = len(words[3]) > 1 and (words[3][0] == 0x01 or words[3][1] == 0x01)
self.content = self.pickWords(words[3:])
# If CTCP, remove 0x01
if self.ctcp:
self.content = self.content[1:len(self.content)-1]
# Split content by words
try:
self.cmds = shlex.split(self.content.decode())
except ValueError:
self.cmds = self.content.decode().split(' ')
elif self.cmd == '353' and len(words) > 3:
for i in range(2, len(words)):
if words[i][0] == 58:
@ -107,7 +98,21 @@ class Message:
self.channel = words[2].decode()
self.content = b' '.join(words[3:])
self.decode()
self.private = private or (self.channel is not None and self.srv is not None and self.channel == self.srv.nick)
if self.cmd == 'PRIVMSG':
self.parse_content()
self.private = private
def parse_content(self):
"""Parse or reparse the message content"""
# If CTCP, remove 0x01
#if self.ctcp:
# self.content = self.content[1:len(self.content)-1]
# Split content by words
try:
self.cmds = shlex.split(self.content)
except ValueError:
self.cmds = self.content.split(' ')
def pickWords(self, words):
"""Parse last argument of a line: can be a single word or a sentence starting with :"""
@ -128,10 +133,6 @@ class Message:
#TODO: use encoding from config file
self.content = self.content.decode('utf-8', 'replace')
@property
def is_owner(self):
return self.nick == self.srv.owner
def authorize_DEPRECATED(self):
"""Is nemubot listening for the sender on this channel?"""
# TODO: deprecated

View File

@ -4,18 +4,16 @@ import re
import sys
from datetime import datetime
nemubotversion = 3.2
CONTEXT = None
nemubotversion = 3.3
def load(context):
"""Load this module"""
global CONTEXT
CONTEXT = context
from hooks import Hook
add_hook("cmd_hook", Hook(cmd_unalias, "unalias"))
add_hook("cmd_hook", Hook(cmd_alias, "alias"))
add_hook("cmd_hook", Hook(cmd_set, "set"))
add_hook("all_pre", Hook(treat_variables))
add_hook("all_pre", Hook(treat_alias))
add_hook("all_post", Hook(treat_variables))
global DATAS
if not DATAS.hasNode("aliases"):
@ -56,30 +54,74 @@ def get_variable(name, msg=None):
return ""
def cmd_set(msg):
if len (msg.cmd) > 2:
set_variable(msg.cmd[1], " ".join(msg.cmd[2:]))
res = Response(msg.sender, "Variable $%s définie." % msg.cmd[1])
if len (msg.cmds) > 2:
set_variable(msg.cmds[1], " ".join(msg.cmds[2:]))
res = Response(msg.sender, "Variable \$%s définie." % msg.cmds[1])
save()
return res
return Response(msg.sender, "!set prend au minimum deux arguments : le nom de la variable et sa valeur.")
def cmd_alias(msg):
if len (msg.cmds) > 1:
res = list()
for alias in msg.cmds[1:]:
if alias[0] == "!":
alias = alias[1:]
if alias in DATAS.getNode("aliases").index:
res.append(Response(msg.sender, "!%s correspond à %s" % (alias,
DATAS.getNode("aliases").index[alias]["origin"]),
channel=msg.channel))
else:
res.append(Response(msg.sender, "!%s n'est pas un alias" % alias,
channel=msg.channel))
return res
else:
return Response(msg.sender, "!alias prend en argument l'alias à étendre.",
channel=msg.channel)
def treat_variables(msg):
if msg.cmd[0] != "set" and re.match(".*(set|cr[ée]{2}|nouvel(le)?) alias.*", msg.content) is None:
cnt = msg.content.split(' ')
for i in range(0, len(cnt)):
res = re.match("^([^a-zA-Z0-9]*)\\$([a-zA-Z0-9]+)(.*)$", cnt[i])
if res is not None:
cnt[i] = res.group(1) + get_variable(res.group(2), msg) + res.group(3)
msg.content = " ".join(cnt)
return True
return False
def cmd_unalias(msg):
if len (msg.cmds) > 1:
res = list()
for alias in msg.cmds[1:]:
if alias in DATAS.getNode("aliases").index:
if DATAS.getNode("aliases").index[alias]["creator"] == msg.nick or msg.is_owner:
DATAS.getNode("aliases").delChild(DATAS.getNode("aliases").index[alias])
res.append(Response(msg.sender, "%s a bien été supprimé" % alias, channel=msg.channel))
else:
res.append(Response(msg.sender, "Vous n'êtes pas le createur de l'alias %s." % alias, channel=msg.channel))
else:
res.append(Response(msg.sender, "%s n'est pas un alias" % alias, channel=msg.channel))
return res
else:
return Response(msg.sender, "!unalias prend en argument l'alias à supprimer.", channel=msg.channel)
def replace_variables(cnt, msg=None):
cnt = cnt.split(' ')
for i in range(0, len(cnt)):
res = re.match("^([^a-zA-Z0-9]*)(\\\\)\\$([a-zA-Z0-9]+)(.*)$", cnt[i])
if res is not None:
if res.group(2) != "":
cnt[i] = res.group(1) + "$" + res.group(3) + res.group(4)
else:
cnt[i] = res.group(1) + get_variable(res.group(3), msg) + res.group(4)
return " ".join(cnt)
def parseanswer(msg):
if msg.cmd[0] in DATAS.getNode("aliases").index:
msg.content = msg.content.replace("!" + msg.cmd[0], DATAS.getNode("aliases").index[msg.cmd[0]]["origin"], 1)
msg.reparsemsg()
def treat_variables(res):
for i in range(0, len(res.messages)):
res.messages[i] = replace_variables(res.messages[i], res)
return True
def treat_alias(msg, hooks_cache):
if (len(msg.cmds[0]) > 0 and
msg.cmds[0][1:] in DATAS.getNode("aliases").index and
msg.cmds[0][1:] not in hooks_cache("cmd_hook")):
msg.content = msg.content.replace(msg.cmds[0],
DATAS.getNode("aliases").index[msg.cmds[0][1:]]["origin"], 1)
msg.content = replace_variables(msg.content, msg)
msg.parse_content()
return True
return False