1
0

main.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_time.h"
  7. #include "xs_openssl.h"
  8. #include "xs_match.h"
  9. #include "xs_random.h"
  10. #include "xs_http.h"
  11. #include "snac.h"
  12. #include <sys/stat.h>
  13. #include <sys/wait.h>
  14. int usage(const char *cmd)
  15. {
  16. printf("snac " VERSION " - A simple, minimalistic ActivityPub instance\n");
  17. printf("Copyright (c) 2022 - 2026 grunfink et al. / MIT license\n");
  18. printf("\n");
  19. if (cmd == NULL) {
  20. printf("Commands:\n");
  21. printf("\n");
  22. }
  23. const char *cmds =
  24. "init [{basedir}] Initializes the data storage\n"
  25. "upgrade {basedir} Upgrade to a new version\n"
  26. "adduser {basedir} [{uid}] Adds a new user\n"
  27. "deluser {basedir} {uid} Deletes a user\n"
  28. "update {basedir} {uid} Sends a user's updated profile\n"
  29. "httpd {basedir} Starts the HTTPD daemon\n"
  30. "purge {basedir} Purges old data\n"
  31. "state {basedir} Prints server state\n"
  32. "webfinger {basedir} {account} Queries about an account (@user@host or actor url)\n"
  33. "queue {basedir} {uid} Processes a user queue\n"
  34. "follow {basedir} {uid} {actor} Follows an actor\n"
  35. "unfollow {basedir} {uid} {actor} Unfollows an actor\n"
  36. "request {basedir} {uid} {url} Requests an object\n"
  37. "insert {basedir} {uid} {url} Requests an object and inserts it into the timeline\n"
  38. "collect_replies {basedir} {uid} {url} Collects all replies from a post\n"
  39. "actor {basedir} [{uid}] {url} Requests an actor\n"
  40. "note {basedir} {uid} {text} [files...] Sends a note with optional attachments\n"
  41. "note_unlisted {basedir} {uid} {text} [files...] Sends an unlisted note with optional attachments\n"
  42. "note_mention {basedir} {uid} {text} [files...] Sends a note only to mentioned accounts\n"
  43. "note_followers {basedir} {uid} {text} [files...] Sends a note only to followers\n"
  44. "boost|announce {basedir} {uid} {url} Boosts (announces) a post\n"
  45. "unboost {basedir} {uid} {url} Unboosts a post\n"
  46. "resetpwd {basedir} {uid} Resets the password of a user\n"
  47. "ping {basedir} {uid} {actor} Pings an actor\n"
  48. "webfinger_s {basedir} {uid} {account} Queries about an account (@user@host or actor url)\n"
  49. "pin {basedir} {uid} {msg_url} Pins a message\n"
  50. "unpin {basedir} {uid} {msg_url} Unpins a message\n"
  51. "bookmark {basedir} {uid} {msg_url} Bookmarks a message\n"
  52. "unbookmark {basedir} {uid} {msg_url} Unbookmarks a message\n"
  53. "block {basedir} {instance_url} Blocks a full instance\n"
  54. "unblock {basedir} {instance_url} Unblocks a full instance\n"
  55. "limit {basedir} {uid} {actor} Limits an actor (drops their announces)\n"
  56. "unlimit {basedir} {uid} {actor} Unlimits an actor\n"
  57. "cool {basedir} {uid} {actor} Mark an actor as cool (notify whenever they post)\n"
  58. "uncool {basedir} {uid} {actor} Actor is no longer cool\n"
  59. "muted {basedir} {uid} Lists the muted actors\n"
  60. "unmute {basedir} {uid} {actor} Unmutes a previously muted actor\n"
  61. "verify_links {basedir} {uid} Verifies a user's links (in the metadata)\n"
  62. "search {basedir} {uid} {regex} Searches posts by content\n"
  63. "export_csv {basedir} {uid} Exports followers, lists, MUTEd and bookmarks to CSV\n"
  64. "export_posts {basedir} {uid} Exports all posts to outbox.json\n"
  65. "alias {basedir} {uid} {account} Sets account (@user@host or actor url) as an alias\n"
  66. "migrate {basedir} {uid} Migrates to the account defined as the alias\n"
  67. "import_csv {basedir} {uid} Imports data from CSV files\n"
  68. "import_list {basedir} {uid} {file} Imports a Mastodon CSV list file\n"
  69. "import_block_list {basedir} {uid} {file} Imports a Mastodon CSV block list file\n"
  70. "lists {basedir} {uid} Returns the names of the lists created by the user\n"
  71. "list_members {basedir} {uid} {name} Returns the list of accounts inside a list\n"
  72. "list_create {basedir} {uid} {name} Creates a new list\n"
  73. "list_remove {basedir} {uid} {name} Removes an existing list\n"
  74. "list_add {basedir} {uid} {name} {acct} Adds an account (@user@host or actor url) to a list\n"
  75. "list_del {basedir} {uid} {name} {actor} Deletes an actor url from a list\n"
  76. "top_ten {basedir} {uid} [{N}] Prints the most popular posts\n"
  77. "refresh {basedir} {uid} Refreshes all actors\n";
  78. if (cmd == NULL)
  79. printf("%s", cmds);
  80. else {
  81. /* only show help for the entered command */
  82. xs *l = xs_split(cmds, "\n");
  83. const char *v;
  84. int cnt = 0;
  85. xs_list_foreach(l, v) {
  86. if (xs_str_in(v, cmd) != -1) {
  87. printf("%s\n", v);
  88. cnt++;
  89. }
  90. }
  91. if (cnt == 0)
  92. printf("%s", cmds);
  93. }
  94. return 1;
  95. }
  96. char *get_argv(int *argi, int argc, char *argv[])
  97. {
  98. if (*argi < argc)
  99. return argv[(*argi)++];
  100. else
  101. return NULL;
  102. }
  103. #define GET_ARGV() get_argv(&argi, argc, argv)
  104. int main(int argc, char *argv[])
  105. {
  106. char *cmd;
  107. char *basedir;
  108. char *user;
  109. char *url;
  110. int argi = 1;
  111. snac snac;
  112. /* ensure group has write access */
  113. umask(0007);
  114. if ((cmd = GET_ARGV()) == NULL)
  115. return usage(cmd);
  116. if (strcmp(cmd, "init") == 0) { /** **/
  117. /* initialize the data storage */
  118. /* ... */
  119. basedir = GET_ARGV();
  120. return snac_init(basedir);
  121. }
  122. if ((basedir = getenv("SNAC_BASEDIR")) == NULL) {
  123. if ((basedir = GET_ARGV()) == NULL)
  124. return usage(cmd);
  125. }
  126. if (strcmp(cmd, "upgrade") == 0) { /** **/
  127. int ret;
  128. /* upgrade */
  129. if ((ret = srv_open(basedir, 1)) == 1)
  130. srv_log(xs_dup("OK"));
  131. return ret;
  132. }
  133. if (!srv_open(basedir, 0)) {
  134. srv_log(xs_fmt("error opening data storage at %s", basedir));
  135. return 1;
  136. }
  137. if (strcmp(cmd, "adduser") == 0) { /** **/
  138. user = GET_ARGV();
  139. return adduser(user);
  140. return 0;
  141. }
  142. if (strcmp(cmd, "httpd") == 0) { /** **/
  143. httpd();
  144. srv_free();
  145. return 0;
  146. }
  147. if (strcmp(cmd, "purge") == 0) { /** **/
  148. purge_all();
  149. return 0;
  150. }
  151. if (strcmp(cmd, "poll_hashtag_rss") == 0) { /** **/
  152. rss_poll_hashtags();
  153. return 0;
  154. }
  155. if (strcmp(cmd, "state") == 0) { /** **/
  156. xs *shm_name = NULL;
  157. srv_state *p_state = srv_state_op(&shm_name, 1);
  158. if (p_state == NULL)
  159. return 1;
  160. srv_state ss = *p_state;
  161. int n;
  162. printf("server: %s (%s)\n", xs_dict_get(srv_config, "host"), USER_AGENT);
  163. xs *uptime = xs_str_time_diff(time(NULL) - ss.srv_start_time);
  164. printf("uptime: %s\n", uptime);
  165. printf("job fifo size (cur): %d\n", ss.job_fifo_size);
  166. printf("job fifo size (peak): %d\n", ss.peak_job_fifo_size);
  167. char *th_states[] = { "stopped", "waiting", "input", "output" };
  168. for (n = 0; n < ss.n_threads; n++)
  169. printf("thread #%d state: %s\n", n, th_states[ss.th_state[n]]);
  170. return 0;
  171. }
  172. if ((user = GET_ARGV()) == NULL)
  173. return usage(cmd);
  174. if (strcmp(cmd, "block") == 0) { /** **/
  175. int ret = instance_block(user);
  176. if (ret < 0) {
  177. fprintf(stderr, "Error blocking instance %s: %d\n", user, ret);
  178. return 1;
  179. }
  180. return 0;
  181. }
  182. if (strcmp(cmd, "unblock") == 0) { /** **/
  183. int ret = instance_unblock(user);
  184. if (ret < 0) {
  185. fprintf(stderr, "Error unblocking instance %s: %d\n", user, ret);
  186. return 1;
  187. }
  188. return 0;
  189. }
  190. if (strcmp(cmd, "webfinger") == 0) { /** **/
  191. xs *actor = NULL;
  192. xs *uid = NULL;
  193. int status;
  194. status = webfinger_request(user, &actor, &uid);
  195. printf("status: %d\n", status);
  196. if (actor != NULL)
  197. printf("actor: %s\n", actor);
  198. if (uid != NULL)
  199. printf("uid: %s\n", uid);
  200. return 0;
  201. }
  202. if (argi == argc && strcmp(cmd, "actor") == 0) { /** **/
  203. /* query an actor without user (non-signed) */
  204. xs *actor = NULL;
  205. int status;
  206. status = actor_request(NULL, user, &actor);
  207. printf("status: %d\n", status);
  208. if (valid_status(status)) {
  209. xs_json_dump(actor, 4, stdout);
  210. printf("\n");
  211. }
  212. return 0;
  213. }
  214. if (!user_open(&snac, user)) {
  215. printf("invalid user '%s'\n", user);
  216. return 1;
  217. }
  218. lastlog_write(&snac, "cmdline");
  219. if (strcmp(cmd, "resetpwd") == 0) { /** **/
  220. return resetpwd(&snac);
  221. }
  222. if (strcmp(cmd, "deluser") == 0) { /** **/
  223. return deluser(&snac);
  224. }
  225. if (strcmp(cmd, "update") == 0) { /** **/
  226. xs *a_msg = msg_actor(&snac);
  227. xs *u_msg = msg_update(&snac, a_msg);
  228. enqueue_message(&snac, u_msg);
  229. return 0;
  230. }
  231. if (strcmp(cmd, "queue") == 0) { /** **/
  232. process_user_queue(&snac);
  233. return 0;
  234. }
  235. if (strcmp(cmd, "verify_links") == 0) { /** **/
  236. verify_links(&snac);
  237. return 0;
  238. }
  239. if (strcmp(cmd, "timeline") == 0) { /** **/
  240. #if 0
  241. xs *list = local_list(&snac, XS_ALL);
  242. xs *body = html_timeline(&snac, list, 1);
  243. printf("%s\n", body);
  244. user_free(&snac);
  245. srv_free();
  246. #endif
  247. xs *idx = xs_fmt("%s/private.idx", snac.basedir);
  248. xs *list = index_list_desc(idx, 0, 256);
  249. xs *tl = timeline_top_level(&snac, list);
  250. xs_json_dump(tl, 4, stdout);
  251. return 0;
  252. }
  253. if (strcmp(cmd, "export_csv") == 0) { /** **/
  254. export_csv(&snac);
  255. return 0;
  256. }
  257. if (strcmp(cmd, "export_posts") == 0) { /** **/
  258. export_posts(&snac);
  259. return 0;
  260. }
  261. if (strcmp(cmd, "import_csv") == 0) { /** **/
  262. import_csv(&snac);
  263. return 0;
  264. }
  265. if (strcmp(cmd, "migrate") == 0) { /** **/
  266. return migrate_account(&snac);
  267. }
  268. if (strcmp(cmd, "lists") == 0) { /** **/
  269. xs *lol = list_maint(&snac, NULL, OP_LIST);
  270. const xs_list *l;
  271. xs_list_foreach(lol, l) {
  272. printf("%s (%s)\n", xs_list_get(l, 1), xs_list_get(l, 0));
  273. }
  274. return 0;
  275. }
  276. if (strcmp(cmd, "muted") == 0) { /** **/
  277. xs *l = muted_list(&snac);
  278. const char *v;
  279. xs_list_foreach(l, v)
  280. printf("%s\n", v);
  281. return 0;
  282. }
  283. if (strcmp(cmd, "top_ten") == 0) { /** **/
  284. int count = 10;
  285. const char *n = GET_ARGV();
  286. if (xs_is_string(n))
  287. count = atoi(n);
  288. xs *l = user_top_ten(&snac, count);
  289. const xs_list *i;
  290. xs_list_foreach(l, i) {
  291. printf("%s %ld★ %ld↺ %ld☺\n", xs_list_get(i, 0),
  292. xs_number_get_l(xs_list_get(i, 1)),
  293. xs_number_get_l(xs_list_get(i, 2)),
  294. xs_number_get_l(xs_list_get(i, 3)));
  295. }
  296. return 0;
  297. }
  298. if (strcmp(cmd, "refresh") == 0) { /** **/
  299. xs *fwers = follower_list(&snac);
  300. xs *fwing = following_list(&snac);
  301. const char *id;
  302. xs_list_foreach(fwers, id)
  303. enqueue_actor_refresh(&snac, id, 0);
  304. xs_list_foreach(fwing, id)
  305. enqueue_actor_refresh(&snac, id, 0);
  306. return 0;
  307. }
  308. if ((url = GET_ARGV()) == NULL)
  309. return usage(cmd);
  310. if (strcmp(cmd, "list_members") == 0) { /** **/
  311. xs *lid = list_maint(&snac, url, OP_FIND);
  312. if (lid != NULL) {
  313. xs *lcont = list_members(&snac, lid, NULL, OP_LIST);
  314. const char *md5;
  315. xs_list_foreach(lcont, md5) {
  316. xs *actor = NULL;
  317. if (valid_status(object_get_by_md5(md5, &actor))) {
  318. printf("%s (%s)\n", xs_dict_get(actor, "id"), xs_dict_get_def(actor, "preferredUsername", ""));
  319. }
  320. }
  321. }
  322. else
  323. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  324. return 0;
  325. }
  326. if (strcmp(cmd, "list_create") == 0) { /** **/
  327. xs *lid = list_maint(&snac, url, OP_FIND);
  328. if (lid == NULL) {
  329. xs *n_lid = list_maint(&snac, url, OP_ADD);
  330. printf("New list named '%s' created (%s)\n", url, n_lid);
  331. }
  332. else
  333. fprintf(stderr, "A list named '%s' already exist\n", url);
  334. return 0;
  335. }
  336. if (strcmp(cmd, "list_remove") == 0) { /** **/
  337. xs *lid = list_maint(&snac, url, OP_FIND);
  338. if (lid != NULL) {
  339. list_maint(&snac, lid, OP_DEL);
  340. printf("List '%s' (%s) deleted\n", url, lid);
  341. }
  342. else
  343. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  344. return 0;
  345. }
  346. if (strcmp(cmd, "list_add") == 0) { /** **/
  347. const char *account = GET_ARGV();
  348. if (account != NULL) {
  349. xs *lid = list_maint(&snac, url, OP_FIND);
  350. if (lid != NULL) {
  351. xs *actor = NULL;
  352. xs *uid = NULL;
  353. if (valid_status(webfinger_request(account, &actor, &uid))) {
  354. xs *md5 = xs_md5_hex(actor, strlen(actor));
  355. list_members(&snac, lid, md5, OP_ADD);
  356. printf("Actor %s (%s) added to list '%s' (%s)\n", actor, uid, url, lid);
  357. }
  358. else
  359. fprintf(stderr, "Cannot resolve account '%s'\n", account);
  360. }
  361. else
  362. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  363. }
  364. return 0;
  365. }
  366. if (strcmp(cmd, "list_del") == 0) { /** **/
  367. const char *account = GET_ARGV();
  368. if (account != NULL) {
  369. xs *lid = list_maint(&snac, url, OP_FIND);
  370. if (lid != NULL) {
  371. xs *md5 = xs_md5_hex(account, strlen(account));
  372. list_members(&snac, lid, md5, OP_DEL);
  373. printf("Actor %s deleted from list '%s' (%s)\n", account, url, lid);
  374. }
  375. else
  376. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  377. }
  378. return 0;
  379. }
  380. if (strcmp(cmd, "alias") == 0) { /** **/
  381. xs *actor = NULL;
  382. xs *uid = NULL;
  383. int status = HTTP_STATUS_OK;
  384. if (*url == '\0')
  385. actor = xs_dup("");
  386. else
  387. status = webfinger_request(url, &actor, &uid);
  388. if (valid_status(status)) {
  389. if (strcmp(actor, snac.actor) == 0) {
  390. snac_log(&snac, xs_fmt("You can't be your own alias"));
  391. return 1;
  392. }
  393. else {
  394. snac.config = xs_dict_set(snac.config, "alias", actor);
  395. snac.config = xs_dict_set(snac.config, "alias_raw", url);
  396. user_persist(&snac, 1);
  397. }
  398. }
  399. else {
  400. snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
  401. return 1;
  402. }
  403. return 0;
  404. }
  405. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  406. xs *actor = NULL;
  407. xs *uid = NULL;
  408. int status;
  409. status = webfinger_request_signed(&snac, url, &actor, &uid);
  410. printf("status: %d\n", status);
  411. if (actor != NULL)
  412. printf("actor: %s\n", actor);
  413. if (uid != NULL)
  414. printf("uid: %s\n", uid);
  415. return 0;
  416. }
  417. if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
  418. xs *msg = msg_admiration(&snac, url, "Announce");
  419. if (msg != NULL) {
  420. enqueue_message(&snac, msg);
  421. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, "");
  422. if (dbglevel) {
  423. xs_json_dump(msg, 4, stdout);
  424. }
  425. }
  426. return 0;
  427. }
  428. if (strcmp(cmd, "assist") == 0) { /** **/
  429. /* undocumented: experimental (do not use) */
  430. xs *msg = msg_admiration(&snac, url, "Accept");
  431. if (msg != NULL) {
  432. enqueue_message(&snac, msg);
  433. if (dbglevel) {
  434. xs_json_dump(msg, 4, stdout);
  435. }
  436. }
  437. return 0;
  438. }
  439. if (strcmp(cmd, "unboost") == 0) { /** **/
  440. xs *msg = msg_repulsion(&snac, url, "Announce");
  441. if (msg != NULL) {
  442. enqueue_message(&snac, msg);
  443. if (dbglevel) {
  444. xs_json_dump(msg, 4, stdout);
  445. }
  446. }
  447. return 0;
  448. }
  449. if (strcmp(cmd, "follow") == 0) { /** **/
  450. xs *msg = msg_follow(&snac, url);
  451. if (msg != NULL) {
  452. const char *actor = xs_dict_get(msg, "object");
  453. following_add(&snac, actor, msg);
  454. enqueue_output_by_actor(&snac, msg, actor, 0);
  455. if (dbglevel) {
  456. xs_json_dump(msg, 4, stdout);
  457. }
  458. }
  459. return 0;
  460. }
  461. if (strcmp(cmd, "unfollow") == 0) { /** **/
  462. xs *object = NULL;
  463. if (valid_status(following_get(&snac, url, &object))) {
  464. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  465. following_del(&snac, url);
  466. enqueue_output_by_actor(&snac, msg, url, 0);
  467. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  468. }
  469. else
  470. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  471. return 0;
  472. }
  473. if (strcmp(cmd, "limit") == 0) { /** **/
  474. int ret;
  475. if (!following_check(&snac, url))
  476. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  477. else
  478. if ((ret = limit(&snac, url)) == 0)
  479. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  480. else
  481. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  482. return 0;
  483. }
  484. if (strcmp(cmd, "unlimit") == 0) { /** **/
  485. int ret;
  486. if (!following_check(&snac, url))
  487. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  488. else
  489. if ((ret = unlimit(&snac, url)) == 0)
  490. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  491. else
  492. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  493. return 0;
  494. }
  495. if (strcmp(cmd, "cool") == 0) { /** **/
  496. int ret;
  497. if ((ret = cool(&snac, url, OP_ADD)) == 0)
  498. snac_log(&snac, xs_fmt("posts from actor %s will now be notified", url));
  499. else
  500. snac_log(&snac, xs_fmt("error marking actor %s as cool (%d)", url, ret));
  501. return 0;
  502. }
  503. if (strcmp(cmd, "uncool") == 0) { /** **/
  504. int ret;
  505. if ((ret = cool(&snac, url, OP_DEL)) == 0)
  506. snac_log(&snac, xs_fmt("posts from actor %s will no longer be notified", url));
  507. else
  508. snac_log(&snac, xs_fmt("error unmarking actor %s as cool (%d)", url, ret));
  509. return 0;
  510. }
  511. if (strcmp(cmd, "unmute") == 0) { /** **/
  512. if (is_muted(&snac, url)) {
  513. unmute(&snac, url);
  514. printf("%s unmuted\n", url);
  515. }
  516. else
  517. printf("%s actor is not muted\n", url);
  518. return 0;
  519. }
  520. if (strcmp(cmd, "search") == 0) { /** **/
  521. int to;
  522. /* 'url' contains the regex */
  523. xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
  524. int c = 0;
  525. const char *v;
  526. /* print results as standalone links */
  527. while (xs_list_next(r, &v, &c)) {
  528. printf("%s/admin/p/%s\n", snac.actor, v);
  529. }
  530. return 0;
  531. }
  532. if (strcmp(cmd, "ping") == 0) { /** **/
  533. xs *actor_o = NULL;
  534. if (!xs_startswith(url, "https:/")) {
  535. /* try to resolve via webfinger */
  536. if (!valid_status(webfinger_request(url, &url, NULL))) {
  537. srv_log(xs_fmt("cannot resolve %s via webfinger", url));
  538. return 1;
  539. }
  540. }
  541. if (valid_status(actor_request(&snac, url, &actor_o))) {
  542. xs *msg = msg_ping(&snac, url);
  543. enqueue_output_by_actor(&snac, msg, url, 0);
  544. if (dbglevel) {
  545. xs_json_dump(msg, 4, stdout);
  546. }
  547. srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
  548. }
  549. else {
  550. srv_log(xs_fmt("Error getting actor %s", url));
  551. return 1;
  552. }
  553. return 0;
  554. }
  555. if (strcmp(cmd, "pin") == 0) { /** **/
  556. int ret = pin(&snac, url);
  557. if (ret < 0) {
  558. fprintf(stderr, "error pinning %s %d\n", url, ret);
  559. return 1;
  560. }
  561. return 0;
  562. }
  563. if (strcmp(cmd, "unpin") == 0) { /** **/
  564. int ret = unpin(&snac, url);
  565. if (ret < 0) {
  566. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  567. return 1;
  568. }
  569. return 0;
  570. }
  571. if (strcmp(cmd, "bookmark") == 0) { /** **/
  572. int ret = bookmark(&snac, url);
  573. if (ret < 0) {
  574. fprintf(stderr, "error bookmarking %s %d\n", url, ret);
  575. return 1;
  576. }
  577. return 0;
  578. }
  579. if (strcmp(cmd, "unbookmark") == 0) { /** **/
  580. int ret = unbookmark(&snac, url);
  581. if (ret < 0) {
  582. fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. if (strcmp(cmd, "question") == 0) { /** **/
  588. int end_secs = 5 * 60;
  589. xs *opts = xs_split(url, ";");
  590. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  591. xs *c_msg = msg_create(&snac, msg);
  592. if (dbglevel) {
  593. xs_json_dump(c_msg, 4, stdout);
  594. }
  595. enqueue_message(&snac, c_msg);
  596. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  597. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  598. return 0;
  599. }
  600. if (strcmp(cmd, "request") == 0) { /** **/
  601. int status;
  602. xs *data = NULL;
  603. status = activitypub_request(&snac, url, &data);
  604. printf("status: %d\n", status);
  605. if (data != NULL) {
  606. xs_json_dump(data, 4, stdout);
  607. }
  608. return 0;
  609. }
  610. if (strcmp(cmd, "collect_replies") == 0) { /** **/
  611. enqueue_collect_replies(&snac, url);
  612. return 0;
  613. }
  614. if (strcmp(cmd, "collect_outbox") == 0) { /** **/
  615. enqueue_collect_outbox(&snac, url);
  616. return 0;
  617. }
  618. if (strcmp(cmd, "insert") == 0) { /** **/
  619. int status;
  620. xs *data = NULL;
  621. status = activitypub_request(&snac, url, &data);
  622. printf("status: %d\n", status);
  623. if (data != NULL) {
  624. xs_json_dump(data, 4, stdout);
  625. enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
  626. if (!timeline_here(&snac, url))
  627. timeline_add(&snac, url, data);
  628. else
  629. printf("Post %s already here\n", url);
  630. }
  631. return 0;
  632. }
  633. if (strcmp(cmd, "request2") == 0) { /** **/
  634. enqueue_object_request(&snac, url, 2);
  635. return 0;
  636. }
  637. if (strcmp(cmd, "actor") == 0) { /** **/
  638. int status;
  639. xs *data = NULL;
  640. status = actor_request(&snac, url, &data);
  641. printf("status: %d\n", status);
  642. if (valid_status(status)) {
  643. xs_json_dump(data, 4, stdout);
  644. }
  645. return 0;
  646. }
  647. if (strcmp(cmd, "import_list") == 0) { /** **/
  648. import_list_csv(&snac, url);
  649. return 0;
  650. }
  651. if (strcmp(cmd, "import_block_list") == 0) { /** **/
  652. import_blocked_accounts_csv(&snac, url);
  653. return 0;
  654. }
  655. if (strcmp(cmd, "note") == 0 || /** **/
  656. strcmp(cmd, "note_unlisted") == 0 || /** **/
  657. strcmp(cmd, "note_mention") == 0 || /** **/
  658. strcmp(cmd, "note_followers") == 0) { /** **/
  659. xs *content = NULL;
  660. xs *msg = NULL;
  661. xs *c_msg = NULL;
  662. xs *attl = xs_list_new();
  663. const char *fn = NULL;
  664. const char *in_reply_to = NULL;
  665. const char **arg_irt = NULL;
  666. int arg_date = 0;
  667. xs *post_date = NULL;
  668. /* iterate possible attachments */
  669. while ((fn = GET_ARGV())) {
  670. FILE *f;
  671. if (arg_irt) {
  672. *arg_irt = fn;
  673. arg_irt = NULL;
  674. }
  675. else
  676. if (arg_date) {
  677. /* convert to ISO */
  678. time_t t = xs_parse_localtime(fn, "%Y%m%d%H%M%S");
  679. if (t == 0) {
  680. fprintf(stderr, "Invalid scheduled date format (must be YYYYmmddHHMMSS)\n");
  681. return 1;
  682. }
  683. post_date = xs_str_iso_date(t);
  684. arg_date = 0;
  685. }
  686. else
  687. if (strcmp(fn, "-r") == 0) {
  688. /* next argument is an inReplyTo */
  689. arg_irt = &in_reply_to;
  690. }
  691. else
  692. if (strcmp(fn, "-d") == 0) {
  693. /* next argument is the schedule date */
  694. arg_date = 1;
  695. }
  696. else
  697. if ((f = fopen(fn, "rb")) != NULL) {
  698. /* get the file size and content */
  699. fseek(f, 0, SEEK_END);
  700. int sz = ftell(f);
  701. fseek(f, 0, SEEK_SET);
  702. xs *atc = xs_readall(f);
  703. fclose(f);
  704. char *ext = strrchr(fn, '.');
  705. char rnd[32];
  706. xs_rnd_buf(rnd, sizeof(rnd));
  707. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  708. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  709. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  710. /* store */
  711. static_put(&snac, id, atc, sz);
  712. xs *l = xs_list_new();
  713. l = xs_list_append(l, url);
  714. l = xs_list_append(l, ""); /* alt text */
  715. attl = xs_list_append(attl, l);
  716. }
  717. else
  718. fprintf(stderr, "Error opening '%s' as attachment\n", fn);
  719. }
  720. if (strcmp(url, "-e") == 0) {
  721. /* get the content from an editor */
  722. #define EDITOR "$EDITOR "
  723. char cmd[] = EDITOR "/tmp/snac-XXXXXX";
  724. FILE *f;
  725. int fd = mkstemp(cmd + strlen(EDITOR));
  726. if (fd >= 0) {
  727. int status = system(cmd);
  728. if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) {
  729. content = xs_readall(f);
  730. fclose(f);
  731. unlink(cmd + strlen(EDITOR));
  732. } else {
  733. printf("Nothing to send\n");
  734. close(fd);
  735. return 1;
  736. }
  737. } else {
  738. fprintf(stderr, "Temp file creation failed\n");
  739. return 1;
  740. }
  741. }
  742. else
  743. if (strcmp(url, "-") == 0) {
  744. /* get the content from stdin */
  745. content = xs_readall(stdin);
  746. }
  747. else
  748. content = xs_dup(url);
  749. if (!content || !*content) {
  750. fprintf(stderr, "Nothing to send\n");
  751. return 1;
  752. }
  753. int scope = SCOPE_PUBLIC;
  754. if (strcmp(cmd, "note_mention") == 0)
  755. scope = SCOPE_MENTIONED;
  756. else
  757. if (strcmp(cmd, "note_unlisted") == 0)
  758. scope = SCOPE_UNLISTED;
  759. else
  760. if (strcmp(cmd, "note_followers") == 0)
  761. scope = SCOPE_FOLLOWERS;
  762. msg = msg_note(&snac, content, NULL, in_reply_to, attl, scope, getenv("LANG"), post_date);
  763. const char *id = xs_dict_get(msg, "id");
  764. if (post_date)
  765. schedule_add(&snac, id, msg);
  766. else {
  767. c_msg = msg_create(&snac, msg);
  768. if (dbglevel) {
  769. xs_json_dump(c_msg, 4, stdout);
  770. }
  771. enqueue_message(&snac, c_msg);
  772. enqueue_webmention(msg);
  773. timeline_add(&snac, id, msg);
  774. }
  775. return 0;
  776. }
  777. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  778. return 1;
  779. }