epicyon/briar.py

105 lines
3.1 KiB
Python
Raw Normal View History

2020-12-24 16:48:03 +00:00
__filename__ = "briar.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-12-24 16:48:03 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-12-24 16:48:03 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "Profile Metadata"
2020-12-24 16:48:03 +00:00
2021-12-26 10:29:52 +00:00
def getBriarAddress(actor_json: {}) -> str:
2020-12-24 16:48:03 +00:00
"""Returns briar address for the given actor
"""
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2020-12-24 16:48:03 +00:00
return ''
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2020-12-24 16:48:03 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue['name'].lower().startswith('briar'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue.get('value'):
continue
if propertyValue['type'] != 'PropertyValue':
continue
propertyValue['value'] = propertyValue['value'].strip()
if len(propertyValue['value']) < 50:
continue
2020-12-24 16:55:06 +00:00
if not propertyValue['value'].startswith('briar://'):
continue
2020-12-24 16:48:03 +00:00
if propertyValue['value'].lower() != propertyValue['value']:
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 ''
2021-12-26 10:29:52 +00:00
def setBriarAddress(actor_json: {}, briarAddress: str) -> None:
2020-12-24 16:48:03 +00:00
"""Sets an briar address for the given actor
"""
notBriarAddress = False
if len(briarAddress) < 50:
notBriarAddress = True
2020-12-24 16:55:06 +00:00
if not briarAddress.startswith('briar://'):
notBriarAddress = True
2020-12-24 16:48:03 +00:00
if briarAddress.lower() != briarAddress:
notBriarAddress = True
if '"' in briarAddress:
notBriarAddress = True
if ' ' in briarAddress:
notBriarAddress = True
if '.' in briarAddress:
notBriarAddress = True
if ',' in briarAddress:
notBriarAddress = True
if '<' in briarAddress:
notBriarAddress = True
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
actor_json['attachment'] = []
2020-12-24 16:48:03 +00:00
# remove any existing value
propertyFound = None
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2020-12-24 16:48:03 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('briar'):
continue
propertyFound = propertyValue
break
if propertyFound:
2021-12-26 10:29:52 +00:00
actor_json['attachment'].remove(propertyFound)
2020-12-24 16:48:03 +00:00
if notBriarAddress:
return
2021-12-26 10:29:52 +00:00
for propertyValue in actor_json['attachment']:
2020-12-24 16:48:03 +00:00
if not propertyValue.get('name'):
continue
if not propertyValue.get('type'):
continue
if not propertyValue['name'].lower().startswith('briar'):
continue
if propertyValue['type'] != 'PropertyValue':
continue
propertyValue['value'] = briarAddress
return
newBriarAddress = {
"name": "Briar",
"type": "PropertyValue",
"value": briarAddress
}
2021-12-26 10:29:52 +00:00
actor_json['attachment'].append(newBriarAddress)