2017-07-28 20:08:31 -04:00
|
|
|
#!/usr/bin/python3
|
|
|
|
"""
|
|
|
|
bash.py - bash.org clone. Saves quotes from quote-logger module to a db
|
|
|
|
author: paulwalko <paul@walko.org>
|
|
|
|
"""
|
|
|
|
|
|
|
|
from threading import Thread
|
2017-08-02 20:24:12 -04:00
|
|
|
import os, sqlite3, time, web
|
2017-07-28 20:08:31 -04:00
|
|
|
|
|
|
|
queue = []
|
|
|
|
|
|
|
|
def setup(self):
|
2018-12-23 17:51:56 -05:00
|
|
|
"""Creates sqlite database to store quotes
|
|
|
|
"""
|
2017-07-28 20:08:31 -04:00
|
|
|
fn = self.nick + '-' + self.config.host + '.bash.db'
|
|
|
|
bash_quotes = 'bash_logs'
|
|
|
|
self.bash_quotes_path = os.path.join(os.path.expanduser('~/.phenny'), bash_quotes)
|
|
|
|
self.bash_db = os.path.join(os.path.expanduser('~/.phenny'), fn)
|
|
|
|
self.bash_conn = sqlite3.connect(self.bash_db)
|
|
|
|
|
|
|
|
if not os.path.exists(self.bash_quotes_path):
|
|
|
|
os.makedirs(self.bash_quotes_path)
|
2018-12-23 23:20:59 -05:00
|
|
|
|
2017-07-28 20:08:31 -04:00
|
|
|
c = self.bash_conn.cursor()
|
|
|
|
c.execute('''create table if not exists quotes (
|
|
|
|
id integer primary key autoincrement,
|
|
|
|
channel varchar(255),
|
|
|
|
nick varchar(255),
|
|
|
|
quote text,
|
|
|
|
time timestamp default CURRENT_TIMESTAMP
|
|
|
|
);''')
|
|
|
|
|
|
|
|
c.execute('''create table if not exists stats (
|
|
|
|
id integer primary key autoincrement,
|
|
|
|
channel varchar(255),
|
|
|
|
nick varchar(255),
|
|
|
|
lines unsigned big int not null default 0,
|
|
|
|
unique (channel, nick) on conflict replace
|
|
|
|
);''')
|
|
|
|
c.close()
|
|
|
|
|
2018-12-23 17:51:56 -05:00
|
|
|
# Start thread to check for new messages
|
2017-07-28 20:08:31 -04:00
|
|
|
Thread(target = insert_into_db_caller, args=(self,)).start()
|
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
def insert_into_db(phenny, sqlite_data):
|
|
|
|
"""inserts message to to temp db"""
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
if not bash.conn:
|
|
|
|
bash.conn = sqlite3.connect(phenny.bash_db)
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c = bash.conn.cursor()
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''INSERT INTO quotes
|
|
|
|
(channel, nick, quote, time)
|
|
|
|
VALUES(
|
|
|
|
:channel,
|
|
|
|
:nick,
|
|
|
|
:quote,
|
|
|
|
CURRENT_TIMESTAMP
|
|
|
|
);''', sqlite_data)
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''INSERT OR REPLACE INTO stats
|
|
|
|
(channel, nick, lines)
|
|
|
|
VALUES(
|
|
|
|
:channel,
|
|
|
|
:nick,
|
|
|
|
COALESCE((SELECT lines FROM stats WHERE channel=:channel AND nick=:nick) + 1, 1)
|
|
|
|
);''', sqlite_data)
|
2017-07-28 20:08:31 -04:00
|
|
|
|
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.close()
|
|
|
|
bash.conn.commit()
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c = bash.conn.cursor()
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''SELECT id FROM quotes ORDER BY id DESC LIMIT 1''')
|
|
|
|
last_id = c.fetchall()[0][0] - 99
|
|
|
|
|
|
|
|
c.execute('''SELECT channel, nick FROM quotes WHERE id < ?''', (last_id,))
|
2017-07-28 20:08:31 -04:00
|
|
|
rows = c.fetchall()
|
2018-12-23 23:20:59 -05:00
|
|
|
for row in rows:
|
|
|
|
channel = row[0]
|
|
|
|
nick = row[1]
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''SELECT lines FROM stats WHERE channel=? AND nick=?''', (channel, nick))
|
|
|
|
lines = 0
|
|
|
|
lines = c.fetchall()[0][0]
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
if lines - 1 == 0:
|
|
|
|
c.execute('''DELETE FROM stats WHERE channel=? AND nick=?''', (channel, nick))
|
|
|
|
else:
|
|
|
|
c.execute('''REPLACE INTO stats
|
|
|
|
(channel, nick, lines)
|
|
|
|
VALUES(
|
|
|
|
?,
|
|
|
|
?,
|
|
|
|
(SELECT lines FROM stats WHERE channel=? AND nick=?) - 1
|
|
|
|
);''', (channel, nick, channel, nick))
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''DELETE FROM quotes WHERE id < ?''', (last_id,))
|
2017-08-02 20:24:12 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.close()
|
|
|
|
bash.conn.commit()
|
2017-08-02 20:24:12 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
def insert_into_db_caller(phenny):
|
|
|
|
"""Checks for a new quote once a second
|
|
|
|
"""
|
|
|
|
while True:
|
|
|
|
global queue
|
|
|
|
if len(queue) > 0:
|
|
|
|
insert_into_db(phenny, queue.pop(0))
|
|
|
|
time.sleep(1)
|
2017-07-28 20:08:31 -04:00
|
|
|
|
|
|
|
def logger(phenny, input):
|
2018-12-23 17:51:56 -05:00
|
|
|
"""logs everyting to specific format, except we only keep last 100 lines
|
|
|
|
"""
|
2017-07-28 20:08:31 -04:00
|
|
|
|
|
|
|
allowed_actions = ['PRIVMSG', 'JOIN', 'QUIT', 'PART', 'NICK', 'KICK', 'MODE']
|
2018-12-23 23:20:59 -05:00
|
|
|
|
2017-07-28 20:08:31 -04:00
|
|
|
if input.event not in allowed_actions:
|
|
|
|
return
|
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
message=''
|
2017-07-28 20:08:31 -04:00
|
|
|
if input.event == 'PRIVMSG':
|
|
|
|
channel = input.sender
|
|
|
|
nick = input.nick
|
|
|
|
if input.group(1)[:8] == '\x01ACTION ':
|
|
|
|
quote = "* {nick} {msg}".format(nick=nick, msg=input.group(1)[8:-1])
|
|
|
|
else:
|
|
|
|
quote = "<{nick}> {msg}".format(nick=nick, msg=input.group(1))
|
|
|
|
elif input.event == 'JOIN':
|
|
|
|
channel = input.group(1)
|
|
|
|
nick = input.nick
|
|
|
|
quote = "--> {nick} has joined {channel}".format(nick=nick, channel=channel)
|
|
|
|
elif input.event == 'PART':
|
|
|
|
channel = input.sender
|
|
|
|
nick = input.nick
|
|
|
|
if input.group(1):
|
|
|
|
message = " ({message})".format(message=input.group(1))
|
2018-12-23 17:51:56 -05:00
|
|
|
quote = "<-- {nick} has left {channel}{message}" \
|
|
|
|
.format(nick=nick, channel=channel, message=message)
|
2017-07-28 20:08:31 -04:00
|
|
|
elif input.event == 'QUIT':
|
|
|
|
channel = 'ALL'
|
|
|
|
nick = input.nick
|
|
|
|
if input.group(1):
|
|
|
|
message = " ({message})".format(message=input.group(1))
|
|
|
|
quote = "<-- {nick} has quit{message}".format(nick=nick, message=message)
|
|
|
|
elif input.event == 'KICK':
|
|
|
|
channel = input.sender
|
|
|
|
nick = input.nick
|
2018-12-23 17:51:56 -05:00
|
|
|
quote = "<-- {nick} has kicked {nick_kick} [{kick_msg}] from {channel}" \
|
2018-12-23 23:20:59 -05:00
|
|
|
.format(nick=nick, nick_kick=input.args[1],
|
|
|
|
kick_msg=input.group(1), channel=channel)
|
2017-07-28 20:08:31 -04:00
|
|
|
elif input.event == 'NICK':
|
|
|
|
channel = 'ALL'
|
|
|
|
nick = input.nick
|
2018-12-23 17:51:56 -05:00
|
|
|
quote = "-- {nick} is now known as {nick_new}" \
|
|
|
|
.format(nick=nick, nick_new=input.group(1))
|
2017-07-28 20:08:31 -04:00
|
|
|
elif len(input.args) > 0 and input.args[0].startswith('#'):
|
|
|
|
channel = input.sender
|
|
|
|
nick = input.nick
|
|
|
|
args = str(input.args[1:]).replace('(', '').replace(')', '').replace("'", '')
|
|
|
|
if args.endswith(','):
|
|
|
|
args = args.replace(',', '')
|
|
|
|
else:
|
|
|
|
args = args.replace(',', ' ')
|
2018-12-23 17:51:56 -05:00
|
|
|
quote = "-- Mode {channel} [{args}] by {nick}" \
|
|
|
|
.format(channel=input.sender, args=args, nick=input.nick)
|
2017-07-28 20:08:31 -04:00
|
|
|
else:
|
|
|
|
return
|
|
|
|
|
|
|
|
quote = "{quote}\n".format(quote=quote)
|
|
|
|
|
|
|
|
sqlite_data = {
|
|
|
|
'channel': channel,
|
|
|
|
'nick': nick,
|
|
|
|
'quote': quote
|
|
|
|
}
|
|
|
|
|
|
|
|
global queue
|
|
|
|
queue.append(sqlite_data)
|
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
def bash(phenny, input):
|
|
|
|
"""'.bash' - queries a quote selection to add"""
|
|
|
|
usage = ('Usage: .bash <nick_1> <# msgs to start at> <nick_2>'
|
|
|
|
' <# msgs to end at> (Specifies a range of messages)')
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
input_all = input.group(2)
|
|
|
|
if not input_all:
|
|
|
|
phenny.say(usage)
|
|
|
|
return
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
input_args = input_all.split()
|
|
|
|
if len(input_args) < 4:
|
|
|
|
phenny.say(usage)
|
|
|
|
return
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
channel = input.sender
|
|
|
|
nick1 = input_args[0]
|
|
|
|
nick1_start_str = input_args[1]
|
|
|
|
nick2 = input_args[2]
|
|
|
|
nick2_end_str = input_args[3]
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
# Check Input Type
|
|
|
|
if not nick1_start_str.isdigit() or not nick2_end_str.isdigit():
|
|
|
|
phenny.say('Error: 2nd & 4th argument must be integers')
|
|
|
|
phenny.say(usage)
|
|
|
|
return
|
|
|
|
|
|
|
|
nick1_start = int(nick1_start_str)
|
|
|
|
nick2_end = int(nick2_end_str)
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
# Connect to db
|
|
|
|
fn = phenny.nick + '-' + phenny.config.host + '.bash.db'
|
|
|
|
bash_db = os.path.join(os.path.expanduser('~/.phenny'), fn)
|
|
|
|
bash_conn = sqlite3.connect(bash_db)
|
|
|
|
c = bash_conn.cursor()
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
# Check input for validity
|
|
|
|
c.execute('''SELECT lines FROM stats
|
|
|
|
WHERE (channel=? OR channel='NULL')
|
|
|
|
AND nick=?''', (channel, nick1))
|
2017-07-28 20:08:31 -04:00
|
|
|
rows = c.fetchall()
|
2018-12-23 23:20:59 -05:00
|
|
|
nick1_total = 0
|
2017-07-28 20:08:31 -04:00
|
|
|
for row in rows:
|
2018-12-23 23:20:59 -05:00
|
|
|
nick1_total = nick1_total + row[0]
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
c.execute('''SELECT lines FROM stats
|
|
|
|
WHERE (channel=? OR channel='NULL')
|
|
|
|
AND nick=?''', (channel, nick2))
|
|
|
|
rows = c.fetchall()
|
|
|
|
nick2_total = 0
|
|
|
|
for row in rows:
|
|
|
|
nick2_total = nick2_total + row[0]
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
if nick1_total < nick1_start:
|
|
|
|
phenny.say("Error: {} has not done {} actions ({} replies not included)" \
|
|
|
|
.format(nick1, nick1_start, phenny.nick))
|
|
|
|
return
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
if nick2_total < nick2_end:
|
|
|
|
phenny.say("Error: {} has not done {} actions ({} replies not included)" \
|
|
|
|
.format(nick2, nick2_end, phenny.nick))
|
|
|
|
return
|
2017-07-28 20:08:31 -04:00
|
|
|
|
2018-12-23 23:20:59 -05:00
|
|
|
# Fetch quote ids
|
|
|
|
c.execute('''SELECT id, channel, nick FROM quotes
|
|
|
|
WHERE (channel=? OR channel='ALL')
|
|
|
|
AND nick=?''', (channel, nick1))
|
|
|
|
rows = c.fetchall()
|
|
|
|
nick1_id = -1
|
|
|
|
for row, i in zip(reversed(rows), range(nick1_start)):
|
|
|
|
if i == nick1_start - 1:
|
|
|
|
nick1_id = row[0]
|
|
|
|
|
|
|
|
c.execute('''SELECT id, channel, nick FROM quotes
|
|
|
|
WHERE (channel=? OR channel='ALL')
|
|
|
|
AND nick=?''', (channel, nick2))
|
|
|
|
rows = c.fetchall()
|
|
|
|
nick2_id = -1
|
|
|
|
for row, i in zip(reversed(rows), range(nick2_end)):
|
|
|
|
if i == nick2_end - 1:
|
|
|
|
nick2_id = row[0]
|
|
|
|
|
|
|
|
if nick2_id < nick1_id:
|
|
|
|
phenny.say('Error, try again. 2nd message must occur after first message.')
|
|
|
|
phenny.say(usage)
|
|
|
|
return
|
|
|
|
|
|
|
|
# Fetch quotes within range of ids
|
|
|
|
c.execute('''SELECT quote FROM quotes
|
|
|
|
WHERE (channel=? OR channel='ALL')
|
|
|
|
AND id >= ? AND id <= ?''', (channel, nick1_id, nick2_id))
|
|
|
|
final_lines = []
|
|
|
|
for line in c.fetchall():
|
|
|
|
final_lines.append(line[0])
|
|
|
|
final_lines = ''.join(final_lines)
|
|
|
|
|
|
|
|
|
|
|
|
quote_json = {
|
|
|
|
'body': final_lines,
|
|
|
|
'tags': ','.join([phenny.nick, nick1, nick2]),
|
|
|
|
'key': phenny.config.bash_api_key
|
|
|
|
}
|
|
|
|
|
|
|
|
web.post('https://bash.vtluug.org/quotes', {}, {}, True, json=quote_json)
|
|
|
|
|
|
|
|
phenny.say('Check https://bash.vtluug.org/quotes to see your quote!')
|
2017-07-28 20:08:31 -04:00
|
|
|
|
|
|
|
bash.conn = None
|
2018-12-23 17:51:56 -05:00
|
|
|
bash.rule = (['bash', 'vtbash', 'quote'], r'(.*)')
|
2017-07-28 20:08:31 -04:00
|
|
|
logger.event = '*'
|
|
|
|
logger.rule = r'(.*)'
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
print(__doc__.strip())
|