main.c 13 KB

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