1
0

main.c 24 KB

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