main.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_encdec.h"
  6. #include "xs_json.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 - 2023 grunfink / 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("httpd {basedir} Starts the HTTPD daemon\n");
  20. printf("purge {basedir} Purges old data\n");
  21. printf("webfinger {basedir} {user} Queries about a @user@host or actor\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. return 1;
  30. }
  31. char *get_argv(int *argi, int argc, char *argv[])
  32. {
  33. if (*argi < argc)
  34. return argv[(*argi)++];
  35. else
  36. return NULL;
  37. }
  38. #define GET_ARGV() get_argv(&argi, argc, argv)
  39. d_char *html_timeline(snac *snac, char *list, int local);
  40. int main(int argc, char *argv[])
  41. {
  42. char *cmd;
  43. char *basedir;
  44. char *user;
  45. char *url;
  46. int argi = 1;
  47. snac snac;
  48. /* ensure group has write access */
  49. umask(0007);
  50. if ((cmd = GET_ARGV()) == NULL)
  51. return usage();
  52. if (strcmp(cmd, "init") == 0) {
  53. /* initialize the data storage */
  54. /* ... */
  55. basedir = GET_ARGV();
  56. return snac_init(basedir);
  57. }
  58. if (strcmp(cmd, "upgrade") == 0) {
  59. int ret;
  60. /* upgrade */
  61. if ((basedir = GET_ARGV()) == NULL)
  62. return usage();
  63. if ((ret = srv_open(basedir, 1)) == 1)
  64. srv_log(xs_dup("OK"));
  65. return ret;
  66. }
  67. if (strcmp(cmd, "markdown") == 0) {
  68. /* undocumented, for testing only */
  69. xs *c = xs_readall(stdin);
  70. xs *fc = not_really_markdown(c);
  71. printf("<html>\n%s\n</html>\n", fc);
  72. return 0;
  73. }
  74. if ((basedir = GET_ARGV()) == NULL)
  75. return usage();
  76. if (!srv_open(basedir, 0)) {
  77. srv_log(xs_fmt("error opening data storage at %s", basedir));
  78. return 1;
  79. }
  80. if (strcmp(cmd, "adduser") == 0) {
  81. user = GET_ARGV();
  82. return adduser(user);
  83. return 0;
  84. }
  85. if (strcmp(cmd, "httpd") == 0) {
  86. httpd();
  87. srv_free();
  88. return 0;
  89. }
  90. if (strcmp(cmd, "purge") == 0) {
  91. purge_all();
  92. return 0;
  93. }
  94. if ((user = GET_ARGV()) == NULL)
  95. return usage();
  96. if (strcmp(cmd, "webfinger") == 0) {
  97. xs *actor = NULL;
  98. xs *uid = NULL;
  99. int status;
  100. status = webfinger_request(user, &actor, &uid);
  101. printf("status: %d\n", status);
  102. if (actor != NULL)
  103. printf("actor: %s\n", actor);
  104. if (uid != NULL)
  105. printf("uid: %s\n", uid);
  106. return 0;
  107. }
  108. if (!user_open(&snac, user)) {
  109. printf("error in user '%s'\n", user);
  110. return 1;
  111. }
  112. if (strcmp(cmd, "resetpwd") == 0) {
  113. return resetpwd(&snac);
  114. }
  115. if (strcmp(cmd, "queue") == 0) {
  116. process_user_queue(&snac);
  117. return 0;
  118. }
  119. if (strcmp(cmd, "timeline") == 0) {
  120. #if 0
  121. xs *list = local_list(&snac, XS_ALL);
  122. xs *body = html_timeline(&snac, list, 1);
  123. printf("%s\n", body);
  124. user_free(&snac);
  125. srv_free();
  126. #endif
  127. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  128. xs *list = index_list_desc(idx, 0, 256);
  129. xs *tl = timeline_top_level(&snac, list);
  130. xs *j = xs_json_dumps_pp(tl, 4);
  131. printf("%s\n", j);
  132. return 0;
  133. }
  134. if ((url = GET_ARGV()) == NULL)
  135. return usage();
  136. if (strcmp(cmd, "announce") == 0) {
  137. xs *msg = msg_admiration(&snac, url, "Announce");
  138. if (msg != NULL) {
  139. enqueue_message(&snac, msg);
  140. if (dbglevel) {
  141. xs *j = xs_json_dumps_pp(msg, 4);
  142. printf("%s\n", j);
  143. }
  144. }
  145. return 0;
  146. }
  147. if (strcmp(cmd, "follow") == 0) {
  148. xs *msg = msg_follow(&snac, url);
  149. if (msg != NULL) {
  150. char *actor = xs_dict_get(msg, "object");
  151. following_add(&snac, actor, msg);
  152. enqueue_output_by_actor(&snac, msg, actor, 0);
  153. if (dbglevel) {
  154. xs *j = xs_json_dumps_pp(msg, 4);
  155. printf("%s\n", j);
  156. }
  157. }
  158. return 0;
  159. }
  160. if (strcmp(cmd, "unfollow") == 0) {
  161. xs *object = NULL;
  162. if (valid_status(following_get(&snac, url, &object))) {
  163. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  164. following_del(&snac, url);
  165. enqueue_output_by_actor(&snac, msg, url, 0);
  166. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  167. }
  168. else
  169. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  170. return 0;
  171. }
  172. if (strcmp(cmd, "request") == 0) {
  173. int status;
  174. xs *data = NULL;
  175. status = activitypub_request(&snac, url, &data);
  176. printf("status: %d\n", status);
  177. if (valid_status(status)) {
  178. xs *j = xs_json_dumps_pp(data, 4);
  179. printf("%s\n", j);
  180. }
  181. return 0;
  182. }
  183. if (strcmp(cmd, "actor") == 0) {
  184. int status;
  185. xs *data = NULL;
  186. status = actor_request(&snac, url, &data);
  187. printf("status: %d\n", status);
  188. if (valid_status(status)) {
  189. xs *j = xs_json_dumps_pp(data, 4);
  190. printf("%s\n", j);
  191. }
  192. return 0;
  193. }
  194. if (strcmp(cmd, "note") == 0) {
  195. xs *content = NULL;
  196. xs *msg = NULL;
  197. xs *c_msg = NULL;
  198. char *in_reply_to = GET_ARGV();
  199. if (strcmp(url, "-e") == 0) {
  200. /* get the content from an editor */
  201. FILE *f;
  202. unlink("/tmp/snac-edit.txt");
  203. system("$EDITOR /tmp/snac-edit.txt");
  204. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  205. content = xs_readall(f);
  206. fclose(f);
  207. unlink("/tmp/snac-edit.txt");
  208. }
  209. else {
  210. printf("Nothing to send\n");
  211. return 1;
  212. }
  213. }
  214. else
  215. if (strcmp(url, "-") == 0) {
  216. /* get the content from stdin */
  217. content = xs_readall(stdin);
  218. }
  219. else
  220. content = xs_dup(url);
  221. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  222. c_msg = msg_create(&snac, msg);
  223. if (dbglevel) {
  224. xs *j = xs_json_dumps_pp(c_msg, 4);
  225. printf("%s\n", j);
  226. }
  227. enqueue_message(&snac, c_msg);
  228. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  229. return 0;
  230. }
  231. return 0;
  232. }