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) ⇒ void
Logs a debugging message.
-
#error(message) ⇒ void
Logs an error message.
-
#exception(e) ⇒ void
Logs an exception.
-
#fatal(message) ⇒ void
Logs a fatal message.
-
#incoming(message) ⇒ void
Logs an incoming IRC message.
-
#info(message) ⇒ void
Logs an info message.
-
#log(messages, event = :debug, level = event) ⇒ void
Logs a message.
-
#outgoing(message) ⇒ void
Logs an outgoing IRC message.
-
#rescue_exception ⇒ void
Use this method to automatically log exceptions to the loggers.
-
#warn(message) ⇒ void
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.
219 220 221 |
# File 'lib/cinch/helpers.rb', line 219 def self.sanitize(string) string.gsub(/[\x00-\x08\x0a-\x1f\x7f]/, "") end |
Instance Method Details
#Channel(channel) ⇒ Channel
Helper method for turning a String into a #Channel object.
38 39 40 41 |
# File 'lib/cinch/helpers.rb', line 38 def Channel(channel) return channel if channel.is_a?(Channel) bot.channel_list.find_ensured(channel) end |
#debug(message) ⇒ void
This method returns an undefined value.
Logs a debugging message.
139 140 141 |
# File 'lib/cinch/helpers.rb', line 139 def debug() log(, :debug) end |
#error(message) ⇒ void
This method returns an undefined value.
Logs an error message.
144 145 146 |
# File 'lib/cinch/helpers.rb', line 144 def error() log(, :error) end |
#exception(e) ⇒ void
This method returns an undefined value.
Logs an exception.
174 175 176 |
# File 'lib/cinch/helpers.rb', line 174 def exception(e) log(e., :exception, :error) end |
#fatal(message) ⇒ void
This method returns an undefined value.
Logs a fatal message.
149 150 151 |
# File 'lib/cinch/helpers.rb', line 149 def fatal() log(, :fatal) end |
#Format(*settings, string) ⇒ String Also known as: Color
Returns the formatted string.
182 183 184 |
# File 'lib/cinch/helpers.rb', line 182 def Format(*settings, string) Formatting.format(*settings, string) end |
#incoming(message) ⇒ void
This method returns an undefined value.
Logs an incoming IRC message.
164 165 166 |
# File 'lib/cinch/helpers.rb', line 164 def incoming() log(, :incoming, :log) end |
#info(message) ⇒ void
This method returns an undefined value.
Logs an info message.
154 155 156 |
# File 'lib/cinch/helpers.rb', line 154 def info() log(, :info) end |
#log(messages, event = :debug, level = event) ⇒ void
This method returns an undefined value.
Logs a message.
129 130 131 132 133 134 135 136 |
# File 'lib/cinch/helpers.rb', line 129 def log(, event = :debug, level = event) if is_a?(Cinch::Plugin) = Array().map { |m| "[#{self.class}] " + m } end @bot.loggers.log(, event, level) end |
#outgoing(message) ⇒ void
This method returns an undefined value.
Logs an outgoing IRC message.
169 170 171 |
# File 'lib/cinch/helpers.rb', line 169 def outgoing() log(, :outgoing, :log) end |
#rescue_exception ⇒ void
This method returns an undefined value.
Use this method to automatically log exceptions to the loggers.
122 123 124 125 126 |
# File 'lib/cinch/helpers.rb', line 122 def rescue_exception yield rescue => e bot.loggers.exception(e) 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.
194 195 196 |
# File 'lib/cinch/helpers.rb', line 194 def Sanitize(string) Cinch::Helpers.sanitize(string) end |
#Target(target) ⇒ Target
Helper method for turning a String into a #Target object.
24 25 26 27 |
# File 'lib/cinch/helpers.rb', line 24 def Target(target) return target if target.is_a?(Target) Target.new(target, bot) end |
#Timer(interval, options = {}, &block) ⇒ Timer
94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/cinch/helpers.rb', line 94 def Timer(interval, = {}, &block) = {method: :timer, threaded: true, interval: interval}.merge() block ||= method([:method]) timer = Cinch::Timer.new(bot, , &block) timer.start if 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.
224 225 226 |
# File 'lib/cinch/helpers.rb', line 224 def Unformat(string) Formatting.unformat(string) end |
#User(user) ⇒ User
Helper method for turning a String into an #User object.
53 54 55 56 57 58 59 60 |
# File 'lib/cinch/helpers.rb', line 53 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) ⇒ void
This method returns an undefined value.
Logs a warning message.
159 160 161 |
# File 'lib/cinch/helpers.rb', line 159 def warn() log(, :warn) end |