Changeset 101

Show
Ignore:
Timestamp:
10/14/07 06:23:09 (2 years ago)
Author:
elghinn
Message:

ThibG and me: some stuff

Location:
adventure
Files:
5 added
4 removed
2 modified

Legend:

Unmodified
Added
Removed
  • adventure/adventuremuc.py

    r100 r101  
    1 import world 
     1#!/usr/bin/python 
     2# -*- coding: utf-8 -*- 
    23 
    3 class AdventureMUC: 
    4     def initialize(jid, secret, addr, port=5347): 
     4from xmpp import Component 
     5from sys import argv 
     6from world import World 
     7 
     8class AdventureMUC(Component): 
     9    def __init__(self, jid, password, server, port=5347): 
    510        self.worlds = {} 
    611     
    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 
    1317 
    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) 
    2421        print 'Adventure component up and running' 
    2522 
    2623 
    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) 
    3427        self.worlds[world.node] = world 
    3528        print ' ' + world.iname 
    3629 
    37     def send(worldnode, worldresource, stanza): 
     30    def send(self, worldnode, worldresource, stanza): 
    3831        stanza.from = Jabber::JID::new(worldnode, self.component.jid.domain, worldresource) 
    3932        self.component.send(stanza) 
    4033 
    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}" 
    4336 
    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 
    5246   
    53     def handle_disco_info(iq): 
    54         if iq.type != :get : 
    55             answer = iq.answer 
    56             answer.type = :error 
    57             answer.add(Jabber::Error.new('bad-request')) 
    58             self.component.send(answer) if iq.type != :error 
    59             return 
    60         answer = iq.answer 
    61         answer.type = :result 
    62         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 = :error 
    70                 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) 
    7872   
    79     def handle_disco_items(iq): 
    80         if iq.type != :get : 
    81             answer = iq.answer 
    82             answer.add(Jabber::Error.new('bad-request')) 
    83             self.component.send(answer) 
    84             return 
    85         answer = iq.answer 
    86         answer.type = :result 
    87         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) 
    9286 
    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}" 
    9589     
    9690        world = self.worlds[pres.to.node] 
    97         if world.nil? : 
     91        if not world: 
    9892            answer = pres.answer 
    9993            answer.type = :error 
     
    117111            world.handle_message(msg) 
    118112        return True 
     113 
     114 
     115 
     116if __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 
     3from place import Place 
     4from xml.dom.minidom import parse as xml_parse 
     5 
     6 
     7class World: 
     8    def __init__(self, muc): 
    79        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 
    828 
    929    def send(resource, stanza): 
     
    1232            self.muc.send(node, resource, stanza) 
    1333 
    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.close 
    19         world 
    20  
    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  
    3534    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: 
    4542                yield(thing) 
    4643 
     
    9491    def handle_presence(pres): 
    9592        # A help for the irritated first: 
    96         if pres.type == :subscribe : 
     93        if pres.type == 'subscribe': 
    9794            msg = Jabber::Message.new(pres.from) 
    98             msg.type = :normal 
    99             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}' 
    10198            send(None, msg) 
    10299            return True 
     
    104101        # Look if player is already known 
    105102        player = None 
    106         for thing in each_element('thing'): 
     103        for thing in self.things.values(): 
    107104            if thing.kind_of?(Player) && pres.to.resource == thing.iname: 
    108105                player = thing 
     
    150147    def handle_message(msg): 
    151148        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: 
    154151                player = thing 
    155152 
     
    167164    def command(player, text): 
    168165        if text == '?': 
    169             player.send_message(nil, "(Command) who") 
     166            player.send_message(None, '(Command) who') 
    170167            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']}') 
    172169            for thing in foreach_thing_by_place(player.place): 
    173170                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}') 
    175172            return True 
    176173        else: 
    177174            words = text.split(/ /) 
    178175            cmd = words.shift 
    179             what = words.shift or "" 
     176            what = words.shift or '' 
    180177            if cmd == 'go': 
    181178                oldplace = place(player.place) 
    182                 newplace = nil 
     179                newplace = None 
    183180 
    184181                for go in oldplace.each_element('go'): 
     
    207204                if handled: 
    208205                    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 
    217207 
    218208class Thing(REXML::Element): 
     
    227217            super(xmlelement) 
    228218 
    229     def iname(): 
     219    def iname(self): 
    230220        return attributes['name'] 
    231221 
    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() 
    234226 
    235227    def place():