Class: Jabber::Protocol::Message
- Inherits:
-
Object
- Object
- Jabber::Protocol::Message
- Defined in:
- lib/jabber4r/protocol.rb
Constant Summary collapse
- NORMAL =
"normal"
- ERROR =
"error"
- CHAT =
"chat"
- GROUPCHAT =
"groupchat"
- HEADLINE =
"headline"
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#error ⇒ Object
Returns the value of attribute error.
-
#errorcode ⇒ Object
Returns the value of attribute errorcode.
-
#from ⇒ Object
Returns the value of attribute from.
-
#id ⇒ Object
Returns the value of attribute id.
-
#oobData ⇒ Object
Returns the value of attribute oobData.
-
#subject ⇒ Object
Returns the value of attribute subject.
-
#thread ⇒ Object
Returns the value of attribute thread.
-
#to ⇒ Object
Returns the value of attribute to.
-
#type ⇒ Object
Returns the value of attribute type.
-
#x ⇒ Object
Returns the value of attribute x.
-
#xhtml ⇒ Object
Returns the value of attribute xhtml.
Class Method Summary collapse
-
.from_element(session, element) ⇒ Object
Factory to build a Message from an XMLElement.
Instance Method Summary collapse
-
#initialize(to, type = NORMAL) ⇒ Message
constructor
Creates a Message.
-
#reply ⇒ Object
Builds a reply to an existing message by setting: 1.
-
#request(ttl = nil, &block) ⇒ Object
Convenience method for send(true).
-
#send(wait = false, ttl = nil, &block) ⇒ Object
Sends the message to the Jabber service for delivery.
-
#session=(session) ⇒ Object
Sets the session instance.
-
#set_body(body) ⇒ Object
Chaining method…sets the body of the message.
-
#set_error(code, reason) ⇒ Object
Sets an error code to be returned(chaining method).
-
#set_outofband(data) ⇒ Object
Chaining method…sets the OOB data of the message.
-
#set_subject(subject) ⇒ Object
Chaining method…sets the subject of the message.
-
#set_thread(thread) ⇒ Object
Chaining method…sets the thread of the message.
-
#set_x(x) ⇒ Object
Chaining method…sets the extended data of the message.
-
#set_xhtml(xhtml) ⇒ Object
Chaining method…sets the XHTML body of the message.
-
#to_s ⇒ Object
see to_xml.
-
#to_xml ⇒ Object
Generates XML that complies with the Jabber protocol for sending the message through the Jabber service.
Constructor Details
#initialize(to, type = NORMAL) ⇒ Message
Creates a Message
- to
- String | Jabber::JID
-
The jabber id to send this message to (or from)
- type
- Integer=NORMAL
-
The type of message…Message::(NORMAL, CHAT, GROUPCHAT, HEADLINE)
670 671 672 673 674 675 |
# File 'lib/jabber4r/protocol.rb', line 670 def initialize(to, type=NORMAL) return unless to to = Jabber::JID.new(to) if to.kind_of? String @to = to if to.kind_of? Jabber::JID @type = type end |
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def body @body end |
#error ⇒ Object
Returns the value of attribute error.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def error @error end |
#errorcode ⇒ Object
Returns the value of attribute errorcode.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def errorcode @errorcode end |
#from ⇒ Object
Returns the value of attribute from.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def from @from end |
#id ⇒ Object
Returns the value of attribute id.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def id @id end |
#oobData ⇒ Object
Returns the value of attribute oobData.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def oobData @oobData end |
#subject ⇒ Object
Returns the value of attribute subject.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def subject @subject end |
#thread ⇒ Object
Returns the value of attribute thread.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def thread @thread end |
#to ⇒ Object
Returns the value of attribute to.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def to @to end |
#type ⇒ Object
Returns the value of attribute type.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def type @type end |
#x ⇒ Object
Returns the value of attribute x.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def x @x end |
#xhtml ⇒ Object
Returns the value of attribute xhtml.
636 637 638 |
# File 'lib/jabber4r/protocol.rb', line 636 def xhtml @xhtml end |
Class Method Details
.from_element(session, element) ⇒ Object
Factory to build a Message from an XMLElement
- session
- Jabber::Session
-
The Jabber session instance
- element
- Jabber::Protocol::ParsedXMLElement
-
The received XML object
- return
- Jabber::Protocol::Message
-
The newly created Message object
650 651 652 653 654 655 656 657 658 659 660 661 662 |
# File 'lib/jabber4r/protocol.rb', line 650 def Message.from_element(session, element) = Message.new(element.attr_to) .from = Jabber::JID.new(element.attr_from) if element.attr_from .type = element.attr_type .id = element.attr_id .thread = element.thread.element_data .body = element.body.element_data .xhtml = element.xhtml.element_data .subject = element.subject.element_data .oobData = element.x.element_data .session=session return end |
Instance Method Details
#reply ⇒ Object
Builds a reply to an existing message by setting:
-
to = from
-
id = id
-
thread = thread
-
type = type
-
session = session
- return
- Jabber::Protocol::Message
-
The reply message
827 828 829 830 831 832 833 834 835 836 |
# File 'lib/jabber4r/protocol.rb', line 827 def reply = Message.new(nil) .to = @from .id = @id .thread = @thread .type = @type .session = @session @is_reply = true return end |
#request(ttl = nil, &block) ⇒ Object
Convenience method for send(true)
- ttl
- Integer = nil
-
The time (in seconds) to wait for a reply before assuming nil
- &block
- Block
-
A block to process the message replies
764 765 766 |
# File 'lib/jabber4r/protocol.rb', line 764 def request(ttl=nil, &block) send(true, ttl, &block) end |
#send(wait = false, ttl = nil, &block) ⇒ Object
Sends the message to the Jabber service for delivery
- wait
- Boolean = false
-
Wait for reply before return?
- ttl
- Integer = nil
-
The time (in seconds) to wait for a reply before assuming nil
- &block
- Block
-
A block to process the message replies
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 |
# File 'lib/jabber4r/protocol.rb', line 775 def send(wait=false, ttl=nil, &block) if wait = nil blockedThread = Thread.current timer_thread = nil timeout = false unless ttl.nil? timer_thread = Thread.new { sleep ttl timeout = true blockedThread.wakeup } end @session.connection.send(self.to_s, block) do |je| if je.element_tag == "message" and je.thread.element_data == @thread je.consume_element = Message.from_element(@session, je) blockedThread.wakeup unless timeout unless timer_thread.nil? timer_thread.kill timer_thread = nil end end end Thread.stop return else @session.connection.send(self.to_s, block) if @session end end |
#session=(session) ⇒ Object
Sets the session instance
- session
- Jabber::Session
-
The session instance
- return
- Jabber::Protocol::Message
-
The current Message object
812 813 814 815 |
# File 'lib/jabber4r/protocol.rb', line 812 def session=(session) @session = session self end |
#set_body(body) ⇒ Object
Chaining method…sets the body of the message
- body
- String
-
The message body
- return
- Jabber::Protocol::Message
-
The current Message object
683 684 685 686 |
# File 'lib/jabber4r/protocol.rb', line 683 def set_body(body) @body = body.gsub(/[&]/, '&').gsub(/[<]/, '<').gsub(/[']/, ''') self end |
#set_error(code, reason) ⇒ Object
Sets an error code to be returned(chaining method)
- code
- Integer
-
the jabber error code
- reason
- String
-
Why the error was reported
- return
- Jabber::Protocol::Message
-
The current Message object
751 752 753 754 755 756 |
# File 'lib/jabber4r/protocol.rb', line 751 def set_error(code,reason) @errorcode=code @error=reason @type="error" self end |
#set_outofband(data) ⇒ Object
Chaining method…sets the OOB data of the message
- data
- String
-
The message OOB data
- return
- Jabber::Protocol::Message
-
The current Message object
727 728 729 730 |
# File 'lib/jabber4r/protocol.rb', line 727 def set_outofband(data) @oobData = data self end |
#set_subject(subject) ⇒ Object
Chaining method…sets the subject of the message
- subject
- String
-
The message subject
- return
- Jabber::Protocol::Message
-
The current Message object
694 695 696 697 |
# File 'lib/jabber4r/protocol.rb', line 694 def set_subject(subject) @subject = subject.gsub(/[&]/, '&').gsub(/[<]/, '<').gsub(/[']/, ''') self end |
#set_thread(thread) ⇒ Object
Chaining method…sets the thread of the message
- thread
- String
-
The message thread id
- return
- Jabber::Protocol::Message
-
The current Message object
716 717 718 719 |
# File 'lib/jabber4r/protocol.rb', line 716 def set_thread(thread) @thread = thread self end |
#set_x(x) ⇒ Object
Chaining method…sets the extended data of the message
- x
- String
-
The message x data
- return
- Jabber::Protocol::Message
-
The current Message object
738 739 740 741 |
# File 'lib/jabber4r/protocol.rb', line 738 def set_x(x) @x = x self end |
#set_xhtml(xhtml) ⇒ Object
Chaining method…sets the XHTML body of the message
- body
- String
-
The message body
- return
- Jabber::Protocol::Message
-
The current message object
705 706 707 708 |
# File 'lib/jabber4r/protocol.rb', line 705 def set_xhtml(xhtml) @xhtml=xhtml self end |
#to_s ⇒ Object
see to_xml
868 869 870 |
# File 'lib/jabber4r/protocol.rb', line 868 def to_s to_xml end |
#to_xml ⇒ Object
Generates XML that complies with the Jabber protocol for sending the message through the Jabber service.
- return
- String
-
The XML string.
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
# File 'lib/jabber4r/protocol.rb', line 844 def to_xml @thread = Jabber.gen_random_thread if @thread.nil? and (not @is_reply) elem = XMLElement.new("message", {"to"=>@to, "type"=>@type}) elem.add_attribute("id", @id) if @id elem.add_child("thread").add_data(@thread) if @thread elem.add_child("subject").add_data(@subject) if @subject elem.add_child("body").add_data(@body) if @body if @xhtml then t=elem.add_child("xhtml").add_attribute("xmlns","http://www.w3.org/1999/xhtml") t.add_child("body").add_data(@xhtml) end if @type=="error" then e=elem.add_child("error"); e.add_attribute("code",@errorcode) if @errorcode e.add_data(@error) if @error end elem.add_child("x").add_attribute("xmlns", "jabber:x:oob").add_data(@oobData) if @oobData elem.add_xml(@x.to_s) if @x return elem.to_s end |