Class: Jabber::Message

Inherits:
XMLStanza show all
Defined in:
lib/xmpp4r/message.rb

Overview

The Message class manages the <message/> stanzas, which is used for all messaging communication.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMLStanza

answer, #answer, #error, #from, #from=, #id, #id=, #normalize, #set_from, #set_id, #set_to, #to, #to=

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, #replace_element_text

Constructor Details

#initialize(to = nil, body = nil) ⇒ Message

Create a new message

>to

a JID or a String object to send the message to.

>body

the message's body


18
19
20
21
22
23
24
25
26
# File 'lib/xmpp4r/message.rb', line 18

def initialize(to = nil, body = nil)
  super("message")
  if not to.nil?
    set_to(to)
  end
  if !body.nil?
    add_element(REXML::Element::new("body").add_text(body))
  end
end

Class Method Details

.import(xmlstanza) ⇒ Object

Create a new message from a stanza, by copying all attributes and children from it.

xmlstanza
REXML::Element

Source

return
Message

Result


109
110
111
# File 'lib/xmpp4r/message.rb', line 109

def Message.import(xmlstanza)
  Message::new.import(xmlstanza)
end

Instance Method Details

#bodyObject

Returns the message's body, or nil. This is the message's plain-text content.


100
101
102
# File 'lib/xmpp4r/message.rb', line 100

def body
  first_element_text('body')
end

#body=(b) ⇒ Object

Sets the message's body

b
String

body to set


117
118
119
# File 'lib/xmpp4r/message.rb', line 117

def body=(b)
  replace_element_text('body', b)
end

#set_body(b) ⇒ Object

Sets the message's body

b
String

body to set

return
REXML::Element

self for chaining


126
127
128
129
# File 'lib/xmpp4r/message.rb', line 126

def set_body(b)
  self.body = b
  self
end

#set_subject(s) ⇒ Object

sets the message's subject

s
String

subject to set

return
REXML::Element

self for chaining


144
145
146
147
# File 'lib/xmpp4r/message.rb', line 144

def set_subject(s)
  self.subject = s
  self
end

#set_thread(s) ⇒ Object

gets the message's thread (chaining-friendly) Please note that this are not [Thread] but a [String]-Identifier to track conversations

s
String

thread to set


167
168
169
170
# File 'lib/xmpp4r/message.rb', line 167

def set_thread(s)
  self.thread = s
  self
end

#set_type(v) ⇒ Object

Set the type of the Message stanza (chaining-friendly)

v
Symbol

or nil


79
80
81
82
# File 'lib/xmpp4r/message.rb', line 79

def set_type(v)
  self.type = v
  self
end

#subjectObject

Returns the message's subject, or nil


151
152
153
# File 'lib/xmpp4r/message.rb', line 151

def subject
  first_element_text('subject')
end

#subject=(s) ⇒ Object

sets the message's subject

s
String

subject to set


135
136
137
# File 'lib/xmpp4r/message.rb', line 135

def subject=(s)
  replace_element_text('subject', s)
end

#threadObject

Returns the message's thread, or nil


174
175
176
# File 'lib/xmpp4r/message.rb', line 174

def thread
  first_element_text('thread')
end

#thread=(s) ⇒ Object

sets the message's thread

s
String

thread to set


158
159
160
161
# File 'lib/xmpp4r/message.rb', line 158

def thread=(s)
  delete_elements('thread')
  replace_element_text('thread', s) unless s.nil?
end

#typeObject

Get the type of the Message stanza

The following Symbols are allowed:

  • :chat

  • :error

  • :groupchat

  • :headline

  • :normal

result
Symbol

or nil


51
52
53
54
55
56
57
58
59
60
# File 'lib/xmpp4r/message.rb', line 51

def type
  case super
    when 'chat' then :chat
    when 'error' then :error
    when 'groupchat' then :groupchat
    when 'headline' then :headline
    when 'normal' then :normal
    else nil
  end
end

#type=(v) ⇒ Object

Set the type of the Message stanza (see Message#type for details)

v
Symbol

or nil


65
66
67
68
69
70
71
72
73
74
# File 'lib/xmpp4r/message.rb', line 65

def type=(v)
  case v
    when :chat then super('chat')
    when :error then super('error')
    when :groupchat then super('groupchat')
    when :headline then super('headline')
    when :normal then super('normal')
    else super(nil)
  end
end

#typed_add(element) ⇒ Object

Add a sub-element

Will be converted to [X] if named “x”

element
REXML::Element

to add


33
34
35
36
37
38
39
# File 'lib/xmpp4r/message.rb', line 33

def typed_add(element)
  if element.kind_of?(REXML::Element) && (element.name == 'x')
    super(X::import(element))
  else
    super(element)
  end
end

#x(namespace = nil) ⇒ Object

Get the first <x/> element in this stanza, or nil if none found.

namespace
String

Optional, find the first <x/> element having this xmlns

result
REXML::Element

or nil


88
89
90
91
92
93
94
95
# File 'lib/xmpp4r/message.rb', line 88

def x(namespace=nil)
  each_element('x') { |x|
    if namespace.nil? or namespace == x.namespace
      return x
    end
  }
  nil
end