main.c 26 KB

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