main.c 24 KB

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