epicyon/cwtch.py

93 lines
2.7 KiB
Python
Raw Normal View History

2021-06-27 11:48:03 +00:00
__filename__ = "cwtch.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.2.0"
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2021-06-27 11:48:03 +00:00
__status__ = "Production"
__module_group__ = "Profile Metadata"
import re
2021-12-28 17:33:54 +00:00
def get_cwtch_address(actor_json: {}) -> str:
2021-06-27 11:48:03 +00:00
"""Returns cwtch address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2021-06-27 11:48:03 +00:00
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('cwtch'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
property_value['value'] = property_value['value'].strip()
if len(property_value['value']) < 2:
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if '"' in property_value['value']:
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if ' ' in property_value['value']:
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if ',' in property_value['value']:
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if '.' in property_value['value']:
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2021-06-27 11:48:03 +00:00
return ''
2021-12-30 20:48:38 +00:00
def set_cwtch_address(actor_json: {}, cwtch_address: str) -> None:
2021-06-27 11:48:03 +00:00
"""Sets an cwtch address for the given actor
"""
2021-12-30 20:48:38 +00:00
not_cwtch_address = False
2021-06-27 11:48:03 +00:00
2021-12-30 20:48:38 +00:00
if len(cwtch_address) < 56:
not_cwtch_address = True
if cwtch_address != cwtch_address.lower():
not_cwtch_address = True
if not re.match("^[a-z0-9]*$", cwtch_address):
not_cwtch_address = True
2021-06-27 11:48:03 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2021-06-27 11:48:03 +00:00
# remove any existing value
2021-12-30 20:48:38 +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'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('cwtch'):
2021-06-27 11:48:03 +00:00
continue
2021-12-30 20:48:38 +00:00
property_found = property_value
2021-06-27 11:48:03 +00:00
break
2021-12-30 20:48:38 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
if not_cwtch_address:
2021-06-27 11:48:03 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('cwtch'):
2021-06-27 11:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2021-06-27 11:48:03 +00:00
continue
2021-12-30 20:48:38 +00:00
property_value['value'] = cwtch_address
2021-06-27 11:48:03 +00:00
return
2021-12-30 20:48:38 +00:00
new_cwtch_address = {
2021-06-27 11:48:03 +00:00
"name": "Cwtch",
"type": "PropertyValue",
2021-12-30 20:48:38 +00:00
"value": cwtch_address
2021-06-27 11:48:03 +00:00
}
2021-12-30 20:48:38 +00:00
actor_json['attachment'].append(new_cwtch_address)