aboutsummaryrefslogtreecommitdiff
path: root/xs_webmention.h
diff options
context:
space:
mode:
authorgrunfink <grunfink@comam.es>2025-06-24 14:06:41 +0200
committergrunfink <grunfink@comam.es>2025-06-24 14:06:41 +0200
commitf88bdd796045dd206b38e07adb13b5af4489b4c5 (patch)
treec7796a9d7a2ff7b7cfa40e3b9df3f09f999f57fa /xs_webmention.h
parent238c5c5f3918f4c64df2364b7c25eb82fd8f16d2 (diff)
Added a webmention hook.
Diffstat (limited to 'xs_webmention.h')
-rw-r--r--xs_webmention.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/xs_webmention.h b/xs_webmention.h
index 8415629..e177573 100644
--- a/xs_webmention.h
+++ b/xs_webmention.h
@@ -5,6 +5,7 @@
#define _XS_WEBMENTION_H
int xs_webmention_send(const char *source, const char *target, const char *user_agent);
+int xs_webmention_hook(const char *source, const char *target, const char *user_agent);
#ifdef XS_IMPLEMENTATION
@@ -118,6 +119,47 @@ int xs_webmention_send(const char *source, const char *target, const char *user_
}
+int xs_webmention_hook(const char *source, const char *target, const char *user_agent)
+/* a Webmention has been received for a target that is ours; check if the source
+ really contains a link to our target */
+{
+ int status = 0;
+
+ xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention");
+ xs *headers = xs_dict_new();
+ headers = xs_dict_set(headers, "accept", "text/html");
+ headers = xs_dict_set(headers, "user-agent", ua);
+
+ xs *g_req = NULL;
+ xs *payload = NULL;
+ int p_size = 0;
+
+ g_req = xs_http_request("GET", source, headers, NULL, 0, &status, &payload, &p_size, 0);
+
+ if (status < 200 || status > 299)
+ return -1;
+
+ if (!xs_is_string(payload))
+ return -2;
+
+ /* note: a "rogue" webmention can include a link to our target in commented-out HTML code */
+
+ xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>");
+ const char *link;
+
+ status = 0;
+ xs_list_foreach(links, link) {
+ /* if the link contains our target, it's valid */
+ if (xs_str_in(link, target) != -1) {
+ status = 1;
+ break;
+ }
+ }
+
+ return status;
+}
+
+
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_WEBMENTION_H */