main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / 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 - 2024 grunfink et al. / 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("deluser {basedir} {uid} Deletes a user\n");
  19. printf("httpd {basedir} Starts the HTTPD daemon\n");
  20. printf("purge {basedir} Purges old data\n");
  21. printf("webfinger {basedir} {actor} Queries about an actor (@user@host or actor url)\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. printf("webfinger_s {basedir} {uid} {actor} Queries about an actor (@user@host or actor url)\n");
  31. printf("pin {basedir} {uid} {msg_url} Pins a message\n");
  32. printf("unpin {basedir} {uid} {msg_url} Unpins a message\n");
  33. printf("block {basedir} {instance_url} Blocks a full instance\n");
  34. printf("unblock {basedir} {instance_url} Unblocks a full instance\n");
  35. printf("limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n");
  36. printf("unlimit {basedir} {uid} {actor} Unlimits an actor\n");
  37. /* printf("question {basedir} {uid} 'opts' Generates a poll (;-separated opts)\n");*/
  38. return 1;
  39. }
  40. char *get_argv(int *argi, int argc, char *argv[])
  41. {
  42. if (*argi < argc)
  43. return argv[(*argi)++];
  44. else
  45. return NULL;
  46. }
  47. #define GET_ARGV() get_argv(&argi, argc, argv)
  48. #include "xs_html.h"
  49. xs_html *html_note(snac *user, char *summary,
  50. char *div_id, char *form_id,
  51. char *ta_plh, char *ta_content,
  52. char *edit_id, char *actor_id,
  53. xs_val *cw_yn, char *cw_text,
  54. xs_val *mnt_only, char *redir,
  55. char *in_reply_to, int poll);
  56. int main(int argc, char *argv[])
  57. {
  58. char *cmd;
  59. char *basedir;
  60. char *user;
  61. char *url;
  62. int argi = 1;
  63. snac snac;
  64. /* ensure group has write access */
  65. umask(0007);
  66. if ((cmd = GET_ARGV()) == NULL)
  67. return usage();
  68. if (strcmp(cmd, "init") == 0) { /** **/
  69. /* initialize the data storage */
  70. /* ... */
  71. basedir = GET_ARGV();
  72. return snac_init(basedir);
  73. }
  74. if (strcmp(cmd, "upgrade") == 0) { /** **/
  75. int ret;
  76. /* upgrade */
  77. if ((basedir = GET_ARGV()) == NULL)
  78. return usage();
  79. if ((ret = srv_open(basedir, 1)) == 1)
  80. srv_log(xs_dup("OK"));
  81. return ret;
  82. }
  83. if (strcmp(cmd, "markdown") == 0) { /** **/
  84. /* undocumented, for testing only */
  85. xs *c = xs_readall(stdin);
  86. xs *fc = not_really_markdown(c, NULL);
  87. printf("<html>\n%s\n</html>\n", fc);
  88. return 0;
  89. }
  90. if ((basedir = GET_ARGV()) == NULL)
  91. return usage();
  92. if (!srv_open(basedir, 0)) {
  93. srv_log(xs_fmt("error opening data storage at %s", basedir));
  94. return 1;
  95. }
  96. if (strcmp(cmd, "adduser") == 0) { /** **/
  97. user = GET_ARGV();
  98. return adduser(user);
  99. return 0;
  100. }
  101. if (strcmp(cmd, "httpd") == 0) { /** **/
  102. httpd();
  103. srv_free();
  104. return 0;
  105. }
  106. if (strcmp(cmd, "purge") == 0) { /** **/
  107. purge_all();
  108. return 0;
  109. }
  110. if ((user = GET_ARGV()) == NULL)
  111. return usage();
  112. if (strcmp(cmd, "block") == 0) { /** **/
  113. int ret = instance_block(user);
  114. if (ret < 0) {
  115. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  116. return 1;
  117. }
  118. return 0;
  119. }
  120. if (strcmp(cmd, "unblock") == 0) { /** **/
  121. int ret = instance_unblock(user);
  122. if (ret < 0) {
  123. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  124. return 1;
  125. }
  126. return 0;
  127. }
  128. if (strcmp(cmd, "webfinger") == 0) { /** **/
  129. xs *actor = NULL;
  130. xs *uid = NULL;
  131. int status;
  132. status = webfinger_request(user, &actor, &uid);
  133. printf("status: %d\n", status);
  134. if (actor != NULL)
  135. printf("actor: %s\n", actor);
  136. if (uid != NULL)
  137. printf("uid: %s\n", uid);
  138. return 0;
  139. }
  140. if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
  141. /* query an actor without user (non-signed) */
  142. xs *actor = NULL;
  143. int status;
  144. status = actor_request(NULL, user, &actor);
  145. printf("status: %d\n", status);
  146. if (valid_status(status)) {
  147. xs_json_dump(actor, 4, stdout);
  148. printf("\n");
  149. }
  150. return 0;
  151. }
  152. if (!user_open(&snac, user)) {
  153. printf("invalid user '%s'\n", user);
  154. return 1;
  155. }
  156. lastlog_write(&snac, "cmdline");
  157. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  158. return resetpwd(&snac);
  159. }
  160. if (strcmp(cmd, "deluser") == 0) { /** **/
  161. return deluser(&snac);
  162. }
  163. if (strcmp(cmd, "update") == 0) { /** **/
  164. xs *a_msg = msg_actor(&snac);
  165. xs *u_msg = msg_update(&snac, a_msg);
  166. enqueue_message(&snac, u_msg);
  167. return 0;
  168. }
  169. if (strcmp(cmd, "queue") == 0) { /** **/
  170. process_user_queue(&snac);
  171. return 0;
  172. }
  173. if (strcmp(cmd, "timeline") == 0) { /** **/
  174. #if 0
  175. xs *list = local_list(&snac, XS_ALL);
  176. xs *body = html_timeline(&snac, list, 1);
  177. printf("%s\n", body);
  178. user_free(&snac);
  179. srv_free();
  180. #endif
  181. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  182. xs *list = index_list_desc(idx, 0, 256);
  183. xs *tl = timeline_top_level(&snac, list);
  184. xs_json_dump(tl, 4, stdout);
  185. return 0;
  186. }
  187. if ((url = GET_ARGV()) == NULL)
  188. return usage();
  189. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  190. xs *actor = NULL;
  191. xs *uid = NULL;
  192. int status;
  193. status = webfinger_request_signed(&snac, url, &actor, &uid);
  194. printf("status: %d\n", status);
  195. if (actor != NULL)
  196. printf("actor: %s\n", actor);
  197. if (uid != NULL)
  198. printf("uid: %s\n", uid);
  199. return 0;
  200. }
  201. if (strcmp(cmd, "announce") == 0) { /** **/
  202. xs *msg = msg_admiration(&snac, url, "Announce");
  203. if (msg != NULL) {
  204. enqueue_message(&snac, msg);
  205. if (dbglevel) {
  206. xs_json_dump(msg, 4, stdout);
  207. }
  208. }
  209. return 0;
  210. }
  211. if (strcmp(cmd, "follow") == 0) { /** **/
  212. xs *msg = msg_follow(&snac, url);
  213. if (msg != NULL) {
  214. char *actor = xs_dict_get(msg, "object");
  215. following_add(&snac, actor, msg);
  216. enqueue_output_by_actor(&snac, msg, actor, 0);
  217. if (dbglevel) {
  218. xs_json_dump(msg, 4, stdout);
  219. }
  220. }
  221. return 0;
  222. }
  223. if (strcmp(cmd, "unfollow") == 0) { /** **/
  224. xs *object = NULL;
  225. if (valid_status(following_get(&snac, url, &object))) {
  226. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  227. following_del(&snac, url);
  228. enqueue_output_by_actor(&snac, msg, url, 0);
  229. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  230. }
  231. else
  232. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  233. return 0;
  234. }
  235. if (strcmp(cmd, "limit") == 0) { /** **/
  236. int ret;
  237. if (!following_check(&snac, url))
  238. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  239. else
  240. if ((ret = limit(&snac, url)) == 0)
  241. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  242. else
  243. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  244. return 0;
  245. }
  246. if (strcmp(cmd, "unlimit") == 0) { /** **/
  247. int ret;
  248. if (!following_check(&snac, url))
  249. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  250. else
  251. if ((ret = unlimit(&snac, url)) == 0)
  252. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  253. else
  254. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  255. return 0;
  256. }
  257. if (strcmp(cmd, "ping") == 0) { /** **/
  258. xs *actor_o = NULL;
  259. if (valid_status(actor_request(&snac, url, &actor_o))) {
  260. xs *msg = msg_ping(&snac, url);
  261. enqueue_output_by_actor(&snac, msg, url, 0);
  262. if (dbglevel) {
  263. xs_json_dump(msg, 4, stdout);
  264. }
  265. }
  266. else {
  267. srv_log(xs_fmt("Error getting actor %s", url));
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. if (strcmp(cmd, "pin") == 0) { /** **/
  273. int ret = pin(&snac, url);
  274. if (ret < 0) {
  275. fprintf(stderr, "error pinning %s %d\n", url, ret);
  276. return 1;
  277. }
  278. return 0;
  279. }
  280. if (strcmp(cmd, "unpin") == 0) { /** **/
  281. int ret = unpin(&snac, url);
  282. if (ret < 0) {
  283. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  284. return 1;
  285. }
  286. return 0;
  287. }
  288. if (strcmp(cmd, "question") == 0) { /** **/
  289. int end_secs = 5 * 60;
  290. xs *opts = xs_split(url, ";");
  291. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  292. xs *c_msg = msg_create(&snac, msg);
  293. if (dbglevel) {
  294. xs_json_dump(c_msg, 4, stdout);
  295. }
  296. enqueue_message(&snac, c_msg);
  297. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  298. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  299. return 0;
  300. }
  301. if (strcmp(cmd, "request") == 0) { /** **/
  302. int status;
  303. xs *data = NULL;
  304. status = activitypub_request(&snac, url, &data);
  305. printf("status: %d\n", status);
  306. if (data != NULL) {
  307. xs_json_dump(data, 4, stdout);
  308. }
  309. return 0;
  310. }
  311. if (strcmp(cmd, "actor") == 0) { /** **/
  312. int status;
  313. xs *data = NULL;
  314. status = actor_request(&snac, url, &data);
  315. printf("status: %d\n", status);
  316. if (valid_status(status)) {
  317. xs_json_dump(data, 4, stdout);
  318. }
  319. return 0;
  320. }
  321. if (strcmp(cmd, "note") == 0) { /** **/
  322. xs *content = NULL;
  323. xs *msg = NULL;
  324. xs *c_msg = NULL;
  325. char *in_reply_to = GET_ARGV();
  326. if (strcmp(url, "-e") == 0) {
  327. /* get the content from an editor */
  328. FILE *f;
  329. unlink("/tmp/snac-edit.txt");
  330. system("$EDITOR /tmp/snac-edit.txt");
  331. if ((f = fopen("/tmp/snac-edit.txt", "r")) != NULL) {
  332. content = xs_readall(f);
  333. fclose(f);
  334. unlink("/tmp/snac-edit.txt");
  335. }
  336. else {
  337. printf("Nothing to send\n");
  338. return 1;
  339. }
  340. }
  341. else
  342. if (strcmp(url, "-") == 0) {
  343. /* get the content from stdin */
  344. content = xs_readall(stdin);
  345. }
  346. else
  347. content = xs_dup(url);
  348. msg = msg_note(&snac, content, NULL, in_reply_to, NULL, 0);
  349. c_msg = msg_create(&snac, msg);
  350. if (dbglevel) {
  351. xs_json_dump(c_msg, 4, stdout);
  352. }
  353. enqueue_message(&snac, c_msg);
  354. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  355. return 0;
  356. }
  357. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  358. return 1;
  359. }