Skip to content
Browse files

move utility methods to their own modules

  • Loading branch information...
1 parent 7d3a8fd commit d8b4df8deae04f1e1f8f2e78c5f56c08830006ce @dominikh dominikh committed Sep 3, 2011
View
78 lib/cinch.rb
@@ -1,74 +1,6 @@
+require 'cinch/version'
+require 'cinch/utilities/kernel'
+require 'cinch/utilities/string'
+require 'cinch/utilities/deprecation'
+require 'cinch/utilities/encoding'
require 'cinch/bot'
-
-module Cinch
- VERSION = '1.1.3'
-
- # @return [Object]
- # @api private
- def self.string_to_const(s)
- return s unless s.is_a?(String)
- s.split("::").inject(Kernel) {|base, name| base.const_get(name) }
- end
-
- # @return [String]
- # @todo Handle mIRC color codes more gracefully.
- # @api private
- def self.filter_string(string)
- string.gsub(/[\x00-\x1f]/, '')
- end
-
- # @api private
- def self.encode_incoming(string, encoding)
- string = string.dup
- if encoding == :irc
- # If incoming text is valid UTF-8, it will be interpreted as
- # such. If it fails validation, a CP1252 -> UTF-8 conversion
- # is performed. This allows you to see non-ASCII from mIRC
- # users (non-UTF-8) and other users sending you UTF-8.
- #
- # (from http://xchat.org/encoding/#hybrid)
- string.force_encoding("UTF-8")
- if !string.valid_encoding?
- string.force_encoding("CP1252").encode!("UTF-8", {:invalid => :replace, :undef => :replace})
- end
- else
- string.force_encoding(encoding).encode!({:invalid => :replace, :undef => :replace})
- string = string.chars.select { |c| c.valid_encoding? }.join
- end
-
- return string
- end
-
- # @api private
- def self.encode_outgoing(string, encoding)
- string = string.dup
- if encoding == :irc
- # If your text contains only characters that fit inside the CP1252
- # code page (aka Windows Latin-1), the entire line will be sent
- # that way. mIRC users should see it correctly. XChat users who
- # are using UTF-8 will also see it correctly, because it will fail
- # UTF-8 validation and will be assumed to be CP1252, even by older
- # XChat versions.
- #
- # If the text doesn't fit inside the CP1252 code page, (for eaxmple if you
- # type Eastern European characters, or Russian) it will be sent as UTF-8. Only
- # UTF-8 capable clients will be able to see these characters correctly
- #
- # (from http://xchat.org/encoding/#hybrid)
- begin
- string.encode!("CP1252")
- rescue Encoding::UndefinedConversionError
- end
- else
- string.encode!(encoding, {:invalid => :replace, :undef => :replace})
- end
-
- return string
- end
-
- # @api private
- def self.print_deprecation(version, method)
- $stderr.puts "Deprecation warning: Beginning with version #{version}, #{method} should not be used anymore."
- $stderr.puts caller
- end
-end
View
20 lib/cinch/bot.rb
@@ -139,7 +139,7 @@ def synchronize(name, &block)
# @deprecated Use `next` or `break` instead
# @note This method will be removed in Cinch 2.0.0
def halt
- Cinch.print_deprecation("1.2.0", "Bot#halt")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#halt")
throw :halt
end
@@ -158,7 +158,7 @@ def raw(command)
# @deprecated See {Target#msg} instead
# @note This method will be removed in Cinch 2.0.0
def msg(recipient, text, notice = false)
- Cinch.print_deprecation("1.2.0", "Bot#msg")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#msg")
Target(recipient).msg(text, notice)
end
@@ -168,15 +168,15 @@ def msg(recipient, text, notice = false)
# @deprecated See {Target#notice} instead
# @note This method will be removed in Cinch 2.0.0
def notice(recipient, text)
- Cinch.print_deprecation("1.2.0", "Bot#notice")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#notice")
Target(recipient).msg(text, true)
end
# @deprecated See {Target#safe_msg} instead
# @note This method will be removed in Cinch 2.0.0
def safe_msg(recipient, text)
- Cinch.print_deprecation("1.2.0", "Bot#safe_msg")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#safe_msg")
Target(recipient).safe_msg(text)
end
@@ -186,23 +186,23 @@ def safe_msg(recipient, text)
# @deprecated See {Target#safe_notice} instead
# @note This method will be removed in Cinch 2.0.0
def safe_notice(recipient, text)
- Cinch.print_deprecation("1.2.0", "Bot#safe_notice")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#safe_notice")
Target(recipient).safe_msg(text, true)
end
# @deprecated See {Target#action} instead
# @note This method will be removed in Cinch 2.0.0
def action(recipient, text)
- Cinch.print_deprecation("1.2.0", "Bot#action")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#action")
Target(recipient).action(text)
end
# @deprecated See {Target#safe_action} instead
# @note This method will be removed in Cinch 2.0.0
def safe_action(recipient, text)
- Cinch.print_deprecation("1.2.0", "Bot#safe_action")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#safe_action")
Target(recipient).safe_action(text)
end
@@ -265,7 +265,7 @@ def on(event, regexps = [], *args, &block)
# @deprecated See {HandlerList#dispatch} instead
# @note This method will be removed in Cinch 2.0.0
def dispatch(event, msg = nil, *arguments)
- Cinch.print_deprecation("1.2.0", "Bot#dispatch")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#dispatch")
@handlers.dispatch(event, msg, *arguments)
end
@@ -276,7 +276,7 @@ def dispatch(event, msg = nil, *arguments)
# @deprecated See {Bot#plugins} and {PluginList#register_plugins} instead
# @note This method will be removed in Cinch 2.0.0
def register_plugins
- Cinch.print_deprecation("1.2.0", "Bot#register_plugins")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#register_plugins")
@plugins.register_plugins(@config.plugins.plugins)
end
@@ -288,7 +288,7 @@ def register_plugins
# @deprecated See {Bot#plugins} and {PluginList#register_plugin} instead
# @note This method will be removed in Cinch 2.0.0
def register_plugin(plugin)
- Cinch.print_deprecation("1.2.0", "Bot#register_plugin")
+ Cinch::Utilities::Deprecation.print_deprecation("1.2.0", "Bot#register_plugin")
@plugins.register_plugin(plugin)
end
View
6 lib/cinch/channel.rb
@@ -24,7 +24,7 @@ class << self
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find_ensured(name, bot)
- Cinch.print_deprecation("1.1.0", "Channel.find_ensured")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "Channel.find_ensured")
downcased_name = name.irc_downcase(bot.irc.isupport["CASEMAPPING"])
@channels[downcased_name] ||= bot.channel_manager.find_ensured(name)
@@ -38,7 +38,7 @@ def find_ensured(name, bot)
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find(name)
- Cinch.print_deprecation("1.1.0", "Channel.find")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "Channel.find")
@channels[name]
end
@@ -48,7 +48,7 @@ def find(name)
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def all
- Cinch.print_deprecation("1.1.0", "User.all")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.all")
@channels.values
end
View
2 lib/cinch/irc.rb
@@ -95,7 +95,7 @@ def start_reading_thread
begin
while line = @socket.readline
rescue_exception do
- line = Cinch.encode_incoming(line, @bot.config.encoding)
+ line = Cinch::Utilities::Encoding.encode_incoming(line, @bot.config.encoding)
parse line
end
end
View
2 lib/cinch/message_queue.rb
@@ -65,7 +65,7 @@ def process!
end
begin
- @socket.writeline Cinch.encode_outgoing(message, @bot.config.encoding) + "\r\n"
+ @socket.writeline Cinch::Utilities::Encoding.encode_outgoing(message, @bot.config.encoding) + "\r\n"
@log << Time.now
@bot.logger.log(message, :outgoing) if @bot.config.verbose
View
4 lib/cinch/plugins_configuration.rb
@@ -18,11 +18,11 @@ def load(new_config, from_default = false)
new_config.each do |option, value|
case option
when :plugins
- _new_config[option] = value.map{|v| Cinch.string_to_const(v)}
+ _new_config[option] = value.map{|v| Cinch::Utilities::Kernel.string_to_const(v)}
when :options
_value = self[:options]
value.each do |k, v|
- k = Cinch.string_to_const(k)
+ k = Cinch::Utilities::Kernel.string_to_const(k)
v = self[:options][k].merge(v)
_value[k] = v
end
View
4 lib/cinch/target.rb
@@ -70,7 +70,7 @@ def msg(text, notice = false)
# @see #msg
# @todo Handle mIRC color codes more gracefully.
def safe_msg(text, notice = false)
- msg(Cinch.filter_string(text), notice)
+ msg(Cinch::Utilities::String.filter_string(text), notice)
end
alias_method :safe_privmsg, :safe_msg
alias_method :safe_send, :safe_msg
@@ -107,7 +107,7 @@ def action(text)
# @see #action
# @todo Handle mIRC color codes more gracefully.
def safe_action(text)
- action(Cinch.filter_string(text))
+ action(Cinch::Utilities::String.filter_string(text))
end
# Send a CTCP to the target.
View
6 lib/cinch/user.rb
@@ -35,7 +35,7 @@ class << self
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find_ensured(*args)
- Cinch.print_deprecation("1.1.0", "User.find_ensured")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.find_ensured")
case args.size
when 2
@@ -64,7 +64,7 @@ def find_ensured(*args)
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def find(nick)
- Cinch.print_deprecation("1.1.0", "User.find")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.find")
@users[downcased_nick]
end
@@ -74,7 +74,7 @@ def find(nick)
# @note This method does not work properly if running more than one bot
# @note This method will be removed in Cinch 2.0.0
def all
- Cinch.print_deprecation("1.1.0", "User.all")
+ Cinch::Utilities::Deprecation.print_deprecation("1.1.0", "User.all")
@users.values
end
View
11 lib/cinch/utilities/deprecation.rb
@@ -0,0 +1,11 @@
+module Cinch
+ module Utilities
+ module Deprecation
+ # @api private
+ def self.print_deprecation(version, method)
+ $stderr.puts "Deprecation warning: Beginning with version #{version}, #{method} should not be used anymore."
+ $stderr.puts caller
+ end
+ end
+ end
+end
View
54 lib/cinch/utilities/encoding.rb
@@ -0,0 +1,54 @@
+module Cinch
+ module Utilities
+ module Encoding
+ # @api private
+ def self.encode_incoming(string, encoding)
+ string = string.dup
+ if encoding == :irc
+ # If incoming text is valid UTF-8, it will be interpreted as
+ # such. If it fails validation, a CP1252 -&gt; UTF-8 conversion
+ # is performed. This allows you to see non-ASCII from mIRC
+ # users (non-UTF-8) and other users sending you UTF-8.
+ #
+ # (from http://xchat.org/encoding/#hybrid)
+ string.force_encoding("UTF-8")
+ if !string.valid_encoding?
+ string.force_encoding("CP1252").encode!("UTF-8", {:invalid => :replace, :undef => :replace})
+ end
+ else
+ string.force_encoding(encoding).encode!({:invalid => :replace, :undef => :replace})
+ string = string.chars.select { |c| c.valid_encoding? }.join
+ end
+
+ return string
+ end
+
+ # @api private
+ def self.encode_outgoing(string, encoding)
+ string = string.dup
+ if encoding == :irc
+ # If your text contains only characters that fit inside the CP1252
+ # code page (aka Windows Latin-1), the entire line will be sent
+ # that way. mIRC users should see it correctly. XChat users who
+ # are using UTF-8 will also see it correctly, because it will fail
+ # UTF-8 validation and will be assumed to be CP1252, even by older
+ # XChat versions.
+ #
+ # If the text doesn't fit inside the CP1252 code page, (for eaxmple if you
+ # type Eastern European characters, or Russian) it will be sent as UTF-8. Only
+ # UTF-8 capable clients will be able to see these characters correctly
+ #
+ # (from http://xchat.org/encoding/#hybrid)
+ begin
+ string.encode!("CP1252")
+ rescue Encoding::UndefinedConversionError
+ end
+ else
+ string.encode!(encoding, {:invalid => :replace, :undef => :replace})
+ end
+
+ return string
+ end
+ end
+ end
+end
View
12 lib/cinch/utilities/kernel.rb
@@ -0,0 +1,12 @@
+module Cinch
+ module Utilities
+ module Kernel
+ # @return [Object]
+ # @api private
+ def self.string_to_const(s)
+ return s unless s.is_a?(String)
+ s.split("::").inject(Kernel) {|base, name| base.const_get(name) }
+ end
+ end
+ end
+end
View
12 lib/cinch/utilities/string.rb
@@ -0,0 +1,12 @@
+module Cinch
+ module Utilities
+ module String
+ # @return [String]
+ # @todo Handle mIRC color codes more gracefully.
+ # @api private
+ def self.filter_string(string)
+ string.gsub(/[\x00-\x1f]/, '')
+ end
+ end
+ end
+end
View
3 lib/cinch/version.rb
@@ -0,0 +1,3 @@
+module Cinch
+ VERSION = '1.1.3'
+end

0 comments on commit d8b4df8

Please sign in to comment.
Something went wrong with that request. Please try again.