diff options
Diffstat (limited to 'activitypub.c')
| -rw-r--r-- | activitypub.c | 151 |
1 files changed, 89 insertions, 62 deletions
diff --git a/activitypub.c b/activitypub.c index 98a15bf..d54e5ba 100644 --- a/activitypub.c +++ b/activitypub.c | |||
| @@ -73,13 +73,14 @@ int actor_request(snac *snac, char *actor, d_char **data) | |||
| 73 | } | 73 | } |
| 74 | 74 | ||
| 75 | 75 | ||
| 76 | void timeline_request(snac *snac, char *id, char *referrer) | 76 | int timeline_request(snac *snac, char *id, char *referrer) |
| 77 | /* ensures that an entry and its ancestors are in the timeline */ | 77 | /* ensures that an entry and its ancestors are in the timeline */ |
| 78 | { | 78 | { |
| 79 | int status = 0; | ||
| 80 | |||
| 79 | if (!xs_is_null(id)) { | 81 | if (!xs_is_null(id)) { |
| 80 | /* is the admired object already there? */ | 82 | /* is the admired object already there? */ |
| 81 | if (!timeline_here(snac, id)) { | 83 | if (!timeline_here(snac, id)) { |
| 82 | int status; | ||
| 83 | xs *object = NULL; | 84 | xs *object = NULL; |
| 84 | 85 | ||
| 85 | /* no; download it */ | 86 | /* no; download it */ |
| @@ -97,6 +98,8 @@ void timeline_request(snac *snac, char *id, char *referrer) | |||
| 97 | } | 98 | } |
| 98 | } | 99 | } |
| 99 | } | 100 | } |
| 101 | |||
| 102 | return status; | ||
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | 105 | ||
| @@ -140,6 +143,65 @@ int send_to_actor(snac *snac, char *actor, char *msg, d_char **payload, int *p_s | |||
| 140 | } | 143 | } |
| 141 | 144 | ||
| 142 | 145 | ||
| 146 | d_char *recipient_list(snac *snac, char *msg, int expand_public) | ||
| 147 | /* returns the list of recipients for a message */ | ||
| 148 | { | ||
| 149 | d_char *list = xs_list_new(); | ||
| 150 | char *to = xs_dict_get(msg, "to"); | ||
| 151 | char *cc = xs_dict_get(msg, "cc"); | ||
| 152 | int n; | ||
| 153 | |||
| 154 | char *lists[] = { to, cc, NULL }; | ||
| 155 | for (n = 0; lists[n]; n++) { | ||
| 156 | char *l = lists[n]; | ||
| 157 | char *v; | ||
| 158 | |||
| 159 | if (xs_type(l) == XSTYPE_STRING) { | ||
| 160 | if (xs_list_in(list, l) == -1) | ||
| 161 | list = xs_list_append(list, l); | ||
| 162 | } | ||
| 163 | else | ||
| 164 | while (xs_list_iter(&l, &v)) { | ||
| 165 | if (expand_public && strcmp(v, public_address) == 0) { | ||
| 166 | /* iterate the followers and add them */ | ||
| 167 | xs *fwers = follower_list(snac); | ||
| 168 | char *fw; | ||
| 169 | |||
| 170 | char *p = fwers; | ||
| 171 | while (xs_list_iter(&p, &fw)) { | ||
| 172 | char *actor = xs_dict_get(fw, "actor"); | ||
| 173 | |||
| 174 | if (xs_list_in(list, actor) == -1) | ||
| 175 | list = xs_list_append(list, actor); | ||
| 176 | } | ||
| 177 | } | ||
| 178 | else | ||
| 179 | if (xs_list_in(list, v) == -1) | ||
| 180 | list = xs_list_append(list, v); | ||
| 181 | } | ||
| 182 | } | ||
| 183 | |||
| 184 | return list; | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | int is_msg_public(snac *snac, char *msg) | ||
| 189 | /* checks if a message is public */ | ||
| 190 | { | ||
| 191 | int ret = 0; | ||
| 192 | xs *rcpts = recipient_list(snac, msg, 0); | ||
| 193 | char *p, *v; | ||
| 194 | |||
| 195 | p = rcpts; | ||
| 196 | while (!ret && xs_list_iter(&p, &v)) { | ||
| 197 | if (strcmp(v, public_address) == 0) | ||
| 198 | ret = 1; | ||
| 199 | } | ||
| 200 | |||
| 201 | return ret; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 143 | /** messages **/ | 205 | /** messages **/ |
| 144 | 206 | ||
| 145 | d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object) | 207 | d_char *msg_base(snac *snac, char *type, char *id, char *actor, char *date, char *object) |
| @@ -323,7 +385,7 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) | |||
| 323 | { | 385 | { |
| 324 | xs *ntid = tid(0); | 386 | xs *ntid = tid(0); |
| 325 | xs *id = xs_fmt("%s/p/%s", snac->actor, ntid); | 387 | xs *id = xs_fmt("%s/p/%s", snac->actor, ntid); |
| 326 | xs *ctxt = xs_fmt("%s#ctxt", id); | 388 | xs *ctxt = NULL; |
| 327 | xs *fc1 = NULL; | 389 | xs *fc1 = NULL; |
| 328 | xs *to = NULL; | 390 | xs *to = NULL; |
| 329 | xs *cc = xs_list_new(); | 391 | xs *cc = xs_list_new(); |
| @@ -341,6 +403,27 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) | |||
| 341 | not_really_markdown(content, &fc1); | 403 | not_really_markdown(content, &fc1); |
| 342 | 404 | ||
| 343 | if (in_reply_to != NULL) { | 405 | if (in_reply_to != NULL) { |
| 406 | xs *p_msg = NULL; | ||
| 407 | |||
| 408 | /* demand this thing */ | ||
| 409 | timeline_request(snac, in_reply_to, NULL); | ||
| 410 | |||
| 411 | if ((p_msg = timeline_find(snac, in_reply_to)) != NULL) { | ||
| 412 | /* add this author as recipient */ | ||
| 413 | char *v; | ||
| 414 | |||
| 415 | if ((v = xs_dict_get(p_msg, "attributedTo")) && xs_list_in(to, v) == -1) | ||
| 416 | to = xs_list_append(to, v); | ||
| 417 | |||
| 418 | if ((v = xs_dict_get(p_msg, "context"))) | ||
| 419 | ctxt = xs_dup(v); | ||
| 420 | |||
| 421 | /* if this message is public, ours will also be */ | ||
| 422 | if (is_msg_public(snac, p_msg) && | ||
| 423 | xs_list_in(to, (char *)public_address) == -1) | ||
| 424 | to = xs_list_append(to, public_address); | ||
| 425 | } | ||
| 426 | |||
| 344 | irt = xs_dup(in_reply_to); | 427 | irt = xs_dup(in_reply_to); |
| 345 | } | 428 | } |
| 346 | else | 429 | else |
| @@ -349,6 +432,9 @@ d_char *msg_note(snac *snac, char *content, char *rcpts, char *in_reply_to) | |||
| 349 | if (tag == NULL) | 432 | if (tag == NULL) |
| 350 | tag = xs_list_new(); | 433 | tag = xs_list_new(); |
| 351 | 434 | ||
| 435 | if (ctxt == NULL) | ||
| 436 | ctxt = xs_fmt("%s#ctxt", id); | ||
| 437 | |||
| 352 | /* add all mentions to the cc */ | 438 | /* add all mentions to the cc */ |
| 353 | p = tag; | 439 | p = tag; |
| 354 | while (xs_list_iter(&p, &v)) { | 440 | while (xs_list_iter(&p, &v)) { |
| @@ -526,65 +612,6 @@ void process_queue(snac *snac) | |||
| 526 | } | 612 | } |
| 527 | 613 | ||
| 528 | 614 | ||
| 529 | d_char *recipient_list(snac *snac, char *msg, int expand_public) | ||
| 530 | /* returns the list of recipients for a message */ | ||
| 531 | { | ||
| 532 | d_char *list = xs_list_new(); | ||
| 533 | char *to = xs_dict_get(msg, "to"); | ||
| 534 | char *cc = xs_dict_get(msg, "cc"); | ||
| 535 | int n; | ||
| 536 | |||
| 537 | char *lists[] = { to, cc, NULL }; | ||
| 538 | for (n = 0; lists[n]; n++) { | ||
| 539 | char *l = lists[n]; | ||
| 540 | char *v; | ||
| 541 | |||
| 542 | if (xs_type(l) == XSTYPE_STRING) { | ||
| 543 | if (xs_list_in(list, l) == -1) | ||
| 544 | list = xs_list_append(list, l); | ||
| 545 | } | ||
| 546 | else | ||
| 547 | while (xs_list_iter(&l, &v)) { | ||
| 548 | if (expand_public && strcmp(v, public_address) == 0) { | ||
| 549 | /* iterate the followers and add them */ | ||
| 550 | xs *fwers = follower_list(snac); | ||
| 551 | char *fw; | ||
| 552 | |||
| 553 | char *p = fwers; | ||
| 554 | while (xs_list_iter(&p, &fw)) { | ||
| 555 | char *actor = xs_dict_get(fw, "actor"); | ||
| 556 | |||
| 557 | if (xs_list_in(list, actor) == -1) | ||
| 558 | list = xs_list_append(list, actor); | ||
| 559 | } | ||
| 560 | } | ||
| 561 | else | ||
| 562 | if (xs_list_in(list, v) == -1) | ||
| 563 | list = xs_list_append(list, v); | ||
| 564 | } | ||
| 565 | } | ||
| 566 | |||
| 567 | return list; | ||
| 568 | } | ||
| 569 | |||
| 570 | |||
| 571 | int is_msg_public(snac *snac, char *msg) | ||
| 572 | /* checks if a message is public */ | ||
| 573 | { | ||
| 574 | int ret = 0; | ||
| 575 | xs *rcpts = recipient_list(snac, msg, 0); | ||
| 576 | char *p, *v; | ||
| 577 | |||
| 578 | p = rcpts; | ||
| 579 | while (!ret && xs_list_iter(&p, &v)) { | ||
| 580 | if (strcmp(v, public_address) == 0) | ||
| 581 | ret = 1; | ||
| 582 | } | ||
| 583 | |||
| 584 | return ret; | ||
| 585 | } | ||
| 586 | |||
| 587 | |||
| 588 | void post(snac *snac, char *msg) | 615 | void post(snac *snac, char *msg) |
| 589 | /* enqueues a message to all its recipients */ | 616 | /* enqueues a message to all its recipients */ |
| 590 | { | 617 | { |