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, #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
-
#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, #quit, #subscribe, #terminate, #terminate_event_loop, #thread_read, #unsubscribe, #wait_for, #whois
Constructor Details
#initialize(path, name, opts = {}) ⇒ Bot
FIXME, raise if selftest fails
36 37 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 |
# File 'lib/butler/bot.rb', line 36 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', :log => @base+'/log', :plugins => @base+'/plugins', :strings => @base+'/strings' ) @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') 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.
27 28 29 |
# File 'lib/butler/bot.rb', line 27 def access @access end |
#base ⇒ Object (readonly)
Returns the value of attribute base.
28 29 30 |
# File 'lib/butler/bot.rb', line 28 def base @base end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
29 30 31 |
# File 'lib/butler/bot.rb', line 29 def config @config end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
30 31 32 |
# File 'lib/butler/bot.rb', line 30 def logger @logger end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
31 32 33 |
# File 'lib/butler/bot.rb', line 31 def path @path end |
#plugins ⇒ Object (readonly)
Returns the value of attribute plugins.
32 33 34 |
# File 'lib/butler/bot.rb', line 32 def plugins @plugins end |
#scheduler ⇒ Object (readonly)
Returns the value of attribute scheduler.
33 34 35 |
# File 'lib/butler/bot.rb', line 33 def scheduler @scheduler end |
Instance Method Details
#add_command(command) ⇒ Object
115 116 117 118 119 |
# File 'lib/butler/bot.rb', line 115 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
121 122 123 124 125 126 127 |
# File 'lib/butler/bot.rb', line 121 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:
178 179 180 181 182 183 184 185 186 |
# File 'lib/butler/bot.rb', line 178 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
130 131 132 |
# File 'lib/butler/bot.rb', line 130 def invocation(sequence) @myself && sequence[/^(?:!?#{@myself.nick}[:;,])\s+/i] end |
#invoke_commands(listener, message) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/butler/bot.rb', line 80 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
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/butler/bot.rb', line 139 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") # FIXME, use #same_nick? if pass && @myself.nick.downcase != nick.downcase 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"]) end |
#on_disconnect(reason) ⇒ Object
161 162 163 164 165 166 167 168 |
# File 'lib/butler/bot.rb', line 161 def on_disconnect(reason) return if reason == :quit unless @reconnect[1].zero? then @reconnect[1] -= 1 sleep(@reconnect[0]) login end end |
#output_to_logfiles ⇒ Object
171 172 173 174 175 176 |
# File 'lib/butler/bot.rb', line 171 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 |
#selftest ⇒ Object
runs a diagnose, will collect all wrongs and raise if it finds any
135 136 137 |
# File 'lib/butler/bot.rb', line 135 def selftest end |