2020-10-09 23:37:35 +02:00
|
|
|
import re
|
|
|
|
|
|
|
|
from edmond.plugin import Plugin
|
|
|
|
|
|
|
|
|
|
|
|
class NotesPlugin(Plugin):
|
|
|
|
|
2020-10-12 19:19:50 +02:00
|
|
|
REQUIRED_CONFIGS = [
|
|
|
|
"commands", "content_regex", "confirmation", "deliver_format", "limit",
|
|
|
|
"too_many_notes"
|
|
|
|
]
|
2020-10-09 23:37:35 +02:00
|
|
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
super().__init__(bot)
|
|
|
|
self._content_re = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def content_re(self):
|
|
|
|
if self._content_re is None:
|
|
|
|
self._content_re = re.compile(self.config["content_regex"])
|
|
|
|
return self._content_re
|
|
|
|
|
2020-10-12 15:45:31 +02:00
|
|
|
def on_join(self, event):
|
|
|
|
nick = event.source.nick
|
|
|
|
if nick == self.bot.nick:
|
|
|
|
return
|
2020-10-12 19:26:06 +02:00
|
|
|
self.deliver_notes(event.target, nick)
|
2020-10-12 15:45:31 +02:00
|
|
|
|
2020-10-09 23:37:35 +02:00
|
|
|
def on_pubmsg(self, event):
|
|
|
|
if not self.should_handle_command(event.arguments[0], no_content=True):
|
|
|
|
return False
|
|
|
|
|
|
|
|
# "note down" command.
|
2020-11-02 17:41:47 +01:00
|
|
|
if self.command.ident == self.config["commands"][0]:
|
|
|
|
content = self.command.raw[len(self.command.match):].strip()
|
2020-10-09 23:37:35 +02:00
|
|
|
match = self.content_re.match(content)
|
|
|
|
if not match:
|
|
|
|
return False
|
|
|
|
groups = match.groupdict()
|
|
|
|
if any(k not in groups for k in ("target", "note")):
|
|
|
|
return False
|
|
|
|
target = groups["target"]
|
2020-10-12 19:19:50 +02:00
|
|
|
|
|
|
|
# Check we did not reach our note limits.
|
|
|
|
if self.count_user_notes(target) >= self.config["limit"]:
|
|
|
|
self.bot.say(event.target, self.config["too_many_notes"])
|
|
|
|
return True
|
|
|
|
|
2020-10-09 23:37:35 +02:00
|
|
|
message = groups["note"]
|
|
|
|
self.bot.log_d(f"Noting for {target}: {message}")
|
|
|
|
note = {
|
|
|
|
"sender": event.source.nick,
|
|
|
|
"dest": target,
|
|
|
|
"message": message
|
|
|
|
}
|
|
|
|
self.append_storage_list_value("notes", note)
|
2020-10-12 19:09:42 +02:00
|
|
|
self.bot.say(event.target, self.config["confirmation"])
|
2020-10-12 19:26:06 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
# "deliver notes for me" command.
|
2020-11-02 17:41:47 +01:00
|
|
|
if self.command.ident == self.config["commands"][1]:
|
2020-10-12 19:26:06 +02:00
|
|
|
self.deliver_notes(event.target, event.source.nick)
|
|
|
|
return True
|
|
|
|
|
2020-11-02 13:30:41 +01:00
|
|
|
return False
|
|
|
|
|
2020-10-12 19:26:06 +02:00
|
|
|
def deliver_notes(self, target, nick):
|
|
|
|
"""Deliver all notes for this user in the target channel/user."""
|
|
|
|
notes = self.get_storage_value("notes", [])
|
|
|
|
notes = list(filter(lambda n: n["dest"] == nick, notes))
|
|
|
|
self.bot.log_d(f"Delivering {len(notes)} notes to {nick}.")
|
|
|
|
for note in notes:
|
|
|
|
message = self.config["deliver_format"].format(
|
|
|
|
sender=note["sender"],
|
|
|
|
dest=nick,
|
|
|
|
message=note["message"],
|
|
|
|
)
|
|
|
|
self.bot.say(target, message)
|
|
|
|
self.remove_storage_list_value("notes", note)
|
2020-10-12 19:19:50 +02:00
|
|
|
|
|
|
|
def count_user_notes(self, nick):
|
|
|
|
"""Count the number of undelivered notes this user has."""
|
|
|
|
notes = self.get_storage_value("notes", [])
|
|
|
|
return sum(note["dest"] == nick for note in notes)
|