1
0

main.c 26 KB

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