2006-07-27 22:36:21 +02:00
|
|
|
## common/zeroconf/roster_zeroconf.py
|
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Stefan Bethge <stefan@lanpartei.de>
|
|
|
|
##
|
|
|
|
## 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 2 only.
|
|
|
|
##
|
|
|
|
## 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.
|
|
|
|
##
|
|
|
|
|
|
|
|
|
2006-06-28 01:09:21 +02:00
|
|
|
from common.zeroconf import zeroconf
|
2006-05-29 21:57:39 +02:00
|
|
|
|
|
|
|
class Roster:
|
|
|
|
def __init__(self, zeroconf):
|
2006-07-06 16:26:44 +02:00
|
|
|
self._data = None
|
2006-05-29 21:57:39 +02:00
|
|
|
self.zeroconf = zeroconf # our zeroconf instance
|
|
|
|
|
2006-06-14 00:42:37 +02:00
|
|
|
def update_roster(self):
|
2006-09-17 17:07:35 +02:00
|
|
|
for val in self.zeroconf.contacts.values():
|
|
|
|
self.setItem(val[zeroconf.C_NAME])
|
2006-06-14 00:42:37 +02:00
|
|
|
|
2006-05-29 21:57:39 +02:00
|
|
|
def getRoster(self):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getRoster'
|
2006-07-06 16:26:44 +02:00
|
|
|
if self._data is None:
|
|
|
|
self._data = {}
|
|
|
|
self.update_roster()
|
2006-05-29 21:57:39 +02:00
|
|
|
return self
|
|
|
|
|
2006-06-14 00:42:37 +02:00
|
|
|
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 self._data.has_key(key):
|
|
|
|
if old_data[key] != self._data[key]:
|
|
|
|
diffs[key] = self._data[key]['status']
|
2006-07-06 16:26:44 +02:00
|
|
|
#print 'roster_zeroconf.py: diffs:' + str(diffs)
|
2006-06-14 00:42:37 +02:00
|
|
|
return diffs
|
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def setItem(self, jid, name = '', groups = ''):
|
2006-06-14 00:42:37 +02:00
|
|
|
#print 'roster_zeroconf.py: setItem %s' % jid
|
2006-09-17 17:07:35 +02:00
|
|
|
(service_jid, domain, interface, protocol, host, address, port, bare_jid, txt) \
|
2006-06-02 20:46:52 +02:00
|
|
|
= self.zeroconf.get_contact(jid)
|
|
|
|
|
|
|
|
self._data[jid]={}
|
|
|
|
self._data[jid]['ask'] = 'no' #?
|
|
|
|
self._data[jid]['subscription'] = 'both'
|
|
|
|
self._data[jid]['groups'] = []
|
|
|
|
self._data[jid]['resources'] = {}
|
|
|
|
self._data[jid]['address'] = address
|
|
|
|
self._data[jid]['host'] = host
|
|
|
|
self._data[jid]['port'] = port
|
|
|
|
txt_dict = self.zeroconf.txt_array_to_dict(txt)
|
|
|
|
if txt_dict.has_key('status'):
|
|
|
|
status = txt_dict['status']
|
|
|
|
else:
|
|
|
|
status = ''
|
2006-09-17 17:07:35 +02:00
|
|
|
nm = ''
|
|
|
|
if txt_dict.has_key('1st'):
|
|
|
|
nm = txt_dict['1st']
|
|
|
|
if txt_dict.has_key('last'):
|
|
|
|
if nm != '':
|
|
|
|
nm = +' '
|
|
|
|
nm += txt_dict['last']
|
|
|
|
if nm:
|
|
|
|
self._data[jid]['name'] = nm
|
2006-07-27 22:36:21 +02:00
|
|
|
else:
|
2006-09-17 17:07:35 +02:00
|
|
|
self._data[jid]['name'] = jid
|
|
|
|
if status == 'avail':
|
|
|
|
status = 'online'
|
2006-06-13 23:19:39 +02:00
|
|
|
self._data[jid]['txt_dict'] = txt_dict
|
2006-06-28 00:28:49 +02:00
|
|
|
if not self._data[jid]['txt_dict'].has_key('msg'):
|
|
|
|
self._data[jid]['txt_dict']['msg'] = ''
|
2006-06-02 20:46:52 +02:00
|
|
|
self._data[jid]['status'] = status
|
|
|
|
self._data[jid]['show'] = status
|
2006-06-13 23:19:39 +02:00
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def delItem(self, jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: delItem %s' % jid
|
2006-06-02 20:46:52 +02:00
|
|
|
if self._data.has_key(jid):
|
|
|
|
del self._data[jid]
|
2006-05-31 01:13:36 +02:00
|
|
|
|
2006-06-13 23:19:39 +02:00
|
|
|
def getItem(self, jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getItem: %s' % jid
|
2006-06-13 23:19:39 +02:00
|
|
|
if self._data.has_key(jid):
|
|
|
|
return self._data[jid]
|
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def __getitem__(self,jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: __getitem__'
|
2006-06-02 20:46:52 +02:00
|
|
|
return self._data[jid]
|
|
|
|
|
|
|
|
def getItems(self):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getItems'
|
2006-06-13 23:19:39 +02:00
|
|
|
# Return list of all [bare] JIDs that the roster currently tracks.
|
2006-06-02 20:46:52 +02:00
|
|
|
return self._data.keys()
|
|
|
|
|
|
|
|
def keys(self):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: keys'
|
2006-06-02 20:46:52 +02:00
|
|
|
return self._data.keys()
|
|
|
|
|
2006-05-29 21:57:39 +02:00
|
|
|
def getRaw(self):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getRaw'
|
2006-05-29 21:57:39 +02:00
|
|
|
return self._data
|
|
|
|
|
|
|
|
def getResources(self, jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getResources(%s)' % jid
|
2006-06-02 20:46:52 +02:00
|
|
|
return {}
|
2006-06-13 23:19:39 +02:00
|
|
|
|
|
|
|
def getGroups(self, jid):
|
|
|
|
return self._data[jid]['groups']
|
2006-07-27 22:36:21 +02:00
|
|
|
def getName(self, jid):
|
|
|
|
if self._data.has_key(jid):
|
|
|
|
return self._data[jid]['name']
|
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def getStatus(self, jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
if self._data.has_key(jid):
|
|
|
|
return self._data[jid]['status']
|
|
|
|
|
|
|
|
def getMessage(self, jid):
|
|
|
|
if self._data.has_key(jid):
|
|
|
|
return self._data[jid]['txt_dict']['msg']
|
2006-06-02 20:46:52 +02:00
|
|
|
|
|
|
|
def getShow(self, jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getShow'
|
2006-06-02 20:46:52 +02:00
|
|
|
return getStatus(jid)
|
|
|
|
|
|
|
|
def getPriority(jid):
|
|
|
|
return 5
|
|
|
|
|
|
|
|
def getSubscription(self,jid):
|
2006-06-28 00:28:49 +02:00
|
|
|
#print 'roster_zeroconf.py: getSubscription'
|
2006-06-02 20:46:52 +02:00
|
|
|
return 'both'
|
|
|
|
|
|
|
|
def Subscribe(self,jid):
|
|
|
|
pass
|
2006-05-29 21:57:39 +02:00
|
|
|
|
2006-06-02 20:46:52 +02:00
|
|
|
def Unsubscribe(self,jid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def Authorize(self,jid):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def Unauthorize(self,jid):
|
|
|
|
pass
|