2018-07-15 13:14:20 +00:00
|
|
|
import datetime
|
2018-10-03 12:22:37 +00:00
|
|
|
from src import EventManager, ModuleManager, utils
|
2018-07-15 13:14:20 +00:00
|
|
|
|
2019-01-02 13:39:49 +00:00
|
|
|
DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
|
|
|
|
|
2018-09-27 10:46:10 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2018-07-23 09:20:00 +00:00
|
|
|
def print_line(self, event, line, channel=None):
|
2018-07-15 13:14:20 +00:00
|
|
|
timestamp = datetime.datetime.now().isoformat()
|
|
|
|
target = str(event["server"])
|
2018-07-23 09:20:00 +00:00
|
|
|
if not channel == None:
|
|
|
|
target += channel
|
2018-11-13 17:23:28 +00:00
|
|
|
self.bot.log.info("%s | %s", [target, utils.irc.to_ansi_colors(line)])
|
2018-07-15 13:14:20 +00:00
|
|
|
|
2018-10-03 15:44:44 +00:00
|
|
|
def _mode_symbols(self, user, channel, server):
|
|
|
|
modes = channel.get_user_status(user)
|
|
|
|
symbols = []
|
2018-10-03 16:11:01 +00:00
|
|
|
modes = list(channel.get_user_status(user))
|
|
|
|
modes.sort(key=lambda x: list(server.prefix_modes.keys()).index(x))
|
2018-10-04 03:46:32 +00:00
|
|
|
for mode in modes:
|
2018-10-03 15:44:44 +00:00
|
|
|
symbols.append(server.prefix_modes[mode])
|
|
|
|
return "".join(symbols)
|
|
|
|
|
2018-10-03 15:47:52 +00:00
|
|
|
def _on_message(self, event, user):
|
|
|
|
symbols = self._mode_symbols(user, event["channel"],
|
2018-10-03 15:44:44 +00:00
|
|
|
event["server"])
|
2018-10-01 12:48:22 +00:00
|
|
|
if event["action"]:
|
2018-10-03 15:47:52 +00:00
|
|
|
self.print_line(event, "* %s%s %s" % (symbols, user.nickname,
|
2018-10-03 15:44:44 +00:00
|
|
|
event["message"]), channel=event["channel"].name)
|
2018-10-01 12:48:22 +00:00
|
|
|
else:
|
2018-10-03 15:47:52 +00:00
|
|
|
self.print_line(event, "<%s%s> %s" % (symbols, user.nickname,
|
2018-10-03 15:44:44 +00:00
|
|
|
event["message"]), channel=event["channel"].name)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.message.channel",
|
2018-09-26 17:27:17 +00:00
|
|
|
priority=EventManager.PRIORITY_HIGH)
|
2018-07-15 13:14:20 +00:00
|
|
|
def channel_message(self, event):
|
2018-10-03 15:47:52 +00:00
|
|
|
self._on_message(event, event["user"])
|
2018-11-14 22:54:27 +00:00
|
|
|
@utils.hook("send.message.channel")
|
2018-07-15 13:14:20 +00:00
|
|
|
def self_channel_message(self, event):
|
2018-10-03 15:47:52 +00:00
|
|
|
self._on_message(event, event["server"].get_user(
|
|
|
|
event["server"].nickname))
|
2018-07-15 13:14:20 +00:00
|
|
|
|
2018-07-23 09:20:00 +00:00
|
|
|
def _on_notice(self, event, target):
|
|
|
|
self.print_line(event, "(notice->%s) <%s> %s" % (
|
|
|
|
target, event["user"].nickname, event["message"]))
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.notice.channel",
|
2018-09-26 17:27:17 +00:00
|
|
|
priority=EventManager.PRIORITY_HIGH)
|
2018-07-23 09:20:00 +00:00
|
|
|
def channel_notice(self, event):
|
|
|
|
self._on_notice(event, event["channel"].name)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.notice.private", priority=EventManager.PRIORITY_HIGH)
|
2018-07-23 09:20:00 +00:00
|
|
|
def private_notice(self, event):
|
|
|
|
self._on_notice(event, event["server"].nickname)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.server-notice", priority=EventManager.PRIORITY_HIGH)
|
2018-07-23 09:20:00 +00:00
|
|
|
def server_notice(self, event):
|
|
|
|
self.print_line(event, "(server notice) %s" % event["message"])
|
|
|
|
|
2018-07-15 13:14:20 +00:00
|
|
|
def _on_join(self, event, nickname):
|
2018-10-01 12:48:22 +00:00
|
|
|
self.print_line(event, "%s joined %s" % (nickname,
|
|
|
|
event["channel"].name))
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.join")
|
2018-07-15 13:14:20 +00:00
|
|
|
def join(self, event):
|
|
|
|
self._on_join(event, event["user"].nickname)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("self.join")
|
2018-07-15 13:14:20 +00:00
|
|
|
def self_join(self, event):
|
|
|
|
self._on_join(event, event["server"].nickname)
|
|
|
|
|
|
|
|
def _on_part(self, event, nickname):
|
2018-10-01 12:48:22 +00:00
|
|
|
self.print_line(event, "%s left %s%s" % (
|
|
|
|
nickname,
|
|
|
|
event["channel"].name,
|
|
|
|
"" if not event["reason"] else " (%s)" % event["reason"]))
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.part")
|
2018-07-15 13:14:20 +00:00
|
|
|
def part(self, event):
|
|
|
|
self._on_part(event, event["user"].nickname)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("self.part")
|
2018-07-15 13:14:20 +00:00
|
|
|
def self_part(self, event):
|
|
|
|
self._on_part(event, event["server"].nickname)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.nick")
|
|
|
|
@utils.hook("self.nick")
|
2018-07-15 13:14:20 +00:00
|
|
|
def on_nick(self, event):
|
2018-10-01 12:48:22 +00:00
|
|
|
self.print_line(event, "%s changed nickname to %s" % (
|
|
|
|
event["old_nickname"], event["new_nickname"]))
|
2018-07-15 13:14:20 +00:00
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.quit")
|
2018-07-15 13:14:20 +00:00
|
|
|
def on_quit(self, event):
|
2018-10-01 12:48:22 +00:00
|
|
|
self.print_line(event, "%s quit%s" % (event["user"].nickname,
|
|
|
|
"" if not event["reason"] else " (%s)" % event["reason"]))
|
2018-07-15 13:14:20 +00:00
|
|
|
|
|
|
|
def _on_kick(self, event, nickname):
|
2018-10-01 12:48:22 +00:00
|
|
|
self.print_line(event, "%s kicked %s from %s%s" % (
|
|
|
|
event["user"].nickname, nickname, event["channel"].name,
|
|
|
|
"" if not event["reason"] else " (%s)" % event["reason"]))
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.kick")
|
2018-07-15 13:14:20 +00:00
|
|
|
def kick(self, event):
|
|
|
|
self._on_kick(event, event["target_user"].nickname)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("self.kick")
|
2018-07-15 13:14:20 +00:00
|
|
|
def self_kick(self, event):
|
|
|
|
self._on_kick(event, event["server"].nickname)
|
2018-08-29 13:57:40 +00:00
|
|
|
|
2018-08-29 14:17:06 +00:00
|
|
|
def _on_topic(self, event, setter, action, topic, channel):
|
|
|
|
self.print_line(event, "topic %s by %s: %s" % (action, setter,
|
|
|
|
topic), channel=channel.name)
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.topic")
|
2018-08-29 13:57:40 +00:00
|
|
|
def on_topic(self, event):
|
2018-08-29 14:17:06 +00:00
|
|
|
self._on_topic(event, event["user"].nickname, "changed",
|
|
|
|
event["topic"], event["channel"])
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.numeric.333")
|
2018-08-29 14:17:06 +00:00
|
|
|
def on_333(self, event):
|
|
|
|
self._on_topic(event, event["setter"], "set",
|
|
|
|
event["channel"].topic, event["channel"])
|
2018-09-02 21:41:04 +00:00
|
|
|
|
2019-01-02 13:39:49 +00:00
|
|
|
unix_dt = datetime.datetime.utcfromtimestamp(event["set_at"])
|
|
|
|
dt = datetime.datetime.strftime(unix_dt, DATETIME_FORMAT)
|
|
|
|
self.print_line(event, "topic set at %s" % dt,
|
|
|
|
channel=event["channel"].name)
|
|
|
|
|
2018-10-03 12:22:37 +00:00
|
|
|
@utils.hook("received.mode.channel")
|
2018-09-02 21:41:04 +00:00
|
|
|
def mode(self, event):
|
|
|
|
args = " ".join(event["mode_args"])
|
|
|
|
if args:
|
|
|
|
args = " %s" % args
|
|
|
|
self.print_line(event, "%s set mode %s%s" % (
|
|
|
|
event["user"].nickname, "".join(event["modes"]),
|
|
|
|
args), channel=event["channel"].name)
|
2018-11-05 16:42:33 +00:00
|
|
|
|
|
|
|
@utils.hook("received.rename")
|
|
|
|
def rename(self, event):
|
|
|
|
self.print_line(event, "%s was renamed to %s" % (
|
|
|
|
event["old_name"], event["new_name"]))
|
2018-11-13 14:59:47 +00:00
|
|
|
|
|
|
|
@utils.hook("received.numeric.376")
|
|
|
|
def motd_end(self, event):
|
|
|
|
for line in event["server"].motd_lines:
|
|
|
|
self.print_line(event, "[MOTD] %s" % line)
|
2018-11-27 17:33:24 +00:00
|
|
|
|
|
|
|
@utils.hook("received.invite")
|
|
|
|
def invite(self, event):
|
2018-11-27 17:36:22 +00:00
|
|
|
self.print_line(event, "%s invited %s to %s" % (
|
|
|
|
event["user"].nickname, event["target_user"].nickname,
|
|
|
|
event["target_channel"]))
|