2019-12-17 14:57:16 +00:00
|
|
|
__filename__ = "xmpp.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
|
|
|
__version__ = "1.1.0"
|
|
|
|
__maintainer__ = "Bob Mottram"
|
|
|
|
__email__ = "bob@freedombone.net"
|
|
|
|
__status__ = "Production"
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
|
|
|
def getXmppAddress(actorJson: {}) -> str:
|
|
|
|
"""Returns xmpp address for the given actor
|
|
|
|
"""
|
|
|
|
if not actorJson.get('attachment'):
|
|
|
|
return ''
|
|
|
|
for propertyValue in actorJson['attachment']:
|
|
|
|
if not propertyValue.get('name'):
|
|
|
|
continue
|
2019-12-17 15:04:45 +00:00
|
|
|
nameLower=propertyValue['name'].lower()
|
|
|
|
if not (nameLower.startswith('xmpp') or \
|
|
|
|
nameLower.startswith('jabber')):
|
2019-12-17 14:57:16 +00:00
|
|
|
continue
|
|
|
|
if not propertyValue.get('type'):
|
|
|
|
continue
|
|
|
|
if not propertyValue.get('value'):
|
|
|
|
continue
|
|
|
|
if propertyValue['type']!='PropertyValue':
|
|
|
|
continue
|
|
|
|
if '@' not in propertyValue['value']:
|
|
|
|
continue
|
2019-12-17 15:06:13 +00:00
|
|
|
if '"' in propertyValue['value']:
|
2019-12-17 14:57:16 +00:00
|
|
|
continue
|
|
|
|
return propertyValue['value']
|
|
|
|
return ''
|
|
|
|
|
|
|
|
def setXmppAddress(actorJson: {},xmppAddress: str) -> None:
|
|
|
|
"""Sets an xmpp address for the given actor
|
|
|
|
"""
|
|
|
|
if not actorJson.get('attachment'):
|
|
|
|
actorJson['attachment']=[]
|
|
|
|
|
2019-12-17 23:35:59 +00:00
|
|
|
# remove any existing value
|
|
|
|
propertyFound=None
|
|
|
|
for propertyValue in actorJson['attachment']:
|
|
|
|
if not propertyValue.get('name'):
|
|
|
|
continue
|
|
|
|
if not propertyValue.get('type'):
|
|
|
|
continue
|
|
|
|
if not (propertyValue['name'].lower().startswith('xmpp') or \
|
|
|
|
propertyValue['name'].lower().startswith('jabber')):
|
|
|
|
continue
|
|
|
|
propertyFound=propertyValue
|
|
|
|
break
|
|
|
|
if propertyFound:
|
|
|
|
actorJson['attachment'].remove(propertyFound)
|
|
|
|
|
2019-12-17 14:57:16 +00:00
|
|
|
if '@' not in xmppAddress:
|
|
|
|
return
|
|
|
|
if '.' not in xmppAddress:
|
|
|
|
return
|
|
|
|
if '"' in xmppAddress:
|
|
|
|
return
|
|
|
|
|
|
|
|
for propertyValue in actorJson['attachment']:
|
|
|
|
if not propertyValue.get('name'):
|
|
|
|
continue
|
|
|
|
if not propertyValue.get('type'):
|
|
|
|
continue
|
2019-12-17 15:04:45 +00:00
|
|
|
nameLower=propertyValue['name'].lower()
|
|
|
|
if not (nameLower.startswith('xmpp') or \
|
|
|
|
nameLower.startswith('jabber')):
|
2019-12-17 14:57:16 +00:00
|
|
|
continue
|
|
|
|
if propertyValue['type']!='PropertyValue':
|
|
|
|
continue
|
|
|
|
propertyValue['value']=xmppAddress
|
|
|
|
return
|
|
|
|
|
|
|
|
newXmppAddress={
|
|
|
|
"name": "XMPP",
|
|
|
|
"type": "PropertyValue",
|
|
|
|
"value": xmppAddress
|
|
|
|
}
|
|
|
|
actorJson['attachment'].append(newXmppAddress)
|