mastoapi.c 86 KB

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