rss.c 10 KB

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