commit b783f28
grunfink
·
2025-05-28 05:56:44 +0000 UTC
parent 97ff661
New function rss_to_timeline().
M
rss.c
+124,
-0
1@@ -5,6 +5,8 @@
2 #include "xs_html.h"
3 #include "xs_regex.h"
4 #include "xs_time.h"
5+#include "xs_match.h"
6+#include "xs_curl.h"
7
8 #include "snac.h"
9
10@@ -103,3 +105,125 @@ xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
11
12 return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
13 }
14+
15+
16+void rss_to_timeline(snac *user, const char *url)
17+/* reads an RSS and inserts all ActivityPub posts into the user's timeline */
18+{
19+ xs *hdrs = xs_dict_new();
20+ hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml");
21+ hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
22+
23+ xs *payload = NULL;
24+ int status;
25+ int p_size;
26+
27+ xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0);
28+
29+ if (!valid_status(status) || !xs_is_string(payload))
30+ return;
31+
32+ /* not an RSS? done */
33+ const char *ctype = xs_dict_get(rsp, "content-type");
34+ if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1)
35+ return;
36+
37+ snac_log(user, xs_fmt("parsing RSS %s", url));
38+
39+ /* yes, parsing is done with regexes (now I have two problems blah blah blah) */
40+ xs *links = xs_regex_select(payload, "<link>[^<]+</link>");
41+ const char *link;
42+
43+ xs_list_foreach(links, link) {
44+ xs *l = xs_replace(link, "<link>", "");
45+ char *p = strchr(l, '<');
46+
47+ if (p == NULL)
48+ continue;
49+ *p = '\0';
50+
51+ /* skip this same URL */
52+ if (strcmp(l, url) == 0)
53+ continue;
54+
55+ snac_debug(user, 1, xs_fmt("RSS link: %s", l));
56+
57+ if (timeline_here(user, l)) {
58+ snac_debug(user, 1, xs_fmt("RSS entry already in timeline %s", l));
59+ continue;
60+ }
61+
62+ /* special trick for Mastodon: convert from the alternate format */
63+ if (strchr(l, '@') != NULL) {
64+ xs *l2 = xs_split(l, "/");
65+
66+ if (xs_list_len(l2) == 5) {
67+ const char *uid = xs_list_get(l2, 3);
68+ if (*uid == '@') {
69+ xs *guessed_id = xs_fmt("https:/" "/%s/users/%s/statuses/%s",
70+ xs_list_get(l2, 2), uid + 1, xs_list_get(l2, -1));
71+
72+ if (timeline_here(user, guessed_id)) {
73+ snac_debug(user, 1, xs_fmt("RSS entry already in timeline (alt) %s", guessed_id));
74+ continue;
75+ }
76+ }
77+ }
78+ }
79+
80+ xs *obj = NULL;
81+
82+ if (!valid_status(object_get(l, &obj))) {
83+ /* object is not here: bring it */
84+ if (!valid_status(activitypub_request(user, l, &obj)))
85+ continue;
86+ }
87+
88+ if (xs_is_dict(obj)) {
89+ const char *id = xs_dict_get(obj, "id");
90+ const char *type = xs_dict_get(obj, "type");
91+ const char *attr_to = get_atto(obj);
92+
93+ if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
94+ continue;
95+
96+ if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
97+ continue;
98+
99+ if (timeline_here(user, id)) {
100+ snac_debug(user, 1, xs_fmt("RSS entry already in timeline (id) %s", id));
101+ continue;
102+ }
103+
104+ if (!valid_status(actor_request(user, attr_to, NULL)))
105+ continue;
106+
107+ timeline_add(user, id, obj);
108+ }
109+ }
110+}
111+
112+
113+void rss_process(void)
114+/* parses all RSS from all users */
115+{
116+ xs *list = user_list();
117+ const char *uid;
118+
119+ xs_list_foreach(list, uid) {
120+ snac user;
121+
122+ if (user_open(&user, uid)) {
123+ const xs_list *rss = xs_dict_get(user.config, "rss");
124+
125+ if (xs_is_list(rss)) {
126+ const char *url;
127+
128+ xs_list_foreach(rss, url)
129+ rss_to_timeline(&user, url);
130+ }
131+
132+ user_free(&user);
133+ }
134+ }
135+}
M
snac.h
+2,
-0
1@@ -463,3 +463,5 @@ const char *lang_str(const char *str, const snac *user);
2
3 xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
4 const char *title, const char *link, const char *desc);
5+void rss_to_timeline(snac *user, const char *url);
6+void rss_process(void);