Class: Jabber::Message

Inherits:
XMPPStanza show all
Includes:
XParent
Defined in:
lib/xmpp4r/message.rb

Overview

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

Constant Summary collapse

CHAT_STATES =
%w(active composing gone inactive paused).freeze

Instance Method Summary collapse

Methods included from XParent

#x

Methods inherited from XMPPStanza

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

Methods inherited from XMPPElement

class_for_name_xmlns, #clone, force_xmlns, force_xmlns?, import, name_xmlns, name_xmlns_for_class, #parent=, #set_xml_lang, #typed_add, #xml_lang, #xml_lang=

Methods inherited from REXML::Element

#==, #delete_elements, #each_elements, #first_element, #first_element_content, #first_element_text, #import, import, #replace_element_content, #replace_element_text, #typed_add

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



25
26
27
28
29
30
31
32
33
# File 'lib/xmpp4r/message.rb', line 25

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

Instance Method Details

#bodyObject

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



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

def body
  first_element_text('body')
end

#body=(b) ⇒ Object

Sets the message’s body

b
String

body to set



89
90
91
# File 'lib/xmpp4r/message.rb', line 89

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

#chat_stateObject

Returns the current chat state, or nil if no chat state is set



198
199
200
201
# File 'lib/xmpp4r/message.rb', line 198

def chat_state
  each_elements(*CHAT_STATES) { |el| return el.name.to_sym }
  return nil
end

#chat_state=(s) ⇒ Object

Sets the chat state :active, :composing, :gone, :inactive, :paused

Raises:



205
206
207
208
209
210
# File 'lib/xmpp4r/message.rb', line 205

def chat_state=(s)
  s = s.to_s
  raise InvalidChatState, "Chat state must be one of #{CHAT_STATES.join(', ')}" unless CHAT_STATES.include?(s)
  CHAT_STATES.each { |state| delete_elements(state) }
  add_element(REXML::Element.new(s).add_namespace('http://jabber.org/protocol/chatstates'))
end

#set_body(b) ⇒ Object

Sets the message’s body

b
String

body to set

return
REXML::Element

self for chaining



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

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

#set_chat_state(s) ⇒ Object

Sets the message’s chat state



214
215
216
217
# File 'lib/xmpp4r/message.rb', line 214

def set_chat_state(s)
  self.state = s
  self
end

#set_subject(s) ⇒ Object

sets the message’s subject

s
String

subject to set

return
REXML::Element

self for chaining



162
163
164
165
# File 'lib/xmpp4r/message.rb', line 162

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



185
186
187
188
# File 'lib/xmpp4r/message.rb', line 185

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



73
74
75
76
# File 'lib/xmpp4r/message.rb', line 73

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

#set_xhtml_body(b) ⇒ Object

Sets the message’s xhtml body

b
String

xhtml body to set (Note: must be a valid xhtml)

return
REXML::Element

self for chaining



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

def set_xhtml_body(b)
  self.xhtml_body = b
  self
end

#subjectObject

Returns the message’s subject, or nil



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

def subject
  first_element_text('subject')
end

#subject=(s) ⇒ Object

sets the message’s subject

s
String

subject to set



153
154
155
# File 'lib/xmpp4r/message.rb', line 153

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

#threadObject

Returns the message’s thread, or nil



192
193
194
# File 'lib/xmpp4r/message.rb', line 192

def thread
  first_element_text('thread')
end

#thread=(s) ⇒ Object

sets the message’s thread

s
String

thread to set



176
177
178
179
# File 'lib/xmpp4r/message.rb', line 176

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



45
46
47
48
49
50
51
52
53
54
# File 'lib/xmpp4r/message.rb', line 45

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



59
60
61
62
63
64
65
66
67
68
# File 'lib/xmpp4r/message.rb', line 59

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

#xhtml_bodyObject

Returns the message’s xhtml body, or nil. This is the message’s xhtml-text content.



106
107
108
109
110
111
112
113
114
# File 'lib/xmpp4r/message.rb', line 106

def xhtml_body
  html = first_element('html', 'http://jabber.org/protocol/xhtml-im')

  if html
    html.first_element_content('body', 'http://www.w3.org/1999/xhtml')
  else
    first_element_content('body', 'http://www.w3.org/1999/xhtml')
  end
end

#xhtml_body=(b) ⇒ Object

Sets the message’s xhtml body

b
String

xhtml body to set (Note: must be a valid xhtml)



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/xmpp4r/message.rb', line 120

def xhtml_body=(b)
  begin
    b = REXML::Document.new("<root>#{b}</root>")
  rescue REXML::ParseException
    raise ArgumentError, "Body is not a valid xhtml. Have you forgot to close some tag?"
  end
  
  html = first_element('html', 'http://jabber.org/protocol/xhtml-im')
  
  if html
    html.replace_element_content('body', b, 'http://www.w3.org/1999/xhtml')
  else
    el = REXML::Element.new('html')
    el.add_namespace('http://jabber.org/protocol/xhtml-im')
    el.replace_element_content('body', b, 'http://www.w3.org/1999/xhtml')
    add_element(el)
  end
end