Class | Jabber::Framework::Bot |
In: |
lib/xmpp4r/framework/bot.rb
|
Parent: | Base |
Abstract handler methods that may be implemented by a deriving class:
# File lib/xmpp4r/framework/bot.rb, line 30 30: def initialize(jid, password) 31: cl = Jabber::Client.new(jid) 32: cl.connect 33: cl.auth(password) 34: 35: super(cl) 36: 37: roster.add_subscription_request_callback do |item,presence| 38: if accept_subscription_from?(presence.from.strip) 39: roster.accept_subscription(presence.from.strip) 40: else 41: roster.decline_subscription(presence.from.strip) 42: end 43: end 44: 45: @pep_notifications = [] 46: cl.add_message_callback do |msg| 47: if msg.type != :error and msg.body 48: if (html = msg.first_element('html')) and respond_to? :on_message_xhtml 49: on_message_xhtml(html.body, msg.body) 50: elsif respond_to? :on_message 51: on_message(msg.body) 52: end 53: elsif msg.type != :error and (event = msg.first_element('event')) 54: event.each_element('items') do |items| 55: node = items.attributes['node'] 56: items.each_element('item') do |item| 57: @pep_notifications.each { |notification_node,callback| 58: if node == notification_node 59: callback.call(msg.from, item) 60: end 61: } 62: end 63: end 64: else 65: false 66: end 67: end 68: 69: add_cap('presence') 70: add_cap(Caps::NS_CAPS) 71: add_cap('message') if respond_to? :on_message 72: add_cap(XHTML::NS_XHTML_IM) if respond_to? :on_message_xhtml 73: 74: @presence_show = nil 75: @presence_status = nil 76: end
Front-end for Roster::Helper#add_subscription_request_callback
Can be overwritten, must return true or false
# File lib/xmpp4r/framework/bot.rb, line 89 89: def accept_subscription_from?(jid) 90: true 91: end
# File lib/xmpp4r/framework/bot.rb, line 141 141: def add_pep_notification(node, &callback) 142: add_cap("#{node}+notify") 143: @pep_notifications << [node, callback] 144: end
Send a simple text chat message
# File lib/xmpp4r/framework/bot.rb, line 95 95: def send_message(to, text) 96: msg = Message.new 97: msg.type = :chat 98: msg.to = to 99: msg.body = text 100: @stream.send(msg) 101: end
Send an XHTML chat message
text: | [String] alternate plain text body, generated from xhtml_contents if nil |
# File lib/xmpp4r/framework/bot.rb, line 106 106: def send_message_xhtml(to, xhtml_contents, text=nil) 107: msg = Message.new 108: msg.type = :chat 109: msg.to = to 110: html = msg.add(XHTML::HTML.new(xhtml_contents)) 111: msg.body = text ? text : html.to_text 112: @stream.send(msg) 113: end
# File lib/xmpp4r/framework/bot.rb, line 125 125: def send_presence 126: roster.wait_for_roster 127: 128: # TODO: vcard photo hash 129: if @presence_show == :unavailable 130: presence = Presence.new(nil, @presence_status) 131: presence.type = :unavailable 132: else 133: presence = Presence.new(@presence_show, @presence_status) 134: end 135: presence.add(disco_caps.generate_caps) 136: @stream.send(presence) 137: end