diff options
author | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-02-13 15:18:39 +0000 |
---|---|---|
committer | louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13> | 2010-02-13 15:18:39 +0000 |
commit | d72780d58db86f0be2e4cfc26cd2a40ef47d5bca (patch) | |
tree | 191cb380b50de2c2846b3e9f9710695442b4c0c9 /src | |
parent | 949ef991d9e8e44c41802f202381c2223876f186 (diff) | |
download | poezio-d72780d58db86f0be2e4cfc26cd2a40ef47d5bca.tar.gz poezio-d72780d58db86f0be2e4cfc26cd2a40ef47d5bca.tar.bz2 poezio-d72780d58db86f0be2e4cfc26cd2a40ef47d5bca.tar.xz poezio-d72780d58db86f0be2e4cfc26cd2a40ef47d5bca.zip |
debut de vcard : les avatars
Diffstat (limited to 'src')
-rw-r--r-- | src/client.py | 4 | ||||
-rw-r--r-- | src/common.py | 35 | ||||
-rw-r--r-- | src/connection.py | 2 | ||||
-rw-r--r-- | src/multiuserchat.py | 67 |
4 files changed, 105 insertions, 3 deletions
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 <louizatakk@fedoraproject.org> + +# 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 <http://www.gnu.org/licenses/>. + +# 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 <dkirov AT gmail.com> + ## Junglecow J <junglecow AT gmail.com> + ## Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org> + ## Travis Shirk <travis AT pobox.com> + ## Nikos Kouremenos <kourem AT gmail.com> + ## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org> + ## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com> + ## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com> + ## Jean-Marie Traissard <jim AT lapin.org> + ## Stephan Erb <steve-e AT h3c.de> + ## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org> + (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) |