main.c 25 KB

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