This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
lain/bot.rb

85 lines
1.6 KiB
Ruby

require 'xmpp4r'
require 'xmpp4r/muc/helper/simplemucclient'
require_relative 'sasl'
require_relative 'config'
module Lain
Version = '0.1'
class Bot
def initialize
$stderr.puts "Lain #{Version}"
@modules = {}
$stderr.print 'Loading modules...'
Config::Modules.each { |mod, cfg|
$stderr.print " #{mod}"
require_relative "modules/#{mod}"
@modules[mod] = Modules.const_get(mod).new(self, cfg)
}
$stderr.puts '.'
@commands = @modules.values.reduce({}) { |c, mod| c.merge mod.commands }.to_a.sort
$stderr.print 'Connecting... '
@cl = Jabber::Client.new(Jabber::JID.new(Config::JID))
@cl.connect
@cl.auth(Config::Password)
@cl.send(Jabber::Presence.new)
$stderr.puts 'connection established.'
@mucs = {}
Config::Rooms.each { |r|
muc = Jabber::MUC::SimpleMUCClient.new(@cl)
@mucs[r] = muc
muc.add_message_callback { |msg|
unless msg.from == r
@modules.each { | _, mod |
begin
mod.on_message muc, msg
rescue
end
}
end
}
$stderr.print "Trying to access room `#{r}'... "
muc.join(r)
begin
muc.configure
rescue
end
$stderr.puts "access granted."
}
$stderr.puts 'Initialization finished.'
end
def commands
@commands
end
def run
@mainthread = Thread.current
Thread.stop
end
def close
@cl.close
end
end
end