main.c 23 KB

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