epicyon/src/briar.py

194 lines
6.6 KiB
Python
Raw Normal View History

2020-12-24 16:48:03 +00:00
__filename__ = "briar.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2026-01-02 11:36:24 +00:00
__version__ = "1.7.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
2026-07-05 10:15:08 +00:00
import pyqrcode
from src.utils import load_json
from src.utils import acct_dir
from src.utils import get_attachment_property_value
from src.utils import remove_html
2026-07-05 10:15:08 +00:00
from src.data import is_a_file
from src.data import erase_file
2021-12-28 17:33:54 +00:00
def get_briar_address(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 ''
2023-11-29 10:58:48 +00:00
if not isinstance(actor_json['attachment'], list):
return ''
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2025-08-26 10:46:27 +00:00
if not isinstance(property_value, dict):
print("WARN: actor attachment is not dict: " + str(property_value))
continue
2026-05-05 12:36:14 +00:00
name_value: str = None
2022-05-11 16:10:38 +00:00
if property_value.get('name'):
2026-05-05 12:36:14 +00:00
if isinstance(property_value['name'], str):
name_value = property_value['name']
2022-05-11 16:10:38 +00:00
elif property_value.get('schema:name'):
2026-05-05 12:36:14 +00:00
if isinstance(property_value['schema:name'], str):
name_value = property_value['schema:name']
2022-05-11 16:10:38 +00:00
if not name_value:
2020-12-24 16:48:03 +00:00
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('briar'):
2020-12-24 16:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-12-24 16:48:03 +00:00
continue
2026-05-05 16:40:22 +00:00
if not isinstance(property_value['type'], str):
continue
prop_value_name, prop_value = \
get_attachment_property_value(property_value)
if not prop_value:
2020-12-24 16:48:03 +00:00
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
2020-12-24 16:48:03 +00:00
continue
property_value[prop_value_name] = prop_value.strip()
if len(property_value[prop_value_name]) < 50:
2020-12-24 16:48:03 +00:00
continue
if not property_value[prop_value_name].startswith('briar://'):
2020-12-24 16:55:06 +00:00
continue
if property_value[prop_value_name].lower() != \
property_value[prop_value_name]:
2020-12-24 16:48:03 +00:00
continue
if '"' in property_value[prop_value_name]:
2020-12-24 16:48:03 +00:00
continue
if ' ' in property_value[prop_value_name]:
2020-12-24 16:48:03 +00:00
continue
if ',' in property_value[prop_value_name]:
2020-12-24 16:48:03 +00:00
continue
if '.' in property_value[prop_value_name]:
2020-12-24 16:48:03 +00:00
continue
return remove_html(property_value[prop_value_name])
2020-12-24 16:48:03 +00:00
return ''
2026-07-05 10:15:08 +00:00
def save_briar_qrcode(base_dir: str,
nickname: str, domain: str,
scale: int = 6) -> bool:
"""Saves a qrcode image for the briar address of the person
This helps to transfer onion or i2p handles to a mobile device
"""
qrcode_filename = \
acct_dir(base_dir, nickname, domain) + '/qrcode_briar.png'
if is_a_file(qrcode_filename):
return False
actor_filename = \
acct_dir(base_dir, nickname, domain) + '.json'
if not is_a_file(actor_filename):
return False
actor_json = load_json(actor_filename)
if not actor_json:
return False
briar_address = get_briar_address(actor_json)
if not briar_address:
return False
url = pyqrcode.create(briar_address)
try:
url.png(qrcode_filename, scale)
return True
except ModuleNotFoundError:
print('EX: save_briar_qrcode pyqrcode png module not found')
return False
def set_briar_address(base_dir: str, nickname: str, domain: str,
actor_json: {}, briar_address: str,
qrcode_scale: int) -> None:
2020-12-24 16:48:03 +00:00
"""Sets an briar address for the given actor
"""
2026-07-05 10:15:08 +00:00
if not briar_address:
qrcode_filename = \
acct_dir(base_dir, nickname, domain) + '/qrcode_briar.png'
if is_a_file(qrcode_filename):
erase_file(qrcode_filename,
'EX: cannot remove briar qrcode ' + qrcode_filename)
2026-04-28 13:04:50 +00:00
not_briar_address: bool = False
2020-12-24 16:48:03 +00:00
2021-12-30 18:38:36 +00:00
if len(briar_address) < 50:
not_briar_address = True
if not briar_address.startswith('briar://'):
not_briar_address = True
if briar_address.lower() != briar_address:
not_briar_address = True
if '"' in briar_address:
not_briar_address = True
if ' ' in briar_address:
not_briar_address = True
if '.' in briar_address:
not_briar_address = True
if ',' in briar_address:
not_briar_address = True
if '<' in briar_address:
not_briar_address = True
2020-12-24 16:48:03 +00:00
2021-12-26 10:29:52 +00:00
if not actor_json.get('attachment'):
2024-12-23 15:39:55 +00:00
actor_json['attachment']: list[dict] = []
2020-12-24 16:48:03 +00:00
# remove any existing value
2026-05-05 12:36:14 +00:00
property_found: dict = None
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2025-08-26 10:46:27 +00:00
if not isinstance(property_value, dict):
print("WARN: actor attachment is not dict: " + str(property_value))
continue
2026-05-05 12:36:14 +00:00
name_value: str = None
2022-05-11 16:10:38 +00:00
if property_value.get('name'):
name_value = property_value['name']
elif property_value.get('schema:name'):
name_value = property_value['schema:name']
if not name_value:
2020-12-24 16:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-12-24 16:48:03 +00:00
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('briar'):
2020-12-24 16:48:03 +00:00
continue
2021-12-30 18:38:36 +00:00
property_found = property_value
2020-12-24 16:48:03 +00:00
break
2021-12-30 18:38:36 +00:00
if property_found:
actor_json['attachment'].remove(property_found)
if not_briar_address:
2020-12-24 16:48:03 +00:00
return
2021-12-26 10:32:45 +00:00
for property_value in actor_json['attachment']:
2025-08-26 10:46:27 +00:00
if not isinstance(property_value, dict):
print("WARN: actor attachment is not dict: " + str(property_value))
continue
2026-05-05 12:54:24 +00:00
name_value: str = None
2022-05-11 16:10:38 +00:00
if property_value.get('name'):
2026-05-05 12:36:14 +00:00
if isinstance(property_value['name'], str):
name_value = property_value['name']
2022-05-11 16:10:38 +00:00
elif property_value.get('schema:name'):
2026-05-05 12:36:14 +00:00
if isinstance(property_value['schema:name'], str):
name_value = property_value['schema:name']
2022-05-11 16:10:38 +00:00
if not name_value:
2020-12-24 16:48:03 +00:00
continue
2021-12-26 10:32:45 +00:00
if not property_value.get('type'):
2020-12-24 16:48:03 +00:00
continue
2026-05-05 16:40:22 +00:00
if not isinstance(property_value['type'], str):
continue
2022-05-11 16:10:38 +00:00
if not name_value.lower().startswith('briar'):
2020-12-24 16:48:03 +00:00
continue
2022-05-11 16:16:34 +00:00
if not property_value['type'].endswith('PropertyValue'):
2020-12-24 16:48:03 +00:00
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
property_value[prop_value_name] = briar_address
2020-12-24 16:48:03 +00:00
return
2021-12-30 18:38:36 +00:00
new_briar_address = {
2020-12-24 16:48:03 +00:00
"name": "Briar",
"type": "PropertyValue",
2021-12-30 18:38:36 +00:00
"value": briar_address
2020-12-24 16:48:03 +00:00
}
2021-12-30 18:38:36 +00:00
actor_json['attachment'].append(new_briar_address)
2026-07-05 10:15:08 +00:00
save_briar_qrcode(base_dir, nickname, domain, qrcode_scale)