2017-05-29 12:39:48 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import re
|
2017-06-30 12:04:50 +00:00
|
|
|
|
|
|
|
import irc3
|
2017-08-22 13:57:57 +00:00
|
|
|
from docopt import Dict
|
2017-05-29 12:39:48 +00:00
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
|
|
|
|
|
|
|
from . import DatabasePlugin
|
|
|
|
|
|
|
|
|
2017-06-01 15:38:34 +00:00
|
|
|
@irc3.plugin
|
2017-05-29 12:39:48 +00:00
|
|
|
class Seen(DatabasePlugin):
|
|
|
|
requires = ['irc3.plugins.command',
|
2017-07-07 00:11:20 +00:00
|
|
|
'bot.plugins.storage']
|
2017-05-29 12:39:48 +00:00
|
|
|
|
2017-06-29 21:04:21 +00:00
|
|
|
@command
|
2017-08-22 13:57:57 +00:00
|
|
|
def seen(self, mask: IrcString, target: IrcString, args: Dict):
|
2017-08-22 14:15:52 +00:00
|
|
|
"""Get last seen date and message for a nick
|
2017-05-29 12:39:48 +00:00
|
|
|
|
|
|
|
%%seen [<nick>]
|
|
|
|
"""
|
2017-08-21 14:59:27 +00:00
|
|
|
nick = args.get('<nick>', mask.nick)
|
2017-06-29 21:04:21 +00:00
|
|
|
|
2017-06-30 12:04:50 +00:00
|
|
|
# Don't be stupid
|
2017-05-29 12:39:48 +00:00
|
|
|
if nick == mask.nick:
|
2017-08-22 14:15:52 +00:00
|
|
|
return '{}, look in the mirror faggot!'.format(nick)
|
2017-06-29 21:04:21 +00:00
|
|
|
|
2017-06-30 12:04:50 +00:00
|
|
|
# Fetch seen from database
|
|
|
|
self.cur.execute('''
|
2017-08-22 14:15:52 +00:00
|
|
|
SELECT
|
2017-06-30 12:04:50 +00:00
|
|
|
*
|
2017-08-22 14:15:52 +00:00
|
|
|
FROM
|
2017-06-30 12:04:50 +00:00
|
|
|
seens
|
2017-08-22 14:15:52 +00:00
|
|
|
WHERE
|
2017-06-30 15:47:24 +00:00
|
|
|
lower(nick) = lower(%s)
|
2017-06-30 12:04:50 +00:00
|
|
|
''', [nick])
|
2017-05-29 12:39:48 +00:00
|
|
|
seen = self.cur.fetchone()
|
2017-06-29 21:04:21 +00:00
|
|
|
|
2017-06-30 12:04:50 +00:00
|
|
|
# No result
|
2017-05-29 12:39:48 +00:00
|
|
|
if not seen:
|
2017-08-22 14:15:52 +00:00
|
|
|
return 'I\'ve never seen {}'.format(nick)
|
2017-06-29 21:04:21 +00:00
|
|
|
|
2017-06-30 12:04:50 +00:00
|
|
|
# Return result
|
2017-05-29 12:39:48 +00:00
|
|
|
return '{nick} was last seen {delta} saying: {message}'.format(
|
|
|
|
nick=seen['nick'],
|
2017-06-30 12:04:50 +00:00
|
|
|
# TODO: relative string delta?
|
|
|
|
delta=seen['seen_at'],
|
2017-05-29 16:44:26 +00:00
|
|
|
message=re.sub(r'\x01ACTION (.*)\x01', r'/me \1', seen['message']),
|
2017-05-29 12:39:48 +00:00
|
|
|
)
|
|
|
|
|
2017-06-29 21:04:21 +00:00
|
|
|
@irc3.event(r'(?i)^:(?P<mask>\S+) PRIVMSG (?P<target>\S+) :(?P<msg>.*)')
|
|
|
|
def save(self, mask: str, target: str, msg: str):
|
2017-06-30 15:47:24 +00:00
|
|
|
mask = IrcString(mask)
|
|
|
|
|
2017-08-22 14:15:52 +00:00
|
|
|
self.cur.execute('''
|
|
|
|
INSERT INTO
|
|
|
|
seens (nick, host, channel, message)
|
|
|
|
VALUES
|
|
|
|
(%s, %s, %s, %s)
|
|
|
|
ON CONFLICT (nick) DO UPDATE SET
|
|
|
|
host = excluded.host,
|
|
|
|
channel = excluded.channel,
|
|
|
|
seen_at = now(),
|
|
|
|
message = excluded.message
|
|
|
|
''', [mask.nick, mask.host, target, msg])
|
|
|
|
self.con.commit()
|