Implement module system and DDate module
This commit is contained in:
parent
27b39b805e
commit
f4423de9f8
3 changed files with 67 additions and 14 deletions
51
lain.rb
51
lain.rb
|
@ -2,30 +2,53 @@
|
||||||
|
|
||||||
|
|
||||||
require 'xmpp4r'
|
require 'xmpp4r'
|
||||||
require 'xmpp4r/muc/helper/simplemucclient'
|
require 'xmpp4r/muc/helper/mucclient'
|
||||||
|
|
||||||
require_relative 'sasl'
|
require_relative 'sasl'
|
||||||
|
|
||||||
require_relative 'config'
|
require_relative 'config'
|
||||||
|
|
||||||
include Jabber
|
module Lain
|
||||||
|
class Bot
|
||||||
|
def initialize
|
||||||
|
@cl = Jabber::Client.new(Jabber::JID.new(Config::JID))
|
||||||
|
@cl.connect
|
||||||
|
@cl.auth(Config::Password)
|
||||||
|
@cl.send(Jabber::Presence.new)
|
||||||
|
|
||||||
|
@muc = Jabber::MUC::MUCClient.new(@cl)
|
||||||
|
|
||||||
cl = Jabber::Client.new(Jabber::JID.new(LainConfig::JID))
|
@modules = {}
|
||||||
cl.connect
|
|
||||||
cl.auth(LainConfig::Password)
|
|
||||||
cl.send(Presence.new)
|
|
||||||
|
|
||||||
mainthread = Thread.current
|
@muc.add_message_callback { |msg|
|
||||||
|
@modules.each { | _, mod |
|
||||||
|
begin
|
||||||
|
mod.on_message @muc, msg
|
||||||
|
rescue
|
||||||
|
end
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
m = Jabber::MUC::MUCClient.new(cl)
|
Config::Modules.each { |mod|
|
||||||
|
require_relative "modules/#{mod}"
|
||||||
|
@modules[mod] = Modules.const_get(mod).new self
|
||||||
|
}
|
||||||
|
|
||||||
m.add_message_callback { |x|
|
Config::Rooms.each { |r| @muc.join("#{r}/#{Config::Nick}") }
|
||||||
puts x
|
end
|
||||||
}
|
|
||||||
|
|
||||||
LainConfig::Rooms.each { |r| m.join("#{r}/#{LainConfig::Nick}") }
|
def run
|
||||||
|
@mainthread = Thread.current
|
||||||
|
Thread.stop
|
||||||
|
end
|
||||||
|
|
||||||
Thread.stop
|
def close
|
||||||
|
@cl.close
|
||||||
|
end
|
||||||
|
|
||||||
cl.close
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
bot = Lain::Bot.new
|
||||||
|
bot.run
|
||||||
|
bot.close
|
||||||
|
|
11
module_base.rb
Normal file
11
module_base.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
module Lain
|
||||||
|
module Modules
|
||||||
|
class Base
|
||||||
|
def initialize(lain)
|
||||||
|
end
|
||||||
|
|
||||||
|
def on_message(muc, message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
19
modules/DDate.rb
Normal file
19
modules/DDate.rb
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
require 'xmpp4r/message'
|
||||||
|
|
||||||
|
require_relative '../module_base'
|
||||||
|
|
||||||
|
module Lain
|
||||||
|
module Modules
|
||||||
|
class DDate < Base
|
||||||
|
def on_message(muc, message)
|
||||||
|
return unless message.type == :groupchat
|
||||||
|
return unless /!ddate\b/ =~ message.body
|
||||||
|
|
||||||
|
p = IO.popen("ddate")
|
||||||
|
while (line = p.gets)
|
||||||
|
muc.send(Jabber::Message.new(message.to, line.chomp))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Reference in a new issue