debut de vcard : les avatars

This commit is contained in:
louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 2010-02-13 15:18:39 +00:00
parent 949ef991d9
commit d72780d58d
6 changed files with 107 additions and 3 deletions

2
README
View file

@ -70,6 +70,8 @@ the Creative Commons BY license (http://creativecommons.org/licenses/by/2.0/)
= People =
Erwan Briand - Handler and MultiUserChat classes
Gaëtan Ribémont (http://www.bonbref.com) - Logo design
= Project =
Gajim - send_vcard method
======================
The code

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.3 KiB

After

Width:  |  Height:  |  Size: 6.6 KiB

View file

@ -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

35
src/common.py Normal file
View file

@ -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)

View file

@ -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:

View file

@ -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)