| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|