summaryrefslogtreecommitdiff
path: root/lib/rbot/core/unicode.rb
diff options
context:
space:
mode:
authorMatthias H <apoc@sixserv.org>2014-02-20 23:28:09 +0100
committerMatthias H <apoc@sixserv.org>2014-02-20 23:28:09 +0100
commitfe6ae0ecaf02182ac5404e7ace3c24b96a12ee9a (patch)
treebc0e52a155ccde98dbcae1121172ad50710781ba /lib/rbot/core/unicode.rb
parentcd5c9cbefe219952f305ee5a9c9ab8468548750b (diff)
[core] unicode plugin that sets server encoding
Diffstat (limited to 'lib/rbot/core/unicode.rb')
-rw-r--r--lib/rbot/core/unicode.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/rbot/core/unicode.rb b/lib/rbot/core/unicode.rb
new file mode 100644
index 00000000..b6471472
--- /dev/null
+++ b/lib/rbot/core/unicode.rb
@@ -0,0 +1,55 @@
+#-- vim:sw=2:et
+#++
+#
+# :title: Unicode plugin
+# To set the encoding of strings coming from the irc server.
+
+class UnicodePlugin < CoreBotModule
+ Config.register Config::BooleanValue.new('encoding.enable',
+ :default => true,
+ :desc => "Support for non-ascii charsets",
+ :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
+
+ Config.register Config::StringValue.new('encoding.charset',
+ :default => 'utf-8',
+ :desc => 'Server encoding.',
+ :on_change => Proc.new { |bot, v| reconfigure_filter(bot) })
+
+ class UnicodeFilter
+ def initialize(charset)
+ @charset = charset
+ end
+
+ def in(data)
+ data.force_encoding @charset if data
+ end
+
+ def out(data)
+ data
+ end
+ end
+
+
+ def initialize(*a)
+ super
+ self.class.reconfigure_filter(@bot)
+ end
+
+ def cleanup
+ debug "cleaning up encodings"
+ @bot.socket.filter = nil
+ super
+ end
+
+ def UnicodePlugin.reconfigure_filter(bot)
+ debug "configuring encodings"
+ charset = bot.config['encoding.charset']
+ if bot.config['encoding.enable']
+ bot.socket.filter = UnicodeFilter.new charset
+ else
+ bot.socket.filter = nil
+ end
+ end
+end
+
+UnicodePlugin.new