2017-05-15 22:26:10 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2017-07-07 00:11:20 +00:00
|
|
|
import re
|
2017-05-15 22:26:10 +00:00
|
|
|
|
2017-07-07 00:11:20 +00:00
|
|
|
import irc3
|
2017-05-16 10:06:29 +00:00
|
|
|
from docopt import Dict as DocOptDict
|
|
|
|
from irc3.plugins.command import command
|
|
|
|
from irc3.utils import IrcString
|
2017-06-30 13:03:01 +00:00
|
|
|
from psycopg2 import Error
|
2017-05-16 10:06:29 +00:00
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
from . import DatabasePlugin
|
2017-07-07 00:11:20 +00:00
|
|
|
from ..utils import parse_int
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
@irc3.plugin
|
|
|
|
class Quotes(DatabasePlugin):
|
2017-05-18 00:01:44 +00:00
|
|
|
requires = ['irc3.plugins.command',
|
2017-07-07 00:11:20 +00:00
|
|
|
'bot.plugins.storage']
|
2017-06-30 13:03:01 +00:00
|
|
|
|
2017-06-30 15:47:24 +00:00
|
|
|
def add_quote(self, mask, nick, quote, channel):
|
2017-06-30 13:03:01 +00:00
|
|
|
# Parse nick from "<@foobar>" like strings
|
2017-07-07 00:11:20 +00:00
|
|
|
nick = re.match(r'<?[~&@%+]?([a-zA-Z0-9_\-^`|\\\[\]{}]+)>?', nick).group(1)
|
2017-06-30 13:03:01 +00:00
|
|
|
|
|
|
|
if not nick:
|
|
|
|
self.bot.notice(mask.nick, '[Quotes] Error parsing nick')
|
|
|
|
else:
|
|
|
|
# Insert quote into database
|
|
|
|
self.cur.execute('''
|
|
|
|
insert into
|
2017-06-30 15:47:24 +00:00
|
|
|
quotes (nick, item, channel, created_by)
|
2017-06-30 13:03:01 +00:00
|
|
|
values
|
2017-06-30 17:33:26 +00:00
|
|
|
(%s, %s, %s, %s)
|
2017-06-30 15:47:24 +00:00
|
|
|
''', [nick, quote, channel, mask.nick])
|
2017-06-30 13:03:01 +00:00
|
|
|
|
|
|
|
def delete_quote(self, nick, quote):
|
2017-07-07 00:11:20 +00:00
|
|
|
index, order = parse_int(quote, select=False)
|
2017-06-30 13:03:01 +00:00
|
|
|
|
|
|
|
if index:
|
|
|
|
# Delete from database
|
|
|
|
self.cur.execute('''
|
|
|
|
with ranked_quotes as (
|
|
|
|
select
|
|
|
|
id,
|
|
|
|
rank() over (partition by nick order by id {order})
|
|
|
|
from
|
|
|
|
quotes
|
|
|
|
where
|
|
|
|
nick = %s
|
|
|
|
)
|
|
|
|
|
|
|
|
delete from
|
|
|
|
quotes
|
|
|
|
where
|
|
|
|
id = (
|
|
|
|
select
|
|
|
|
id
|
|
|
|
from
|
|
|
|
ranked_quotes
|
|
|
|
where
|
|
|
|
rank = %s
|
|
|
|
)
|
|
|
|
'''.format(order=order), [nick, index])
|
2017-05-15 22:26:10 +00:00
|
|
|
|
|
|
|
@command(options_first=True)
|
2017-06-30 13:03:01 +00:00
|
|
|
def q(self, mask: IrcString, target: IrcString, args: DocOptDict):
|
2017-06-30 18:14:02 +00:00
|
|
|
"""Get, add or delete quotes for an user.
|
2017-05-16 12:27:34 +00:00
|
|
|
|
2017-05-15 22:26:10 +00:00
|
|
|
%%q <cmd> <nick> <quote>...
|
2017-05-16 06:54:47 +00:00
|
|
|
%%q <nick> [<index>]
|
2017-05-15 22:26:10 +00:00
|
|
|
"""
|
|
|
|
cmd = args.get('<cmd>')
|
|
|
|
nick = args['<nick>']
|
2017-07-04 14:41:54 +00:00
|
|
|
quote = ' '.join(args.get('<quote>'))
|
2017-06-30 13:03:01 +00:00
|
|
|
|
|
|
|
if cmd and quote:
|
|
|
|
try:
|
|
|
|
# Anybody can add
|
2017-05-16 06:54:47 +00:00
|
|
|
if cmd == 'add':
|
2017-06-30 15:47:24 +00:00
|
|
|
self.add_quote(mask, nick, quote, target)
|
2017-06-30 13:03:01 +00:00
|
|
|
# But only admins can delete
|
|
|
|
elif cmd == 'del' and self.guard.has_permission(mask, 'admin'):
|
|
|
|
self.delete_quote(nick, quote)
|
|
|
|
|
2017-05-16 06:54:47 +00:00
|
|
|
self.con.commit()
|
2017-07-04 13:22:20 +00:00
|
|
|
except Error as ex:
|
2017-06-30 13:03:01 +00:00
|
|
|
# Rollback transaction on error
|
|
|
|
self.con.rollback()
|
2017-05-15 22:26:10 +00:00
|
|
|
else:
|
2017-05-16 06:54:47 +00:00
|
|
|
index = args.get('<index>')
|
2017-06-30 13:03:01 +00:00
|
|
|
values = [nick]
|
|
|
|
|
2017-05-16 06:54:47 +00:00
|
|
|
if index:
|
|
|
|
index = parse_int(index)
|
|
|
|
if not index:
|
|
|
|
return
|
2017-07-07 00:11:20 +00:00
|
|
|
index, order = index
|
2017-06-30 13:03:01 +00:00
|
|
|
order = 'rank {order}'.format(order=order)
|
|
|
|
offset = 'offset %s'
|
|
|
|
values.append(index)
|
2017-05-15 22:26:10 +00:00
|
|
|
else:
|
2017-05-16 06:54:47 +00:00
|
|
|
order = 'random()'
|
2017-06-30 13:03:01 +00:00
|
|
|
offset = ''
|
|
|
|
|
|
|
|
# Fetch quote from database
|
|
|
|
self.cur.execute("""
|
|
|
|
with ranked_quotes as (
|
|
|
|
select
|
|
|
|
nick,
|
|
|
|
item,
|
|
|
|
rank() over (partition by nick order by id),
|
|
|
|
count(*) over (partition by nick) as total
|
|
|
|
from
|
|
|
|
quotes
|
|
|
|
)
|
|
|
|
|
|
|
|
select
|
|
|
|
*
|
|
|
|
from
|
|
|
|
ranked_quotes
|
|
|
|
where
|
2017-06-30 15:47:24 +00:00
|
|
|
lower(nick) like lower(%s)
|
2017-06-30 13:03:01 +00:00
|
|
|
order by
|
|
|
|
{order}
|
|
|
|
limit
|
|
|
|
1
|
|
|
|
{offset}
|
|
|
|
""".format(order=order, offset=offset), values)
|
2017-05-16 06:54:47 +00:00
|
|
|
result = self.cur.fetchone()
|
2017-06-30 13:03:01 +00:00
|
|
|
|
2017-05-16 06:54:47 +00:00
|
|
|
if result:
|
2017-06-30 13:03:01 +00:00
|
|
|
return '[{rank}/{total}] <{nick}> {item}'.format(**result)
|