This repository has been archived on 2023-04-13. You can view files and clone it, but cannot push or open issues or pull requests.
CloudBot/disabled_stuff/notes.py

192 lines
4.4 KiB
Python
Raw Normal View History

2011-11-20 10:23:31 +01:00
import re
2014-02-14 04:36:57 +01:00
from util import hook
db_ready = False
2011-11-20 10:23:31 +01:00
2014-02-13 12:47:01 +01:00
def clean_sql(sql):
2011-11-20 10:23:31 +01:00
return re.sub(r'\s+', " ", sql).strip()
def db_init(db):
global db_ready
if db_ready:
2011-11-20 10:23:31 +01:00
return
exists = db.execute("""
select exists (
select * from sqlite_master where type = "table" and name = "todos"
)
""").fetchone()[0] == 1
if not exists:
2014-02-13 12:47:01 +01:00
db.execute(clean_sql("""
2011-11-20 10:23:31 +01:00
create virtual table todos using fts4(
user,
text,
added,
tokenize=porter
)"""))
db.commit()
db_ready = True
2011-11-20 10:23:31 +01:00
def db_getall(db, nick, limit=-1):
return db.execute("""
select added, text
from todos
where lower(user) = lower(?)
order by added desc
limit ?
""", (nick, limit))
2014-02-14 05:03:08 +01:00
def db_get(db, nick, note_id):
2011-11-20 10:23:31 +01:00
return db.execute("""
select added, text from todos
where lower(user) = lower(?)
order by added desc
limit 1
offset ?
2014-02-14 05:03:08 +01:00
""", (nick, note_id)).fetchone()
2011-11-20 10:23:31 +01:00
def db_del(db, nick, limit='all'):
row = db.execute("""
delete from todos
where rowid in (
select rowid from todos
where lower(user) = lower(?)
order by added desc
limit ?
offset ?)
""", (nick,
2013-09-04 12:30:04 +02:00
-1 if limit == 'all' else 1,
0 if limit == 'all' else limit))
2011-11-20 10:23:31 +01:00
db.commit()
return row
def db_add(db, nick, text):
db.execute("""
insert into todos (user, text, added)
values (?, ?, CURRENT_TIMESTAMP)
""", (nick, text))
db.commit()
def db_search(db, nick, query):
return db.execute("""
select added, text
from todos
where todos match ?
and lower(user) = lower(?)
order by added desc
""", (query, nick))
2013-09-11 03:44:02 +02:00
@hook.command("notes")
2011-11-20 10:23:31 +01:00
@hook.command
2013-09-11 03:44:02 +02:00
def note(inp, nick='', chan='', db=None, notice=None, bot=None):
2014-02-13 12:47:01 +01:00
"""note(s) <add|del|list|search> args -- Manipulates your list of notes."""
2011-11-20 10:23:31 +01:00
db_init(db)
parts = inp.split()
cmd = parts[0].lower()
args = parts[1:]
2012-03-11 23:47:22 +01:00
# code to allow users to access each others factoids and a copy of help
2013-09-11 03:44:02 +02:00
# ".note (add|del|list|search) [@user] args -- Manipulates your list of todos."
2012-03-11 23:47:22 +01:00
#if len(args) and args[0].startswith("@"):
# nick = args[0][1:]
# args = args[1:]
2011-11-20 10:23:31 +01:00
if cmd == 'add':
if not len(args):
return "no text"
text = " ".join(args)
db_add(db, nick, text)
2013-09-11 03:44:02 +02:00
notice("Note added!")
2011-11-20 10:23:31 +01:00
return
elif cmd == 'get':
if len(args):
try:
index = int(args[0])
except ValueError:
notice("Invalid number format.")
return
else:
index = 0
row = db_get(db, nick, index)
if not row:
notice("No such entry.")
return
notice("[{}]: {}: {}".format(index, row[0], row[1]))
2011-11-20 10:23:31 +01:00
elif cmd == 'del' or cmd == 'delete' or cmd == 'remove':
if not len(args):
return "error"
if args[0] == 'all':
index = 'all'
else:
try:
index = int(args[0])
except ValueError:
notice("Invalid number.")
return
rows = db_del(db, nick, index)
notice("Deleted {} entries".format(rows.rowcount))
2011-11-20 10:23:31 +01:00
elif cmd == 'list':
limit = -1
if len(args):
try:
limit = int(args[0])
limit = max(-1, limit)
except ValueError:
notice("Invalid number.")
return
rows = db_getall(db, nick, limit)
found = False
for (index, row) in enumerate(rows):
notice("[{}]: {}: {}".format(index, row[0], row[1]))
2011-11-20 10:23:31 +01:00
found = True
if not found:
notice("{} has no entries.".format(nick))
2011-11-20 10:23:31 +01:00
elif cmd == 'search':
if not len(args):
notice("No search query given!")
return
query = " ".join(args)
rows = db_search(db, nick, query)
found = False
for (index, row) in enumerate(rows):
notice("[{}]: {}: {}".format(index, row[0], row[1]))
2011-11-20 10:23:31 +01:00
found = True
if not found:
notice("{} has no matching entries for: {}".format(nick, query))
2011-11-20 10:23:31 +01:00
else:
notice("Unknown command: {}".format(cmd))