main.c 7.0 KB

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