2.93 rss.c
  1/* snac - A simple, minimalistic ActivityPub instance */
  2/* copyright (c) 2025 - 2026 grunfink et al. / MIT license */
  3
  4#include "xs.h"
  5#include "xs_html.h"
  6#include "xs_regex.h"
  7#include "xs_time.h"
  8#include "xs_match.h"
  9#include "xs_curl.h"
 10#include "xs_openssl.h"
 11#include "xs_json.h"
 12#include "xs_http.h"
 13#include "xs_unicode.h"
 14
 15#include "snac.h"
 16
 17xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
 18                        const char *title, const char *link, const char *desc)
 19/* converts a timeline to rss */
 20{
 21    xs_html *rss = xs_html_tag("rss",
 22        xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
 23        xs_html_attr("version",       "2.0"),
 24        xs_html_attr("xmlns:atom",    "http:/" "/www.w3.org/2005/Atom"));
 25
 26    xs_html *channel = xs_html_tag("channel",
 27        xs_html_tag("title",
 28            xs_html_text(title)),
 29        xs_html_tag("language",
 30            xs_html_text("en")),
 31        xs_html_tag("link",
 32            xs_html_text(link)),
 33        xs_html_sctag("atom:link",
 34            xs_html_attr("href", link),
 35            xs_html_attr("rel", "self"),
 36            xs_html_attr("type", "application/rss+xml")),
 37        xs_html_tag("generator",
 38            xs_html_text(USER_AGENT)),
 39        xs_html_tag("description",
 40            xs_html_text(desc)));
 41
 42    xs_html_add(rss, channel);
 43
 44    int cnt = 0;
 45    const char *v;
 46
 47    int show_unlisted = user ? xs_is_true(xs_dict_get(user->config, "show_unlisted")) : 0;
 48
 49    xs_list_foreach(timeline, v) {
 50        xs *msg = NULL;
 51
 52        if (user) {
 53            if (!valid_status(timeline_get_by_md5(user, v, &msg)))
 54                continue;
 55        }
 56        else {
 57            if (!valid_status(object_get_by_md5(v, &msg)))
 58                continue;
 59        }
 60
 61        const char *id = xs_dict_get(msg, "id");
 62        const char *content = xs_dict_get(msg, "content");
 63        const char *published = xs_dict_get(msg, "published");
 64
 65        if (user && !is_msg_mine(user, id))
 66            continue;
 67
 68        if (!id || !content || !published)
 69            continue;
 70
 71        int scope = get_msg_visibility(msg);
 72
 73        if (scope == SCOPE_MENTIONED || scope == SCOPE_FOLLOWERS)
 74            continue;
 75
 76        if (scope == SCOPE_UNLISTED && !show_unlisted)
 77            continue;
 78
 79        /* create a title with the first line of the content */
 80        xs *title = xs_replace(content, "<br>", "\n");
 81        title = xs_regex_replace_i(title, "<[^>]+>", " ");
 82        title = xs_regex_replace_i(title, "&[^;]+;", " ");
 83        int i;
 84
 85        for (i = 0; title[i] && title[i] != '\n' && i < 50; ) {
 86            const char *p = &title[i];
 87            unsigned int cp = xs_utf8_dec(&p);
 88            int n = p - title;
 89            if (cp == 0xfffd || n > 50)
 90                break;
 91            i = n;
 92        }
 93
 94        if (title[i] != '\0') {
 95            title[i] = '\0';
 96            title = xs_str_cat(title, "...");
 97        }
 98
 99        title = xs_strip_i(title);
100
101        /* convert the date */
102        time_t t = xs_parse_iso_date(published, 0);
103        xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
104
105        /* if it's the first one, add it to the header */
106        if (cnt == 0)
107            xs_html_add(channel,
108                xs_html_tag("lastBuildDate",
109                    xs_html_text(rss_date)));
110
111        xs *att_content = xs_dup(content);
112        xs *attach_list = get_attachments(msg);
113
114        /* add possible attachments */
115        if (xs_list_len(attach_list)) {
116            const xs_val *a;
117            xs_html *content_attachments = xs_html_tag("div",
118                xs_html_attr("class", "snac-content-attachments"));
119
120            xs_list_foreach(attach_list, a) {
121                const char *type = xs_dict_get(a, "type");
122                const char *o_href = xs_dict_get(a, "href");
123                const char *name = xs_dict_get_def(a, "name", "");
124
125                if (!xs_is_string(type) || !xs_is_string(o_href))
126                    continue;
127
128                if (xs_startswith(type, "image/")) {
129                    xs_html_add(content_attachments,
130                        xs_html_tag("figure",
131                            xs_html_sctag("img",
132                                xs_html_attr("src", o_href)),
133                            xs_html_tag("figcaption",
134                                xs_html_text(name))));
135                }
136                else {
137                    xs_html_add(content_attachments,
138                        xs_html_tag("a",
139                            xs_html_attr("href", o_href),
140                            xs_html_text(name),
141                            xs_html_text(" "),
142                            xs_html_text(o_href),
143                        xs_html_sctag("br", NULL)));
144                }
145            }
146
147            xs *s1 = xs_html_render(content_attachments);
148            att_content = xs_str_cat(att_content, s1);
149        }
150
151        xs_html_add(channel,
152            xs_html_tag("item",
153                xs_html_tag("title",
154                    xs_html_text(title)),
155                xs_html_tag("link",
156                    xs_html_text(id)),
157                xs_html_tag("guid",
158                    xs_html_text(id)),
159                xs_html_tag("pubDate",
160                    xs_html_text(rss_date)),
161                xs_html_tag("description",
162                    xs_html_text(att_content))));
163
164        cnt++;
165    }
166
167    return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
168}
169
170
171void rss_to_timeline(snac *user, const char *url)
172/* reads an RSS and inserts all ActivityPub posts into the user's timeline */
173{
174    if (!xs_startswith(url, "https:/") && !xs_startswith(url, "http:/"))
175        return;
176
177    xs *hdrs = xs_dict_new();
178    hdrs = xs_dict_set(hdrs, "accept",     "application/rss+xml");
179    hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
180
181    /* get the RSS metadata */
182    xs *md5 = xs_md5_hex(url, strlen(url));
183    xs *rss_md_fn = xs_fmt("%s/rss", user->basedir);
184    mkdirx(rss_md_fn);
185    rss_md_fn = xs_str_cat(rss_md_fn, "/", md5, ".json");
186
187    xs *rss_md = NULL;
188    const char *etag = NULL;
189
190    FILE *f;
191    if ((f = fopen(rss_md_fn, "r")) != NULL) {
192        rss_md = xs_json_load(f);
193        fclose(f);
194
195        etag = xs_dict_get(rss_md, "etag");
196
197        if (xs_is_string(etag))
198            hdrs = xs_dict_set(hdrs, "if-none-match", etag);
199    }
200
201    if (rss_md == NULL)
202        rss_md = xs_dict_new();
203
204    xs *payload = NULL;
205    int status;
206    int p_size;
207
208    xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0);
209
210    snac_log(user, xs_fmt("parsing RSS %s %d", url, status));
211
212    if (!valid_status(status) || !xs_is_string(payload))
213        return;
214
215    /* not an RSS? done */
216    const char *ctype = xs_dict_get(rsp, "content-type");
217    if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1)
218        return;
219
220    /* yes, parsing is done with regexes (now I have two problems blah blah blah) */
221    xs *links = xs_regex_select(payload, "<link>[^<]+</link>");
222    const char *link;
223
224    xs_list_foreach(links, link) {
225        xs *l = xs_replace(link, "<link>", "");
226        char *p = strchr(l, '<');
227
228        if (p == NULL)
229            continue;
230        *p = '\0';
231
232        /* skip this same URL */
233        if (strcmp(l, url) == 0)
234            continue;
235
236        /* skip crap */
237        if (!xs_startswith(l, "https:/") && !xs_startswith(l, "http:/"))
238            continue;
239
240        snac_debug(user, 1, xs_fmt("RSS link: %s", l));
241
242        if (timeline_here(user, l)) {
243            snac_debug(user, 1, xs_fmt("RSS entry already in timeline %s", l));
244            continue;
245        }
246
247        /* special trick for Mastodon: convert from the alternate format */
248        if (strchr(l, '@') != NULL) {
249            xs *l2 = xs_split(l, "/");
250
251            if (xs_list_len(l2) == 5) {
252                const char *uid = xs_list_get(l2, 3);
253                if (*uid == '@') {
254                    xs *guessed_id = xs_fmt("https:/" "/%s/users/%s/statuses/%s",
255                        xs_list_get(l2, 2), uid + 1, xs_list_get(l2, -1));
256
257                    if (timeline_here(user, guessed_id)) {
258                        snac_debug(user, 1, xs_fmt("RSS entry already in timeline (alt) %s", guessed_id));
259                        continue;
260                    }
261                }
262            }
263        }
264
265        xs *obj = NULL;
266
267        if (!valid_status(object_get(l, &obj))) {
268            /* object is not here: bring it */
269            if (!valid_status(activitypub_request(user, l, &obj)))
270                continue;
271        }
272
273        if (xs_is_dict(obj)) {
274            const char *id      = xs_dict_get(obj, "id");
275            const char *type    = xs_dict_get(obj, "type");
276            const char *attr_to = get_atto(obj);
277
278            if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
279                continue;
280
281            if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
282                continue;
283
284            /* the instance is [back] online */
285            instance_failure(id, 2);
286
287            if (timeline_here(user, id)) {
288                snac_debug(user, 1, xs_fmt("RSS entry already in timeline (id) %s", id));
289                continue;
290            }
291
292            enqueue_actor_refresh(user, attr_to, 0);
293
294            timeline_add(user, id, obj);
295
296            snac_log(user, xs_fmt("new '%s' (RSS) %s %s", type, attr_to, id));
297        }
298    }
299
300    /* update the RSS metadata */
301    etag = xs_dict_get(rsp, "etag");
302
303    if (xs_is_string(etag)) {
304        rss_md = xs_dict_set(rss_md, "etag", etag);
305        rss_md = xs_dict_set(rss_md, "url", url);
306        if ((f = fopen(rss_md_fn, "w")) != NULL) {
307            xs_json_dump(rss_md, 4, f);
308            fclose(f);
309        }
310    }
311}
312
313
314void rss_poll_hashtags(void)
315/* parses all RSS from all users */
316{
317    xs *list = user_list();
318    const char *uid;
319
320    xs_list_foreach(list, uid) {
321        snac user;
322
323        if (user_open(&user, uid)) {
324            const xs_list *rss = xs_dict_get(user.config, "followed_hashtags");
325
326            if (xs_is_list(rss)) {
327                const char *url;
328
329                xs_list_foreach(rss, url)
330                    rss_to_timeline(&user, url);
331            }
332
333            user_free(&user);
334        }
335    }
336}