Skip to content

Commit

Permalink
* added: fallbacks for unknown commands can be overwritten
Browse files Browse the repository at this point in the history
 * added: dvb now displays minutes
 * added: autologin setting
  • Loading branch information
Hendrik Sollich committed Mar 14, 2011
1 parent 6aefbdf commit 5fb27fd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
2 changes: 2 additions & 0 deletions config.yml.example
Expand Up @@ -5,3 +5,5 @@ account:
options:
admins: ['admin@server']
autoauthorize: false
autologin: true
defaultstatus: "I'm on and off for testing..."
7 changes: 5 additions & 2 deletions 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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -176,14 +177,16 @@ 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)
bot.add_cmd(cli_hello)
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)
Expand Down
42 changes: 27 additions & 15 deletions xxrb.rb
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -179,13 +188,16 @@ 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)
message.type=(type)
@client.send(message)
end


# initializes roster
def init_roster
if @client
Expand All @@ -198,6 +210,7 @@ def init_roster
end
end


# lists roster
def get_roster
if @roster
Expand Down Expand Up @@ -253,4 +266,3 @@ def remove (jid)
end

end

0 comments on commit 5fb27fd

Please sign in to comment.