rss.c 7.9 KB

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