From d72780d58db86f0be2e4cfc26cd2a40ef47d5bca Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Sat, 13 Feb 2010 15:18:39 +0000 Subject: debut de vcard : les avatars --- src/client.py | 4 ++-- src/common.py | 35 +++++++++++++++++++++++++++ src/connection.py | 2 +- src/multiuserchat.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 src/common.py (limited to 'src') diff --git a/src/client.py b/src/client.py index 9840d259..d3561a86 100644 --- a/src/client.py +++ b/src/client.py @@ -20,8 +20,8 @@ import sys # disable any printout (this would mess the display) stderr = sys.stderr -sys.stdout = open('/dev/null', 'w') -sys.stderr = open('/dev/null', 'w') +# sys.stdout = open('/dev/null', 'w') +# sys.stderr = open('/dev/null', 'w') from connection import Connection from multiuserchat import MultiUserChat diff --git a/src/common.py b/src/common.py new file mode 100644 index 00000000..015f1cec --- /dev/null +++ b/src/common.py @@ -0,0 +1,35 @@ +# -*- coding: utf-8 -*- + +# Copyright 2010, Florent Le Coz + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation version 3 of the License. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# various useful functions + +import base64 +import os +import mimetypes +import hashlib + +def get_base64_from_file(path): + if not os.path.isfile(path): + return (None, None, "File does not exist") + size = os.path.getsize(path) + if size > 16384: + return (None, None,"File is too big") + fd = open(path, 'rb') + data = fd.read() + encoded = base64.encodestring(data) + sha1 = hashlib.sha1(data).hexdigest() + mime_type = mimetypes.guess_type(path)[0] + return (encoded, mime_type, sha1) diff --git a/src/connection.py b/src/connection.py index 8a042502..2dec8f35 100644 --- a/src/connection.py +++ b/src/connection.py @@ -52,7 +52,7 @@ class Connection(threading.Thread): if not self.authenticate(): logger.error('Could not authenticate to server') sys.exit(-1) - self.client.sendInitPresence() + self.client.sendInitPresence(requestRoster=0) self.online = 1 # 2 when confirmation of auth is received self.register_handlers() while 1: diff --git a/src/multiuserchat.py b/src/multiuserchat.py index 4f132fd4..8c948513 100644 --- a/src/multiuserchat.py +++ b/src/multiuserchat.py @@ -19,6 +19,9 @@ from xmpp import NS_MUC_ADMIN, NS_MUC from xmpp.protocol import Presence, Iq, Message, JID +import xmpp +import common +import threading from handler import Handler from config import config @@ -34,9 +37,72 @@ def is_jid(jid): if JID(jid).getNode() != '': return True +class VcardSender(threading.Thread): + """ + avatar sending is really slow (don't know why...) + use a thread to send it... + """ + def __init__(self, connection): + threading.Thread.__init__(self) + self.connection = connection + self.handler = Handler() + + def run(self): + self.send_vcard() + + def send_vcard(self): + """ + Method stolen from Gajim (thanks) + ## Copyright (C) 2006 Dimitur Kirov + ## Junglecow J + ## Copyright (C) 2006-2007 Tomasz Melcer + ## Travis Shirk + ## Nikos Kouremenos + ## Copyright (C) 2006-2008 Yann Leboulanger + ## Copyright (C) 2007 Julien Pivotto + ## Copyright (C) 2007-2008 Brendan Taylor + ## Jean-Marie Traissard + ## Stephan Erb + ## Copyright (C) 2008 Jonathan Schleifer + (one of these people coded this method, probably) + """ + if not self.connection: + return + vcard = { + "FN":"Poezio tester", + } + photo_file_path = config.get('photo', '../data/poezio_80.png') + (image, mime_type, sha1) = common.get_base64_from_file(photo_file_path) + if image: + vcard['PHOTO'] = {"TYPE":mime_type,"BINVAL":image} + iq = xmpp.Iq(typ = 'set') + iq2 = iq.setTag(xmpp.NS_VCARD + ' vCard') + for i in vcard: + if i == 'jid': + continue + if isinstance(vcard[i], dict): + iq3 = iq2.addChild(i) + for j in vcard[i]: + iq3.addChild(j).setData(vcard[i][j]) + elif isinstance(vcard[i], list): + for j in vcard[i]: + iq3 = iq2.addChild(i) + for k in j: + iq3.addChild(k).setData(j[k]) + else: + iq2.addChild(i).setData(vcard[i]) + # id_ = self.connect.getAnId() + # iq.setID(id_) + self.connection.send(iq) + iq = xmpp.Iq(typ = 'set') + iq2 = iq.setTag(xmpp.NS_VCARD_UPDATE) + iq2.addChild('PHOTO').setData(sha1) + self.connection.send(iq) + class MultiUserChat(object): def __init__(self, connection): self.connection = connection + self.vcard_sender = VcardSender(self.connection) self.rooms = [] self.rn = {} @@ -64,6 +130,7 @@ class MultiUserChat(object): else: nick = config.get('default_nick', 'poezio') self.handler.emit('join-room', room=roomname, nick=nick) + self.vcard_sender.start() def send_message(self, room, message): mes = Message(to=room) -- cgit v1.2.3