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