From 05b5a540ac85dbaa9392b85221d064806482cfb3 Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Sun, 20 Dec 2020 23:08:41 +0000 Subject: [PATCH 1/8] logging: don't log is opt_in_channels is empty and channel not in array Add an opt_in_channels config which blocks logging if the array is not empty and the channel is not in it. Assumes if array is empty that logging should happen everywhere. --- sopel_modules/chanlogs/__init__.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index d33e6ae..bff0a00 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -54,7 +54,7 @@ class ChanlogsSection(StaticSection): quit_template = ValidatedAttribute('quit_template', default=None) nick_template = ValidatedAttribute('nick_template', default=None) topic_template = ValidatedAttribute('topic_template', default=None) - + opt_in_channels = ListAttribute('opt_in_channels') def configure(config): config.define_section('chanlogs', ChanlogsSection, validate=False) @@ -127,7 +127,8 @@ def log_message(bot, message): # if this is a private message and we're not logging those, return early if message.sender.is_nick() and not bot.config.chanlogs.privmsg: return - + if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + return # determine which template we want, message or action if message.startswith("\001ACTION ") and message.endswith("\001"): tpl = bot.config.chanlogs.action_template or ACTION_TPL @@ -147,6 +148,8 @@ def log_message(bot, message): @sopel.module.event("JOIN") @sopel.module.unblockable def log_join(bot, trigger): + if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + return tpl = bot.config.chanlogs.join_template or JOIN_TPL logline = _format_template(tpl, bot, trigger) fpath = get_fpath(bot, trigger, channel=trigger.sender) @@ -159,6 +162,8 @@ def log_join(bot, trigger): @sopel.module.event("PART") @sopel.module.unblockable def log_part(bot, trigger): + if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + return tpl = bot.config.chanlogs.part_template or PART_TPL logline = _format_template(tpl, bot, trigger=trigger) fpath = get_fpath(bot, trigger, channel=trigger.sender) @@ -198,7 +203,8 @@ def log_nick_change(bot, trigger): privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user is present in for channel, privileges in privcopy: - if old_nick in privileges or new_nick in privileges: + if old_nick in privileges or new_nick in privileges and message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + return fpath = get_fpath(bot, trigger, channel) with bot.memory['chanlog_locks'][fpath]: with open(fpath, "ab") as f: @@ -209,6 +215,8 @@ def log_nick_change(bot, trigger): @sopel.module.event("TOPIC") @sopel.module.unblockable def log_topic(bot, trigger): + if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + return tpl = bot.config.chanlogs.topic_template or TOPIC_TPL logline = _format_template(tpl, bot, trigger) fpath = get_fpath(bot, trigger, channel=trigger.sender) From 12099b3e4e731999075fa55be69fd9106d47a083 Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Mon, 21 Dec 2020 10:18:09 +0000 Subject: [PATCH 2/8] make this more sane --- sopel_modules/chanlogs/__init__.py | 34 ++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index bff0a00..0a4ca98 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -111,6 +111,17 @@ def _format_template(tpl, bot, trigger, **kwargs): return formatted +def _channel_is_opted_in(channel, opted_in_channels): + if not opted_in_channels or str(channel) in opted_in_channels: # cast channel to string because I don't trust python + # returns true if channel should be logged. + # If opted_in_channels is empty, assume logged. + # If channel in opted_in_channels, logged + # Otherwise, don't log + return True + else: + return False + + def setup(bot): bot.config.define_section('chanlogs', ChanlogsSection) @@ -127,7 +138,7 @@ def log_message(bot, message): # if this is a private message and we're not logging those, return early if message.sender.is_nick() and not bot.config.chanlogs.privmsg: return - if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): return # determine which template we want, message or action if message.startswith("\001ACTION ") and message.endswith("\001"): @@ -148,7 +159,7 @@ def log_message(bot, message): @sopel.module.event("JOIN") @sopel.module.unblockable def log_join(bot, trigger): - if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.join_template or JOIN_TPL logline = _format_template(tpl, bot, trigger) @@ -162,7 +173,7 @@ def log_join(bot, trigger): @sopel.module.event("PART") @sopel.module.unblockable def log_part(bot, trigger): - if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.part_template or PART_TPL logline = _format_template(tpl, bot, trigger=trigger) @@ -183,12 +194,13 @@ def log_quit(bot, trigger): # make a copy of bot.privileges that we can safely iterate over privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user was present in - for channel, privileges in privcopy: - if trigger.nick in privileges: - fpath = get_fpath(bot, trigger, channel) - with bot.memory['chanlog_locks'][fpath]: - with open(fpath, "ab") as f: - f.write(logline.encode('utf8')) + if _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + for channel, privileges in privcopy: + if trigger.nick in privileges: + fpath = get_fpath(bot, trigger, channel) + with bot.memory['chanlog_locks'][fpath]: + with open(fpath, "ab") as f: + f.write(logline.encode('utf8')) @sopel.module.rule('.*') @@ -203,7 +215,7 @@ def log_nick_change(bot, trigger): privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user is present in for channel, privileges in privcopy: - if old_nick in privileges or new_nick in privileges and message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + if old_nick in privileges or new_nick in privileges and message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): return fpath = get_fpath(bot, trigger, channel) with bot.memory['chanlog_locks'][fpath]: @@ -215,7 +227,7 @@ def log_nick_change(bot, trigger): @sopel.module.event("TOPIC") @sopel.module.unblockable def log_topic(bot, trigger): - if message.sender.is_channel() and bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels: + if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.topic_template or TOPIC_TPL logline = _format_template(tpl, bot, trigger) From dc0f55ab210ae6b1d4e22365fbc6875e025339f3 Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Mon, 21 Dec 2020 10:19:08 +0000 Subject: [PATCH 3/8] Update __init__.py --- sopel_modules/chanlogs/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index 0a4ca98..67ef8ca 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -56,6 +56,7 @@ class ChanlogsSection(StaticSection): topic_template = ValidatedAttribute('topic_template', default=None) opt_in_channels = ListAttribute('opt_in_channels') + def configure(config): config.define_section('chanlogs', ChanlogsSection, validate=False) config.chanlogs.configure_setting( From 9fbcd9b7aa52526daeac9705a93c19a481e4bda9 Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Mon, 21 Dec 2020 10:22:09 +0000 Subject: [PATCH 4/8] fix --- sopel_modules/chanlogs/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index 67ef8ca..89a80f7 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -112,8 +112,8 @@ def _format_template(tpl, bot, trigger, **kwargs): return formatted -def _channel_is_opted_in(channel, opted_in_channels): - if not opted_in_channels or str(channel) in opted_in_channels: # cast channel to string because I don't trust python +def _channel_is_opted_in(is_channel, channel, opted_in_channels): + if is_channel and not opted_in_channels or str(channel) in opted_in_channels: # cast channel to string because I don't trust python # returns true if channel should be logged. # If opted_in_channels is empty, assume logged. # If channel in opted_in_channels, logged @@ -139,7 +139,7 @@ def log_message(bot, message): # if this is a private message and we're not logging those, return early if message.sender.is_nick() and not bot.config.chanlogs.privmsg: return - if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): return # determine which template we want, message or action if message.startswith("\001ACTION ") and message.endswith("\001"): @@ -160,7 +160,7 @@ def log_message(bot, message): @sopel.module.event("JOIN") @sopel.module.unblockable def log_join(bot, trigger): - if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) return tpl = bot.config.chanlogs.join_template or JOIN_TPL logline = _format_template(tpl, bot, trigger) @@ -174,7 +174,7 @@ def log_join(bot, trigger): @sopel.module.event("PART") @sopel.module.unblockable def log_part(bot, trigger): - if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) return tpl = bot.config.chanlogs.part_template or PART_TPL logline = _format_template(tpl, bot, trigger=trigger) @@ -195,7 +195,7 @@ def log_quit(bot, trigger): # make a copy of bot.privileges that we can safely iterate over privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user was present in - if _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) for channel, privileges in privcopy: if trigger.nick in privileges: fpath = get_fpath(bot, trigger, channel) @@ -216,7 +216,7 @@ def log_nick_change(bot, trigger): privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user is present in for channel, privileges in privcopy: - if old_nick in privileges or new_nick in privileges and message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if old_nick in privileges or new_nick in privileges and not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): return fpath = get_fpath(bot, trigger, channel) with bot.memory['chanlog_locks'][fpath]: @@ -228,7 +228,7 @@ def log_nick_change(bot, trigger): @sopel.module.event("TOPIC") @sopel.module.unblockable def log_topic(bot, trigger): - if message.sender.is_channel() and _channel_is_opted_in(trigger.sender, bot.config.chanlogs.opt_in_channels): + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) return tpl = bot.config.chanlogs.topic_template or TOPIC_TPL logline = _format_template(tpl, bot, trigger) From 530a9b641f0300858e10d7be03547cdcbe41278c Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Fri, 29 Jan 2021 08:42:31 +0000 Subject: [PATCH 5/8] Update sopel_modules/chanlogs/__init__.py Co-authored-by: dgw --- sopel_modules/chanlogs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index 89a80f7..25303d3 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -160,7 +160,7 @@ def log_message(bot, message): @sopel.module.event("JOIN") @sopel.module.unblockable def log_join(bot, trigger): - if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.join_template or JOIN_TPL logline = _format_template(tpl, bot, trigger) From 70897af0f3372d817a30c9a5209be6eb0a2def18 Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Fri, 29 Jan 2021 08:42:44 +0000 Subject: [PATCH 6/8] Update sopel_modules/chanlogs/__init__.py Co-authored-by: dgw --- sopel_modules/chanlogs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index 25303d3..2b8930b 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -174,7 +174,7 @@ def log_join(bot, trigger): @sopel.module.event("PART") @sopel.module.unblockable def log_part(bot, trigger): - if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.part_template or PART_TPL logline = _format_template(tpl, bot, trigger=trigger) From d9fc0a1318996158d8ac247c84a11e3571fe5bdd Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Fri, 29 Jan 2021 08:42:51 +0000 Subject: [PATCH 7/8] Update sopel_modules/chanlogs/__init__.py Co-authored-by: dgw --- sopel_modules/chanlogs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index 2b8930b..bd2097f 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -195,7 +195,7 @@ def log_quit(bot, trigger): # make a copy of bot.privileges that we can safely iterate over privcopy = list(bot.privileges.items()) # write logline to *all* channels that the user was present in - if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): for channel, privileges in privcopy: if trigger.nick in privileges: fpath = get_fpath(bot, trigger, channel) From 5519459c6012e0c4fa48f87203ecbb543b52c55b Mon Sep 17 00:00:00 2001 From: RhinosF1 Date: Fri, 29 Jan 2021 08:42:59 +0000 Subject: [PATCH 8/8] Update sopel_modules/chanlogs/__init__.py Co-authored-by: dgw --- sopel_modules/chanlogs/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sopel_modules/chanlogs/__init__.py b/sopel_modules/chanlogs/__init__.py index bd2097f..ae3a3ec 100644 --- a/sopel_modules/chanlogs/__init__.py +++ b/sopel_modules/chanlogs/__init__.py @@ -228,7 +228,7 @@ def log_nick_change(bot, trigger): @sopel.module.event("TOPIC") @sopel.module.unblockable def log_topic(bot, trigger): - if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels) + if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels): return tpl = bot.config.chanlogs.topic_template or TOPIC_TPL logline = _format_template(tpl, bot, trigger)