main.c 25 KB

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