Class: Butler::Bot

Inherits:
IRC::Client show all
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

Attributes inherited from IRC::Client

#channel_charset, #channels, #client_charset, #irc, #myself, #server_charset, #users

Instance Method Summary collapse

Methods included from Log::Comfort

#debug, #error, #exception, #fail, #info, #log, #warn

Methods inherited from IRC::Client

#event_loop, #filter, #join, #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'
	)
	@log_device = $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"], {
		:port => @config["connections.main.port"],
		:host => @config["connections.main.host"]
	}.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

#accessObject (readonly)

Returns the value of attribute access.



27
28
29
# File 'lib/butler/bot.rb', line 27

def access
  @access
end

#baseObject (readonly)

Returns the value of attribute base.



28
29
30
# File 'lib/butler/bot.rb', line 28

def base
  @base
end

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/butler/bot.rb', line 29

def config
  @config
end

#log_deviceObject (readonly)

Returns the value of attribute log_device.



30
31
32
# File 'lib/butler/bot.rb', line 30

def log_device
  @log_device
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/butler/bot.rb', line 31

def path
  @path
end

#pluginsObject (readonly)

Returns the value of attribute plugins.



32
33
34
# File 'lib/butler/bot.rb', line 32

def plugins
  @plugins
end

#schedulerObject (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

#inspectObject

: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, message)
	return unless ((sequence = invocation(message.text)) || message.realm == :private)
	message.invocation = sequence || ""
	message.language   = "en"
	trigger            = message.arguments.first
	access             = message.from && message.from.access.method(:authorized?)
	return unless access and trigger
	
	trigger  = trigger.downcase
	commands = []
	if @commands[message.language] && @commands[message.language][trigger] then
		commands.concat(@commands[message.language][trigger].to_a)
	end
	if message.language != "en" && @commands["en"] && @commands["en"][trigger] then
		commands.concat(@commands["en"][trigger].to_a)
	end

	commands.each { |command|
		if args = (command.invoked_by?(message)) then
			if !command.authorization || access.call(command.authorization) then
				Thread.new {
					begin
						command.call(message, *args)
					rescue Exception => e
						exception(e)
					end
				}
				break if command.abort_invocations?
			else
				info("#{message.from} (#{message.from.access.id}) had no authorization for 'plugin/#{command.plugin.base}'")
			end
		end
	}
end

#loginObject



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 
	@access.default_user = @access["default_user"]
	@access.default_user.

	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])
		
	end
end

#output_to_logfilesObject



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
	@log_device = Log.file(@path.log+'/error.log')
	$stdout     = Log.forward(@log_device, :warn)
	$stderr     = Log.forward(@log_device, :info)
end

#selftestObject

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