main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. lastlog_write(&snac, "cmdline");
  114. if (strcmp(cmd, "resetpwd") == 0) {
  115. return resetpwd(&snac);
  116. }
  117. if (strcmp(cmd, "queue") == 0) {
  118. process_user_queue(&snac);
  119. return 0;
  120. }
  121. if (strcmp(cmd, "timeline") == 0) {
  122. #if 0
  123. xs *list = local_list(&snac, XS_ALL);
  124. xs *body = html_timeline(&snac, list, 1);
  125. printf("%s\n", body);
  126. user_free(&snac);
  127. srv_free();
  128. #endif
  129. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  130. xs *list = index_list_desc(idx, 0, 256);
  131. xs *tl = timeline_top_level(&snac, list);
  132. xs *j = xs_json_dumps_pp(tl, 4);
  133. printf("%s\n", j);
  134. return 0;
  135. }
  136. if ((url = GET_ARGV()) == NULL)
  137. return usage();
  138. if (strcmp(cmd, "announce") == 0) {
  139. xs *msg = msg_admiration(&snac, url, "Announce");
  140. if (msg != NULL) {
  141. enqueue_message(&snac, msg);
  142. if (dbglevel) {
  143. xs *j = xs_json_dumps_pp(msg, 4);
  144. printf("%s\n", j);
  145. }
  146. }
  147. return 0;
  148. }
  149. if (strcmp(cmd, "follow") == 0) {
  150. xs *msg = msg_follow(&snac, url);
  151. if (msg != NULL) {
  152. char *actor = xs_dict_get(msg, "object");
  153. following_add(&snac, actor, msg);
  154. enqueue_output_by_actor(&snac, msg, actor, 0);
  155. if (dbglevel) {
  156. xs *j = xs_json_dumps_pp(msg, 4);
  157. printf("%s\n", j);
  158. }
  159. }
  160. return 0;
  161. }
  162. if (strcmp(cmd, "unfollow") == 0) {
  163. xs *object = NULL;
  164. if (valid_status(following_get(&snac, url, &object))) {
  165. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  166. following_del(&snac, url);
  167. enqueue_output_by_actor(&snac, msg, url, 0);
  168. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  169. }
  170. else
  171. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  172. return 0;
  173. }
  174. if (strcmp(cmd, "ping") == 0) {
  175. xs *actor_o = NULL;
  176. if (valid_status(actor_request(&snac, url, &actor_o))) {
  177. xs *msg = msg_ping(&snac, url);
  178. enqueue_output_by_actor(&snac, msg, url, 0);
  179. if (dbglevel) {
  180. xs *j = xs_json_dumps_pp(msg, 4);
  181. printf("%s\n", j);
  182. }
  183. }
  184. else {
  185. srv_log(xs_fmt("Error getting actor %s", url));
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. if (strcmp(cmd, "request") == 0) {
  191. int status;
  192. xs *data = NULL;
  193. status = activitypub_request(&snac, url, &data);
  194. printf("status: %d\n", status);
  195. if (valid_status(status)) {
  196. xs *j = xs_json_dumps_pp(data, 4);
  197. printf("%s\n", j);
  198. }
  199. return 0;
  200. }
  201. if (strcmp(cmd, "actor") == 0) {
  202. int status;
  203. xs *data = NULL;
  204. status = actor_request(&snac, url, &data);
  205. printf("status: %d\n", status);
  206. if (valid_status(status)) {
  207. xs *j = xs_json_dumps_pp(data, 4);
  208. printf("%s\n", j);
  209. }
  210. return 0;
  211. }
  212. if (strcmp(cmd, "note") == 0) {
  213. xs *content = NULL;
  214. xs *msg = NULL;
  215. xs *c_msg = NULL;
  216. char *in_reply_to = GET_ARGV();
  217. if (strcmp(url, "-e") == 0) {
  218. /* get the content from an editor */
  219. FILE *f;
  220. unlink("/tmp/snac-edit.txt");
  221. system("$EDITOR /tmp/snac-edit.txt");
  222. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  223. content = xs_readall(f);
  224. fclose(f);
  225. unlink("/tmp/snac-edit.txt");
  226. }
  227. else {
  228. printf("Nothing to send\n");
  229. return 1;
  230. }
  231. }
  232. else
  233. if (strcmp(url, "-") == 0) {
  234. /* get the content from stdin */
  235. content = xs_readall(stdin);
  236. }
  237. else
  238. content = xs_dup(url);
  239. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  240. c_msg = msg_create(&snac, msg);
  241. if (dbglevel) {
  242. xs *j = xs_json_dumps_pp(c_msg, 4);
  243. printf("%s\n", j);
  244. }
  245. enqueue_message(&snac, c_msg);
  246. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  247. return 0;
  248. }
  249. return 0;
  250. }