浏览代码

!notes: Improved help and added aliases.

tags/v0.3
Ben Kurtovic 8 年前
父节点
当前提交
49f178760f
共有 2 个文件被更改,包括 38 次插入21 次删除
  1. +1
    -0
      CHANGELOG
  2. +37
    -21
      earwigbot/commands/notes.py

+ 1
- 0
CHANGELOG 查看文件

@@ -1,6 +1,7 @@
v0.3 (unreleased): v0.3 (unreleased):


- Copyvio detector: improved sentence splitting algorithm. - Copyvio detector: improved sentence splitting algorithm.
- IRC > !notes: Improved help and added aliases.
- IRC > !remind: Added !remind all. Fixed multithreading efficiency issues. - IRC > !remind: Added !remind all. Fixed multithreading efficiency issues.
Improved time detection. Improved time detection.
- IRC > !stalk: Allow regular expressions as page titles or usernames. - IRC > !stalk: Allow regular expressions as page titles or usernames.


+ 37
- 21
earwigbot/commands/notes.py 查看文件

@@ -32,7 +32,19 @@ class Notes(Command):
"""A mini IRC-based wiki for storing notes, tips, and reminders.""" """A mini IRC-based wiki for storing notes, tips, and reminders."""
name = "notes" name = "notes"
commands = ["notes", "note", "about"] commands = ["notes", "note", "about"]
version = 2
version = "2.1"

aliases = {
"all": "list",
"show": "read",
"get": "read",
"add": "edit",
"write": "edit",
"change": "edit",
"modify": "edit",
"move": "rename",
"remove": "delete"
}


def setup(self): def setup(self):
self._dbfile = path.join(self.config.root_dir, "notes.db") self._dbfile = path.join(self.config.root_dir, "notes.db")
@@ -50,14 +62,13 @@ class Notes(Command):
} }


if not data.args: if not data.args:
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))
self.do_help(data)
return return
command = data.args[0].lower() command = data.args[0].lower()
if command in commands: if command in commands:
commands[command](data) commands[command](data)
elif command in self.aliases:
commands[self.aliases[command]](data)
else: else:
msg = "Unknown subcommand: \x0303{0}\x0F.".format(command) msg = "Unknown subcommand: \x0303{0}\x0F.".format(command)
self.reply(data, msg) self.reply(data, msg)
@@ -83,8 +94,13 @@ class Notes(Command):
try: try:
command = data.args[1] command = data.args[1]
except IndexError: except IndexError:
self.reply(data, "Please specify a subcommand to get help on.")
msg = ("\x0302The Earwig Mini-Wiki\x0F: running v{0}. Subcommands "
"are: {1}. You can get help on any with '!{2} help subcommand'.")
cmnds = ", ".join((info.keys()))
self.reply(data, msg.format(self.version, cmnds, data.command))
return return
if command in self.aliases:
command = self.aliases[command]
try: try:
help_ = re.sub(r"\s\s+", " ", info[command].replace("\n", "")) help_ = re.sub(r"\s\s+", " ", info[command].replace("\n", ""))
self.reply(data, "\x0303{0}\x0F: ".format(command) + help_) self.reply(data, "\x0303{0}\x0F: ".format(command) + help_)
@@ -113,7 +129,7 @@ class Notes(Command):
INNER JOIN revisions ON entry_revision = rev_id INNER JOIN revisions ON entry_revision = rev_id
WHERE entry_slug = ?""" WHERE entry_slug = ?"""
try: try:
slug = self.slugify(data.args[1])
slug = self._slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to read from.") self.reply(data, "Please specify an entry to read from.")
return return
@@ -141,7 +157,7 @@ class Notes(Command):
query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)" query3 = "INSERT INTO entries VALUES (?, ?, ?, ?)"
query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?" query4 = "UPDATE entries SET entry_revision = ? WHERE entry_id = ?"
try: try:
slug = self.slugify(data.args[1])
slug = self._slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to edit.") self.reply(data, "Please specify an entry to edit.")
return return
@@ -157,17 +173,17 @@ class Notes(Command):
create = False create = False
except sqlite.OperationalError: except sqlite.OperationalError:
id_, title, author = 1, data.args[1].decode("utf8"), data.host id_, title, author = 1, data.args[1].decode("utf8"), data.host
self.create_db(conn)
self._create_db(conn)
except TypeError: except TypeError:
id_ = self.get_next_entry(conn)
id_ = self._get_next_entry(conn)
title, author = data.args[1].decode("utf8"), data.host title, author = data.args[1].decode("utf8"), data.host
permdb = self.config.irc["permissions"] permdb = self.config.irc["permissions"]
if author != data.host and not permdb.is_admin(data): if author != data.host and not permdb.is_admin(data):
msg = "You must be an author or a bot admin to edit this entry." msg = "You must be an author or a bot admin to edit this entry."
self.reply(data, msg) self.reply(data, msg)
return return
revid = self.get_next_revision(conn)
userid = self.get_user(conn, data.host)
revid = self._get_next_revision(conn)
userid = self._get_user(conn, data.host)
now = datetime.utcnow().strftime("%b %d, %Y %H:%M:%S") now = datetime.utcnow().strftime("%b %d, %Y %H:%M:%S")
conn.execute(query2, (revid, id_, userid, now, content)) conn.execute(query2, (revid, id_, userid, now, content))
if create: if create:
@@ -185,7 +201,7 @@ class Notes(Command):
INNER JOIN users ON rev_user = user_id INNER JOIN users ON rev_user = user_id
WHERE entry_slug = ?""" WHERE entry_slug = ?"""
try: try:
slug = self.slugify(data.args[1])
slug = self._slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to get info on.") self.reply(data, "Please specify an entry to get info on.")
return return
@@ -221,7 +237,7 @@ class Notes(Command):
query2 = """UPDATE entries SET entry_slug = ?, entry_title = ? query2 = """UPDATE entries SET entry_slug = ?, entry_title = ?
WHERE entry_id = ?""" WHERE entry_id = ?"""
try: try:
slug = self.slugify(data.args[1])
slug = self._slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to rename.") self.reply(data, "Please specify an entry to rename.")
return return
@@ -246,7 +262,7 @@ class Notes(Command):
msg = "You must be an author or a bot admin to rename this entry." msg = "You must be an author or a bot admin to rename this entry."
self.reply(data, msg) self.reply(data, msg)
return return
args = (self.slugify(newtitle), newtitle.decode("utf8"), id_)
args = (self._slugify(newtitle), newtitle.decode("utf8"), id_)
conn.execute(query2, args) conn.execute(query2, args)


msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F." msg = "Entry \x0302{0}\x0F renamed to \x0302{1}\x0F."
@@ -261,7 +277,7 @@ class Notes(Command):
query2 = "DELETE FROM entries WHERE entry_id = ?" query2 = "DELETE FROM entries WHERE entry_id = ?"
query3 = "DELETE FROM revisions WHERE rev_entry = ?" query3 = "DELETE FROM revisions WHERE rev_entry = ?"
try: try:
slug = self.slugify(data.args[1])
slug = self._slugify(data.args[1])
except IndexError: except IndexError:
self.reply(data, "Please specify an entry to delete.") self.reply(data, "Please specify an entry to delete.")
return return
@@ -283,11 +299,11 @@ class Notes(Command):


self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1])) self.reply(data, "Entry \x0302{0}\x0F deleted.".format(data.args[1]))


def slugify(self, name):
def _slugify(self, name):
"""Convert *name* into an identifier for storing in the database.""" """Convert *name* into an identifier for storing in the database."""
return name.lower().replace("_", "").replace("-", "").decode("utf8") return name.lower().replace("_", "").replace("-", "").decode("utf8")


def create_db(self, conn):
def _create_db(self, conn):
"""Initialize the notes database with its necessary tables.""" """Initialize the notes database with its necessary tables."""
script = """ script = """
CREATE TABLE entries (entry_id, entry_slug, entry_title, CREATE TABLE entries (entry_id, entry_slug, entry_title,
@@ -298,19 +314,19 @@ class Notes(Command):
""" """
conn.executescript(script) conn.executescript(script)


def get_next_entry(self, conn):
def _get_next_entry(self, conn):
"""Get the next entry ID.""" """Get the next entry ID."""
query = "SELECT MAX(entry_id) FROM entries" query = "SELECT MAX(entry_id) FROM entries"
later = conn.execute(query).fetchone()[0] later = conn.execute(query).fetchone()[0]
return later + 1 if later else 1 return later + 1 if later else 1


def get_next_revision(self, conn):
def _get_next_revision(self, conn):
"""Get the next revision ID.""" """Get the next revision ID."""
query = "SELECT MAX(rev_id) FROM revisions" query = "SELECT MAX(rev_id) FROM revisions"
later = conn.execute(query).fetchone()[0] later = conn.execute(query).fetchone()[0]
return later + 1 if later else 1 return later + 1 if later else 1


def get_user(self, conn, host):
def _get_user(self, conn, host):
"""Get the user ID corresponding to a hostname, or make one.""" """Get the user ID corresponding to a hostname, or make one."""
query1 = "SELECT user_id FROM users WHERE user_host = ?" query1 = "SELECT user_id FROM users WHERE user_host = ?"
query2 = "SELECT MAX(user_id) FROM users" query2 = "SELECT MAX(user_id) FROM users"


正在加载...
取消
保存