main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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("purge {basedir} Purges old data\n");
  19. printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
  20. printf("queue {basedir} {uid} Processes a user queue\n");
  21. printf("follow {basedir} {uid} {actor} Follows an actor\n");
  22. printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  23. // printf("check {basedir} [{uid}] Checks the database\n");
  24. // printf("update {basedir} {uid} Sends a user update to followers\n");
  25. // printf("passwd {basedir} {uid} Sets the password for {uid}\n");
  26. // printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  27. // printf("mute {basedir} {uid} {actor} Mutes an actor\n");
  28. // printf("unmute {basedir} {uid} {actor} Unmutes an actor\n");
  29. // printf("like {basedir} {uid} {url} Likes an url\n");
  30. // printf("announce {basedir} {uid} {url} Announces (boosts) an url\n");
  31. // printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  32. printf("request {basedir} {uid} {url} Requests an object\n");
  33. printf("actor {basedir} {uid} {url} Requests an actor\n");
  34. printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  35. return 1;
  36. }
  37. char *get_argv(int *argi, int argc, char *argv[])
  38. {
  39. if (*argi < argc)
  40. return argv[(*argi)++];
  41. else
  42. return NULL;
  43. }
  44. #define GET_ARGV() get_argv(&argi, argc, argv)
  45. int main(int argc, char *argv[])
  46. {
  47. char *cmd;
  48. char *basedir;
  49. char *user;
  50. char *url;
  51. int argi = 1;
  52. snac snac;
  53. if ((cmd = GET_ARGV()) == NULL)
  54. return usage();
  55. if (strcmp(cmd, "init") == 0) {
  56. /* initialize the database */
  57. /* ... */
  58. basedir = GET_ARGV();
  59. return initdb(basedir);
  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, "adduser") == 0) {
  68. user = GET_ARGV();
  69. return adduser(user);
  70. return 0;
  71. }
  72. if (strcmp(cmd, "httpd") == 0) {
  73. httpd();
  74. return 0;
  75. }
  76. if (strcmp(cmd, "purge") == 0) {
  77. purge_all();
  78. return 0;
  79. }
  80. if ((user = GET_ARGV()) == NULL)
  81. return usage();
  82. if (strcmp(cmd, "webfinger") == 0) {
  83. xs *actor = NULL;
  84. xs *uid = NULL;
  85. int status;
  86. status = webfinger_request(user, &actor, &uid);
  87. printf("status: %d\n", status);
  88. if (actor != NULL)
  89. printf("actor: %s\n", actor);
  90. if (uid != NULL)
  91. printf("uid: %s\n", uid);
  92. return 0;
  93. }
  94. if (!user_open(&snac, user)) {
  95. printf("error in user '%s'\n", user);
  96. return 1;
  97. }
  98. if (strcmp(cmd, "queue") == 0) {
  99. process_queue(&snac);
  100. return 0;
  101. }
  102. if ((url = GET_ARGV()) == NULL)
  103. return usage();
  104. if (strcmp(cmd, "announce") == 0) {
  105. xs *msg = msg_admiration(&snac, url, "Announce");
  106. if (msg != NULL) {
  107. post(&snac, msg);
  108. if (dbglevel) {
  109. xs *j = xs_json_dumps_pp(msg, 4);
  110. printf("%s\n", j);
  111. }
  112. }
  113. return 0;
  114. }
  115. if (strcmp(cmd, "follow") == 0) {
  116. xs *msg = msg_follow(&snac, url);
  117. if (msg != NULL) {
  118. char *actor = xs_dict_get(msg, "object");
  119. following_add(&snac, actor, msg);
  120. enqueue_output(&snac, msg, actor, 0);
  121. if (dbglevel) {
  122. xs *j = xs_json_dumps_pp(msg, 4);
  123. printf("%s\n", j);
  124. }
  125. }
  126. return 0;
  127. }
  128. if (strcmp(cmd, "unfollow") == 0) {
  129. xs *object = NULL;
  130. if (valid_status(following_get(&snac, url, &object))) {
  131. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  132. following_del(&snac, url);
  133. enqueue_output(&snac, msg, url, 0);
  134. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  135. }
  136. else
  137. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  138. return 0;
  139. }
  140. if (strcmp(cmd, "request") == 0) {
  141. int status;
  142. xs *data = NULL;
  143. status = activitypub_request(&snac, url, &data);
  144. printf("status: %d\n", status);
  145. if (valid_status(status)) {
  146. xs *j = xs_json_dumps_pp(data, 4);
  147. printf("%s\n", j);
  148. }
  149. return 0;
  150. }
  151. if (strcmp(cmd, "actor") == 0) {
  152. int status;
  153. xs *data = NULL;
  154. status = actor_request(&snac, url, &data);
  155. printf("status: %d\n", status);
  156. if (valid_status(status)) {
  157. xs *j = xs_json_dumps_pp(data, 4);
  158. printf("%s\n", j);
  159. }
  160. return 0;
  161. }
  162. if (strcmp(cmd, "note") == 0) {
  163. xs *content = NULL;
  164. xs *msg = NULL;
  165. xs *c_msg = NULL;
  166. char *in_reply_to = GET_ARGV();
  167. if (strcmp(url, "-") == 0) {
  168. /* get the content from an editor */
  169. FILE *f;
  170. system("$EDITOR /tmp/snac-edit.txt");
  171. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  172. content = xs_readall(f);
  173. fclose(f);
  174. unlink("/tmp/snac-edit.txt");
  175. }
  176. else {
  177. printf("Nothing to send\n");
  178. return 1;
  179. }
  180. }
  181. else
  182. content = xs_dup(url);
  183. msg = msg_note(&snac, content, NULL, in_reply_to, NULL);
  184. c_msg = msg_create(&snac, msg);
  185. if (dbglevel) {
  186. xs *j = xs_json_dumps_pp(c_msg, 4);
  187. printf("%s\n", j);
  188. }
  189. post(&snac, c_msg);
  190. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  191. return 0;
  192. }
  193. return 0;
  194. }