epicyon/tox.py

101 lines
3.0 KiB
Python
Raw Normal View History

2020-04-04 12:11:50 +00:00
__filename__ = "tox.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-04 12:11:50 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-04 12:11:50 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2020-03-22 14:42:26 +00:00
2021-12-28 17:33:54 +00:00
def get_tox_address(actor_json: {}) -> str:
2020-03-22 14:42:26 +00:00
"""Returns tox address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2020-03-22 14:42:26 +00:00
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('tox'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('value'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
property_value['value'] = property_value['value'].strip()
if len(property_value['value']) != 76:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['value'].upper() != property_value['value']:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if '"' in property_value['value']:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if ' ' in property_value['value']:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if ',' in property_value['value']:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if '.' in property_value['value']:
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
return property_value['value']
2020-03-22 14:42:26 +00:00
return ''
2020-04-04 12:11:50 +00:00
def set_tox_address(actor_json: {}, tox_address: str) -> None:
2020-03-22 14:42:26 +00:00
"""Sets an tox address for the given actor
"""
2020-07-06 10:21:09 +00:00
notToxAddress = False
if len(tox_address) != 76:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if tox_address.upper() != tox_address:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if '"' in tox_address:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if ' ' in tox_address:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if '.' in tox_address:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if ',' in tox_address:
2020-07-06 10:21:09 +00:00
notToxAddress = True
if '<' in tox_address:
notToxAddress = True
2020-07-06 10:21:09 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2020-03-22 14:42:26 +00:00
# remove any existing value
2020-04-04 12:11:50 +00:00
propertyFound = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('tox'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
propertyFound = property_value
2020-03-22 14:42:26 +00:00
break
if propertyFound:
2021-12-26 10:29:52 +00:00
actor_json['attachment'].remove(propertyFound)
2020-07-06 10:21:09 +00:00
if notToxAddress:
2020-03-22 14:42:26 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
if not property_value.get('name'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value['name'].lower().startswith('tox'):
2020-03-22 14:42:26 +00:00
continue
2021-12-26 10:32:45 +00:00
if property_value['type'] != 'PropertyValue':
2020-03-22 14:42:26 +00:00
continue
property_value['value'] = tox_address
2020-03-22 14:42:26 +00:00
return
2020-04-04 12:11:50 +00:00
newToxAddress = {
2020-03-22 14:42:26 +00:00
"name": "Tox",
"type": "PropertyValue",
"value": tox_address
2020-03-22 14:42:26 +00:00
}
2021-12-26 10:29:52 +00:00
actor_json['attachment'].append(newToxAddress)