Skip to content

Commit d72780d

Browse files
author
louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13
committed
debut de vcard : les avatars
1 parent 949ef99 commit d72780d

File tree

6 files changed

+107
-3
lines changed

6 files changed

+107
-3
lines changed

README

+2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ the Creative Commons BY license (http://creativecommons.org/licenses/by/2.0/)
7070
= People =
7171
Erwan Briand - Handler and MultiUserChat classes
7272
Gaëtan Ribémont (http://www.bonbref.com) - Logo design
73+
= Project =
74+
Gajim - send_vcard method
7375

7476
======================
7577
The code

data/poezio_80.png

-1.68 KB
Loading

src/client.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import sys
2121
# disable any printout (this would mess the display)
2222
stderr = sys.stderr
23-
sys.stdout = open('/dev/null', 'w')
24-
sys.stderr = open('/dev/null', 'w')
23+
# sys.stdout = open('/dev/null', 'w')
24+
# sys.stderr = open('/dev/null', 'w')
2525

2626
from connection import Connection
2727
from multiuserchat import MultiUserChat

src/common.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright 2010, Florent Le Coz <louizatakk@fedoraproject.org>
4+
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation version 3 of the License.
8+
9+
# This program is distributed in the hope that it will be useful,
10+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
# GNU General Public License for more details.
13+
14+
# You should have received a copy of the GNU General Public License
15+
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
17+
# various useful functions
18+
19+
import base64
20+
import os
21+
import mimetypes
22+
import hashlib
23+
24+
def get_base64_from_file(path):
25+
if not os.path.isfile(path):
26+
return (None, None, "File does not exist")
27+
size = os.path.getsize(path)
28+
if size > 16384:
29+
return (None, None,"File is too big")
30+
fd = open(path, 'rb')
31+
data = fd.read()
32+
encoded = base64.encodestring(data)
33+
sha1 = hashlib.sha1(data).hexdigest()
34+
mime_type = mimetypes.guess_type(path)[0]
35+
return (encoded, mime_type, sha1)

src/connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def run(self):
5252
if not self.authenticate():
5353
logger.error('Could not authenticate to server')
5454
sys.exit(-1)
55-
self.client.sendInitPresence()
55+
self.client.sendInitPresence(requestRoster=0)
5656
self.online = 1 # 2 when confirmation of auth is received
5757
self.register_handlers()
5858
while 1:

src/multiuserchat.py

+67
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
from xmpp import NS_MUC_ADMIN, NS_MUC
2121
from xmpp.protocol import Presence, Iq, Message, JID
22+
import xmpp
23+
import common
24+
import threading
2225

2326
from handler import Handler
2427
from config import config
@@ -34,9 +37,72 @@ def is_jid(jid):
3437
if JID(jid).getNode() != '':
3538
return True
3639

40+
class VcardSender(threading.Thread):
41+
"""
42+
avatar sending is really slow (don't know why...)
43+
use a thread to send it...
44+
"""
45+
def __init__(self, connection):
46+
threading.Thread.__init__(self)
47+
self.connection = connection
48+
self.handler = Handler()
49+
50+
def run(self):
51+
self.send_vcard()
52+
53+
def send_vcard(self):
54+
"""
55+
Method stolen from Gajim (thanks)
56+
## Copyright (C) 2006 Dimitur Kirov <dkirov AT gmail.com>
57+
## Junglecow J <junglecow AT gmail.com>
58+
## Copyright (C) 2006-2007 Tomasz Melcer <liori AT exroot.org>
59+
## Travis Shirk <travis AT pobox.com>
60+
## Nikos Kouremenos <kourem AT gmail.com>
61+
## Copyright (C) 2006-2008 Yann Leboulanger <asterix AT lagaule.org>
62+
## Copyright (C) 2007 Julien Pivotto <roidelapluie AT gmail.com>
63+
## Copyright (C) 2007-2008 Brendan Taylor <whateley AT gmail.com>
64+
## Jean-Marie Traissard <jim AT lapin.org>
65+
## Stephan Erb <steve-e AT h3c.de>
66+
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
67+
(one of these people coded this method, probably)
68+
"""
69+
if not self.connection:
70+
return
71+
vcard = {
72+
"FN":"Poezio tester",
73+
}
74+
photo_file_path = config.get('photo', '../data/poezio_80.png')
75+
(image, mime_type, sha1) = common.get_base64_from_file(photo_file_path)
76+
if image:
77+
vcard['PHOTO'] = {"TYPE":mime_type,"BINVAL":image}
78+
iq = xmpp.Iq(typ = 'set')
79+
iq2 = iq.setTag(xmpp.NS_VCARD + ' vCard')
80+
for i in vcard:
81+
if i == 'jid':
82+
continue
83+
if isinstance(vcard[i], dict):
84+
iq3 = iq2.addChild(i)
85+
for j in vcard[i]:
86+
iq3.addChild(j).setData(vcard[i][j])
87+
elif isinstance(vcard[i], list):
88+
for j in vcard[i]:
89+
iq3 = iq2.addChild(i)
90+
for k in j:
91+
iq3.addChild(k).setData(j[k])
92+
else:
93+
iq2.addChild(i).setData(vcard[i])
94+
# id_ = self.connect.getAnId()
95+
# iq.setID(id_)
96+
self.connection.send(iq)
97+
iq = xmpp.Iq(typ = 'set')
98+
iq2 = iq.setTag(xmpp.NS_VCARD_UPDATE)
99+
iq2.addChild('PHOTO').setData(sha1)
100+
self.connection.send(iq)
101+
37102
class MultiUserChat(object):
38103
def __init__(self, connection):
39104
self.connection = connection
105+
self.vcard_sender = VcardSender(self.connection)
40106

41107
self.rooms = []
42108
self.rn = {}
@@ -64,6 +130,7 @@ def on_connected(self, jid):
64130
else:
65131
nick = config.get('default_nick', 'poezio')
66132
self.handler.emit('join-room', room=roomname, nick=nick)
133+
self.vcard_sender.start()
67134

68135
def send_message(self, room, message):
69136
mes = Message(to=room)

0 commit comments

Comments
 (0)