diff --git a/.gitignore b/.gitignore index d26b33d..62de81d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ working +*.yml +*.swp +*.vim diff --git a/source/common.rb b/source/common.rb new file mode 100644 index 0000000..6ad044b --- /dev/null +++ b/source/common.rb @@ -0,0 +1,15 @@ +module Vrobot4 + Version = '4.00'.freeze + + @@debug = false + def self.debug= set; @@debug = !!set; end + def self.debug ; @@debug; end + + def self.log lv, *text + if lv != :DEBUG || @@debug + puts "[" + lv.to_s.center(8) + "] " + text.join(" ") + end + end +end + +## EOF diff --git a/source/main.rb b/source/main.rb new file mode 100644 index 0000000..26bab13 --- /dev/null +++ b/source/main.rb @@ -0,0 +1,56 @@ +require './common.rb' +require './server.rb' +require './module.rb' + +require 'yaml' + +module Vrobot4 + def self.loadServer servinfo + name = servinfo["name"] + type = servinfo["type"] + + log :INFO, "loadServer: loading configuration for", name + + serv = Server::get_server_type(type).new(servinfo) + + servinfo.has_key?("modules") && servinfo["modules"].each \ + {|mod| serv.loadMod Module::get_module_type(mod).new} + + serv + end + + def self.loadBot botinfo + servers = [] + log :INFO, "loadBot: loading configuration for", botinfo["name"] + botinfo["servers"].each {|servinfo| servers << loadServer(servinfo)} + servers + end + + def self.main cfgname + cfg = nil + + log :INFO, "vrobot version", Version + + begin; cfg = YAML.load IO.read(cfgname) + rescue; log :ERROR, "error reading bot config:", $!; exit; end + + begin + bots = [] + thrds = [] + cfg["loadmods"].each {|mod| load mod, true} + cfg["bots"] .each {|botinfo| bots << loadBot(botinfo)} + log :DEBUG, "bots:", bots.to_s + bots.each do |servs| + servs.each {|serv| thrds << Thread.new {serv.connect}} + end + thrds.each {|th| th.join} + rescue + log :ERROR, "error loading bot configuration:", $! + exit + end + end +end + +Vrobot4.main "bot.yml" + +## EOF diff --git a/source/mod_util.rb b/source/mod_util.rb new file mode 100644 index 0000000..8da430b --- /dev/null +++ b/source/mod_util.rb @@ -0,0 +1,14 @@ +class Mod_Util < Vrobot4::Module::Module + Vrobot4::Module.add_module_type self, "Utilities" + + def initialize + super() + register :testf, "Test function." + end + + def testf msg, argv + puts "test function run", msg, argv + end +end + +## EOF diff --git a/source/module.rb b/source/module.rb new file mode 100644 index 0000000..435f9ed --- /dev/null +++ b/source/module.rb @@ -0,0 +1,45 @@ +module Vrobot4::Module + class Command + attr_reader :help_str + + def initialize fn, help + @function = fn + @help_str = help + end + + def run msg, argv + @function.call(msg, argv) + end + end + + class Module + def initialize + @commands = {} + end + + def register fn, help + @commands[fn.to_s] = Command.new(self.method(fn), help) + end + + def eachCmd + @commands.each {|cmd| yield cmd} + end + + def onCommand msg, cmdname, argv + @commands[cmdname].run(msg, argv) if @commands.has_key? cmdname + end + end + + @@module_types = {} + + def self.add_module_type t, name + @@module_types[name] = t + Vrobot4.log :INFO, "added module type:", name + end + + def self.get_module_type s + @@module_types[s] + end +end + +## EOF diff --git a/source/server.rb b/source/server.rb new file mode 100644 index 0000000..94886c3 --- /dev/null +++ b/source/server.rb @@ -0,0 +1,53 @@ +module Vrobot4::Server + class User + end + + class Message + attr_reader :msg + + def initialize msg + @msg = msg + end + + def to_s + @msg + end + end + + class Channel + end + + class Server + def initialize + @modules = [] + end + + def loadMod mod + @modules << mod + end + + def onMessage msg + if msg.start_with? '.' + onCommand msg, nil, nil + end + log :MSG, msg.to_s + end + + def onCommand msg, cmdname, argv + @modules.each {|mod| mod.onCommand msg, cmdname, argv} + end + end + + @@server_types = {} + + def self.add_server_type t, name + @@server_types[name] = t + Vrobot4.log :INFO, "added server type:", name + end + + def self.get_server_type s + @@server_types[s] + end +end + +## EOF diff --git a/source/sv_discord.rb b/source/sv_discord.rb new file mode 100644 index 0000000..66afb25 --- /dev/null +++ b/source/sv_discord.rb @@ -0,0 +1,14 @@ +class Sv_Discord < Vrobot4::Server::Server + Vrobot4::Server.add_server_type self, "Discord" + + def initialize info + super() + Vrobot4.log :INFO, "Sv_Discord:", info["serverid"] + end + + def connect + puts "connected" + end +end + +## EOF