utils.c 28 KB

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