Class: XMPPBot::Bot
- Inherits:
-
Object
- Object
- XMPPBot::Bot
- Defined in:
- lib/xmppbot/bot.rb
Instance Attribute Summary collapse
-
#auto_accept_subscriptions ⇒ Object
Returns the value of attribute auto_accept_subscriptions.
-
#jid ⇒ Object
Returns the value of attribute jid.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#password ⇒ Object
Returns the value of attribute password.
Instance Method Summary collapse
- #accept_subscription_from(jid) ⇒ Object
-
#accept_subscriptions ⇒ Object
accept subscription request from the user then send a subscription request to that same user.
-
#announce_presence ⇒ Object
You have to call this after a successful connection to notify everyone that you are online.
-
#connect ⇒ Object
connect, authenticate and start event loop.
-
#disconnect ⇒ Object
Stop the event loop.
-
#on_message_received ⇒ Object
callback for message stanzas.
-
#on_presence_received ⇒ Object
callback for presence stanzas.
-
#send(obj) ⇒ Object
Send a message or presence object to the stream.
- #subscribe_to(jid) ⇒ Object
- #unsubscribe_from(jid) ⇒ Object
Instance Attribute Details
#auto_accept_subscriptions ⇒ Object
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 |
#jid ⇒ Object
Returns the value of attribute jid.
9 10 11 |
# File 'lib/xmppbot/bot.rb', line 9 def jid @jid end |
#log_level ⇒ Object
Returns the value of attribute log_level.
9 10 11 |
# File 'lib/xmppbot/bot.rb', line 9 def log_level @log_level end |
#password ⇒ Object
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_subscriptions ⇒ Object
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_presence ⇒ Object
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 |
#connect ⇒ Object
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 |
#disconnect ⇒ Object
Stop the event loop
88 89 90 |
# File 'lib/xmppbot/bot.rb', line 88 def disconnect StropheRuby::EventLoop.stop(@ctx) end |
#on_message_received ⇒ Object
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 @conn.add_handler("message") do |stanza| yield(Message.new(stanza)) if Message.new(stanza).body end end |
#on_presence_received ⇒ Object
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 |