main.c 27 KB

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