58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
import re
|
|
|
|
from edmond.plugin import Plugin
|
|
|
|
|
|
class NotesPlugin(Plugin):
|
|
|
|
REQUIRED_CONFIGS = ["commands", "content_regex", "deliver_format"]
|
|
|
|
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
|
|
|
|
def on_join(self, event):
|
|
nick = event.source.nick
|
|
if nick == self.bot.nick:
|
|
return
|
|
notes = self.get_storage_value("notes", [])
|
|
notes = filter(lambda n: n["dest"] == nick, notes)
|
|
for note in notes:
|
|
message = self.config["deliver_format"].format(
|
|
sender=note["sender"],
|
|
dest=nick,
|
|
message=note["message"],
|
|
)
|
|
self.bot.say(event.target, message)
|
|
self.remove_storage_list_value("notes", note)
|
|
|
|
def on_pubmsg(self, event):
|
|
if not self.should_handle_command(event.arguments[0], no_content=True):
|
|
return False
|
|
|
|
# "note down" command.
|
|
command0 = self.config["commands"][0]
|
|
if self.command.ident.startswith(command0):
|
|
content = self.command.ident[len(command0):].strip()
|
|
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"]
|
|
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)
|