瀏覽代碼

!notes: Improved help and added aliases.

tags/v0.3
Ben Kurtovic 9 年之前
父節點
當前提交
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):

- Copyvio detector: improved sentence splitting algorithm.
- IRC > !notes: Improved help and added aliases.
- IRC > !remind: Added !remind all. Fixed multithreading efficiency issues.
Improved time detection.
- 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."""
name = "notes"
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):
self._dbfile = path.join(self.config.root_dir, "notes.db")
@@ -50,14 +62,13 @@ class Notes(Command):
}

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

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

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."""
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."""
script = """
CREATE TABLE entries (entry_id, entry_slug, entry_title,
@@ -298,19 +314,19 @@ class Notes(Command):
"""
conn.executescript(script)

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

def get_next_revision(self, conn):
def _get_next_revision(self, conn):
"""Get the next revision ID."""
query = "SELECT MAX(rev_id) FROM revisions"
later = conn.execute(query).fetchone()[0]
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."""
query1 = "SELECT user_id FROM users WHERE user_host = ?"
query2 = "SELECT MAX(user_id) FROM users"


Loading…
取消
儲存