2019-05-25 20:40:06 +00:00
|
|
|
#--depends-on config
|
|
|
|
#--depends-on format_activity
|
|
|
|
|
2019-03-06 13:36:46 +00:00
|
|
|
import datetime, os.path
|
|
|
|
from src import ModuleManager, utils
|
|
|
|
|
2020-01-27 23:51:30 +00:00
|
|
|
SETTING = utils.BoolSetting("channel-log",
|
|
|
|
"Enable/disable channel logging")
|
|
|
|
|
|
|
|
@utils.export("channelset",utils.BoolSetting("log",
|
2019-06-28 22:16:05 +00:00
|
|
|
"Enable/disable channel logging"))
|
2020-01-27 23:51:30 +00:00
|
|
|
@utils.export("serverset", SETTING)
|
|
|
|
@utils.export("botset", SETTING)
|
2019-03-06 13:36:46 +00:00
|
|
|
class Module(ModuleManager.BaseModule):
|
2020-01-27 23:51:30 +00:00
|
|
|
def _enabled(self, server, channel):
|
|
|
|
return channel.get_setting("log",
|
|
|
|
server.get_setting("channel-log",
|
|
|
|
self.bot.get_setting("channel-log", False)))
|
2020-01-27 23:23:20 +00:00
|
|
|
def _file(self, server_name, channel_name):
|
2020-01-30 17:12:57 +00:00
|
|
|
# if a channel name has os.path.sep (e.g. "/") in it, the channel's log
|
|
|
|
# file will create a subdirectory.
|
|
|
|
#
|
|
|
|
# to avoid this, we'll replace os.path.sep with "," (0x2C) as it is
|
|
|
|
# forbidden in channel names.
|
|
|
|
sanitised_name = channel_name.replace(os.path.sep, ",")
|
|
|
|
return self.data_directory("%s/%s.log" % (server_name, sanitised_name))
|
2020-02-24 13:14:05 +00:00
|
|
|
def _write_line(self, channel, line):
|
2020-02-24 17:10:43 +00:00
|
|
|
channel._log_file.write("%s\n" % line)
|
2020-02-24 17:21:35 +00:00
|
|
|
channel._log_file.flush()
|
2020-02-24 13:14:05 +00:00
|
|
|
def _write(self, channel, filename, key, line):
|
2020-02-24 17:10:43 +00:00
|
|
|
if not hasattr(channel, "_log_file"):
|
|
|
|
channel._log_file = utils.io.open(filename, "a")
|
|
|
|
channel._log_rsa = None
|
|
|
|
channel._log_aes = None
|
2020-02-24 13:14:05 +00:00
|
|
|
|
2020-02-24 17:10:43 +00:00
|
|
|
if key and not key == channel._log_rsa:
|
2020-02-24 13:14:05 +00:00
|
|
|
aes_key = utils.security.aes_key()
|
2020-02-24 17:10:43 +00:00
|
|
|
channel._log_rsa = key
|
|
|
|
channel._log_aes = aes_key
|
2020-02-24 13:14:05 +00:00
|
|
|
|
|
|
|
aes_key_line = utils.security.rsa_encrypt(key, aes_key)
|
|
|
|
self._write_line(channel, "\x03%s" % aes_key_line)
|
|
|
|
|
2020-02-24 17:10:43 +00:00
|
|
|
if not channel._log_aes == None:
|
2020-02-24 13:14:05 +00:00
|
|
|
line = "\x04%s" % utils.security.aes_encrypt(
|
2020-02-24 17:10:43 +00:00
|
|
|
channel._log_aes, line)
|
2020-02-24 13:14:05 +00:00
|
|
|
self._write_line(channel, line)
|
|
|
|
|
2020-01-27 23:51:30 +00:00
|
|
|
def _log(self, server, channel, line):
|
|
|
|
if self._enabled(server, channel):
|
2020-02-24 13:14:05 +00:00
|
|
|
filename = self._file(str(server), str(channel))
|
2020-02-20 14:56:04 +00:00
|
|
|
timestamp = utils.datetime.format.datetime_human(
|
|
|
|
datetime.datetime.now())
|
|
|
|
log_line = "%s %s" % (timestamp, line)
|
2020-02-24 13:14:05 +00:00
|
|
|
self._write(channel, filename, self.bot.config.get("log-key"),
|
|
|
|
log_line)
|
2019-03-06 13:36:46 +00:00
|
|
|
|
|
|
|
@utils.hook("formatted.message.channel")
|
|
|
|
@utils.hook("formatted.notice.channel")
|
|
|
|
@utils.hook("formatted.join")
|
|
|
|
@utils.hook("formatted.part")
|
|
|
|
@utils.hook("formatted.nick")
|
|
|
|
@utils.hook("formatted.invite")
|
|
|
|
@utils.hook("formatted.mode.channel")
|
|
|
|
@utils.hook("formatted.topic")
|
|
|
|
@utils.hook("formatted.topic-timestamp")
|
|
|
|
@utils.hook("formatted.kick")
|
|
|
|
@utils.hook("formatted.quit")
|
|
|
|
@utils.hook("formatted.rename")
|
2019-10-31 15:36:22 +00:00
|
|
|
@utils.hook("formatted.chghost")
|
2020-02-11 11:37:58 +00:00
|
|
|
@utils.hook("formatted.account")
|
2019-03-06 13:36:46 +00:00
|
|
|
def on_formatted(self, event):
|
|
|
|
if event["channel"]:
|
2020-01-27 23:51:30 +00:00
|
|
|
self._log(event["server"], event["channel"], event["line"])
|
2019-03-06 13:36:46 +00:00
|
|
|
elif event["user"]:
|
|
|
|
for channel in event["user"].channels:
|
2020-01-27 23:51:30 +00:00
|
|
|
self._log(event["server"], channel, event["line"])
|
|
|
|
|