2006-07-27 22:36:21 +02:00
|
|
|
## common/zeroconf/roster_zeroconf.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2006-07-27 22:36:21 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2006-07-27 22:36:21 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2006-07-27 22:36:21 +02:00
|
|
|
## 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.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
##
|
2006-07-27 22:36:21 +02:00
|
|
|
|
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.zeroconf import zeroconf
|
|
|
|
from gajim.common.zeroconf.zeroconf import Constant, ConstantRI
|
2006-05-29 21:57:39 +02:00
|
|
|
|
|
|
|
class Roster:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, zeroconf):
|
|
|
|
self._data = None
|
|
|
|
self.zeroconf = zeroconf # our zeroconf instance
|
2010-10-26 11:25:30 +02:00
|
|
|
self.version = ''
|
|
|
|
self.received_from_server = True
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def update_roster(self):
|
|
|
|
for val in self.zeroconf.contacts.values():
|
2017-02-07 21:18:41 +01:00
|
|
|
self.setItem(val[Constant.NAME])
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def getRoster(self):
|
|
|
|
if self._data is None:
|
|
|
|
self._data = {}
|
|
|
|
self.update_roster()
|
|
|
|
return self
|
|
|
|
|
|
|
|
def getDiffs(self):
|
|
|
|
"""
|
|
|
|
Update the roster with new data and return dict with jid -> new status
|
|
|
|
pairs to do notifications and stuff
|
|
|
|
"""
|
|
|
|
diffs = {}
|
|
|
|
old_data = self._data.copy()
|
|
|
|
self.update_roster()
|
|
|
|
for key in old_data.keys():
|
|
|
|
if key in self._data:
|
|
|
|
if old_data[key] != self._data[key]:
|
|
|
|
diffs[key] = self._data[key]['status']
|
|
|
|
return diffs
|
|
|
|
|
|
|
|
def setItem(self, jid, name='', groups=''):
|
|
|
|
contact = self.zeroconf.get_contact(jid)
|
|
|
|
if not contact:
|
|
|
|
return
|
|
|
|
|
2015-04-06 16:33:10 +02:00
|
|
|
addresses = []
|
|
|
|
i = 0
|
2017-02-07 21:18:41 +01:00
|
|
|
for ri in contact[Constant.RESOLVED_INFO]:
|
2015-04-06 16:33:10 +02:00
|
|
|
addresses += [{}]
|
2017-02-07 21:18:41 +01:00
|
|
|
addresses[i]['host'] = ri[ConstantRI.HOST]
|
|
|
|
addresses[i]['address'] = ri[ConstantRI.ADDRESS]
|
|
|
|
addresses[i]['port'] = ri[ConstantRI.PORT]
|
2015-04-06 16:33:10 +02:00
|
|
|
i += 1
|
2017-02-07 21:18:41 +01:00
|
|
|
txt = contact[Constant.TXT]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
self._data[jid]={}
|
|
|
|
self._data[jid]['ask'] = 'none'
|
|
|
|
self._data[jid]['subscription'] = 'both'
|
|
|
|
self._data[jid]['groups'] = []
|
|
|
|
self._data[jid]['resources'] = {}
|
2015-04-06 16:33:10 +02:00
|
|
|
self._data[jid]['addresses'] = addresses
|
2010-02-08 15:08:40 +01:00
|
|
|
txt_dict = self.zeroconf.txt_array_to_dict(txt)
|
|
|
|
status = txt_dict.get('status', '')
|
|
|
|
if not status:
|
|
|
|
status = 'avail'
|
|
|
|
nm = txt_dict.get('1st', '')
|
|
|
|
if 'last' in txt_dict:
|
|
|
|
if nm != '':
|
|
|
|
nm += ' '
|
|
|
|
nm += txt_dict['last']
|
|
|
|
if nm:
|
|
|
|
self._data[jid]['name'] = nm
|
|
|
|
else:
|
|
|
|
self._data[jid]['name'] = jid
|
|
|
|
if status == 'avail':
|
|
|
|
status = 'online'
|
|
|
|
self._data[jid]['txt_dict'] = txt_dict
|
|
|
|
if 'msg' not in self._data[jid]['txt_dict']:
|
|
|
|
self._data[jid]['txt_dict']['msg'] = ''
|
|
|
|
self._data[jid]['status'] = status
|
|
|
|
self._data[jid]['show'] = status
|
|
|
|
|
|
|
|
def setItemMulti(self, items):
|
|
|
|
for i in items:
|
|
|
|
self.setItem(jid=i['jid'], name=i['name'], groups=i['groups'])
|
|
|
|
|
|
|
|
def delItem(self, jid):
|
|
|
|
if jid in self._data:
|
|
|
|
del self._data[jid]
|
|
|
|
|
|
|
|
def getItem(self, jid):
|
|
|
|
if jid in self._data:
|
|
|
|
return self._data[jid]
|
|
|
|
|
|
|
|
def __getitem__(self, jid):
|
|
|
|
return self._data[jid]
|
|
|
|
|
|
|
|
def getItems(self):
|
|
|
|
# Return list of all [bare] JIDs that the roster currently tracks.
|
|
|
|
return self._data.keys()
|
|
|
|
|
|
|
|
def keys(self):
|
|
|
|
return self._data.keys()
|
|
|
|
|
|
|
|
def getRaw(self):
|
|
|
|
return self._data
|
|
|
|
|
|
|
|
def getResources(self, jid):
|
|
|
|
return {}
|
|
|
|
|
|
|
|
def getGroups(self, jid):
|
|
|
|
return self._data[jid]['groups']
|
|
|
|
|
|
|
|
def getName(self, jid):
|
|
|
|
if jid in self._data:
|
|
|
|
return self._data[jid]['name']
|
|
|
|
|
|
|
|
def getStatus(self, jid):
|
|
|
|
if jid in self._data:
|
|
|
|
return self._data[jid]['status']
|
|
|
|
|
|
|
|
def getMessage(self, jid):
|
|
|
|
if jid in self._data:
|
|
|
|
return self._data[jid]['txt_dict']['msg']
|
|
|
|
|
|
|
|
def getShow(self, jid):
|
|
|
|
return self.getStatus(jid)
|
|
|
|
|
|
|
|
def getPriority(self, jid):
|
|
|
|
return 5
|
|
|
|
|
|
|
|
def getSubscription(self, jid):
|
|
|
|
return 'both'
|
|
|
|
|
|
|
|
def Subscribe(self, jid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def Unsubscribe(self, jid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def Authorize(self, jid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def Unauthorize(self, jid):
|
|
|
|
pass
|