main.c 10 KB

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