remove unused plugins
This commit is contained in:
parent
33f801bc5e
commit
aa64273ee8
|
@ -1,11 +0,0 @@
|
||||||
from util import hook
|
|
||||||
|
|
||||||
import requests
|
|
||||||
|
|
||||||
@hook.command
|
|
||||||
def derpiback(pls):
|
|
||||||
r = requests.get("https://derpibooru.org")
|
|
||||||
if "J6-eVNTVvMk" in r.text:
|
|
||||||
return "nope derpibooru is still down for maintenance, at soonest it will be tomorrow"
|
|
||||||
|
|
||||||
return "yep"
|
|
103
plugins/log.py
103
plugins/log.py
|
@ -1,103 +0,0 @@
|
||||||
"""
|
|
||||||
log.py: written by Scaevolus 2009
|
|
||||||
"""
|
|
||||||
|
|
||||||
import os
|
|
||||||
import codecs
|
|
||||||
import time
|
|
||||||
import re
|
|
||||||
|
|
||||||
from util import hook
|
|
||||||
|
|
||||||
|
|
||||||
log_fds = {} # '%(net)s %(chan)s' : (filename, fd)
|
|
||||||
|
|
||||||
timestamp_format = '%H:%M:%S'
|
|
||||||
|
|
||||||
formats = {'PRIVMSG': '<%(nick)s> %(msg)s',
|
|
||||||
'PART': '-!- %(nick)s [%(user)s@%(host)s] has left %(chan)s',
|
|
||||||
'JOIN': '-!- %(nick)s [%(user)s@%(host)s] has joined %(param0)s',
|
|
||||||
'MODE': '-!- mode/%(chan)s [%(param_tail)s] by %(nick)s',
|
|
||||||
'KICK': '-!- %(param1)s was kicked from %(chan)s by %(nick)s [%(msg)s]',
|
|
||||||
'TOPIC': '-!- %(nick)s changed the topic of %(chan)s to: %(msg)s',
|
|
||||||
'QUIT': '-!- %(nick)s has quit [%(msg)s]',
|
|
||||||
'PING': '',
|
|
||||||
'NOTICE': ''
|
|
||||||
}
|
|
||||||
|
|
||||||
ctcp_formats = {'ACTION': '* %(nick)s %(ctcpmsg)s'}
|
|
||||||
|
|
||||||
irc_color_re = re.compile(r'(\x03(\d+,\d+|\d)|[\x0f\x02\x16\x1f])')
|
|
||||||
|
|
||||||
|
|
||||||
def get_log_filename(dir, server, chan):
|
|
||||||
return os.path.join(dir, 'log', gmtime('%Y'), server,
|
|
||||||
(gmtime('%%s.%m-%d.log') % chan).lower())
|
|
||||||
|
|
||||||
|
|
||||||
def gmtime(format):
|
|
||||||
return time.strftime(format, time.gmtime())
|
|
||||||
|
|
||||||
|
|
||||||
def beautify(input):
|
|
||||||
format = formats.get(input.command, '%(raw)s')
|
|
||||||
args = dict(input)
|
|
||||||
|
|
||||||
leng = len(args['paraml'])
|
|
||||||
for n, p in enumerate(args['paraml']):
|
|
||||||
args['param' + str(n)] = p
|
|
||||||
args['param_' + str(abs(n - leng))] = p
|
|
||||||
|
|
||||||
args['param_tail'] = ' '.join(args['paraml'][1:])
|
|
||||||
args['msg'] = irc_color_re.sub('', args['msg'])
|
|
||||||
|
|
||||||
if input.command == 'PRIVMSG' and input.msg.count('\x01') >= 2:
|
|
||||||
ctcp = input.msg.split('\x01', 2)[1].split(' ', 1)
|
|
||||||
if len(ctcp) == 1:
|
|
||||||
ctcp += ['']
|
|
||||||
args['ctcpcmd'], args['ctcpmsg'] = ctcp
|
|
||||||
format = ctcp_formats.get(args['ctcpcmd'],
|
|
||||||
'%(nick)s [%(user)s@%(host)s] requested unknown CTCP '
|
|
||||||
'%(ctcpcmd)s from %(chan)s: %(ctcpmsg)s')
|
|
||||||
|
|
||||||
return format % args
|
|
||||||
|
|
||||||
|
|
||||||
def get_log_fd(dir, server, chan):
|
|
||||||
fn = get_log_filename(dir, server, chan)
|
|
||||||
cache_key = '%s %s' % (server, chan)
|
|
||||||
filename, fd = log_fds.get(cache_key, ('', 0))
|
|
||||||
|
|
||||||
if fn != filename: # we need to open a file for writing
|
|
||||||
if fd != 0: # is a valid fd
|
|
||||||
fd.flush()
|
|
||||||
fd.close()
|
|
||||||
dir = os.path.split(fn)[0]
|
|
||||||
if not os.path.exists(dir):
|
|
||||||
os.makedirs(dir)
|
|
||||||
fd = codecs.open(fn, 'a', 'utf-8')
|
|
||||||
log_fds[cache_key] = (fn, fd)
|
|
||||||
|
|
||||||
return fd
|
|
||||||
|
|
||||||
|
|
||||||
@hook.singlethread
|
|
||||||
@hook.event('*')
|
|
||||||
def log(paraml, input=None, bot=None):
|
|
||||||
timestamp = gmtime(timestamp_format)
|
|
||||||
|
|
||||||
if input.command == 'QUIT': # these are temporary fixes until proper
|
|
||||||
input.chan = 'quit' # presence tracking is implemented
|
|
||||||
if input.command == 'NICK':
|
|
||||||
input.chan = 'nick'
|
|
||||||
|
|
||||||
beau = beautify(input)
|
|
||||||
|
|
||||||
if beau == '': # don't log this
|
|
||||||
return
|
|
||||||
|
|
||||||
if input.chan:
|
|
||||||
fd = get_log_fd(bot.persist_dir, input.server, input.chan)
|
|
||||||
fd.write(timestamp + ' ' + beau + '\n')
|
|
||||||
|
|
||||||
print timestamp, input.chan, beau.encode('utf8', 'ignore')
|
|
193
plugins/quote.py
193
plugins/quote.py
|
@ -1,193 +0,0 @@
|
||||||
import random
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from util import hook
|
|
||||||
|
|
||||||
|
|
||||||
def add_quote(db, chan, nick, add_nick, msg):
|
|
||||||
db.execute('''insert or fail into quote (chan, nick, add_nick,
|
|
||||||
msg, time) values(?,?,?,?,?)''',
|
|
||||||
(chan, nick, add_nick, msg, time.time()))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def del_quote(db, chan, nick, msg):
|
|
||||||
updated = db.execute('''update quote set deleted = 1 where
|
|
||||||
chan=? and lower(nick)=lower(?) and msg=?''',
|
|
||||||
(chan, nick, msg))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
if updated.rowcount == 0:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
def get_quotes_by_nick(db, chan, nick):
|
|
||||||
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
|
||||||
"and chan=? and lower(nick)=lower(?) order by time",
|
|
||||||
(chan, nick)).fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
def get_quotes_by_chan(db, chan):
|
|
||||||
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
|
||||||
"and chan=? order by time", (chan,)).fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
def get_quote_by_id(db, num):
|
|
||||||
return db.execute("select time, nick, msg from quote where deleted!=1 "
|
|
||||||
"and rowid=?", (num,)).fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
def format_quote(q, num, n_quotes):
|
|
||||||
ctime, nick, msg = q
|
|
||||||
return "[%d/%d] %s <%s> %s" % (num, n_quotes,
|
|
||||||
time.strftime("%Y-%m-%d", time.gmtime(ctime)), nick, msg)
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command('q')
|
|
||||||
@hook.command
|
|
||||||
def quote(inp, nick='', chan='', db=None, admin=False):
|
|
||||||
".q/.quote [#chan] [nick] [#n]/.quote add|delete <nick> <msg> -- gets " \
|
|
||||||
"random or [#n]th quote by <nick> or from <#chan>/adds or deletes " \
|
|
||||||
"quote"
|
|
||||||
|
|
||||||
db.execute("create table if not exists quote"
|
|
||||||
"(chan, nick, add_nick, msg, time real, deleted default 0, "
|
|
||||||
"primary key (chan, nick, msg))")
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
add = re.match(r"add[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
|
|
||||||
delete = re.match(r"delete[^\w@]+(\S+?)>?\s+(.*)", inp, re.I)
|
|
||||||
retrieve = re.match(r"(\S+)(?:\s+#?(-?\d+))?$", inp)
|
|
||||||
retrieve_chan = re.match(r"(#\S+)\s+(\S+)(?:\s+#?(-?\d+))?$", inp)
|
|
||||||
retrieve_id = re.match(r"(\d+)$", inp)
|
|
||||||
|
|
||||||
if add:
|
|
||||||
quoted_nick, msg = add.groups()
|
|
||||||
try:
|
|
||||||
add_quote(db, chan, quoted_nick, nick, msg)
|
|
||||||
db.commit()
|
|
||||||
except db.IntegrityError:
|
|
||||||
return "message already stored, doing nothing."
|
|
||||||
return "quote added."
|
|
||||||
if delete:
|
|
||||||
if not admin:
|
|
||||||
return 'only admins can delete quotes'
|
|
||||||
quoted_nick, msg = delete.groups()
|
|
||||||
if del_quote(db, chan, quoted_nick, msg):
|
|
||||||
return "deleted quote '%s'" % msg
|
|
||||||
else:
|
|
||||||
return "found no matching quotes to delete"
|
|
||||||
elif retrieve_id:
|
|
||||||
quote_id, = retrieve_id.groups()
|
|
||||||
num = 1
|
|
||||||
quotes = get_quote_by_id(db, quote_id)
|
|
||||||
elif retrieve:
|
|
||||||
select, num = retrieve.groups()
|
|
||||||
if select.startswith('#'):
|
|
||||||
quotes = get_quotes_by_chan(db, select)
|
|
||||||
else:
|
|
||||||
quotes = get_quotes_by_nick(db, chan, select)
|
|
||||||
elif retrieve_chan:
|
|
||||||
chan, nick, num = retrieve_chan.groups()
|
|
||||||
|
|
||||||
quotes = get_quotes_by_nick(db, chan, nick)
|
|
||||||
else:
|
|
||||||
return quote.__doc__
|
|
||||||
|
|
||||||
if num:
|
|
||||||
num = int(num)
|
|
||||||
|
|
||||||
n_quotes = len(quotes)
|
|
||||||
|
|
||||||
if not n_quotes:
|
|
||||||
return "no quotes found"
|
|
||||||
|
|
||||||
if num:
|
|
||||||
if num > n_quotes or (num < 0 and num < -n_quotes):
|
|
||||||
return "I only have %d quote%s for %s" % (n_quotes,
|
|
||||||
('s', '')[n_quotes == 1], select)
|
|
||||||
elif num < 0:
|
|
||||||
selected_quote = quotes[num]
|
|
||||||
num = n_quotes + num + 1
|
|
||||||
else:
|
|
||||||
selected_quote = quotes[num - 1]
|
|
||||||
else:
|
|
||||||
num = random.randint(1, n_quotes)
|
|
||||||
selected_quote = quotes[num - 1]
|
|
||||||
|
|
||||||
return format_quote(selected_quote, num, n_quotes)
|
|
||||||
|
|
||||||
|
|
||||||
class QuoteTest(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
import sqlite3
|
|
||||||
self.db = sqlite3.connect(':memory:')
|
|
||||||
|
|
||||||
quote('', db=self.db) # init DB
|
|
||||||
|
|
||||||
def quote(self, arg, **kwargs):
|
|
||||||
return quote(arg, chan='#test', nick='alice', db=self.db, **kwargs)
|
|
||||||
|
|
||||||
def add_quote(self, msg=''):
|
|
||||||
add_quote(self.db, '#test', 'Socrates', 'Plato',
|
|
||||||
msg or 'Education is the kindling of a flame,'
|
|
||||||
' not the filling of a vessel.')
|
|
||||||
|
|
||||||
def test_retrieve_chan(self):
|
|
||||||
self.add_quote()
|
|
||||||
assert '<Socrates> Education' in self.quote('#test')
|
|
||||||
|
|
||||||
def test_retrieve_user(self):
|
|
||||||
self.add_quote()
|
|
||||||
assert '<Socrates> Education' in self.quote('socrates')
|
|
||||||
|
|
||||||
def test_no_quotes(self):
|
|
||||||
assert "no quotes found" in self.quote("#notachan")
|
|
||||||
|
|
||||||
def test_quote_too_high(self):
|
|
||||||
self.add_quote()
|
|
||||||
assert 'I only have 1 quote for #test' in self.quote('#test 4')
|
|
||||||
|
|
||||||
def test_add(self):
|
|
||||||
self.quote("add <someone> witty phrase")
|
|
||||||
assert 'witty' in self.quote('#test')
|
|
||||||
|
|
||||||
def test_add_twice(self):
|
|
||||||
self.quote("add <someone> lol")
|
|
||||||
assert 'already stored' in self.quote("add <someone> lol")
|
|
||||||
|
|
||||||
def test_del_not_admin(self):
|
|
||||||
assert 'only admins' in self.quote('delete whoever 4')
|
|
||||||
|
|
||||||
def test_del_not_exists(self):
|
|
||||||
assert 'found no matching' in self.quote(
|
|
||||||
'delete whoever 4', admin=True)
|
|
||||||
|
|
||||||
def test_del(self):
|
|
||||||
self.add_quote("hi")
|
|
||||||
assert "deleted quote 'hi'" in self.quote(
|
|
||||||
'delete socrates hi', admin=True)
|
|
||||||
|
|
||||||
def test_retrieve_id(self):
|
|
||||||
self.add_quote()
|
|
||||||
assert 'Education is' in self.quote('1')
|
|
||||||
|
|
||||||
def test_retrieve_chan_user(self):
|
|
||||||
self.add_quote()
|
|
||||||
assert 'Education' in self.quote('#test socrates')
|
|
||||||
assert 'Education' in self.quote('#test socrates 1')
|
|
||||||
|
|
||||||
def test_nth(self):
|
|
||||||
self.add_quote('first quote')
|
|
||||||
self.add_quote('second quote')
|
|
||||||
self.add_quote('third quote')
|
|
||||||
self.add_quote('fourth quote')
|
|
||||||
assert 'third' in self.quote('socrates -2')
|
|
||||||
assert 'only have 4' in self.quote('socrates -9')
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
unittest.main()
|
|
|
@ -1,189 +0,0 @@
|
||||||
"""
|
|
||||||
remember.py: written by Scaevolus 2010
|
|
||||||
"""
|
|
||||||
|
|
||||||
import re
|
|
||||||
import string
|
|
||||||
import unittest
|
|
||||||
|
|
||||||
from util import hook
|
|
||||||
|
|
||||||
|
|
||||||
def db_init(db):
|
|
||||||
db.execute("create table if not exists memory(chan, word, data, nick,"
|
|
||||||
" primary key(chan, word))")
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def get_memory(db, chan, word):
|
|
||||||
row = db.execute("select data from memory where chan=? and word=lower(?)",
|
|
||||||
(chan, word)).fetchone()
|
|
||||||
if row:
|
|
||||||
return row[0]
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
|
||||||
@hook.command("r")
|
|
||||||
def remember(inp, nick='', chan='', db=None):
|
|
||||||
".remember <word> [+]<data> s/<before>/<after> -- maps word to data in the memory, or "
|
|
||||||
" does a string replacement (not regex)"
|
|
||||||
db_init(db)
|
|
||||||
|
|
||||||
append = False
|
|
||||||
replacement = False
|
|
||||||
|
|
||||||
try:
|
|
||||||
head, tail = inp.split(None, 1)
|
|
||||||
except ValueError:
|
|
||||||
return remember.__doc__
|
|
||||||
|
|
||||||
data = get_memory(db, chan, head)
|
|
||||||
if data is not None:
|
|
||||||
_head, _tail = data.split(None, 1)
|
|
||||||
else:
|
|
||||||
_head, _tail = head, ''
|
|
||||||
|
|
||||||
if tail[0] == '+':
|
|
||||||
append = True
|
|
||||||
# ignore + symbol
|
|
||||||
new = tail[1:]
|
|
||||||
# data is stored with the input so ignore it when re-adding it
|
|
||||||
if len(tail) > 1 and tail[1] in (string.punctuation + ' '):
|
|
||||||
tail = _tail + new
|
|
||||||
else:
|
|
||||||
tail = _tail + ' ' + new
|
|
||||||
|
|
||||||
if len(tail) > 2 and tail[0] == 's' and tail[1] in string.punctuation:
|
|
||||||
if _tail == '':
|
|
||||||
return "I don't know about that."
|
|
||||||
args = tail.split(tail[1])
|
|
||||||
if len(args) == 4 and args[3] == '':
|
|
||||||
args = args[:-1]
|
|
||||||
if len(args) == 3:
|
|
||||||
replacement = True
|
|
||||||
_, src, dst = args
|
|
||||||
new_data = _tail.replace(src, dst, 1)
|
|
||||||
if new_data == _tail:
|
|
||||||
return 'replacement left data unchanged'
|
|
||||||
tail = new_data
|
|
||||||
else:
|
|
||||||
return 'invalid replacement syntax -- try s$foo$bar instead?'
|
|
||||||
|
|
||||||
db.execute("replace into memory(chan, word, data, nick) values"
|
|
||||||
" (?,lower(?),?,?)", (chan, head, head + ' ' + tail, nick))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
if data:
|
|
||||||
if append:
|
|
||||||
return "appending %s to %s" % (new, data.replace('"', "''"))
|
|
||||||
elif replacement:
|
|
||||||
return "replacing '%s' with '%s' in %s" % (src, dst, _tail)
|
|
||||||
else:
|
|
||||||
return 'forgetting "%s", remembering this instead.' % \
|
|
||||||
data.replace('"', "''")
|
|
||||||
else:
|
|
||||||
return 'done.'
|
|
||||||
|
|
||||||
|
|
||||||
@hook.command
|
|
||||||
@hook.command("f")
|
|
||||||
def forget(inp, chan='', db=None):
|
|
||||||
".forget <word> -- forgets the mapping that word had"
|
|
||||||
|
|
||||||
db_init(db)
|
|
||||||
data = get_memory(db, chan, inp)
|
|
||||||
|
|
||||||
if data:
|
|
||||||
db.execute("delete from memory where chan=? and word=lower(?)",
|
|
||||||
(chan, inp))
|
|
||||||
db.commit()
|
|
||||||
return 'forgot `%s`' % data.replace('`', "'")
|
|
||||||
else:
|
|
||||||
return "I don't know about that."
|
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(r'^\? ?(.+)')
|
|
||||||
def question(inp, chan='', say=None, db=None):
|
|
||||||
"?<word> -- shows what data is associated with word"
|
|
||||||
db_init(db)
|
|
||||||
|
|
||||||
data = get_memory(db, chan, inp.group(1).strip())
|
|
||||||
if data:
|
|
||||||
say(data)
|
|
||||||
|
|
||||||
|
|
||||||
class MemoryTest(unittest.TestCase):
|
|
||||||
def setUp(self):
|
|
||||||
import sqlite3
|
|
||||||
self.db = sqlite3.connect(':memory:')
|
|
||||||
|
|
||||||
def remember(self, inp, nick='someone', chan='#test'):
|
|
||||||
return remember(inp, nick=nick, chan=chan, db=self.db)
|
|
||||||
|
|
||||||
def forget(self, inp, chan='#test'):
|
|
||||||
return forget(inp, chan=chan, db=self.db)
|
|
||||||
|
|
||||||
def question(self, inp, chan='#test'):
|
|
||||||
output = []
|
|
||||||
question(re.match(r'(.*)', inp),
|
|
||||||
chan=chan, say=output.append, db=self.db)
|
|
||||||
return output[0] if output else None
|
|
||||||
|
|
||||||
def test_remember(self):
|
|
||||||
assert 'done.' == self.remember('dogs :3')
|
|
||||||
assert 'dogs :3' == self.question('dogs')
|
|
||||||
|
|
||||||
def test_remember_doc(self):
|
|
||||||
assert '.remember <word>' in self.remember('bad_syntax')
|
|
||||||
|
|
||||||
def test_remember_overwrite(self):
|
|
||||||
self.remember('dogs :(')
|
|
||||||
assert 'forgetting "dogs :("' in self.remember('dogs :3')
|
|
||||||
assert 'dogs :3' == self.question('dogs')
|
|
||||||
|
|
||||||
def test_remember_hygiene(self):
|
|
||||||
self.remember('python good', chan='#python')
|
|
||||||
self.remember('python bad', chan='#ruby')
|
|
||||||
assert 'python good' == self.question('python', '#python')
|
|
||||||
assert 'python bad' == self.question('python', '#ruby')
|
|
||||||
|
|
||||||
def test_remember_append(self):
|
|
||||||
self.remember('ball big')
|
|
||||||
self.remember('ball +red')
|
|
||||||
assert 'ball big red' == self.question('ball')
|
|
||||||
|
|
||||||
def test_remember_append_punctuation(self):
|
|
||||||
self.remember('baby young')
|
|
||||||
self.remember('baby +, hungry')
|
|
||||||
assert 'baby young, hungry' == self.question('baby')
|
|
||||||
|
|
||||||
def test_remember_replace(self):
|
|
||||||
self.remember('person is very rich (rich!)')
|
|
||||||
self.remember('person s/rich/poor/')
|
|
||||||
assert 'person is very poor (rich!)' == self.question('person')
|
|
||||||
|
|
||||||
def test_remember_replace_invalid(self):
|
|
||||||
self.remember('fact bar')
|
|
||||||
assert 'invalid replacement' in self.remember('fact s/too/many/seps/!')
|
|
||||||
assert 'invalid replacement' in self.remember('fact s/toofew')
|
|
||||||
|
|
||||||
def test_remember_replace_ineffective(self):
|
|
||||||
self.remember('hay stack')
|
|
||||||
assert 'unchanged' in self.remember('hay s:needle:shiny needle')
|
|
||||||
|
|
||||||
def test_remember_replace_missing(self):
|
|
||||||
assert "I don't know about that" in self.remember('hay s/what/lol')
|
|
||||||
|
|
||||||
def test_question_empty(self):
|
|
||||||
assert self.question('not_in_db') is None
|
|
||||||
|
|
||||||
def test_forget(self):
|
|
||||||
self.remember('meat good', chan='#carnivore')
|
|
||||||
self.remember('meat bad', chan='#vegan')
|
|
||||||
assert 'forgot `meat good`' in self.forget('meat', chan='#carnivore')
|
|
||||||
assert 'meat bad' == self.question('meat', chan='#vegan')
|
|
||||||
|
|
||||||
def test_forget_missing(self):
|
|
||||||
assert "don't know" in self.forget('fakekey')
|
|
|
@ -1,51 +0,0 @@
|
||||||
from random import choice, random, randint
|
|
||||||
import time
|
|
||||||
|
|
||||||
from util import hook
|
|
||||||
|
|
||||||
COLORS = ['03','04','06','07','08','09','10','11','12','13']
|
|
||||||
|
|
||||||
#Shibe generation code borrowed from aji
|
|
||||||
|
|
||||||
class pvec:
|
|
||||||
def __init__(self, num):
|
|
||||||
self.v = [1.0] * num
|
|
||||||
self.norm()
|
|
||||||
def norm(self):
|
|
||||||
s = sum(self.v)
|
|
||||||
self.v = [x / s for x in self.v]
|
|
||||||
def pick(self):
|
|
||||||
r = random() * sum(self.v) # sum should always be 1, but meh
|
|
||||||
s = 0
|
|
||||||
for i, x in enumerate(self.v):
|
|
||||||
s += x
|
|
||||||
if r < s:
|
|
||||||
break
|
|
||||||
def calc(j, x):
|
|
||||||
fac = (1 - 3.5 / (abs(i - j) + 4.5))
|
|
||||||
return x * fac
|
|
||||||
self.v = [calc(j, x) for j, x in enumerate(self.v)]
|
|
||||||
self.norm()
|
|
||||||
return i
|
|
||||||
|
|
||||||
spvec = pvec(40)
|
|
||||||
for i in range(10):
|
|
||||||
spvec.pick()
|
|
||||||
|
|
||||||
last_color = '00'
|
|
||||||
|
|
||||||
def gen_prefix():
|
|
||||||
global last_color
|
|
||||||
|
|
||||||
color = choice(COLORS)
|
|
||||||
while color == last_color:
|
|
||||||
color = choice(COLORS)
|
|
||||||
|
|
||||||
last_color = color
|
|
||||||
return ' ' * spvec.pick() + '\3' + color
|
|
||||||
|
|
||||||
@hook.command("shibe")
|
|
||||||
def shibeify(inp):
|
|
||||||
return "%s%s" %\
|
|
||||||
(gen_prefix(), inp)
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
from util import hook, http
|
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(r'(?i)http://(?:www\.)?tinyurl.com/([A-Za-z0-9\-]+)')
|
|
||||||
def tinyurl(match):
|
|
||||||
try:
|
|
||||||
return http.open(match.group()).url.strip()
|
|
||||||
except http.URLError, e:
|
|
||||||
pass
|
|
|
@ -1,80 +0,0 @@
|
||||||
import math
|
|
||||||
import time
|
|
||||||
|
|
||||||
from util import hook, urlnorm, timesince
|
|
||||||
|
|
||||||
|
|
||||||
expiration_period = 60 * 60 * 24 # 1 day
|
|
||||||
|
|
||||||
ignored_urls = [urlnorm.normalize("http://google.com")]
|
|
||||||
|
|
||||||
|
|
||||||
def db_init(db):
|
|
||||||
db.execute("create table if not exists urlhistory"
|
|
||||||
"(chan, url, nick, time)")
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def insert_history(db, chan, url, nick):
|
|
||||||
db.execute("insert into urlhistory(chan, url, nick, time) "
|
|
||||||
"values(?,?,?,?)", (chan, url, nick, time.time()))
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
|
|
||||||
def get_history(db, chan, url):
|
|
||||||
db.execute("delete from urlhistory where time < ?",
|
|
||||||
(time.time() - expiration_period,))
|
|
||||||
return db.execute("select nick, time from urlhistory where "
|
|
||||||
"chan=? and url=? order by time desc", (chan, url)).fetchall()
|
|
||||||
|
|
||||||
|
|
||||||
def nicklist(nicks):
|
|
||||||
nicks = sorted(dict(nicks), key=unicode.lower)
|
|
||||||
if len(nicks) <= 2:
|
|
||||||
return ' and '.join(nicks)
|
|
||||||
else:
|
|
||||||
return ', and '.join((', '.join(nicks[:-1]), nicks[-1]))
|
|
||||||
|
|
||||||
|
|
||||||
def format_reply(history):
|
|
||||||
if not history:
|
|
||||||
return
|
|
||||||
|
|
||||||
last_nick, recent_time = history[0]
|
|
||||||
last_time = timesince.timesince(recent_time)
|
|
||||||
|
|
||||||
if len(history) == 1:
|
|
||||||
return "%s linked that %s ago." % (last_nick, last_time)
|
|
||||||
|
|
||||||
hour_span = math.ceil((time.time() - history[-1][1]) / 3600)
|
|
||||||
hour_span = '%.0f hours' % hour_span if hour_span > 1 else 'hour'
|
|
||||||
|
|
||||||
hlen = len(history)
|
|
||||||
ordinal = ["once", "twice", "%d times" % hlen][min(hlen, 3) - 1]
|
|
||||||
|
|
||||||
if len(dict(history)) == 1:
|
|
||||||
last = "last linked %s ago" % last_time
|
|
||||||
else:
|
|
||||||
last = "last linked by %s %s ago" % (last_nick, last_time)
|
|
||||||
|
|
||||||
return "that url has been posted %s in the past %s by %s (%s)." % (ordinal,
|
|
||||||
hour_span, nicklist(history), last)
|
|
||||||
|
|
||||||
|
|
||||||
@hook.regex(r'([a-zA-Z]+://|www\.)[^ ]+')
|
|
||||||
def urlinput(match, nick='', chan='', db=None, bot=None):
|
|
||||||
db_init(db)
|
|
||||||
url = urlnorm.normalize(match.group().encode('utf-8'))
|
|
||||||
if url not in ignored_urls:
|
|
||||||
url = url.decode('utf-8')
|
|
||||||
history = get_history(db, chan, url)
|
|
||||||
insert_history(db, chan, url, nick)
|
|
||||||
|
|
||||||
inp = match.string.lower()
|
|
||||||
|
|
||||||
for name in dict(history):
|
|
||||||
if name.lower() in inp: # person was probably quoting a line
|
|
||||||
return # that had a link. don't remind them.
|
|
||||||
|
|
||||||
if nick not in dict(history):
|
|
||||||
return format_reply(history)
|
|
Loading…
Reference in New Issue