Class: Butler::Bot
- Inherits:
-
IRC::Client
- Object
- IRC::Client
- Butler::Bot
- Includes:
- Log::Comfort
- Defined in:
- lib/butler/bot.rb
Constant Summary
Constants inherited from IRC::Client
IRC::Client::DefaultOptions, IRC::Client::DefaultTimeout
Instance Attribute Summary collapse
-
#access ⇒ Object
readonly
Returns the value of attribute access.
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#plugins ⇒ Object
readonly
Returns the value of attribute plugins.
-
#scheduler ⇒ Object
readonly
Returns the value of attribute scheduler.
Attributes inherited from IRC::Client
#channel_charset, #channels, #client_charset, #disconnect_callback, #irc, #myself, #server_charset, #users
Instance Method Summary collapse
- #add_command(command) ⇒ Object
- #delete_command(command) ⇒ Object
-
#initialize(path, name, opts = {}) ⇒ Bot
constructor
FIXME, raise if selftest fails.
-
#inspect ⇒ Object
:nodoc:.
-
#invocation(sequence) ⇒ Object
returns the rest of the sequence if it was an invocation, nil else.
- #invoke_commands(listener, message) ⇒ Object
- #login ⇒ Object
- #on_disconnect(reason) ⇒ Object
- #output_to_logfiles ⇒ Object
- #plugins_dispatch(event, *args) ⇒ Object
- #quit(reason = nil, *args) ⇒ Object
-
#selftest ⇒ Object
runs a diagnose, will collect all wrongs and raise if it finds any.
Methods included from Log::Comfort
#debug, #error, #exception, #fail, #info, #log, #warn
Methods inherited from IRC::Client
#event_loop, #filter, #join, #load_command_set, #process, #subscribe, #subscribe_listener, #terminate, #terminate_event_loop, #thread_read, #unsubscribe, #wait_for, #whois
Constructor Details
#initialize(path, name, opts = {}) ⇒ Bot
FIXME, raise if selftest fails
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/butler/bot.rb', line 38 def initialize(path, name, opts={}) path ||= Butler.path @irc = nil # early inspects @name = name @base = "#{path.bots}/#{name}" @path = OpenStruct.new( :access => @base+'/access', :base => @base, :config => @base+'/config', :lib => @base+'/lib', :log => @base+'/log', :plugins => @base+'/plugins', :strings => @base+'/strings' ) $LOAD_PATH.unshift(@path.lib) require 'butlerinit' if File.exist?(@path.lib+"/butlerinit.rb") @logger = $stderr @config = Configuration.new(@base+'/config') @scheduler = Scheduler.new @access = Access.new( Access::YAMLBase.new(Access::User::Base, @base+'/access/user'), Access::YAMLBase.new(Access::Role::Base, @base+'/access/role'), Access::YAMLBase.new(Access::Privilege::Base, @base+'/access/privilege') #:channel => Access::YAMLBase.new("#{TestDir}/channel", Access::Location) ) @on_disconnect = nil @on_reconnect = nil @reconnect = [ opts.delete(:reconnect_delay) || 60, opts.delete(:reconnect_tries) || -1 ] super(@config["connections/main/server"], { :host => @config["connections/main/host"], :port => @config["connections/main/port"], }.merge(opts)) # { lang => { trigger => SortedSet[ *commands ] } } @commands = {} @plugins = Plugins.new(self, @base+'/plugins') if $DEBUG then @irc.extend DebugLog @irc.raw_log = File.open(@path.log+'/debug.log', 'wb') @irc.raw_log.sync = true end subscribe(:PRIVMSG, -10, &method(:invoke_commands)) subscribe(:NOTICE, -10, &method(:invoke_commands)) selftest end |
Instance Attribute Details
#access ⇒ Object (readonly)
Returns the value of attribute access.
29 30 31 |
# File 'lib/butler/bot.rb', line 29 def access @access end |
#base ⇒ Object (readonly)
Returns the value of attribute base.
30 31 32 |
# File 'lib/butler/bot.rb', line 30 def base @base end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
31 32 33 |
# File 'lib/butler/bot.rb', line 31 def config @config end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
32 33 34 |
# File 'lib/butler/bot.rb', line 32 def logger @logger end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
33 34 35 |
# File 'lib/butler/bot.rb', line 33 def path @path end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
34 35 36 |
# File 'lib/butler/bot.rb', line 34 def plugins @plugins end |
#scheduler ⇒ Object (readonly)
Returns the value of attribute scheduler.
35 36 37 |
# File 'lib/butler/bot.rb', line 35 def scheduler @scheduler end |
Instance Method Details
#add_command(command) ⇒ Object
126 127 128 129 130 |
# File 'lib/butler/bot.rb', line 126 def add_command(command) @commands[command.language] ||= {} @commands[command.language][command.trigger] ||= SortedSet.new @commands[command.language][command.trigger].add(command) end |
#delete_command(command) ⇒ Object
132 133 134 135 136 137 138 |
# File 'lib/butler/bot.rb', line 132 def delete_command(command) @commands[command.language][command.trigger].delete(command) if @commands[command.language][command.trigger].empty? then @commands[command.language].delete(command.trigger) @commands.delete(command.language) if @commands[command.language].empty? end end |
#inspect ⇒ Object
:nodoc:
209 210 211 212 213 214 215 216 217 |
# File 'lib/butler/bot.rb', line 209 def inspect # :nodoc: "#<%s:0x%08x %s (in %s) irc=%s>" % [ self.class, object_id << 1, @name, @base, @irc.inspect ] end |
#invocation(sequence) ⇒ Object
returns the rest of the sequence if it was an invocation, nil else
141 142 143 |
# File 'lib/butler/bot.rb', line 141 def invocation(sequence) @myself && sequence[/^(?:!?#{@myself.nick}[:;,])\s+/i] end |
#invoke_commands(listener, message) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/butler/bot.rb', line 91 def invoke_commands(listener, ) return unless ((sequence = invocation(.text)) || .realm == :private) .invocation = sequence || "" .language = "en" trigger = .arguments.first access = .from && .from.access.method(:authorized?) return unless access and trigger trigger = trigger.downcase commands = [] if @commands[.language] && @commands[.language][trigger] then commands.concat(@commands[.language][trigger].to_a) end if .language != "en" && @commands["en"] && @commands["en"][trigger] then commands.concat(@commands["en"][trigger].to_a) end commands.each { |command| if args = (command.invoked_by?()) then if !command. || access.call(command.) then Thread.new { begin command.call(, *args) rescue Exception => e exception(e) end } break if command.abort_invocations? else info("#{.from} (#{.from.access.id}) had no authorization for 'plugin/#{command.plugin.base}'") end end } end |
#login ⇒ Object
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/butler/bot.rb', line 150 def login @access.default_user = @access["default_user"] @access.default_user.login nick = @config["connections/main/nick"] pass = @config["connections/main/password"] super( nick, @config["connections/main/user"], @config["connections/main/real"] ) info("Logged in") if pass && @myself.nick.same_nick?(nick) then @irc.ghost(nick, pass) sleep(1) #Â FIXME, do a wait_for or similar @irc.nick(nick) end @irc.identify(pass) if pass join(*@config["connections/main/channels"]) plugins_dispatch(:on_login, "main") end |
#on_disconnect(reason) ⇒ Object
172 173 174 175 176 177 178 179 180 |
# File 'lib/butler/bot.rb', line 172 def on_disconnect(reason) return if reason == :quit plugins_dispatch(:on_disconnect, reason) unless @reconnect[1].zero? then @reconnect[1] -= 1 sleep(@reconnect[0]) login end end |
#output_to_logfiles ⇒ Object
202 203 204 205 206 207 |
# File 'lib/butler/bot.rb', line 202 def output_to_logfiles # Errors still go to $stderr, $stdin handles puts as "info" level $stderr prints @logger = Log.file(@path.log+'/error.log') $stdout = Log.forward(@logger, :warn) $stderr = Log.forward(@logger, :info) end |
#plugins_dispatch(event, *args) ⇒ Object
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/butler/bot.rb', line 190 def plugins_dispatch(event, *args) Thread.new { # avoid total lockup due to an improperly written on_disconnect begin @plugins.instances.each { |plugin| plugin.send(event, *args) } rescue Exception => e exception(e) end } end |
#quit(reason = nil, *args) ⇒ Object
182 183 184 185 186 187 188 |
# File 'lib/butler/bot.rb', line 182 def quit(reason=nil, *args) timeout(3) { plugins_dispatch(:on_quit, reason, *args).join } ensure super(reason) end |
#selftest ⇒ Object
runs a diagnose, will collect all wrongs and raise if it finds any
146 147 148 |
# File 'lib/butler/bot.rb', line 146 def selftest end |