main.c 17 KB

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