utils.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_time.h"
  7. #include "xs_openssl.h"
  8. #include "xs_random.h"
  9. #include "xs_glob.h"
  10. #include "xs_curl.h"
  11. #include "xs_regex.h"
  12. #include "snac.h"
  13. #include <sys/stat.h>
  14. #include <stdlib.h>
  15. static const char *default_srv_config = "{"
  16. "\"host\": \"\","
  17. "\"prefix\": \"\","
  18. "\"address\": \"127.0.0.1\","
  19. "\"port\": 8001,"
  20. "\"layout\": 0.0,"
  21. "\"dbglevel\": 0,"
  22. "\"queue_retry_minutes\": 2,"
  23. "\"queue_retry_max\": 10,"
  24. "\"queue_timeout\": 6,"
  25. "\"queue_timeout_2\": 8,"
  26. "\"cssurls\": [\"\"],"
  27. "\"max_timeline_entries\": 50,"
  28. "\"timeline_purge_days\": 120,"
  29. "\"local_purge_days\": 0,"
  30. "\"min_account_age\": 0,"
  31. "\"admin_email\": \"\","
  32. "\"admin_account\": \"\","
  33. "\"title\": \"\","
  34. "\"short_description\": \"\","
  35. "\"protocol\": \"https\","
  36. "\"fastcgi\": false"
  37. "}";
  38. static const char *default_css =
  39. "body { max-width: 48em; margin: auto; line-height: 1.5; padding: 0.8em; word-wrap: break-word; }\n"
  40. "pre { overflow-x: scroll; }\n"
  41. ".snac-embedded-video, img { max-width: 100% }\n"
  42. ".snac-origin { font-size: 85% }\n"
  43. ".snac-score { float: right; font-size: 85% }\n"
  44. ".snac-top-user { text-align: center; padding-bottom: 2em }\n"
  45. ".snac-top-user-name { font-size: 200% }\n"
  46. ".snac-top-user-id { font-size: 150% }\n"
  47. ".snac-announcement { border: black 1px solid; padding: 0.5em }\n"
  48. ".snac-avatar { float: left; height: 2.5em; width: 2.5em; padding: 0.25em }\n"
  49. ".snac-author { font-size: 90%; text-decoration: none }\n"
  50. ".snac-author-tag { font-size: 80% }\n"
  51. ".snac-pubdate { color: #a0a0a0; font-size: 90% }\n"
  52. ".snac-top-controls { padding-bottom: 1.5em }\n"
  53. ".snac-post { border-top: 1px solid #a0a0a0; padding-top: 0.5em; padding-bottom: 0.5em; }\n"
  54. ".snac-children { padding-left: 1em; border-left: 1px solid #a0a0a0; }\n"
  55. ".snac-textarea { font-family: inherit; width: 100% }\n"
  56. ".snac-history { border: 1px solid #606060; border-radius: 3px; margin: 2.5em 0; padding: 0 2em }\n"
  57. ".snac-btn-mute { float: right; margin-left: 0.5em }\n"
  58. ".snac-btn-unmute { float: right; margin-left: 0.5em }\n"
  59. ".snac-btn-follow { float: right; margin-left: 0.5em }\n"
  60. ".snac-btn-unfollow { float: right; margin-left: 0.5em }\n"
  61. ".snac-btn-hide { float: right; margin-left: 0.5em }\n"
  62. ".snac-btn-delete { float: right; margin-left: 0.5em }\n"
  63. ".snac-btn-limit { float: right; margin-left: 0.5em }\n"
  64. ".snac-btn-unlimit { float: right; margin-left: 0.5em }\n"
  65. ".snac-footer { margin-top: 2em; font-size: 75% }\n"
  66. ".snac-poll-result { margin-left: auto; margin-right: auto; }\n"
  67. ".snac-list-of-lists { padding-left: 0; }\n"
  68. ".snac-list-of-lists li { display: inline; border: 1px solid #a0a0a0; border-radius: 25px;\n"
  69. " margin-right: 0.5em; padding-left: 0.5em; padding-right: 0.5em; }\n"
  70. "@media (prefers-color-scheme: dark) { \n"
  71. " body, input, textarea { background-color: #000; color: #fff; }\n"
  72. " a { color: #7799dd }\n"
  73. " a:visited { color: #aa99dd }\n"
  74. "}\n"
  75. ;
  76. const char *snac_blurb =
  77. "<p><b>%host%</b> is a <a href=\"https:/"
  78. "/en.wikipedia.org/wiki/Fediverse\">Fediverse</a> "
  79. "instance that uses the <a href=\"https:/"
  80. "/en.wikipedia.org/wiki/ActivityPub\">ActivityPub</a> "
  81. "protocol. In other words, users at this host can communicate with people "
  82. "that use software like Mastodon, Pleroma, Friendica, etc. "
  83. "all around the world.</p>\n"
  84. "<p>This server runs the "
  85. "<a href=\"" WHAT_IS_SNAC_URL "\">snac</a> software and there is no "
  86. "automatic sign-up process.</p>\n"
  87. ;
  88. static const char *greeting_html =
  89. "<!DOCTYPE html>\n"
  90. "<html><head>\n"
  91. "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>\n"
  92. "<link rel=\"icon\" type=\"image/x-icon\" href=\"https://%host%/favicon.ico\"/>\n"
  93. "<title>Welcome to %host%</title>\n"
  94. "<body style=\"margin: auto; max-width: 50em\">\n"
  95. "%blurb%"
  96. "<p>The following users are part of this community:</p>\n"
  97. "\n"
  98. "%userlist%\n"
  99. "\n"
  100. "<p>This site is powered by <abbr title=\"Social Networks Are Crap\">snac</abbr>.</p>\n"
  101. "</body></html>\n";
  102. int write_default_css(void)
  103. {
  104. FILE *f;
  105. xs *sfn = xs_fmt("%s/style.css", srv_basedir);
  106. if ((f = fopen(sfn, "w")) == NULL)
  107. return 1;
  108. fwrite(default_css, strlen(default_css), 1, f);
  109. fclose(f);
  110. return 0;
  111. }
  112. int snac_init(const char *basedir)
  113. {
  114. FILE *f;
  115. if (basedir == NULL) {
  116. printf("Base directory: "); fflush(stdout);
  117. srv_basedir = xs_strip_i(xs_readline(stdin));
  118. }
  119. else
  120. srv_basedir = xs_str_new(basedir);
  121. if (srv_basedir == NULL || *srv_basedir == '\0')
  122. return 1;
  123. if (xs_endswith(srv_basedir, "/"))
  124. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  125. if (mtime(srv_basedir) != 0.0) {
  126. printf("ERROR: directory '%s' must not exist.\n", srv_basedir);
  127. return 1;
  128. }
  129. srv_config = xs_json_loads(default_srv_config);
  130. xs *layout = xs_number_new(disk_layout);
  131. srv_config = xs_dict_set(srv_config, "layout", layout);
  132. int is_unix_socket = 0;
  133. printf("Network address or full path to unix socket [%s]: ", xs_dict_get(srv_config, "address")); fflush(stdout);
  134. {
  135. xs *i = xs_strip_i(xs_readline(stdin));
  136. if (*i) {
  137. srv_config = xs_dict_set(srv_config, "address", i);
  138. if (*i == '/')
  139. is_unix_socket = 1;
  140. }
  141. }
  142. if (!is_unix_socket) {
  143. printf("Network port [%d]: ", (int)xs_number_get(xs_dict_get(srv_config, "port"))); fflush(stdout);
  144. {
  145. xs *i = xs_strip_i(xs_readline(stdin));
  146. if (*i) {
  147. xs *n = xs_number_new(atoi(i));
  148. srv_config = xs_dict_set(srv_config, "port", n);
  149. }
  150. }
  151. }
  152. else {
  153. xs *n = xs_number_new(0);
  154. srv_config = xs_dict_set(srv_config, "port", n);
  155. }
  156. printf("Host name: "); fflush(stdout);
  157. {
  158. xs *i = xs_strip_i(xs_readline(stdin));
  159. if (*i == '\0')
  160. return 1;
  161. srv_config = xs_dict_set(srv_config, "host", i);
  162. }
  163. printf("URL prefix: "); fflush(stdout);
  164. {
  165. xs *i = xs_strip_i(xs_readline(stdin));
  166. if (*i) {
  167. if (xs_endswith(i, "/"))
  168. i = xs_crop_i(i, 0, -1);
  169. srv_config = xs_dict_set(srv_config, "prefix", i);
  170. }
  171. }
  172. printf("Admin email address (optional): "); fflush(stdout);
  173. {
  174. xs *i = xs_strip_i(xs_readline(stdin));
  175. srv_config = xs_dict_set(srv_config, "admin_email", i);
  176. }
  177. if (mkdirx(srv_basedir) == -1) {
  178. printf("ERROR: cannot create directory '%s'\n", srv_basedir);
  179. return 1;
  180. }
  181. xs *udir = xs_fmt("%s/user", srv_basedir);
  182. mkdirx(udir);
  183. xs *odir = xs_fmt("%s/object", srv_basedir);
  184. mkdirx(odir);
  185. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  186. mkdirx(qdir);
  187. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  188. mkdirx(ibdir);
  189. xs *gfn = xs_fmt("%s/greeting.html", srv_basedir);
  190. if ((f = fopen(gfn, "w")) == NULL) {
  191. printf("ERROR: cannot create '%s'\n", gfn);
  192. return 1;
  193. }
  194. xs *gh = xs_replace(greeting_html, "%blurb%", snac_blurb);
  195. fwrite(gh, strlen(gh), 1, f);
  196. fclose(f);
  197. if (write_default_css()) {
  198. printf("ERROR: cannot create style.css\n");
  199. return 1;
  200. }
  201. xs *cfn = xs_fmt("%s/server.json", srv_basedir);
  202. if ((f = fopen(cfn, "w")) == NULL) {
  203. printf("ERROR: cannot create '%s'\n", cfn);
  204. return 1;
  205. }
  206. xs_json_dump(srv_config, 4, f);
  207. fclose(f);
  208. printf("Done.\n");
  209. return 0;
  210. }
  211. void new_password(const char *uid, xs_str **clear_pwd, xs_str **hashed_pwd)
  212. /* creates a random password */
  213. {
  214. int rndbuf[3];
  215. xs_rnd_buf(rndbuf, sizeof(rndbuf));
  216. *clear_pwd = xs_base64_enc((char *)rndbuf, sizeof(rndbuf));
  217. *hashed_pwd = hash_password(uid, *clear_pwd, NULL);
  218. }
  219. int adduser(const char *uid)
  220. /* creates a new user */
  221. {
  222. snac snac;
  223. xs *config = xs_dict_new();
  224. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  225. xs *pwd = NULL;
  226. xs *pwd_f = NULL;
  227. xs *key = NULL;
  228. FILE *f;
  229. if (uid == NULL) {
  230. printf("Username: "); fflush(stdout);
  231. uid = xs_strip_i(xs_readline(stdin));
  232. }
  233. if (!validate_uid(uid)) {
  234. printf("ERROR: only alphanumeric characters and _ are allowed in user ids.\n");
  235. return 1;
  236. }
  237. if (user_open(&snac, uid)) {
  238. printf("ERROR: user '%s' already exists\n", snac.uid);
  239. return 1;
  240. }
  241. new_password(uid, &pwd, &pwd_f);
  242. config = xs_dict_append(config, "uid", uid);
  243. config = xs_dict_append(config, "name", uid);
  244. config = xs_dict_append(config, "avatar", "");
  245. config = xs_dict_append(config, "bio", "");
  246. config = xs_dict_append(config, "cw", "");
  247. config = xs_dict_append(config, "published", date);
  248. config = xs_dict_append(config, "passwd", pwd_f);
  249. xs *basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  250. if (mkdirx(basedir) == -1) {
  251. printf("ERROR: cannot create directory '%s'\n", basedir);
  252. return 0;
  253. }
  254. const char *dirs[] = {
  255. "followers", "following", "muted", "hidden",
  256. "public", "private", "queue", "history",
  257. "static", NULL };
  258. int n;
  259. for (n = 0; dirs[n]; n++) {
  260. xs *d = xs_fmt("%s/%s", basedir, dirs[n]);
  261. mkdirx(d);
  262. }
  263. xs *cfn = xs_fmt("%s/user.json", basedir);
  264. if ((f = fopen(cfn, "w")) == NULL) {
  265. printf("ERROR: cannot create '%s'\n", cfn);
  266. return 1;
  267. }
  268. else {
  269. xs_json_dump(config, 4, f);
  270. fclose(f);
  271. }
  272. printf("\nCreating RSA key...\n");
  273. key = xs_evp_genkey(4096);
  274. printf("Done.\n");
  275. xs *kfn = xs_fmt("%s/key.json", basedir);
  276. if ((f = fopen(kfn, "w")) == NULL) {
  277. printf("ERROR: cannot create '%s'\n", kfn);
  278. return 1;
  279. }
  280. else {
  281. xs_json_dump(key, 4, f);
  282. fclose(f);
  283. }
  284. printf("\nUser password is %s\n", pwd);
  285. printf("\nGo to %s/%s and continue configuring your user there.\n", srv_baseurl, uid);
  286. return 0;
  287. }
  288. int resetpwd(snac *snac)
  289. /* creates a new password for the user */
  290. {
  291. xs *clear_pwd = NULL;
  292. xs *hashed_pwd = NULL;
  293. xs *fn = xs_fmt("%s/user.json", snac->basedir);
  294. FILE *f;
  295. int ret = 0;
  296. new_password(snac->uid, &clear_pwd, &hashed_pwd);
  297. snac->config = xs_dict_set(snac->config, "passwd", hashed_pwd);
  298. if ((f = fopen(fn, "w")) != NULL) {
  299. xs_json_dump(snac->config, 4, f);
  300. fclose(f);
  301. printf("New password for user %s is %s\n", snac->uid, clear_pwd);
  302. }
  303. else {
  304. printf("ERROR: cannot write to %s\n", fn);
  305. ret = 1;
  306. }
  307. return ret;
  308. }
  309. void rm_rf(const char *dir)
  310. /* does an rm -rf (yes, I'm also scared) */
  311. {
  312. xs *d = xs_str_cat(xs_dup(dir), "/" "*");
  313. xs *l = xs_glob(d, 0, 0);
  314. xs_list *p = l;
  315. const xs_str *v;
  316. if (dbglevel >= 1)
  317. printf("Deleting directory %s\n", dir);
  318. while (xs_list_iter(&p, &v)) {
  319. struct stat st;
  320. if (stat(v, &st) != -1) {
  321. if (st.st_mode & S_IFDIR) {
  322. rm_rf(v);
  323. }
  324. else {
  325. if (dbglevel >= 1)
  326. printf("Deleting file %s\n", v);
  327. if (unlink(v) == -1)
  328. printf("ERROR: cannot delete file %s\n", v);
  329. }
  330. }
  331. else
  332. printf("ERROR: stat() fail for %s\n", v);
  333. }
  334. if (rmdir(dir) == -1)
  335. printf("ERROR: cannot delete directory %s\n", dir);
  336. }
  337. int deluser(snac *user)
  338. /* deletes a user */
  339. {
  340. int ret = 0;
  341. xs *fwers = following_list(user);
  342. xs_list *p = fwers;
  343. const xs_str *v;
  344. while (xs_list_iter(&p, &v)) {
  345. xs *object = NULL;
  346. if (valid_status(following_get(user, v, &object))) {
  347. xs *msg = msg_undo(user, xs_dict_get(object, "object"));
  348. following_del(user, v);
  349. enqueue_output_by_actor(user, msg, v, 0);
  350. printf("Unfollowing actor %s\n", v);
  351. }
  352. }
  353. rm_rf(user->basedir);
  354. return ret;
  355. }
  356. void verify_links(snac *user)
  357. /* verifies a user's links */
  358. {
  359. xs *metadata = NULL;
  360. const xs_dict *md = xs_dict_get(user->config, "metadata");
  361. const char *k, *v;
  362. int changed = 0;
  363. xs *headers = xs_dict_new();
  364. headers = xs_dict_append(headers, "accept", "text/html");
  365. headers = xs_dict_append(headers, "user-agent", USER_AGENT " (link verify)");
  366. if (xs_type(md) == XSTYPE_DICT)
  367. metadata = xs_dup(md);
  368. else
  369. if (xs_type(md) == XSTYPE_STRING) {
  370. /* convert to dict for easier iteration */
  371. metadata = xs_dict_new();
  372. xs *l = xs_split(md, "\n");
  373. const char *ll;
  374. xs_list_foreach(l, ll) {
  375. xs *kv = xs_split_n(ll, "=", 1);
  376. const char *k = xs_list_get(kv, 0);
  377. const char *v = xs_list_get(kv, 1);
  378. if (k && v) {
  379. xs *kk = xs_strip_i(xs_dup(k));
  380. xs *vv = xs_strip_i(xs_dup(v));
  381. metadata = xs_dict_set(metadata, kk, vv);
  382. }
  383. }
  384. }
  385. int c = 0;
  386. while (metadata && xs_dict_next(metadata, &k, &v, &c)) {
  387. /* not an https link? skip */
  388. if (!xs_startswith(v, "https:/" "/"))
  389. continue;
  390. int status;
  391. xs *req = NULL;
  392. xs *payload = NULL;
  393. int p_size = 0;
  394. req = xs_http_request("GET", v, headers, NULL, 0, &status,
  395. &payload, &p_size, 0);
  396. if (!valid_status(status)) {
  397. snac_log(user, xs_fmt("link %s verify error %d", v, status));
  398. continue;
  399. }
  400. /* extract the links */
  401. xs *ls = xs_regex_select(payload, "< *(a|link) +[^>]+>");
  402. xs_list *lp = ls;
  403. const char *ll;
  404. int vfied = 0;
  405. while (!vfied && xs_list_iter(&lp, &ll)) {
  406. /* extract href and rel */
  407. xs *r = xs_regex_select(ll, "(href|rel) *= *(\"[^\"]*\"|'[^']*')");
  408. /* must have both attributes */
  409. if (xs_list_len(r) != 2)
  410. continue;
  411. xs *href = NULL;
  412. int is_rel_me = 0;
  413. xs_list *pr = r;
  414. const char *ar;
  415. while (xs_list_iter(&pr, &ar)) {
  416. xs *nq = xs_dup(ar);
  417. nq = xs_replace_i(nq, "\"", "");
  418. nq = xs_replace_i(nq, "'", "");
  419. xs *r2 = xs_split_n(nq, "=", 1);
  420. if (xs_list_len(r2) != 2)
  421. continue;
  422. xs *ak = xs_strip_i(xs_dup(xs_list_get(r2, 0)));
  423. xs *av = xs_strip_i(xs_dup(xs_list_get(r2, 1)));
  424. if (strcmp(ak, "href") == 0)
  425. href = xs_dup(av);
  426. else
  427. if (strcmp(ak, "rel") == 0) {
  428. /* split the value by spaces */
  429. xs *vbs = xs_split(av, " ");
  430. /* is any of it "me"? */
  431. if (xs_list_in(vbs, "me") != -1)
  432. is_rel_me = 1;
  433. }
  434. }
  435. /* after all this acrobatics, do we have an href and a rel="me"? */
  436. if (href != NULL && is_rel_me) {
  437. /* is it the same as the actor? */
  438. if (strcmp(href, user->actor) == 0) {
  439. /* got it! */
  440. xs *verified_time = xs_number_new((double)time(NULL));
  441. if (user->links == NULL)
  442. user->links = xs_dict_new();
  443. user->links = xs_dict_set(user->links, v, verified_time);
  444. vfied = 1;
  445. }
  446. else
  447. snac_debug(user, 1,
  448. xs_fmt("verify link %s rel='me' found but not related (%s)", v, href));
  449. }
  450. }
  451. if (vfied) {
  452. changed++;
  453. snac_log(user, xs_fmt("link %s verified", v));
  454. }
  455. else {
  456. snac_log(user, xs_fmt("link %s not verified (rel='me' not found)", v));
  457. }
  458. }
  459. if (changed) {
  460. FILE *f;
  461. /* update the links.json file */
  462. xs *fn = xs_fmt("%s/links.json", user->basedir);
  463. xs *bfn = xs_fmt("%s.bak", fn);
  464. rename(fn, bfn);
  465. if ((f = fopen(fn, "w")) != NULL) {
  466. xs_json_dump(user->links, 4, f);
  467. fclose(f);
  468. }
  469. else
  470. rename(bfn, fn);
  471. }
  472. }
  473. void export_csv(snac *user)
  474. /* exports user data to current directory in a way that pleases Mastodon */
  475. {
  476. FILE *f;
  477. const char *fn;
  478. fn = "bookmarks.csv";
  479. if ((f = fopen(fn, "w")) != NULL) {
  480. snac_log(user, xs_fmt("Creating %s...", fn));
  481. xs *l = bookmark_list(user);
  482. const char *md5;
  483. xs_list_foreach(l, md5) {
  484. xs *post = NULL;
  485. if (valid_status(object_get_by_md5(md5, &post))) {
  486. const char *id = xs_dict_get(post, "id");
  487. if (xs_type(id) == XSTYPE_STRING)
  488. fprintf(f, "%s\n", id);
  489. }
  490. }
  491. fclose(f);
  492. }
  493. else
  494. snac_log(user, xs_fmt("Cannot create file %s", fn));
  495. fn = "blocked_accounts.csv";
  496. if ((f = fopen(fn, "w")) != NULL) {
  497. snac_log(user, xs_fmt("Creating %s...", fn));
  498. xs *l = muted_list(user);
  499. const char *actor;
  500. xs_list_foreach(l, actor) {
  501. xs *uid = NULL;
  502. webfinger_request_fake(actor, NULL, &uid);
  503. fprintf(f, "%s\n", uid);
  504. }
  505. fclose(f);
  506. }
  507. else
  508. snac_log(user, xs_fmt("Cannot create file %s", fn));
  509. fn = "lists.csv";
  510. if ((f = fopen(fn, "w")) != NULL) {
  511. snac_log(user, xs_fmt("Creating %s...", fn));
  512. xs *lol = list_maint(user, NULL, 0);
  513. const xs_list *li;
  514. xs_list_foreach(lol, li) {
  515. const char *lid = xs_list_get(li, 0);
  516. const char *ltitle = xs_list_get(li, 1);
  517. xs *actors = list_content(user, lid, NULL, 0);
  518. const char *md5;
  519. xs_list_foreach(actors, md5) {
  520. xs *actor = NULL;
  521. if (valid_status(object_get_by_md5(md5, &actor))) {
  522. const char *id = xs_dict_get(actor, "id");
  523. xs *uid = NULL;
  524. webfinger_request_fake(id, NULL, &uid);
  525. fprintf(f, "%s,%s\n", ltitle, uid);
  526. }
  527. }
  528. }
  529. fclose(f);
  530. }
  531. else
  532. snac_log(user, xs_fmt("Cannot create file %s", fn));
  533. fn = "following_accounts.csv";
  534. if ((f = fopen(fn, "w")) != NULL) {
  535. snac_log(user, xs_fmt("Creating %s...", fn));
  536. fprintf(f, "Account address,Show boosts,Notify on new posts,Languages\n");
  537. xs *fwing = following_list(user);
  538. const char *actor;
  539. xs_list_foreach(fwing, actor) {
  540. xs *uid = NULL;
  541. webfinger_request_fake(actor, NULL, &uid);
  542. fprintf(f, "%s,%s,false,\n", uid, limited(user, actor, 0) ? "false" : "true");
  543. }
  544. fclose(f);
  545. }
  546. else
  547. snac_log(user, xs_fmt("Cannot create file %s", fn));
  548. }
  549. void import_blocked_accounts_csv(snac *user, const char *fn)
  550. /* imports a Mastodon CSV file of blocked accounts */
  551. {
  552. FILE *f;
  553. if ((f = fopen(fn, "r")) != NULL) {
  554. snac_log(user, xs_fmt("Importing from %s...", fn));
  555. while (!feof(f)) {
  556. xs *l = xs_strip_i(xs_readline(f));
  557. if (*l && strchr(l, '@') != NULL) {
  558. xs *url = NULL;
  559. xs *uid = NULL;
  560. if (valid_status(webfinger_request(l, &url, &uid))) {
  561. if (is_muted(user, url))
  562. snac_log(user, xs_fmt("Actor %s already MUTEd", url));
  563. else {
  564. mute(user, url);
  565. snac_log(user, xs_fmt("MUTEd actor %s", url));
  566. }
  567. }
  568. else
  569. snac_log(user, xs_fmt("Webfinger error for account %s", l));
  570. }
  571. }
  572. fclose(f);
  573. }
  574. else
  575. snac_log(user, xs_fmt("Cannot open file %s", fn));
  576. }
  577. void import_following_accounts_csv(snac *user, const char *fn)
  578. /* imports a Mastodon CSV file of accounts to follow */
  579. {
  580. FILE *f;
  581. if ((f = fopen(fn, "r")) != NULL) {
  582. snac_log(user, xs_fmt("Importing from %s...", fn));
  583. while (!feof(f)) {
  584. xs *l = xs_strip_i(xs_readline(f));
  585. if (*l) {
  586. xs *l2 = xs_split(l, ",");
  587. const char *acct = xs_list_get(l2, 0);
  588. const char *show = xs_list_get(l2, 1);
  589. if (acct) {
  590. /* not a valid account? skip (probably the CSV header) */
  591. if (strchr(acct, '@') == NULL)
  592. continue;
  593. xs *msg = msg_follow(user, acct);
  594. if (msg == NULL) {
  595. snac_log(user, xs_fmt("Cannot follow %s -- server down?", acct));
  596. continue;
  597. }
  598. const char *actor = xs_dict_get(msg, "object");
  599. if (following_check(user, actor))
  600. snac_log(user, xs_fmt("Actor %s already followed", actor));
  601. else {
  602. following_add(user, actor, msg);
  603. enqueue_output_by_actor(user, msg, actor, 0);
  604. snac_log(user, xs_fmt("Following %s", actor));
  605. }
  606. if (show && strcmp(show, "false") == 0) {
  607. limit(user, actor);
  608. snac_log(user, xs_fmt("Limiting boosts from actor %s", actor));
  609. }
  610. else {
  611. unlimit(user, actor);
  612. snac_log(user, xs_fmt("Unlimiting boosts from actor %s", actor));
  613. }
  614. }
  615. }
  616. }
  617. fclose(f);
  618. }
  619. else
  620. snac_log(user, xs_fmt("Cannot open file %s", fn));
  621. }
  622. void import_list_csv(snac *user, const char *fn)
  623. /* imports a Mastodon CSV file list */
  624. {
  625. FILE *f;
  626. if ((f = fopen(fn, "r")) != NULL) {
  627. snac_log(user, xs_fmt("Importing from %s...", fn));
  628. while (!feof(f)) {
  629. xs *l = xs_strip_i(xs_readline(f));
  630. if (*l) {
  631. xs *l2 = xs_split(l, ",");
  632. const char *lname = xs_list_get(l2, 0);
  633. const char *acct = xs_list_get(l2, 1);
  634. if (lname && acct) {
  635. /* create the list */
  636. xs *list_id = list_maint(user, lname, 1);
  637. xs *url = NULL;
  638. xs *uid = NULL;
  639. if (valid_status(webfinger_request(acct, &url, &uid))) {
  640. xs *actor_md5 = xs_md5_hex(url, strlen(url));
  641. list_content(user, list_id, actor_md5, 1);
  642. snac_log(user, xs_fmt("Added %s to list %s", url, lname));
  643. if (!following_check(user, url)) {
  644. xs *msg = msg_follow(user, url);
  645. if (msg == NULL) {
  646. snac_log(user, xs_fmt("Cannot follow %s -- server down?", acct));
  647. continue;
  648. }
  649. following_add(user, url, msg);
  650. enqueue_output_by_actor(user, msg, url, 0);
  651. snac_log(user, xs_fmt("Following %s", url));
  652. }
  653. }
  654. else
  655. snac_log(user, xs_fmt("Webfinger error while adding %s to list %s", acct, lname));
  656. }
  657. }
  658. }
  659. fclose(f);
  660. }
  661. else
  662. snac_log(user, xs_fmt("Cannot open file %s", fn));
  663. }
  664. void import_csv(snac *user)
  665. /* import CSV files from Mastodon */
  666. {
  667. FILE *f;
  668. const char *fn;
  669. import_blocked_accounts_csv(user, "blocked_accounts.csv");
  670. import_following_accounts_csv(user, "following_accounts.csv");
  671. import_list_csv(user, "lists.csv");
  672. fn = "bookmarks.csv";
  673. if ((f = fopen(fn, "r")) != NULL) {
  674. snac_log(user, xs_fmt("Importing from %s...", fn));
  675. while (!feof(f)) {
  676. xs *l = xs_strip_i(xs_readline(f));
  677. if (*l) {
  678. xs *post = NULL;
  679. if (!object_get(l, &post)) {
  680. if (!valid_status(activitypub_request(user, l, &post))) {
  681. snac_log(user, xs_fmt("Error getting object %s for bookmarking", l));
  682. continue;
  683. }
  684. }
  685. if (post == NULL)
  686. continue;
  687. /* request the actor that created the post */
  688. const char *actor = get_atto(post);
  689. if (xs_type(actor) == XSTYPE_STRING)
  690. actor_request(user, actor, NULL);
  691. object_add_ow(l, post);
  692. timeline_add(user, l, post);
  693. bookmark(user, l);
  694. snac_log(user, xs_fmt("Bookmarked %s", l));
  695. }
  696. }
  697. fclose(f);
  698. }
  699. else
  700. snac_log(user, xs_fmt("Cannot open file %s", fn));
  701. }