2015-12-31 19:43:10 +00:00
|
|
|
__author__ = "Steffen Vogel"
|
|
|
|
__copyright__ = "Copyright 2015, Steffen Vogel"
|
|
|
|
__license__ = "GPLv3"
|
|
|
|
__maintainer__ = "Steffen Vogel"
|
|
|
|
__email__ = "post@steffenvogel.de"
|
2013-08-01 23:45:51 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
"""
|
2013-08-01 23:45:51 +00:00
|
|
|
This file is part of transWhat
|
|
|
|
|
|
|
|
transWhat 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, either version 3 of the License, or
|
|
|
|
any later version.
|
|
|
|
|
|
|
|
transwhat 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 transWhat. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
2013-06-28 17:46:25 +00:00
|
|
|
from Spectrum2 import protocol_pb2
|
|
|
|
|
|
|
|
import logging
|
2015-12-14 18:14:20 +00:00
|
|
|
import time
|
2016-01-01 09:42:11 +00:00
|
|
|
import utils
|
2016-01-02 15:08:30 +00:00
|
|
|
import base64
|
|
|
|
|
|
|
|
import deferred
|
|
|
|
from deferred import call
|
2013-06-28 17:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Buddy():
|
2015-12-01 19:33:02 +00:00
|
|
|
def __init__(self, owner, number, nick, statusMsg, groups, image_hash):
|
2013-06-28 17:46:25 +00:00
|
|
|
self.nick = nick
|
|
|
|
self.owner = owner
|
|
|
|
self.number = number
|
|
|
|
self.groups = groups
|
2015-12-31 19:43:10 +00:00
|
|
|
self.image_hash = image_hash if image_hash is not None else ""
|
2016-01-01 09:42:11 +00:00
|
|
|
self.statusMsg = u""
|
2015-11-10 16:04:20 +00:00
|
|
|
self.lastseen = 0
|
|
|
|
self.presence = 0
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2015-09-05 22:14:12 +00:00
|
|
|
def update(self, nick, groups, image_hash):
|
2013-06-28 17:46:25 +00:00
|
|
|
self.nick = nick
|
|
|
|
self.groups = groups
|
2015-09-21 18:30:11 +00:00
|
|
|
if image_hash is not None:
|
|
|
|
self.image_hash = image_hash
|
2013-06-28 17:46:25 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2015-12-31 19:43:10 +00:00
|
|
|
return "%s (nick=%s)" % (self.number, self.nick)
|
2013-06-28 17:46:25 +00:00
|
|
|
|
|
|
|
class BuddyList(dict):
|
|
|
|
|
2015-12-01 21:21:38 +00:00
|
|
|
def __init__(self, owner, backend, user, session):
|
2015-12-01 19:33:02 +00:00
|
|
|
self.owner = owner
|
2015-12-01 19:54:58 +00:00
|
|
|
self.backend = backend
|
2015-12-01 21:21:38 +00:00
|
|
|
self.session = session
|
2015-12-01 19:54:58 +00:00
|
|
|
self.user = user
|
|
|
|
self.logger = logging.getLogger(self.__class__.__name__)
|
|
|
|
self.synced = False
|
2015-11-09 22:09:12 +00:00
|
|
|
|
2015-12-02 20:51:29 +00:00
|
|
|
def _load(self, buddies):
|
2015-12-01 19:33:02 +00:00
|
|
|
for buddy in buddies:
|
|
|
|
number = buddy.buddyName
|
|
|
|
nick = buddy.alias
|
2016-01-01 09:42:11 +00:00
|
|
|
statusMsg = buddy.statusMessage.decode('utf-8')
|
2015-12-01 19:33:02 +00:00
|
|
|
groups = [g for g in buddy.group]
|
|
|
|
image_hash = buddy.iconHash
|
|
|
|
self[number] = Buddy(self.owner, number, nick, statusMsg,
|
|
|
|
groups, image_hash)
|
2015-12-02 20:51:29 +00:00
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("Update roster")
|
2015-12-01 19:54:58 +00:00
|
|
|
|
|
|
|
contacts = self.keys()
|
2015-12-31 19:43:10 +00:00
|
|
|
contacts.remove('bot')
|
2015-12-01 19:54:58 +00:00
|
|
|
|
|
|
|
if self.synced == False:
|
2015-12-02 20:51:29 +00:00
|
|
|
self.session.sendSync(contacts, delta = False, interactive = True)
|
2015-12-01 19:54:58 +00:00
|
|
|
self.synced = True
|
|
|
|
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("Roster add: %s", str(list(contacts)))
|
2015-12-01 19:54:58 +00:00
|
|
|
|
2015-12-01 21:21:38 +00:00
|
|
|
for number in contacts:
|
|
|
|
buddy = self[number]
|
2015-12-14 18:14:20 +00:00
|
|
|
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
|
|
|
|
buddy.groups, protocol_pb2.STATUS_NONE,
|
2015-12-31 19:43:10 +00:00
|
|
|
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
2015-12-14 18:14:20 +00:00
|
|
|
self.session.subscribePresence(number)
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("%s is requesting statuses of: %s", self.user, contacts)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.session.requestStatuses(contacts, success = self.onStatus)
|
|
|
|
|
|
|
|
def onStatus(self, contacts):
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("%s received statuses of: %s", self.user, contacts)
|
2015-12-14 18:14:20 +00:00
|
|
|
for number, (status, time) in contacts.iteritems():
|
|
|
|
buddy = self[number]
|
2016-01-01 09:42:11 +00:00
|
|
|
if status is None:
|
|
|
|
buddy.statusMsg = ""
|
|
|
|
else:
|
|
|
|
buddy.statusMsg = utils.softToUni(status)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.updateSpectrum(buddy)
|
2015-12-01 19:54:58 +00:00
|
|
|
|
2013-06-28 17:46:25 +00:00
|
|
|
|
2015-12-02 20:51:29 +00:00
|
|
|
def load(self, buddies):
|
|
|
|
if self.session.loggedIn:
|
|
|
|
self._load(buddies)
|
|
|
|
else:
|
|
|
|
self.session.loginQueue.append(lambda: self._load(buddies))
|
|
|
|
|
2015-09-05 22:14:12 +00:00
|
|
|
def update(self, number, nick, groups, image_hash):
|
2013-06-28 17:46:25 +00:00
|
|
|
if number in self:
|
|
|
|
buddy = self[number]
|
2015-09-05 22:14:12 +00:00
|
|
|
buddy.update(nick, groups, image_hash)
|
2013-06-28 17:46:25 +00:00
|
|
|
else:
|
2015-12-31 19:43:10 +00:00
|
|
|
buddy = Buddy(self.owner, number, nick, "", groups, image_hash)
|
2015-12-01 21:21:38 +00:00
|
|
|
self[number] = buddy
|
2015-12-31 19:43:10 +00:00
|
|
|
self.logger.debug("Roster add: %s", buddy)
|
2016-01-02 15:08:30 +00:00
|
|
|
self.session.sendSync([number], delta = True, interactive = True)
|
|
|
|
self.session.subscribePresence(number)
|
|
|
|
self.session.requestStatuses([number], success = self.onStatus)
|
|
|
|
if image_hash == "" or image_hash is None:
|
|
|
|
self.requestVCard(number)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.updateSpectrum(buddy)
|
|
|
|
return buddy
|
|
|
|
|
|
|
|
def updateSpectrum(self, buddy):
|
2015-12-02 20:51:29 +00:00
|
|
|
if buddy.presence == 0:
|
|
|
|
status = protocol_pb2.STATUS_NONE
|
2015-12-31 19:43:10 +00:00
|
|
|
elif buddy.presence == 'unavailable':
|
2015-12-02 20:51:29 +00:00
|
|
|
status = protocol_pb2.STATUS_AWAY
|
|
|
|
else:
|
|
|
|
status = protocol_pb2.STATUS_ONLINE
|
2015-12-14 18:14:20 +00:00
|
|
|
|
|
|
|
statusmsg = buddy.statusMsg
|
|
|
|
if buddy.lastseen != 0:
|
|
|
|
timestamp = time.localtime(buddy.lastseen)
|
2015-12-31 19:43:10 +00:00
|
|
|
statusmsg += time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
2015-12-14 18:14:20 +00:00
|
|
|
|
2016-01-02 15:08:30 +00:00
|
|
|
iconHash = buddy.image_hash if buddy.image_hash is not None else ""
|
|
|
|
|
|
|
|
self.logger.debug("Updating buddy %s (%s) in %s, image_hash = %s",
|
|
|
|
buddy.nick, buddy.number, buddy.groups, iconHash)
|
|
|
|
self.logger.debug("Status Message: %s", statusmsg)
|
2015-12-14 18:14:20 +00:00
|
|
|
self.backend.handleBuddyChanged(self.user, buddy.number, buddy.nick,
|
2016-01-02 15:08:30 +00:00
|
|
|
buddy.groups, status, statusMessage=statusmsg, iconHash=iconHash)
|
2013-06-28 17:46:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
def remove(self, number):
|
2015-09-03 18:04:29 +00:00
|
|
|
try:
|
|
|
|
buddy = self[number]
|
2015-12-01 21:21:38 +00:00
|
|
|
del self[number]
|
2015-12-31 19:43:10 +00:00
|
|
|
self.backend.handleBuddyChanged(self.user, number, "", [],
|
2015-12-01 19:54:58 +00:00
|
|
|
protocol_pb2.STATUS_NONE)
|
|
|
|
self.backend.handleBuddyRemoved(self.user, number)
|
2015-12-01 21:21:38 +00:00
|
|
|
self.session.unsubscribePresence(number)
|
2015-12-01 19:54:58 +00:00
|
|
|
# TODO Sync remove
|
2015-09-03 18:04:29 +00:00
|
|
|
return buddy
|
|
|
|
except KeyError:
|
|
|
|
return None
|
2016-01-02 15:08:30 +00:00
|
|
|
|
|
|
|
def requestVCard(self, buddy, ID=None):
|
|
|
|
if buddy == self.user or buddy == self.user.split('@')[0]:
|
|
|
|
buddy = self.session.legacyName
|
|
|
|
|
|
|
|
# Get profile picture
|
|
|
|
self.logger.debug('Requesting profile picture of %s', buddy)
|
|
|
|
response = deferred.Deferred()
|
|
|
|
self.session.requestProfilePicture(buddy, onSuccess = response.run)
|
|
|
|
response = response.arg(0)
|
|
|
|
|
|
|
|
pictureData = response.pictureData()
|
|
|
|
# Send VCard
|
|
|
|
if ID != None:
|
|
|
|
call(self.logger.debug, 'Sending VCard (%s) with image id %s: %s',
|
|
|
|
ID, response.pictureId(), pictureData.then(base64.b64encode))
|
|
|
|
call(self.backend.handleVCard, self.user, ID, buddy, "", "",
|
|
|
|
pictureData)
|
|
|
|
|
|
|
|
# Send image hash
|
|
|
|
if not buddy == self.session.legacyName:
|
|
|
|
try:
|
|
|
|
obuddy = self[buddy]
|
|
|
|
nick = obuddy.nick
|
|
|
|
groups = obuddy.groups
|
|
|
|
except KeyError:
|
|
|
|
nick = ""
|
|
|
|
groups = []
|
|
|
|
image_hash = pictureData.then(utils.sha1hash)
|
|
|
|
call(self.logger.debug, 'Image hash is %s', image_hash)
|
|
|
|
call(self.update, buddy, nick, groups, image_hash)
|