main.c 22 KB

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