From 901ddf761ac6e3b344f4b0a1f07087da2e873dbd Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 30 Aug 2012 23:15:28 -0400 Subject: [PATCH 01/11] Starting work --- earwigbot/commands/notes.py | 269 ++++++++++++++++++++++++-------------------- 1 file changed, 146 insertions(+), 123 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index ec9b817..e1ed3a0 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -20,148 +20,171 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from os import path +import sqlite3 as sqlite +from threading import Lock + from earwigbot.commands import Command class Notes(Command): """A mini IRC-based wiki for storing notes, tips, and reminders.""" name = "notes" + commands = ["notes", "note", "about"] + + def setup(self): + self._dbfile = path.join(self.config.root_dir, "notes.db") + self._db_access_lock = Lock() def process(self, data): pass + def create_db(self, conn): + """Initialize the notes database with its necessary tables.""" + script = """ + CREATE TABLE pages (page_id, page_title); + CREATE TABLE users (user_id, user_name); + CREATE TABLE revisions (rev_id, rev_page, rev_user, rev_content); + """ + conn.executescript(script) + + + + + + + class OldCommand(object): def parse(self): - if command == "notes" or command == "note" or command == "about" or command == "data" or command == "database": - try: - action = line2[4] - except BaseException: - reply("What do you want me to do? Type \"!notes help\" for more information.", chan, nick) + try: + action = line2[4] + except BaseException: + reply("What do you want me to do? Type \"!notes help\" for more information.", chan, nick) + return + import MySQLdb + db = MySQLdb.connect(db="u_earwig_ircbot", host="sql", read_default_file="/home/earwig/.my.cnf") + specify = ' '.join(line2[5:]) + if action == "help" or action == "manual": + shortCommandList = "read, write, change, undo, delete, move, author, category, list, report, developer" + if specify == "read": + say("To read an entry, type \"!notes read \".", chan) + elif specify == "write": + say("To write a new entry, type \"!notes write \". This will create a new entry only if one does not exist, see the below command...", chan) + elif specify == "change": + say("To change an entry, type \"!notes change \". The old entry will be stored in the database, so it can be undone later.", chan) + elif specify == "undo": + say("To undo a change, type \"!notes undo \".", chan) + elif specify == "delete": + say("To delete an entry, type \"!notes delete \". For security reasons, only bot admins can do this.", chan) + elif specify == "move": + say("To move an entry, type \"!notes move \".", chan) + elif specify == "author": + say("To return the author of an entry, type \"!notes author \".", chan) + elif specify == "category" or specify == "cat": + say("To change an entry's category, type \"!notes category \".", chan) + elif specify == "list": + say("To list all categories in the database, type \"!notes list\". Type \"!notes list \" to get all entries in a certain category.", chan) + elif specify == "report": + say("To give some statistics about the mini-wiki, including some debugging information, type \"!notes report\" in a PM.", chan) + elif specify == "developer": + say("To do developer work, such as writing to the database directly, type \"!notes developer \". This can only be done by the bot owner.", chan) + else: + db.query("SELECT * FROM version;") + r = db.use_result() + data = r.fetch_row(0) + version = data[0] + reply("The Earwig Mini-Wiki: running v%s." % version, chan, nick) + reply("The full list of commands, for reference, are: %s." % shortCommandList, chan, nick) + reply("For an explaination of a certain command, type \"!notes help \".", chan, nick) + reply("You can also access the database from the Toolserver: http://toolserver.org/~earwig/cgi-bin/irc_database.py", chan, nick) + time.sleep(0.4) + return + elif action == "read": + specify = string.lower(specify) + if " " in specify: specify = string.split(specify, " ")[0] + if not specify or "\"" in specify: + reply("Please include the name of the entry you would like to read after the command, e.g. !notes read earwig", chan, nick) return - import MySQLdb - db = MySQLdb.connect(db="u_earwig_ircbot", host="sql", read_default_file="/home/earwig/.my.cnf") - specify = ' '.join(line2[5:]) - if action == "help" or action == "manual": - shortCommandList = "read, write, change, undo, delete, move, author, category, list, report, developer" - if specify == "read": - say("To read an entry, type \"!notes read \".", chan) - elif specify == "write": - say("To write a new entry, type \"!notes write \". This will create a new entry only if one does not exist, see the below command...", chan) - elif specify == "change": - say("To change an entry, type \"!notes change \". The old entry will be stored in the database, so it can be undone later.", chan) - elif specify == "undo": - say("To undo a change, type \"!notes undo \".", chan) - elif specify == "delete": - say("To delete an entry, type \"!notes delete \". For security reasons, only bot admins can do this.", chan) - elif specify == "move": - say("To move an entry, type \"!notes move \".", chan) - elif specify == "author": - say("To return the author of an entry, type \"!notes author \".", chan) - elif specify == "category" or specify == "cat": - say("To change an entry's category, type \"!notes category \".", chan) - elif specify == "list": - say("To list all categories in the database, type \"!notes list\". Type \"!notes list \" to get all entries in a certain category.", chan) - elif specify == "report": - say("To give some statistics about the mini-wiki, including some debugging information, type \"!notes report\" in a PM.", chan) - elif specify == "developer": - say("To do developer work, such as writing to the database directly, type \"!notes developer \". This can only be done by the bot owner.", chan) - else: - db.query("SELECT * FROM version;") - r = db.use_result() - data = r.fetch_row(0) - version = data[0] - reply("The Earwig Mini-Wiki: running v%s." % version, chan, nick) - reply("The full list of commands, for reference, are: %s." % shortCommandList, chan, nick) - reply("For an explaination of a certain command, type \"!notes help \".", chan, nick) - reply("You can also access the database from the Toolserver: http://toolserver.org/~earwig/cgi-bin/irc_database.py", chan, nick) - time.sleep(0.4) + try: + db.query("SELECT entry_content FROM entries WHERE entry_title = \"%s\";" % specify) + r = db.use_result() + data = r.fetch_row(0) + entry = data[0][0] + say("Entry \"\x02%s\x0F\": %s" % (specify, entry), chan) + except Exception: + reply("There is no entry titled \"\x02%s\x0F\"." % specify, chan, nick) + return + elif action == "delete" or action == "remove": + specify = string.lower(specify) + if " " in specify: specify = string.split(specify, " ")[0] + if not specify or "\"" in specify: + reply("Please include the name of the entry you would like to delete after the command, e.g. !notes delete earwig", chan, nick) return - elif action == "read": - specify = string.lower(specify) - if " " in specify: specify = string.split(specify, " ")[0] - if not specify or "\"" in specify: - reply("Please include the name of the entry you would like to read after the command, e.g. !notes read earwig", chan, nick) - return + if authy == "owner" or authy == "admin": try: - db.query("SELECT entry_content FROM entries WHERE entry_title = \"%s\";" % specify) + db.query("DELETE from entries where entry_title = \"%s\";" % specify) r = db.use_result() - data = r.fetch_row(0) - entry = data[0][0] - say("Entry \"\x02%s\x0F\": %s" % (specify, entry), chan) - except Exception: - reply("There is no entry titled \"\x02%s\x0F\"." % specify, chan, nick) - return - elif action == "delete" or action == "remove": - specify = string.lower(specify) - if " " in specify: specify = string.split(specify, " ")[0] - if not specify or "\"" in specify: - reply("Please include the name of the entry you would like to delete after the command, e.g. !notes delete earwig", chan, nick) - return - if authy == "owner" or authy == "admin": - try: - db.query("DELETE from entries where entry_title = \"%s\";" % specify) - r = db.use_result() - db.commit() - reply("The entry on \"\x02%s\x0F\" has been removed." % specify, chan, nick) - except Exception: - phenny.reply("Unable to remove the entry on \"\x02%s\x0F\", because it doesn't exist." % specify, chan, nick) - else: - reply("Only bot admins can remove entries.", chan, nick) - return - elif action == "developer": - if authy == "owner": - db.query(specify) - r = db.use_result() - try: - print r.fetch_row(0) - except Exception: - pass db.commit() - reply("Done.", chan, nick) - else: - reply("Only the bot owner can modify the raw database.", chan, nick) - return - elif action == "write": - try: - write = line2[5] - content = ' '.join(line2[6:]) + reply("The entry on \"\x02%s\x0F\" has been removed." % specify, chan, nick) except Exception: - reply("Please include some content in your entry.", chan, nick) - return - db.query("SELECT * from entries WHERE entry_title = \"%s\";" % write) + phenny.reply("Unable to remove the entry on \"\x02%s\x0F\", because it doesn't exist." % specify, chan, nick) + else: + reply("Only bot admins can remove entries.", chan, nick) + return + elif action == "developer": + if authy == "owner": + db.query(specify) r = db.use_result() - data = r.fetch_row(0) - if data: - reply("An entry on %s already exists; please use \"!notes change %s %s\"." % (write, write, content), chan, nick) - return - content2 = content.replace('"', '\\' + '"') - db.query("INSERT INTO entries (entry_title, entry_author, entry_category, entry_content, entry_content_old) VALUES (\"%s\", \"%s\", \"uncategorized\", \"%s\", NULL);" % (write, nick, content2)) - db.commit() - reply("You have written an entry titled \"\x02%s\x0F\", with the following content: \"%s\"" % (write, content), chan, nick) - return - elif action == "change": - reply("NotImplementedError", chan, nick) - elif action == "undo": - reply("NotImplementedError", chan, nick) - elif action == "move": - reply("NotImplementedError", chan, nick) - elif action == "author": try: - entry = line2[5] + print r.fetch_row(0) except Exception: - reply("Please include the name of the entry you would like to get information for after the command, e.g. !notes author earwig", chan, nick) - return - db.query("SELECT entry_author from entries WHERE entry_title = \"%s\";" % entry) - r = db.use_result() - data = r.fetch_row(0) - if data: - say("The author of \"\x02%s\x0F\" is \x02%s\x0F." % (entry, data[0][0]), chan) - return - reply("There is no entry titled \"\x02%s\x0F\"." % entry, chan, nick) + pass + db.commit() + reply("Done.", chan, nick) + else: + reply("Only the bot owner can modify the raw database.", chan, nick) + return + elif action == "write": + try: + write = line2[5] + content = ' '.join(line2[6:]) + except Exception: + reply("Please include some content in your entry.", chan, nick) + return + db.query("SELECT * from entries WHERE entry_title = \"%s\";" % write) + r = db.use_result() + data = r.fetch_row(0) + if data: + reply("An entry on %s already exists; please use \"!notes change %s %s\"." % (write, write, content), chan, nick) + return + content2 = content.replace('"', '\\' + '"') + db.query("INSERT INTO entries (entry_title, entry_author, entry_category, entry_content, entry_content_old) VALUES (\"%s\", \"%s\", \"uncategorized\", \"%s\", NULL);" % (write, nick, content2)) + db.commit() + reply("You have written an entry titled \"\x02%s\x0F\", with the following content: \"%s\"" % (write, content), chan, nick) + return + elif action == "change": + reply("NotImplementedError", chan, nick) + elif action == "undo": + reply("NotImplementedError", chan, nick) + elif action == "move": + reply("NotImplementedError", chan, nick) + elif action == "author": + try: + entry = line2[5] + except Exception: + reply("Please include the name of the entry you would like to get information for after the command, e.g. !notes author earwig", chan, nick) + return + db.query("SELECT entry_author from entries WHERE entry_title = \"%s\";" % entry) + r = db.use_result() + data = r.fetch_row(0) + if data: + say("The author of \"\x02%s\x0F\" is \x02%s\x0F." % (entry, data[0][0]), chan) return - elif action == "cat" or action == "category": - reply("NotImplementedError", chan, nick) - elif action == "list": - reply("NotImplementedError", chan, nick) - elif action == "report": - reply("NotImplementedError", chan, nick) + reply("There is no entry titled \"\x02%s\x0F\"." % entry, chan, nick) + return + elif action == "cat" or action == "category": + reply("NotImplementedError", chan, nick) + elif action == "list": + reply("NotImplementedError", chan, nick) + elif action == "report": + reply("NotImplementedError", chan, nick) From caf27c758820ad759ff9a19817db60da341d3b28 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Thu, 30 Aug 2012 23:51:12 -0400 Subject: [PATCH 02/11] More work on notes. --- earwigbot/commands/notes.py | 168 +++++++++----------------------------------- 1 file changed, 32 insertions(+), 136 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index e1ed3a0..9fa099c 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -30,13 +30,32 @@ class Notes(Command): """A mini IRC-based wiki for storing notes, tips, and reminders.""" name = "notes" commands = ["notes", "note", "about"] + version = 2 def setup(self): self._dbfile = path.join(self.config.root_dir, "notes.db") self._db_access_lock = Lock() def process(self, data): - pass + commands = { + "list": self.do_list, + "read": self.do_read, + "edit": self.do_edit, + "rename": self.do_rename, + "delete": self.do_delete, + "info": self.do_info, + } + + if not data.args: + msg = "The Earwig Mini-Wiki: running v{0}. Subcommands are: {1}. You can get help on any with '!{2} help subcommand'." + cmnds = ", ".join((commands)) + self.reply(data, msg.format(self.version, cmnds, data.command)) + return + command = data.args[0].lower() + if command in commands: + commands[command](data) + else: + self.reply("Unknown subcommand: \x0303{0}\x0F.".format(command)) def create_db(self, conn): """Initialize the notes database with its necessary tables.""" @@ -47,144 +66,21 @@ class Notes(Command): """ conn.executescript(script) + def do_list(self): + pass + def do_read(self): + pass + def edit(self): + pass + def rename(self): + pass + def delete(self): + pass + def info(self): + pass - -class OldCommand(object): - def parse(self): - try: - action = line2[4] - except BaseException: - reply("What do you want me to do? Type \"!notes help\" for more information.", chan, nick) - return - import MySQLdb - db = MySQLdb.connect(db="u_earwig_ircbot", host="sql", read_default_file="/home/earwig/.my.cnf") - specify = ' '.join(line2[5:]) - if action == "help" or action == "manual": - shortCommandList = "read, write, change, undo, delete, move, author, category, list, report, developer" - if specify == "read": - say("To read an entry, type \"!notes read \".", chan) - elif specify == "write": - say("To write a new entry, type \"!notes write \". This will create a new entry only if one does not exist, see the below command...", chan) - elif specify == "change": - say("To change an entry, type \"!notes change \". The old entry will be stored in the database, so it can be undone later.", chan) - elif specify == "undo": - say("To undo a change, type \"!notes undo \".", chan) - elif specify == "delete": - say("To delete an entry, type \"!notes delete \". For security reasons, only bot admins can do this.", chan) - elif specify == "move": - say("To move an entry, type \"!notes move \".", chan) - elif specify == "author": - say("To return the author of an entry, type \"!notes author \".", chan) - elif specify == "category" or specify == "cat": - say("To change an entry's category, type \"!notes category \".", chan) - elif specify == "list": - say("To list all categories in the database, type \"!notes list\". Type \"!notes list \" to get all entries in a certain category.", chan) - elif specify == "report": - say("To give some statistics about the mini-wiki, including some debugging information, type \"!notes report\" in a PM.", chan) - elif specify == "developer": - say("To do developer work, such as writing to the database directly, type \"!notes developer \". This can only be done by the bot owner.", chan) - else: - db.query("SELECT * FROM version;") - r = db.use_result() - data = r.fetch_row(0) - version = data[0] - reply("The Earwig Mini-Wiki: running v%s." % version, chan, nick) - reply("The full list of commands, for reference, are: %s." % shortCommandList, chan, nick) - reply("For an explaination of a certain command, type \"!notes help \".", chan, nick) - reply("You can also access the database from the Toolserver: http://toolserver.org/~earwig/cgi-bin/irc_database.py", chan, nick) - time.sleep(0.4) - return - elif action == "read": - specify = string.lower(specify) - if " " in specify: specify = string.split(specify, " ")[0] - if not specify or "\"" in specify: - reply("Please include the name of the entry you would like to read after the command, e.g. !notes read earwig", chan, nick) - return - try: - db.query("SELECT entry_content FROM entries WHERE entry_title = \"%s\";" % specify) - r = db.use_result() - data = r.fetch_row(0) - entry = data[0][0] - say("Entry \"\x02%s\x0F\": %s" % (specify, entry), chan) - except Exception: - reply("There is no entry titled \"\x02%s\x0F\"." % specify, chan, nick) - return - elif action == "delete" or action == "remove": - specify = string.lower(specify) - if " " in specify: specify = string.split(specify, " ")[0] - if not specify or "\"" in specify: - reply("Please include the name of the entry you would like to delete after the command, e.g. !notes delete earwig", chan, nick) - return - if authy == "owner" or authy == "admin": - try: - db.query("DELETE from entries where entry_title = \"%s\";" % specify) - r = db.use_result() - db.commit() - reply("The entry on \"\x02%s\x0F\" has been removed." % specify, chan, nick) - except Exception: - phenny.reply("Unable to remove the entry on \"\x02%s\x0F\", because it doesn't exist." % specify, chan, nick) - else: - reply("Only bot admins can remove entries.", chan, nick) - return - elif action == "developer": - if authy == "owner": - db.query(specify) - r = db.use_result() - try: - print r.fetch_row(0) - except Exception: - pass - db.commit() - reply("Done.", chan, nick) - else: - reply("Only the bot owner can modify the raw database.", chan, nick) - return - elif action == "write": - try: - write = line2[5] - content = ' '.join(line2[6:]) - except Exception: - reply("Please include some content in your entry.", chan, nick) - return - db.query("SELECT * from entries WHERE entry_title = \"%s\";" % write) - r = db.use_result() - data = r.fetch_row(0) - if data: - reply("An entry on %s already exists; please use \"!notes change %s %s\"." % (write, write, content), chan, nick) - return - content2 = content.replace('"', '\\' + '"') - db.query("INSERT INTO entries (entry_title, entry_author, entry_category, entry_content, entry_content_old) VALUES (\"%s\", \"%s\", \"uncategorized\", \"%s\", NULL);" % (write, nick, content2)) - db.commit() - reply("You have written an entry titled \"\x02%s\x0F\", with the following content: \"%s\"" % (write, content), chan, nick) - return - elif action == "change": - reply("NotImplementedError", chan, nick) - elif action == "undo": - reply("NotImplementedError", chan, nick) - elif action == "move": - reply("NotImplementedError", chan, nick) - elif action == "author": - try: - entry = line2[5] - except Exception: - reply("Please include the name of the entry you would like to get information for after the command, e.g. !notes author earwig", chan, nick) - return - db.query("SELECT entry_author from entries WHERE entry_title = \"%s\";" % entry) - r = db.use_result() - data = r.fetch_row(0) - if data: - say("The author of \"\x02%s\x0F\" is \x02%s\x0F." % (entry, data[0][0]), chan) - return - reply("There is no entry titled \"\x02%s\x0F\"." % entry, chan, nick) - return - elif action == "cat" or action == "category": - reply("NotImplementedError", chan, nick) - elif action == "list": - reply("NotImplementedError", chan, nick) - elif action == "report": - reply("NotImplementedError", chan, nick) From 865859b136715ea8186f70754b7c2b0dcd250c67 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 00:05:31 -0400 Subject: [PATCH 03/11] Implement do_list(), do_read(); update schema. --- earwigbot/commands/notes.py | 55 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 12 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 9fa099c..1aeea6a 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -47,7 +47,7 @@ class Notes(Command): } if not data.args: - msg = "The Earwig Mini-Wiki: running v{0}. Subcommands are: {1}. You can get help on any with '!{2} help subcommand'." + msg = "\x0302The Earwig Mini-Wiki\x0F: running v{0}. Subcommands are: {1}. You can get help on any with '!{2} help subcommand'." cmnds = ", ".join((commands)) self.reply(data, msg.format(self.version, cmnds, data.command)) return @@ -60,27 +60,58 @@ class Notes(Command): def create_db(self, conn): """Initialize the notes database with its necessary tables.""" script = """ - CREATE TABLE pages (page_id, page_title); + CREATE TABLE entries (entry_id, entry_slug, entry_title, entry_revision); CREATE TABLE users (user_id, user_name); - CREATE TABLE revisions (rev_id, rev_page, rev_user, rev_content); + CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_content); """ conn.executescript(script) - def do_list(self): - pass + def do_list(self, data): + """Show a list of entries in the notes database.""" + query = "SELECT entry_title FROM entries" + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + try: + entries = conn.execute(query).fetchall() + except sqlite.OperationalError: + entires = [] - def do_read(self): - pass + if entries: + self.reply(data, "Entries: {0}".format(", ".join(entries))) + else: + self.reply(data, "No entries in the database.") - def edit(self): - pass + def do_read(self, data): + """Read an entry from the notes database.""" + query = "SELECT entry_title, rev_content FROM entries INNER JOIN revisions ON entry_revision = rev_id WHERE entry_slug = ?" + try: + slug = data.args[1].lower().replace("_", "").replace("-", "") + except IndexError: + self.reply(data, "Please name an entry to read from.") + return - def rename(self): + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + try: + title, content = conn.execute(query, (slug,)).fetchone() + except sqlite.OperationalError: + title, content = slug, None + + if content: + self.reply(data, "\x0302{0}\x0F: {1}".format(title, content)) + else: + self.reply(data, "Entry \x0302{0}\x0F not found.".format(title)) + + def do_edit(self, data): + """Edit an entry in the notes database.""" pass - def delete(self): + def do_rename(self, data): + """Rename an entry in the notes database.""" pass - def info(self): + def do_delete(self, data): + """Delete an entry from the notes database.""" pass + def do_info(self, data): + """Get info on an entry in the notes database.""" + pass From 6e27d9d7eb63d8b784d49e951b766802a7ce5e1c Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 00:25:33 -0400 Subject: [PATCH 04/11] Implement do_info(). --- earwigbot/commands/notes.py | 52 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 1aeea6a..00ca631 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -41,9 +41,10 @@ class Notes(Command): "list": self.do_list, "read": self.do_read, "edit": self.do_edit, + "info": self.do_info, + "history": self.do_history, "rename": self.do_rename, "delete": self.do_delete, - "info": self.do_info, } if not data.args: @@ -61,8 +62,8 @@ class Notes(Command): """Initialize the notes database with its necessary tables.""" script = """ CREATE TABLE entries (entry_id, entry_slug, entry_title, entry_revision); - CREATE TABLE users (user_id, user_name); - CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_content); + CREATE TABLE users (user_id, user_host); + CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_timestamp, rev_content); """ conn.executescript(script) @@ -82,7 +83,9 @@ class Notes(Command): def do_read(self, data): """Read an entry from the notes database.""" - query = "SELECT entry_title, rev_content FROM entries INNER JOIN revisions ON entry_revision = rev_id WHERE entry_slug = ?" + query = """SELECT entry_title, rev_content FROM entries + INNER JOIN revisions ON entry_revision = rev_id + WHERE entry_slug = ?""" try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: @@ -104,6 +107,43 @@ class Notes(Command): """Edit an entry in the notes database.""" pass + def do_info(self, data): + """Get info on an entry in the notes database.""" + pass + + def do_history(self, data): + """Get the history of an entry in the notes database.""" + query = """SELECT entry_title, rev_timestamp, user_host FROM entries + INNER JOIN revisions ON entry_revision = rev_id + INNER JOIN users ON rev_user = user_id + WHERE entry_slug = ?""" + try: + slug = data.args[1].lower().replace("_", "").replace("-", "") + except IndexError: + self.reply(data, "Please name an entry to get the history of.") + return + + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + try: + data = conn.execute(query, (slug,)).fetchall() + except sqlite.OperationalError: + data = [] + + if data: + title = data[0][0] + times = [datum[1] for datum in data] + earliest = min(times).strftime("%b %d, %Y %H:%M:%S") + msg = "\x0302{0}\x0F: {1} edits since {2}" + msg = msg.format(title, len(data), earliest) + if len(times) > 1: + latest = max(times).strftime("%b %d, %Y %H:%M:%S") + msg += "; last edit on {0}".format(lastest) + names = [datum[2] for datum in data] + msg += "; authors: {0}.".format(", ".join(list(set(names)))) + self.reply(data, msg) + else: + self.reply(data, "Entry \x0302{0}\x0F not found.".format(title)) + def do_rename(self, data): """Rename an entry in the notes database.""" pass @@ -111,7 +151,3 @@ class Notes(Command): def do_delete(self, data): """Delete an entry from the notes database.""" pass - - def do_info(self, data): - """Get info on an entry in the notes database.""" - pass From 5e155d9a6a2e95c94508ce48ab8ccadc6efbf04d Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:17:02 -0400 Subject: [PATCH 05/11] Implement do_edit(). --- earwigbot/commands/notes.py | 101 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 83 insertions(+), 18 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 00ca631..8672bbd 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -20,6 +20,7 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. +from datetime import datetime from os import path import sqlite3 as sqlite from threading import Lock @@ -42,7 +43,6 @@ class Notes(Command): "read": self.do_read, "edit": self.do_edit, "info": self.do_info, - "history": self.do_history, "rename": self.do_rename, "delete": self.do_delete, } @@ -58,15 +58,6 @@ class Notes(Command): else: self.reply("Unknown subcommand: \x0303{0}\x0F.".format(command)) - def create_db(self, conn): - """Initialize the notes database with its necessary tables.""" - script = """ - CREATE TABLE entries (entry_id, entry_slug, entry_title, entry_revision); - CREATE TABLE users (user_id, user_host); - CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_timestamp, rev_content); - """ - conn.executescript(script) - def do_list(self, data): """Show a list of entries in the notes database.""" query = "SELECT entry_title FROM entries" @@ -95,7 +86,7 @@ class Notes(Command): with sqlite.connect(self._dbfile) as conn, self._db_access_lock: try: title, content = conn.execute(query, (slug,)).fetchone() - except sqlite.OperationalError: + except (sqlite.OperationalError, TypeError): title, content = slug, None if content: @@ -105,22 +96,60 @@ class Notes(Command): def do_edit(self, data): """Edit an entry in the notes database.""" - pass + query1 = """SELECT entry_id, entry_title, user_host FROM entries + INNER JOIN revisions ON entry_revision = rev_id + INNER JOIN users ON rev_user = user_id + WHERE entry_slug = ?""" + query2 = "INSERT INTO revisions VALUES (?, ?, ?, ?, ?)" + query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)" + query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?" + try: + slug = data.args[1].lower().replace("_", "").replace("-", "") + except IndexError: + self.reply(data, "Please name an entry to edit.") + return + content = " ".join(data.args[2:]).strip() + if not content: + self.reply(data, "Please give some content to put in the entry.") + return + + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + create = True + try: + id_, title, author = conn.execute(query1, (slug,)).fetchone() + create = False + except sqlite.OperationalError: + id_, title, author = 1, data.args[1], data.host + self.create_db(conn) + except TypeError: + id_ = self.get_next_entry(conn) + title, author = data.args[1], data.host + permdb = self.config.irc["permissions"] + if author != data.host and not permdb.is_admin(data): + msg = "You must be an author or a bot admin to edit this entry." + self.reply(data, msg) + return + revid = self.get_next_revision(conn) + userid = self.get_user(conn, data.host) + now = datetime.utcnow() + conn.execute(query2, (revid, id_, userid, now, content)) + if create: + conn.execute(query3, (id_, slug, title, revid)) + else: + conn.execute(query4, (revid, id_)) + + self.reply(data, "Entry \x0302{0}\x0F updated.".format(title)) def do_info(self, data): """Get info on an entry in the notes database.""" - pass - - def do_history(self, data): - """Get the history of an entry in the notes database.""" query = """SELECT entry_title, rev_timestamp, user_host FROM entries - INNER JOIN revisions ON entry_revision = rev_id + INNER JOIN revisions ON entry_id = rev_entry INNER JOIN users ON rev_user = user_id WHERE entry_slug = ?""" try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: - self.reply(data, "Please name an entry to get the history of.") + self.reply(data, "Please name an entry to get info on.") return with sqlite.connect(self._dbfile) as conn, self._db_access_lock: @@ -151,3 +180,39 @@ class Notes(Command): def do_delete(self, data): """Delete an entry from the notes database.""" pass + + def create_db(self, conn): + """Initialize the notes database with its necessary tables.""" + script = """ + CREATE TABLE entries (entry_id, entry_slug, entry_title, + entry_revision); + CREATE TABLE users (user_id, user_host); + CREATE TABLE revisions (rev_id, rev_entry, rev_user, rev_timestamp, + rev_content); + """ + conn.executescript(script) + + def get_next_entry(self, conn): + """Get the next entry ID.""" + query = "SELECT MAX(entry_id) FROM entries" + next = conn.execute(query).fetchone()[0] + return next + 1 if next else 1 + + def get_next_revision(self, conn): + """Get the next revision ID.""" + query = "SELECT MAX(rev_id) FROM revisions" + next = conn.execute(query).fetchone()[0] + return next + 1 if next else 1 + + def get_user(self, conn, host): + """Get the user ID corresponding to a hostname, or make one.""" + query1 = "SELECT user_host FROM users WHERE user_id = ?" + query2 = "SELECT MAX(user_id) FROM users" + query3 = "INSERT INTO users VALUES (?, ?)" + user = conn.execute(query1).fetchone()[0] + if user: + return user + last = conn.execute(query2).fetchone()[0] + next = last + 1 if last else 1 + conn.execute(query3, (next, host)) + return next From 6114e9e2abd46690edcf29fdd5bd7f87a629eb11 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:27:51 -0400 Subject: [PATCH 06/11] Implement do_rename(). --- earwigbot/commands/notes.py | 47 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 5 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 8672bbd..dfaf1f1 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -80,7 +80,7 @@ class Notes(Command): try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: - self.reply(data, "Please name an entry to read from.") + self.reply(data, "Please specify an entry to read from.") return with sqlite.connect(self._dbfile) as conn, self._db_access_lock: @@ -106,7 +106,7 @@ class Notes(Command): try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: - self.reply(data, "Please name an entry to edit.") + self.reply(data, "Please specify an entry to edit.") return content = " ".join(data.args[2:]).strip() if not content: @@ -149,7 +149,7 @@ class Notes(Command): try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: - self.reply(data, "Please name an entry to get info on.") + self.reply(data, "Please specify an entry to get info on.") return with sqlite.connect(self._dbfile) as conn, self._db_access_lock: @@ -171,15 +171,52 @@ class Notes(Command): msg += "; authors: {0}.".format(", ".join(list(set(names)))) self.reply(data, msg) else: + title = data.args[1] self.reply(data, "Entry \x0302{0}\x0F not found.".format(title)) def do_rename(self, data): """Rename an entry in the notes database.""" - pass + query1 = """SELECT entry_id, user_host FROM entries + INNER JOIN revisions ON entry_revision = rev_id + INNER JOIN users ON rev_user = user_id + WHERE entry_slug = ?""" + query2 = "UPDATE entries SET entry_title = ? WHERE entry_id = ?" + try: + slug = data.args[1].lower().replace("_", "").replace("-", "") + except IndexError: + self.reply(data, "Please specify an entry to rename.") + return + try: + newtitle = data.args[2] + except IndexError: + self.reply(data, "Please specify an entry to rename.") + return + if newtitle == data.args[1]: + self.reply(data, "The old and new titles are identical.") + return + + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + try: + id_, author = conn.execute(query1, (slug,)).fetchone() + except (sqlite.OperationalError, TypeError): + msg = "Entry \x0302{0}\x0F not found.".format(data.args[1]) + self.reply(data, msg) + return + if author != data.host and not permdb.is_admin(data): + msg = "You must be an author or a bot admin to rename this entry." + self.reply(data, msg) + return + conn.execute(query2, (newtitle, id_)) + msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F." + self.reply(data, msg.format(data.args[1], newtitle)) def do_delete(self, data): """Delete an entry from the notes database.""" - pass + try: + slug = data.args[1].lower().replace("_", "").replace("-", "") + except IndexError: + self.reply(data, "Please specify an entry to delete.") + return def create_db(self, conn): """Initialize the notes database with its necessary tables.""" From 275ac5d9e81f3d0f7ffbd6fcccf346be478f563b Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:30:46 -0400 Subject: [PATCH 07/11] Implement do_delete(). --- earwigbot/commands/notes.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index dfaf1f1..8674515 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -189,10 +189,10 @@ class Notes(Command): try: newtitle = data.args[2] except IndexError: - self.reply(data, "Please specify an entry to rename.") + self.reply(data, "Please specify a new name for the entry.") return if newtitle == data.args[1]: - self.reply(data, "The old and new titles are identical.") + self.reply(data, "The old and new names are identical.") return with sqlite.connect(self._dbfile) as conn, self._db_access_lock: @@ -207,17 +207,40 @@ class Notes(Command): self.reply(data, msg) return conn.execute(query2, (newtitle, id_)) + msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F." self.reply(data, msg.format(data.args[1], newtitle)) def do_delete(self, data): """Delete an entry from the notes database.""" + query1 = """SELECT entry_id, user_host FROM entries + INNER JOIN revisions ON entry_revision = rev_id + INNER JOIN users ON rev_user = user_id + WHERE entry_slug = ?""" + query2 = "DELETE FROM entries WHERE entry_id = ?" + query3 = "DELETE FROM revisions WHERE rev_entry = ?" try: slug = data.args[1].lower().replace("_", "").replace("-", "") except IndexError: self.reply(data, "Please specify an entry to delete.") return + with sqlite.connect(self._dbfile) as conn, self._db_access_lock: + try: + id_, author = conn.execute(query1, (slug,)).fetchone() + except (sqlite.OperationalError, TypeError): + msg = "Entry \x0302{0}\x0F not found.".format(data.args[1]) + self.reply(data, msg) + return + if author != data.host and not permdb.is_admin(data): + msg = "You must be an author or a bot admin to delete this entry." + self.reply(data, msg) + return + conn.execute(query2, (id_)) + conn.execute(query3, (id_)) + + self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1])) + def create_db(self, conn): """Initialize the notes database with its necessary tables.""" script = """ From 45d82cc7eb94bf8b8506a67491dd4d2fc1fbdb34 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:34:13 -0400 Subject: [PATCH 08/11] Fix a bunch of typos. --- earwigbot/commands/notes.py | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 8674515..66bce53 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -56,7 +56,8 @@ class Notes(Command): if command in commands: commands[command](data) else: - self.reply("Unknown subcommand: \x0303{0}\x0F.".format(command)) + msg = "Unknown subcommand: \x0303{0}\x0F.".format(command) + self.reply(data, msg) def do_list(self, data): """Show a list of entries in the notes database.""" @@ -65,7 +66,7 @@ class Notes(Command): try: entries = conn.execute(query).fetchall() except sqlite.OperationalError: - entires = [] + entries = [] if entries: self.reply(data, "Entries: {0}".format(", ".join(entries))) @@ -154,20 +155,20 @@ class Notes(Command): with sqlite.connect(self._dbfile) as conn, self._db_access_lock: try: - data = conn.execute(query, (slug,)).fetchall() + info = conn.execute(query, (slug,)).fetchall() except sqlite.OperationalError: - data = [] + info = [] - if data: - title = data[0][0] - times = [datum[1] for datum in data] + if info: + title = info[0][0] + times = [datum[1] for datum in info] earliest = min(times).strftime("%b %d, %Y %H:%M:%S") msg = "\x0302{0}\x0F: {1} edits since {2}" - msg = msg.format(title, len(data), earliest) + msg = msg.format(title, len(info), earliest) if len(times) > 1: latest = max(times).strftime("%b %d, %Y %H:%M:%S") - msg += "; last edit on {0}".format(lastest) - names = [datum[2] for datum in data] + msg += "; last edit on {0}".format(latest) + names = [datum[2] for datum in info] msg += "; authors: {0}.".format(", ".join(list(set(names)))) self.reply(data, msg) else: @@ -202,6 +203,7 @@ class Notes(Command): msg = "Entry \x0302{0}\x0F not found.".format(data.args[1]) self.reply(data, msg) return + permdb = self.config.irc["permissions"] if author != data.host and not permdb.is_admin(data): msg = "You must be an author or a bot admin to rename this entry." self.reply(data, msg) @@ -232,6 +234,7 @@ class Notes(Command): msg = "Entry \x0302{0}\x0F not found.".format(data.args[1]) self.reply(data, msg) return + permdb = self.config.irc["permissions"] if author != data.host and not permdb.is_admin(data): msg = "You must be an author or a bot admin to delete this entry." self.reply(data, msg) @@ -255,14 +258,14 @@ class Notes(Command): def get_next_entry(self, conn): """Get the next entry ID.""" query = "SELECT MAX(entry_id) FROM entries" - next = conn.execute(query).fetchone()[0] - return next + 1 if next else 1 + later = conn.execute(query).fetchone()[0] + return later + 1 if later else 1 def get_next_revision(self, conn): """Get the next revision ID.""" query = "SELECT MAX(rev_id) FROM revisions" - next = conn.execute(query).fetchone()[0] - return next + 1 if next else 1 + later = conn.execute(query).fetchone()[0] + return later + 1 if later else 1 def get_user(self, conn, host): """Get the user ID corresponding to a hostname, or make one.""" @@ -273,6 +276,6 @@ class Notes(Command): if user: return user last = conn.execute(query2).fetchone()[0] - next = last + 1 if last else 1 - conn.execute(query3, (next, host)) - return next + later = last + 1 if last else 1 + conn.execute(query3, (later, host)) + return later From 776bbb6dc700032b6eb4aa658241700c66f15367 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:47:10 -0400 Subject: [PATCH 09/11] Implement do_help(). --- earwigbot/commands/notes.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index 66bce53..c0fc691 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -39,6 +39,7 @@ class Notes(Command): def process(self, data): commands = { + "help": self.do_help, "list": self.do_list, "read": self.do_read, "edit": self.do_edit, @@ -59,6 +60,30 @@ class Notes(Command): msg = "Unknown subcommand: \x0303{0}\x0F.".format(command) self.reply(data, msg) + def do_help(self, data): + """Get help on a subcommand.""" + help = { + "help": "Get help on other subcommands.", + "list": "List existing entries." + "read": "Read an existing entry ('!notes read [name]')." + "edit": "Modify or create a new entry ('!notes edit name [entry content]...')." + "info": "Get information on an existing entry ('!notes info [name]')." + "rename": "Rename an existing entry ('!notes rename [old_name] [new_name]')." + "delete": "Delete an existing entry ('!notes delete [name]')." + } + + try: + command = data.args[1] + except IndexError: + self.reply(data, "Please specify a subcommand to get help on.") + return + try: + prefix = "\x0303{0}\x0F: ".format(command) + self.reply(data, prefix + help[command]) + except KeyError: + msg = "Unknown subcommand: \x0303{0}\x0F.".format(command) + self.reply(data, msg) + def do_list(self, data): """Show a list of entries in the notes database.""" query = "SELECT entry_title FROM entries" From 8c654d1869fd0e1e73a1550f919de1e194951bbb Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 01:52:46 -0400 Subject: [PATCH 10/11] Fixes for do_help(). --- earwigbot/commands/notes.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index c0fc691..a54bd84 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -22,6 +22,7 @@ from datetime import datetime from os import path +import re import sqlite3 as sqlite from threading import Lock @@ -62,14 +63,20 @@ class Notes(Command): def do_help(self, data): """Get help on a subcommand.""" - help = { + info = { "help": "Get help on other subcommands.", - "list": "List existing entries." - "read": "Read an existing entry ('!notes read [name]')." - "edit": "Modify or create a new entry ('!notes edit name [entry content]...')." - "info": "Get information on an existing entry ('!notes info [name]')." - "rename": "Rename an existing entry ('!notes rename [old_name] [new_name]')." - "delete": "Delete an existing entry ('!notes delete [name]')." + "list": "List existing entries.", + "read": "Read an existing entry ('!notes read [name]').", + "edit": """Modify or create a new entry ('!notes edit name + [entry content]...'). If modifying, you must be the + entry author or a bot admin.""", + "info": """Get information on an existing entry ('!notes info + [name]').""", + "rename": """Rename an existing entry ('!notes rename [old_name] + [new_name]'). You must be the entry author or a bot + admin.""", + "delete": """Delete an existing entry ('!notes delete [name]'). You + must be the entry author or a bot admin.""", } try: @@ -78,8 +85,8 @@ class Notes(Command): self.reply(data, "Please specify a subcommand to get help on.") return try: - prefix = "\x0303{0}\x0F: ".format(command) - self.reply(data, prefix + help[command]) + help_ = re.sub(r"\s\s+", " ", info[command].replace("\n", "")) + self.reply(data, "\x0303{0}\x0F: ".format(command) + help_) except KeyError: msg = "Unknown subcommand: \x0303{0}\x0F.".format(command) self.reply(data, msg) From 2a09064767d574e98efe2e0d7b37042591b807f4 Mon Sep 17 00:00:00 2001 From: Ben Kurtovic Date: Fri, 31 Aug 2012 02:14:36 -0400 Subject: [PATCH 11/11] Updates and bugfixes. --- earwigbot/commands/notes.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/earwigbot/commands/notes.py b/earwigbot/commands/notes.py index a54bd84..a430ae7 100644 --- a/earwigbot/commands/notes.py +++ b/earwigbot/commands/notes.py @@ -101,6 +101,7 @@ class Notes(Command): entries = [] if entries: + entries = [entry[0] for entry in entries] self.reply(data, "Entries: {0}".format(", ".join(entries))) else: self.reply(data, "No entries in the database.") @@ -111,7 +112,7 @@ class Notes(Command): INNER JOIN revisions ON entry_revision = rev_id WHERE entry_slug = ?""" try: - slug = data.args[1].lower().replace("_", "").replace("-", "") + slug = self.slugify(data.args[1]) except IndexError: self.reply(data, "Please specify an entry to read from.") return @@ -137,7 +138,7 @@ class Notes(Command): query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)" query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?" try: - slug = data.args[1].lower().replace("_", "").replace("-", "") + slug = self.slugify(data.args[1]) except IndexError: self.reply(data, "Please specify an entry to edit.") return @@ -164,7 +165,7 @@ class Notes(Command): return revid = self.get_next_revision(conn) userid = self.get_user(conn, data.host) - now = datetime.utcnow() + now = datetime.utcnow().strftime("%b %d, %Y %H:%M:%S") conn.execute(query2, (revid, id_, userid, now, content)) if create: conn.execute(query3, (id_, slug, title, revid)) @@ -180,7 +181,7 @@ class Notes(Command): INNER JOIN users ON rev_user = user_id WHERE entry_slug = ?""" try: - slug = data.args[1].lower().replace("_", "").replace("-", "") + slug = self.slugify(data.args[1]) except IndexError: self.reply(data, "Please specify an entry to get info on.") return @@ -194,11 +195,11 @@ class Notes(Command): if info: title = info[0][0] times = [datum[1] for datum in info] - earliest = min(times).strftime("%b %d, %Y %H:%M:%S") + earliest = min(times) msg = "\x0302{0}\x0F: {1} edits since {2}" msg = msg.format(title, len(info), earliest) if len(times) > 1: - latest = max(times).strftime("%b %d, %Y %H:%M:%S") + latest = max(times) msg += "; last edit on {0}".format(latest) names = [datum[2] for datum in info] msg += "; authors: {0}.".format(", ".join(list(set(names)))) @@ -213,9 +214,10 @@ class Notes(Command): INNER JOIN revisions ON entry_revision = rev_id INNER JOIN users ON rev_user = user_id WHERE entry_slug = ?""" - query2 = "UPDATE entries SET entry_title = ? WHERE entry_id = ?" + query2 = """UPDATE entries SET entry_slug = ?, entry_title = ? + WHERE entry_id = ?""" try: - slug = data.args[1].lower().replace("_", "").replace("-", "") + slug = self.slugify(data.args[1]) except IndexError: self.reply(data, "Please specify an entry to rename.") return @@ -240,7 +242,7 @@ class Notes(Command): msg = "You must be an author or a bot admin to rename this entry." self.reply(data, msg) return - conn.execute(query2, (newtitle, id_)) + conn.execute(query2, (self.slugify(newtitle), newtitle, id_)) msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F." self.reply(data, msg.format(data.args[1], newtitle)) @@ -254,7 +256,7 @@ class Notes(Command): query2 = "DELETE FROM entries WHERE entry_id = ?" query3 = "DELETE FROM revisions WHERE rev_entry = ?" try: - slug = data.args[1].lower().replace("_", "").replace("-", "") + slug = self.slugify(data.args[1]) except IndexError: self.reply(data, "Please specify an entry to delete.") return @@ -271,11 +273,15 @@ class Notes(Command): msg = "You must be an author or a bot admin to delete this entry." self.reply(data, msg) return - conn.execute(query2, (id_)) - conn.execute(query3, (id_)) + conn.execute(query2, (id_,)) + conn.execute(query3, (id_,)) self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1])) + def slugify(self, name): + """Convert *name* into an identifier for storing in the database.""" + return name.lower().replace("_", "").replace("-", "") + def create_db(self, conn): """Initialize the notes database with its necessary tables.""" script = """ @@ -301,12 +307,12 @@ class Notes(Command): def get_user(self, conn, host): """Get the user ID corresponding to a hostname, or make one.""" - query1 = "SELECT user_host FROM users WHERE user_id = ?" + query1 = "SELECT user_id FROM users WHERE user_host = ?" query2 = "SELECT MAX(user_id) FROM users" query3 = "INSERT INTO users VALUES (?, ?)" - user = conn.execute(query1).fetchone()[0] + user = conn.execute(query1, (host,)).fetchone() if user: - return user + return user[0] last = conn.execute(query2).fetchone()[0] later = last + 1 if last else 1 conn.execute(query3, (later, host))