Permalink
Browse files

Merge branch 'master' of github.com:rkneufeld/botfly

  • Loading branch information...
2 parents 5dd8372 + 5e10447 commit 9e11473f6ac9da70307eca3f2d38ea1001e96ef6 Ryan Neufeld committed Mar 31, 2010
Showing with 44 additions and 18 deletions.
  1. +1 −1 VERSION
  2. +5 −3 lib/botfly.rb
  3. +21 −14 lib/botfly/bot.rb
  4. +17 −0 lib/botfly/responder/subscription_request_responder.rb
View
@@ -1 +1 @@
-0.2.2
+0.3.0
View
@@ -2,15 +2,17 @@
require 'xmpp4r'
require 'xmpp4r/muc'
-require 'forwardable'
-require 'logger'
+require 'xmpp4r/roster'
require 'botfly/common_block_acceptor'
require 'botfly/responder'
require 'botfly/bot'
require 'botfly/matcher'
require 'botfly/muc_client'
+require 'logger'
+require 'forwardable'
+
Thread.abort_on_exception = true
module Botfly
@@ -22,7 +24,7 @@ def Botfly.login(jid,pass,logfile=STDOUT,&block)
@logfile = logfile
Botfly.logger.info("BOTFLY: #login")
bot = Botfly::Bot.new(jid,pass)
- bot.connect
+ bot.connect # Must connect first, since MUC requires an active connection to initiate
bot.instance_exec(&block)
return bot # At this point doesn't get returned, as the thread is stopped
end
View
@@ -2,51 +2,58 @@
module Botfly
class Bot < CommonBlockAcceptor
- attr_reader :jid
+ attr_accessor :responders, :client, :roster, :jid
- def initialize(jid,pass)
+ def initialize(jid,pass, opts = {})
super
Botfly.logger.info(" BOT: Bot#new")
@password = pass
@jid = Jabber::JID.new(jid)
@client = Jabber::Client.new(@jid)
@main_thread = Thread.current
+ if opts[:gtalk]
+ @host = "talk.google.com"
+ end
end
def connect
- Botfly.logger.info(" BOT: Connecting to #{@jid}...")
- @client.connect
+ Botfly.logger.info(" BOT: Connecting to #{@host || @jid}...")
+ @client.connect(@host)
@client.auth(@password)
Botfly.logger.info(" BOT: Connected")
register_for_callbacks
@client.send(Jabber::Presence.new.set_status("Carrier has arrived"))
+ @roster = Jabber::Roster::Helper.new(@client)
#Thread.stop
end
-
+
def join(room_name,&block)
return Botfly::MUCClient.new(room_name,self,&block)
end
-
+
def quit
@client.close
@main_thread.continue
end
-
- def to_debug_s
- "BOT"
- end
-
+
+ def to_debug_s; "BOT"; end
+
private
def register_for_callbacks
Botfly.logger.info(" BOT: Registering for callbacks with client")
# @client.add_update_callback {|presence| respond_to(:update, :presence => presence) }
-# @client.add_subscription_request_callback {|item, pres| } # requires Roster helper
- @client.add_message_callback do |message|
+ @client.add_subscription_request_callback do |item, pres| # requires Roster helper
+ Botfly.logger.debug(" CB: Got Message")
+ respond_to(:subscription_request, :roster_item => item, :presence => pres)
+ end
+
+ @client.add_message_callback do |message|
Botfly.logger.debug(" CB: Got Message")
respond_to(:message, :message => message)
end
- @client.add_presence_callback do |new_presence,old_presence|
+
+ @client.add_presence_callback do |new_presence,old_presence|
Botfly.logger.debug(" CB: Got Presence")
respond_to(:presence, :old => old_presence, :new => new_presence)
end
@@ -0,0 +1,17 @@
+module Botfly
+ class SubscriptionRequestResponder < Responder
+ extend Forwardable
+ def setup_instance_variables(params)
+ Botfly.logger.debug(" RSP: SubscriptionRequestResponder setting up instance variables")
+ @roster_item = params[:roster_item]
+ @presence = params[:presence]
+ @from = @presence.from
+ end
+
+ def accept
+ @bot.roster.accept_subscription(@from)
+ end
+ def decline
+ @bot.roster.decline_subscription(@from)
+ end
+end

0 comments on commit 9e11473

Please sign in to comment.