epicyon/xmpp.py

90 lines
2.7 KiB
Python
Raw Normal View History

2020-04-04 14:17:48 +00:00
__filename__ = "xmpp.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2022-02-03 13:58:20 +00:00
__version__ = "1.3.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
2021-12-28 17:33:54 +00:00
def get_xmpp_address(actor_json: {}) -> str:
2019-12-17 14:57:16 +00:00
"""Returns xmpp address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2019-12-17 14:57:16 +00:00
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 14:57:16 +00:00
continue
2022-01-03 21:19:01 +00:00
name_lower = property_value['name'].lower()
if not (name_lower.startswith('xmpp') or
name_lower.startswith('jabber')):
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if '@' not in property_value['value']:
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if '"' in property_value['value']:
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2019-12-17 14:57:16 +00:00
return ''
2020-04-04 14:17:48 +00:00
def set_xmpp_address(actor_json: {}, xmpp_address: str) -> None:
2019-12-17 14:57:16 +00:00
"""Sets an xmpp address for the given actor
"""
2022-01-03 21:19:01 +00:00
not_xmpp_address = False
if '@' not in xmpp_address:
2022-01-03 21:19:01 +00:00
not_xmpp_address = True
if '.' not in xmpp_address:
2022-01-03 21:19:01 +00:00
not_xmpp_address = True
if '"' in xmpp_address:
2022-01-03 21:19:01 +00:00
not_xmpp_address = True
if '<' in xmpp_address:
2022-01-03 21:19:01 +00:00
not_xmpp_address = True
2020-07-06 10:27:10 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2019-12-17 14:57:16 +00:00
2019-12-17 23:35:59 +00:00
# remove any existing value
2022-01-03 10:27:55 +00:00
property_found = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 23:35:59 +00:00
continue
2021-12-26 10:32:45 +00:00
if not (property_value['name'].lower().startswith('xmpp') or
property_value['name'].lower().startswith('jabber')):
2019-12-17 23:35:59 +00:00
continue
2022-01-03 10:27:55 +00:00
property_found = property_value
2019-12-17 23:35:59 +00:00
break
2022-01-03 10:27:55 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
2022-01-03 21:19:01 +00:00
if not_xmpp_address:
2019-12-17 14:57:16 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2019-12-17 14:57:16 +00:00
continue
2022-01-03 21:19:01 +00:00
name_lower = property_value['name'].lower()
if not (name_lower.startswith('xmpp') or
name_lower.startswith('jabber')):
2019-12-17 14:57:16 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2019-12-17 14:57:16 +00:00
continue
property_value['value'] = xmpp_address
2019-12-17 14:57:16 +00:00
return
2022-01-03 21:19:01 +00:00
new_xmpp_address = {
2019-12-17 14:57:16 +00:00
"name": "XMPP",
"type": "PropertyValue",
"value": xmpp_address
2019-12-17 14:57:16 +00:00
}
2022-01-03 21:19:01 +00:00
actor_json['attachment'].append(new_xmpp_address)