main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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("follow {basedir} {uid} {actor} Follows an actor\n");
  20. // printf("check {basedir} [{uid}] Checks the database\n");
  21. // printf("purge {basedir} [{uid}] Purges old data\n");
  22. // printf("adduser {basedir} [{uid}] Adds a new user\n");
  23. // printf("update {basedir} {uid} Sends a user update to followers\n");
  24. // printf("passwd {basedir} {uid} Sets the password for {uid}\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. if (dbglevel) {
  99. xs *j = xs_json_dumps_pp(msg, 4);
  100. printf("%s\n", j);
  101. }
  102. }
  103. return 0;
  104. }
  105. if (strcmp(cmd, "follow") == 0) {
  106. xs *msg = msg_follow(&snac, url);
  107. if (msg != NULL) {
  108. char *actor = xs_dict_get(msg, "object");
  109. following_add(&snac, actor, msg);
  110. enqueue_output(&snac, msg, actor, 0);
  111. if (dbglevel) {
  112. xs *j = xs_json_dumps_pp(msg, 4);
  113. printf("%s\n", j);
  114. }
  115. }
  116. return 0;
  117. }
  118. if (strcmp(cmd, "request") == 0) {
  119. int status;
  120. xs *data = NULL;
  121. status = activitypub_request(&snac, url, &data);
  122. printf("status: %d\n", status);
  123. if (valid_status(status)) {
  124. xs *j = xs_json_dumps_pp(data, 4);
  125. printf("%s\n", j);
  126. }
  127. return 0;
  128. }
  129. if (strcmp(cmd, "actor") == 0) {
  130. int status;
  131. xs *data = NULL;
  132. status = actor_request(&snac, url, &data);
  133. printf("status: %d\n", status);
  134. if (valid_status(status)) {
  135. xs *j = xs_json_dumps_pp(data, 4);
  136. printf("%s\n", j);
  137. }
  138. return 0;
  139. }
  140. if (strcmp(cmd, "note") == 0) {
  141. xs *content = NULL;
  142. xs *msg = NULL;
  143. xs *c_msg = NULL;
  144. char *in_reply_to = GET_ARGV();
  145. if (strcmp(url, "-") == 0) {
  146. /* get the content from an editor */
  147. FILE *f;
  148. system("$EDITOR /tmp/snac-edit.txt");
  149. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  150. content = xs_readall(f);
  151. fclose(f);
  152. unlink("/tmp/snac-edit.txt");
  153. }
  154. else {
  155. printf("Nothing to send\n");
  156. return 1;
  157. }
  158. }
  159. else
  160. content = xs_dup(url);
  161. msg = msg_note(&snac, content, NULL, in_reply_to);
  162. c_msg = msg_create(&snac, msg);
  163. if (dbglevel) {
  164. xs *j = xs_json_dumps_pp(c_msg, 4);
  165. printf("%s\n", j);
  166. }
  167. post(&snac, c_msg);
  168. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  169. return 0;
  170. }
  171. return 0;
  172. }
  173. #if 0
  174. {
  175. snac snac;
  176. printf("%s\n", tid(0));
  177. srv_open("/home/angel/lib/snac/comam.es/");
  178. user_open(&snac, "mike");
  179. xs *headers = xs_dict_new();
  180. int status;
  181. d_char *payload;
  182. int p_size;
  183. xs *response;
  184. response = http_signed_request(&snac, "GET", "https://mastodon.social/users/VictorMoral",
  185. headers, NULL, 0, &status, &payload, &p_size);
  186. {
  187. xs *j1 = xs_json_dumps_pp(response, 4);
  188. printf("response:\n%s\n", j1);
  189. printf("payload:\n%s\n", payload);
  190. }
  191. {
  192. xs *list = queue(&snac);
  193. char *p, *fn;
  194. p = list;
  195. while (xs_list_iter(&p, &fn)) {
  196. xs *obj;
  197. obj = dequeue(&snac, fn);
  198. printf("%s\n", xs_dict_get(obj, "actor"));
  199. }
  200. }
  201. #if 0
  202. {
  203. xs *list = follower_list(&snac);
  204. char *p, *obj;
  205. p = list;
  206. while (xs_list_iter(&p, &obj)) {
  207. char *actor = xs_dict_get(obj, "actor");
  208. printf("%s\n", actor);
  209. }
  210. }
  211. {
  212. xs *list = timeline_list(&snac);
  213. char *p, *fn;
  214. p = list;
  215. while (xs_list_iter(&p, &fn)) {
  216. xs *tle = timeline_get(&snac, fn);
  217. printf("%s\n", xs_dict_get(tle, "id"));
  218. }
  219. }
  220. {
  221. xs *list = user_list();
  222. char *p, *uid;
  223. p = list;
  224. while (xs_list_iter(&p, &uid)) {
  225. if (user_open(&snac, uid)) {
  226. printf("%s (%s)\n", uid, xs_dict_get(snac.config, "name"));
  227. user_free(&snac);
  228. }
  229. }
  230. }
  231. #endif
  232. return 0;
  233. }
  234. #endif