epicyon/xmpp.py

90 lines
2.6 KiB
Python
Raw Normal View History

2020-04-04 14:17:48 +00:00
__filename__ = "xmpp.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-04 14:17:48 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-04 14:17:48 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2019-12-17 14:57:16 +00:00
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
2020-04-04 14:17:48 +00:00
nameLower = propertyValue['name'].lower()
if not (nameLower.startswith('xmpp') or
2019-12-17 15:04:45 +00:00
nameLower.startswith('jabber')):
2019-12-17 14:57:16 +00:00
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
2020-04-04 14:17:48 +00:00
if propertyValue['type'] != 'PropertyValue':
2019-12-17 14:57:16 +00:00
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 ''
2020-04-04 14:17:48 +00:00
def setXmppAddress(actorJson: {}, xmppAddress: str) -> None:
2019-12-17 14:57:16 +00:00
"""Sets an xmpp address for the given actor
"""
2020-07-06 10:27:10 +00:00
notXmppAddress = False
if '@' not in xmppAddress:
notXmppAddress = True
if '.' not in xmppAddress:
notXmppAddress = True
if '"' in xmppAddress:
notXmppAddress = True
if '<' in xmppAddress:
notXmppAddress = True
2020-07-06 10:27:10 +00:00
2019-12-17 14:57:16 +00:00
if not actorJson.get('attachment'):
2020-04-04 14:17:48 +00:00
actorJson['attachment'] = []
2019-12-17 14:57:16 +00:00
2019-12-17 23:35:59 +00:00
# remove any existing value
2020-04-04 14:17:48 +00:00
propertyFound = None
2019-12-17 23:35:59 +00:00
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
2020-04-04 14:17:48 +00:00
if not (propertyValue['name'].lower().startswith('xmpp') or
2019-12-17 23:35:59 +00:00
propertyValue['name'].lower().startswith('jabber')):
continue
2020-04-04 14:17:48 +00:00
propertyFound = propertyValue
2019-12-17 23:35:59 +00:00
break
if propertyFound:
actorJson['attachment'].remove(propertyFound)
2020-07-06 10:27:10 +00:00
if notXmppAddress:
2019-12-17 14:57:16 +00:00
return
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
2020-04-04 14:17:48 +00:00
nameLower = propertyValue['name'].lower()
if not (nameLower.startswith('xmpp') or
2019-12-17 15:04:45 +00:00
nameLower.startswith('jabber')):
2019-12-17 14:57:16 +00:00
continue
2020-04-04 14:17:48 +00:00
if propertyValue['type'] != 'PropertyValue':
2019-12-17 14:57:16 +00:00
continue
2020-04-04 14:17:48 +00:00
propertyValue['value'] = xmppAddress
2019-12-17 14:57:16 +00:00
return
2020-04-04 14:17:48 +00:00
newXmppAddress = {
2019-12-17 14:57:16 +00:00
"name": "XMPP",
"type": "PropertyValue",
"value": xmppAddress
}
actorJson['attachment'].append(newXmppAddress)