Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ircbot: add shorturl plugin #94

Merged
merged 7 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions ircbot/plugin/help.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def register(bot):
threading.Thread(target=help_server, args=(bot,), daemon=True).start()
bot.listen(r'^help$', help, require_mention=True)
bot.listen(r'^macros$', help_macro, require_mention=True)
bot.listen(r'^macros?$', help_macro, require_mention=True)


def help(bot, msg):
Expand Down Expand Up @@ -39,6 +39,11 @@ def render_response(self, template, **context):
self.end_headers()
self.wfile.write(rendered)

def render_404(self):
self.send_response(404, 'File not found')
self.end_headers()
self.wfile.write(b'404 File not found')

def do_GET(self):
if self.path == '/':
plugins = collections.defaultdict(set)
Expand All @@ -55,9 +60,7 @@ def do_GET(self):
macros=bot.plugins['macros'].list(bot),
)
else:
self.send_response(404, 'File not found')
self.end_headers()
self.wfile.write(b'404 File not found')
self.render_404()

return RequestHandler

Expand Down
79 changes: 79 additions & 0 deletions ircbot/plugin/shorturls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""Control ocfweb shorturls through ircbot."""
from ocflib.misc.shorturls import add_shorturl
from ocflib.misc.shorturls import delete_shorturl
from ocflib.misc.shorturls import get_connection as shorturl_db
from ocflib.misc.shorturls import get_shorturl
from ocflib.misc.shorturls import rename_shorturl
from ocflib.misc.shorturls import replace_shorturl


def register(bot):
bot.listen(r'^!shorturl get (.+)$', show)
bot.listen(r'^!shorturl add ([^ ]+) (.+)$', add, require_privileged_oper=True)
bot.listen(r'^!shorturl delete ([^ ]+)$', delete, require_privileged_oper=True)
bot.listen(r'^!shorturl rename ([^ ]+) ([^ ]+)$', rename, require_privileged_oper=True)
bot.listen(r'^!shorturl replace ([^ ]+) (.+)$', replace, require_privileged_oper=True)


def show(bot, msg):
"""Return a shorturl by slug."""

slug = msg.match.group(1)

with shorturl_db() as ctx:
target = get_shorturl(ctx, slug)

if not target:
msg.respond('shorturl `{}` does not exist.'.format(slug))
else:
msg.respond(target, ping=False)


def add(bot, msg):
"""Add a new shorturl."""

slug = msg.match.group(1)
target = msg.match.group(2)

if len(slug) > 100:
msg.respond('shorturl slugs must be <= 100 characters')
return

with shorturl_db(user='ocfircbot', password=bot.mysql_password) as ctx:

# validation occurs in ocflib, and uniqueness is guaranteed
# by the database constraint. The error will propagate up if
# someone tries to add or rename an entry that results in a dupe
add_shorturl(ctx, slug, target)
msg.respond('shorturl added as `{}`'.format(slug))


def delete(bot, msg):
"""Delete a shorturl."""

slug = msg.match.group(1)
with shorturl_db(user='ocfircbot', password=bot.mysql_password) as ctx:
delete_shorturl(ctx, slug)
msg.respond('shorturl `{}` has been deleted.'.format(slug))


def rename(bot, msg):
"""Rename a shorturl."""

old_slug = msg.match.group(1)
new_slug = msg.match.group(2)

with shorturl_db(user='ocfircbot', password=bot.mysql_password) as ctx:
rename_shorturl(ctx, old_slug, new_slug)
msg.respond('shorturl `{}` has been renamed to `{}`'.format(old_slug, new_slug))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when the old slug does not exist?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing, the where clause doesn't match anything so it's a no-op



def replace(bot, msg):
"""Replace the target of a shorturl slug."""

slug = msg.match.group(1)
new_target = msg.match.group(2)

with shorturl_db(user='ocfircbot', password=bot.mysql_password) as ctx:
replace_shorturl(ctx, slug, new_target)
msg.respond('shorturl `{}` updated'.format(slug))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

likewise, what happens when the slug does not exist?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ibid