Module: Cinch::Helpers
Overview
The Helpers module contains a number of methods whose purpose is
to make writing plugins easier by hiding parts of the API. The
#Channel helper, for example, provides an easier way for turning
a String object into a #Channel object than directly using
ChannelList: Compare Channel("#some_channel")
with
bot.channel_list.find_ensured("#some_channel")
.
The Helpers module automatically gets included in all plugins.
Type casts collapse
-
#Channel(channel) ⇒ Channel
Helper method for turning a String into a #Channel object.
-
#Target(target) ⇒ Target
Helper method for turning a String into a #Target object.
-
#Timer(interval, options = {}, &block) ⇒ Timer
-
#User(user) ⇒ User
Helper method for turning a String into an #User object.
Logging collapse
-
#debug(message)
Logs a debugging message.
-
#error(message)
Logs an error message.
-
#exception(e)
Logs an exception.
-
#fatal(message)
Logs a fatal message.
-
#incoming(message)
Logs an incoming IRC message.
-
#info(message)
Logs an info message.
-
#log(messages, event = :debug, level = event)
Logs a message.
-
#outgoing(message)
Logs an outgoing IRC message.
-
#rescue_exception
Use this method to automatically log exceptions to the loggers.
-
#warn(message)
Logs a warning message.
Formatting collapse
-
.sanitize(string) ⇒ String
Deletes all characters in the ranges 0–8, 10–31 as well as the character 127, that is all non-printable characters and newlines.
-
#Format(*settings, string) ⇒ String
(also: #Color)
The formatted string.
-
#Sanitize(string) ⇒ String
Deletes all characters in the ranges 0–8, 10–31 as well as the character 127, that is all non-printable characters and newlines.
-
#Unformat(string) ⇒ String
Deletes all mIRC formatting codes from the string.
Class Method Details
.sanitize(string) ⇒ String
Deletes all characters in the ranges 0–8, 10–31 as well as the character 127, that is all non-printable characters and newlines.
This method is useful for filtering text from external sources before sending it to IRC.
Note that this method does not gracefully handle mIRC color codes, because it will leave the numeric arguments behind. If your text comes from IRC, you may want to filter it through #Unformat first. If you want to send sanitized input that includes your own formatting, first use this method, then add your formatting.
There exist methods for sending messages that automatically call this method, namely Target#safe_msg, Target#safe_notice, and Target#safe_action.
220 221 222 |
# File 'lib/cinch/helpers.rb', line 220 def self.sanitize(string) string.gsub(/[\x00-\x08\x10-\x1f\x7f]/, '') end |
Instance Method Details
#Channel(channel) ⇒ Channel
Helper method for turning a String into a #Channel object.
37 38 39 40 |
# File 'lib/cinch/helpers.rb', line 37 def Channel(channel) return channel if channel.is_a?(Channel) bot.channel_list.find_ensured(channel) end |
#debug(message)
This method returns an undefined value.
Logs a debugging message.
140 141 142 |
# File 'lib/cinch/helpers.rb', line 140 def debug() log(, :debug) end |
#error(message)
This method returns an undefined value.
Logs an error message.
145 146 147 |
# File 'lib/cinch/helpers.rb', line 145 def error() log(, :error) end |
#exception(e)
This method returns an undefined value.
Logs an exception.
175 176 177 |
# File 'lib/cinch/helpers.rb', line 175 def exception(e) log(e., :exception, :error) end |
#fatal(message)
This method returns an undefined value.
Logs a fatal message.
150 151 152 |
# File 'lib/cinch/helpers.rb', line 150 def fatal() log(, :fatal) end |
#Format(*settings, string) ⇒ String Also known as: Color
Returns the formatted string.
183 184 185 |
# File 'lib/cinch/helpers.rb', line 183 def Format(*settings, string) Formatting.format(*settings, string) end |
#incoming(message)
This method returns an undefined value.
Logs an incoming IRC message.
165 166 167 |
# File 'lib/cinch/helpers.rb', line 165 def incoming() log(, :incoming, :log) end |
#info(message)
This method returns an undefined value.
Logs an info message.
155 156 157 |
# File 'lib/cinch/helpers.rb', line 155 def info() log(, :info) end |
#log(messages, event = :debug, level = event)
This method returns an undefined value.
Logs a message.
130 131 132 133 134 135 136 137 |
# File 'lib/cinch/helpers.rb', line 130 def log(, event = :debug, level = event) if self.is_a?(Cinch::Plugin) = Array().map {|m| "[#{self.class}] " + m } end @bot.loggers.log(, event, level) end |
#outgoing(message)
This method returns an undefined value.
Logs an outgoing IRC message.
170 171 172 |
# File 'lib/cinch/helpers.rb', line 170 def outgoing() log(, :outgoing, :log) end |
#rescue_exception
This method returns an undefined value.
Use this method to automatically log exceptions to the loggers.
121 122 123 124 125 126 127 |
# File 'lib/cinch/helpers.rb', line 121 def rescue_exception begin yield rescue => e bot.loggers.exception(e) end end |
#Sanitize(string) ⇒ String
Deletes all characters in the ranges 0–8, 10–31 as well as the character 127, that is all non-printable characters and newlines.
This method is useful for filtering text from external sources before sending it to IRC.
Note that this method does not gracefully handle mIRC color codes, because it will leave the numeric arguments behind. If your text comes from IRC, you may want to filter it through #Unformat first. If you want to send sanitized input that includes your own formatting, first use this method, then add your formatting.
There exist methods for sending messages that automatically call this method, namely Target#safe_msg, Target#safe_notice, and Target#safe_action.
195 196 197 |
# File 'lib/cinch/helpers.rb', line 195 def Sanitize(string) Cinch::Helpers.sanitize(string) end |
#Target(target) ⇒ Target
Helper method for turning a String into a #Target object.
23 24 25 26 |
# File 'lib/cinch/helpers.rb', line 23 def Target(target) return target if target.is_a?(Target) Target.new(target, bot) end |
#Timer(interval, options = {}, &block) ⇒ Timer
93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/cinch/helpers.rb', line 93 def Timer(interval, = {}, &block) = {:method => :timer, :threaded => true, :interval => interval}.merge() block ||= self.method([:method]) timer = Cinch::Timer.new(bot, , &block) timer.start if self.respond_to?(:timers) timers << timer end timer end |
#Unformat(string) ⇒ String
Deletes all mIRC formatting codes from the string. This strips formatting for bold, underline and so on, as well as color codes. This does include removing the numeric arguments.
225 226 227 |
# File 'lib/cinch/helpers.rb', line 225 def Unformat(string) Formatting.unformat(string) end |
#User(user) ⇒ User
Helper method for turning a String into an #User object.
52 53 54 55 56 57 58 59 |
# File 'lib/cinch/helpers.rb', line 52 def User(user) return user if user.is_a?(User) if user == bot.nick bot else bot.user_list.find_ensured(user) end end |
#warn(message)
This method returns an undefined value.
Logs a warning message.
160 161 162 |
# File 'lib/cinch/helpers.rb', line 160 def warn() log(, :warn) end |