Class: XMPPBot::Bot

Inherits:
Object
  • Object
show all
Defined in:
lib/xmppbot/bot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auto_accept_subscriptionsObject

Returns the value of attribute auto_accept_subscriptions.



9
10
11
# File 'lib/xmppbot/bot.rb', line 9

def auto_accept_subscriptions
  @auto_accept_subscriptions
end

#jidObject

Returns the value of attribute jid.



9
10
11
# File 'lib/xmppbot/bot.rb', line 9

def jid
  @jid
end

#log_levelObject

Returns the value of attribute log_level.



9
10
11
# File 'lib/xmppbot/bot.rb', line 9

def log_level
  @log_level
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/xmppbot/bot.rb', line 9

def password
  @password
end

Instance Method Details

#accept_subscription_from(jid) ⇒ Object



54
55
56
57
58
59
# File 'lib/xmppbot/bot.rb', line 54

def accept_subscription_from(jid)
  p = Presence.new
  p.type = "subscribed"
  p.to=jid.scrap_resource
  self.send(p)
end

#accept_subscriptionsObject

accept subscription request from the user then send a subscription request to that same user.



45
46
47
48
49
50
51
52
# File 'lib/xmppbot/bot.rb', line 45

def accept_subscriptions
  on_presence_received do |pres|
    if pres.type == "subscribe"
      accept_subscription_from(pres.from)          
      subscribe_to(pres.from)
    end
  end
end

#announce_presenceObject

You have to call this after a successful connection to notify everyone that you are online. This is called the “initial presence” (see 5.1.1 at xmpp.org/rfcs/rfc3921.html)



103
104
105
106
107
# File 'lib/xmppbot/bot.rb', line 103

def announce_presence
  presence = StropheRuby::Stanza.new
  presence.name="presence"
  send_stanza(presence)    
end

#connectObject

connect, authenticate and start event loop



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xmppbot/bot.rb', line 11

def connect
  StropheRuby::EventLoop.prepare

  @ctx=StropheRuby::Context.new(@log_level)
  @conn=StropheRuby::Connection.new(@ctx)
  @conn.jid = @jid
  @conn.password= @password
  
  @conn.connect do |status|
    if status == StropheRuby::ConnectionEvents::CONNECT
      accept_subscriptions if auto_accept_subscriptions
      keep_alive
    end
    yield(status)
  end

  main_thread = Thread.current      

  #start the event loop in a separate thread      
  Thread.new do
    Thread.current.abort_on_exception = true
            
    @ctx.loop_status=1
    while @ctx.loop_status == 1
      StropheRuby::EventLoop.run_once(@ctx,1)
    end
  
    #shutdown down strophe and wake up the calling thread
    StropheRuby::EventLoop.shutdown
    main_thread.wakeup
  end
end

#disconnectObject

Stop the event loop



88
89
90
# File 'lib/xmppbot/bot.rb', line 88

def disconnect
  StropheRuby::EventLoop.stop(@ctx)
end

#on_message_receivedObject

callback for message stanzas. The parameter sent in the code block is the received Message object.



110
111
112
113
114
# File 'lib/xmppbot/bot.rb', line 110

def on_message_received
  @conn.add_handler("message") do |stanza|
    yield(Message.new(stanza)) if Message.new(stanza).body
  end
end

#on_presence_receivedObject

callback for presence stanzas. The parameter sent in the code block is the received Presence object.



117
118
119
120
121
# File 'lib/xmppbot/bot.rb', line 117

def on_presence_received
  @conn.add_handler("presence") do |stanza|
    yield(Presence.new(stanza))
  end
end

#send(obj) ⇒ Object

Send a message or presence object to the stream



93
94
95
96
97
98
99
# File 'lib/xmppbot/bot.rb', line 93

def send(obj)      
  if obj.respond_to?(:stanza)        
    send_stanza(obj.stanza)      
  else
    raise("Error: StropheRuby::Stanza object has not been set")
  end
end

#subscribe_to(jid) ⇒ Object



61
62
63
64
65
66
# File 'lib/xmppbot/bot.rb', line 61

def subscribe_to(jid)
  p = Presence.new
  p.type = "subscribe"
  p.to = jid.scrap_resource
  self.send(p)    
end

#unsubscribe_from(jid) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/xmppbot/bot.rb', line 68

def unsubscribe_from(jid)
  iq = StropheRuby::Stanza.new
  iq.name="iq"
  iq.type="set"
  
  query = StropheRuby::Stanza.new
  query.name="query"
  query.ns="jabber:iq:roster"
  
  item = StropheRuby::Stanza.new
  item.name="item"
  item.set_attribute("jid",jid.scrap_resource)
  item.set_attribute("subscription","remove")
  
  query.add_child(item)
  iq.add_child(query)
  send_stanza(iq)
end