Class: XMPPBot::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/xmppbot/message.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stanza = nil) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
18
# File 'lib/xmppbot/message.rb', line 10

def initialize(stanza=nil)
  if stanza
    @stanza=stanza
  else
    @stanza=StropheRuby::Stanza.new
    @stanza.name="message"
    @stanza.type="chat"
  end
end

Instance Attribute Details

#stanzaObject (readonly)

Returns the value of attribute stanza.



9
10
11
# File 'lib/xmppbot/message.rb', line 9

def stanza
  @stanza
end

Instance Method Details

#bodyObject



36
37
38
# File 'lib/xmppbot/message.rb', line 36

def body
  self.stanza.child_by_name("body").text rescue nil
end

#body=(str) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/xmppbot/message.rb', line 40

def body=(str)
  children = self.stanza.children
  
  #Strangely enough, sending a "<" character to our stream will terminate it.
  #I guess expat (the xml parser) should take care of encoding the
  #special characters to ensure that the xml remains valid... but it doesn't do it.
  str.gsub!(/[<>]/) {|s| s == "<" ? '&lt;' : '&gt;'}
  if children      
    children.children.text = str
  else  
    body_stanza = StropheRuby::Stanza.new
    body_stanza.name="body"
  
    text_stanza = StropheRuby::Stanza.new
    text_stanza.text=str
  
    body_stanza.add_child(text_stanza)    
    self.stanza.add_child(body_stanza)
  end
end

#fromObject



20
21
22
# File 'lib/xmppbot/message.rb', line 20

def from
  self.stanza.attribute("from") rescue nil
end

#from=(value) ⇒ Object



24
25
26
# File 'lib/xmppbot/message.rb', line 24

def from=(value)
  self.stanza.set_attribute("from",value.to_s)
end

#to=(to) ⇒ Object



61
62
63
# File 'lib/xmppbot/message.rb', line 61

def to=(to)
  self.stanza.set_attribute("to",to)
end

#typeObject



28
29
30
# File 'lib/xmppbot/message.rb', line 28

def type
  self.stanza.type
end

#type=(type) ⇒ Object



32
33
34
# File 'lib/xmppbot/message.rb', line 32

def type=(type)
  self.stanza.type=type
end