root/adventure/movable.py

Revision 156, 4.0 kB (checked in by thib, 6 months ago)

Quelques modifs ( World, Place et Thing héritent d'object )

Line 
1 # -*- coding: utf-8 -*-
2
3 #  MUDMUC
4 #  movable.py
5 #  Copyright (c) 2008 Thibaut Girka, Anaël Verrier
6
7 #  This program is free software; you can redistribute it and/or modify
8 #  it under the terms of the GNU General Public License as published by
9 #  the Free Software Foundation; version 3 only.
10
11 #  This program is distributed in the hope that it will be useful,
12 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #  GNU General Public License for more details.
15
16 #  You should have received a copy of the GNU General Public License
17 #  along with this program; if not, write to the Free Software
18 #  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
20 from os.path import isfile, isabs, join as path_join
21 from base64 import encodestring
22 from mimetypes import guess_type as guess_mimetype
23 from sha import sha
24
25 from xmpp import Message, Presence, NS_MUC_USER, NS_VCARD, Node
26
27 class Movable(object):
28     def __init__(self, world, name, jid):
29         self.world = world
30         self.name = name
31         self.jid = unicode(jid)
32         self.place = None
33         self.presence = None
34         self.image_path = None
35        
36         self.vcard = None
37    
38     def set_vcard(self, image=None, birthday=None):
39         vcard = Node(NS_VCARD + ' vCard')
40         vcard.addChild(node=Node('NICKNAME', {}, self.name))
41         if image:
42             if not isabs(image):
43                 image_path = path_join(self.world.data_dir, image)
44             if isfile(image_path):
45                 self.image_path = image
46                
47                 file = open(image_path, 'rb')
48                 data = file.read()
49                 encoded_data = encodestring(data)
50                 mime = guess_mimetype(image)[0]
51                 file.close()
52                 photo = vcard.setTag('PHOTO')
53                 photo.addChild(node=Node('TYPE', {}, mime))
54                 photo.addChild(node=Node('BINVAL', {}, encoded_data))
55                
56                 vcard_pres = self.presence.getTag('x', namespace=NS_VCARD + ':x:update')
57                 if not vcard_pres:
58                     vcard_pres = self.presence.setTag('x', namespace=NS_VCARD + ':x:update')
59                 vcard_pres.addChild(node=Node('photo', {},
60                                               sha(data).hexdigest()))
61            
62         if birthday:
63             vcard.addChild(node=Node('BDAY', {}, birthday))
64         #TODO: DESC
65         self.vcard = vcard
66    
67     def say(self, text, target=None):
68         """ Make the player say something. If target is None, broadcast """
69         if target is None:
70             self.place.broadcast_message(self.name, text)
71         else:
72             target.send_message(self.name, text)
73    
74     def move_to(self, to_, warning=True):
75         """ Move the player to its new place """
76         oldplace = self.place
77         self.place = to_
78         if self.place:
79             self.place.player_entered(self, oldplace)
80         elif warning:
81             self.send_message(None,
82                               'Sorry! The room isn\'t available!\n'
83                               'It\'s a bug for sure '
84                               '(either in the data '
85                               'either in the engine) :/')
86         if oldplace is not None:
87             oldplace.player_leaved(self)
88                              
89     def fix_presence(self, role=None, affiliation=None):
90         """
91         Fix presence: Remove double presence tag and set
92         the specified role and affiliation
93         """
94         pres = Presence(node=self.presence)
95        
96         tag_x = pres.getTag('x', namespace=NS_MUC_USER)
97         if not tag_x:
98             tag_x = pres.setTag('x', namespace=NS_MUC_USER)
99        
100         tag_item = tag_x.getTag('item', namespace=NS_MUC_USER)
101         if not tag_item:
102             tag_item = tag_x.setTag('item', namespace=NS_MUC_USER)
103        
104         tag_item.setAttr('affiliation', affiliation)
105         tag_item.setAttr('role', role)
106        
107         return pres
108
109
Note: See TracBrowser for help on using the browser.