main.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_json.h"
  6. #include "snac.h"
  7. #include <sys/stat.h>
  8. int usage(void)
  9. {
  10. printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  11. printf("Copyright (c) 2022 - 2023 grunfink / MIT license\n");
  12. printf("\n");
  13. printf("Commands:\n");
  14. printf("\n");
  15. printf("init [{basedir}] Initializes the data storage\n");
  16. printf("upgrade {basedir} Upgrade to a new version\n");
  17. printf("adduser {basedir} [{uid}] Adds a new user\n");
  18. printf("httpd {basedir} Starts the HTTPD daemon\n");
  19. printf("purge {basedir} Purges old data\n");
  20. printf("webfinger {basedir} {user} Queries about a @user@host or actor\n");
  21. printf("queue {basedir} {uid} Processes a user queue\n");
  22. printf("follow {basedir} {uid} {actor} Follows an actor\n");
  23. printf("unfollow {basedir} {uid} {actor} Unfollows an actor\n");
  24. printf("request {basedir} {uid} {url} Requests an object\n");
  25. printf("actor {basedir} {uid} {url} Requests an actor\n");
  26. printf("note {basedir} {uid} {'text'} Sends a note to followers\n");
  27. printf("resetpwd {basedir} {uid} Resets the password of a user\n");
  28. printf("ping {basedir} {uid} {actor} Pings an actor\n");
  29. return 1;
  30. }
  31. char *get_argv(int *argi, int argc, char *argv[])
  32. {
  33. if (*argi < argc)
  34. return argv[(*argi)++];
  35. else
  36. return NULL;
  37. }
  38. #define GET_ARGV() get_argv(&argi, argc, argv)
  39. d_char *html_timeline(snac *snac, char *list, int local);
  40. int main(int argc, char *argv[])
  41. {
  42. char *cmd;
  43. char *basedir;
  44. char *user;
  45. char *url;
  46. int argi = 1;
  47. snac snac;
  48. /* ensure group has write access */
  49. umask(0007);
  50. if ((cmd = GET_ARGV()) == NULL)
  51. return usage();
  52. if (strcmp(cmd, "init") == 0) {
  53. /* initialize the data storage */
  54. /* ... */
  55. basedir = GET_ARGV();
  56. return snac_init(basedir);
  57. }
  58. if (strcmp(cmd, "upgrade") == 0) {
  59. int ret;
  60. /* upgrade */
  61. if ((basedir = GET_ARGV()) == NULL)
  62. return usage();
  63. if ((ret = srv_open(basedir, 1)) == 1)
  64. srv_log(xs_dup("OK"));
  65. return ret;
  66. }
  67. if (strcmp(cmd, "markdown") == 0) {
  68. /* undocumented, for testing only */
  69. xs *c = xs_readall(stdin);
  70. xs *fc = not_really_markdown(c);
  71. printf("<html>\n%s\n</html>\n", fc);
  72. return 0;
  73. }
  74. if ((basedir = GET_ARGV()) == NULL)
  75. return usage();
  76. if (!srv_open(basedir, 0)) {
  77. srv_log(xs_fmt("error opening data storage at %s", basedir));
  78. return 1;
  79. }
  80. if (strcmp(cmd, "adduser") == 0) {
  81. user = GET_ARGV();
  82. return adduser(user);
  83. return 0;
  84. }
  85. if (strcmp(cmd, "httpd") == 0) {
  86. httpd();
  87. srv_free();
  88. return 0;
  89. }
  90. if (strcmp(cmd, "purge") == 0) {
  91. purge_all();
  92. return 0;
  93. }
  94. if ((user = GET_ARGV()) == NULL)
  95. return usage();
  96. if (strcmp(cmd, "webfinger") == 0) {
  97. xs *actor = NULL;
  98. xs *uid = NULL;
  99. int status;
  100. status = webfinger_request(user, &actor, &uid);
  101. printf("status: %d\n", status);
  102. if (actor != NULL)
  103. printf("actor: %s\n", actor);
  104. if (uid != NULL)
  105. printf("uid: %s\n", uid);
  106. return 0;
  107. }
  108. if (!user_open(&snac, user)) {
  109. printf("error in user '%s'\n", user);
  110. return 1;
  111. }
  112. lastlog_write(&snac, "cmdline");
  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 *actor_o = NULL;
  175. if (valid_status(actor_request(&snac, url, &actor_o))) {
  176. xs *msg = msg_ping(&snac, url);
  177. enqueue_output_by_actor(&snac, msg, url, 0);
  178. if (dbglevel) {
  179. xs *j = xs_json_dumps_pp(msg, 4);
  180. printf("%s\n", j);
  181. }
  182. }
  183. else {
  184. srv_log(xs_fmt("Error getting actor %s", url));
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. if (strcmp(cmd, "request") == 0) {
  190. int status;
  191. xs *data = NULL;
  192. status = activitypub_request(&snac, url, &data);
  193. printf("status: %d\n", status);
  194. if (valid_status(status)) {
  195. xs *j = xs_json_dumps_pp(data, 4);
  196. printf("%s\n", j);
  197. }
  198. return 0;
  199. }
  200. if (strcmp(cmd, "actor") == 0) {
  201. int status;
  202. xs *data = NULL;
  203. status = actor_request(&snac, url, &data);
  204. printf("status: %d\n", status);
  205. if (valid_status(status)) {
  206. xs *j = xs_json_dumps_pp(data, 4);
  207. printf("%s\n", j);
  208. }
  209. return 0;
  210. }
  211. if (strcmp(cmd, "note") == 0) {
  212. xs *content = NULL;
  213. xs *msg = NULL;
  214. xs *c_msg = NULL;
  215. char *in_reply_to = GET_ARGV();
  216. if (strcmp(url, "-e") == 0) {
  217. /* get the content from an editor */
  218. FILE *f;
  219. unlink("/tmp/snac-edit.txt");
  220. system("$EDITOR /tmp/snac-edit.txt");
  221. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  222. content = xs_readall(f);
  223. fclose(f);
  224. unlink("/tmp/snac-edit.txt");
  225. }
  226. else {
  227. printf("Nothing to send\n");
  228. return 1;
  229. }
  230. }
  231. else
  232. if (strcmp(url, "-") == 0) {
  233. /* get the content from stdin */
  234. content = xs_readall(stdin);
  235. }
  236. else
  237. content = xs_dup(url);
  238. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  239. c_msg = msg_create(&snac, msg);
  240. if (dbglevel) {
  241. xs *j = xs_json_dumps_pp(c_msg, 4);
  242. printf("%s\n", j);
  243. }
  244. enqueue_message(&snac, c_msg);
  245. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  246. return 0;
  247. }
  248. return 0;
  249. }