main.c 11 KB

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