Class: TinyIRC::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyirc/bot.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file, db_file) ⇒ Bot

Returns a new instance of Bot.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tinyirc/bot.rb', line 18

def initialize(config_file, db_file)
  @config_file = config_file
  
  @sockets = {}
  @plugins = {}
  @groups  = {}

  @log = ParticleLog.new 'bot', ParticleLog::INFO
  @log.important 'Hello, IRC!'
  
  @db = SQLite3::Database.open db_file
  @db.execute <<~SQL
    CREATE TABLE IF NOT EXISTS groupinfo (
      server VARCHAR(32),
      host VARCHAR(64),
      name VARCHAR(32)
    );
  SQL
  @log.info "db = #{db_file}"

  @config_mtx = Mutex.new

  load_config
end

Class Attribute Details

.logObject

Returns the value of attribute log.



3
4
5
# File 'lib/tinyirc/bot.rb', line 3

def log
  @log
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def config
  @config
end

#config_fileObject

Returns the value of attribute config_file.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def config_file
  @config_file
end

#config_mtxObject

Returns the value of attribute config_mtx.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def config_mtx
  @config_mtx
end

#dbObject

Returns the value of attribute db.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def db
  @db
end

#groupsObject

Returns the value of attribute groups.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def groups
  @groups
end

#logObject

Returns the value of attribute log.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def log
  @log
end

#pluginsObject

Returns the value of attribute plugins.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def plugins
  @plugins
end

#prefixObject

Returns the value of attribute prefix.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def prefix
  @prefix
end

#socketsObject

Returns the value of attribute sockets.



6
7
8
# File 'lib/tinyirc/bot.rb', line 6

def sockets
  @sockets
end

Instance Method Details

#_read_line(socket) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'lib/tinyirc/bot.rb', line 293

def _read_line(socket)
  res = IO.select([socket.sock], nil, nil, 0.001)
  return nil unless res
  begin
    return socket.sock.readline("\r\n", chomp: true).force_encoding("UTF-8")
  rescue => e
    socket.log.error "#{e.class.name} - #{e.message}"
    socket.mtx.synchronize do
      socket.running = false
    end
    handle_event(type: :disconnect, socket: socket)
    return nil
  end
end

#add(name, cfg) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/tinyirc/bot.rb', line 73

def add(full, name)
  n = File.basename(name)
  if @plugins.include? n
    @log.error "Cannot have multiple plugins with same basename (#{n})"
  else
    @plugins[n] = TinyIRC::Plugin.new self, full
  end
end

#add_group(name, group_config) ⇒ Object

Raises:

  • (RuntimeError)


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/tinyirc/bot.rb', line 118

def add_group(name, group_config)
  raise RuntimeError, 'User-defined group names cannot contain slashes!' if name.index '/'
  g = TinyIRC::Group.new name
  (group_config[name]['include'] || []).each do |g2|
    if g2.index '/'
      g2p, _ = *g2.split('/', 2)
      unless @plugins.include? g2p
        @log.error "There's no plugin called `#{g2p}`"
        next
      end
      unless @plugins[g2p].groups.include? g2
        @log.error "Plugin `#{g2p}` does not have group `#{g2}`"
        next
      end
      @plugins[g2p].groups[g2].perms.each do |perm|
        g.perm(perm.plugin, perm.command, perm.branch)
      end
    else
      unless @groups.include? g2
        @log.error "There's no group called `#{g2}`"
        next
      end
      @groups[g2].perms.each do |perm|
        g.perm(perm.plugin, perm.command, perm.branch)
      end
    end 
  end
  (group_config[name]['perms'] || []).each do |perm|
    g.perm *TinyIRC::Permission.parse(perm)
  end
  @groups[name] = g
end

#handle_command(h, cmd_info) ⇒ Object



269
270
271
272
273
274
# File 'lib/tinyirc/bot.rb', line 269

def handle_command(h, cmd_info)
  @plugins.each_pair do |name, plugin|
    return true if plugin.handle_command(h, cmd_info)
  end
  false
end

#handle_event(e) ⇒ Object



276
277
278
279
280
281
# File 'lib/tinyirc/bot.rb', line 276

def handle_event(e)
  TinyIRC.define_event_methods(e)
  Thread.new do
    @plugins['core'].handle_event(e)
  end
end

#inspectObject



360
361
362
# File 'lib/tinyirc/bot.rb', line 360

def inspect
  '#<TinyIRC::Bot>'
end

#ioloopObject



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
# File 'lib/tinyirc/bot.rb', line 292

def ioloop
  def _read_line(socket)
    res = IO.select([socket.sock], nil, nil, 0.001)
    return nil unless res
    begin
      return socket.sock.readline("\r\n", chomp: true).force_encoding("UTF-8")
    rescue => e
      socket.log.error "#{e.class.name} - #{e.message}"
      socket.mtx.synchronize do
        socket.running = false
      end
      handle_event(type: :disconnect, socket: socket)
      return nil
    end
  end

  def read_line(socket)
    return unless socket.running
    msg = _read_line(socket)
    return unless msg
    socket.log.io "R> #{msg}"
    handle_event(type: :raw, raw_data: msg, socket: socket, bot: self)
  end

  def write_line(socket)
    return unless socket.running
    
    previous = socket.last_write
    current = Time.now
    diff = current - previous
    return if diff < 0.7 && diff > 0
    # todo - implement bursts

    msg = begin
      socket.queue.pop true
    rescue ThreadError
      nil
    end
    
    return unless msg
    begin
      socket.direct_write msg.force_encoding("UTF-8")
    rescue => e
      socket.log.error "#{e.class.name} - #{e.message}"
      socket.mtx.synchronize do
        socket.running = false
      end
      handle_event(type: :disconnect, socket: socket)
      return nil
    end

    socket.last_write = current
  end

  while true do
    sleep 0.001

    @sockets.each_pair do |sname, socket|
      sleep 0.001

      next unless socket.running

      read_line(socket)
      write_line(socket)
    end
  end
end

#load_configObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/tinyirc/bot.rb', line 43

def load_config
  @config_mtx.synchronize do
    @config = YAML.parse_file(@config_file).to_ruby
    @prefix = @config['prefix'] || '!'
    @log.info "prefix = #{@prefix}"

    load_plugin_config
    load_group_config
    load_cooldown_config
    load_server_config
  end
end

#load_cooldown_configObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/tinyirc/bot.rb', line 172

def load_cooldown_config
  cooldown_config = @config['cooldowns'] || {}

  l = ParticleLog.new('cooldowns', ParticleLog::INFO)

  cooldown_config.each_pair do |cmd, cld|
    cld = cld.to_i
    a = cmd.split('/', 3)
    if !a[0] || !a[1] && a[2]
      l.error "Invalid command entry: #{cld}"
      next
    end
    pname = a[0]
    command = a[1] || :all
    branch = a[2] || :all
    unless @plugins.include? pname
      l.error "There's no plugin called `#{pname}`"
      next
    end
    plugin = @plugins[pname]
    upd = lambda do |command|
      if branch == :all
        command.branches.each_pair do |_, b|
          b.cooldown = cld
          l.info "#{pname}/#{command.name}/#{b.id} <- #{cld}s"
        end
      else
        unless command.branches.include? branch
          l.error "`#{pname}/#{command.name}` command does not have branch called `#{branch}`"
          next
        end
        command.branches[branch].cooldown = cld
        l.info "#{pname}/#{command.name}/#{branch} <- #{cld}s"
      end
    end
    if command == :all
      plugin.commands.each_pair do |_, cmd|
        upd.(cmd)
      end
    else
      unless plugin.commands.include? command
        l.error "`#{pname}/#{command}` command does not have branch called `#{branch}`"
        next
      end
      upd.(plugin.commands[command])
    end
  end
end

#load_group_configObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/tinyirc/bot.rb', line 110

def load_group_config
  @groups = {}

  group_config = {
    "world" => {},
    "admin" => {}
  }.merge(@config['groups'] || {})

  def add_group(name, group_config)
    raise RuntimeError, 'User-defined group names cannot contain slashes!' if name.index '/'
    g = TinyIRC::Group.new name
    (group_config[name]['include'] || []).each do |g2|
      if g2.index '/'
        g2p, _ = *g2.split('/', 2)
        unless @plugins.include? g2p
          @log.error "There's no plugin called `#{g2p}`"
          next
        end
        unless @plugins[g2p].groups.include? g2
          @log.error "Plugin `#{g2p}` does not have group `#{g2}`"
          next
        end
        @plugins[g2p].groups[g2].perms.each do |perm|
          g.perm(perm.plugin, perm.command, perm.branch)
        end
      else
        unless @groups.include? g2
          @log.error "There's no group called `#{g2}`"
          next
        end
        @groups[g2].perms.each do |perm|
          g.perm(perm.plugin, perm.command, perm.branch)
        end
      end 
    end
    (group_config[name]['perms'] || []).each do |perm|
      g.perm *TinyIRC::Permission.parse(perm)
    end
    @groups[name] = g
  end

  def add(name, group_config)
    add_group(name, group_config)
  end

  def remove(name)
    @groups.delete name
  end

  (@groups.keys & group_config.keys).each do |k|
    add_group(k, group_config)
  end

  (@groups.keys - group_config.keys).each do |k|
    remove k
  end

  (group_config.keys - @groups.keys).each do |k|
    add(k, group_config)
  end
end

#load_plugin_configObject



70
71
72
73
74
75
76
77
78
79
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
# File 'lib/tinyirc/bot.rb', line 70

def load_plugin_config
  plugin_config = { "tinyirc/plugins/core" => nil }.merge(@config['plugins'] || {})

  def add(full, name)
    n = File.basename(name)
    if @plugins.include? n
      @log.error "Cannot have multiple plugins with same basename (#{n})"
    else
      @plugins[n] = TinyIRC::Plugin.new self, full
    end
  end

  def remove(name)
    @plugins.delete name
  end

  pkeys = {}
  plugin_config.keys.each do |k|
    pkeys[File.basename(k)] = k
  end
  
  (@plugins.keys - pkeys.keys).each do |k|
    @plugins[k].log.info 'Destroying...'
    remove k
  end

  (pkeys.keys - @plugins.keys).each do |k|
    add pkeys[k], k
  end

  @plugins.each_pair do |k, v|
    cfg = plugin_config[k] || {}
    raise RuntimeError, 'Invalid config type' if cfg.class != Hash
    v._l_config(cfg)
    unless v.loaded
      v._l_postinit
    end
  end
end

#load_server_configObject



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
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
# File 'lib/tinyirc/bot.rb', line 221

def load_server_config
  server_config = @config['servers'] || {}

  def add(name, cfg)
    s = TinyIRC::IRCSocket.new(self, name, cfg)
    Thread.new { s.connect }
    @sockets[name] = s
  end

  def remove(name)
    @sockets[name].mtx.synchronize do
      @sockets.delete(name).disconnect
    end
  end

  (@sockets.keys & server_config.keys).each do |k|
    s = @sockets[k]
    c = server_config[k]
    Thread.new do
      #s.mtx.synchronize do
        should_restart = false
        should_restart = true if s.host != c['host']
        should_restart = true if s.port != c['port']
        should_restart = true if s.user != c['user']
        should_restart = true if s.rnam != c['rnam']

        s.disconnect if should_restart
        
        s.host = c['host']
        s.port = c['port']
        s.nick = c['nick']
        s.user = c['user']
        s.pass = c['pass']
        s.rnam = c['rnam']

        s.connect if should_restart
    end
  end

  (@sockets.keys - server_config.keys).each do |k|
    remove k
  end

  (server_config.keys - @sockets.keys).each do |k|
    add(k, server_config[k])
  end
end

#read_line(socket) ⇒ Object



308
309
310
311
312
313
314
# File 'lib/tinyirc/bot.rb', line 308

def read_line(socket)
  return unless socket.running
  msg = _read_line(socket)
  return unless msg
  socket.log.io "R> #{msg}"
  handle_event(type: :raw, raw_data: msg, socket: socket, bot: self)
end

#reloadObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tinyirc/bot.rb', line 56

def reload
  @config_mtx.synchronize do
    @plugins = {}
    @config = YAML.parse_file(@config_file).to_ruby
    @prefix = @config['prefix'] || '!'
    @log.info "prefix = #{@prefix}"

    load_plugin_config
    load_group_config
    load_cooldown_config
    load_server_config
  end
end

#remove(name) ⇒ Object



82
83
84
# File 'lib/tinyirc/bot.rb', line 82

def remove(name)
  @plugins.delete name
end

#startObject



283
284
285
286
287
288
289
290
# File 'lib/tinyirc/bot.rb', line 283

def start
  Thread.new do
    ioloop
  end

  TinyIRC::App.cfg self
  TinyIRC::App.run!
end

#write_line(socket) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/tinyirc/bot.rb', line 316

def write_line(socket)
  return unless socket.running
  
  previous = socket.last_write
  current = Time.now
  diff = current - previous
  return if diff < 0.7 && diff > 0
  # todo - implement bursts

  msg = begin
    socket.queue.pop true
  rescue ThreadError
    nil
  end
  
  return unless msg
  begin
    socket.direct_write msg.force_encoding("UTF-8")
  rescue => e
    socket.log.error "#{e.class.name} - #{e.message}"
    socket.mtx.synchronize do
      socket.running = false
    end
    handle_event(type: :disconnect, socket: socket)
    return nil
  end

  socket.last_write = current
end