mastoapi.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_openssl.h"
  6. #include "xs_json.h"
  7. #include "xs_io.h"
  8. #include "xs_time.h"
  9. #include "xs_glob.h"
  10. #include "xs_set.h"
  11. #include "snac.h"
  12. static xs_str *random_str(void)
  13. /* just what is says in the tin */
  14. {
  15. unsigned int data[4] = {0};
  16. FILE *f;
  17. if ((f = fopen("/dev/random", "r")) != NULL) {
  18. fread(data, sizeof(data), 1, f);
  19. fclose(f);
  20. }
  21. else {
  22. data[0] = random() % 0xffffffff;
  23. data[1] = random() % 0xffffffff;
  24. data[2] = random() % 0xffffffff;
  25. data[3] = random() % 0xffffffff;
  26. }
  27. return xs_hex_enc((char *)data, sizeof(data));
  28. }
  29. int app_add(const char *id, const xs_dict *app)
  30. /* stores an app */
  31. {
  32. if (!xs_is_hex(id))
  33. return 500;
  34. int status = 201;
  35. xs *fn = xs_fmt("%s/app/", srv_basedir);
  36. FILE *f;
  37. mkdirx(fn);
  38. fn = xs_str_cat(fn, id);
  39. fn = xs_str_cat(fn, ".json");
  40. if ((f = fopen(fn, "w")) != NULL) {
  41. xs *j = xs_json_dumps_pp(app, 4);
  42. fwrite(j, strlen(j), 1, f);
  43. fclose(f);
  44. }
  45. else
  46. status = 500;
  47. return status;
  48. }
  49. xs_str *_app_fn(const char *id)
  50. {
  51. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  52. }
  53. xs_dict *app_get(const char *id)
  54. /* gets an app */
  55. {
  56. if (!xs_is_hex(id))
  57. return NULL;
  58. xs *fn = _app_fn(id);
  59. xs_dict *app = NULL;
  60. FILE *f;
  61. if ((f = fopen(fn, "r")) != NULL) {
  62. xs *j = xs_readall(f);
  63. fclose(f);
  64. app = xs_json_loads(j);
  65. }
  66. return app;
  67. }
  68. int app_del(const char *id)
  69. /* deletes an app */
  70. {
  71. if (!xs_is_hex(id))
  72. return -1;
  73. xs *fn = _app_fn(id);
  74. return unlink(fn);
  75. }
  76. int token_add(const char *id, const xs_dict *token)
  77. /* stores a token */
  78. {
  79. if (!xs_is_hex(id))
  80. return 500;
  81. int status = 201;
  82. xs *fn = xs_fmt("%s/token/", srv_basedir);
  83. FILE *f;
  84. mkdirx(fn);
  85. fn = xs_str_cat(fn, id);
  86. fn = xs_str_cat(fn, ".json");
  87. if ((f = fopen(fn, "w")) != NULL) {
  88. xs *j = xs_json_dumps_pp(token, 4);
  89. fwrite(j, strlen(j), 1, f);
  90. fclose(f);
  91. }
  92. else
  93. status = 500;
  94. return status;
  95. }
  96. xs_dict *token_get(const char *id)
  97. /* gets a token */
  98. {
  99. if (!xs_is_hex(id))
  100. return NULL;
  101. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  102. xs_dict *token = NULL;
  103. FILE *f;
  104. if ((f = fopen(fn, "r")) != NULL) {
  105. xs *j = xs_readall(f);
  106. fclose(f);
  107. token = xs_json_loads(j);
  108. }
  109. return token;
  110. }
  111. int token_del(const char *id)
  112. /* deletes a token */
  113. {
  114. if (!xs_is_hex(id))
  115. return -1;
  116. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  117. return unlink(fn);
  118. }
  119. const char *login_page = ""
  120. "<!DOCTYPE html>\n"
  121. "<body><h1>%s OAuth identify</h1>\n"
  122. "<div style=\"background-color: red; color: white\">%s</div>\n"
  123. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  124. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  125. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  126. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  127. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  128. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  129. "<input type=\"submit\" value=\"OK\">\n"
  130. "</form><p>%s</p></body>\n"
  131. "";
  132. int oauth_get_handler(const xs_dict *req, const char *q_path,
  133. char **body, int *b_size, char **ctype)
  134. {
  135. (void)b_size;
  136. if (!xs_startswith(q_path, "/oauth/"))
  137. return 0;
  138. /* {
  139. xs *j = xs_json_dumps_pp(req, 4);
  140. printf("oauth get:\n%s\n", j);
  141. }*/
  142. int status = 404;
  143. xs_dict *msg = xs_dict_get(req, "q_vars");
  144. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  145. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  146. if (strcmp(cmd, "/authorize") == 0) { /** **/
  147. const char *cid = xs_dict_get(msg, "client_id");
  148. const char *ruri = xs_dict_get(msg, "redirect_uri");
  149. const char *rtype = xs_dict_get(msg, "response_type");
  150. const char *state = xs_dict_get(msg, "state");
  151. status = 400;
  152. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  153. xs *app = app_get(cid);
  154. if (app != NULL) {
  155. const char *host = xs_dict_get(srv_config, "host");
  156. if (xs_is_null(state))
  157. state = "";
  158. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-login",
  159. ruri, cid, state, USER_AGENT);
  160. *ctype = "text/html";
  161. status = 200;
  162. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  163. }
  164. else
  165. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  166. }
  167. else
  168. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  169. }
  170. else
  171. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  172. const char *host = xs_dict_get(srv_config, "host");
  173. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-get-token",
  174. "", "", "", USER_AGENT);
  175. *ctype = "text/html";
  176. status = 200;
  177. }
  178. return status;
  179. }
  180. int oauth_post_handler(const xs_dict *req, const char *q_path,
  181. const char *payload, int p_size,
  182. char **body, int *b_size, char **ctype)
  183. {
  184. (void)p_size;
  185. (void)b_size;
  186. if (!xs_startswith(q_path, "/oauth/"))
  187. return 0;
  188. /* {
  189. xs *j = xs_json_dumps_pp(req, 4);
  190. printf("oauth post:\n%s\n", j);
  191. }*/
  192. int status = 404;
  193. char *i_ctype = xs_dict_get(req, "content-type");
  194. xs *args = NULL;
  195. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  196. args = xs_json_loads(payload);
  197. else
  198. args = xs_dup(xs_dict_get(req, "p_vars"));
  199. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  200. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  201. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  202. const char *login = xs_dict_get(args, "login");
  203. const char *passwd = xs_dict_get(args, "passwd");
  204. const char *redir = xs_dict_get(args, "redir");
  205. const char *cid = xs_dict_get(args, "cid");
  206. const char *state = xs_dict_get(args, "state");
  207. const char *host = xs_dict_get(srv_config, "host");
  208. /* by default, generate another login form with an error */
  209. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  210. redir, cid, state, USER_AGENT);
  211. *ctype = "text/html";
  212. status = 200;
  213. if (login && passwd && redir && cid) {
  214. snac snac;
  215. if (user_open(&snac, login)) {
  216. /* check the login + password */
  217. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  218. /* success! redirect to the desired uri */
  219. xs *code = random_str();
  220. xs_free(*body);
  221. *body = xs_fmt("%s?code=%s", redir, code);
  222. status = 303;
  223. /* if there is a state, add it */
  224. if (!xs_is_null(state) && *state) {
  225. *body = xs_str_cat(*body, "&state=");
  226. *body = xs_str_cat(*body, state);
  227. }
  228. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  229. login, *body));
  230. /* assign the login to the app */
  231. xs *app = app_get(cid);
  232. if (app != NULL) {
  233. app = xs_dict_set(app, "uid", login);
  234. app = xs_dict_set(app, "code", code);
  235. app_add(cid, app);
  236. }
  237. else
  238. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  239. }
  240. else
  241. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  242. user_free(&snac);
  243. }
  244. else
  245. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  246. }
  247. else
  248. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  249. }
  250. else
  251. if (strcmp(cmd, "/token") == 0) { /** **/
  252. xs *wrk = NULL;
  253. const char *gtype = xs_dict_get(args, "grant_type");
  254. const char *code = xs_dict_get(args, "code");
  255. const char *cid = xs_dict_get(args, "client_id");
  256. const char *csec = xs_dict_get(args, "client_secret");
  257. const char *ruri = xs_dict_get(args, "redirect_uri");
  258. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  259. but if it's enabled, it makes it crash after some more steps, which
  260. is FAR WORSE */
  261. // const char *scope = xs_dict_get(args, "scope");
  262. const char *scope = NULL;
  263. /* no client_secret? check if it's inside an authorization header
  264. (AndStatus does it this way) */
  265. if (xs_is_null(csec)) {
  266. const char *auhdr = xs_dict_get(req, "authorization");
  267. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  268. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  269. int size;
  270. xs *s2 = xs_base64_dec(s1, &size);
  271. if (!xs_is_null(s2)) {
  272. xs *l1 = xs_split(s2, ":");
  273. if (xs_list_len(l1) == 2) {
  274. wrk = xs_dup(xs_list_get(l1, 1));
  275. csec = wrk;
  276. }
  277. }
  278. }
  279. }
  280. if (gtype && code && cid && csec && ruri) {
  281. xs *app = app_get(cid);
  282. if (app == NULL) {
  283. status = 401;
  284. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  285. }
  286. else
  287. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  288. status = 401;
  289. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  290. }
  291. else {
  292. xs *rsp = xs_dict_new();
  293. xs *cat = xs_number_new(time(NULL));
  294. xs *tokid = random_str();
  295. rsp = xs_dict_append(rsp, "access_token", tokid);
  296. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  297. rsp = xs_dict_append(rsp, "created_at", cat);
  298. if (!xs_is_null(scope))
  299. rsp = xs_dict_append(rsp, "scope", scope);
  300. *body = xs_json_dumps_pp(rsp, 4);
  301. *ctype = "application/json";
  302. status = 200;
  303. const char *uid = xs_dict_get(app, "uid");
  304. srv_debug(1, xs_fmt("oauth token: "
  305. "successful login for %s, new token %s", uid, tokid));
  306. xs *token = xs_dict_new();
  307. token = xs_dict_append(token, "token", tokid);
  308. token = xs_dict_append(token, "client_id", cid);
  309. token = xs_dict_append(token, "client_secret", csec);
  310. token = xs_dict_append(token, "uid", uid);
  311. token = xs_dict_append(token, "code", code);
  312. token_add(tokid, token);
  313. }
  314. }
  315. else {
  316. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  317. status = 400;
  318. }
  319. }
  320. else
  321. if (strcmp(cmd, "/revoke") == 0) { /** **/
  322. const char *cid = xs_dict_get(args, "client_id");
  323. const char *csec = xs_dict_get(args, "client_secret");
  324. const char *tokid = xs_dict_get(args, "token");
  325. if (cid && csec && tokid) {
  326. xs *token = token_get(tokid);
  327. *body = xs_str_new("{}");
  328. *ctype = "application/json";
  329. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  330. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  331. status = 403;
  332. }
  333. else {
  334. token_del(tokid);
  335. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  336. status = 200;
  337. /* also delete the app, as it serves no purpose from now on */
  338. app_del(cid);
  339. }
  340. }
  341. else {
  342. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  343. status = 403;
  344. }
  345. }
  346. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  347. const char *login = xs_dict_get(args, "login");
  348. const char *passwd = xs_dict_get(args, "passwd");
  349. const char *host = xs_dict_get(srv_config, "host");
  350. /* by default, generate another login form with an error */
  351. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  352. "", "", "", USER_AGENT);
  353. *ctype = "text/html";
  354. status = 200;
  355. if (login && passwd) {
  356. snac user;
  357. if (user_open(&user, login)) {
  358. /* check the login + password */
  359. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  360. /* success! create a new token */
  361. xs *tokid = random_str();
  362. srv_debug(1, xs_fmt("x-snac-new-token: "
  363. "successful login for %s, new token %s", login, tokid));
  364. xs *token = xs_dict_new();
  365. token = xs_dict_append(token, "token", tokid);
  366. token = xs_dict_append(token, "client_id", "snac-client");
  367. token = xs_dict_append(token, "client_secret", "");
  368. token = xs_dict_append(token, "uid", login);
  369. token = xs_dict_append(token, "code", "");
  370. token_add(tokid, token);
  371. *ctype = "text/plain";
  372. xs_free(*body);
  373. *body = xs_dup(tokid);
  374. }
  375. user_free(&user);
  376. }
  377. }
  378. }
  379. return status;
  380. }
  381. xs_str *mastoapi_id(const xs_dict *msg)
  382. /* returns a somewhat Mastodon-compatible status id */
  383. {
  384. const char *id = xs_dict_get(msg, "id");
  385. xs *md5 = xs_md5_hex(id, strlen(id));
  386. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  387. }
  388. #define MID_TO_MD5(id) (id + 10)
  389. xs_dict *mastoapi_account(const xs_dict *actor)
  390. /* converts an ActivityPub actor to a Mastodon account */
  391. {
  392. xs_dict *acct = xs_dict_new();
  393. const char *prefu = xs_dict_get(actor, "preferredUsername");
  394. const char *display_name = xs_dict_get(actor, "name");
  395. if (xs_is_null(display_name) || *display_name == '\0')
  396. display_name = prefu;
  397. const char *id = xs_dict_get(actor, "id");
  398. const char *pub = xs_dict_get(actor, "published");
  399. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  400. acct = xs_dict_append(acct, "id", acct_md5);
  401. acct = xs_dict_append(acct, "username", prefu);
  402. acct = xs_dict_append(acct, "display_name", display_name);
  403. {
  404. /* create the acct field as user@host */
  405. xs *l = xs_split(id, "/");
  406. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  407. acct = xs_dict_append(acct, "acct", fquid);
  408. }
  409. if (pub)
  410. acct = xs_dict_append(acct, "created_at", pub);
  411. else {
  412. /* unset created_at crashes Tusky, so lie like a mf */
  413. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  414. acct = xs_dict_append(acct, "created_at", date);
  415. }
  416. const char *note = xs_dict_get(actor, "summary");
  417. if (xs_is_null(note))
  418. note = "";
  419. acct = xs_dict_append(acct, "note", note);
  420. acct = xs_dict_append(acct, "url", id);
  421. xs *avatar = NULL;
  422. xs_dict *av = xs_dict_get(actor, "icon");
  423. if (xs_type(av) == XSTYPE_DICT)
  424. avatar = xs_dup(xs_dict_get(av, "url"));
  425. else
  426. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  427. acct = xs_dict_append(acct, "avatar", avatar);
  428. /* emojis */
  429. xs_list *p;
  430. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  431. xs *eml = xs_list_new();
  432. xs_dict *v;
  433. xs *t = xs_val_new(XSTYPE_TRUE);
  434. while (xs_list_iter(&p, &v)) {
  435. const char *type = xs_dict_get(v, "type");
  436. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  437. const char *name = xs_dict_get(v, "name");
  438. const xs_dict *icon = xs_dict_get(v, "icon");
  439. if (!xs_is_null(name) && !xs_is_null(icon)) {
  440. const char *url = xs_dict_get(icon, "url");
  441. if (!xs_is_null(url)) {
  442. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  443. xs *d1 = xs_dict_new();
  444. d1 = xs_dict_append(d1, "shortcode", nm);
  445. d1 = xs_dict_append(d1, "url", url);
  446. d1 = xs_dict_append(d1, "static_url", url);
  447. d1 = xs_dict_append(d1, "visible_in_picker", t);
  448. eml = xs_list_append(eml, d1);
  449. }
  450. }
  451. }
  452. }
  453. acct = xs_dict_append(acct, "emojis", eml);
  454. }
  455. acct = xs_dict_append(acct, "header", "");
  456. return acct;
  457. }
  458. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  459. /* converts an ActivityPub note to a Mastodon status */
  460. {
  461. xs *actor = NULL;
  462. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  463. /* if the author is not here, discard */
  464. if (actor == NULL)
  465. return NULL;
  466. xs *acct = mastoapi_account(actor);
  467. xs *f = xs_val_new(XSTYPE_FALSE);
  468. xs *t = xs_val_new(XSTYPE_TRUE);
  469. xs *n = xs_val_new(XSTYPE_NULL);
  470. xs *idx = NULL;
  471. xs *ixc = NULL;
  472. char *tmp;
  473. char *id = xs_dict_get(msg, "id");
  474. xs *mid = mastoapi_id(msg);
  475. xs_dict *st = xs_dict_new();
  476. st = xs_dict_append(st, "id", mid);
  477. st = xs_dict_append(st, "uri", id);
  478. st = xs_dict_append(st, "url", id);
  479. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  480. st = xs_dict_append(st, "account", acct);
  481. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  482. st = xs_dict_append(st, "visibility",
  483. is_msg_public(snac, msg) ? "public" : "private");
  484. tmp = xs_dict_get(msg, "sensitive");
  485. if (xs_is_null(tmp))
  486. tmp = f;
  487. st = xs_dict_append(st, "sensitive", tmp);
  488. tmp = xs_dict_get(msg, "summary");
  489. if (xs_is_null(tmp))
  490. tmp = "";
  491. st = xs_dict_append(st, "spoiler_text", tmp);
  492. /* create the list of attachments */
  493. xs *matt = xs_list_new();
  494. xs_list *att = xs_dict_get(msg, "attachment");
  495. xs_str *aobj;
  496. while (xs_list_iter(&att, &aobj)) {
  497. const char *mtype = xs_dict_get(aobj, "mediaType");
  498. if (!xs_is_null(mtype)) {
  499. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  500. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  501. xs *matte = xs_dict_new();
  502. matte = xs_dict_append(matte, "id", matteid);
  503. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  504. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  505. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  506. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  507. const char *name = xs_dict_get(aobj, "name");
  508. if (xs_is_null(name))
  509. name = "";
  510. matte = xs_dict_append(matte, "description", name);
  511. matt = xs_list_append(matt, matte);
  512. }
  513. }
  514. }
  515. st = xs_dict_append(st, "media_attachments", matt);
  516. {
  517. xs *ml = xs_list_new();
  518. xs *htl = xs_list_new();
  519. xs *eml = xs_list_new();
  520. xs_list *p = xs_dict_get(msg, "tag");
  521. xs_dict *v;
  522. int n = 0;
  523. while (xs_list_iter(&p, &v)) {
  524. const char *type = xs_dict_get(v, "type");
  525. if (xs_is_null(type))
  526. continue;
  527. xs *d1 = xs_dict_new();
  528. if (strcmp(type, "Mention") == 0) {
  529. const char *name = xs_dict_get(v, "name");
  530. const char *href = xs_dict_get(v, "href");
  531. if (!xs_is_null(name) && !xs_is_null(href) &&
  532. strcmp(href, snac->actor) != 0) {
  533. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  534. xs *id = xs_fmt("%d", n++);
  535. d1 = xs_dict_append(d1, "id", id);
  536. d1 = xs_dict_append(d1, "username", nm);
  537. d1 = xs_dict_append(d1, "acct", nm);
  538. d1 = xs_dict_append(d1, "url", href);
  539. ml = xs_list_append(ml, d1);
  540. }
  541. }
  542. else
  543. if (strcmp(type, "Hashtag") == 0) {
  544. const char *name = xs_dict_get(v, "name");
  545. const char *href = xs_dict_get(v, "href");
  546. if (!xs_is_null(name) && !xs_is_null(href)) {
  547. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  548. d1 = xs_dict_append(d1, "name", nm);
  549. d1 = xs_dict_append(d1, "url", href);
  550. htl = xs_list_append(htl, d1);
  551. }
  552. }
  553. else
  554. if (strcmp(type, "Emoji") == 0) {
  555. const char *name = xs_dict_get(v, "name");
  556. const xs_dict *icon = xs_dict_get(v, "icon");
  557. if (!xs_is_null(name) && !xs_is_null(icon)) {
  558. const char *url = xs_dict_get(icon, "url");
  559. if (!xs_is_null(url)) {
  560. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  561. xs *t = xs_val_new(XSTYPE_TRUE);
  562. d1 = xs_dict_append(d1, "shortcode", nm);
  563. d1 = xs_dict_append(d1, "url", url);
  564. d1 = xs_dict_append(d1, "static_url", url);
  565. d1 = xs_dict_append(d1, "visible_in_picker", t);
  566. d1 = xs_dict_append(d1, "category", "Emojis");
  567. eml = xs_list_append(eml, d1);
  568. }
  569. }
  570. }
  571. }
  572. st = xs_dict_append(st, "mentions", ml);
  573. st = xs_dict_append(st, "tags", htl);
  574. st = xs_dict_append(st, "emojis", eml);
  575. }
  576. xs_free(idx);
  577. xs_free(ixc);
  578. idx = object_likes(id);
  579. ixc = xs_number_new(xs_list_len(idx));
  580. st = xs_dict_append(st, "favourites_count", ixc);
  581. st = xs_dict_append(st, "favourited",
  582. xs_list_in(idx, snac->md5) != -1 ? t : f);
  583. xs_free(idx);
  584. xs_free(ixc);
  585. idx = object_announces(id);
  586. ixc = xs_number_new(xs_list_len(idx));
  587. st = xs_dict_append(st, "reblogs_count", ixc);
  588. st = xs_dict_append(st, "reblogged",
  589. xs_list_in(idx, snac->md5) != -1 ? t : f);
  590. xs_free(idx);
  591. xs_free(ixc);
  592. idx = object_children(id);
  593. ixc = xs_number_new(xs_list_len(idx));
  594. st = xs_dict_append(st, "replies_count", ixc);
  595. /* default in_reply_to values */
  596. st = xs_dict_append(st, "in_reply_to_id", n);
  597. st = xs_dict_append(st, "in_reply_to_account_id", n);
  598. tmp = xs_dict_get(msg, "inReplyTo");
  599. if (!xs_is_null(tmp)) {
  600. xs *irto = NULL;
  601. if (valid_status(object_get(tmp, &irto))) {
  602. xs *irt_mid = mastoapi_id(irto);
  603. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  604. char *at = NULL;
  605. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  606. xs *at_md5 = xs_md5_hex(at, strlen(at));
  607. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  608. }
  609. }
  610. }
  611. st = xs_dict_append(st, "reblog", n);
  612. st = xs_dict_append(st, "poll", n);
  613. st = xs_dict_append(st, "card", n);
  614. st = xs_dict_append(st, "language", n);
  615. tmp = xs_dict_get(msg, "sourceContent");
  616. if (xs_is_null(tmp))
  617. tmp = "";
  618. st = xs_dict_append(st, "text", tmp);
  619. tmp = xs_dict_get(msg, "updated");
  620. if (xs_is_null(tmp))
  621. tmp = n;
  622. st = xs_dict_append(st, "edited_at", tmp);
  623. return st;
  624. }
  625. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  626. {
  627. xs_dict *rel = NULL;
  628. xs *actor_o = NULL;
  629. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  630. xs *t = xs_val_new(XSTYPE_TRUE);
  631. xs *f = xs_val_new(XSTYPE_FALSE);
  632. rel = xs_dict_new();
  633. const char *actor = xs_dict_get(actor_o, "id");
  634. rel = xs_dict_append(rel, "id", md5);
  635. rel = xs_dict_append(rel, "following",
  636. following_check(snac, actor) ? t : f);
  637. rel = xs_dict_append(rel, "showing_reblogs", t);
  638. rel = xs_dict_append(rel, "notifying", f);
  639. rel = xs_dict_append(rel, "followed_by",
  640. follower_check(snac, actor) ? t : f);
  641. rel = xs_dict_append(rel, "blocking",
  642. is_muted(snac, actor) ? t : f);
  643. rel = xs_dict_append(rel, "muting", f);
  644. rel = xs_dict_append(rel, "muting_notifications", f);
  645. rel = xs_dict_append(rel, "requested", f);
  646. rel = xs_dict_append(rel, "domain_blocking", f);
  647. rel = xs_dict_append(rel, "endorsed", f);
  648. rel = xs_dict_append(rel, "note", "");
  649. }
  650. return rel;
  651. }
  652. int process_auth_token(snac *snac, const xs_dict *req)
  653. /* processes an authorization token, if there is one */
  654. {
  655. int logged_in = 0;
  656. char *v;
  657. /* if there is an authorization field, try to validate it */
  658. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  659. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  660. xs *token = token_get(tokid);
  661. if (token != NULL) {
  662. const char *uid = xs_dict_get(token, "uid");
  663. if (!xs_is_null(uid) && user_open(snac, uid)) {
  664. logged_in = 1;
  665. /* this counts as a 'login' */
  666. lastlog_write(snac, "mastoapi");
  667. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  668. }
  669. else
  670. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  671. }
  672. else
  673. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  674. }
  675. return logged_in;
  676. }
  677. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  678. char **body, int *b_size, char **ctype)
  679. {
  680. (void)b_size;
  681. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  682. return 0;
  683. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  684. /* {
  685. xs *j = xs_json_dumps_pp(req, 4);
  686. printf("mastoapi get:\n%s\n", j);
  687. }*/
  688. int status = 404;
  689. xs_dict *args = xs_dict_get(req, "q_vars");
  690. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  691. snac snac1 = {0};
  692. int logged_in = process_auth_token(&snac1, req);
  693. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  694. if (logged_in) {
  695. xs *acct = xs_dict_new();
  696. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  697. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  698. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  699. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  700. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  701. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  702. acct = xs_dict_append(acct, "url", snac1.actor);
  703. acct = xs_dict_append(acct, "header", "");
  704. xs *avatar = NULL;
  705. char *av = xs_dict_get(snac1.config, "avatar");
  706. if (xs_is_null(av) || *av == '\0')
  707. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  708. else
  709. avatar = xs_dup(av);
  710. acct = xs_dict_append(acct, "avatar", avatar);
  711. *body = xs_json_dumps_pp(acct, 4);
  712. *ctype = "application/json";
  713. status = 200;
  714. }
  715. else {
  716. status = 422; // "Unprocessable entity" (no login)
  717. }
  718. }
  719. else
  720. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  721. /* find if an account is followed, blocked, etc. */
  722. /* the account to get relationships about is in args "id[]" */
  723. if (logged_in) {
  724. xs *res = xs_list_new();
  725. const char *md5 = xs_dict_get(args, "id[]");
  726. if (!xs_is_null(md5)) {
  727. if (xs_type(md5) == XSTYPE_LIST)
  728. md5 = xs_list_get(md5, 0);
  729. xs *rel = mastoapi_relationship(&snac1, md5);
  730. if (rel != NULL)
  731. res = xs_list_append(res, rel);
  732. }
  733. *body = xs_json_dumps_pp(res, 4);
  734. *ctype = "application/json";
  735. status = 200;
  736. }
  737. else
  738. status = 422;
  739. }
  740. else
  741. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  742. /* account-related information */
  743. xs *l = xs_split(cmd, "/");
  744. const char *uid = xs_list_get(l, 3);
  745. const char *opt = xs_list_get(l, 4);
  746. if (uid != NULL) {
  747. snac snac2;
  748. xs *out = NULL;
  749. xs *actor = NULL;
  750. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  751. /* search for accounts starting with q */
  752. const char *aq = xs_dict_get(args, "q");
  753. if (!xs_is_null(aq)) {
  754. xs *q = xs_tolower_i(xs_dup(aq));
  755. out = xs_list_new();
  756. xs *wing = following_list(&snac1);
  757. xs *wers = follower_list(&snac1);
  758. xs *ulst = user_list();
  759. xs_list *p;
  760. xs_str *v;
  761. xs_set seen;
  762. xs_set_init(&seen);
  763. /* user relations */
  764. xs_list *lsts[] = { wing, wers, NULL };
  765. int n;
  766. for (n = 0; (p = lsts[n]) != NULL; n++) {
  767. while (xs_list_iter(&p, &v)) {
  768. /* already seen? skip */
  769. if (xs_set_add(&seen, v) == 0)
  770. continue;
  771. xs *actor = NULL;
  772. if (valid_status(object_get(v, &actor))) {
  773. const char *uname = xs_dict_get(actor, "preferredUsername");
  774. if (!xs_is_null(uname)) {
  775. xs *luname = xs_tolower_i(xs_dup(uname));
  776. if (xs_startswith(luname, q)) {
  777. xs *acct = mastoapi_account(actor);
  778. out = xs_list_append(out, acct);
  779. }
  780. }
  781. }
  782. }
  783. }
  784. /* local users */
  785. p = ulst;
  786. while (xs_list_iter(&p, &v)) {
  787. snac user;
  788. /* skip this same user */
  789. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  790. continue;
  791. /* skip if the uid does not start with the query */
  792. xs *v2 = xs_tolower_i(xs_dup(v));
  793. if (!xs_startswith(v2, q))
  794. continue;
  795. if (user_open(&user, v)) {
  796. /* if it's not already seen, add it */
  797. if (xs_set_add(&seen, user.actor) == 1) {
  798. xs *actor = msg_actor(&user);
  799. xs *acct = mastoapi_account(actor);
  800. out = xs_list_append(out, acct);
  801. }
  802. user_free(&user);
  803. }
  804. }
  805. xs_set_free(&seen);
  806. }
  807. }
  808. else
  809. /* is it a local user? */
  810. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  811. if (opt == NULL) {
  812. /* account information */
  813. actor = msg_actor(&snac2);
  814. out = mastoapi_account(actor);
  815. }
  816. else
  817. if (strcmp(opt, "statuses") == 0) { /** **/
  818. /* the public list of posts of a user */
  819. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  820. xs_list *p = timeline;
  821. xs_str *v;
  822. out = xs_list_new();
  823. while (xs_list_iter(&p, &v)) {
  824. xs *msg = NULL;
  825. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  826. /* add only posts by the author */
  827. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  828. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  829. xs *st = mastoapi_status(&snac2, msg);
  830. out = xs_list_append(out, st);
  831. }
  832. }
  833. }
  834. }
  835. user_free(&snac2);
  836. }
  837. else {
  838. /* try the uid as the md5 of a possibly loaded actor */
  839. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  840. if (opt == NULL) {
  841. /* account information */
  842. out = mastoapi_account(actor);
  843. }
  844. else
  845. if (strcmp(opt, "statuses") == 0) {
  846. /* we don't serve statuses of others; return the empty list */
  847. out = xs_list_new();
  848. }
  849. }
  850. }
  851. if (out != NULL) {
  852. *body = xs_json_dumps_pp(out, 4);
  853. *ctype = "application/json";
  854. status = 200;
  855. }
  856. }
  857. }
  858. else
  859. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  860. /* the private timeline */
  861. if (logged_in) {
  862. const char *max_id = xs_dict_get(args, "max_id");
  863. const char *since_id = xs_dict_get(args, "since_id");
  864. const char *min_id = xs_dict_get(args, "min_id");
  865. const char *limit_s = xs_dict_get(args, "limit");
  866. int limit = 0;
  867. int cnt = 0;
  868. if (!xs_is_null(limit_s))
  869. limit = atoi(limit_s);
  870. if (limit == 0)
  871. limit = 20;
  872. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  873. xs *out = xs_list_new();
  874. xs_list *p = timeline;
  875. xs_str *v;
  876. while (xs_list_iter(&p, &v) && cnt < limit) {
  877. xs *msg = NULL;
  878. /* only return entries older that max_id */
  879. if (max_id) {
  880. if (strcmp(v, max_id) == 0)
  881. max_id = NULL;
  882. continue;
  883. }
  884. /* only returns entries newer than since_id */
  885. if (since_id) {
  886. if (strcmp(v, since_id) == 0)
  887. break;
  888. }
  889. /* only returns entries newer than min_id */
  890. /* what does really "Return results immediately newer than ID" mean? */
  891. if (min_id) {
  892. if (strcmp(v, min_id) == 0)
  893. break;
  894. }
  895. /* get the entry */
  896. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  897. continue;
  898. /* discard non-Notes */
  899. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  900. continue;
  901. /* discard notes from muted morons */
  902. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  903. continue;
  904. /* discard hidden notes */
  905. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  906. continue;
  907. /* discard poll votes (they have a name) */
  908. if (!xs_is_null(xs_dict_get(msg, "name")))
  909. continue;
  910. /* convert the Note into a Mastodon status */
  911. xs *st = mastoapi_status(&snac1, msg);
  912. if (st != NULL)
  913. out = xs_list_append(out, st);
  914. cnt++;
  915. }
  916. *body = xs_json_dumps_pp(out, 4);
  917. *ctype = "application/json";
  918. status = 200;
  919. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  920. }
  921. else {
  922. status = 401; // unauthorized
  923. }
  924. }
  925. else
  926. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  927. /* the instance public timeline (public timelines for all users) */
  928. /* NOTE: this api call needs no authorization; but,
  929. I need a logged-in user in mastoapi_status() for
  930. is_msg_public() and the liked/boosted flags,
  931. so it will silently fail for pure public access */
  932. const char *limit_s = xs_dict_get(args, "limit");
  933. int limit = 0;
  934. int cnt = 0;
  935. if (!xs_is_null(limit_s))
  936. limit = atoi(limit_s);
  937. if (limit == 0)
  938. limit = 20;
  939. xs *timeline = timeline_instance_list(0, limit);
  940. xs *out = xs_list_new();
  941. xs_list *p = timeline;
  942. xs_str *md5;
  943. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  944. xs *msg = NULL;
  945. /* get the entry */
  946. if (!valid_status(object_get_by_md5(md5, &msg)))
  947. continue;
  948. /* discard non-Notes */
  949. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  950. continue;
  951. /* convert the Note into a Mastodon status */
  952. xs *st = mastoapi_status(&snac1, msg);
  953. if (st != NULL) {
  954. out = xs_list_append(out, st);
  955. cnt++;
  956. }
  957. }
  958. *body = xs_json_dumps_pp(out, 4);
  959. *ctype = "application/json";
  960. status = 200;
  961. }
  962. else
  963. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  964. /* TBD */
  965. *body = xs_dup("[]");
  966. *ctype = "application/json";
  967. status = 200;
  968. }
  969. else
  970. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  971. if (logged_in) {
  972. xs *l = notify_list(&snac1, 0);
  973. xs *out = xs_list_new();
  974. xs_list *p = l;
  975. xs_dict *v;
  976. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  977. while (xs_list_iter(&p, &v)) {
  978. xs *noti = notify_get(&snac1, v);
  979. if (noti == NULL)
  980. continue;
  981. const char *type = xs_dict_get(noti, "type");
  982. const char *utype = xs_dict_get(noti, "utype");
  983. const char *objid = xs_dict_get(noti, "objid");
  984. xs *actor = NULL;
  985. xs *entry = NULL;
  986. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  987. continue;
  988. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  989. continue;
  990. if (is_hidden(&snac1, objid))
  991. continue;
  992. /* convert the type */
  993. if (strcmp(type, "Like") == 0)
  994. type = "favourite";
  995. else
  996. if (strcmp(type, "Announce") == 0)
  997. type = "reblog";
  998. else
  999. if (strcmp(type, "Follow") == 0)
  1000. type = "follow";
  1001. else
  1002. if (strcmp(type, "Create") == 0)
  1003. type = "mention";
  1004. else
  1005. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1006. type = "poll";
  1007. else
  1008. continue;
  1009. /* excluded type? */
  1010. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1011. continue;
  1012. xs *mn = xs_dict_new();
  1013. mn = xs_dict_append(mn, "type", type);
  1014. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1015. mn = xs_dict_append(mn, "id", id);
  1016. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1017. xs *acct = mastoapi_account(actor);
  1018. mn = xs_dict_append(mn, "account", acct);
  1019. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1020. xs *st = mastoapi_status(&snac1, entry);
  1021. mn = xs_dict_append(mn, "status", st);
  1022. }
  1023. out = xs_list_append(out, mn);
  1024. }
  1025. *body = xs_json_dumps_pp(out, 4);
  1026. *ctype = "application/json";
  1027. status = 200;
  1028. }
  1029. else
  1030. status = 401;
  1031. }
  1032. else
  1033. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1034. /* snac will never have filters */
  1035. *body = xs_dup("[]");
  1036. *ctype = "application/json";
  1037. status = 200;
  1038. }
  1039. else
  1040. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1041. /* snac will never support a list of favourites */
  1042. *body = xs_dup("[]");
  1043. *ctype = "application/json";
  1044. status = 200;
  1045. }
  1046. else
  1047. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1048. /* snac does not support bookmarks */
  1049. *body = xs_dup("[]");
  1050. *ctype = "application/json";
  1051. status = 200;
  1052. }
  1053. else
  1054. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1055. /* snac does not support lists */
  1056. *body = xs_dup("[]");
  1057. *ctype = "application/json";
  1058. status = 200;
  1059. }
  1060. else
  1061. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1062. /* snac does not schedule notes */
  1063. *body = xs_dup("[]");
  1064. *ctype = "application/json";
  1065. status = 200;
  1066. }
  1067. else
  1068. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1069. /* snac does not support optional follow confirmations */
  1070. *body = xs_dup("[]");
  1071. *ctype = "application/json";
  1072. status = 200;
  1073. }
  1074. else
  1075. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1076. /* snac has no announcements (yet?) */
  1077. *body = xs_dup("[]");
  1078. *ctype = "application/json";
  1079. status = 200;
  1080. }
  1081. else
  1082. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1083. /* are you kidding me? */
  1084. *body = xs_dup("[]");
  1085. *ctype = "application/json";
  1086. status = 200;
  1087. }
  1088. else
  1089. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1090. /* returns an instance object */
  1091. xs *ins = xs_dict_new();
  1092. const char *host = xs_dict_get(srv_config, "host");
  1093. ins = xs_dict_append(ins, "uri", host);
  1094. ins = xs_dict_append(ins, "domain", host);
  1095. ins = xs_dict_append(ins, "title", host);
  1096. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1097. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1098. ins = xs_dict_append(ins, "description", host);
  1099. ins = xs_dict_append(ins, "short_description", host);
  1100. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1101. ins = xs_dict_append(ins, "thumbnail", susie);
  1102. const char *v = xs_dict_get(srv_config, "admin_email");
  1103. if (xs_is_null(v) || *v == '\0')
  1104. v = "admin@localhost";
  1105. ins = xs_dict_append(ins, "email", v);
  1106. xs *l1 = xs_list_new();
  1107. ins = xs_dict_append(ins, "rules", l1);
  1108. l1 = xs_list_append(l1, "en");
  1109. ins = xs_dict_append(ins, "languages", l1);
  1110. xs *d1 = xs_dict_new();
  1111. ins = xs_dict_append(ins, "urls", d1);
  1112. xs *z = xs_number_new(0);
  1113. d1 = xs_dict_append(d1, "user_count", z);
  1114. d1 = xs_dict_append(d1, "status_count", z);
  1115. d1 = xs_dict_append(d1, "domain_count", z);
  1116. ins = xs_dict_append(ins, "stats", d1);
  1117. xs *f = xs_val_new(XSTYPE_FALSE);
  1118. ins = xs_dict_append(ins, "registrations", f);
  1119. ins = xs_dict_append(ins, "approval_required", f);
  1120. ins = xs_dict_append(ins, "invites_enabled", f);
  1121. xs *cfg = xs_dict_new();
  1122. {
  1123. xs *d11 = xs_dict_new();
  1124. xs *mc = xs_number_new(100000);
  1125. xs *mm = xs_number_new(8);
  1126. xs *cr = xs_number_new(32);
  1127. d11 = xs_dict_append(d11, "max_characters", mc);
  1128. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1129. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1130. cfg = xs_dict_append(cfg, "statuses", d11);
  1131. }
  1132. ins = xs_dict_append(ins, "configuration", cfg);
  1133. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1134. if (!xs_is_null(admin_account) && *admin_account) {
  1135. snac admin;
  1136. if (user_open(&admin, admin_account)) {
  1137. xs *actor = msg_actor(&admin);
  1138. xs *acct = mastoapi_account(actor);
  1139. ins = xs_dict_append(ins, "contact_account", acct);
  1140. user_free(&admin);
  1141. }
  1142. }
  1143. *body = xs_json_dumps_pp(ins, 4);
  1144. *ctype = "application/json";
  1145. status = 200;
  1146. }
  1147. else
  1148. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1149. /* information about a status */
  1150. xs *l = xs_split(cmd, "/");
  1151. const char *id = xs_list_get(l, 3);
  1152. const char *op = xs_list_get(l, 4);
  1153. if (!xs_is_null(id)) {
  1154. xs *msg = NULL;
  1155. xs *out = NULL;
  1156. /* skip the 'fake' part of the id */
  1157. id = MID_TO_MD5(id);
  1158. if (valid_status(object_get_by_md5(id, &msg))) {
  1159. if (op == NULL) {
  1160. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1161. /* return the status itself */
  1162. out = mastoapi_status(&snac1, msg);
  1163. }
  1164. }
  1165. else
  1166. if (strcmp(op, "context") == 0) { /** **/
  1167. /* return ancestors and children */
  1168. xs *anc = xs_list_new();
  1169. xs *des = xs_list_new();
  1170. xs_list *p;
  1171. xs_str *v;
  1172. char pid[64];
  1173. /* build the [grand]parent list, moving up */
  1174. strncpy(pid, id, sizeof(pid));
  1175. while (object_parent(pid, pid, sizeof(pid))) {
  1176. xs *m2 = NULL;
  1177. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1178. xs *st = mastoapi_status(&snac1, m2);
  1179. anc = xs_list_insert(anc, 0, st);
  1180. }
  1181. else
  1182. break;
  1183. }
  1184. /* build the children list */
  1185. xs *children = object_children(xs_dict_get(msg, "id"));
  1186. p = children;
  1187. while (xs_list_iter(&p, &v)) {
  1188. xs *m2 = NULL;
  1189. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1190. xs *st = mastoapi_status(&snac1, m2);
  1191. des = xs_list_append(des, st);
  1192. }
  1193. }
  1194. out = xs_dict_new();
  1195. out = xs_dict_append(out, "ancestors", anc);
  1196. out = xs_dict_append(out, "descendants", des);
  1197. }
  1198. else
  1199. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1200. strcmp(op, "favourited_by") == 0) { /** **/
  1201. /* return the list of people who liked or boosted this */
  1202. out = xs_list_new();
  1203. xs *l = NULL;
  1204. if (op[0] == 'r')
  1205. l = object_announces(xs_dict_get(msg, "id"));
  1206. else
  1207. l = object_likes(xs_dict_get(msg, "id"));
  1208. xs_list *p = l;
  1209. xs_str *v;
  1210. while (xs_list_iter(&p, &v)) {
  1211. xs *actor2 = NULL;
  1212. if (valid_status(object_get_by_md5(v, &actor2))) {
  1213. xs *acct2 = mastoapi_account(actor2);
  1214. out = xs_list_append(out, acct2);
  1215. }
  1216. }
  1217. }
  1218. }
  1219. else
  1220. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1221. if (out != NULL) {
  1222. *body = xs_json_dumps_pp(out, 4);
  1223. *ctype = "application/json";
  1224. status = 200;
  1225. }
  1226. }
  1227. }
  1228. else
  1229. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1230. *body = xs_dup("{}");
  1231. *ctype = "application/json";
  1232. status = 200;
  1233. }
  1234. else
  1235. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1236. *body = xs_dup("{}");
  1237. *ctype = "application/json";
  1238. status = 200;
  1239. }
  1240. else
  1241. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1242. *body = xs_dup("[]");
  1243. *ctype = "application/json";
  1244. status = 200;
  1245. }
  1246. else
  1247. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1248. const char *q = xs_dict_get(args, "q");
  1249. const char *type = xs_dict_get(args, "type");
  1250. const char *offset = xs_dict_get(args, "offset");
  1251. xs *acl = xs_list_new();
  1252. xs *stl = xs_list_new();
  1253. xs *htl = xs_list_new();
  1254. xs *res = xs_dict_new();
  1255. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1256. /* reply something only for offset 0; otherwise,
  1257. apps like Tusky keep asking again and again */
  1258. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1259. /* do a webfinger query */
  1260. char *actor = NULL;
  1261. char *user = NULL;
  1262. if (valid_status(webfinger_request(q, &actor, &user))) {
  1263. xs *actor_o = NULL;
  1264. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1265. xs *acct = mastoapi_account(actor_o);
  1266. acl = xs_list_append(acl, acct);
  1267. }
  1268. }
  1269. }
  1270. }
  1271. res = xs_dict_append(res, "accounts", acl);
  1272. res = xs_dict_append(res, "statuses", stl);
  1273. res = xs_dict_append(res, "hashtags", htl);
  1274. *body = xs_json_dumps_pp(res, 4);
  1275. *ctype = "application/json";
  1276. status = 200;
  1277. }
  1278. /* user cleanup */
  1279. if (logged_in)
  1280. user_free(&snac1);
  1281. return status;
  1282. }
  1283. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1284. const char *payload, int p_size,
  1285. char **body, int *b_size, char **ctype)
  1286. {
  1287. (void)p_size;
  1288. (void)b_size;
  1289. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1290. return 0;
  1291. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1292. /* {
  1293. xs *j = xs_json_dumps_pp(req, 4);
  1294. printf("mastoapi post:\n%s\n", j);
  1295. }*/
  1296. int status = 404;
  1297. xs *args = NULL;
  1298. char *i_ctype = xs_dict_get(req, "content-type");
  1299. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1300. args = xs_json_loads(payload);
  1301. else
  1302. args = xs_dup(xs_dict_get(req, "p_vars"));
  1303. if (args == NULL)
  1304. return 400;
  1305. /* {
  1306. xs *j = xs_json_dumps_pp(args, 4);
  1307. printf("%s\n", j);
  1308. }*/
  1309. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1310. snac snac = {0};
  1311. int logged_in = process_auth_token(&snac, req);
  1312. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1313. const char *name = xs_dict_get(args, "client_name");
  1314. const char *ruri = xs_dict_get(args, "redirect_uris");
  1315. const char *scope = xs_dict_get(args, "scope");
  1316. if (xs_type(ruri) == XSTYPE_LIST)
  1317. ruri = xs_dict_get(ruri, 0);
  1318. if (name && ruri) {
  1319. xs *app = xs_dict_new();
  1320. xs *id = xs_replace_i(tid(0), ".", "");
  1321. xs *csec = random_str();
  1322. xs *vkey = random_str();
  1323. xs *cid = NULL;
  1324. /* pick a non-existent random cid */
  1325. for (;;) {
  1326. cid = random_str();
  1327. xs *p_app = app_get(cid);
  1328. if (p_app == NULL)
  1329. break;
  1330. xs_free(cid);
  1331. }
  1332. app = xs_dict_append(app, "name", name);
  1333. app = xs_dict_append(app, "redirect_uri", ruri);
  1334. app = xs_dict_append(app, "client_id", cid);
  1335. app = xs_dict_append(app, "client_secret", csec);
  1336. app = xs_dict_append(app, "vapid_key", vkey);
  1337. app = xs_dict_append(app, "id", id);
  1338. *body = xs_json_dumps_pp(app, 4);
  1339. *ctype = "application/json";
  1340. status = 200;
  1341. app = xs_dict_append(app, "code", "");
  1342. if (scope)
  1343. app = xs_dict_append(app, "scope", scope);
  1344. app_add(cid, app);
  1345. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1346. }
  1347. }
  1348. else
  1349. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1350. if (logged_in) {
  1351. /* post a new Note */
  1352. /* {
  1353. xs *j = xs_json_dumps_pp(args, 4);
  1354. printf("%s\n", j);
  1355. }*/
  1356. const char *content = xs_dict_get(args, "status");
  1357. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1358. const char *visibility = xs_dict_get(args, "visibility");
  1359. const char *summary = xs_dict_get(args, "spoiler_text");
  1360. const char *media_ids = xs_dict_get(args, "media_ids");
  1361. if (xs_is_null(media_ids))
  1362. media_ids = xs_dict_get(args, "media_ids[]");
  1363. if (xs_is_null(visibility))
  1364. visibility = "public";
  1365. xs *attach_list = xs_list_new();
  1366. xs *irt = NULL;
  1367. /* is it a reply? */
  1368. if (mid != NULL) {
  1369. xs *r_msg = NULL;
  1370. const char *md5 = MID_TO_MD5(mid);
  1371. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1372. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1373. }
  1374. /* does it have attachments? */
  1375. if (!xs_is_null(media_ids)) {
  1376. xs *mi = NULL;
  1377. if (xs_type(media_ids) == XSTYPE_LIST)
  1378. mi = xs_dup(media_ids);
  1379. else {
  1380. mi = xs_list_new();
  1381. mi = xs_list_append(mi, media_ids);
  1382. }
  1383. xs_list *p = mi;
  1384. xs_str *v;
  1385. while (xs_list_iter(&p, &v)) {
  1386. xs *l = xs_list_new();
  1387. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1388. xs *desc = static_get_meta(&snac, v);
  1389. l = xs_list_append(l, url);
  1390. l = xs_list_append(l, desc);
  1391. attach_list = xs_list_append(attach_list, l);
  1392. }
  1393. }
  1394. /* prepare the message */
  1395. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1396. strcmp(visibility, "public") == 0 ? 0 : 1);
  1397. if (!xs_is_null(summary) && *summary) {
  1398. xs *t = xs_val_new(XSTYPE_TRUE);
  1399. msg = xs_dict_set(msg, "sensitive", t);
  1400. msg = xs_dict_set(msg, "summary", summary);
  1401. }
  1402. /* store */
  1403. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1404. /* 'Create' message */
  1405. xs *c_msg = msg_create(&snac, msg);
  1406. enqueue_message(&snac, c_msg);
  1407. timeline_touch(&snac);
  1408. /* convert to a mastodon status as a response code */
  1409. xs *st = mastoapi_status(&snac, msg);
  1410. *body = xs_json_dumps_pp(st, 4);
  1411. *ctype = "application/json";
  1412. status = 200;
  1413. }
  1414. else
  1415. status = 401;
  1416. }
  1417. else
  1418. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1419. if (logged_in) {
  1420. /* operations on a status */
  1421. xs *l = xs_split(cmd, "/");
  1422. const char *mid = xs_list_get(l, 3);
  1423. const char *op = xs_list_get(l, 4);
  1424. if (!xs_is_null(mid)) {
  1425. xs *msg = NULL;
  1426. xs *out = NULL;
  1427. /* skip the 'fake' part of the id */
  1428. mid = MID_TO_MD5(mid);
  1429. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1430. char *id = xs_dict_get(msg, "id");
  1431. if (op == NULL) {
  1432. /* no operation (?) */
  1433. }
  1434. else
  1435. if (strcmp(op, "favourite") == 0) { /** **/
  1436. xs *n_msg = msg_admiration(&snac, id, "Like");
  1437. if (n_msg != NULL) {
  1438. enqueue_message(&snac, n_msg);
  1439. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1440. out = mastoapi_status(&snac, msg);
  1441. }
  1442. }
  1443. else
  1444. if (strcmp(op, "unfavourite") == 0) { /** **/
  1445. /* partial support: as the original Like message
  1446. is not stored anywhere here, it's not possible
  1447. to send an Undo + Like; the only thing done here
  1448. is to delete the actor from the list of likes */
  1449. object_unadmire(id, snac.actor, 1);
  1450. }
  1451. else
  1452. if (strcmp(op, "reblog") == 0) { /** **/
  1453. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1454. if (n_msg != NULL) {
  1455. enqueue_message(&snac, n_msg);
  1456. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1457. out = mastoapi_status(&snac, msg);
  1458. }
  1459. }
  1460. else
  1461. if (strcmp(op, "unreblog") == 0) { /** **/
  1462. /* partial support: see comment in 'unfavourite' */
  1463. object_unadmire(id, snac.actor, 0);
  1464. }
  1465. else
  1466. if (strcmp(op, "bookmark") == 0) { /** **/
  1467. /* snac does not support bookmarks */
  1468. }
  1469. else
  1470. if (strcmp(op, "unbookmark") == 0) { /** **/
  1471. /* snac does not support bookmarks */
  1472. }
  1473. else
  1474. if (strcmp(op, "pin") == 0) { /** **/
  1475. /* snac does not support pinning */
  1476. }
  1477. else
  1478. if (strcmp(op, "unpin") == 0) { /** **/
  1479. /* snac does not support pinning */
  1480. }
  1481. else
  1482. if (strcmp(op, "mute") == 0) { /** **/
  1483. /* Mastodon's mute is snac's hide */
  1484. }
  1485. else
  1486. if (strcmp(op, "unmute") == 0) { /** **/
  1487. /* Mastodon's unmute is snac's unhide */
  1488. }
  1489. }
  1490. if (out != NULL) {
  1491. *body = xs_json_dumps_pp(out, 4);
  1492. *ctype = "application/json";
  1493. status = 200;
  1494. }
  1495. }
  1496. }
  1497. else
  1498. status = 401;
  1499. }
  1500. else
  1501. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1502. if (logged_in) {
  1503. notify_clear(&snac);
  1504. timeline_touch(&snac);
  1505. *body = xs_dup("{}");
  1506. *ctype = "application/json";
  1507. status = 200;
  1508. }
  1509. else
  1510. status = 401;
  1511. }
  1512. else
  1513. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1514. /* I don't know what I'm doing */
  1515. if (logged_in) {
  1516. char *v;
  1517. xs *wpush = xs_dict_new();
  1518. wpush = xs_dict_append(wpush, "id", "1");
  1519. v = xs_dict_get(args, "data");
  1520. v = xs_dict_get(v, "alerts");
  1521. wpush = xs_dict_append(wpush, "alerts", v);
  1522. v = xs_dict_get(args, "subscription");
  1523. v = xs_dict_get(v, "endpoint");
  1524. wpush = xs_dict_append(wpush, "endpoint", v);
  1525. xs *server_key = random_str();
  1526. wpush = xs_dict_append(wpush, "server_key", server_key);
  1527. *body = xs_json_dumps_pp(wpush, 4);
  1528. *ctype = "application/json";
  1529. status = 200;
  1530. }
  1531. else
  1532. status = 401;
  1533. }
  1534. else
  1535. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1536. if (logged_in) {
  1537. /* {
  1538. xs *j = xs_json_dumps_pp(args, 4);
  1539. printf("%s\n", j);
  1540. }*/
  1541. const xs_list *file = xs_dict_get(args, "file");
  1542. const char *desc = xs_dict_get(args, "description");
  1543. if (xs_is_null(desc))
  1544. desc = "";
  1545. status = 400;
  1546. if (xs_type(file) == XSTYPE_LIST) {
  1547. const char *fn = xs_list_get(file, 0);
  1548. if (*fn != '\0') {
  1549. char *ext = strrchr(fn, '.');
  1550. xs *hash = xs_md5_hex(fn, strlen(fn));
  1551. xs *id = xs_fmt("%s%s", hash, ext);
  1552. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1553. int fo = xs_number_get(xs_list_get(file, 1));
  1554. int fs = xs_number_get(xs_list_get(file, 2));
  1555. /* store */
  1556. static_put(&snac, id, payload + fo, fs);
  1557. static_put_meta(&snac, id, desc);
  1558. /* prepare a response */
  1559. xs *rsp = xs_dict_new();
  1560. rsp = xs_dict_append(rsp, "id", id);
  1561. rsp = xs_dict_append(rsp, "type", "image");
  1562. rsp = xs_dict_append(rsp, "url", url);
  1563. rsp = xs_dict_append(rsp, "preview_url", url);
  1564. rsp = xs_dict_append(rsp, "remote_url", url);
  1565. rsp = xs_dict_append(rsp, "description", desc);
  1566. *body = xs_json_dumps_pp(rsp, 4);
  1567. *ctype = "application/json";
  1568. status = 200;
  1569. }
  1570. }
  1571. }
  1572. else
  1573. status = 401;
  1574. }
  1575. else
  1576. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1577. if (logged_in) {
  1578. /* account-related information */
  1579. xs *l = xs_split(cmd, "/");
  1580. const char *md5 = xs_list_get(l, 3);
  1581. const char *opt = xs_list_get(l, 4);
  1582. xs *rsp = NULL;
  1583. if (!xs_is_null(md5) && *md5) {
  1584. xs *actor_o = NULL;
  1585. if (xs_is_null(opt)) {
  1586. /* ? */
  1587. }
  1588. else
  1589. if (strcmp(opt, "follow") == 0) { /** **/
  1590. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1591. const char *actor = xs_dict_get(actor_o, "id");
  1592. xs *msg = msg_follow(&snac, actor);
  1593. if (msg != NULL) {
  1594. /* reload the actor from the message, in may be different */
  1595. actor = xs_dict_get(msg, "object");
  1596. following_add(&snac, actor, msg);
  1597. enqueue_output_by_actor(&snac, msg, actor, 0);
  1598. rsp = mastoapi_relationship(&snac, md5);
  1599. }
  1600. }
  1601. }
  1602. else
  1603. if (strcmp(opt, "unfollow") == 0) { /** **/
  1604. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1605. const char *actor = xs_dict_get(actor_o, "id");
  1606. /* get the following object */
  1607. xs *object = NULL;
  1608. if (valid_status(following_get(&snac, actor, &object))) {
  1609. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1610. following_del(&snac, actor);
  1611. enqueue_output_by_actor(&snac, msg, actor, 0);
  1612. rsp = mastoapi_relationship(&snac, md5);
  1613. }
  1614. }
  1615. }
  1616. else
  1617. if (strcmp(opt, "block") == 0) { /** **/
  1618. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1619. const char *actor = xs_dict_get(actor_o, "id");
  1620. mute(&snac, actor);
  1621. rsp = mastoapi_relationship(&snac, md5);
  1622. }
  1623. }
  1624. else
  1625. if (strcmp(opt, "unblock") == 0) { /** **/
  1626. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1627. const char *actor = xs_dict_get(actor_o, "id");
  1628. unmute(&snac, actor);
  1629. rsp = mastoapi_relationship(&snac, md5);
  1630. }
  1631. }
  1632. }
  1633. if (rsp != NULL) {
  1634. *body = xs_json_dumps_pp(rsp, 4);
  1635. *ctype = "application/json";
  1636. status = 200;
  1637. }
  1638. }
  1639. else
  1640. status = 401;
  1641. }
  1642. /* user cleanup */
  1643. if (logged_in)
  1644. user_free(&snac);
  1645. return status;
  1646. }
  1647. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1648. const char *payload, int p_size,
  1649. char **body, int *b_size, char **ctype)
  1650. {
  1651. (void)p_size;
  1652. (void)b_size;
  1653. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1654. return 0;
  1655. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1656. /* {
  1657. xs *j = xs_json_dumps_pp(req, 4);
  1658. printf("mastoapi put:\n%s\n", j);
  1659. }*/
  1660. int status = 404;
  1661. xs *args = NULL;
  1662. char *i_ctype = xs_dict_get(req, "content-type");
  1663. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1664. args = xs_json_loads(payload);
  1665. else
  1666. args = xs_dup(xs_dict_get(req, "p_vars"));
  1667. if (args == NULL)
  1668. return 400;
  1669. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1670. snac snac = {0};
  1671. int logged_in = process_auth_token(&snac, req);
  1672. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1673. if (logged_in) {
  1674. xs *l = xs_split(cmd, "/");
  1675. const char *stid = xs_list_get(l, 3);
  1676. if (!xs_is_null(stid)) {
  1677. const char *desc = xs_dict_get(args, "description");
  1678. /* set the image metadata */
  1679. static_put_meta(&snac, stid, desc);
  1680. /* prepare a response */
  1681. xs *rsp = xs_dict_new();
  1682. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1683. rsp = xs_dict_append(rsp, "id", stid);
  1684. rsp = xs_dict_append(rsp, "type", "image");
  1685. rsp = xs_dict_append(rsp, "url", url);
  1686. rsp = xs_dict_append(rsp, "preview_url", url);
  1687. rsp = xs_dict_append(rsp, "remote_url", url);
  1688. rsp = xs_dict_append(rsp, "description", desc);
  1689. *body = xs_json_dumps_pp(rsp, 4);
  1690. *ctype = "application/json";
  1691. status = 200;
  1692. }
  1693. }
  1694. else
  1695. status = 401;
  1696. }
  1697. /* user cleanup */
  1698. if (logged_in)
  1699. user_free(&snac);
  1700. return status;
  1701. }
  1702. void mastoapi_purge(void)
  1703. {
  1704. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1705. xs *files = xs_glob(spec, 1, 0);
  1706. xs_list *p = files;
  1707. xs_str *v;
  1708. time_t mt = time(NULL) - 3600;
  1709. while (xs_list_iter(&p, &v)) {
  1710. xs *cid = xs_replace(v, ".json", "");
  1711. xs *fn = _app_fn(cid);
  1712. if (mtime(fn) < mt) {
  1713. /* get the app */
  1714. xs *app = app_get(cid);
  1715. if (app) {
  1716. /* old apps with no uid are incomplete cruft */
  1717. const char *uid = xs_dict_get(app, "uid");
  1718. if (xs_is_null(uid) || *uid == '\0') {
  1719. unlink(fn);
  1720. srv_debug(2, xs_fmt("purged %s", fn));
  1721. }
  1722. }
  1723. }
  1724. }
  1725. }
  1726. #endif /* #ifndef NO_MASTODON_API */