main.c 9.8 KB

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