diff --git a/config.yml.example b/config.yml.example index 4c29fb3..d4bb0c6 100644 --- a/config.yml.example +++ b/config.yml.example @@ -5,3 +5,5 @@ account: options: admins: ['admin@server'] autoauthorize: false + autologin: true + defaultstatus: "I'm on and off for testing..." diff --git a/jabberbot.rb b/jabberbot.rb index 2e0d55b..9fbabf6 100755 --- a/jabberbot.rb +++ b/jabberbot.rb @@ -1,4 +1,5 @@ #!/usr/bin/ruby + # This Class initializes the bot which is defined in xxrb.rb # it defines commands that make use of core functionality within xxrb # and adds them to the bot. @@ -101,7 +102,7 @@ def xmpp_dvb.action parsed = JSON::parse(result.body) list = parsed.map do |line| out = "" unless out - out += line[2] +"\t"+ line[0] +"\t"+ line[1] + "\n" + out += line[2] +"min\t"+ line[0] +"\t"+ line[1] + "\n" end result = "Results for "+@args+":\n" + list.to_s else @@ -176,6 +177,8 @@ def cli_remove.action # Here we add our commands to the bot # Some will later be moved into the Xxrb class and become basic functionality + + #bot.fallback = lambda { |command, args| puts command } bot.add_cmd(cli_help) bot.add_cmd(cli_send) bot.add_cmd(cli_chat) @@ -183,7 +186,7 @@ def cli_remove.action bot.add_cmd(cli_remove) bot.add_cmd(cli_connect) #bot.add_cmd(cli_listen) - #FIXME bot.add_cmd(cli_status) + bot.add_cmd(cli_status) bot.add_cmd(cli_roster) bot.add_cmd(xmpp_hello) diff --git a/xxrb.rb b/xxrb.rb index 11b8005..85ae171 100644 --- a/xxrb.rb +++ b/xxrb.rb @@ -6,12 +6,15 @@ include Jabber -#inspired by xmpp4r example client class Xxrb + attr_writer :cli_fallback, :xmpp_fallback + attr_reader :cli_fallback, :xmpp_fallback, :cli_cmds, :xmpp_cmds + def initialize @cli_cmds = {} @xmpp_cmds = {} + if File.exists?('config.yml') file = File.open('config.yml') conf = YAML::load(file) @@ -20,22 +23,23 @@ def initialize @jid = conf['account']['jid'] @password = conf['account']['password'] @autoauthorize = conf['options']['autoauthorize'] + @autologin = conf['options']['autologin'] + + @cli_fallback = lambda { |command, args| puts command + ' is not a command' } + @xmpp_fallback = lambda { |command, args| puts command + ' is not a command' } else puts "Error: no config file" Thread.current.exit end + + if @autologin + connect + status(conf['options']['defaultstatus']) + end end # Begin of getters - def cli_cmds - @cli_cmds - end - - def xmpp_cmds - @xmpp_cmds - end - def roster get_roster @roster_string @@ -91,14 +95,19 @@ def cmds(pool) def take_cmd(pool, line) command, args = line.split(' ', 2) unless line.nil? if command - unless pool[command.to_sym] == nil - action = lambda { pool[command.to_sym].execute(args) } - else - action = proc { 'command "'+command+'" not found' } - end + unless pool[command.to_sym] == nil + action = lambda { pool[command.to_sym].execute(args) } + else + if pool == @cli_cmds + action = lambda{ @cli_fallback.call(command, args) } + else + action = lambda{ @xmpp_fallback.call(command, args) } + end + end else action = proc {} end + action end @@ -179,6 +188,8 @@ def accept_subscribers end end + + # sends a message to a recipient either as type (:chat, :groupchat, :headline, :normal ) def send(type, recipient) body = gets.strip message = Message.new(JID.new(recipient),body) @@ -186,6 +197,7 @@ def send(type, recipient) @client.send(message) end + # initializes roster def init_roster if @client @@ -198,6 +210,7 @@ def init_roster end end + # lists roster def get_roster if @roster @@ -253,4 +266,3 @@ def remove (jid) end end -