diff options
| author | 2025-06-24 14:06:41 +0200 | |
|---|---|---|
| committer | 2025-06-24 14:06:41 +0200 | |
| commit | f88bdd796045dd206b38e07adb13b5af4489b4c5 (patch) | |
| tree | c7796a9d7a2ff7b7cfa40e3b9df3f09f999f57fa /httpd.c | |
| parent | Some JSON code tweaks. (diff) | |
| download | snac2-f88bdd796045dd206b38e07adb13b5af4489b4c5.tar.gz snac2-f88bdd796045dd206b38e07adb13b5af4489b4c5.tar.xz snac2-f88bdd796045dd206b38e07adb13b5af4489b4c5.zip | |
Added a webmention hook.
Diffstat (limited to 'httpd.c')
| -rw-r--r-- | httpd.c | 62 |
1 files changed, 62 insertions, 0 deletions
| @@ -12,6 +12,7 @@ | |||
| 12 | #include "xs_openssl.h" | 12 | #include "xs_openssl.h" |
| 13 | #include "xs_fcgi.h" | 13 | #include "xs_fcgi.h" |
| 14 | #include "xs_html.h" | 14 | #include "xs_html.h" |
| 15 | #include "xs_webmention.h" | ||
| 15 | 16 | ||
| 16 | #include "snac.h" | 17 | #include "snac.h" |
| 17 | 18 | ||
| @@ -373,6 +374,63 @@ int server_get_handler(xs_dict *req, const char *q_path, | |||
| 373 | } | 374 | } |
| 374 | 375 | ||
| 375 | 376 | ||
| 377 | int server_post_handler(const xs_dict *req, const char *q_path, | ||
| 378 | char *payload, int p_size, | ||
| 379 | char **body, int *b_size, char **ctype) | ||
| 380 | { | ||
| 381 | int status = 0; | ||
| 382 | |||
| 383 | if (strcmp(q_path, "/webmention-hook") == 0) { | ||
| 384 | status = HTTP_STATUS_BAD_REQUEST; | ||
| 385 | |||
| 386 | const xs_dict *p_vars = xs_dict_get(req, "p_vars"); | ||
| 387 | |||
| 388 | if (!xs_is_dict(p_vars)) | ||
| 389 | return status; | ||
| 390 | |||
| 391 | const char *source = xs_dict_get(p_vars, "source"); | ||
| 392 | const char *target = xs_dict_get(p_vars, "target"); | ||
| 393 | |||
| 394 | if (!xs_is_string(source) || !xs_is_string(target)) { | ||
| 395 | srv_debug(1, xs_fmt("webmention-hook bad source or target")); | ||
| 396 | return status; | ||
| 397 | } | ||
| 398 | |||
| 399 | if (!xs_startswith(target, srv_baseurl)) { | ||
| 400 | srv_debug(1, xs_fmt("webmention-hook unknown target %s", target)); | ||
| 401 | return status; | ||
| 402 | } | ||
| 403 | |||
| 404 | if (!object_here(target)) { | ||
| 405 | srv_debug(0, xs_fmt("webmention-hook target %s not / no longer here", target)); | ||
| 406 | return status; | ||
| 407 | } | ||
| 408 | |||
| 409 | /* get the user */ | ||
| 410 | xs *s1 = xs_replace(target, srv_baseurl, ""); | ||
| 411 | |||
| 412 | xs *l1 = xs_split(s1, "/"); | ||
| 413 | const char *uid = xs_list_get(l1, 1); | ||
| 414 | snac user; | ||
| 415 | |||
| 416 | if (!xs_is_string(uid) || !user_open(&user, uid)) | ||
| 417 | return status; | ||
| 418 | |||
| 419 | int r = xs_webmention_hook(source, target, USER_AGENT); | ||
| 420 | |||
| 421 | if (r > 0) | ||
| 422 | notify_add(&user, "Webmention", NULL, source, target, xs_stock(XSTYPE_DICT)); | ||
| 423 | |||
| 424 | srv_log(xs_fmt("webmention-hook source=%s target=%s %d", source, target, r)); | ||
| 425 | |||
| 426 | user_free(&user); | ||
| 427 | status = HTTP_STATUS_OK; | ||
| 428 | } | ||
| 429 | |||
| 430 | return status; | ||
| 431 | } | ||
| 432 | |||
| 433 | |||
| 376 | void httpd_connection(FILE *f) | 434 | void httpd_connection(FILE *f) |
| 377 | /* the connection processor */ | 435 | /* the connection processor */ |
| 378 | { | 436 | { |
| @@ -444,6 +502,10 @@ void httpd_connection(FILE *f) | |||
| 444 | else | 502 | else |
| 445 | if (strcmp(method, "POST") == 0) { | 503 | if (strcmp(method, "POST") == 0) { |
| 446 | 504 | ||
| 505 | if (status == 0) | ||
| 506 | status = server_post_handler(req, q_path, | ||
| 507 | payload, p_size, &body, &b_size, &ctype); | ||
| 508 | |||
| 447 | #ifndef NO_MASTODON_API | 509 | #ifndef NO_MASTODON_API |
| 448 | if (status == 0) | 510 | if (status == 0) |
| 449 | status = oauth_post_handler(req, q_path, | 511 | status = oauth_post_handler(req, q_path, |