diff options
Diffstat (limited to 'xs_webmention.h')
| -rw-r--r-- | xs_webmention.h | 42 |
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 @@ | |||
| 5 | #define _XS_WEBMENTION_H | 5 | #define _XS_WEBMENTION_H |
| 6 | 6 | ||
| 7 | int xs_webmention_send(const char *source, const char *target, const char *user_agent); | 7 | int xs_webmention_send(const char *source, const char *target, const char *user_agent); |
| 8 | int xs_webmention_hook(const char *source, const char *target, const char *user_agent); | ||
| 8 | 9 | ||
| 9 | 10 | ||
| 10 | #ifdef XS_IMPLEMENTATION | 11 | #ifdef XS_IMPLEMENTATION |
| @@ -118,6 +119,47 @@ int xs_webmention_send(const char *source, const char *target, const char *user_ | |||
| 118 | } | 119 | } |
| 119 | 120 | ||
| 120 | 121 | ||
| 122 | int xs_webmention_hook(const char *source, const char *target, const char *user_agent) | ||
| 123 | /* a Webmention has been received for a target that is ours; check if the source | ||
| 124 | really contains a link to our target */ | ||
| 125 | { | ||
| 126 | int status = 0; | ||
| 127 | |||
| 128 | xs *ua = xs_fmt("%s (Webmention)", user_agent ? user_agent : "xs_webmention"); | ||
| 129 | xs *headers = xs_dict_new(); | ||
| 130 | headers = xs_dict_set(headers, "accept", "text/html"); | ||
| 131 | headers = xs_dict_set(headers, "user-agent", ua); | ||
| 132 | |||
| 133 | xs *g_req = NULL; | ||
| 134 | xs *payload = NULL; | ||
| 135 | int p_size = 0; | ||
| 136 | |||
| 137 | g_req = xs_http_request("GET", source, headers, NULL, 0, &status, &payload, &p_size, 0); | ||
| 138 | |||
| 139 | if (status < 200 || status > 299) | ||
| 140 | return -1; | ||
| 141 | |||
| 142 | if (!xs_is_string(payload)) | ||
| 143 | return -2; | ||
| 144 | |||
| 145 | /* note: a "rogue" webmention can include a link to our target in commented-out HTML code */ | ||
| 146 | |||
| 147 | xs *links = xs_regex_select(payload, "<(a +|link +)[^>]+>"); | ||
| 148 | const char *link; | ||
| 149 | |||
| 150 | status = 0; | ||
| 151 | xs_list_foreach(links, link) { | ||
| 152 | /* if the link contains our target, it's valid */ | ||
| 153 | if (xs_str_in(link, target) != -1) { | ||
| 154 | status = 1; | ||
| 155 | break; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | return status; | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 121 | #endif /* XS_IMPLEMENTATION */ | 163 | #endif /* XS_IMPLEMENTATION */ |
| 122 | 164 | ||
| 123 | #endif /* _XS_WEBMENTION_H */ | 165 | #endif /* _XS_WEBMENTION_H */ |