2019-03-05 09:29:54 +00:00
|
|
|
import datetime
|
2019-03-05 09:19:06 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
|
|
|
|
|
|
|
class Module(ModuleManager.BaseModule):
|
2019-11-14 11:48:51 +00:00
|
|
|
def _color(self, nickname):
|
|
|
|
return utils.irc.hash_colorize(nickname)
|
2019-11-11 12:20:27 +00:00
|
|
|
|
2019-11-14 11:48:51 +00:00
|
|
|
def _event(self, type, server, line, context, minimal=None, pretty=None,
|
2019-06-21 09:32:07 +00:00
|
|
|
channel=None, user=None, **kwargs):
|
2019-03-06 08:08:20 +00:00
|
|
|
self.events.on("formatted").on(type).call(server=server,
|
2019-06-05 12:18:13 +00:00
|
|
|
context=context, line=line, channel=channel, user=user,
|
2019-11-14 11:48:51 +00:00
|
|
|
minimal=minimal, pretty=pretty, **kwargs)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
def _mode_symbols(self, user, channel, server):
|
2019-09-12 21:39:21 +00:00
|
|
|
modes = list(channel.get_user_modes(user))
|
2019-06-23 09:44:45 +00:00
|
|
|
if modes:
|
2019-09-12 21:39:21 +00:00
|
|
|
modes = [mode for mode in modes if mode in server.prefix_modes]
|
2019-06-23 09:44:45 +00:00
|
|
|
modes.sort(key=lambda x: list(server.prefix_modes.keys()).index(x))
|
|
|
|
return server.prefix_modes[modes[0]]
|
|
|
|
return ""
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-11-15 15:00:46 +00:00
|
|
|
def _privmsg(self, event, channel, user):
|
2019-03-05 09:19:06 +00:00
|
|
|
symbols = ""
|
|
|
|
if channel:
|
|
|
|
symbols = self._mode_symbols(user, channel, event["server"])
|
|
|
|
|
|
|
|
if event["action"]:
|
2019-11-14 11:48:51 +00:00
|
|
|
format = "* %s%s %s"
|
2019-03-05 09:19:06 +00:00
|
|
|
else:
|
2019-11-14 11:48:51 +00:00
|
|
|
format = "<%s%s> %s"
|
|
|
|
|
2019-11-15 15:00:46 +00:00
|
|
|
minimal = format % ("", user.nickname, event["message"])
|
|
|
|
normal = format % (symbols, user.nickname, event["message"])
|
|
|
|
pretty = format % (symbols, self._color(user.nickname),
|
2019-11-14 11:48:51 +00:00
|
|
|
event["message"])
|
|
|
|
|
|
|
|
return minimal, normal, pretty
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
@utils.hook("send.message.channel")
|
|
|
|
@utils.hook("received.message.channel")
|
|
|
|
def channel_message(self, event):
|
2019-11-15 15:00:46 +00:00
|
|
|
minimal, normal, pretty = self._privmsg(event, event["channel"],
|
|
|
|
event["user"])
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("message.channel", event["server"], normal,
|
2019-11-15 15:00:46 +00:00
|
|
|
event["channel"].name, channel=event["channel"], user=event["user"],
|
2019-11-14 11:48:51 +00:00
|
|
|
parsed_line=event["line"], minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-11-15 15:00:46 +00:00
|
|
|
def _on_notice(self, event, user, channel):
|
|
|
|
symbols = ""
|
|
|
|
if channel:
|
|
|
|
symbols = self._mode_symbols(user, channel, event["server"])
|
|
|
|
|
|
|
|
format = "-%s%s- %s"
|
|
|
|
minimal = format % ("", user.nickname, event["message"])
|
|
|
|
normal = format % (symbols, user.nickname, event["message"])
|
|
|
|
pretty = format % (symbols, self._color(user.nickname),
|
|
|
|
event["message"])
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
return minimal, normal, pretty
|
2019-11-15 15:00:46 +00:00
|
|
|
def _channel_notice(self, event, user, channel):
|
|
|
|
minimal, normal, pretty = self._on_notice(event, user, channel)
|
2019-11-14 11:48:51 +00:00
|
|
|
self._event("notice.channel", event["server"], normal,
|
2019-07-02 07:48:18 +00:00
|
|
|
event["channel"].name, parsed_line=event["line"], channel=channel,
|
2019-11-14 11:56:57 +00:00
|
|
|
user=event["user"], minimal=minimal, pretty=pretty)
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2019-07-01 18:54:00 +00:00
|
|
|
@utils.hook("received.notice.channel")
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("send.notice.channel")
|
2019-11-15 15:00:46 +00:00
|
|
|
def channel_notice(self, event):
|
|
|
|
self._channel_notice(event, event["user"], event["channel"])
|
|
|
|
|
2019-07-01 18:54:00 +00:00
|
|
|
@utils.hook("received.notice.private")
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("send.notice.private")
|
2019-11-15 15:00:46 +00:00
|
|
|
def private_notice(self, event):
|
2019-11-19 17:23:23 +00:00
|
|
|
minimal, normal, pretty = self._on_notice(event, event["user"], None)
|
|
|
|
self._event("notice.private", event["server"], normal,
|
|
|
|
event["target"].nickname, parsed_line=event["line"],
|
|
|
|
user=event["user"], minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
def _on_join(self, event, user):
|
2019-11-14 11:48:51 +00:00
|
|
|
channel_name = event["channel"].name
|
|
|
|
|
|
|
|
minimal = "%s joined %s" % (user.nickname, channel_name)
|
2019-11-13 11:19:28 +00:00
|
|
|
|
2019-11-14 11:48:51 +00:00
|
|
|
normal_format = "- %s (%s) joined %s"
|
|
|
|
normal = normal_format % (user.nickname, user.userhost(), channel_name)
|
|
|
|
pretty = normal_format % (self._color(user.nickname), user.userhost(),
|
|
|
|
channel_name)
|
2019-06-05 12:18:13 +00:00
|
|
|
|
2019-11-14 11:48:51 +00:00
|
|
|
self._event("join", event["server"], normal, event["channel"].name,
|
|
|
|
channel=event["channel"], user=user, minimal=minimal,
|
|
|
|
pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("received.join")
|
|
|
|
def join(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._on_join(event, event["user"])
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("self.join")
|
|
|
|
def self_join(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._on_join(event, event["server"].get_user(event["server"].nickname))
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-10-31 15:35:53 +00:00
|
|
|
@utils.hook("received.chghost")
|
|
|
|
def _on_chghost(self, event):
|
2019-11-15 12:29:00 +00:00
|
|
|
username = event["username"]
|
|
|
|
hostname = event["hostname"]
|
|
|
|
|
|
|
|
format = "%s changed host to %s@%s"
|
|
|
|
minimal = format % (event["user"].nickname, username, hostname)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (event["user"].nickname, username, hostname)
|
|
|
|
pretty = normal_format % (self._color(event["user"].nickname), username,
|
|
|
|
hostname)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("chghost", event["server"], normal, None,
|
|
|
|
user=event["user"], minimal=minimal, pretty=pretty)
|
2019-10-31 15:35:53 +00:00
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
def _on_part(self, event, user):
|
2019-11-15 12:29:00 +00:00
|
|
|
channel_name = event["channel"].name
|
2019-03-08 17:20:57 +00:00
|
|
|
reason = event["reason"]
|
2019-04-02 15:56:15 +00:00
|
|
|
reason = "" if not reason else " (%s)" % reason
|
2019-06-05 12:18:13 +00:00
|
|
|
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "%s left %s%s"
|
|
|
|
minimal = format % (user.nickname, channel_name, reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (user.nickname, channel_name, reason)
|
|
|
|
pretty = normal_format % (self._color(user.nickname), channel_name,
|
|
|
|
reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("part", event["server"], normal, event["channel"].name,
|
|
|
|
channel=event["channel"], user=user, minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("received.part")
|
|
|
|
def part(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._on_part(event, event["user"])
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("self.part")
|
|
|
|
def self_part(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._on_part(event, event["server"].get_user(event["server"].nickname))
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
def _on_nick(self, event, user):
|
2019-11-14 11:48:51 +00:00
|
|
|
old_nickname = event["old_nickname"]
|
|
|
|
new_nickname = event["new_nickname"]
|
|
|
|
|
|
|
|
format = "%s changed nickname to %s"
|
|
|
|
minimal = format % (old_nickname, new_nickname)
|
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
|
|
|
normal = normal_format % (old_nickname, new_nickname)
|
|
|
|
pretty = normal_format % (
|
|
|
|
self._color(old_nickname), self._color(new_nickname))
|
|
|
|
|
|
|
|
self._event("nick", event["server"], normal, None, user=user,
|
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-06 13:36:18 +00:00
|
|
|
@utils.hook("received.nick")
|
|
|
|
def nick(self, event):
|
|
|
|
self._on_nick(event, event["user"])
|
|
|
|
@utils.hook("self.nick")
|
|
|
|
def self_nick(self, event):
|
|
|
|
self._on_nick(event, event["server"].get_user(event["server"].nickname))
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-07-01 18:54:00 +00:00
|
|
|
@utils.hook("received.server-notice")
|
2019-03-05 09:19:06 +00:00
|
|
|
def server_notice(self, event):
|
|
|
|
line = "(server notice) %s" % event["message"]
|
|
|
|
self._event("server-notice", event["server"], line, None)
|
|
|
|
|
|
|
|
@utils.hook("received.invite")
|
|
|
|
def invite(self, event):
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "%s invited %s to %s"
|
|
|
|
|
|
|
|
sender = event["user"].nickname
|
|
|
|
target = event["target_user"].nickname
|
|
|
|
channel_name = event["target_channel"]
|
|
|
|
|
|
|
|
minimal = format % (sender, target, channel_name)
|
2019-11-14 11:59:50 +00:00
|
|
|
normal = "- %s" % minimal
|
2019-11-15 12:29:00 +00:00
|
|
|
pretty = format % (self._color(sender), target, channel_name)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("invite", event["server"], normal, event["target_channel"],
|
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
@utils.hook("received.mode.channel")
|
|
|
|
def mode(self, event):
|
2019-11-15 12:29:00 +00:00
|
|
|
modes = "".join(event["modes_str"])
|
2019-07-03 07:13:28 +00:00
|
|
|
args = " ".join(event["args_str"])
|
2019-03-05 09:19:06 +00:00
|
|
|
if args:
|
|
|
|
args = " %s" % args
|
|
|
|
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "%s set mode %s%s"
|
|
|
|
minimal = format % (event["user"].nickname, modes, args)
|
2019-06-05 12:18:13 +00:00
|
|
|
|
2019-11-14 11:48:51 +00:00
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (event["user"].nickname, modes, args)
|
|
|
|
pretty = normal_format % (self._color(event["user"].nickname), modes,
|
|
|
|
args)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("mode.channel", event["server"], normal,
|
2019-06-05 12:18:13 +00:00
|
|
|
event["channel"].name, channel=event["channel"], user=event["user"],
|
2019-11-14 11:48:51 +00:00
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-11-11 13:07:41 +00:00
|
|
|
def _on_topic(self, event, nickname, action, topic):
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "topic %s by %s: %s"
|
|
|
|
minimal = format % (action, nickname, topic)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (action, nickname, topic)
|
|
|
|
pretty = normal_format % (action, self._color(nickname), topic)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("topic", event["server"], normal, event["channel"].name,
|
|
|
|
channel=event["channel"], user=event.get("user", None),
|
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("received.topic")
|
|
|
|
def on_topic(self, event):
|
|
|
|
self._on_topic(event, event["user"].nickname, "changed",
|
|
|
|
event["topic"])
|
|
|
|
@utils.hook("received.333")
|
|
|
|
def on_333(self, event):
|
2019-09-16 17:41:47 +00:00
|
|
|
self._on_topic(event, event["setter"].nickname, "set",
|
2019-03-05 09:30:50 +00:00
|
|
|
event["channel"].topic)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-11-15 13:59:09 +00:00
|
|
|
dt = utils.datetime.iso8601_format(
|
|
|
|
utils.datetime.datetime_timestamp(event["set_at"]))
|
2019-11-14 14:32:36 +00:00
|
|
|
|
|
|
|
minimal = "topic set at %s" % dt
|
|
|
|
normal = "- %s" % minimal
|
|
|
|
|
|
|
|
self._event("topic-timestamp", event["server"], normal,
|
|
|
|
event["channel"].name, channel=event["channel"], minimal=minimal)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-11-14 11:48:51 +00:00
|
|
|
def _on_kick(self, event, kicked_nickname):
|
|
|
|
sender_nickname = event["user"].nickname
|
2019-11-15 12:29:00 +00:00
|
|
|
channel_name = event["channel"].name
|
2019-11-11 13:07:41 +00:00
|
|
|
|
2019-03-05 09:19:06 +00:00
|
|
|
reason = ""
|
|
|
|
if event["reason"]:
|
2019-04-16 06:54:07 +00:00
|
|
|
reason = " (%s)" % event["reason"]
|
2019-08-16 15:09:57 +00:00
|
|
|
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "%s kicked %s from %s%s"
|
|
|
|
minimal = format % (sender_nickname, kicked_nickname, channel_name,
|
2019-11-14 11:48:51 +00:00
|
|
|
reason)
|
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (sender_nickname, kicked_nickname,
|
|
|
|
channel_name, reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
pretty = normal_format % (self._color(sender_nickname),
|
2019-11-15 12:29:00 +00:00
|
|
|
self._color(kicked_nickname), channel_name, reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("kick", event["server"], normal, event["channel"].name,
|
2019-08-16 15:09:57 +00:00
|
|
|
channel=event["channel"], user=event.get("user", None),
|
2019-11-14 11:48:51 +00:00
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("received.kick")
|
|
|
|
def kick(self, event):
|
|
|
|
self._on_kick(event, event["target_user"].nickname)
|
|
|
|
@utils.hook("self.kick")
|
|
|
|
def self_kick(self, event):
|
|
|
|
self._on_kick(event, event["server"].nickname)
|
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
def _quit(self, event, user, reason):
|
2019-04-02 20:17:01 +00:00
|
|
|
reason = "" if not reason else " (%s)" % reason
|
2019-06-05 12:18:13 +00:00
|
|
|
|
2019-11-15 12:29:00 +00:00
|
|
|
format = "%s quit%s"
|
|
|
|
minimal = format % (user.nickname, reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
normal_format = "- %s" % format
|
2019-11-15 12:29:00 +00:00
|
|
|
normal = normal_format % (user.nickname, reason)
|
|
|
|
pretty = normal_format % (self._color(user.nickname), reason)
|
2019-11-14 11:48:51 +00:00
|
|
|
|
|
|
|
self._event("quit", event["server"], normal, None, user=user,
|
|
|
|
minimal=minimal, pretty=pretty)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("received.quit")
|
|
|
|
def on_quit(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._quit(event, event["user"], event["reason"])
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("send.quit")
|
|
|
|
def send_quit(self, event):
|
2019-03-06 13:36:18 +00:00
|
|
|
self._quit(event, event["server"].get_user(event["server"].nickname),
|
|
|
|
event["reason"])
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
@utils.hook("received.rename")
|
|
|
|
def rename(self, event):
|
|
|
|
line = "%s was renamed to %s" % (event["old_name"], event["new_name"])
|
2019-03-06 13:39:09 +00:00
|
|
|
self._event("rename", event["server"], line, event["old_name"],
|
|
|
|
channel=event["channel"])
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
@utils.hook("received.376")
|
|
|
|
def motd_end(self, event):
|
|
|
|
for line in event["server"].motd_lines:
|
|
|
|
line = "[MOTD] %s" % line
|
|
|
|
self._event("motd", event["server"], line, None)
|