1
0

main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. {
  95. xs *j = xs_json_dumps_pp(msg, 4);
  96. printf("%s\n", j);
  97. }
  98. return 0;
  99. }
  100. if (strcmp(cmd, "request") == 0) {
  101. int status;
  102. xs *data = NULL;
  103. status = activitypub_request(&snac, url, &data);
  104. printf("status: %d\n", status);
  105. if (valid_status(status)) {
  106. xs *j = xs_json_dumps_pp(data, 4);
  107. printf("%s\n", j);
  108. }
  109. return 0;
  110. }
  111. if (strcmp(cmd, "actor") == 0) {
  112. int status;
  113. xs *data = NULL;
  114. status = actor_request(&snac, url, &data);
  115. printf("status: %d\n", status);
  116. return 0;
  117. }
  118. return 0;
  119. }
  120. #if 0
  121. {
  122. snac snac;
  123. printf("%s\n", tid(0));
  124. srv_open("/home/angel/lib/snac/comam.es/");
  125. user_open(&snac, "mike");
  126. xs *headers = xs_dict_new();
  127. int status;
  128. d_char *payload;
  129. int p_size;
  130. xs *response;
  131. response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
  132. headers, NULL, 0, &status, &payload, &p_size);
  133. {
  134. xs *j1 = xs_json_dumps_pp(response, 4);
  135. printf("response:\n%s\n", j1);
  136. printf("payload:\n%s\n", payload);
  137. }
  138. {
  139. xs *list = queue(&snac);
  140. char *p, *fn;
  141. p = list;
  142. while (xs_list_iter(&p, &fn)) {
  143. xs *obj;
  144. obj = dequeue(&snac, fn);
  145. printf("%s\n", xs_dict_get(obj, "actor"));
  146. }
  147. }
  148. #if 0
  149. {
  150. xs *list = follower_list(&snac);
  151. char *p, *obj;
  152. p = list;
  153. while (xs_list_iter(&p, &obj)) {
  154. char *actor = xs_dict_get(obj, "actor");
  155. printf("%s\n", actor);
  156. }
  157. }
  158. {
  159. xs *list = timeline_list(&snac);
  160. char *p, *fn;
  161. p = list;
  162. while (xs_list_iter(&p, &fn)) {
  163. xs *tle = timeline_get(&snac, fn);
  164. printf("%s\n", xs_dict_get(tle, "id"));
  165. }
  166. }
  167. {
  168. xs *list = user_list();
  169. char *p, *uid;
  170. p = list;
  171. while (xs_list_iter(&p, &uid)) {
  172. if (user_open(&snac, uid)) {
  173. printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
  174. user_free(&snac);
  175. }
  176. }
  177. }
  178. #endif
  179. return 0;
  180. }
  181. #endif