rss.c 7.9 KB

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