rss.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. xs *hdrs = xs_dict_new();
  92. hdrs = xs_dict_set(hdrs, "accept", "application/rss+xml");
  93. hdrs = xs_dict_set(hdrs, "user-agent", USER_AGENT);
  94. xs *payload = NULL;
  95. int status;
  96. int p_size;
  97. xs *rsp = xs_http_request("GET", url, hdrs, NULL, 0, &status, &payload, &p_size, 0);
  98. if (!valid_status(status) || !xs_is_string(payload))
  99. return;
  100. /* not an RSS? done */
  101. const char *ctype = xs_dict_get(rsp, "content-type");
  102. if (!xs_is_string(ctype) || xs_str_in(ctype, "application/rss+xml") == -1)
  103. return;
  104. snac_log(user, xs_fmt("parsing RSS %s", url));
  105. /* yes, parsing is done with regexes (now I have two problems blah blah blah) */
  106. xs *links = xs_regex_select(payload, "<link>[^<]+</link>");
  107. const char *link;
  108. xs_list_foreach(links, link) {
  109. xs *l = xs_replace(link, "<link>", "");
  110. char *p = strchr(l, '<');
  111. if (p == NULL)
  112. continue;
  113. *p = '\0';
  114. /* skip this same URL */
  115. if (strcmp(l, url) == 0)
  116. continue;
  117. snac_debug(user, 1, xs_fmt("RSS link: %s", l));
  118. if (timeline_here(user, l)) {
  119. snac_debug(user, 1, xs_fmt("RSS entry already in timeline %s", l));
  120. continue;
  121. }
  122. /* special trick for Mastodon: convert from the alternate format */
  123. if (strchr(l, '@') != NULL) {
  124. xs *l2 = xs_split(l, "/");
  125. if (xs_list_len(l2) == 5) {
  126. const char *uid = xs_list_get(l2, 3);
  127. if (*uid == '@') {
  128. xs *guessed_id = xs_fmt("https:/" "/%s/users/%s/statuses/%s",
  129. xs_list_get(l2, 2), uid + 1, xs_list_get(l2, -1));
  130. if (timeline_here(user, guessed_id)) {
  131. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (alt) %s", guessed_id));
  132. continue;
  133. }
  134. }
  135. }
  136. }
  137. xs *obj = NULL;
  138. if (!valid_status(object_get(l, &obj))) {
  139. /* object is not here: bring it */
  140. if (!valid_status(activitypub_request(user, l, &obj)))
  141. continue;
  142. }
  143. if (xs_is_dict(obj)) {
  144. const char *id = xs_dict_get(obj, "id");
  145. const char *type = xs_dict_get(obj, "type");
  146. const char *attr_to = get_atto(obj);
  147. if (!xs_is_string(id) || !xs_is_string(type) || !xs_is_string(attr_to))
  148. continue;
  149. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  150. continue;
  151. if (timeline_here(user, id)) {
  152. snac_debug(user, 1, xs_fmt("RSS entry already in timeline (id) %s", id));
  153. continue;
  154. }
  155. if (!valid_status(actor_request(user, attr_to, NULL)))
  156. continue;
  157. timeline_add(user, id, obj);
  158. }
  159. }
  160. }
  161. void rss_process(void)
  162. /* parses all RSS from all users */
  163. {
  164. xs *list = user_list();
  165. const char *uid;
  166. xs_list_foreach(list, uid) {
  167. snac user;
  168. if (user_open(&user, uid)) {
  169. const xs_list *rss = xs_dict_get(user.config, "rss");
  170. if (xs_is_list(rss)) {
  171. const char *url;
  172. xs_list_foreach(rss, url)
  173. rss_to_timeline(&user, url);
  174. }
  175. user_free(&user);
  176. }
  177. }
  178. }