Skip to content

Commit

Permalink
Got things connectng - starting to work out bugs in the responder chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Neufeld authored and Ryan Neufeld committed Mar 13, 2010
1 parent 724c5dc commit 7283861
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -17,5 +17,6 @@ tmtags
coverage
rdoc
pkg
log

## PROJECT::SPECIFIC
3 changes: 2 additions & 1 deletion botfly.gemspec
Expand Up @@ -24,6 +24,7 @@ Gem::Specification.new do |s|
"Rakefile",
"VERSION",
"botfly.gemspec",
"example.rb",
"lib/botfly.rb",
"lib/botfly/bot.rb",
"lib/botfly/matcher.rb",
Expand All @@ -37,7 +38,7 @@ Gem::Specification.new do |s|
s.homepage = %q{http://github.com/rkneufeld/botfly}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.rubygems_version = %q{1.3.5}
s.summary = %q{A quick and easy DSL for generating Jabber bots}
s.test_files = [
"spec/botfly_spec.rb",
Expand Down
10 changes: 8 additions & 2 deletions example.rb
@@ -1,8 +1,14 @@
require 'rubygems'
require 'botfly'

Botfly.login("mucker@limun.org/bot","CD.mucker") do
on.message do
require 'yaml'

config = YAML::load(ARGF.read) if ARGF
puts config.inspect
bot = Botfly.login(config["jid"],config["pass"]) do
on.message.nick(/rkneufeld/) do
say("You don't say!")
end
connect
end
loop {}
10 changes: 5 additions & 5 deletions lib/botfly.rb
Expand Up @@ -9,13 +9,13 @@

module Botfly
def Botfly.logger
return @logger || @logger=Logger.new(STDOUT)
return @logger || @logger = Logger.new(@logfile)
end
def Botfly.login(jid,pass,&block)
Botfly.logger.debug("Botfly#login")
def Botfly.login(jid,pass,logfile=STDOUT,&block)
@logfile = logfile
Botfly.logger.info("Botfly#login")
bot = Botfly::Bot.new(jid,pass)
bot.instance_eval(&block)
Botfly.logger.debug("Done Botfly#login")
return bot
return bot # At this point doesn't get returned, as the thread is stopped
end
end
35 changes: 20 additions & 15 deletions lib/botfly/bot.rb
@@ -1,46 +1,51 @@
require 'rubygems'

jid = 'mucker@limun.org/botfly'
pass = 'CD.mucker'

module Botfly
class Bot
attr_reader :responders
attr_reader :responders, :client
def initialize(jid,pass)
Botfly.logger.debug("Bot#new")
@jid = Jabber::JID.new(jid)
Botfly.logger.info("Bot#new")
@password = pass
@jid = Jabber::JID.new(jid)
@client = Jabber::Client.new(@jid)
end

def on
Botfly.logger.debug("Bot#on")
Botfly.logger.info("Bot#on")
responder = Botfly::Responder.new(@client, self)
(@responders ||= []) << responder
return responder
end

def connect
Botfly.logger.debug("Connecting...")
register_for_xmpp_callbacks
Botfly.logger.info("Connecting to #{@jid}...")
@client.connect
@client.auth(@password)
Botfly.logger.debug("Connected.")
Thread.stop
Botfly.logger.info("Connected")
register_for_xmpp_callbacks

@client.send(Jabber::Presence.new.set_status("Carrier has arrived"))
#Thread.stop
end

private

def register_for_xmpp_callbacks
Botfly.logger.debug("Registering for callbacks with @client")
Botfly.logger.info("Registering for callbacks with client")
# @client.add_update_callback {|presence| respond_to(:update, :presence => presence) }
@client.add_message_callback {|msg| respond_to(:message, :message => message) }
@client.add_presence_callback {|old_presence,new_presence| respond_to(:presence, :old => old_presence, :new => new_presence) }
@client.add_message_callback do |msg|
Botfly.logger.debug("Got message - #{msg.inspect}")
respond_to(:message, :message => message)
end
@client.add_presence_callback do |old_presence,new_presence|
Botfly.logger.debug("Got Presence")
respond_to(:presence, :old => old_presence, :new => new_presence)
end
# @client.add_subscription_request_callback {|item, pres| } # requires Roster helper
end

def respond_to(callback, params)
Botfly.logger.debug("Responding to callback of type: #{callback}")
Botfly.logger.info("Responding to callback of type: #{callback}")
responders = params[:muc] ? @muc_responders[params[:muc]] : @responders
responders.reject {|r| r.type != callback}.each {|r| r.callback_with params}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/botfly/matcher/matcher.rb
@@ -1,7 +1,7 @@
module Botfly
class Matcher
def initialize(condition)
Botfly.logger.debug("Creating Matcher")
Botfly.logger.info("Creating Matcher")
@condition = condition
end
def match(params)
Expand Down
15 changes: 8 additions & 7 deletions lib/botfly/responder.rb
Expand Up @@ -6,37 +6,38 @@ class Responder
attr_reader :callback, :type

def initialize(client,bot)
Botfly.logger.debug("Responder#new")
Botfly.logger.info("Responder#new")
@matcher_chain = []
@parent_bot = bot
@client = client
end

def method_missing(method,condition=nil,&block)
Botfly.logger.debug("Responder##{method}(#{condition.inspect})")
Botfly.logger.info("Responder##{method}(#{condition.inspect})")
if condition # method is matcher name
klass = Botfly.const_get(method.capitalize + "Matcher")
klass = Botfly.const_get(method.to_s.capitalize + "Matcher")
@matcher_chain << klass.new(condition)
Botfly.logger.debug("Matcher chain: #{@matcher_chain.inspect}")
#could rescue NameError, but this way we raise that it doesn't exist
else # method is callback name
# TODO: Check callback is in acceptable list - MUC subclass can override this list
@type = method.to_sym
end
if block_given? && @type
Botfly.logger.debug("Callback recorded")
Botfly.logger.info("Callback recorded")
@callback = block
end
return self
end

def callback_with(params)
@p=params
Botfly.logger.debug("Launching callback")
Botfly.logger.info("Launching callback")
instance_eval @callback
end

def say(msg) #delegate this to the object
@client.say(msg)
def send(nick,msg) #delegate this to the object
@client.send(Jabber::Message.new(nick,msg))
end

# TODO: add other @client actions as delegates
Expand Down

0 comments on commit 7283861

Please sign in to comment.