main.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 *content = NULL;
  128. xs *msg = NULL;
  129. xs *c_msg = NULL;
  130. char *in_reply_to = GET_ARGV();
  131. if (strcmp(url, "-") == 0) {
  132. /* get the content from an editor */
  133. FILE *f;
  134. system("$EDITOR /tmp/snac-edit.txt");
  135. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  136. content = xs_readall(f);
  137. fclose(f);
  138. unlink("/tmp/snac-edit.txt");
  139. }
  140. else {
  141. printf("Nothing to send\n");
  142. return 1;
  143. }
  144. }
  145. else
  146. content = xs_dup(url);
  147. msg = msg_note(&snac, content, NULL, in_reply_to);
  148. c_msg = msg_create(&snac, msg);
  149. {
  150. xs *j = xs_json_dumps_pp(c_msg, 4);
  151. printf("%s\n", j);
  152. }
  153. post(&snac, c_msg);
  154. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  155. return 0;
  156. }
  157. return 0;
  158. }
  159. #if 0
  160. {
  161. snac snac;
  162. printf("%s\n", tid(0));
  163. srv_open("/home/angel/lib/snac/comam.es/");
  164. user_open(&snac, "mike");
  165. xs *headers = xs_dict_new();
  166. int status;
  167. d_char *payload;
  168. int p_size;
  169. xs *response;
  170. response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
  171. headers, NULL, 0, &status, &payload, &p_size);
  172. {
  173. xs *j1 = xs_json_dumps_pp(response, 4);
  174. printf("response:\n%s\n", j1);
  175. printf("payload:\n%s\n", payload);
  176. }
  177. {
  178. xs *list = queue(&snac);
  179. char *p, *fn;
  180. p = list;
  181. while (xs_list_iter(&p, &fn)) {
  182. xs *obj;
  183. obj = dequeue(&snac, fn);
  184. printf("%s\n", xs_dict_get(obj, "actor"));
  185. }
  186. }
  187. #if 0
  188. {
  189. xs *list = follower_list(&snac);
  190. char *p, *obj;
  191. p = list;
  192. while (xs_list_iter(&p, &obj)) {
  193. char *actor = xs_dict_get(obj, "actor");
  194. printf("%s\n", actor);
  195. }
  196. }
  197. {
  198. xs *list = timeline_list(&snac);
  199. char *p, *fn;
  200. p = list;
  201. while (xs_list_iter(&p, &fn)) {
  202. xs *tle = timeline_get(&snac, fn);
  203. printf("%s\n", xs_dict_get(tle, "id"));
  204. }
  205. }
  206. {
  207. xs *list = user_list();
  208. char *p, *uid;
  209. p = list;
  210. while (xs_list_iter(&p, &uid)) {
  211. if (user_open(&snac, uid)) {
  212. printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
  213. user_free(&snac);
  214. }
  215. }
  216. }
  217. #endif
  218. return 0;
  219. }
  220. #endif