transpub/buddy.py

145 lines
4.1 KiB
Python
Raw Normal View History

2013-08-01 23:45:51 +00:00
__author__ = "Steffen Vogel"
__copyright__ = "Copyright 2013, Steffen Vogel"
__license__ = "GPLv3"
__maintainer__ = "Steffen Vogel"
__email__ = "post@steffenvogel.de"
__status__ = "Prototype"
"""
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
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
self.image_hash = image_hash if image_hash is not None else ""
self.statusMsg = ""
2015-11-10 16:04:20 +00:00
self.lastseen = 0
self.presence = 0
2013-06-28 17:46:25 +00:00
def update(self, nick, groups, image_hash):
2013-06-28 17:46:25 +00:00
self.nick = nick
self.groups = groups
if image_hash is not None:
self.image_hash = image_hash
2013-06-28 17:46:25 +00:00
def __str__(self):
2015-12-01 19:33:02 +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
def _load(self, buddies):
2015-12-01 19:33:02 +00:00
for buddy in buddies:
number = buddy.buddyName
nick = buddy.alias
statusMsg = buddy.statusMessage
groups = [g for g in buddy.group]
image_hash = buddy.iconHash
self[number] = Buddy(self.owner, number, nick, statusMsg,
groups, image_hash)
2015-12-01 19:54:58 +00:00
self.logger.debug("Update roster")
# old = self.buddies.keys()
# self.buddies.load()
# new = self.buddies.keys()
# contacts = new
contacts = self.keys()
if self.synced == False:
self.session.sendSync(contacts, delta = False, interactive = True)
2015-12-01 19:54:58 +00:00
self.synced = True
# add = set(new) - set(old)
# remove = set(old) - set(new)
# self.logger.debug("Roster remove: %s", str(list(remove)))
self.logger.debug("Roster add: %s", str(list(contacts)))
# for number in remove:
# self.backend.handleBuddyChanged(self.user, number, "", [],
# protocol_pb2.STATUS_NONE)
# self.backend.handleBuddyRemoved(self.user, number)
# self.unsubscribePresence(number)
#
2015-12-01 21:21:38 +00:00
for number in contacts:
buddy = self[number]
if number != 'bot':
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
buddy.groups, protocol_pb2.STATUS_NONE,
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
self.session.subscribePresence(number)
2015-12-01 19:54:58 +00:00
2013-06-28 17:46:25 +00:00
def load(self, buddies):
if self.session.loggedIn:
self._load(buddies)
else:
self.session.loginQueue.append(lambda: self._load(buddies))
def update(self, number, nick, groups, image_hash):
2013-06-28 17:46:25 +00:00
if number in self:
buddy = self[number]
buddy.update(nick, groups, image_hash)
2013-06-28 17:46:25 +00:00
else:
2015-12-01 21:21:38 +00:00
self.session.sendSync([number], delta = True, interactive = True)
self.session.subscribePresence(number)
2015-12-01 19:33:02 +00:00
buddy = Buddy(self.owner, number, nick, "", groups, image_hash)
2015-12-01 21:21:38 +00:00
self[number] = buddy
2015-12-01 19:54:58 +00:00
self.logger.debug("Roster add: %s", buddy)
if buddy.presence == 0:
status = protocol_pb2.STATUS_NONE
elif buddy.presence == 'unavailable':
status = protocol_pb2.STATUS_AWAY
else:
status = protocol_pb2.STATUS_ONLINE
2015-12-01 19:54:58 +00:00
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
buddy.groups, status,
2015-12-01 19:54:58 +00:00
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
2013-06-28 17:46:25 +00:00
return buddy
def remove(self, number):
try:
buddy = self[number]
2015-12-01 21:21:38 +00:00
del self[number]
2015-12-01 19:54:58 +00:00
self.backend.handleBuddyChanged(self.user, number, "", [],
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
return buddy
except KeyError:
return None