From c1ddb10e06e77e33d75895cf6b396fd1f376c685 Mon Sep 17 00:00:00 2001 From: mrhanky Date: Mon, 29 May 2017 14:39:48 +0200 Subject: [PATCH] Added seen plugin --- config.json | 1 + nxy/plugins/seen.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ schema.sql | 10 ++++++++++ 3 files changed, 55 insertions(+) create mode 100644 nxy/plugins/seen.py diff --git a/config.json b/config.json index b656afc..e3a5b4e 100644 --- a/config.json +++ b/config.json @@ -17,6 +17,7 @@ "nxy.plugins.kernel", "nxy.plugins.mcmaniac", "nxy.plugins.quotes", + "nxy.plugins.seen", "nxy.plugins.tell", "nxy.plugins.timer", "nxy.plugins.useless", diff --git a/nxy/plugins/seen.py b/nxy/plugins/seen.py new file mode 100644 index 0000000..db8c3a9 --- /dev/null +++ b/nxy/plugins/seen.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- +import re +import irc3 + +from datetime import datetime +from docopt import Dict as DocOptDict +from irc3.plugins.command import command +from irc3.utils import IrcString + +from . import DatabasePlugin + + +class Seen(DatabasePlugin): + requires = ['irc3.plugins.command', + 'nxy.plugins.database'] + + @command(options_first=True) + def seen(self, mask: IrcString, channel: IrcString, args: DocOptDict): + """Get last seen date and message for a nick. + + %%seen [] + """ + nick = args.get('') or mask.nick + if nick == mask.nick: + return '{nick}, have you seen in the mirror?'.format(nick=nick) + self.cur.execute('select * from seens where nick = ?', [nick]) + seen = self.cur.fetchone() + if not seen: + return 'I\'ve never seen {nick}'.format(nick=nick) + return '{nick} was last seen {delta} saying: {message}'.format( + nick=seen['nick'], + # TODO: relate string delta + delta=datetime.now() - seen['last_seen'], + message=re.sub(r'\x01ACTION (.*)\x01', r'\1', seen['message']), + ) + + @irc3.event(r'(?i)^:(?P\S+) PRIVMSG (?P\S+) :(?P.*)') + def save(self, mask: str, channel: str, msg: str): + nick = IrcString(mask).nick + if nick != self.bot.nick: + self.cur.execute('insert or replace into seens (nick, channel, ' + 'message, last_seen) values (?, ?, ?, ?)', + [nick, channel.lower(), msg, datetime.now()]) + self.con.commit() diff --git a/schema.sql b/schema.sql index 6cd83b3..5f453c8 100644 --- a/schema.sql +++ b/schema.sql @@ -29,3 +29,13 @@ create table if not exists tells ( created timestamp not null default current_timestamp, unique (to_user, message) ); + + +create table if not exists seens ( + id integer primary key autoincrement, + nick text not null, + channel text not null, + message text not null, + last_seen timestamp not null, + unique (nick collate nocase) on conflict replace +);