main.c 25 KB

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