main.c 23 KB

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