main.c 27 KB

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