main.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 " VERSION " - 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. initdb(basedir);
  59. return 0;
  60. }
  61. if ((basedir = GET_ARGV()) == NULL)
  62. return usage();
  63. if (!srv_open(basedir)) {
  64. srv_log(xs_fmt("error opening database at %s", basedir));
  65. return 1;
  66. }
  67. if (strcmp(cmd, "httpd") == 0) {
  68. httpd();
  69. return 0;
  70. }
  71. if ((user = GET_ARGV()) == NULL)
  72. return usage();
  73. if (strcmp(cmd, "webfinger") == 0) {
  74. xs *actor = NULL;
  75. xs *uid = NULL;
  76. int status;
  77. status = webfinger_request(user, &actor, &uid);
  78. printf("status: %d\n", status);
  79. if (actor != NULL)
  80. printf("actor: %s\n", actor);
  81. if (uid != NULL)
  82. printf("uid: %s\n", uid);
  83. return 0;
  84. }
  85. if (!user_open(&snac, user)) {
  86. printf("error in user '%s'\n", user);
  87. return 1;
  88. }
  89. if (strcmp(cmd, "queue") == 0) {
  90. process_queue(&snac);
  91. return 0;
  92. }
  93. if ((url = GET_ARGV()) == NULL)
  94. return usage();
  95. if (strcmp(cmd, "announce") == 0) {
  96. xs *msg = msg_admiration(&snac, url, "Announce");
  97. if (msg != NULL) {
  98. post(&snac, msg);
  99. if (dbglevel) {
  100. xs *j = xs_json_dumps_pp(msg, 4);
  101. printf("%s\n", j);
  102. }
  103. }
  104. return 0;
  105. }
  106. if (strcmp(cmd, "follow") == 0) {
  107. xs *msg = msg_follow(&snac, url);
  108. if (msg != NULL) {
  109. char *actor = xs_dict_get(msg, "object");
  110. following_add(&snac, actor, msg);
  111. enqueue_output(&snac, msg, actor, 0);
  112. if (dbglevel) {
  113. xs *j = xs_json_dumps_pp(msg, 4);
  114. printf("%s\n", j);
  115. }
  116. }
  117. return 0;
  118. }
  119. if (strcmp(cmd, "request") == 0) {
  120. int status;
  121. xs *data = NULL;
  122. status = activitypub_request(&snac, url, &data);
  123. printf("status: %d\n", status);
  124. if (valid_status(status)) {
  125. xs *j = xs_json_dumps_pp(data, 4);
  126. printf("%s\n", j);
  127. }
  128. return 0;
  129. }
  130. if (strcmp(cmd, "actor") == 0) {
  131. int status;
  132. xs *data = NULL;
  133. status = actor_request(&snac, url, &data);
  134. printf("status: %d\n", status);
  135. if (valid_status(status)) {
  136. xs *j = xs_json_dumps_pp(data, 4);
  137. printf("%s\n", j);
  138. }
  139. return 0;
  140. }
  141. if (strcmp(cmd, "note") == 0) {
  142. xs *content = NULL;
  143. xs *msg = NULL;
  144. xs *c_msg = NULL;
  145. char *in_reply_to = GET_ARGV();
  146. if (strcmp(url, "-") == 0) {
  147. /* get the content from an editor */
  148. FILE *f;
  149. system("$EDITOR /tmp/snac-edit.txt");
  150. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  151. content = xs_readall(f);
  152. fclose(f);
  153. unlink("/tmp/snac-edit.txt");
  154. }
  155. else {
  156. printf("Nothing to send\n");
  157. return 1;
  158. }
  159. }
  160. else
  161. content = xs_dup(url);
  162. msg = msg_note(&snac, content, NULL, in_reply_to);
  163. c_msg = msg_create(&snac, msg);
  164. if (dbglevel) {
  165. xs *j = xs_json_dumps_pp(c_msg, 4);
  166. printf("%s\n", j);
  167. }
  168. post(&snac, c_msg);
  169. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  170. return 0;
  171. }
  172. return 0;
  173. }