Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: don't log if opt_in_channels is empty or channel not in it #7

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
35 changes: 24 additions & 11 deletions sopel_modules/chanlogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -111,6 +112,17 @@ def _format_template(tpl, bot, trigger, **kwargs):
return formatted


def _channel_is_opted_in(is_channel, channel, opted_in_channels):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this take is_channel as an argument? Callers derive it from Trigger.sender.is_nick() anyway, so… that can be part of the function.

Also: sopel.tools.Identifier, of which Trigger.sender is an instance, doesn't have an is_channel() method. Did you…test this patch?

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

channel should be cast to sopel.tools.Identifier, not str.

Also PEP8 demands two spaces before # to start a line-end comment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't everything in the array be a string though?

# 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)

Expand All @@ -127,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 bot.config.chanlogs.opt_in_channels in not [] and trigger not in 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"):
Expand All @@ -148,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 bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels:
if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels)
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved
return
tpl = bot.config.chanlogs.join_template or JOIN_TPL
logline = _format_template(tpl, bot, trigger)
Expand All @@ -162,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 bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels:
if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels)
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved
return
tpl = bot.config.chanlogs.part_template or PART_TPL
logline = _format_template(tpl, bot, trigger=trigger)
Expand All @@ -183,12 +195,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 not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels)
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved
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('.*')
Expand All @@ -203,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 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 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]:
Expand All @@ -215,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 bot.config.chanlogs.opt_in_channels in not [] and trigger not in bot.config.chanlogs.opt_in_channels:
if not _channel_is_opted_in(message.sender.is_channel(), trigger.sender, bot.config.chanlogs.opt_in_channels)
RhinosF1 marked this conversation as resolved.
Show resolved Hide resolved
return
tpl = bot.config.chanlogs.topic_template or TOPIC_TPL
logline = _format_template(tpl, bot, trigger)
Expand Down