diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-12-31 09:44:06 +0100 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2012-12-31 09:44:06 +0100 |
commit | f4423de9f8da6000a6ff42f6a81c3295b18d9e67 (patch) | |
tree | 87f26e82359f463ace515b4b5efe49efe676978c | |
parent | 27b39b805ed823212ba7f8850531e9ebae31be35 (diff) | |
download | lain-f4423de9f8da6000a6ff42f6a81c3295b18d9e67.tar lain-f4423de9f8da6000a6ff42f6a81c3295b18d9e67.zip |
Implement module system and DDate module
-rw-r--r-- | lain.rb | 67 | ||||
-rw-r--r-- | module_base.rb | 11 | ||||
-rw-r--r-- | modules/DDate.rb | 19 |
3 files changed, 75 insertions, 22 deletions
@@ -2,30 +2,53 @@ require 'xmpp4r' -require 'xmpp4r/muc/helper/simplemucclient' +require 'xmpp4r/muc/helper/mucclient' require_relative 'sasl' require_relative 'config' -include Jabber - - -cl = Jabber::Client.new(Jabber::JID.new(LainConfig::JID)) -cl.connect -cl.auth(LainConfig::Password) -cl.send(Presence.new) - -mainthread = Thread.current - -m = Jabber::MUC::MUCClient.new(cl) - -m.add_message_callback { |x| - puts x -} - -LainConfig::Rooms.each { |r| m.join("#{r}/#{LainConfig::Nick}") } - -Thread.stop - -cl.close +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) + + @modules = {} + + @muc.add_message_callback { |msg| + @modules.each { | _, mod | + begin + mod.on_message @muc, msg + rescue + end + } + } + + Config::Modules.each { |mod| + require_relative "modules/#{mod}" + @modules[mod] = Modules.const_get(mod).new self + } + + Config::Rooms.each { |r| @muc.join("#{r}/#{Config::Nick}") } + end + + def run + @mainthread = Thread.current + Thread.stop + end + + def close + @cl.close + end + + end +end + +bot = Lain::Bot.new +bot.run +bot.close diff --git a/module_base.rb b/module_base.rb new file mode 100644 index 0000000..500fd20 --- /dev/null +++ b/module_base.rb @@ -0,0 +1,11 @@ +module Lain + module Modules + class Base + def initialize(lain) + end + + def on_message(muc, message) + end + end + end +end diff --git a/modules/DDate.rb b/modules/DDate.rb new file mode 100644 index 0000000..1402648 --- /dev/null +++ b/modules/DDate.rb @@ -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 |