main.c 25 KB

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