summaryrefslogtreecommitdiff
path: root/activitypub.c
diff options
context:
space:
mode:
authorGravatar Oliver2025-05-21 21:11:47 +0200
committerGravatar Oliver2025-05-21 21:11:47 +0200
commit979718e3cc5489efdce2acc6f8c86f6d00bb91c7 (patch)
tree7b4936bdb5ea07c7dd617d508c487d70020f0146 /activitypub.c
parentpo/de_DE.po aktualisiert (diff)
parentMerge pull request 'make greetings theme adaptive; define style in header' (#... (diff)
downloadsnac2-979718e3cc5489efdce2acc6f8c86f6d00bb91c7.tar.gz
snac2-979718e3cc5489efdce2acc6f8c86f6d00bb91c7.tar.xz
snac2-979718e3cc5489efdce2acc6f8c86f6d00bb91c7.zip
Merge pull request 'master refresh' (#8) from grunfink/snac2:master into master
Reviewed-on: https://codeberg.org/zen/snac2/pulls/8
Diffstat (limited to 'activitypub.c')
-rw-r--r--activitypub.c115
1 files changed, 82 insertions, 33 deletions
diff --git a/activitypub.c b/activitypub.c
index c00c371..120b4a1 100644
--- a/activitypub.c
+++ b/activitypub.c
@@ -11,6 +11,7 @@
11#include "xs_set.h" 11#include "xs_set.h"
12#include "xs_match.h" 12#include "xs_match.h"
13#include "xs_unicode.h" 13#include "xs_unicode.h"
14#include "xs_webmention.h"
14 15
15#include "snac.h" 16#include "snac.h"
16 17
@@ -34,7 +35,7 @@ const char *susie_cool =
34 "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX" 35 "+ZcgN7wF7ZVihXkfSlWIVzIA6dbQzaygllpNuTX"
35 "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC"; 36 "ZmmFNlvxADX1+o0cUPMbAAAAAElFTkSuQmCC";
36 37
37const char *susie_muertos = 38const char *susie_muertos =
38 "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC" 39 "iVBORw0KGgoAAAANSUhEUgAAAEAAAABAAQAAAAC"
39 "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT" 40 "CEkxzAAAAV0lEQVQoz4XQsQ0AMQxCUW/A/lv+DT"
40 "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m" 41 "ic6zGRolekIMyMELNp8PiCEw6Q4w4NoAt53IH5m"
@@ -1044,17 +1045,21 @@ void notify(snac *snac, const char *type, const char *utype, const char *actor,
1044 1045
1045 xs *subject = xs_fmt("snac notify for @%s@%s", 1046 xs *subject = xs_fmt("snac notify for @%s@%s",
1046 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host")); 1047 xs_dict_get(snac->config, "uid"), xs_dict_get(srv_config, "host"));
1047 xs *from = xs_fmt("snac-daemon <snac-daemon@%s>", xs_dict_get(srv_config, "host")); 1048 xs *from = xs_fmt("<snac-daemon@%s>", xs_dict_get(srv_config, "host"));
1048 xs *header = xs_fmt( 1049 xs *header = xs_fmt(
1049 "From: %s\n" 1050 "From: snac-daemon %s\n"
1050 "To: %s\n" 1051 "To: %s\n"
1051 "Subject: %s\n" 1052 "Subject: %s\n"
1052 "\n", 1053 "\n",
1053 from, email, subject); 1054 from, email, subject);
1054 1055
1055 xs *email_body = xs_fmt("%s%s", header, body); 1056 xs *mailinfo = xs_dict_new();
1057 xs *bd = xs_fmt("%s%s", header, body);
1058 mailinfo = xs_dict_append(mailinfo, "from", from);
1059 mailinfo = xs_dict_append(mailinfo, "to", email);
1060 mailinfo = xs_dict_append(mailinfo, "body", bd);
1056 1061
1057 enqueue_email(email_body, 0); 1062 enqueue_email(mailinfo, 0);
1058 } 1063 }
1059 1064
1060 /* telegram */ 1065 /* telegram */
@@ -1327,6 +1332,10 @@ xs_dict *msg_actor(snac *snac)
1327 msg = xs_dict_set(msg, "preferredUsername", snac->uid); 1332 msg = xs_dict_set(msg, "preferredUsername", snac->uid);
1328 msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published")); 1333 msg = xs_dict_set(msg, "published", xs_dict_get(snac->config, "published"));
1329 1334
1335 // this exists so we get the emoji tags from our name too.
1336 // and then we just throw away the result, because it's kinda useless to have markdown in the display name.
1337 xs *name_dummy = not_really_markdown(xs_dict_get(snac->config, "name"), NULL, &tags);
1338
1330 xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags); 1339 xs *f_bio_2 = not_really_markdown(xs_dict_get(snac->config, "bio"), NULL, &tags);
1331 f_bio = process_tags(snac, f_bio_2, &tags); 1340 f_bio = process_tags(snac, f_bio_2, &tags);
1332 msg = xs_dict_set(msg, "summary", f_bio); 1341 msg = xs_dict_set(msg, "summary", f_bio);
@@ -2563,32 +2572,53 @@ int process_input_message(snac *snac, const xs_dict *msg, const xs_dict *req)
2563} 2572}
2564 2573
2565 2574
2566int send_email(const char *msg) 2575int send_email(const xs_dict *mailinfo)
2567/* invoke sendmail with email headers and body in msg */ 2576/* invoke curl */
2568{ 2577{
2569 FILE *f; 2578 const char *url = xs_dict_get(srv_config, "smtp_url");
2570 int status; 2579
2571 int fds[2]; 2580 if (!xs_is_string(url) || *url == '\0') {
2572 pid_t pid; 2581 /* revert back to old sendmail pipe behaviour */
2573 if (pipe(fds) == -1) return -1; 2582 const char *msg = xs_dict_get(mailinfo, "body");
2574 pid = vfork(); 2583 FILE *f;
2575 if (pid == -1) return -1; 2584 int status;
2576 else if (pid == 0) { 2585 int fds[2];
2577 dup2(fds[0], 0); 2586 pid_t pid;
2587
2588 if (pipe(fds) == -1) return -1;
2589 pid = vfork();
2590 if (pid == -1) return -1;
2591 else if (pid == 0) {
2592 dup2(fds[0], 0);
2593 close(fds[0]);
2594 close(fds[1]);
2595 execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL);
2596 _exit(1);
2597 }
2578 close(fds[0]); 2598 close(fds[0]);
2579 close(fds[1]); 2599 if ((f = fdopen(fds[1], "w")) == NULL) {
2580 execl("/usr/sbin/sendmail", "sendmail", "-t", (char *) NULL); 2600 close(fds[1]);
2581 _exit(1); 2601 return -1;
2582 } 2602 }
2583 close(fds[0]); 2603 fprintf(f, "%s\n", msg);
2584 if ((f = fdopen(fds[1], "w")) == NULL) { 2604 fclose(f);
2585 close(fds[1]); 2605 if (waitpid(pid, &status, 0) == -1) return -1;
2586 return -1; 2606 return status;
2587 } 2607 }
2588 fprintf(f, "%s\n", msg); 2608
2589 fclose(f); 2609 const char
2590 if (waitpid(pid, &status, 0) == -1) return -1; 2610 *user = xs_dict_get(srv_config, "smtp_username"),
2591 return status; 2611 *pass = xs_dict_get(srv_config, "smtp_password"),
2612 *from = xs_dict_get(mailinfo, "from"),
2613 *to = xs_dict_get(mailinfo, "to"),
2614 *body = xs_dict_get(mailinfo, "body");
2615
2616 if (url == NULL || *url == '\0')
2617 url = "smtp://localhost";
2618
2619 int smtp_port = parse_port(url, NULL);
2620
2621 return xs_smtp_request(url, user, pass, from, to, body, smtp_port == 465 || smtp_port == 587);
2592} 2622}
2593 2623
2594 2624
@@ -2759,6 +2789,8 @@ int process_user_queue(snac *snac)
2759 cnt++; 2789 cnt++;
2760 } 2790 }
2761 2791
2792 scheduled_process(snac);
2793
2762 return cnt; 2794 return cnt;
2763} 2795}
2764 2796
@@ -2859,7 +2891,7 @@ void process_queue_item(xs_dict *q_item)
2859 else 2891 else
2860 if (strcmp(type, "email") == 0) { 2892 if (strcmp(type, "email") == 0) {
2861 /* send this email */ 2893 /* send this email */
2862 const xs_str *msg = xs_dict_get(q_item, "message"); 2894 const xs_dict *msg = xs_dict_get(q_item, "message");
2863 int retries = xs_number_get(xs_dict_get(q_item, "retries")); 2895 int retries = xs_number_get(xs_dict_get(q_item, "retries"));
2864 2896
2865 if (!send_email(msg)) 2897 if (!send_email(msg))
@@ -2999,6 +3031,25 @@ void process_queue_item(xs_dict *q_item)
2999 } 3031 }
3000 } 3032 }
3001 else 3033 else
3034 if (strcmp(type, "webmention") == 0) {
3035 const xs_dict *msg = xs_dict_get(q_item, "message");
3036 const char *source = xs_dict_get(msg, "id");
3037 const char *content = xs_dict_get(msg, "content");
3038
3039 if (xs_is_string(source) && xs_is_string(content)) {
3040 xs *links = xs_regex_select(content, "\"https?:/" "/[^\"]+");
3041 const char *link;
3042
3043 xs_list_foreach(links, link) {
3044 xs *target = xs_strip_chars_i(xs_dup(link), "\"");
3045
3046 int r = xs_webmention_send(source, target, USER_AGENT);
3047
3048 srv_debug(1, xs_fmt("webmention source=%s target=%s %d", source, target, r));
3049 }
3050 }
3051 }
3052 else
3002 srv_log(xs_fmt("unexpected q_item type '%s'", type)); 3053 srv_log(xs_fmt("unexpected q_item type '%s'", type));
3003} 3054}
3004 3055
@@ -3153,8 +3204,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
3153 int total = 0; 3204 int total = 0;
3154 3205
3155 if (show_contact_metrics) { 3206 if (show_contact_metrics) {
3156 xs *l = follower_list(&snac); 3207 total = follower_list_len(&snac);
3157 total = xs_list_len(l);
3158 } 3208 }
3159 3209
3160 xs *id = xs_fmt("%s/%s", snac.actor, p_path); 3210 xs *id = xs_fmt("%s/%s", snac.actor, p_path);
@@ -3165,8 +3215,7 @@ int activitypub_get_handler(const xs_dict *req, const char *q_path,
3165 int total = 0; 3215 int total = 0;
3166 3216
3167 if (show_contact_metrics) { 3217 if (show_contact_metrics) {
3168 xs *l = following_list(&snac); 3218 total = following_list_len(&snac);
3169 total = xs_list_len(l);
3170 } 3219 }
3171 3220
3172 xs *id = xs_fmt("%s/%s", snac.actor, p_path); 3221 xs *id = xs_fmt("%s/%s", snac.actor, p_path);