-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
announce.py
554 lines (495 loc) · 20.3 KB
/
announce.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
""" ActivityPub Announce (aka retweet/boost) """
__filename__ = "announce.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.5.0"
__maintainer__ = "Bob Mottram"
__email__ = "[email protected]"
__status__ = "Production"
__module_group__ = "ActivityPub"
import os
from flags import has_group_type
from flags import url_permitted
from utils import text_in_file
from utils import get_user_paths
from utils import has_object_string_object
from utils import has_object_dict
from utils import remove_domain_port
from utils import remove_id_ending
from utils import has_users_path
from utils import get_full_domain
from utils import get_status_number
from utils import create_outbox_dir
from utils import get_nickname_from_actor
from utils import get_domain_from_actor
from utils import locate_post
from utils import save_json
from utils import undo_announce_collection_entry
from utils import update_announce_collection
from utils import local_actor_url
from utils import replace_users_with_at
from utils import has_actor
from utils import has_object_string_type
from utils import get_actor_from_post
from posts import send_signed_json
from posts import get_person_box
from session import post_json
from webfinger import webfinger_handle
from auth import create_basic_auth_header
def no_of_announces(post_json_object: {}) -> int:
"""Returns the number of announces on a given post
"""
obj = post_json_object
if has_object_dict(post_json_object):
obj = post_json_object['object']
if not obj.get('shares'):
return 0
if not isinstance(obj['shares'], dict):
return 0
if not obj['shares'].get('items'):
obj['shares']['items'] = []
obj['shares']['totalItems'] = 0
return len(obj['shares']['items'])
def is_announce(post_json_object: {}) -> bool:
"""Is the given post an announce?
"""
if not post_json_object.get('type'):
return False
if post_json_object['type'] != 'Announce':
return False
return True
def is_self_announce(post_json_object: {}) -> bool:
"""Is the given post a self announce?
"""
if not post_json_object.get('actor'):
return False
if not post_json_object.get('type'):
return False
if post_json_object['type'] != 'Announce':
return False
if not post_json_object.get('object'):
return False
actor_url = get_actor_from_post(post_json_object)
if not isinstance(actor_url, str):
return False
if not isinstance(post_json_object['object'], str):
return False
return actor_url in post_json_object['object']
def outbox_announce(recent_posts_cache: {},
base_dir: str, message_json: {}, debug: bool) -> bool:
""" Adds or removes announce entries from the shares collection
within a given post
"""
if not has_actor(message_json, debug):
return False
if not isinstance(message_json['actor'], str):
return False
if not message_json.get('type'):
return False
if not message_json.get('object'):
return False
if message_json['type'] == 'Announce':
if not isinstance(message_json['object'], str):
return False
if is_self_announce(message_json):
return False
actor_url = get_actor_from_post(message_json)
nickname = get_nickname_from_actor(actor_url)
if not nickname:
print('WARN: no nickname found in ' + actor_url)
return False
domain, _ = get_domain_from_actor(actor_url)
if not domain:
print('WARN: no domain found in ' + actor_url)
return False
post_filename = locate_post(base_dir, nickname, domain,
message_json['object'])
if post_filename:
update_announce_collection(recent_posts_cache,
base_dir, post_filename,
actor_url,
nickname, domain, debug)
return True
elif message_json['type'] == 'Undo':
if not has_object_string_type(message_json, debug):
return False
if message_json['object']['type'] == 'Announce':
if not isinstance(message_json['object']['object'], str):
return False
actor_url = get_actor_from_post(message_json)
nickname = get_nickname_from_actor(actor_url)
if not nickname:
print('WARN: no nickname found in ' + actor_url)
return False
domain, _ = get_domain_from_actor(actor_url)
if not domain:
print('WARN: no domain found in ' + actor_url)
return False
post_filename = locate_post(base_dir, nickname, domain,
message_json['object']['object'])
if post_filename:
undo_announce_collection_entry(recent_posts_cache,
base_dir, post_filename,
actor_url,
domain, debug)
return True
return False
def announced_by_person(is_announced: bool, post_actor: str,
nickname: str, domain_full: str) -> bool:
"""Returns True if the given post is announced by the given person
"""
if not post_actor:
return False
if is_announced:
users_paths = get_user_paths()
for possible_path in users_paths:
if post_actor.endswith(domain_full + possible_path + nickname):
return True
return False
def create_announce(session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int,
to_url: str, cc_url: str, http_prefix: str,
object_url: str, save_to_file: bool,
client_to_server: bool,
send_threads: [], post_log: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str,
curr_domain: str,
onion_domain: str, i2p_domain: str,
sites_unavailable: [],
system_language: str) -> {}:
"""Creates an announce message
Typically to_url will be https://www.w3.org/ns/activitystreams#Public
and cc_url might be a specific person favorited or repeated and the
followers url object_url is typically the url of the message,
corresponding to url or atomUri in createPostBase
"""
if not url_permitted(object_url, federation_list):
return None
domain = remove_domain_port(domain)
full_domain = get_full_domain(domain, port)
status_number, published = get_status_number()
new_announce_id = http_prefix + '://' + full_domain + \
'/users/' + nickname + '/statuses/' + status_number
atom_uri_str = local_actor_url(http_prefix, nickname, full_domain) + \
'/statuses/' + status_number
new_announce = {
"@context": [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'actor': local_actor_url(http_prefix, nickname, full_domain),
'atomUri': atom_uri_str,
'cc': [],
'id': new_announce_id + '/activity',
'object': object_url,
'published': published,
'to': [to_url],
'type': 'Announce'
}
if cc_url:
if len(cc_url) > 0:
new_announce['cc'] = [cc_url]
if save_to_file:
outbox_dir = create_outbox_dir(nickname, domain, base_dir)
filename = \
outbox_dir + '/' + new_announce_id.replace('/', '#') + '.json'
save_json(new_announce, filename)
announce_nickname = None
announce_domain = None
announce_port = None
group_account = False
if has_users_path(object_url):
announce_nickname = get_nickname_from_actor(object_url)
announce_domain, announce_port = get_domain_from_actor(object_url)
if announce_nickname and announce_domain:
if '/' + str(announce_nickname) + '/' in object_url:
announce_actor = \
object_url.split('/' + announce_nickname + '/')[0] + \
'/' + announce_nickname
if has_group_type(base_dir, announce_actor, person_cache):
group_account = True
if announce_nickname and announce_domain:
extra_headers = {}
send_signed_json(new_announce, session, base_dir,
nickname, domain, port,
announce_nickname, announce_domain,
announce_port,
http_prefix, client_to_server, federation_list,
send_threads, post_log, cached_webfingers,
person_cache,
debug, project_version, None, group_account,
signing_priv_key_pem, 639633,
curr_domain, onion_domain, i2p_domain,
extra_headers, sites_unavailable,
system_language)
return new_announce
def announce_public(session, base_dir: str, federation_list: [],
nickname: str, domain: str, port: int, http_prefix: str,
object_url: str, client_to_server: bool,
send_threads: [], post_log: [],
person_cache: {}, cached_webfingers: {},
debug: bool, project_version: str,
signing_priv_key_pem: str,
curr_domain: str,
onion_domain: str, i2p_domain: str,
sites_unavailable: [],
system_language: str) -> {}:
"""Makes a public announcement
"""
from_domain = get_full_domain(domain, port)
to_url = 'https://www.w3.org/ns/activitystreams#Public'
cc_url = local_actor_url(http_prefix, nickname, from_domain) + '/followers'
return create_announce(session, base_dir, federation_list,
nickname, domain, port,
to_url, cc_url, http_prefix,
object_url, True, client_to_server,
send_threads, post_log,
person_cache, cached_webfingers,
debug, project_version,
signing_priv_key_pem, curr_domain,
onion_domain, i2p_domain,
sites_unavailable,
system_language)
def send_announce_via_server(base_dir: str, session,
from_nickname: str, password: str,
from_domain: str, from_port: int,
http_prefix: str, repeat_object_url: str,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str,
system_language: str) -> {}:
"""Creates an announce message via c2s
"""
if not session:
print('WARN: No session for send_announce_via_server')
return 6
from_domain_full = get_full_domain(from_domain, from_port)
to_url = 'https://www.w3.org/ns/activitystreams#Public'
actor_str = local_actor_url(http_prefix, from_nickname, from_domain_full)
cc_url = actor_str + '/followers'
status_number, published = get_status_number()
new_announce_id = actor_str + '/statuses/' + status_number
new_announce_json = {
"@context": [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'actor': actor_str,
'atomUri': new_announce_id,
'cc': [cc_url],
'id': new_announce_id + '/activity',
'object': repeat_object_url,
'published': published,
'to': [to_url],
'type': 'Announce'
}
handle = http_prefix + '://' + from_domain_full + '/@' + from_nickname
# lookup the inbox for the To handle
wf_request = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
from_domain, project_version, debug, False,
signing_priv_key_pem)
if not wf_request:
if debug:
print('DEBUG: announce webfinger failed for ' + handle)
return 1
if not isinstance(wf_request, dict):
print('WARN: announce webfinger for ' + handle +
' did not return a dict. ' + str(wf_request))
return 1
post_to_box = 'outbox'
# get the actor inbox for the To handle
origin_domain = from_domain
(inbox_url, _, _, from_person_id,
_, _, _, _) = get_person_box(signing_priv_key_pem,
origin_domain,
base_dir, session, wf_request,
person_cache,
project_version, http_prefix,
from_nickname, from_domain,
post_to_box, 73528,
system_language)
if not inbox_url:
if debug:
print('DEBUG: announce no ' + post_to_box +
' was found for ' + handle)
return 3
if not from_person_id:
if debug:
print('DEBUG: announce no actor was found for ' + handle)
return 4
auth_header = create_basic_auth_header(from_nickname, password)
headers = {
'host': from_domain,
'Content-type': 'application/json',
'Authorization': auth_header
}
post_result = post_json(http_prefix, from_domain_full,
session, new_announce_json, [], inbox_url,
headers, 3, True)
if not post_result:
print('WARN: announce not posted')
if debug:
print('DEBUG: c2s POST announce success')
return new_announce_json
def send_undo_announce_via_server(base_dir: str, session,
undo_post_json_object: {},
nickname: str, password: str,
domain: str, port: int, http_prefix: str,
cached_webfingers: {}, person_cache: {},
debug: bool, project_version: str,
signing_priv_key_pem: str,
system_language: str) -> {}:
"""Undo an announce message via c2s
"""
if not session:
print('WARN: No session for send_undo_announce_via_server')
return 6
domain_full = get_full_domain(domain, port)
actor = local_actor_url(http_prefix, nickname, domain_full)
handle = replace_users_with_at(actor)
status_number, _ = get_status_number()
unannounce_json = {
"@context": [
'https://www.w3.org/ns/activitystreams',
'https://w3id.org/security/v1'
],
'id': actor + '/statuses/' + str(status_number) + '/undo',
'type': 'Undo',
'actor': actor,
'object': undo_post_json_object['object']
}
# lookup the inbox for the To handle
wf_request = webfinger_handle(session, handle, http_prefix,
cached_webfingers,
domain, project_version, debug, False,
signing_priv_key_pem)
if not wf_request:
if debug:
print('DEBUG: undo announce webfinger failed for ' + handle)
return 1
if not isinstance(wf_request, dict):
print('WARN: undo announce webfinger for ' + handle +
' did not return a dict. ' + str(wf_request))
return 1
post_to_box = 'outbox'
# get the actor inbox for the To handle
origin_domain = domain
(inbox_url, _, _, from_person_id,
_, _, _, _) = get_person_box(signing_priv_key_pem,
origin_domain,
base_dir, session, wf_request,
person_cache,
project_version, http_prefix,
nickname, domain,
post_to_box, 73528,
system_language)
if not inbox_url:
if debug:
print('DEBUG: undo announce no ' + post_to_box +
' was found for ' + handle)
return 3
if not from_person_id:
if debug:
print('DEBUG: undo announce no actor was found for ' + handle)
return 4
auth_header = create_basic_auth_header(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': auth_header
}
post_result = post_json(http_prefix, domain_full,
session, unannounce_json, [], inbox_url,
headers, 3, True)
if not post_result:
print('WARN: undo announce not posted')
if debug:
print('DEBUG: c2s POST undo announce success')
return unannounce_json
def outbox_undo_announce(recent_posts_cache: {},
base_dir: str, nickname: str, domain: str,
message_json: {}, debug: bool) -> None:
""" When an undo announce is received by the outbox from c2s
"""
if not message_json.get('type'):
return
if not message_json['type'] == 'Undo':
return
if not has_object_string_type(message_json, debug):
return
if not message_json['object']['type'] == 'Announce':
if debug:
print('DEBUG: not a undo announce')
return
if not has_object_string_object(message_json, debug):
return
if debug:
print('DEBUG: c2s undo announce request arrived in outbox')
message_id = remove_id_ending(message_json['object']['object'])
domain = remove_domain_port(domain)
post_filename = locate_post(base_dir, nickname, domain, message_id)
if not post_filename:
if debug:
print('DEBUG: c2s undo announce post not found in inbox or outbox')
print(message_id)
return True
actor_url = get_actor_from_post(message_json)
undo_announce_collection_entry(recent_posts_cache, base_dir, post_filename,
actor_url, domain, debug)
if debug:
print('DEBUG: post undo announce via c2s - ' + post_filename)
def announce_seen(base_dir: str, nickname: str, domain: str,
message_json: {}) -> bool:
"""have the given announce been seen?
"""
if not message_json.get('id'):
return False
if not isinstance(message_json['id'], str):
return False
if not message_json.get('object'):
return False
if not isinstance(message_json['object'], str):
return False
# is this your own announce?
announce_id = remove_id_ending(message_json['id'])
if '://' + domain in announce_id and \
'/users/' + nickname + '/' in announce_id:
return False
post_url = remove_id_ending(message_json['object'])
post_filename = locate_post(base_dir, nickname, domain, post_url)
if not post_filename:
return False
seen_filename = post_filename + '.seen'
if not os.path.isfile(seen_filename):
return False
if text_in_file(announce_id, seen_filename):
return False
return True
def mark_announce_as_seen(base_dir: str, nickname: str, domain: str,
message_json: {}) -> None:
"""Marks the given announce post as seen
"""
if not message_json.get('id'):
return
if not isinstance(message_json['id'], str):
return
if not message_json.get('object'):
return
if not isinstance(message_json['object'], str):
return
post_url = remove_id_ending(message_json['object'])
post_filename = locate_post(base_dir, nickname, domain, post_url)
if not post_filename:
return
seen_filename = post_filename + '.seen'
if os.path.isfile(seen_filename):
return
announce_id = remove_id_ending(message_json['id'])
try:
with open(seen_filename, 'w+', encoding='utf-8') as fp_seen:
fp_seen.write(announce_id)
except OSError:
print('EX: mark_announce_as_seen unable to write ' + seen_filename)