main.c 5.8 KB

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