rss.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2025 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 "snac.h"
  10. xs_str *rss_from_timeline(snac *user, const xs_list *timeline,
  11. const char *title, const char *link, const char *desc)
  12. /* converts a timeline to rss */
  13. {
  14. xs_html *rss = xs_html_tag("rss",
  15. xs_html_attr("xmlns:content", "http:/" "/purl.org/rss/1.0/modules/content/"),
  16. xs_html_attr("version", "2.0"),
  17. xs_html_attr("xmlns:atom", "http:/" "/www.w3.org/2005/Atom"));
  18. xs_html *channel = xs_html_tag("channel",
  19. xs_html_tag("title",
  20. xs_html_text(title)),
  21. xs_html_tag("language",
  22. xs_html_text("en")),
  23. xs_html_tag("link",
  24. xs_html_text(link)),
  25. xs_html_sctag("atom:link",
  26. xs_html_attr("href", link),
  27. xs_html_attr("rel", "self"),
  28. xs_html_attr("type", "application/rss+xml")),
  29. xs_html_tag("generator",
  30. xs_html_text(USER_AGENT)),
  31. xs_html_tag("description",
  32. xs_html_text(desc)));
  33. xs_html_add(rss, channel);
  34. int cnt = 0;
  35. const char *v;
  36. xs_list_foreach(timeline, v) {
  37. xs *msg = NULL;
  38. if (user) {
  39. if (!valid_status(timeline_get_by_md5(user, v, &msg)))
  40. continue;
  41. }
  42. else {
  43. if (!valid_status(object_get_by_md5(v, &msg)))
  44. continue;
  45. }
  46. const char *id = xs_dict_get(msg, "id");
  47. const char *content = xs_dict_get(msg, "content");
  48. const char *published = xs_dict_get(msg, "published");
  49. if (user && !xs_startswith(id, user->actor))
  50. continue;
  51. if (!id || !content || !published)
  52. continue;
  53. /* create a title with the first line of the content */
  54. xs *title = xs_replace(content, "<br>", "\n");
  55. title = xs_regex_replace_i(title, "<[^>]+>", " ");
  56. title = xs_regex_replace_i(title, "&[^;]+;", " ");
  57. int i;
  58. for (i = 0; title[i] && title[i] != '\n' && i < 50; i++);
  59. if (title[i] != '\0') {
  60. title[i] = '\0';
  61. title = xs_str_cat(title, "...");
  62. }
  63. title = xs_strip_i(title);
  64. /* convert the date */
  65. time_t t = xs_parse_iso_date(published, 0);
  66. xs *rss_date = xs_str_utctime(t, "%a, %d %b %Y %T +0000");
  67. /* if it's the first one, add it to the header */
  68. if (cnt == 0)
  69. xs_html_add(channel,
  70. xs_html_tag("lastBuildDate",
  71. xs_html_text(rss_date)));
  72. xs_html_add(channel,
  73. xs_html_tag("item",
  74. xs_html_tag("title",
  75. xs_html_text(title)),
  76. xs_html_tag("link",
  77. xs_html_text(id)),
  78. xs_html_tag("guid",
  79. xs_html_text(id)),
  80. xs_html_tag("pubDate",
  81. xs_html_text(rss_date)),
  82. xs_html_tag("description",
  83. xs_html_text(content))));
  84. cnt++;
  85. }
  86. return xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
  87. }
  88. void rss_to_timeline(snac *user, const char *url)
  89. /* reads an RSS and inserts all ActivityPub posts into the user's timeline */
  90. {
  91. if (!xs_startswith(url, "https:/") && !xs_startswith(url, "http:/"))
  92. return;
  93. xs *hdrs = xs_dict_new();
  94. hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml");
  95. hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
  96. xs *payload = NULL;
  97. int status;
  98. int p_size;
  99. xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0);
  100. if (!valid_status(status) || !xs_is_string(payload))
  101. return;
  102. /* not an RSS? done */
  103. const char *ctype = xs_dict_get(rsp, "content-type");
  104. if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1)
  105. return;
  106. snac_log(user, xs_fmt("parsing RSS %s", url));
  107. /* yes, parsing is done with regexes (now I have two problems blah blah blah) */
  108. xs *links = xs_regex_select(payload, "<link>[^<]+</link>");
  109. const char *link;
  110. xs_list_foreach(links, link) {
  111. xs *l = xs_replace(link, "<link>", "");
  112. char *p = strchr(l, '<');
  113. if (p == NULL)
  114. continue;
  115. *p = '\0';
  116. /* skip this same URL */
  117. if (strcmp(l, url) == 0)
  118. continue;
  119. /* skip crap */
  120. if (!xs_startswith(l, "https:/") && !xs_startswith(l, "http:/"))
  121. return;
  122. snac_debug(user, 1, xs_fmt("RSS link: %s", l));
  123. if (timeline_here(user, l)) {
  124. snac_debug(user, 1, xs_fmt("RSS entry already in timeline %s", l));
  125. continue;
  126. }
  127. /* special trick for Mastodon: convert from the alternate format */
  128. if (strchr(l, '@') != NULL) {
  129. xs *l2 = xs_split(l, "/");
  130. if (xs_list_len(l2) == 5) {
  131. const char *uid = xs_list_get(l2, 3);
  132. if (*uid == '@') {
  133. xs *guessed_id = xs_fmt("https:/" "/%s/users/%s/statuses/%s",
  134. xs_list_get(l2, 2), uid + 1, xs_list_get(l2, -1));
  135. if (timeline_here(user, guessed_id)) {
  136. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (alt) %s", guessed_id));
  137. continue;
  138. }
  139. }
  140. }
  141. }
  142. xs *obj = NULL;
  143. if (!valid_status(object_get(l, &obj))) {
  144. /* object is not here: bring it */
  145. if (!valid_status(activitypub_request(user, l, &obj)))
  146. continue;
  147. }
  148. if (xs_is_dict(obj)) {
  149. const char *id = xs_dict_get(obj, "id");
  150. const char *type = xs_dict_get(obj, "type");
  151. const char *attr_to = get_atto(obj);
  152. if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
  153. continue;
  154. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  155. continue;
  156. if (timeline_here(user, id)) {
  157. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (id) %s", id));
  158. continue;
  159. }
  160. if (!valid_status(actor_request(user, attr_to, NULL)))
  161. continue;
  162. timeline_add(user, id, obj);
  163. }
  164. }
  165. }
  166. void rss_poll_hashtags(void)
  167. /* parses all RSS from all users */
  168. {
  169. xs *list = user_list();
  170. const char *uid;
  171. xs_list_foreach(list, uid) {
  172. snac user;
  173. if (user_open(&user, uid)) {
  174. const xs_list *rss = xs_dict_get(user.config, "followed_hashtags");
  175. if (xs_is_list(rss)) {
  176. const char *url;
  177. xs_list_foreach(rss, url)
  178. rss_to_timeline(&user, url);
  179. }
  180. user_free(&user);
  181. }
  182. }
  183. }