main.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 "snac.h"
  9. #include <sys/stat.h>
  10. int usage(void)
  11. {
  12. printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  13. printf("Copyright (c) 2022 - 2024 grunfink et al. / MIT license\n");
  14. printf("\n");
  15. printf("Commands:\n");
  16. printf("\n");
  17. printf("init [{basedir}] Initializes the data storage\n");
  18. printf("upgrade {basedir} Upgrade to a new version\n");
  19. printf("adduser {basedir} [{uid}] Adds a new user\n");
  20. printf("deluser {basedir} {uid} Deletes a user\n");
  21. printf("httpd {basedir} Starts the HTTPD daemon\n");
  22. printf("purge {basedir} Purges old data\n");
  23. printf("state {basedir} Prints server state\n");
  24. printf("webfinger {basedir} {account} Queries about an account (@user@host or actor url)\n");
  25. printf("queue {basedir} {uid} Processes a user queue\n");
  26. printf("follow {basedir} {uid} {actor} Follows an actor\n");
  27. printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  28. printf("request {basedir} {uid} {url} Requests an object\n");
  29. printf("insert {basedir} {uid} {url} Requests an object and inserts it into the timeline\n");
  30. printf("actor {basedir} [{uid}] {url} Requests an actor\n");
  31. printf("note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n");
  32. printf("note_unlisted {basedir} {uid} {text} [files...] Sends an unlisted note with optional attachments\n");
  33. printf("boost|announce {basedir} {uid} {url} Boosts (announces) a post\n");
  34. printf("unboost {basedir} {uid} {url} Unboosts a post\n");
  35. printf("resetpwd {basedir} {uid} Resets the password of a user\n");
  36. printf("ping {basedir} {uid} {actor} Pings an actor\n");
  37. printf("webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n");
  38. printf("pin {basedir} {uid} {msg_url} Pins a message\n");
  39. printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
  40. printf("bookmark {basedir} {uid} {msg_url} Bookmarks a message\n");
  41. printf("unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n");
  42. printf("block {basedir} {instance_url} Blocks a full instance\n");
  43. printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
  44. printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
  45. printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
  46. printf("verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n");
  47. printf("search {basedir} {uid} {regex} Searches posts by content\n");
  48. printf("export_csv {basedir} {uid} Exports data as CSV files into current directory\n");
  49. printf("alias {basedir} {uid} {account} Sets account (@user@host or actor url) as an alias\n");
  50. printf("migrate {basedir} {uid} Migrates to the account defined as the alias\n");
  51. printf("import_csv {basedir} {uid} Imports data from CSV files in the current directory\n");
  52. printf("import_list {basedir} {uid} {file} Imports a Mastodon CSV list file\n");
  53. printf("import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n");
  54. return 1;
  55. }
  56. char *get_argv(int *argi, int argc, char *argv[])
  57. {
  58. if (*argi < argc)
  59. return argv[(*argi)++];
  60. else
  61. return NULL;
  62. }
  63. #define GET_ARGV() get_argv(&argi, argc, argv)
  64. int main(int argc, char *argv[])
  65. {
  66. char *cmd;
  67. char *basedir;
  68. char *user;
  69. char *url;
  70. int argi = 1;
  71. snac snac;
  72. /* ensure group has write access */
  73. umask(0007);
  74. if ((cmd = GET_ARGV()) == NULL)
  75. return usage();
  76. if (strcmp(cmd, "init") == 0) { /** **/
  77. /* initialize the data storage */
  78. /* ... */
  79. basedir = GET_ARGV();
  80. return snac_init(basedir);
  81. }
  82. if (strcmp(cmd, "upgrade") == 0) { /** **/
  83. int ret;
  84. /* upgrade */
  85. if ((basedir = GET_ARGV()) == NULL)
  86. return usage();
  87. if ((ret = srv_open(basedir, 1)) == 1)
  88. srv_log(xs_dup("OK"));
  89. return ret;
  90. }
  91. if (strcmp(cmd, "markdown") == 0) { /** **/
  92. /* undocumented, for testing only */
  93. xs *c = xs_readall(stdin);
  94. xs *fc = not_really_markdown(c, NULL, NULL);
  95. printf("<html>\n%s\n</html>\n", fc);
  96. return 0;
  97. }
  98. if ((basedir = GET_ARGV()) == NULL)
  99. return usage();
  100. if (!srv_open(basedir, 0)) {
  101. srv_log(xs_fmt("error opening data storage at %s", basedir));
  102. return 1;
  103. }
  104. if (strcmp(cmd, "adduser") == 0) { /** **/
  105. user = GET_ARGV();
  106. return adduser(user);
  107. return 0;
  108. }
  109. if (strcmp(cmd, "httpd") == 0) { /** **/
  110. httpd();
  111. srv_free();
  112. return 0;
  113. }
  114. if (strcmp(cmd, "purge") == 0) { /** **/
  115. purge_all();
  116. return 0;
  117. }
  118. if (strcmp(cmd, "state") == 0) { /** **/
  119. xs *shm_name = NULL;
  120. srv_state *p_state = srv_state_op(&shm_name, 1);
  121. if (p_state == NULL)
  122. return 1;
  123. srv_state ss = *p_state;
  124. int n;
  125. printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
  126. xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
  127. printf("uptime: %s\n", uptime);
  128. printf("job fifo size (cur): %d\n", ss.job_fifo_size);
  129. printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
  130. char *th_states[] = { "stopped", "waiting", "input", "output" };
  131. for (n = 0; n < ss.n_threads; n++)
  132. printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
  133. return 0;
  134. }
  135. if ((user = GET_ARGV()) == NULL)
  136. return usage();
  137. if (strcmp(cmd, "block") == 0) { /** **/
  138. int ret = instance_block(user);
  139. if (ret < 0) {
  140. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  141. return 1;
  142. }
  143. return 0;
  144. }
  145. if (strcmp(cmd, "unblock") == 0) { /** **/
  146. int ret = instance_unblock(user);
  147. if (ret < 0) {
  148. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. if (strcmp(cmd, "webfinger") == 0) { /** **/
  154. xs *actor = NULL;
  155. xs *uid = NULL;
  156. int status;
  157. status = webfinger_request(user, &actor, &uid);
  158. printf("status: %d\n", status);
  159. if (actor != NULL)
  160. printf("actor: %s\n", actor);
  161. if (uid != NULL)
  162. printf("uid: %s\n", uid);
  163. return 0;
  164. }
  165. if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
  166. /* query an actor without user (non-signed) */
  167. xs *actor = NULL;
  168. int status;
  169. status = actor_request(NULL, user, &actor);
  170. printf("status: %d\n", status);
  171. if (valid_status(status)) {
  172. xs_json_dump(actor, 4, stdout);
  173. printf("\n");
  174. }
  175. return 0;
  176. }
  177. if (!user_open(&snac, user)) {
  178. printf("invalid user '%s'\n", user);
  179. return 1;
  180. }
  181. lastlog_write(&snac, "cmdline");
  182. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  183. return resetpwd(&snac);
  184. }
  185. if (strcmp(cmd, "deluser") == 0) { /** **/
  186. return deluser(&snac);
  187. }
  188. if (strcmp(cmd, "update") == 0) { /** **/
  189. xs *a_msg = msg_actor(&snac);
  190. xs *u_msg = msg_update(&snac, a_msg);
  191. enqueue_message(&snac, u_msg);
  192. return 0;
  193. }
  194. if (strcmp(cmd, "queue") == 0) { /** **/
  195. process_user_queue(&snac);
  196. return 0;
  197. }
  198. if (strcmp(cmd, "verify_links") == 0) { /** **/
  199. verify_links(&snac);
  200. return 0;
  201. }
  202. if (strcmp(cmd, "timeline") == 0) { /** **/
  203. #if 0
  204. xs *list = local_list(&snac, XS_ALL);
  205. xs *body = html_timeline(&snac, list, 1);
  206. printf("%s\n", body);
  207. user_free(&snac);
  208. srv_free();
  209. #endif
  210. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  211. xs *list = index_list_desc(idx, 0, 256);
  212. xs *tl = timeline_top_level(&snac, list);
  213. xs_json_dump(tl, 4, stdout);
  214. return 0;
  215. }
  216. if (strcmp(cmd, "export_csv") == 0) { /** **/
  217. export_csv(&snac);
  218. return 0;
  219. }
  220. if (strcmp(cmd, "import_csv") == 0) { /** **/
  221. import_csv(&snac);
  222. return 0;
  223. }
  224. if (strcmp(cmd, "migrate") == 0) { /** **/
  225. return migrate_account(&snac);
  226. }
  227. if ((url = GET_ARGV()) == NULL)
  228. return usage();
  229. if (strcmp(cmd, "alias") == 0) { /** **/
  230. xs *actor = NULL;
  231. xs *uid = NULL;
  232. int status = HTTP_STATUS_OK;
  233. if (*url == '\0')
  234. actor = xs_dup("");
  235. else
  236. status = webfinger_request(url, &actor, &uid);
  237. if (valid_status(status)) {
  238. if (strcmp(actor, snac.actor) == 0) {
  239. snac_log(&snac, xs_fmt("You can't be your own alias"));
  240. return 1;
  241. }
  242. else {
  243. snac.config = xs_dict_set(snac.config, "alias", actor);
  244. snac.config = xs_dict_set(snac.config, "alias_raw", url);
  245. user_persist(&snac, 1);
  246. }
  247. }
  248. else {
  249. snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  255. xs *actor = NULL;
  256. xs *uid = NULL;
  257. int status;
  258. status = webfinger_request_signed(&snac, url, &actor, &uid);
  259. printf("status: %d\n", status);
  260. if (actor != NULL)
  261. printf("actor: %s\n", actor);
  262. if (uid != NULL)
  263. printf("uid: %s\n", uid);
  264. return 0;
  265. }
  266. if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
  267. xs *msg = msg_admiration(&snac, url, "Announce");
  268. if (msg != NULL) {
  269. enqueue_message(&snac, msg);
  270. if (dbglevel) {
  271. xs_json_dump(msg, 4, stdout);
  272. }
  273. }
  274. return 0;
  275. }
  276. if (strcmp(cmd, "unboost") == 0) { /** **/
  277. xs *msg = msg_repulsion(&snac, url, "Announce");
  278. if (msg != NULL) {
  279. enqueue_message(&snac, msg);
  280. if (dbglevel) {
  281. xs_json_dump(msg, 4, stdout);
  282. }
  283. }
  284. return 0;
  285. }
  286. if (strcmp(cmd, "follow") == 0) { /** **/
  287. xs *msg = msg_follow(&snac, url);
  288. if (msg != NULL) {
  289. const char *actor = xs_dict_get(msg, "object");
  290. following_add(&snac, actor, msg);
  291. enqueue_output_by_actor(&snac, msg, actor, 0);
  292. if (dbglevel) {
  293. xs_json_dump(msg, 4, stdout);
  294. }
  295. }
  296. return 0;
  297. }
  298. if (strcmp(cmd, "unfollow") == 0) { /** **/
  299. xs *object = NULL;
  300. if (valid_status(following_get(&snac, url, &object))) {
  301. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  302. following_del(&snac, url);
  303. enqueue_output_by_actor(&snac, msg, url, 0);
  304. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  305. }
  306. else
  307. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  308. return 0;
  309. }
  310. if (strcmp(cmd, "limit") == 0) { /** **/
  311. int ret;
  312. if (!following_check(&snac, url))
  313. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  314. else
  315. if ((ret = limit(&snac, url)) == 0)
  316. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  317. else
  318. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  319. return 0;
  320. }
  321. if (strcmp(cmd, "unlimit") == 0) { /** **/
  322. int ret;
  323. if (!following_check(&snac, url))
  324. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  325. else
  326. if ((ret = unlimit(&snac, url)) == 0)
  327. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  328. else
  329. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  330. return 0;
  331. }
  332. if (strcmp(cmd, "search") == 0) { /** **/
  333. int to;
  334. /* 'url' contains the regex */
  335. xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
  336. int c = 0;
  337. const char *v;
  338. /* print results as standalone links */
  339. while (xs_list_next(r, &v, &c)) {
  340. printf("%s/admin/p/%s\n", snac.actor, v);
  341. }
  342. return 0;
  343. }
  344. if (strcmp(cmd, "ping") == 0) { /** **/
  345. xs *actor_o = NULL;
  346. if (!xs_startswith(url, "https:/")) {
  347. /* try to resolve via webfinger */
  348. if (!valid_status(webfinger_request(url, &url, NULL))) {
  349. srv_log(xs_fmt("cannot resolve %s via webfinger", url));
  350. return 1;
  351. }
  352. }
  353. if (valid_status(actor_request(&snac, url, &actor_o))) {
  354. xs *msg = msg_ping(&snac, url);
  355. enqueue_output_by_actor(&snac, msg, url, 0);
  356. if (dbglevel) {
  357. xs_json_dump(msg, 4, stdout);
  358. }
  359. srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
  360. }
  361. else {
  362. srv_log(xs_fmt("Error getting actor %s", url));
  363. return 1;
  364. }
  365. return 0;
  366. }
  367. if (strcmp(cmd, "pin") == 0) { /** **/
  368. int ret = pin(&snac, url);
  369. if (ret < 0) {
  370. fprintf(stderr, "error pinning %s %d\n", url, ret);
  371. return 1;
  372. }
  373. return 0;
  374. }
  375. if (strcmp(cmd, "unpin") == 0) { /** **/
  376. int ret = unpin(&snac, url);
  377. if (ret < 0) {
  378. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  379. return 1;
  380. }
  381. return 0;
  382. }
  383. if (strcmp(cmd, "bookmark") == 0) { /** **/
  384. int ret = bookmark(&snac, url);
  385. if (ret < 0) {
  386. fprintf(stderr, "error bookmarking %s %d\n", url, ret);
  387. return 1;
  388. }
  389. return 0;
  390. }
  391. if (strcmp(cmd, "unbookmark") == 0) { /** **/
  392. int ret = unbookmark(&snac, url);
  393. if (ret < 0) {
  394. fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
  395. return 1;
  396. }
  397. return 0;
  398. }
  399. if (strcmp(cmd, "question") == 0) { /** **/
  400. int end_secs = 5 * 60;
  401. xs *opts = xs_split(url, ";");
  402. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  403. xs *c_msg = msg_create(&snac, msg);
  404. if (dbglevel) {
  405. xs_json_dump(c_msg, 4, stdout);
  406. }
  407. enqueue_message(&snac, c_msg);
  408. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  409. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  410. return 0;
  411. }
  412. if (strcmp(cmd, "request") == 0) { /** **/
  413. int status;
  414. xs *data = NULL;
  415. status = activitypub_request(&snac, url, &data);
  416. printf("status: %d\n", status);
  417. if (data != NULL) {
  418. xs_json_dump(data, 4, stdout);
  419. }
  420. return 0;
  421. }
  422. if (strcmp(cmd, "insert") == 0) { /** **/
  423. int status;
  424. xs *data = NULL;
  425. status = activitypub_request(&snac, url, &data);
  426. printf("status: %d\n", status);
  427. if (data != NULL) {
  428. xs_json_dump(data, 4, stdout);
  429. enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
  430. if (!timeline_here(&snac, url))
  431. timeline_add(&snac, url, data);
  432. else
  433. printf("Post %s already here\n", url);
  434. }
  435. return 0;
  436. }
  437. if (strcmp(cmd, "request2") == 0) { /** **/
  438. enqueue_object_request(&snac, url, 2);
  439. return 0;
  440. }
  441. if (strcmp(cmd, "actor") == 0) { /** **/
  442. int status;
  443. xs *data = NULL;
  444. status = actor_request(&snac, url, &data);
  445. printf("status: %d\n", status);
  446. if (valid_status(status)) {
  447. xs_json_dump(data, 4, stdout);
  448. }
  449. return 0;
  450. }
  451. if (strcmp(cmd, "import_list") == 0) { /** **/
  452. import_list_csv(&snac, url);
  453. return 0;
  454. }
  455. if (strcmp(cmd, "import_block_list") == 0) { /** **/
  456. import_blocked_accounts_csv(&snac, url);
  457. return 0;
  458. }
  459. if (strcmp(cmd, "note") == 0 || strcmp(cmd, "note_unlisted") == 0) { /** **/
  460. xs *content = NULL;
  461. xs *msg = NULL;
  462. xs *c_msg = NULL;
  463. xs *attl = xs_list_new();
  464. char *fn = NULL;
  465. /* iterate possible attachments */
  466. while ((fn = GET_ARGV())) {
  467. FILE *f;
  468. if ((f = fopen(fn, "rb")) != NULL) {
  469. /* get the file size and content */
  470. fseek(f, 0, SEEK_END);
  471. int sz = ftell(f);
  472. fseek(f, 0, SEEK_SET);
  473. xs *atc = xs_readall(f);
  474. fclose(f);
  475. char *ext = strrchr(fn, '.');
  476. xs *hash = xs_md5_hex(fn, strlen(fn));
  477. xs *id = xs_fmt("%s%s", hash, ext);
  478. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  479. /* store */
  480. static_put(&snac, id, atc, sz);
  481. xs *l = xs_list_new();
  482. l = xs_list_append(l, url);
  483. l = xs_list_append(l, ""); /* alt text */
  484. attl = xs_list_append(attl, l);
  485. }
  486. else
  487. fprintf(stderr, "Error opening '%s' as attachment\n", fn);
  488. }
  489. if (strcmp(url, "-e") == 0) {
  490. /* get the content from an editor */
  491. FILE *f;
  492. unlink("/tmp/snac-edit.txt");
  493. system("$EDITOR /tmp/snac-edit.txt");
  494. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  495. content = xs_readall(f);
  496. fclose(f);
  497. unlink("/tmp/snac-edit.txt");
  498. }
  499. else {
  500. printf("Nothing to send\n");
  501. return 1;
  502. }
  503. }
  504. else
  505. if (strcmp(url, "-") == 0) {
  506. /* get the content from stdin */
  507. content = xs_readall(stdin);
  508. }
  509. else
  510. content = xs_dup(url);
  511. msg = msg_note(&snac, content, NULL, NULL, attl, 0, getenv("LANG"));
  512. if (strcmp(cmd, "note_unlisted") == 0) {
  513. /* according to Mastodon, "unlisted" posts (now called "quiet public")
  514. has the public address as a cc instead of to, so toggle it */
  515. xs *to = xs_dup(xs_dict_get(msg, "to"));
  516. msg = xs_dict_set(msg, "cc", to);
  517. msg = xs_dict_set(msg, "to", xs_stock(XSTYPE_LIST));
  518. }
  519. c_msg = msg_create(&snac, msg);
  520. if (dbglevel) {
  521. xs_json_dump(c_msg, 4, stdout);
  522. }
  523. enqueue_message(&snac, c_msg);
  524. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  525. return 0;
  526. }
  527. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  528. return 1;
  529. }