main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 grunfink - MIT license */
  3. #include "xs.h"
  4. #include "xs_encdec.h"
  5. #include "xs_json.h"
  6. #include "snac.h"
  7. int usage(void)
  8. {
  9. printf("snac - A simple, minimalistic ActivityPub instance\n");
  10. printf("Copyright (c) 2022 grunfink - MIT license\n");
  11. printf("\n");
  12. printf("Commands:\n");
  13. printf("\n");
  14. printf("init [{basedir}] Initializes the database\n");
  15. printf("httpd {basedir} Starts the HTTPD daemon\n");
  16. printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
  17. printf("queue {basedir} {uid} Processes a user queue\n");
  18. // printf("check {basedir} [{uid}] Checks the database\n");
  19. // printf("purge {basedir} [{uid}] Purges old data\n");
  20. // printf("adduser {basedir} [{uid}] Adds a new user\n");
  21. // printf("update {basedir} {uid} Sends a user update to followers\n");
  22. // printf("passwd {basedir} {uid} Sets the password for {uid}\n");
  23. // printf("follow {basedir} {uid} {actor} Follows an actor\n");
  24. // printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  25. // printf("mute {basedir} {uid} {actor} Mutes an actor\n");
  26. // printf("unmute {basedir} {uid} {actor} Unmutes an actor\n");
  27. // printf("like {basedir} {uid} {url} Likes an url\n");
  28. // printf("announce {basedir} {uid} {url} Announces (boosts) an url\n");
  29. // printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  30. printf("request {basedir} {uid} {url} Requests an object\n");
  31. printf("actor {basedir} {uid} {url} Requests an actor\n");
  32. return 1;
  33. }
  34. char *get_argv(int *argi, int argc, char *argv[])
  35. {
  36. if (*argi < argc)
  37. return argv[(*argi)++];
  38. else
  39. return NULL;
  40. }
  41. #define GET_ARGV() get_argv(&argi, argc, argv)
  42. int main(int argc, char *argv[])
  43. {
  44. char *cmd;
  45. char *basedir;
  46. char *user;
  47. char *url;
  48. int argi = 1;
  49. snac snac;
  50. if ((cmd = GET_ARGV()) == NULL)
  51. return usage();
  52. if (strcmp(cmd, "init") == 0) {
  53. /* initialize the database */
  54. /* ... */
  55. basedir = GET_ARGV();
  56. return 0;
  57. }
  58. if ((basedir = GET_ARGV()) == NULL)
  59. return usage();
  60. if (!srv_open(basedir)) {
  61. srv_log(xs_fmt("error opening database at %s", basedir));
  62. return 1;
  63. }
  64. if (strcmp(cmd, "httpd") == 0) {
  65. httpd();
  66. return 0;
  67. }
  68. if ((user = GET_ARGV()) == NULL)
  69. return usage();
  70. if (strcmp(cmd, "webfinger") == 0) {
  71. xs *actor = NULL;
  72. xs *uid = NULL;
  73. int status;
  74. status = webfinger_request(user, &actor, &uid);
  75. printf("status: %d\n", status);
  76. if (actor != NULL)
  77. printf("actor: %s\n", actor);
  78. if (uid != NULL)
  79. printf("uid: %s\n", uid);
  80. return 0;
  81. }
  82. if (!user_open(&snac, user)) {
  83. printf("error in user '%s'\n", user);
  84. return 1;
  85. }
  86. if (strcmp(cmd, "queue") == 0) {
  87. process_queue(&snac);
  88. return 0;
  89. }
  90. if ((url = GET_ARGV()) == NULL)
  91. return usage();
  92. if (strcmp(cmd, "announce") == 0) {
  93. xs *msg = msg_admiration(&snac, url, "Announce");
  94. if (msg != NULL) {
  95. post(&snac, msg);
  96. xs *j = xs_json_dumps_pp(msg, 4);
  97. printf("%s\n", j);
  98. }
  99. return 0;
  100. }
  101. if (strcmp(cmd, "request") == 0) {
  102. int status;
  103. xs *data = NULL;
  104. status = activitypub_request(&snac, url, &data);
  105. printf("status: %d\n", status);
  106. if (valid_status(status)) {
  107. xs *j = xs_json_dumps_pp(data, 4);
  108. printf("%s\n", j);
  109. }
  110. return 0;
  111. }
  112. if (strcmp(cmd, "actor") == 0) {
  113. int status;
  114. xs *data = NULL;
  115. status = actor_request(&snac, url, &data);
  116. printf("status: %d\n", status);
  117. return 0;
  118. }
  119. return 0;
  120. }
  121. #if 0
  122. {
  123. snac snac;
  124. printf("%s\n", tid(0));
  125. srv_open("/home/angel/lib/snac/comam.es/");
  126. user_open(&snac, "mike");
  127. xs *headers = xs_dict_new();
  128. int status;
  129. d_char *payload;
  130. int p_size;
  131. xs *response;
  132. response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
  133. headers, NULL, 0, &status, &payload, &p_size);
  134. {
  135. xs *j1 = xs_json_dumps_pp(response, 4);
  136. printf("response:\n%s\n", j1);
  137. printf("payload:\n%s\n", payload);
  138. }
  139. {
  140. xs *list = queue(&snac);
  141. char *p, *fn;
  142. p = list;
  143. while (xs_list_iter(&p, &fn)) {
  144. xs *obj;
  145. obj = dequeue(&snac, fn);
  146. printf("%s\n", xs_dict_get(obj, "actor"));
  147. }
  148. }
  149. #if 0
  150. {
  151. xs *list = follower_list(&snac);
  152. char *p, *obj;
  153. p = list;
  154. while (xs_list_iter(&p, &obj)) {
  155. char *actor = xs_dict_get(obj, "actor");
  156. printf("%s\n", actor);
  157. }
  158. }
  159. {
  160. xs *list = timeline_list(&snac);
  161. char *p, *fn;
  162. p = list;
  163. while (xs_list_iter(&p, &fn)) {
  164. xs *tle = timeline_get(&snac, fn);
  165. printf("%s\n", xs_dict_get(tle, "id"));
  166. }
  167. }
  168. {
  169. xs *list = user_list();
  170. char *p, *uid;
  171. p = list;
  172. while (xs_list_iter(&p, &uid)) {
  173. if (user_open(&snac, uid)) {
  174. printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
  175. user_free(&snac);
  176. }
  177. }
  178. }
  179. #endif
  180. return 0;
  181. }
  182. #endif