Class: Cinch::Bot
Overview
Instance Attribute Summary (collapse)
-
- (Callback) callback
readonly
private
-
- (ChannelList) channel_list
readonly
All channels the bot knows about.
-
- (Array<Channel>) channels
readonly
All channels the bot currently is in.
-
- (Configuration::Bot) config
readonly
-
- (HandlerList) handlers
readonly
The HandlerList, providing access to all registered plugins and plugin manipulation as well as calling handlers.
-
- (IRC) irc
readonly
The underlying IRC connection.
-
- (Boolean) last_connection_was_successful
private
-
- (LoggerList) loggers
The logger list containing all loggers.
-
- (Array<String>) modes
The bot’s modes.
-
- (String) nick
The bot’s nickname.
-
- (PluginList) plugins
readonly
The PluginList giving access to (un)loading plugins.
-
- (Boolean) quitting
readonly
Whether the bot is in the process of disconnecting.
-
- (UserList) user_list
readonly
All users the bot knows about.
Attributes inherited from User
#authname, #away, #data, #host, #idle, #in_whois, #last_nick, #monitored, #online, #realname, #secure, #signed_on_at, #synced, #unknown, #user
Attributes inherited from Target
Helper methods (collapse)
-
- helpers { ... }
Define helper methods in the context of the bot.
-
- synchronize(name) { ... }
Since Cinch uses threads, all handlers can be run simultaneously, even the same handler multiple times.
Events & Plugins (collapse)
-
- (Handler) on(event, regexp = //, *args) {|*args| ... }
Registers a handler.
Bot Control (collapse)
-
- configure {|config| ... }
This method is used to set a bot’s options.
-
- quit(message = nil)
Disconnects from the server.
-
- start(plugins = true)
Connects the bot to a server.
-
- (Object) stop
Channel Control (collapse)
-
- (Channel) join(channel, key = nil)
Join a channel.
-
- (Channel) part(channel, reason = nil)
Part a channel.
Instance Method Summary (collapse)
-
- (self) bot
private
-
- (String) generate_next_nick!(base = nil)
private
Try to create a free nick, first by cycling through all available alternatives and then by appending underscores.
-
- (Bot) initialize { ... }
constructor
A new instance of Bot.
-
- (String) inspect
-
- oper(password, user = nil)
Gain oper privileges.
-
- set_mode(mode)
Sets a mode on the bot.
-
- (String) set_nick(nick)
private
Used for updating the bot’s nick from within the IRC parser.
-
- (Boolean) strict?
True if the bot reports ISUPPORT violations as exceptions.
-
- unset_mode(mode)
Unsets a mode on the bot.
Methods included from Helpers
#Channel, #Format, #Target, #Timer, #User, #debug, #error, #exception, #fatal, #incoming, #info, #log, #outgoing, #rescue_exception, #warn
Methods inherited from User
#attr, #authed?, #dcc_send, #end_of_whois, #mask, #match, #monitor, #to_s, #unmonitor, #unsync_all, #update_nick, #whois
Methods included from Syncable
#attr, #attribute_synced?, #mark_as_synced, #sync, #unsync, #unsync_all, #wait_until_synced
Methods inherited from Target
#<=>, #action, #concretize, #ctcp, #eql?, #msg, #notice, #safe_action, #safe_msg, #safe_notice
Constructor Details
- (Bot) initialize { ... }
A new instance of Bot
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
# File 'lib/cinch/bot.rb', line 340 def initialize(&b) @loggers = LoggerList.new @loggers << Logger::FormattedLogger.new($stderr) @config = Configuration::Bot.new @handlers = HandlerList.new @semaphores_mutex = Mutex.new @semaphores = Hash.new { |h, k| h[k] = Mutex.new } @callback = Callback.new(self) @channels = [] @quitting = false @modes = [] @user_list = UserList.new(self) @channel_list = ChannelList.new(self) @plugins = PluginList.new(self) @join_handler = nil @join_timer = nil super(nil, self) instance_eval(&b) if block_given? end |
Instance Attribute Details
- (Callback) callback (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
103 104 105 |
# File 'lib/cinch/bot.rb', line 103 def callback @callback end |
- (ChannelList) channel_list (readonly)
All channels the bot knows about.
95 96 97 |
# File 'lib/cinch/bot.rb', line 95 def channel_list @channel_list end |
- (Array<Channel>) channels (readonly)
All channels the bot currently is in
77 78 79 |
# File 'lib/cinch/bot.rb', line 77 def channels @channels end |
- (Configuration::Bot) config (readonly)
63 64 65 |
# File 'lib/cinch/bot.rb', line 63 def config @config end |
- (HandlerList) handlers (readonly)
The HandlerList, providing access to all registered plugins and plugin manipulation as well as calling handlers.
111 112 113 |
# File 'lib/cinch/bot.rb', line 111 def handlers @handlers end |
- (IRC) irc (readonly)
The underlying IRC connection
68 69 70 |
# File 'lib/cinch/bot.rb', line 68 def irc @irc end |
- (Boolean) last_connection_was_successful
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
99 100 101 |
# File 'lib/cinch/bot.rb', line 99 def last_connection_was_successful @last_connection_was_successful end |
- (LoggerList) loggers
The logger list containing all loggers
74 75 76 |
# File 'lib/cinch/bot.rb', line 74 def loggers @loggers end |
- (Array<String>) modes
The bot’s modes.
117 118 119 |
# File 'lib/cinch/bot.rb', line 117 def modes @modes end |
- (String) nick=(new_nick) - (String) nick
The bot’s nickname.
57 58 59 |
# File 'lib/cinch/bot.rb', line 57 def nick @nick end |
- (PluginList) plugins (readonly)
The PluginList giving access to (un)loading plugins
82 83 84 |
# File 'lib/cinch/bot.rb', line 82 def plugins @plugins end |
- (Boolean) quitting (readonly)
Whether the bot is in the process of disconnecting
85 86 87 |
# File 'lib/cinch/bot.rb', line 85 def quitting @quitting end |
Instance Method Details
- (self) bot
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
367 368 369 370 |
# File 'lib/cinch/bot.rb', line 367 def bot # This method is needed for the Helpers interface self end |
- configure {|config| ... }
This method returns an undefined value.
This method is used to set a bot’s options. It indeed does nothing else but yielding #config, but it makes for a nice DSL.
218 219 220 |
# File 'lib/cinch/bot.rb', line 218 def configure yield @config end |
- (String) generate_next_nick!(base = nil)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Try to create a free nick, first by cycling through all available alternatives and then by appending underscores.
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 |
# File 'lib/cinch/bot.rb', line 454 def generate_next_nick!(base = nil) nicks = @config.nicks || [] if base # if `base` is not in our list of nicks to try, assume that it's # custom and just append an underscore if !nicks.include?(base) new_nick = base + "_" else # if we have a base, try the next nick or append an # underscore if no more nicks are left new_index = nicks.index(base) + 1 if nicks[new_index] new_nick = nicks[new_index] else new_nick = base + "_" end end else # if we have no base, try the first possible nick new_nick = @config.nicks ? @config.nicks.first : @config.nick end @config.nick = new_nick end |
- helpers { ... }
This method returns an undefined value.
Define helper methods in the context of the bot.
125 126 127 |
# File 'lib/cinch/bot.rb', line 125 def helpers(&b) @callback.instance_eval(&b) end |
- (String) inspect
481 482 483 |
# File 'lib/cinch/bot.rb', line 481 def inspect "#<Bot nick=#{@name.inspect}>" end |
- (Channel) join(channel, key = nil)
Join a channel.
311 312 313 314 315 316 |
# File 'lib/cinch/bot.rb', line 311 def join(channel, key = nil) channel = Channel(channel) channel.join(key) channel end |
- (Handler) on(event, regexp = //, *args) {|*args| ... }
Registers a handler.
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/cinch/bot.rb', line 188 def on(event, regexp = //, *args, &block) event = event.to_s.to_sym pattern = case regexp when Pattern regexp when Regexp Pattern.new(nil, regexp, nil) else if event == :ctcp Pattern.generate(:ctcp, regexp) else Pattern.new(/^/, /#{Regexp.escape(regexp.to_s)}/, /$/) end end handler = Handler.new(self, event, pattern, {args: args, execute_in_callback: true}, &block) @handlers.register(handler) return handler end |
- oper(password, user = nil)
This method returns an undefined value.
Gain oper privileges.
442 443 444 445 |
# File 'lib/cinch/bot.rb', line 442 def oper(password, user = nil) user ||= self.nick @irc.send "OPER #{user} #{password}" end |
- (Channel) part(channel, reason = nil)
Part a channel.
324 325 326 327 328 329 |
# File 'lib/cinch/bot.rb', line 324 def part(channel, reason = nil) channel = Channel(channel) channel.part(reason) channel end |
- quit(message = nil)
This method returns an undefined value.
Disconnects from the server.
226 227 228 229 230 231 |
# File 'lib/cinch/bot.rb', line 226 def quit( = nil) @quitting = true command = ? "QUIT :#{}" : "QUIT" @irc.send command end |
- set_mode(mode)
This method returns an undefined value.
Sets a mode on the bot.
379 380 381 382 |
# File 'lib/cinch/bot.rb', line 379 def set_mode(mode) @modes << mode unless @modes.include?(mode) @irc.send "MODE #{nick} +#{mode}" end |
- (String) set_nick(nick)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used for updating the bot’s nick from within the IRC parser.
410 411 412 |
# File 'lib/cinch/bot.rb', line 410 def set_nick(nick) @name = nick end |
- start(plugins = true)
This method returns an undefined value.
Connects the bot to a server.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/cinch/bot.rb', line 238 def start(plugins = true) @reconnects = 0 @plugins.register_plugins(@config.plugins.plugins) if plugins begin @user_list.each do |user| user.in_whois = false user.unsync_all end # reset state of all users @channel_list.each do |channel| channel.unsync_all end # reset state of all channels @join_handler.unregister if @join_handler @join_timer.stop if @join_timer join_lambda = lambda { @config.channels.each { |channel| Channel(channel).join }} if @config.delay_joins.is_a?(Symbol) @join_handler = join_handler = on(@config.delay_joins) { join_handler.unregister join_lambda.call } else @join_timer = Timer.new(self, interval: @config.delay_joins, shots: 1) { join_lambda.call } end @modes = [] @loggers.info "Connecting to #{@config.server}:#{@config.port}" @irc = IRC.new(self) @irc.start if @config.reconnect && !@quitting # double the delay for each unsuccesful reconnection attempt if @last_connection_was_successful @reconnects = 0 @last_connection_was_successful = false else @reconnects += 1 end # Throttle reconnect attempts wait = 2**@reconnects wait = @config.max_reconnect_delay if wait > @config.max_reconnect_delay @loggers.info "Waiting #{wait} seconds before reconnecting" start_time = Time.now while !@quitting && (Time.now - start_time) < wait sleep 1 end end end while @config.reconnect and not @quitting end |
- (Object) stop
295 296 297 298 299 300 |
# File 'lib/cinch/bot.rb', line 295 def stop # @plugins.each do |plugin| # plugin.storage.save # plugin.storage.unload # end end |
- (Boolean) strict?
True if the bot reports ISUPPORT violations as exceptions.
335 336 337 |
# File 'lib/cinch/bot.rb', line 335 def strict? @config.strictness == :strict end |
- synchronize(name) { ... }
This method returns an undefined value.
Since Cinch uses threads, all handlers can be run simultaneously, even the same handler multiple times. This also means, that your code has to be thread-safe. Most of the time, this is not a problem, but if you are accessing stored data, you will most likely have to synchronize access to it. Instead of managing all mutexes yourself, Cinch provides a synchronize method, which takes a name and block.
Synchronize blocks with the same name share the same mutex, which means that only one of them will be executed at a time.
160 161 162 163 164 165 |
# File 'lib/cinch/bot.rb', line 160 def synchronize(name, &block) # Must run the default block +/ fetch in a thread safe way in order to # ensure we always get the same mutex for a given name. semaphore = @semaphores_mutex.synchronize { @semaphores[name] } semaphore.synchronize(&block) end |
- unset_mode(mode)
This method returns an undefined value.
Unsets a mode on the bot.
389 390 391 392 |
# File 'lib/cinch/bot.rb', line 389 def unset_mode(mode) @modes.delete(mode) @irc.send "MODE #{nick} -#{mode}" end |