Class: Jabber::Protocol::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/jabber4r/protocol.rb

Constant Summary collapse

NORMAL =
"normal"
ERROR =
"error"
CHAT =
"chat"
GROUPCHAT =
"groupchat"
HEADLINE =
"headline"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bodyObject

Returns the value of attribute body.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def body
  @body
end

#errorObject

Returns the value of attribute error.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def error
  @error
end

#errorcodeObject

Returns the value of attribute errorcode.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def errorcode
  @errorcode
end

#fromObject

Returns the value of attribute from.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def from
  @from
end

#idObject

Returns the value of attribute id.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def id
  @id
end

#oobDataObject

Returns the value of attribute oobData.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def oobData
  @oobData
end

#subjectObject

Returns the value of attribute subject.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def subject
  @subject
end

#threadObject

Returns the value of attribute thread.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def thread
  @thread
end

#toObject

Returns the value of attribute to.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def to
  @to
end

#typeObject

Returns the value of attribute type.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def type
  @type
end

#xObject

Returns the value of attribute x.



636
637
638
# File 'lib/jabber4r/protocol.rb', line 636

def x
  @x
end

#xhtmlObject

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 = Message.new(element.attr_to)
  message.from = Jabber::JID.new(element.attr_from) if element.attr_from
  message.type = element.attr_type
  message.id = element.attr_id
  message.thread = element.thread.element_data
  message.body = element.body.element_data
  message.xhtml = element.xhtml.element_data
  message.subject = element.subject.element_data
  message.oobData = element.x.element_data
  message.session=session
  return message
end

Instance Method Details

#replyObject

Builds a reply to an existing message by setting:

  1. to = from

  2. id = id

  3. thread = thread

  4. type = type

  5. 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 = Message.new(nil)
  message.to = @from
  message.id = @id
  message.thread = @thread
  message.type = @type
  message.session = @session
  @is_reply = true
  return message
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
    message = 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 = 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 message
  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(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
  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(/[&]/, '&amp;').gsub(/[<]/, '&lt;').gsub(/[']/, '&apos;')
  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_sObject

see to_xml



868
869
870
# File 'lib/jabber4r/protocol.rb', line 868

def to_s
  to_xml
end

#to_xmlObject

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