main.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. srv_free();
  75. return 0;
  76. }
  77. if (strcmp(cmd, "purge") == 0) {
  78. purge_all();
  79. return 0;
  80. }
  81. if ((user = GET_ARGV()) == NULL)
  82. return usage();
  83. if (strcmp(cmd, "webfinger") == 0) {
  84. xs *actor = NULL;
  85. xs *uid = NULL;
  86. int status;
  87. status = webfinger_request(user, &actor, &uid);
  88. printf("status: %d\n", status);
  89. if (actor != NULL)
  90. printf("actor: %s\n", actor);
  91. if (uid != NULL)
  92. printf("uid: %s\n", uid);
  93. return 0;
  94. }
  95. if (!user_open(&snac, user)) {
  96. printf("error in user '%s'\n", user);
  97. return 1;
  98. }
  99. if (strcmp(cmd, "queue") == 0) {
  100. process_queue(&snac);
  101. return 0;
  102. }
  103. if ((url = GET_ARGV()) == NULL)
  104. return usage();
  105. if (strcmp(cmd, "announce") == 0) {
  106. xs *msg = msg_admiration(&snac, url, "Announce");
  107. if (msg != NULL) {
  108. post(&snac, msg);
  109. if (dbglevel) {
  110. xs *j = xs_json_dumps_pp(msg, 4);
  111. printf("%s\n", j);
  112. }
  113. }
  114. return 0;
  115. }
  116. if (strcmp(cmd, "follow") == 0) {
  117. xs *msg = msg_follow(&snac, url);
  118. if (msg != NULL) {
  119. char *actor = xs_dict_get(msg, "object");
  120. following_add(&snac, actor, msg);
  121. enqueue_output(&snac, msg, actor, 0);
  122. if (dbglevel) {
  123. xs *j = xs_json_dumps_pp(msg, 4);
  124. printf("%s\n", j);
  125. }
  126. }
  127. return 0;
  128. }
  129. if (strcmp(cmd, "unfollow") == 0) {
  130. xs *object = NULL;
  131. if (valid_status(following_get(&snac, url, &object))) {
  132. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  133. following_del(&snac, url);
  134. enqueue_output(&snac, msg, url, 0);
  135. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  136. }
  137. else
  138. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  139. return 0;
  140. }
  141. if (strcmp(cmd, "request") == 0) {
  142. int status;
  143. xs *data = NULL;
  144. status = activitypub_request(&snac, url, &data);
  145. printf("status: %d\n", status);
  146. if (valid_status(status)) {
  147. xs *j = xs_json_dumps_pp(data, 4);
  148. printf("%s\n", j);
  149. }
  150. return 0;
  151. }
  152. if (strcmp(cmd, "actor") == 0) {
  153. int status;
  154. xs *data = NULL;
  155. status = actor_request(&snac, url, &data);
  156. printf("status: %d\n", status);
  157. if (valid_status(status)) {
  158. xs *j = xs_json_dumps_pp(data, 4);
  159. printf("%s\n", j);
  160. }
  161. return 0;
  162. }
  163. if (strcmp(cmd, "note") == 0) {
  164. xs *content = NULL;
  165. xs *msg = NULL;
  166. xs *c_msg = NULL;
  167. char *in_reply_to = GET_ARGV();
  168. if (strcmp(url, "-") == 0) {
  169. /* get the content from an editor */
  170. FILE *f;
  171. system("$EDITOR /tmp/snac-edit.txt");
  172. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  173. content = xs_readall(f);
  174. fclose(f);
  175. unlink("/tmp/snac-edit.txt");
  176. }
  177. else {
  178. printf("Nothing to send\n");
  179. return 1;
  180. }
  181. }
  182. else
  183. content = xs_dup(url);
  184. msg = msg_note(&snac, content, NULL, in_reply_to, NULL);
  185. c_msg = msg_create(&snac, msg);
  186. if (dbglevel) {
  187. xs *j = xs_json_dumps_pp(c_msg, 4);
  188. printf("%s\n", j);
  189. }
  190. post(&snac, c_msg);
  191. timeline_add(&snac, xs_dict_get(msg, "id"), msg, in_reply_to, NULL);
  192. return 0;
  193. }
  194. return 0;
  195. }