rss.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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_html_add(channel,
  86. xs_html_tag("item",
  87. xs_html_tag("title",
  88. xs_html_text(title)),
  89. xs_html_tag("link",
  90. xs_html_text(id)),
  91. xs_html_tag("guid",
  92. xs_html_text(id)),
  93. xs_html_tag("pubDate",
  94. xs_html_text(rss_date)),
  95. xs_html_tag("description",
  96. xs_html_text(content))));
  97. cnt++;
  98. }
  99. return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  100. }
  101. void rss_to_timeline(snac *user, const char *url)
  102. /* reads an RSS and inserts all ActivityPub posts into the user's timeline */
  103. {
  104. if (!xs_startswith(url, "https:/") && !xs_startswith(url, "http:/"))
  105. return;
  106. xs *hdrs = xs_dict_new();
  107. hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml");
  108. hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
  109. /* get the RSS metadata */
  110. xs *md5 = xs_md5_hex(url, strlen(url));
  111. xs *rss_md_fn = xs_fmt("%s/rss", user->basedir);
  112. mkdirx(rss_md_fn);
  113. rss_md_fn = xs_str_cat(rss_md_fn, "/", md5, ".json");
  114. xs *rss_md = NULL;
  115. const char *etag = NULL;
  116. FILE *f;
  117. if ((f = fopen(rss_md_fn, "r")) != NULL) {
  118. rss_md = xs_json_load(f);
  119. fclose(f);
  120. etag = xs_dict_get(rss_md, "etag");
  121. if (xs_is_string(etag))
  122. hdrs = xs_dict_set(hdrs, "if-none-match", etag);
  123. }
  124. if (rss_md == NULL)
  125. rss_md = xs_dict_new();
  126. xs *payload = NULL;
  127. int status;
  128. int p_size;
  129. xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0);
  130. snac_log(user, xs_fmt("parsing RSS %s %d", url, status));
  131. if (!valid_status(status) || !xs_is_string(payload))
  132. return;
  133. /* not an RSS? done */
  134. const char *ctype = xs_dict_get(rsp, "content-type");
  135. if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1)
  136. return;
  137. /* yes, parsing is done with regexes (now I have two problems blah blah blah) */
  138. xs *links = xs_regex_select(payload, "<link>[^<]+</link>");
  139. const char *link;
  140. xs_list_foreach(links, link) {
  141. xs *l = xs_replace(link, "<link>", "");
  142. char *p = strchr(l, '<');
  143. if (p == NULL)
  144. continue;
  145. *p = '\0';
  146. /* skip this same URL */
  147. if (strcmp(l, url) == 0)
  148. continue;
  149. /* skip crap */
  150. if (!xs_startswith(l, "https:/") && !xs_startswith(l, "http:/"))
  151. continue;
  152. snac_debug(user, 1, xs_fmt("RSS link: %s", l));
  153. if (timeline_here(user, l)) {
  154. snac_debug(user, 1, xs_fmt("RSS entry already in timeline %s", l));
  155. continue;
  156. }
  157. /* special trick for Mastodon: convert from the alternate format */
  158. if (strchr(l, '@') != NULL) {
  159. xs *l2 = xs_split(l, "/");
  160. if (xs_list_len(l2) == 5) {
  161. const char *uid = xs_list_get(l2, 3);
  162. if (*uid == '@') {
  163. xs *guessed_id = xs_fmt("https:/" "/%s/users/%s/statuses/%s",
  164. xs_list_get(l2, 2), uid + 1, xs_list_get(l2, -1));
  165. if (timeline_here(user, guessed_id)) {
  166. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (alt) %s", guessed_id));
  167. continue;
  168. }
  169. }
  170. }
  171. }
  172. xs *obj = NULL;
  173. if (!valid_status(object_get(l, &obj))) {
  174. /* object is not here: bring it */
  175. if (!valid_status(activitypub_request(user, l, &obj)))
  176. continue;
  177. }
  178. if (xs_is_dict(obj)) {
  179. const char *id = xs_dict_get(obj, "id");
  180. const char *type = xs_dict_get(obj, "type");
  181. const char *attr_to = get_atto(obj);
  182. if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
  183. continue;
  184. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  185. continue;
  186. if (timeline_here(user, id)) {
  187. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (id) %s", id));
  188. continue;
  189. }
  190. enqueue_actor_refresh(user, attr_to, 0);
  191. timeline_add(user, id, obj);
  192. snac_log(user, xs_fmt("new '%s' (RSS) %s %s", type, attr_to, id));
  193. }
  194. }
  195. /* update the RSS metadata */
  196. etag = xs_dict_get(rsp, "etag");
  197. if (xs_is_string(etag)) {
  198. rss_md = xs_dict_set(rss_md, "etag", etag);
  199. rss_md = xs_dict_set(rss_md, "url", url);
  200. if ((f = fopen(rss_md_fn, "w")) != NULL) {
  201. xs_json_dump(rss_md, 4, f);
  202. fclose(f);
  203. }
  204. }
  205. }
  206. void rss_poll_hashtags(void)
  207. /* parses all RSS from all users */
  208. {
  209. xs *list = user_list();
  210. const char *uid;
  211. xs_list_foreach(list, uid) {
  212. snac user;
  213. if (user_open(&user, uid)) {
  214. const xs_list *rss = xs_dict_get(user.config, "followed_hashtags");
  215. if (xs_is_list(rss)) {
  216. const char *url;
  217. xs_list_foreach(rss, url)
  218. rss_to_timeline(&user, url);
  219. }
  220. user_free(&user);
  221. }
  222. }
  223. }