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

86 lines
1.6 KiB
Ruby
Raw Normal View History

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