1
0

rss.c 8.0 KB

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