epicyon/tox.py

101 lines
2.9 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
def getToxAddress(actorJson: {}) -> str:
"""Returns tox address for the given actor
"""
if not actorJson.get('attachment'):
return ''
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue['name'].lower().startswith('tox'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
2020-04-04 12:11:50 +00:00
if propertyValue['type'] != 'PropertyValue':
2020-03-22 14:42:26 +00:00
continue
2020-04-04 12:11:50 +00:00
propertyValue['value'] = propertyValue['value'].strip()
if len(propertyValue['value']) != 76:
2020-03-22 14:42:26 +00:00
continue
2020-04-04 12:11:50 +00:00
if propertyValue['value'].upper() != propertyValue['value']:
2020-03-22 14:42:26 +00:00
continue
if '"' in propertyValue['value']:
continue
if ' ' in propertyValue['value']:
continue
if ',' in propertyValue['value']:
continue
if '.' in propertyValue['value']:
continue
return propertyValue['value']
return ''
2020-04-04 12:11:50 +00:00
def setToxAddress(actorJson: {}, toxAddress: 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(toxAddress) != 76:
notToxAddress = True
if toxAddress.upper() != toxAddress:
notToxAddress = True
if '"' in toxAddress:
notToxAddress = True
if ' ' in toxAddress:
notToxAddress = True
if '.' in toxAddress:
notToxAddress = True
if ',' in toxAddress:
notToxAddress = True
if '<' in toxAddress:
notToxAddress = True
2020-07-06 10:21:09 +00:00
2020-03-22 14:42:26 +00:00
if not actorJson.get('attachment'):
2020-04-04 12:11:50 +00:00
actorJson['attachment'] = []
2020-03-22 14:42:26 +00:00
# remove any existing value
2020-04-04 12:11:50 +00:00
propertyFound = None
2020-03-22 14:42:26 +00:00
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('tox'):
continue
2020-04-04 12:11:50 +00:00
propertyFound = propertyValue
2020-03-22 14:42:26 +00:00
break
if propertyFound:
actorJson['attachment'].remove(propertyFound)
2020-07-06 10:21:09 +00:00
if notToxAddress:
2020-03-22 14:42:26 +00:00
return
for propertyValue in actorJson['attachment']:
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('tox'):
continue
2020-04-04 12:11:50 +00:00
if propertyValue['type'] != 'PropertyValue':
2020-03-22 14:42:26 +00:00
continue
2020-04-04 12:11:50 +00:00
propertyValue['value'] = toxAddress
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": toxAddress
}
actorJson['attachment'].append(newToxAddress)