Changeset 101
- Timestamp:
- 10/14/07 06:23:09 (1 year ago)
- Files:
-
- adventure/adventure.py (deleted)
- adventure/adventure.rb (deleted)
- adventure/adventuremuc.py (modified) (2 diffs)
- adventure/cave.xml (added)
- adventure/cube.xml (deleted)
- adventure/place.py (added)
- adventure/player.py (added)
- adventure/thing.py (added)
- adventure/tower.xml (deleted)
- adventure/world.dtd (added)
- adventure/world.py (modified) (8 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
adventure/adventuremuc.py
r100 r101 1 import world 1 #!/usr/bin/python 2 # -*- coding: utf-8 -*- 2 3 3 class AdventureMUC: 4 def initialize(jid, secret, addr, port=5347): 4 from xmpp import Component 5 from sys import argv 6 from world import World 7 8 class AdventureMUC(Component): 9 def __init__(self, jid, password, server, port=5347): 5 10 self.worlds = {} 6 11 7 self.component = Jabber::Component::new(jid) 8 self.component.connect(addr, port) 9 self.component.auth(secret) 10 self.component.on_exception { |e,| 11 puts "#{e.class}: #{e}\n#{e.backtrace.join("\n")}" 12 } 12 Component.__init__(server, port) 13 if not self.connect(server, port): 14 raise Exception, 'Unable to connect to %s:%s' % (server, port)) 15 if not self.auth(jid, password): 16 raise Exception, 'Unable to autenticate as %s' % jid 13 17 14 self.component.add_iq_callback { |iq| 15 handle_iq(iq) 16 } 17 self.component.add_presence_callback { |pres| 18 handle_presence(pres) 19 } 20 self.component.add_message_callback { |msg| 21 handle_message(msg) 22 } 23 18 self.RegisterHandler('iq', self.handle_iq) 19 self.RegisterHandler('presence', self.handle_presence) 20 self.RegisterHandler('message', self.handle_message) 24 21 print 'Adventure component up and running' 25 22 26 23 27 def add_world(file): 28 print 'Adding world from #{file}...' 29 try: 30 world = World.new_from_file(self, file) 31 except Exception, e: 32 print e 33 exit 24 def add_world(self, filename): 25 print 'Adding world from "%s"...' % filename 26 world = World.new_from_file(self, filename) 34 27 self.worlds[world.node] = world 35 28 print ' ' + world.iname 36 29 37 def send( worldnode, worldresource, stanza):30 def send(self, worldnode, worldresource, stanza): 38 31 stanza.from = Jabber::JID::new(worldnode, self.component.jid.domain, worldresource) 39 32 self.component.send(stanza) 40 33 41 def handle_iq( iq):42 print "iq: from #{iq.from} type #{iq.type} to #{iq.to}: #{iq.queryns}"34 def handle_iq(self, iq): 35 #print "iq: from #{iq.from} type #{iq.type} to #{iq.to}: #{iq.queryns}" 43 36 44 if iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoInfo): 45 handle_disco_info(iq) 46 return True 47 elif iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoItems): 48 handle_disco_items(iq) 49 return True 50 else: 51 return False 37 #if iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoInfo): 38 # handle_disco_info(iq) 39 # return True 40 #elif iq.query.kind_of?(Jabber::Discovery::IqQueryDiscoItems): 41 # handle_disco_items(iq) 42 # return True 43 #else: 44 # return False 45 pass 52 46 53 def handle_disco_info(iq):54 if iq.type != :get :55 answer = iq.answer56 answer.type = :error57 answer.add(Jabber::Error.new('bad-request'))58 self.component.send(answer) if iq.type != :error59 return60 answer = iq.answer61 answer.type = :result62 if iq.to.node == nil:63 answer.query.add(Jabber::Discovery::Identity.new('conference', 'Adventure component', 'text'))64 answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace))65 answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace))66 else:67 world = self.worlds[iq.to.node]68 if world.nil? :69 answer.type = :error70 answer.query.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.'))71 else:72 answer.query.add(Jabber::Discovery::Identity.new('conference', world.iname, 'text'))73 answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace))74 answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace))75 answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUC.new.namespace))76 answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUCUser.new.namespace))77 self.component.send(answer)47 #def handle_disco_info(iq): 48 # if iq.type != :get : 49 # answer = iq.answer 50 # answer.type = :error 51 # answer.add(Jabber::Error.new('bad-request')) 52 # self.component.send(answer) if iq.type != :error 53 # return 54 # answer = iq.answer 55 # answer.type = :result 56 # if iq.to.node == nil: 57 # answer.query.add(Jabber::Discovery::Identity.new('conference', 'Adventure component', 'text')) 58 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace)) 59 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace)) 60 # else: 61 # world = self.worlds[iq.to.node] 62 # if world.nil? : 63 # answer.type = :error 64 # answer.query.add(Jabber::Error.new('item-not-found', 'The world you are trying to reach is currently unavailable.')) 65 # else: 66 # answer.query.add(Jabber::Discovery::Identity.new('conference', world.iname, 'text')) 67 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoInfo.new.namespace)) 68 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::Discovery::IqQueryDiscoItems.new.namespace)) 69 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUC.new.namespace)) 70 # answer.query.add(Jabber::Discovery::Feature.new(Jabber::MUC::XMUCUser.new.namespace)) 71 # self.component.send(answer) 78 72 79 def handle_disco_items(iq):80 if iq.type != :get :81 answer = iq.answer82 answer.add(Jabber::Error.new('bad-request'))83 self.component.send(answer)84 return85 answer = iq.answer86 answer.type = :result87 if iq.to.node == nil:88 self.worlds.each { |node,world|89 answer.query.add(Jabber::Discovery::Item.new(Jabber::JID::new(String::new(node), self.component.jid.domain), world.iname))90 }91 self.component.send(answer)73 #def handle_disco_items(iq): 74 # if iq.type != :get : 75 # answer = iq.answer 76 # answer.add(Jabber::Error.new('bad-request')) 77 # self.component.send(answer) 78 # return 79 # answer = iq.answer 80 # answer.type = :result 81 # if iq.to.node == nil: 82 # self.worlds.each { |node,world| 83 # answer.query.add(Jabber::Discovery::Item.new(Jabber::JID::new(String::new(node), self.component.jid.domain), world.iname)) 84 # } 85 # self.component.send(answer) 92 86 93 def handle_presence( pres):94 print "presence: from #{pres.from} type #{pres.type} to #{pres.to}"87 def handle_presence(self, pres): 88 #print "presence: from #{pres.from} type #{pres.type} to #{pres.to}" 95 89 96 90 world = self.worlds[pres.to.node] 97 if world.nil?:91 if not world: 98 92 answer = pres.answer 99 93 answer.type = :error … … 117 111 world.handle_message(msg) 118 112 return True 113 114 115 116 if __name__ == '__main__': 117 if len argv != 4: 118 print 'Syntax: ./adventure.rb <JID> <Password> <Host> <Port>' 119 print 'See README for further help' 120 muc = AdventureMUC(*argv) 121 muc.add_world('cave.xml') adventure/world.py
r100 r101 1 require 'rexml/document' 2 3 class World(REXML::Element): 4 def initialize(muc): 5 super('world') 6 1 # -*- coding: utf-8 -*- 2 3 from place import Place 4 from xml.dom.minidom import parse as xml_parse 5 6 7 class World: 8 def __init__(self, muc): 7 9 self.muc = muc 10 self.places = dict() # {'place_name': place, ...} 11 #self.things = 12 13 doc = xml_parse(filename).documentElement 14 get_elements = doc.documentElement.getElementsByTagName 15 16 places = get_elements('places') 17 for place in places: 18 name = place.getAttribute('name') 19 place_obj = Place(name) 20 gos = place.getElementsByTagName('go') 21 for go in gos: 22 place_obj.exits[ 23 go.getAttribute('spec')] = go.getAttribute('place') 24 #things = get_elements('thing') 25 #for thing in things: 26 27 8 28 9 29 def send(resource, stanza): … … 12 32 self.muc.send(node, resource, stanza) 13 33 14 def World.new_from_file(muc, filename):15 file = File.new(filename)16 world = World.new(muc)17 world.import(REXML::Document.new(file).root)18 file.close19 world20 21 def add(xmlelement):22 if xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'place'):23 super(Place::new.import(xmlelement))24 elif xmlelement.kind_of?(REXML::Element) && (xmlelement.name == 'thing') && !xmlelement.kind_of?(Player):25 super(Thing::new(self).import(xmlelement))26 else:27 super(xmlelement)28 29 def node():30 return attributes['node']31 32 def iname():33 return attributes['name']34 35 34 def place(placename): 36 pl = None 37 for place in each_element('place'): 38 if place.iname == placename: 39 pl = place 40 return pl 41 42 def each_thing_by_place(place, &block): 43 for thing in each_element('thing'): 44 if thing.place == place: 35 if self.places.has_key(placename): 36 return self.places[placename] 37 return None 38 39 def each_thing_by_place(placename): 40 for thing in self.things.values(): 41 if thing.place == placename: 45 42 yield(thing) 46 43 … … 94 91 def handle_presence(pres): 95 92 # A help for the irritated first: 96 if pres.type == :subscribe:93 if pres.type == 'subscribe': 97 94 msg = Jabber::Message.new(pres.from) 98 msg.type = :normal99 msg.subject = "Adventure component help"100 msg.body = "You don't need to subscribe to my presence. Simply use your Jabber client to join the MUC or conference at #{pres.to.strip}"95 msg.type = 'normal' 96 msg.subject = 'Adventure component help' 97 msg.body = 'You don\'t need to subscribe to my presence. Simply use your Jabber client to join the MUC or conference at #{pres.to.strip}' 101 98 send(None, msg) 102 99 return True … … 104 101 # Look if player is already known 105 102 player = None 106 for thing in each_element('thing'):103 for thing in self.things.values(): 107 104 if thing.kind_of?(Player) && pres.to.resource == thing.iname: 108 105 player = thing … … 150 147 def handle_message(msg): 151 148 player = None 152 for thing in each_element('thing'):153 if thing.kind_of?(Player) && msg.to.resource == nil&& msg.from == thing.jid:149 for thing in self.things.values(): 150 if thing.kind_of?(Player) && msg.to.resource == None && msg.from == thing.jid: 154 151 player = thing 155 152 … … 167 164 def command(player, text): 168 165 if text == '?': 169 player.send_message( nil, "(Command) who")166 player.send_message(None, '(Command) who') 170 167 for go in place(player.place).each_element('go'): 171 player.send_message(None, "(Command) go #{go.attributes['spec']}")168 player.send_message(None, '(Command) go #{go.attributes['spec']}') 172 169 for thing in foreach_thing_by_place(player.place): 173 170 for c in thing.each_element('on-command'): 174 player.send_message(None, "(Command) #{c.attributes['command']} #{thing.command_name}")171 player.send_message(None, '(Command) #{c.attributes['command']} #{thing.command_name}') 175 172 return True 176 173 else: 177 174 words = text.split(/ /) 178 175 cmd = words.shift 179 what = words.shift or ""176 what = words.shift or '' 180 177 if cmd == 'go': 181 178 oldplace = place(player.place) 182 newplace = nil179 newplace = None 183 180 184 181 for go in oldplace.each_element('go'): … … 207 204 if handled: 208 205 return True 209 False 210 211 class Place(REXML::Element): 212 def initialize: 213 super('place') 214 215 def iname(): 216 return attributes['name'] 206 return False 217 207 218 208 class Thing(REXML::Element): … … 227 217 super(xmlelement) 228 218 229 def iname( ):219 def iname(self): 230 220 return attributes['name'] 231 221 232 def command_name(): 233 return attributes['command-name'].nil? ? iname : attributes['command-name'] 222 def command_name(self): 223 if attributes['command-name']: 224 return attributes['command-name'] 225 return self.iname() 234 226 235 227 def place():
