Module: IRCSupport::Encoding
- Defined in:
- lib/ircsupport/encoding.rb
Class Method Summary collapse
-
.decode_irc(string, encoding = :irc) ⇒ String
Decode a message from an IRC connection.
-
.decode_irc!(string, encoding = :irc) ⇒ String
Decode a message from an IRC connection, modifying it in place.
-
.encode_irc(string, encoding = :irc) ⇒ String
Encode a message to be sent over an IRC connection.
-
.encode_irc!(string, encoding = :irc) ⇒ String
Encode a message to be sent over an IRC connection, modifying it in place.
Class Method Details
.decode_irc(string, encoding = :irc) ⇒ String
Decode a message from an IRC connection.
9 10 11 12 |
# File 'lib/ircsupport/encoding.rb', line 9 def decode_irc(string, encoding = :irc) string = string.dup decode_irc!(string, encoding) end |
.decode_irc!(string, encoding = :irc) ⇒ String
Decode a message from an IRC connection, modifying it in place.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ircsupport/encoding.rb', line 27 def decode_irc!(string, encoding = :irc) 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 |
.encode_irc(string, encoding = :irc) ⇒ String
Encode a message to be sent over an IRC connection.
18 19 20 21 |
# File 'lib/ircsupport/encoding.rb', line 18 def encode_irc(string, encoding = :irc) string = string.dup encode_irc!(string, encoding) end |
.encode_irc!(string, encoding = :irc) ⇒ String
Encode a message to be sent over an IRC connection, modifying it in place.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/ircsupport/encoding.rb', line 51 def encode_irc!(string, encoding = :irc) 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 example 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}).force_encoding("ASCII-8BIT") end return string end |