made commands work in channel AND in private message.

This commit is contained in:
jesopo 2016-03-29 16:22:22 +01:00
parent 78f1f68a98
commit 57a0e2f10f
No known key found for this signature in database
GPG key ID: 0BBDEB2AEFCFFCB3
5 changed files with 70 additions and 49 deletions

View file

@ -247,6 +247,7 @@ def handle_PRIVMSG(line, line_split, bot, server):
line=line, line_split=line_split, server=server, line=line, line_split=line_split, server=server,
user=user, message=message, message_split=message_split, user=user, message=message, message_split=message_split,
action=action) action=action)
user.log.add_line(user.nickname, message, action)
@handler(description="response to a WHO command for user information") @handler(description="response to a WHO command for user information")
def handle_352(line, line_split, bot, server): def handle_352(line, line_split, bot, server):
user = server.get_user(line_split[7]) user = server.get_user(line_split[7])

View file

@ -183,12 +183,14 @@ class Server(object):
self.send("QUIT :%s" % reason) self.send("QUIT :%s" % reason)
def send_message(self, target, message): def send_message(self, target, message):
self.send("PRIVMSG %s :%s" % (target, message)) self.send("PRIVMSG %s :%s" % (target, message))
if self.has_channel(target):
action = message.startswith("\01ACTION ") and message.endswith( action = message.startswith("\01ACTION ") and message.endswith(
"\01") "\01")
if action: if action:
message = message.split("\01ACTION ", 1)[1][:-1] message = message.split("\01ACTION ", 1)[1][:-1]
if self.has_channel(target):
self.get_channel(target).log.add_line(None, message, action, True) self.get_channel(target).log.add_line(None, message, action, True)
else:
self.get_user(target).log.add_line(None, message, action, True)
def send_notice(self, target, message): def send_notice(self, target, message):
self.send("NOTICE %s :%s" % (target, message)) self.send("NOTICE %s :%s" % (target, message))
def send_mode(self, target, mode=None, args=None): def send_mode(self, target, mode=None, args=None):

View file

@ -1,4 +1,5 @@
import uuid import uuid
import IRCLog
class User(object): class User(object):
def __init__(self, nickname, server, bot): def __init__(self, nickname, server, bot):
@ -12,6 +13,7 @@ class User(object):
self.id = None self.id = None
while self.id == None or self.id in server.users: while self.id == None or self.id in server.users:
self.id = uuid.uuid1().hex self.id = uuid.uuid1().hex
self.log = IRCLog.Log(bot)
def set_nickname(self, nickname): def set_nickname(self, nickname):
self.nickname = nickname self.nickname = nickname
self.nickname_lower = nickname.lower() self.nickname_lower = nickname.lower()

View file

@ -4,10 +4,10 @@ STR_CONTINUED = "(...continued) "
OUT_CUTOFF = 400 OUT_CUTOFF = 400
class ChannelOut(object): class Out(object):
def __init__(self, module_name, channel): def __init__(self, module_name, target):
self.module_name = module_name self.module_name = module_name
self.channel = channel self.target = target
self._text = "" self._text = ""
self.written = False self.written = False
def write(self, text): def write(self, text):
@ -23,16 +23,16 @@ class ChannelOut(object):
).rstrip(), STR_MORE) ).rstrip(), STR_MORE)
self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF: self._text = "%s%s" % (STR_CONTINUED, text_encoded[OUT_CUTOFF:
].decode("utf8").lstrip()) ].decode("utf8").lstrip())
self.channel.send_message(text) self.target.send_message(text)
def set_prefix(self, prefix): def set_prefix(self, prefix):
self.module_name = prefix self.module_name = prefix
def has_text(self): def has_text(self):
return bool(self._text) return bool(self._text)
class ChannelStdOut(ChannelOut): class StdOut(Out):
def prefix(self): def prefix(self):
return self.module_name return self.module_name
class ChannelStdErr(ChannelOut): class StdErr(Out):
def prefix(self): def prefix(self):
return "!%s" % self.module_name return "!%s" % self.module_name
@ -43,9 +43,11 @@ class Module(object):
self.channel_message) self.channel_message)
bot.events.on("received").on("message").on("private").hook( bot.events.on("received").on("message").on("private").hook(
self.private_message) self.private_message)
bot.events.on("received").on("command").on("help").hook(self.help) bot.events.on("received").on("command").on("help").hook(self.help,
help="Show help on commands")
bot.events.on("received").on("command").on("more").hook(self.more,
help="Get more output from the last command")
bot.events.on("new").on("user", "channel").hook(self.new) bot.events.on("new").on("user", "channel").hook(self.new)
bot.events.on("received").on("command").on("more").hook(self.more)
bot.events.on("send").on("stdout").hook(self.send_stdout) bot.events.on("send").on("stdout").hook(self.send_stdout)
bot.events.on("send").on("stderr").hook(self.send_stderr) bot.events.on("send").on("stderr").hook(self.send_stderr)
@ -64,48 +66,62 @@ class Module(object):
return self.bot.events.on("received").on("command").on(command return self.bot.events.on("received").on("command").on(command
).get_hooks()[0] ).get_hooks()[0]
def message(self, event, command):
if self.has_command(command):
hook = self.get_hook(command)
is_channel = False
if "channel" in event:
target = event["channel"]
is_channel = True
else:
target = event["user"]
if not is_channel and hook.kwargs.get("channel_only"):
return
log = target.log
log.skip_next()
module_name = hook.function.__self__._name
stdout, stderr = StdOut(module_name, target), StdErr(module_name,
target)
returns = self.bot.events.on("preprocess").on("command"
).call(hook=hook, user=event["user"], server=event["server"])
for returned in returns:
if returned:
stderr.write(returned).send()
return
args_split = list(filter(None, event["message_split"][1:]))
min_args = hook.kwargs.get("min_args")
if min_args and len(args_split) < min_args:
stderr.write("Not enough arguments (minimum: %d)" % min_args
).send()
else:
args = " ".join(args_split)
server = event["server"]
user = event["user"]
self.bot.events.on("received").on("command").on(command).call(
1, user=user, server=server, target=target, log=log,
args=args, args_split=args_split, stdout=stdout, stderr=stderr,
command=command.lower())
stdout.send()
target.last_stdout = stdout
stderr.send()
target.last_stderr = stderr
def channel_message(self, event): def channel_message(self, event):
command_prefix = event["channel"].get_setting("command_prefix", command_prefix = event["channel"].get_setting("command_prefix",
event["server"].get_setting("command_prefix", "!")) event["server"].get_setting("command_prefix", "!"))
if event["message_split"][0].startswith(command_prefix): if event["message_split"][0].startswith(command_prefix):
command = event["message_split"][0].replace( command = event["message_split"][0].replace(
command_prefix, "", 1).lower() command_prefix, "", 1).lower()
if self.has_command(command): self.message(event, command)
hook = self.get_hook(command)
module_name = hook.function.__self__._name
stdout = ChannelStdOut(module_name, event["channel"])
stderr = ChannelStdErr(module_name, event["channel"])
returns = self.bot.events.on("preprocess").on("command"
).call(hook=hook, user=event["user"], server=event[
"server"])
for returned in returns:
if returned:
event["stderr"].write(returned).send()
return
min_args = hook.kwargs.get("min_args")
# get rid of all the empty strings
args_split = list(filter(None, event["message_split"][1:]))
if min_args and len(args_split) < min_args:
ChannelStdErr("Error", event["channel"]
).write("Not enough arguments ("
"minimum: %d)" % min_args).send()
else:
args = " ".join(args_split)
self.bot.events.on("received").on("command").on(
command).call(1, user=event["user"],
channel=event["channel"], args=args,
args_split=args_split, server=event["server"
], stdout=stdout, stderr=stderr, command=command,
log=event["channel"].log, target=event["channel"])
stdout.send()
stderr.send()
if stdout.has_text():
event["channel"].last_stdout = stdout
if stderr.has_text():
event["channel"].last_stderr = stderr
event["channel"].log.skip_next()
def private_message(self, event): def private_message(self, event):
pass if event["message_split"]:
command = event["message_split"][0].lower()
self.message(event, command)
def help(self, event): def help(self, event):
if event["args"]: if event["args"]:
@ -125,7 +141,7 @@ class Module(object):
event["stdout"].write("Commands: %s" % ", ".join(help_available)) event["stdout"].write("Commands: %s" % ", ".join(help_available))
def more(self, event): def more(self, event):
if event["target"].last_stdout: if event["target"].last_stdout and event["target"].last_stdout.has_text():
event["target"].last_stdout.send() event["target"].last_stdout.send()
def send_stdout(self, event): def send_stdout(self, event):

View file

@ -13,7 +13,7 @@ class Module(object):
def translate(self, event): def translate(self, event):
phrase = event["args"] phrase = event["args"]
if not phrase: if not phrase:
phrase = event["channel"].log.get() phrase = event["log"].get()
if phrase: if phrase:
phrase = phrase.message phrase = phrase.message
if not phrase: if not phrase: