-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
briar.py
132 lines (121 loc) · 4.25 KB
/
briar.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
__filename__ = "briar.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.6.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "Profile Metadata"
from utils import get_attachment_property_value
from utils import remove_html
def get_briar_address(actor_json: {}) -> str:
"""Returns briar address for the given actor
"""
if not actor_json.get('attachment'):
return ''
if not isinstance(actor_json['attachment'], list):
return ''
for property_value in actor_json['attachment']:
name_value = None
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:
continue
if not name_value.lower().startswith('briar'):
continue
if not property_value.get('type'):
continue
prop_value_name, prop_value = \
get_attachment_property_value(property_value)
if not prop_value:
continue
if not property_value['type'].endswith('PropertyValue'):
continue
property_value[prop_value_name] = prop_value.strip()
if len(property_value[prop_value_name]) < 50:
continue
if not property_value[prop_value_name].startswith('briar://'):
continue
if property_value[prop_value_name].lower() != \
property_value[prop_value_name]:
continue
if '"' in property_value[prop_value_name]:
continue
if ' ' in property_value[prop_value_name]:
continue
if ',' in property_value[prop_value_name]:
continue
if '.' in property_value[prop_value_name]:
continue
return remove_html(property_value[prop_value_name])
return ''
def set_briar_address(actor_json: {}, briar_address: str) -> None:
"""Sets an briar address for the given actor
"""
not_briar_address = False
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
if not actor_json.get('attachment'):
actor_json['attachment']: list[dict] = []
# remove any existing value
property_found = None
for property_value in actor_json['attachment']:
name_value = None
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:
continue
if not property_value.get('type'):
continue
if not name_value.lower().startswith('briar'):
continue
property_found = property_value
break
if property_found:
actor_json['attachment'].remove(property_found)
if not_briar_address:
return
for property_value in actor_json['attachment']:
name_value = None
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:
continue
if not property_value.get('type'):
continue
if not name_value.lower().startswith('briar'):
continue
if not property_value['type'].endswith('PropertyValue'):
continue
prop_value_name, _ = \
get_attachment_property_value(property_value)
if not prop_value_name:
continue
property_value[prop_value_name] = briar_address
return
new_briar_address = {
"name": "Briar",
"type": "PropertyValue",
"value": briar_address
}
actor_json['attachment'].append(new_briar_address)