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-06-05 12:18:13 +00:00
|
|
|
def _event(self, type, server, line, context, minimal=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-06-21 09:32:07 +00:00
|
|
|
minimal=minimal, **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-03-05 09:30:36 +00:00
|
|
|
def _privmsg(self, event, channel, user, nickname):
|
2019-03-05 09:19:06 +00:00
|
|
|
symbols = ""
|
|
|
|
if channel:
|
|
|
|
symbols = self._mode_symbols(user, channel, event["server"])
|
|
|
|
|
|
|
|
if event["action"]:
|
|
|
|
return "* %s%s %s" % (symbols, nickname, event["message"])
|
|
|
|
else:
|
|
|
|
return "<%s%s> %s" % (symbols, nickname, event["message"])
|
|
|
|
|
|
|
|
@utils.hook("send.message.channel")
|
|
|
|
@utils.hook("received.message.channel")
|
|
|
|
def channel_message(self, event):
|
2019-03-05 09:30:36 +00:00
|
|
|
nickname = None
|
|
|
|
user = None
|
|
|
|
if "user" in event and event["user"]:
|
|
|
|
user = event["user"]
|
2019-03-05 09:19:06 +00:00
|
|
|
nickname = event["user"].nickname
|
2019-03-05 09:30:36 +00:00
|
|
|
else:
|
|
|
|
nickname = event["server"].nickname
|
|
|
|
user = event["server"].get_user(nickname)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-03-05 09:30:36 +00:00
|
|
|
line = self._privmsg(event, event["channel"], user, nickname)
|
2019-03-05 09:19:06 +00:00
|
|
|
self._event("message.channel", event["server"], line,
|
2019-06-21 09:32:07 +00:00
|
|
|
event["channel"].name, channel=event["channel"], user=user,
|
2019-06-21 19:20:28 +00:00
|
|
|
parsed_line=event["line"])
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-06-20 16:58:02 +00:00
|
|
|
def _on_notice(self, event, sender):
|
|
|
|
return "(notice) <%s> %s" % (sender, event["message"])
|
2019-03-05 09:52:38 +00:00
|
|
|
def _channel_notice(self, event, sender, channel):
|
2019-06-20 16:58:02 +00:00
|
|
|
line = self._on_notice(event, sender)
|
2019-06-20 16:08:33 +00:00
|
|
|
self._event("notice.channel", event["server"], line,
|
2019-07-02 07:48:18 +00:00
|
|
|
event["channel"].name, parsed_line=event["line"], channel=channel,
|
|
|
|
user=event["user"])
|
2019-06-21 09:32:07 +00:00
|
|
|
|
2019-03-05 09:52:38 +00:00
|
|
|
def _private_notice(self, event, sender, target):
|
2019-06-20 16:58:02 +00:00
|
|
|
line = self._on_notice(event, sender)
|
2019-06-21 09:32:07 +00:00
|
|
|
self._event("notice.private", event["server"], line, None,
|
2019-07-02 07:48:18 +00:00
|
|
|
parsed_line=event["line"], user=event["user"])
|
2019-03-05 09:52:38 +00:00
|
|
|
|
2019-07-01 18:54:00 +00:00
|
|
|
@utils.hook("received.notice.channel")
|
2019-03-05 09:19:06 +00:00
|
|
|
def channel_notice(self, event):
|
2019-03-05 09:52:38 +00:00
|
|
|
self._channel_notice(event, event["user"].nickname, event["channel"])
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("send.notice.channel")
|
|
|
|
def self_notice_channel(self, event):
|
2019-03-05 09:52:38 +00:00
|
|
|
self._channel_notice(event, event["server"].nickname, event["channel"])
|
2019-07-01 18:54:00 +00:00
|
|
|
@utils.hook("received.notice.private")
|
2019-03-05 09:19:06 +00:00
|
|
|
def private_notice(self, event):
|
2019-03-05 09:52:38 +00:00
|
|
|
self._private_notice(event, event["user"].nickname,
|
|
|
|
event["server"].nickname)
|
2019-03-05 09:19:06 +00:00
|
|
|
@utils.hook("send.notice.private")
|
|
|
|
def self_private_notice(self, event):
|
2019-03-05 09:52:38 +00:00
|
|
|
self._private_notice(event, event["server"].nickname,
|
|
|
|
event["user"].nickname)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
def _on_join(self, event, user):
|
2019-03-12 10:41:45 +00:00
|
|
|
line = "- %s joined %s" % (user.hostmask(), event["channel"].name)
|
2019-06-05 12:18:13 +00:00
|
|
|
minimal_line = "%s joined %s" % (user.nickname, event["channel"].name)
|
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
self._event("join", event["server"], line, event["channel"].name,
|
2019-06-05 12:18:13 +00:00
|
|
|
channel=event["channel"], user=user, minimal=minimal_line)
|
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-03-06 13:36:18 +00:00
|
|
|
def _on_part(self, event, user):
|
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
|
|
|
line_minimal = "%s left %s%s" % (user.nickname, event["channel"].name,
|
|
|
|
reason)
|
|
|
|
line = "- %s" % line_minimal
|
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
self._event("part", event["server"], line, event["channel"].name,
|
2019-06-05 12:18:13 +00:00
|
|
|
channel=event["channel"], user=user, minimal=line_minimal)
|
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-06-05 12:18:13 +00:00
|
|
|
line_minimal = "%s changed nickname to %s" % (
|
2019-03-05 09:19:06 +00:00
|
|
|
event["old_nickname"], event["new_nickname"])
|
2019-06-05 12:18:13 +00:00
|
|
|
line = "- %s" % line_minimal
|
|
|
|
|
|
|
|
self._event("nick", event["server"], line, None, user=user,
|
|
|
|
minimal=line_minimal)
|
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):
|
|
|
|
line = "%s invited %s to %s" % (
|
|
|
|
event["user"].nickname, event["target_user"].nickname,
|
|
|
|
event["target_channel"])
|
|
|
|
self._event("invite", event["server"], line, event["target_channel"])
|
|
|
|
|
|
|
|
@utils.hook("received.mode.channel")
|
|
|
|
def mode(self, event):
|
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-06-05 12:18:13 +00:00
|
|
|
line_minimal = "%s set mode %s%s" % (
|
2019-07-03 07:13:28 +00:00
|
|
|
event["user"].nickname, "".join(event["modes_str"]), args)
|
2019-06-05 12:18:13 +00:00
|
|
|
line = "- %s" % line_minimal
|
|
|
|
|
2019-03-05 09:19:06 +00:00
|
|
|
self._event("mode.channel", event["server"], line,
|
2019-06-05 12:18:13 +00:00
|
|
|
event["channel"].name, channel=event["channel"], user=event["user"],
|
|
|
|
minimal=line_minimal)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
def _on_topic(self, event, setter, action, topic):
|
|
|
|
line = "topic %s by %s: %s" % (action, setter, topic)
|
2019-03-06 13:36:18 +00:00
|
|
|
self._event("topic", event["server"], line, event["channel"].name,
|
|
|
|
channel=event["channel"], user=event.get("user", None))
|
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):
|
|
|
|
self._on_topic(event, event["setter"], "set",
|
2019-03-05 09:30:50 +00:00
|
|
|
event["channel"].topic)
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
unix_dt = datetime.datetime.utcfromtimestamp(event["set_at"])
|
2019-03-05 09:30:10 +00:00
|
|
|
dt = datetime.datetime.strftime(unix_dt, utils.ISO8601_PARSE)
|
2019-03-05 09:19:06 +00:00
|
|
|
line = "topic set at %s" % dt
|
|
|
|
self._event("topic-timestamp", event["server"], line,
|
2019-03-06 15:04:04 +00:00
|
|
|
event["channel"].name, channel=event["channel"])
|
2019-03-05 09:19:06 +00:00
|
|
|
|
|
|
|
def _on_kick(self, event, nickname):
|
|
|
|
reason = ""
|
|
|
|
if event["reason"]:
|
2019-04-16 06:54:07 +00:00
|
|
|
reason = " (%s)" % event["reason"]
|
2019-08-16 15:09:57 +00:00
|
|
|
line_minimal = "%s kicked %s from %s%s" % (
|
2019-03-05 09:19:06 +00:00
|
|
|
event["user"].nickname, nickname, event["channel"].name, reason)
|
2019-08-16 15:09:57 +00:00
|
|
|
line = "- %s" % line_minimal
|
|
|
|
|
2019-03-06 13:36:18 +00:00
|
|
|
self._event("kick", event["server"], line, event["channel"].name,
|
2019-08-16 15:09:57 +00:00
|
|
|
channel=event["channel"], user=event.get("user", None),
|
|
|
|
minimal=line_minimal)
|
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
|
|
|
line_minimal = "%s quit%s" % (user.nickname, reason)
|
|
|
|
line = "- %s" % line_minimal
|
|
|
|
|
|
|
|
self._event("quit", event["server"], line, None, user=user,
|
|
|
|
minimal=line_minimal)
|
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)
|