From f130d55a745bb59cb50dd0cf054ec065722c3051 Mon Sep 17 00:00:00 2001 From: Dorian Stoll Date: Sun, 5 Mar 2017 17:35:07 +0100 Subject: [PATCH] Implement an escaping scheme that handles all non-URL-safe characters. 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. --- config/application.yml.example | 3 +++ lib/irclogger/viewer_helpers.rb | 35 +++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/config/application.yml.example b/config/application.yml.example index 06f28a0..8e80a5b 100644 --- a/config/application.yml.example +++ b/config/application.yml.example @@ -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 diff --git a/lib/irclogger/viewer_helpers.rb b/lib/irclogger/viewer_helpers.rb index d141962..5bd7e5c 100644 --- a/lib/irclogger/viewer_helpers.rb +++ b/lib/irclogger/viewer_helpers.rb @@ -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)