This repository has been archived by the owner on Oct 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
ircbot: add shorturl plugin #94
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
83b8d95
ircbot: add shorturl plugin
abizer c066279
shorturl regex to only allow alphanum and /
abizer 477f530
shorturl register helper and bugfix
abizer 10ab80e
remove shorturl bouncer from ircbot
abizer 1ebed71
move shorturls to their own db, require oper to modify
abizer 2c617b0
shorturls: update shorturls to use ocflib functionality
abizer 0cc96ea
shorturls: update comments
abizer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
|
||
|
||
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. likewise, what happens when the slug does not exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ibid |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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