main.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  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↺ %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. xs_number_get_l(xs_list_get(i, 3)));
  293. }
  294. return 0;
  295. }
  296. if (strcmp(cmd, "refresh") == 0) { /** **/
  297. xs *fwers = follower_list(&snac);
  298. xs *fwing = following_list(&snac);
  299. const char *id;
  300. xs_list_foreach(fwers, id)
  301. enqueue_actor_refresh(&snac, id, 0);
  302. xs_list_foreach(fwing, id)
  303. enqueue_actor_refresh(&snac, id, 0);
  304. return 0;
  305. }
  306. if ((url = GET_ARGV()) == NULL)
  307. return usage(cmd);
  308. if (strcmp(cmd, "list_members") == 0) { /** **/
  309. xs *lid = list_maint(&snac, url, 4);
  310. if (lid != NULL) {
  311. xs *lcont = list_members(&snac, lid, NULL, 0);
  312. const char *md5;
  313. xs_list_foreach(lcont, md5) {
  314. xs *actor = NULL;
  315. if (valid_status(object_get_by_md5(md5, &actor))) {
  316. printf("%s (%s)\n", xs_dict_get(actor, "id"), xs_dict_get_def(actor, "preferredUsername", ""));
  317. }
  318. }
  319. }
  320. else
  321. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  322. return 0;
  323. }
  324. if (strcmp(cmd, "list_create") == 0) { /** **/
  325. xs *lid = list_maint(&snac, url, 4);
  326. if (lid == NULL) {
  327. xs *n_lid = list_maint(&snac, url, 1);
  328. printf("New list named '%s' created (%s)\n", url, n_lid);
  329. }
  330. else
  331. fprintf(stderr, "A list named '%s' already exist\n", url);
  332. return 0;
  333. }
  334. if (strcmp(cmd, "list_remove") == 0) { /** **/
  335. xs *lid = list_maint(&snac, url, 4);
  336. if (lid != NULL) {
  337. list_maint(&snac, lid, 2);
  338. printf("List '%s' (%s) deleted\n", url, lid);
  339. }
  340. else
  341. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  342. return 0;
  343. }
  344. if (strcmp(cmd, "list_add") == 0) { /** **/
  345. const char *account = GET_ARGV();
  346. if (account != NULL) {
  347. xs *lid = list_maint(&snac, url, 4);
  348. if (lid != NULL) {
  349. xs *actor = NULL;
  350. xs *uid = NULL;
  351. if (valid_status(webfinger_request(account, &actor, &uid))) {
  352. xs *md5 = xs_md5_hex(actor, strlen(actor));
  353. list_members(&snac, lid, md5, 1);
  354. printf("Actor %s (%s) added to list '%s' (%s)\n", actor, uid, url, lid);
  355. }
  356. else
  357. fprintf(stderr, "Cannot resolve account '%s'\n", account);
  358. }
  359. else
  360. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  361. }
  362. return 0;
  363. }
  364. if (strcmp(cmd, "list_del") == 0) { /** **/
  365. const char *account = GET_ARGV();
  366. if (account != NULL) {
  367. xs *lid = list_maint(&snac, url, 4);
  368. if (lid != NULL) {
  369. xs *md5 = xs_md5_hex(account, strlen(account));
  370. list_members(&snac, lid, md5, 2);
  371. printf("Actor %s deleted from list '%s' (%s)\n", account, url, lid);
  372. }
  373. else
  374. fprintf(stderr, "Cannot find a list named '%s'\n", url);
  375. }
  376. return 0;
  377. }
  378. if (strcmp(cmd, "alias") == 0) { /** **/
  379. xs *actor = NULL;
  380. xs *uid = NULL;
  381. int status = HTTP_STATUS_OK;
  382. if (*url == '\0')
  383. actor = xs_dup("");
  384. else
  385. status = webfinger_request(url, &actor, &uid);
  386. if (valid_status(status)) {
  387. if (strcmp(actor, snac.actor) == 0) {
  388. snac_log(&snac, xs_fmt("You can't be your own alias"));
  389. return 1;
  390. }
  391. else {
  392. snac.config = xs_dict_set(snac.config, "alias", actor);
  393. snac.config = xs_dict_set(snac.config, "alias_raw", url);
  394. user_persist(&snac, 1);
  395. }
  396. }
  397. else {
  398. snac_log(&snac, xs_fmt("Webfinger error for %s %d", url, status));
  399. return 1;
  400. }
  401. return 0;
  402. }
  403. if (strcmp(cmd, "webfinger_s") == 0) { /** **/
  404. xs *actor = NULL;
  405. xs *uid = NULL;
  406. int status;
  407. status = webfinger_request_signed(&snac, url, &actor, &uid);
  408. printf("status: %d\n", status);
  409. if (actor != NULL)
  410. printf("actor: %s\n", actor);
  411. if (uid != NULL)
  412. printf("uid: %s\n", uid);
  413. return 0;
  414. }
  415. if (strcmp(cmd, "boost") == 0 || strcmp(cmd, "announce") == 0) { /** **/
  416. xs *msg = msg_admiration(&snac, url, "Announce");
  417. if (msg != NULL) {
  418. enqueue_message(&snac, msg);
  419. timeline_admire(&snac, xs_dict_get(msg, "object"), snac.actor, 0, "");
  420. if (dbglevel) {
  421. xs_json_dump(msg, 4, stdout);
  422. }
  423. }
  424. return 0;
  425. }
  426. if (strcmp(cmd, "assist") == 0) { /** **/
  427. /* undocumented: experimental (do not use) */
  428. xs *msg = msg_admiration(&snac, url, "Accept");
  429. if (msg != NULL) {
  430. enqueue_message(&snac, msg);
  431. if (dbglevel) {
  432. xs_json_dump(msg, 4, stdout);
  433. }
  434. }
  435. return 0;
  436. }
  437. if (strcmp(cmd, "unboost") == 0) { /** **/
  438. xs *msg = msg_repulsion(&snac, url, "Announce");
  439. if (msg != NULL) {
  440. enqueue_message(&snac, msg);
  441. if (dbglevel) {
  442. xs_json_dump(msg, 4, stdout);
  443. }
  444. }
  445. return 0;
  446. }
  447. if (strcmp(cmd, "follow") == 0) { /** **/
  448. xs *msg = msg_follow(&snac, url);
  449. if (msg != NULL) {
  450. const char *actor = xs_dict_get(msg, "object");
  451. following_add(&snac, actor, msg);
  452. enqueue_output_by_actor(&snac, msg, actor, 0);
  453. if (dbglevel) {
  454. xs_json_dump(msg, 4, stdout);
  455. }
  456. }
  457. return 0;
  458. }
  459. if (strcmp(cmd, "unfollow") == 0) { /** **/
  460. xs *object = NULL;
  461. if (valid_status(following_get(&snac, url, &object))) {
  462. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  463. following_del(&snac, url);
  464. enqueue_output_by_actor(&snac, msg, url, 0);
  465. snac_log(&snac, xs_fmt("unfollowed actor %s", url));
  466. }
  467. else
  468. snac_log(&snac, xs_fmt("actor is not being followed %s", url));
  469. return 0;
  470. }
  471. if (strcmp(cmd, "limit") == 0) { /** **/
  472. int ret;
  473. if (!following_check(&snac, url))
  474. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  475. else
  476. if ((ret = limit(&snac, url)) == 0)
  477. snac_log(&snac, xs_fmt("actor %s is now limited", url));
  478. else
  479. snac_log(&snac, xs_fmt("error limiting actor %s (%d)", url, ret));
  480. return 0;
  481. }
  482. if (strcmp(cmd, "unlimit") == 0) { /** **/
  483. int ret;
  484. if (!following_check(&snac, url))
  485. snac_log(&snac, xs_fmt("actor %s is not being followed", url));
  486. else
  487. if ((ret = unlimit(&snac, url)) == 0)
  488. snac_log(&snac, xs_fmt("actor %s is no longer limited", url));
  489. else
  490. snac_log(&snac, xs_fmt("error unlimiting actor %s (%d)", url, ret));
  491. return 0;
  492. }
  493. if (strcmp(cmd, "unmute") == 0) { /** **/
  494. if (is_muted(&snac, url)) {
  495. unmute(&snac, url);
  496. printf("%s unmuted\n", url);
  497. }
  498. else
  499. printf("%s actor is not muted\n", url);
  500. return 0;
  501. }
  502. if (strcmp(cmd, "search") == 0) { /** **/
  503. int to;
  504. /* 'url' contains the regex */
  505. xs *r = content_search(&snac, url, 1, 0, XS_ALL, 10, &to);
  506. int c = 0;
  507. const char *v;
  508. /* print results as standalone links */
  509. while (xs_list_next(r, &v, &c)) {
  510. printf("%s/admin/p/%s\n", snac.actor, v);
  511. }
  512. return 0;
  513. }
  514. if (strcmp(cmd, "ping") == 0) { /** **/
  515. xs *actor_o = NULL;
  516. if (!xs_startswith(url, "https:/")) {
  517. /* try to resolve via webfinger */
  518. if (!valid_status(webfinger_request(url, &url, NULL))) {
  519. srv_log(xs_fmt("cannot resolve %s via webfinger", url));
  520. return 1;
  521. }
  522. }
  523. if (valid_status(actor_request(&snac, url, &actor_o))) {
  524. xs *msg = msg_ping(&snac, url);
  525. enqueue_output_by_actor(&snac, msg, url, 0);
  526. if (dbglevel) {
  527. xs_json_dump(msg, 4, stdout);
  528. }
  529. srv_log(xs_fmt("Ping sent to %s -- see log for Pong reply", url));
  530. }
  531. else {
  532. srv_log(xs_fmt("Error getting actor %s", url));
  533. return 1;
  534. }
  535. return 0;
  536. }
  537. if (strcmp(cmd, "pin") == 0) { /** **/
  538. int ret = pin(&snac, url);
  539. if (ret < 0) {
  540. fprintf(stderr, "error pinning %s %d\n", url, ret);
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. if (strcmp(cmd, "unpin") == 0) { /** **/
  546. int ret = unpin(&snac, url);
  547. if (ret < 0) {
  548. fprintf(stderr, "error unpinning %s %d\n", url, ret);
  549. return 1;
  550. }
  551. return 0;
  552. }
  553. if (strcmp(cmd, "bookmark") == 0) { /** **/
  554. int ret = bookmark(&snac, url);
  555. if (ret < 0) {
  556. fprintf(stderr, "error bookmarking %s %d\n", url, ret);
  557. return 1;
  558. }
  559. return 0;
  560. }
  561. if (strcmp(cmd, "unbookmark") == 0) { /** **/
  562. int ret = unbookmark(&snac, url);
  563. if (ret < 0) {
  564. fprintf(stderr, "error unbookmarking %s %d\n", url, ret);
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. if (strcmp(cmd, "question") == 0) { /** **/
  570. int end_secs = 5 * 60;
  571. xs *opts = xs_split(url, ";");
  572. xs *msg = msg_question(&snac, "Poll", NULL, opts, 0, end_secs);
  573. xs *c_msg = msg_create(&snac, msg);
  574. if (dbglevel) {
  575. xs_json_dump(c_msg, 4, stdout);
  576. }
  577. enqueue_message(&snac, c_msg);
  578. enqueue_close_question(&snac, xs_dict_get(msg, "id"), end_secs);
  579. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  580. return 0;
  581. }
  582. if (strcmp(cmd, "request") == 0) { /** **/
  583. int status;
  584. xs *data = NULL;
  585. status = activitypub_request(&snac, url, &data);
  586. printf("status: %d\n", status);
  587. if (data != NULL) {
  588. xs_json_dump(data, 4, stdout);
  589. }
  590. return 0;
  591. }
  592. if (strcmp(cmd, "collect_replies") == 0) { /** **/
  593. enqueue_collect_replies(&snac, url);
  594. return 0;
  595. }
  596. if (strcmp(cmd, "collect_outbox") == 0) { /** **/
  597. enqueue_collect_outbox(&snac, url);
  598. return 0;
  599. }
  600. if (strcmp(cmd, "insert") == 0) { /** **/
  601. int status;
  602. xs *data = NULL;
  603. status = activitypub_request(&snac, url, &data);
  604. printf("status: %d\n", status);
  605. if (data != NULL) {
  606. xs_json_dump(data, 4, stdout);
  607. enqueue_actor_refresh(&snac, xs_dict_get(data, "attributedTo"), 0);
  608. if (!timeline_here(&snac, url))
  609. timeline_add(&snac, url, data);
  610. else
  611. printf("Post %s already here\n", url);
  612. }
  613. return 0;
  614. }
  615. if (strcmp(cmd, "request2") == 0) { /** **/
  616. enqueue_object_request(&snac, url, 2);
  617. return 0;
  618. }
  619. if (strcmp(cmd, "actor") == 0) { /** **/
  620. int status;
  621. xs *data = NULL;
  622. status = actor_request(&snac, url, &data);
  623. printf("status: %d\n", status);
  624. if (valid_status(status)) {
  625. xs_json_dump(data, 4, stdout);
  626. }
  627. return 0;
  628. }
  629. if (strcmp(cmd, "import_list") == 0) { /** **/
  630. import_list_csv(&snac, url);
  631. return 0;
  632. }
  633. if (strcmp(cmd, "import_block_list") == 0) { /** **/
  634. import_blocked_accounts_csv(&snac, url);
  635. return 0;
  636. }
  637. if (strcmp(cmd, "note") == 0 || /** **/
  638. strcmp(cmd, "note_unlisted") == 0 || /** **/
  639. strcmp(cmd, "note_mention") == 0 || /** **/
  640. strcmp(cmd, "note_followers") == 0) { /** **/
  641. xs *content = NULL;
  642. xs *msg = NULL;
  643. xs *c_msg = NULL;
  644. xs *attl = xs_list_new();
  645. const char *fn = NULL;
  646. const char *in_reply_to = NULL;
  647. const char **arg_irt = NULL;
  648. int arg_date = 0;
  649. xs *post_date = NULL;
  650. /* iterate possible attachments */
  651. while ((fn = GET_ARGV())) {
  652. FILE *f;
  653. if (arg_irt) {
  654. *arg_irt = fn;
  655. arg_irt = NULL;
  656. }
  657. else
  658. if (arg_date) {
  659. /* convert to ISO */
  660. time_t t = xs_parse_localtime(fn, "%Y%m%d%H%M%S");
  661. if (t == 0) {
  662. fprintf(stderr, "Invalid scheduled date format (must be YYYYmmddHHMMSS)\n");
  663. return 1;
  664. }
  665. post_date = xs_str_iso_date(t);
  666. arg_date = 0;
  667. }
  668. else
  669. if (strcmp(fn, "-r") == 0) {
  670. /* next argument is an inReplyTo */
  671. arg_irt = &in_reply_to;
  672. }
  673. else
  674. if (strcmp(fn, "-d") == 0) {
  675. /* next argument is the schedule date */
  676. arg_date = 1;
  677. }
  678. else
  679. if ((f = fopen(fn, "rb")) != NULL) {
  680. /* get the file size and content */
  681. fseek(f, 0, SEEK_END);
  682. int sz = ftell(f);
  683. fseek(f, 0, SEEK_SET);
  684. xs *atc = xs_readall(f);
  685. fclose(f);
  686. char *ext = strrchr(fn, '.');
  687. char rnd[32];
  688. xs_rnd_buf(rnd, sizeof(rnd));
  689. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  690. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  691. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  692. /* store */
  693. static_put(&snac, id, atc, sz);
  694. xs *l = xs_list_new();
  695. l = xs_list_append(l, url);
  696. l = xs_list_append(l, ""); /* alt text */
  697. attl = xs_list_append(attl, l);
  698. }
  699. else
  700. fprintf(stderr, "Error opening '%s' as attachment\n", fn);
  701. }
  702. if (strcmp(url, "-e") == 0) {
  703. /* get the content from an editor */
  704. #define EDITOR "$EDITOR "
  705. char cmd[] = EDITOR "/tmp/snac-XXXXXX";
  706. FILE *f;
  707. int fd = mkstemp(cmd + strlen(EDITOR));
  708. if (fd >= 0) {
  709. int status = system(cmd);
  710. if (WIFEXITED(status) && WEXITSTATUS(status) == 0 && (f = fdopen(fd, "r")) != NULL) {
  711. content = xs_readall(f);
  712. fclose(f);
  713. unlink(cmd + strlen(EDITOR));
  714. } else {
  715. printf("Nothing to send\n");
  716. close(fd);
  717. return 1;
  718. }
  719. } else {
  720. fprintf(stderr, "Temp file creation failed\n");
  721. return 1;
  722. }
  723. }
  724. else
  725. if (strcmp(url, "-") == 0) {
  726. /* get the content from stdin */
  727. content = xs_readall(stdin);
  728. }
  729. else
  730. content = xs_dup(url);
  731. if (!content || !*content) {
  732. fprintf(stderr, "Nothing to send\n");
  733. return 1;
  734. }
  735. int scope = SCOPE_PUBLIC;
  736. if (strcmp(cmd, "note_mention") == 0)
  737. scope = SCOPE_MENTIONED;
  738. else
  739. if (strcmp(cmd, "note_unlisted") == 0)
  740. scope = SCOPE_UNLISTED;
  741. else
  742. if (strcmp(cmd, "note_followers") == 0)
  743. scope = SCOPE_FOLLOWERS;
  744. msg = msg_note(&snac, content, NULL, in_reply_to, attl, scope, getenv("LANG"), post_date);
  745. const char *id = xs_dict_get(msg, "id");
  746. if (post_date)
  747. schedule_add(&snac, id, msg);
  748. else {
  749. c_msg = msg_create(&snac, msg);
  750. if (dbglevel) {
  751. xs_json_dump(c_msg, 4, stdout);
  752. }
  753. enqueue_message(&snac, c_msg);
  754. enqueue_webmention(msg);
  755. timeline_add(&snac, id, msg);
  756. }
  757. return 0;
  758. }
  759. fprintf(stderr, "ERROR: bad command '%s'\n", cmd);
  760. return 1;
  761. }