main.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 - 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("httpd {basedir} Starts the HTTPD daemon\n");
  17. printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
  18. printf("queue {basedir} {uid} Processes a user queue\n");
  19. // printf("check {basedir} [{uid}] Checks the database\n");
  20. // printf("purge {basedir} [{uid}] Purges old data\n");
  21. // printf("adduser {basedir} [{uid}] Adds a new user\n");
  22. // printf("update {basedir} {uid} Sends a user update to followers\n");
  23. // printf("passwd {basedir} {uid} Sets the password for {uid}\n");
  24. // printf("follow {basedir} {uid} {actor} Follows an actor\n");
  25. // printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  26. // printf("mute {basedir} {uid} {actor} Mutes an actor\n");
  27. // printf("unmute {basedir} {uid} {actor} Unmutes an actor\n");
  28. // printf("like {basedir} {uid} {url} Likes an url\n");
  29. // printf("announce {basedir} {uid} {url} Announces (boosts) an url\n");
  30. // printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  31. printf("request {basedir} {uid} {url} Requests an object\n");
  32. printf("actor {basedir} {uid} {url} Requests an actor\n");
  33. printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  34. return 1;
  35. }
  36. char *get_argv(int *argi, int argc, char *argv[])
  37. {
  38. if (*argi < argc)
  39. return argv[(*argi)++];
  40. else
  41. return NULL;
  42. }
  43. #define GET_ARGV() get_argv(&argi, argc, argv)
  44. int main(int argc, char *argv[])
  45. {
  46. char *cmd;
  47. char *basedir;
  48. char *user;
  49. char *url;
  50. int argi = 1;
  51. snac snac;
  52. if ((cmd = GET_ARGV()) == NULL)
  53. return usage();
  54. if (strcmp(cmd, "init") == 0) {
  55. /* initialize the database */
  56. /* ... */
  57. basedir = GET_ARGV();
  58. return 0;
  59. }
  60. if ((basedir = GET_ARGV()) == NULL)
  61. return usage();
  62. if (!srv_open(basedir)) {
  63. srv_log(xs_fmt("error opening database at %s", basedir));
  64. return 1;
  65. }
  66. if (strcmp(cmd, "httpd") == 0) {
  67. httpd();
  68. return 0;
  69. }
  70. if ((user = GET_ARGV()) == NULL)
  71. return usage();
  72. if (strcmp(cmd, "webfinger") == 0) {
  73. xs *actor = NULL;
  74. xs *uid = NULL;
  75. int status;
  76. status = webfinger_request(user, &actor, &uid);
  77. printf("status: %d\n", status);
  78. if (actor != NULL)
  79. printf("actor: %s\n", actor);
  80. if (uid != NULL)
  81. printf("uid: %s\n", uid);
  82. return 0;
  83. }
  84. if (!user_open(&snac, user)) {
  85. printf("error in user '%s'\n", user);
  86. return 1;
  87. }
  88. if (strcmp(cmd, "queue") == 0) {
  89. process_queue(&snac);
  90. return 0;
  91. }
  92. if ((url = GET_ARGV()) == NULL)
  93. return usage();
  94. if (strcmp(cmd, "announce") == 0) {
  95. xs *msg = msg_admiration(&snac, url, "Announce");
  96. if (msg != NULL) {
  97. post(&snac, msg);
  98. xs *j = xs_json_dumps_pp(msg, 4);
  99. printf("%s\n", j);
  100. }
  101. return 0;
  102. }
  103. if (strcmp(cmd, "request") == 0) {
  104. int status;
  105. xs *data = NULL;
  106. status = activitypub_request(&snac, url, &data);
  107. printf("status: %d\n", status);
  108. if (valid_status(status)) {
  109. xs *j = xs_json_dumps_pp(data, 4);
  110. printf("%s\n", j);
  111. }
  112. return 0;
  113. }
  114. if (strcmp(cmd, "actor") == 0) {
  115. int status;
  116. xs *data = NULL;
  117. status = actor_request(&snac, url, &data);
  118. printf("status: %d\n", status);
  119. if (valid_status(status)) {
  120. xs *j = xs_json_dumps_pp(data, 4);
  121. printf("%s\n", j);
  122. }
  123. return 0;
  124. }
  125. if (strcmp(cmd, "note") == 0) {
  126. int status;
  127. xs *data = NULL;
  128. xs *content = NULL;
  129. xs *f_content = NULL;
  130. if (strcmp(url, "-") == 0) {
  131. /* get the content from an editor */
  132. FILE *f;
  133. system("$EDITOR /tmp/snac-edit.txt");
  134. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  135. content = xs_readall(f);
  136. fclose(f);
  137. unlink("/tmp/snac-edit.txt");
  138. }
  139. else {
  140. printf("Nothing to send\n");
  141. return 1;
  142. }
  143. }
  144. else
  145. content = xs_dup(url);
  146. not_really_markdown(content, &f_content);
  147. printf("%s\n", f_content);
  148. return 0;
  149. }
  150. return 0;
  151. }
  152. #if 0
  153. {
  154. snac snac;
  155. printf("%s\n", tid(0));
  156. srv_open("/home/angel/lib/snac/comam.es/");
  157. user_open(&snac, "mike");
  158. xs *headers = xs_dict_new();
  159. int status;
  160. d_char *payload;
  161. int p_size;
  162. xs *response;
  163. response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
  164. headers, NULL, 0, &status, &payload, &p_size);
  165. {
  166. xs *j1 = xs_json_dumps_pp(response, 4);
  167. printf("response:\n%s\n", j1);
  168. printf("payload:\n%s\n", payload);
  169. }
  170. {
  171. xs *list = queue(&snac);
  172. char *p, *fn;
  173. p = list;
  174. while (xs_list_iter(&p, &fn)) {
  175. xs *obj;
  176. obj = dequeue(&snac, fn);
  177. printf("%s\n", xs_dict_get(obj, "actor"));
  178. }
  179. }
  180. #if 0
  181. {
  182. xs *list = follower_list(&snac);
  183. char *p, *obj;
  184. p = list;
  185. while (xs_list_iter(&p, &obj)) {
  186. char *actor = xs_dict_get(obj, "actor");
  187. printf("%s\n", actor);
  188. }
  189. }
  190. {
  191. xs *list = timeline_list(&snac);
  192. char *p, *fn;
  193. p = list;
  194. while (xs_list_iter(&p, &fn)) {
  195. xs *tle = timeline_get(&snac, fn);
  196. printf("%s\n", xs_dict_get(tle, "id"));
  197. }
  198. }
  199. {
  200. xs *list = user_list();
  201. char *p, *uid;
  202. p = list;
  203. while (xs_list_iter(&p, &uid)) {
  204. if (user_open(&snac, uid)) {
  205. printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
  206. user_free(&snac);
  207. }
  208. }
  209. }
  210. #endif
  211. return 0;
  212. }
  213. #endif