main.c 6.7 KB

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