main.c 13 KB

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