main.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. d_char *html_timeline(snac *snac, char *list, int local);
  46. int main(int argc, char *argv[])
  47. {
  48. char *cmd;
  49. char *basedir;
  50. char *user;
  51. char *url;
  52. int argi = 1;
  53. snac snac;
  54. if ((cmd = GET_ARGV()) == NULL)
  55. return usage();
  56. if (strcmp(cmd, "init") == 0) {
  57. /* initialize the database */
  58. /* ... */
  59. basedir = GET_ARGV();
  60. return initdb(basedir);
  61. }
  62. if (strcmp(cmd, "markdown") == 0) {
  63. /* undocumented, for testing only */
  64. xs *c = xs_readall(stdin);
  65. xs *fc = not_really_markdown(c);
  66. printf("<html>\n%s\n</html>\n", fc);
  67. return 0;
  68. }
  69. if ((basedir = GET_ARGV()) == NULL)
  70. return usage();
  71. if (!srv_open(basedir)) {
  72. srv_log(xs_fmt("error opening database at %s", basedir));
  73. return 1;
  74. }
  75. if (strcmp(cmd, "adduser") == 0) {
  76. user = GET_ARGV();
  77. return adduser(user);
  78. return 0;
  79. }
  80. if (strcmp(cmd, "httpd") == 0) {
  81. httpd();
  82. srv_free();
  83. return 0;
  84. }
  85. if (strcmp(cmd, "purge") == 0) {
  86. purge_all();
  87. return 0;
  88. }
  89. if ((user = GET_ARGV()) == NULL)
  90. return usage();
  91. if (strcmp(cmd, "webfinger") == 0) {
  92. xs *actor = NULL;
  93. xs *uid = NULL;
  94. int status;
  95. status = webfinger_request(user, &actor, &uid);
  96. printf("status: %d\n", status);
  97. if (actor != NULL)
  98. printf("actor: %s\n", actor);
  99. if (uid != NULL)
  100. printf("uid: %s\n", uid);
  101. return 0;
  102. }
  103. if (!user_open(&snac, user)) {
  104. printf("error in user '%s'\n", user);
  105. return 1;
  106. }
  107. if (strcmp(cmd, "queue") == 0) {
  108. process_queue(&snac);
  109. return 0;
  110. }
  111. if (strcmp(cmd, "timeline") == 0) {
  112. xs *list = local_list(&snac, 0xfffffff);
  113. xs *body = html_timeline(&snac, list, 1);
  114. printf("%s\n", body);
  115. user_free(&snac);
  116. srv_free();
  117. return 0;
  118. }
  119. if ((url = GET_ARGV()) == NULL)
  120. return usage();
  121. if (strcmp(cmd, "announce") == 0) {
  122. xs *msg = msg_admiration(&snac, url, "Announce");
  123. if (msg != NULL) {
  124. post(&snac, msg);
  125. if (dbglevel) {
  126. xs *j = xs_json_dumps_pp(msg, 4);
  127. printf("%s\n", j);
  128. }
  129. }
  130. return 0;
  131. }
  132. if (strcmp(cmd, "follow") == 0) {
  133. xs *msg = msg_follow(&snac, url);
  134. if (msg != NULL) {
  135. char *actor = xs_dict_get(msg, "object");
  136. following_add(&snac, actor, msg);
  137. enqueue_output(&snac, msg, actor, 0);
  138. if (dbglevel) {
  139. xs *j = xs_json_dumps_pp(msg, 4);
  140. printf("%s\n", j);
  141. }
  142. }
  143. return 0;
  144. }
  145. if (strcmp(cmd, "unfollow") == 0) {
  146. xs *object = NULL;
  147. if (valid_status(following_get(&snac, url, &object))) {
  148. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  149. following_del(&snac, url);
  150. enqueue_output(&snac, msg, url, 0);
  151. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  152. }
  153. else
  154. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  155. return 0;
  156. }
  157. if (strcmp(cmd, "request") == 0) {
  158. int status;
  159. xs *data = NULL;
  160. status = activitypub_request(&snac, url, &data);
  161. printf("status: %d\n", status);
  162. if (valid_status(status)) {
  163. xs *j = xs_json_dumps_pp(data, 4);
  164. printf("%s\n", j);
  165. }
  166. return 0;
  167. }
  168. if (strcmp(cmd, "actor") == 0) {
  169. int status;
  170. xs *data = NULL;
  171. status = actor_request(&snac, url, &data);
  172. printf("status: %d\n", status);
  173. if (valid_status(status)) {
  174. xs *j = xs_json_dumps_pp(data, 4);
  175. printf("%s\n", j);
  176. }
  177. return 0;
  178. }
  179. if (strcmp(cmd, "note") == 0) {
  180. xs *content = NULL;
  181. xs *msg = NULL;
  182. xs *c_msg = NULL;
  183. char *in_reply_to = GET_ARGV();
  184. if (strcmp(url, "-") == 0) {
  185. /* get the content from an editor */
  186. FILE *f;
  187. system("$EDITOR /tmp/snac-edit.txt");
  188. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  189. content = xs_readall(f);
  190. fclose(f);
  191. unlink("/tmp/snac-edit.txt");
  192. }
  193. else {
  194. printf("Nothing to send\n");
  195. return 1;
  196. }
  197. }
  198. else
  199. content = xs_dup(url);
  200. msg = msg_note(&snac, content, NULL, in_reply_to, NULL);
  201. c_msg = msg_create(&snac, msg);
  202. if (dbglevel) {
  203. xs *j = xs_json_dumps_pp(c_msg, 4);
  204. printf("%s\n", j);
  205. }
  206. post(&snac, c_msg);
  207. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  208. return 0;
  209. }
  210. return 0;
  211. }