main.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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} {actor} Queries about an actor (@user@host or actor url)\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. printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n");
  30. printf("pin {basedir} {uid} {msg_url} Pins a message\n");
  31. printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
  32. printf("block {basedir} {instance_url} Blocks a full instance\n");
  33. printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
  34. /* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\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. /* ensure group has write access */
  55. umask(0007);
  56. if ((cmd = GET_ARGV()) == NULL)
  57. return usage();
  58. if (strcmp(cmd, "init") == 0) { /** **/
  59. /* initialize the data storage */
  60. /* ... */
  61. basedir = GET_ARGV();
  62. return snac_init(basedir);
  63. }
  64. if (strcmp(cmd, "upgrade") == 0) { /** **/
  65. int ret;
  66. /* upgrade */
  67. if ((basedir = GET_ARGV()) == NULL)
  68. return usage();
  69. if ((ret = srv_open(basedir, 1)) == 1)
  70. srv_log(xs_dup("OK"));
  71. return ret;
  72. }
  73. if (strcmp(cmd, "markdown") == 0) { /** **/
  74. /* undocumented, for testing only */
  75. xs *c = xs_readall(stdin);
  76. xs *fc = not_really_markdown(c, NULL);
  77. printf("<html>\n%s\n</html>\n", fc);
  78. return 0;
  79. }
  80. if ((basedir = GET_ARGV()) == NULL)
  81. return usage();
  82. if (!srv_open(basedir, 0)) {
  83. srv_log(xs_fmt("error opening data storage at %s", basedir));
  84. return 1;
  85. }
  86. if (strcmp(cmd, "adduser") == 0) { /** **/
  87. user = GET_ARGV();
  88. return adduser(user);
  89. return 0;
  90. }
  91. if (strcmp(cmd, "httpd") == 0) { /** **/
  92. httpd();
  93. srv_free();
  94. return 0;
  95. }
  96. if (strcmp(cmd, "purge") == 0) { /** **/
  97. purge_all();
  98. return 0;
  99. }
  100. if ((user = GET_ARGV()) == NULL)
  101. return usage();
  102. if (strcmp(cmd, "block") == 0) { /** **/
  103. int ret = instance_block(user);
  104. if (ret < 0) {
  105. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  106. return 1;
  107. }
  108. return 0;
  109. }
  110. if (strcmp(cmd, "unblock") == 0) { /** **/
  111. int ret = instance_unblock(user);
  112. if (ret < 0) {
  113. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  114. return 1;
  115. }
  116. return 0;
  117. }
  118. if (strcmp(cmd, "webfinger") == 0) { /** **/
  119. xs *actor = NULL;
  120. xs *uid = NULL;
  121. int status;
  122. status = webfinger_request(user, &actor, &uid);
  123. printf("status: %d\n", status);
  124. if (actor != NULL)
  125. printf("actor: %s\n", actor);
  126. if (uid != NULL)
  127. printf("uid: %s\n", uid);
  128. return 0;
  129. }
  130. if (!user_open(&snac, user)) {
  131. printf("error in user '%s'\n", user);
  132. return 1;
  133. }
  134. lastlog_write(&snac, "cmdline");
  135. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  136. return resetpwd(&snac);
  137. }
  138. if (strcmp(cmd, "queue") == 0) { /** **/
  139. process_user_queue(&snac);
  140. return 0;
  141. }
  142. if (strcmp(cmd, "timeline") == 0) { /** **/
  143. #if 0
  144. xs *list = local_list(&snac, XS_ALL);
  145. xs *body = html_timeline(&snac, list, 1);
  146. printf("%s\n", body);
  147. user_free(&snac);
  148. srv_free();
  149. #endif
  150. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  151. xs *list = index_list_desc(idx, 0, 256);
  152. xs *tl = timeline_top_level(&snac, list);
  153. xs *j = xs_json_dumps_pp(tl, 4);
  154. printf("%s\n", j);
  155. return 0;
  156. }
  157. if ((url = GET_ARGV()) == NULL)
  158. return usage();
  159. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  160. xs *actor = NULL;
  161. xs *uid = NULL;
  162. int status;
  163. status = webfinger_request_signed(&snac, url, &actor, &uid);
  164. printf("status: %d\n", status);
  165. if (actor != NULL)
  166. printf("actor: %s\n", actor);
  167. if (uid != NULL)
  168. printf("uid: %s\n", uid);
  169. return 0;
  170. }
  171. if (strcmp(cmd, "announce") == 0) { /** **/
  172. xs *msg = msg_admiration(&snac, url, "Announce");
  173. if (msg != NULL) {
  174. enqueue_message(&snac, msg);
  175. if (dbglevel) {
  176. xs *j = xs_json_dumps_pp(msg, 4);
  177. printf("%s\n", j);
  178. }
  179. }
  180. return 0;
  181. }
  182. if (strcmp(cmd, "follow") == 0) { /** **/
  183. xs *msg = msg_follow(&snac, url);
  184. if (msg != NULL) {
  185. char *actor = xs_dict_get(msg, "object");
  186. following_add(&snac, actor, msg);
  187. enqueue_output_by_actor(&snac, msg, actor, 0);
  188. if (dbglevel) {
  189. xs *j = xs_json_dumps_pp(msg, 4);
  190. printf("%s\n", j);
  191. }
  192. }
  193. return 0;
  194. }
  195. if (strcmp(cmd, "unfollow") == 0) { /** **/
  196. xs *object = NULL;
  197. if (valid_status(following_get(&snac, url, &object))) {
  198. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  199. following_del(&snac, url);
  200. enqueue_output_by_actor(&snac, msg, url, 0);
  201. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  202. }
  203. else
  204. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  205. return 0;
  206. }
  207. if (strcmp(cmd, "ping") == 0) { /** **/
  208. xs *actor_o = NULL;
  209. if (valid_status(actor_request(&snac, url, &actor_o))) {
  210. xs *msg = msg_ping(&snac, url);
  211. enqueue_output_by_actor(&snac, msg, url, 0);
  212. if (dbglevel) {
  213. xs *j = xs_json_dumps_pp(msg, 4);
  214. printf("%s\n", j);
  215. }
  216. }
  217. else {
  218. srv_log(xs_fmt("Error getting actor %s", url));
  219. return 1;
  220. }
  221. return 0;
  222. }
  223. if (strcmp(cmd, "pin") == 0) { /** **/
  224. int ret = pin(&snac, url);
  225. if (ret < 0) {
  226. fprintf(stderr, "error pinning %s %d\n", url, ret);
  227. return 1;
  228. }
  229. return 0;
  230. }
  231. if (strcmp(cmd, "unpin") == 0) { /** **/
  232. int ret = unpin(&snac, url);
  233. if (ret < 0) {
  234. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  235. return 1;
  236. }
  237. return 0;
  238. }
  239. if (strcmp(cmd, "question") == 0) { /** **/
  240. int end_secs = 5 * 60;
  241. xs *opts = xs_split(url, ";");
  242. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  243. xs *c_msg = msg_create(&snac, msg);
  244. if (dbglevel) {
  245. xs *j = xs_json_dumps_pp(c_msg, 4);
  246. printf("%s\n", j);
  247. }
  248. enqueue_message(&snac, c_msg);
  249. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  250. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  251. return 0;
  252. }
  253. if (strcmp(cmd, "request") == 0) { /** **/
  254. int status;
  255. xs *data = NULL;
  256. status = activitypub_request(&snac, url, &data);
  257. printf("status: %d\n", status);
  258. if (data != NULL) {
  259. xs *j = xs_json_dumps_pp(data, 4);
  260. printf("%s\n", j);
  261. }
  262. return 0;
  263. }
  264. if (strcmp(cmd, "actor") == 0) { /** **/
  265. int status;
  266. xs *data = NULL;
  267. status = actor_request(&snac, url, &data);
  268. printf("status: %d\n", status);
  269. if (valid_status(status)) {
  270. xs *j = xs_json_dumps_pp(data, 4);
  271. printf("%s\n", j);
  272. }
  273. return 0;
  274. }
  275. if (strcmp(cmd, "note") == 0) { /** **/
  276. xs *content = NULL;
  277. xs *msg = NULL;
  278. xs *c_msg = NULL;
  279. char *in_reply_to = GET_ARGV();
  280. if (strcmp(url, "-e") == 0) {
  281. /* get the content from an editor */
  282. FILE *f;
  283. unlink("/tmp/snac-edit.txt");
  284. system("$EDITOR /tmp/snac-edit.txt");
  285. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  286. content = xs_readall(f);
  287. fclose(f);
  288. unlink("/tmp/snac-edit.txt");
  289. }
  290. else {
  291. printf("Nothing to send\n");
  292. return 1;
  293. }
  294. }
  295. else
  296. if (strcmp(url, "-") == 0) {
  297. /* get the content from stdin */
  298. content = xs_readall(stdin);
  299. }
  300. else
  301. content = xs_dup(url);
  302. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  303. c_msg = msg_create(&snac, msg);
  304. if (dbglevel) {
  305. xs *j = xs_json_dumps_pp(c_msg, 4);
  306. printf("%s\n", j);
  307. }
  308. enqueue_message(&snac, c_msg);
  309. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  310. return 0;
  311. }
  312. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  313. return 1;
  314. }