main.c 14 KB

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