main.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 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. #include <sys/stat.h>
  9. int usage(void)
  10. {
  11. printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  12. printf("Copyright (c) 2022 - 2023 grunfink / MIT license\n");
  13. printf("\n");
  14. printf("Commands:\n");
  15. printf("\n");
  16. printf("init [{basedir}] Initializes the data storage\n");
  17. printf("upgrade {basedir} Upgrade to a new version\n");
  18. printf("adduser {basedir} [{uid}] Adds a new user\n");
  19. printf("httpd {basedir} Starts the HTTPD daemon\n");
  20. printf("purge {basedir} Purges old data\n");
  21. printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
  22. printf("queue {basedir} {uid} Processes a user queue\n");
  23. printf("follow {basedir} {uid} {actor} Follows an actor\n");
  24. printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  25. printf("request {basedir} {uid} {url} Requests an object\n");
  26. printf("actor {basedir} {uid} {url} Requests an actor\n");
  27. printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  28. printf("resetpwd {basedir} {uid} Resets the password of a user\n");
  29. printf("ping {basedir} {uid} {actor} Pings an actor\n");
  30. return 1;
  31. }
  32. char *get_argv(int *argi, int argc, char *argv[])
  33. {
  34. if (*argi < argc)
  35. return argv[(*argi)++];
  36. else
  37. return NULL;
  38. }
  39. #define GET_ARGV() get_argv(&argi, argc, argv)
  40. d_char *html_timeline(snac *snac, char *list, int local);
  41. int main(int argc, char *argv[])
  42. {
  43. char *cmd;
  44. char *basedir;
  45. char *user;
  46. char *url;
  47. int argi = 1;
  48. snac snac;
  49. /* ensure group has write access */
  50. umask(0007);
  51. if ((cmd = GET_ARGV()) == NULL)
  52. return usage();
  53. if (strcmp(cmd, "init") == 0) {
  54. /* initialize the data storage */
  55. /* ... */
  56. basedir = GET_ARGV();
  57. return snac_init(basedir);
  58. }
  59. if (strcmp(cmd, "upgrade") == 0) {
  60. int ret;
  61. /* upgrade */
  62. if ((basedir = GET_ARGV()) == NULL)
  63. return usage();
  64. if ((ret = srv_open(basedir, 1)) == 1)
  65. srv_log(xs_dup("OK"));
  66. return ret;
  67. }
  68. if (strcmp(cmd, "markdown") == 0) {
  69. /* undocumented, for testing only */
  70. xs *c = xs_readall(stdin);
  71. xs *fc = not_really_markdown(c);
  72. printf("<html>\n%s\n</html>\n", fc);
  73. return 0;
  74. }
  75. if ((basedir = GET_ARGV()) == NULL)
  76. return usage();
  77. if (!srv_open(basedir, 0)) {
  78. srv_log(xs_fmt("error opening data storage at %s", basedir));
  79. return 1;
  80. }
  81. if (strcmp(cmd, "adduser") == 0) {
  82. user = GET_ARGV();
  83. return adduser(user);
  84. return 0;
  85. }
  86. if (strcmp(cmd, "httpd") == 0) {
  87. httpd();
  88. srv_free();
  89. return 0;
  90. }
  91. if (strcmp(cmd, "purge") == 0) {
  92. purge_all();
  93. return 0;
  94. }
  95. if ((user = GET_ARGV()) == NULL)
  96. return usage();
  97. if (strcmp(cmd, "webfinger") == 0) {
  98. xs *actor = NULL;
  99. xs *uid = NULL;
  100. int status;
  101. status = webfinger_request(user, &actor, &uid);
  102. printf("status: %d\n", status);
  103. if (actor != NULL)
  104. printf("actor: %s\n", actor);
  105. if (uid != NULL)
  106. printf("uid: %s\n", uid);
  107. return 0;
  108. }
  109. if (!user_open(&snac, user)) {
  110. printf("error in user '%s'\n", user);
  111. return 1;
  112. }
  113. if (strcmp(cmd, "resetpwd") == 0) {
  114. return resetpwd(&snac);
  115. }
  116. if (strcmp(cmd, "queue") == 0) {
  117. process_user_queue(&snac);
  118. return 0;
  119. }
  120. if (strcmp(cmd, "timeline") == 0) {
  121. #if 0
  122. xs *list = local_list(&snac, XS_ALL);
  123. xs *body = html_timeline(&snac, list, 1);
  124. printf("%s\n", body);
  125. user_free(&snac);
  126. srv_free();
  127. #endif
  128. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  129. xs *list = index_list_desc(idx, 0, 256);
  130. xs *tl = timeline_top_level(&snac, list);
  131. xs *j = xs_json_dumps_pp(tl, 4);
  132. printf("%s\n", j);
  133. return 0;
  134. }
  135. if ((url = GET_ARGV()) == NULL)
  136. return usage();
  137. if (strcmp(cmd, "announce") == 0) {
  138. xs *msg = msg_admiration(&snac, url, "Announce");
  139. if (msg != NULL) {
  140. enqueue_message(&snac, msg);
  141. if (dbglevel) {
  142. xs *j = xs_json_dumps_pp(msg, 4);
  143. printf("%s\n", j);
  144. }
  145. }
  146. return 0;
  147. }
  148. if (strcmp(cmd, "follow") == 0) {
  149. xs *msg = msg_follow(&snac, url);
  150. if (msg != NULL) {
  151. char *actor = xs_dict_get(msg, "object");
  152. following_add(&snac, actor, msg);
  153. enqueue_output_by_actor(&snac, msg, actor, 0);
  154. if (dbglevel) {
  155. xs *j = xs_json_dumps_pp(msg, 4);
  156. printf("%s\n", j);
  157. }
  158. }
  159. return 0;
  160. }
  161. if (strcmp(cmd, "unfollow") == 0) {
  162. xs *object = NULL;
  163. if (valid_status(following_get(&snac, url, &object))) {
  164. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  165. following_del(&snac, url);
  166. enqueue_output_by_actor(&snac, msg, url, 0);
  167. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  168. }
  169. else
  170. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  171. return 0;
  172. }
  173. if (strcmp(cmd, "ping") == 0) {
  174. xs *msg = msg_ping(&snac, url);
  175. enqueue_output_by_actor(&snac, msg, url, 0);
  176. if (dbglevel) {
  177. xs *j = xs_json_dumps_pp(msg, 4);
  178. printf("%s\n", msg);
  179. }
  180. return 0;
  181. }
  182. if (strcmp(cmd, "request") == 0) {
  183. int status;
  184. xs *data = NULL;
  185. status = activitypub_request(&snac, url, &data);
  186. printf("status: %d\n", status);
  187. if (valid_status(status)) {
  188. xs *j = xs_json_dumps_pp(data, 4);
  189. printf("%s\n", j);
  190. }
  191. return 0;
  192. }
  193. if (strcmp(cmd, "actor") == 0) {
  194. int status;
  195. xs *data = NULL;
  196. status = actor_request(&snac, url, &data);
  197. printf("status: %d\n", status);
  198. if (valid_status(status)) {
  199. xs *j = xs_json_dumps_pp(data, 4);
  200. printf("%s\n", j);
  201. }
  202. return 0;
  203. }
  204. if (strcmp(cmd, "note") == 0) {
  205. xs *content = NULL;
  206. xs *msg = NULL;
  207. xs *c_msg = NULL;
  208. char *in_reply_to = GET_ARGV();
  209. if (strcmp(url, "-e") == 0) {
  210. /* get the content from an editor */
  211. FILE *f;
  212. unlink("/tmp/snac-edit.txt");
  213. system("$EDITOR /tmp/snac-edit.txt");
  214. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  215. content = xs_readall(f);
  216. fclose(f);
  217. unlink("/tmp/snac-edit.txt");
  218. }
  219. else {
  220. printf("Nothing to send\n");
  221. return 1;
  222. }
  223. }
  224. else
  225. if (strcmp(url, "-") == 0) {
  226. /* get the content from stdin */
  227. content = xs_readall(stdin);
  228. }
  229. else
  230. content = xs_dup(url);
  231. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  232. c_msg = msg_create(&snac, msg);
  233. if (dbglevel) {
  234. xs *j = xs_json_dumps_pp(c_msg, 4);
  235. printf("%s\n", j);
  236. }
  237. enqueue_message(&snac, c_msg);
  238. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  239. return 0;
  240. }
  241. return 0;
  242. }