Skip to content

Commit

Permalink
Implement an escaping scheme that handles all non-URL-safe characters.
Browse files Browse the repository at this point in the history
This is a change from the previous scheme that only addresses dots;
old installations must be migrated by listing every channel in
the "legacy_escaping_scheme" key in application.yml.
  • Loading branch information
Dorian Stoll authored and whitequark committed Mar 8, 2017
1 parent 6720816 commit f130d55
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 3 additions & 0 deletions config/application.yml.example
Expand Up @@ -9,4 +9,7 @@ realname: whitequark's logger bot
nickname: logger_test
channels:
- "#irclogger-test"
- "##irclogger-test"
legacy_escaping_scheme:
- ".irclogger-test"
domain: example.com
35 changes: 33 additions & 2 deletions lib/irclogger/viewer_helpers.rb
Expand Up @@ -4,12 +4,43 @@ module IrcLogger
module ViewerHelpers
include Rack::Utils

CHANNEL_ESCAPE = {
'~' => '~~',
'#' => '~h~',
'/' => '~s~',
'%' => '~p~',
'\\' => '~r~',
'?' => '~q~',
'`' => '~a~',
'<' => '~l~',
'>' => '~g~',
'|' => '~b~',
'{' => '~o~',
'}' => '~c~',
'^' => '~x~',
'"' => '~d~'
}
CHANNEL_ESCAPE_INVERTED = CHANNEL_ESCAPE.invert

def escape_url(url)
url.gsub(/(.)/) { |m| CHANNEL_ESCAPE.key?(m) ? CHANNEL_ESCAPE[m] : m }
end

def unescape_url(url)
url.gsub(/~[^~]?~/) { |m| CHANNEL_ESCAPE_INVERTED.key?(m) ? CHANNEL_ESCAPE_INVERTED[m] : m }
end

def channel_escape(channel)
channel[1..-1].gsub(/^#+/) { |m| '.' * m.length }
escape_url(channel[1..-1])
end

def channel_unescape(channel)
"##{channel.gsub(/^\.+/) { |m| '#' * m.length }}"
c = unescape_url(channel)
if (Config.key?('legacy_escaping_scheme') &&
Config['legacy_escaping_scheme'].include?(c))
"\##{c.gsub(/^\.+/) { |m| '#' * m.length }}"
end
"\##{c}"
end

def channel_url(channel, postfix=nil)
Expand Down

0 comments on commit f130d55

Please sign in to comment.