main.c 11 KB

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