Class: AdventureMUC

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb

Instance Method Summary collapse

Constructor Details

#initialize(jid, secret, addr, port = 5347) ⇒ AdventureMUC

Returns a new instance of AdventureMUC.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 4

def initialize(jid, secret, addr, port=5347)
  @worlds = {}

  @component = Jabber::Component.new(jid)
  @component.connect(addr, port)
  @component.auth(secret)
  @component.on_exception { |e,|
    puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}"
  }

  @component.add_iq_callback { |iq|
    handle_iq(iq)
  }
  @component.add_presence_callback { |pres|
    handle_presence(pres)
  }
  @component.add_message_callback { |msg|
    handle_message(msg)
  }

  puts "Adventure component up and running"
end

Instance Method Details

#add_world(file) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 27

def add_world(file)
  print "Adding world from #{file}..."
  begin
    world = World.new_from_file(self, file)
  rescue Exception => e
    puts " #{e.to_s}"
    exit
  end
  @worlds[world.node] = world
  puts " #{world.iname}"
end

#handle_disco_info(iq) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 58

def handle_disco_info(iq)
  if iq.type != :get
    answer = iq.answer
    answer.type = :error
    answer.add(Jabber::ErrorResponse.new('bad-request'))
    @component.send(answer) if iq.type != :error
    return
  end
  answer = iq.answer
  answer.type = :result
  if iq.to.node == nil
    answer.query.add(Jabber::Discovery::Identity.new('conference', 'Adventure component', 'text'))
    answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace))
    answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace))
  else
    world = @worlds[iq.to.node]
    if world.nil?
      answer.type = :error
      answer.query.add(Jabber::ErrorResponse.new('item-not-found', 'The world you are trying to reach is currently unavailable.'))
    else
      answer.query.add(Jabber::Discovery::Identity.new('conference', world.iname, 'text'))
      answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace))
      answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace))
      answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUC.new.namespace))
      answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUCUser.new.namespace))
    end
  end
  @component.send(answer)
end

#handle_disco_items(iq) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 88

def handle_disco_items(iq)
  if iq.type != :get
    answer = iq.answer
    answer.add(Jabber::ErrorResponse.new('bad-request'))
    @component.send(answer)
    return
  end
  answer = iq.answer
  answer.type = :result
  if iq.to.node == nil
    @worlds.each { |node,world|
      answer.query.add(Jabber::Discovery::Item.new(Jabber::JID.new(node, @component.jid.domain), world.iname))
    }
  end
  @component.send(answer)
end

#handle_iq(iq) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 44

def handle_iq(iq)
  puts "iq: from #{iq.from} type #{iq.type} to #{iq.to}: #{iq.queryns}"

  if iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoInfo)
    handle_disco_info(iq)
    true
  elsif iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoItems)
    handle_disco_items(iq)
    true
  else
    false
  end
end

#handle_message(msg) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 121

def handle_message(msg)
  puts "message: from #{msg.from} type #{msg.type} to #{msg.to}: #{msg.body.inspect}"

  world = @worlds[msg.to.node]
  if world.nil?
    answer = msg.answer
    answer.type = :error
    answer.add(Jabber::ErrorResponse.new('item-not-found', 'The world you are trying to reach is currently unavailable.'))
    @component.send(answer)
  else
    world.handle_message(msg)
  end

  true
end

#handle_presence(pres) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 105

def handle_presence(pres)
  puts "presence: from #{pres.from} type #{pres.type} to #{pres.to}"

  world = @worlds[pres.to.node]
  if world.nil?
    answer = pres.answer
    answer.type = :error
    answer.add(Jabber::ErrorResponse.new('item-not-found', 'The world you are trying to reach is currently unavailable.'))
    @component.send(answer)
  else
    world.handle_presence(pres)
  end

  true
end

#send(worldnode, worldresource, stanza) ⇒ Object



39
40
41
42
# File 'lib/vendor/xmpp4r/data/doc/xmpp4r/examples/advanced/adventure/adventuremuc.rb', line 39

def send(worldnode, worldresource, stanza)
  stanza.from = Jabber::JID.new(worldnode, @component.jid.domain, worldresource)
  @component.send(stanza)
end