rss.c 9.7 KB

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