mastoapi.c 89 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776
  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. /* dict of validated links */
  488. xs_dict *val_links = NULL;
  489. xs_dict *metadata = xs_stock_dict;
  490. snac user = {0};
  491. if (xs_startswith(id, srv_baseurl)) {
  492. /* if it's a local user, open it and pick its validated links */
  493. if (user_open(&user, prefu)) {
  494. val_links = user.links;
  495. metadata = xs_dict_get_def(user.config, "metadata", xs_stock_dict);
  496. }
  497. }
  498. if (xs_is_null(val_links))
  499. val_links = xs_stock_dict;
  500. while (xs_list_iter(&p, &v)) {
  501. char *type = xs_dict_get(v, "type");
  502. char *name = xs_dict_get(v, "name");
  503. char *value = xs_dict_get(v, "value");
  504. if (!xs_is_null(type) && !xs_is_null(name) &&
  505. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  506. xs *val_date = NULL;
  507. char *url = xs_dict_get(metadata, name);
  508. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  509. xs_number *verified_time = xs_dict_get(val_links, url);
  510. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  511. time_t t = xs_number_get(verified_time);
  512. if (t > 0)
  513. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  514. }
  515. }
  516. xs *d = xs_dict_new();
  517. d = xs_dict_append(d, "name", name);
  518. d = xs_dict_append(d, "value", value);
  519. d = xs_dict_append(d, "verified_at",
  520. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  521. val_date : xs_stock_null);
  522. fields = xs_list_append(fields, d);
  523. }
  524. }
  525. user_free(&user);
  526. acct = xs_dict_append(acct, "fields", fields);
  527. return acct;
  528. }
  529. xs_str *mastoapi_date(char *date)
  530. /* converts an ISO 8601 date to whatever format Mastodon uses */
  531. {
  532. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  533. s = xs_str_cat(s, ".000Z");
  534. return s;
  535. }
  536. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  537. /* creates a mastoapi Poll object */
  538. {
  539. xs_dict *poll = xs_dict_new();
  540. xs *mid = mastoapi_id(msg);
  541. xs_list *opts = NULL;
  542. xs_val *v;
  543. int num_votes = 0;
  544. xs *options = xs_list_new();
  545. poll = xs_dict_append(poll, "id", mid);
  546. xs *fd = mastoapi_date(xs_dict_get(msg, "endTime"));
  547. poll = xs_dict_append(poll, "expires_at", fd);
  548. poll = xs_dict_append(poll, "expired",
  549. xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
  550. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  551. poll = xs_dict_append(poll, "multiple", xs_stock_false);
  552. else {
  553. opts = xs_dict_get(msg, "anyOf");
  554. poll = xs_dict_append(poll, "multiple", xs_stock_true);
  555. }
  556. while (xs_list_iter(&opts, &v)) {
  557. const char *title = xs_dict_get(v, "name");
  558. const char *replies = xs_dict_get(v, "replies");
  559. if (title && replies) {
  560. const char *votes_count = xs_dict_get(replies, "totalItems");
  561. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  562. xs *d = xs_dict_new();
  563. d = xs_dict_append(d, "title", title);
  564. d = xs_dict_append(d, "votes_count", votes_count);
  565. options = xs_list_append(options, d);
  566. num_votes += xs_number_get(votes_count);
  567. }
  568. }
  569. }
  570. poll = xs_dict_append(poll, "options", options);
  571. xs *vc = xs_number_new(num_votes);
  572. poll = xs_dict_append(poll, "votes_count", vc);
  573. poll = xs_dict_append(poll, "voted",
  574. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  575. xs_stock_true : xs_stock_false);
  576. return poll;
  577. }
  578. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  579. /* converts an ActivityPub note to a Mastodon status */
  580. {
  581. xs *actor = NULL;
  582. actor_get(get_atto(msg), &actor);
  583. /* if the author is not here, discard */
  584. if (actor == NULL)
  585. return NULL;
  586. const char *type = xs_dict_get(msg, "type");
  587. const char *id = xs_dict_get(msg, "id");
  588. /* fail if it's not a valid actor */
  589. if (xs_is_null(type) || xs_is_null(id))
  590. return NULL;
  591. xs *acct = mastoapi_account(actor);
  592. if (acct == NULL)
  593. return NULL;
  594. xs *idx = NULL;
  595. xs *ixc = NULL;
  596. char *tmp;
  597. xs *mid = mastoapi_id(msg);
  598. xs_dict *st = xs_dict_new();
  599. st = xs_dict_append(st, "id", mid);
  600. st = xs_dict_append(st, "uri", id);
  601. st = xs_dict_append(st, "url", id);
  602. st = xs_dict_append(st, "account", acct);
  603. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  604. st = xs_dict_append(st, "created_at", fd);
  605. {
  606. const char *content = xs_dict_get(msg, "content");
  607. const char *name = xs_dict_get(msg, "name");
  608. xs *s1 = NULL;
  609. if (name && content)
  610. s1 = xs_fmt("%s<br><br>%s", name, content);
  611. else
  612. if (name)
  613. s1 = xs_dup(name);
  614. else
  615. if (content)
  616. s1 = xs_dup(content);
  617. else
  618. s1 = xs_str_new(NULL);
  619. st = xs_dict_append(st, "content", s1);
  620. }
  621. st = xs_dict_append(st, "visibility",
  622. is_msg_public(msg) ? "public" : "private");
  623. tmp = xs_dict_get(msg, "sensitive");
  624. if (xs_is_null(tmp))
  625. tmp = xs_stock_false;
  626. st = xs_dict_append(st, "sensitive", tmp);
  627. tmp = xs_dict_get(msg, "summary");
  628. if (xs_is_null(tmp))
  629. tmp = "";
  630. st = xs_dict_append(st, "spoiler_text", tmp);
  631. /* create the list of attachments */
  632. xs *attach = get_attachments(msg);
  633. {
  634. xs_list *p = attach;
  635. xs_dict *v;
  636. xs *matt = xs_list_new();
  637. while (xs_list_iter(&p, &v)) {
  638. char *type = xs_dict_get(v, "type");
  639. char *href = xs_dict_get(v, "href");
  640. char *name = xs_dict_get(v, "name");
  641. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  642. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  643. xs *d = xs_dict_new();
  644. d = xs_dict_append(d, "id", matteid);
  645. d = xs_dict_append(d, "url", href);
  646. d = xs_dict_append(d, "preview_url", href);
  647. d = xs_dict_append(d, "remote_url", href);
  648. d = xs_dict_append(d, "description", name);
  649. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  650. matt = xs_list_append(matt, d);
  651. }
  652. }
  653. st = xs_dict_append(st, "media_attachments", matt);
  654. }
  655. {
  656. xs *ml = xs_list_new();
  657. xs *htl = xs_list_new();
  658. xs *eml = xs_list_new();
  659. xs_list *tag = xs_dict_get(msg, "tag");
  660. int n = 0;
  661. xs *tag_list = NULL;
  662. if (xs_type(tag) == XSTYPE_DICT) {
  663. tag_list = xs_list_new();
  664. tag_list = xs_list_append(tag_list, tag);
  665. }
  666. else
  667. if (xs_type(tag) == XSTYPE_LIST)
  668. tag_list = xs_dup(tag);
  669. else
  670. tag_list = xs_list_new();
  671. tag = tag_list;
  672. xs_dict *v;
  673. while (xs_list_iter(&tag, &v)) {
  674. const char *type = xs_dict_get(v, "type");
  675. if (xs_is_null(type))
  676. continue;
  677. xs *d1 = xs_dict_new();
  678. if (strcmp(type, "Mention") == 0) {
  679. const char *name = xs_dict_get(v, "name");
  680. const char *href = xs_dict_get(v, "href");
  681. if (!xs_is_null(name) && !xs_is_null(href) &&
  682. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  683. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  684. xs *id = xs_fmt("%d", n++);
  685. d1 = xs_dict_append(d1, "id", id);
  686. d1 = xs_dict_append(d1, "username", nm);
  687. d1 = xs_dict_append(d1, "acct", nm);
  688. d1 = xs_dict_append(d1, "url", href);
  689. ml = xs_list_append(ml, d1);
  690. }
  691. }
  692. else
  693. if (strcmp(type, "Hashtag") == 0) {
  694. const char *name = xs_dict_get(v, "name");
  695. const char *href = xs_dict_get(v, "href");
  696. if (!xs_is_null(name) && !xs_is_null(href)) {
  697. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  698. d1 = xs_dict_append(d1, "name", nm);
  699. d1 = xs_dict_append(d1, "url", href);
  700. htl = xs_list_append(htl, d1);
  701. }
  702. }
  703. else
  704. if (strcmp(type, "Emoji") == 0) {
  705. const char *name = xs_dict_get(v, "name");
  706. const xs_dict *icon = xs_dict_get(v, "icon");
  707. if (!xs_is_null(name) && !xs_is_null(icon)) {
  708. const char *url = xs_dict_get(icon, "url");
  709. if (!xs_is_null(url)) {
  710. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  711. d1 = xs_dict_append(d1, "shortcode", nm);
  712. d1 = xs_dict_append(d1, "url", url);
  713. d1 = xs_dict_append(d1, "static_url", url);
  714. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  715. d1 = xs_dict_append(d1, "category", "Emojis");
  716. eml = xs_list_append(eml, d1);
  717. }
  718. }
  719. }
  720. }
  721. st = xs_dict_append(st, "mentions", ml);
  722. st = xs_dict_append(st, "tags", htl);
  723. st = xs_dict_append(st, "emojis", eml);
  724. }
  725. xs_free(idx);
  726. xs_free(ixc);
  727. idx = object_likes(id);
  728. ixc = xs_number_new(xs_list_len(idx));
  729. st = xs_dict_append(st, "favourites_count", ixc);
  730. st = xs_dict_append(st, "favourited",
  731. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  732. xs_free(idx);
  733. xs_free(ixc);
  734. idx = object_announces(id);
  735. ixc = xs_number_new(xs_list_len(idx));
  736. st = xs_dict_append(st, "reblogs_count", ixc);
  737. st = xs_dict_append(st, "reblogged",
  738. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  739. /* get the last person who boosted this */
  740. xs *boosted_by_md5 = NULL;
  741. if (xs_list_len(idx))
  742. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  743. xs_free(idx);
  744. xs_free(ixc);
  745. idx = object_children(id);
  746. ixc = xs_number_new(xs_list_len(idx));
  747. st = xs_dict_append(st, "replies_count", ixc);
  748. /* default in_reply_to values */
  749. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  750. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  751. tmp = xs_dict_get(msg, "inReplyTo");
  752. if (!xs_is_null(tmp)) {
  753. xs *irto = NULL;
  754. if (valid_status(object_get(tmp, &irto))) {
  755. xs *irt_mid = mastoapi_id(irto);
  756. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  757. char *at = NULL;
  758. if (!xs_is_null(at = get_atto(irto))) {
  759. xs *at_md5 = xs_md5_hex(at, strlen(at));
  760. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  761. }
  762. }
  763. }
  764. st = xs_dict_append(st, "reblog", xs_stock_null);
  765. st = xs_dict_append(st, "card", xs_stock_null);
  766. st = xs_dict_append(st, "language", xs_stock_null);
  767. tmp = xs_dict_get(msg, "sourceContent");
  768. if (xs_is_null(tmp))
  769. tmp = "";
  770. st = xs_dict_append(st, "text", tmp);
  771. tmp = xs_dict_get(msg, "updated");
  772. xs *fd2 = NULL;
  773. if (xs_is_null(tmp))
  774. tmp = xs_stock_null;
  775. else {
  776. fd2 = mastoapi_date(tmp);
  777. tmp = fd2;
  778. }
  779. st = xs_dict_append(st, "edited_at", tmp);
  780. if (strcmp(type, "Question") == 0) {
  781. xs *poll = mastoapi_poll(snac, msg);
  782. st = xs_dict_append(st, "poll", poll);
  783. }
  784. else
  785. st = xs_dict_append(st, "poll", xs_stock_null);
  786. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  787. st = xs_dict_append(st, "pinned",
  788. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  789. /* is it a boost? */
  790. if (!xs_is_null(boosted_by_md5)) {
  791. /* create a new dummy status, using st as the 'reblog' field */
  792. xs_dict *bst = xs_dup(st);
  793. xs *b_actor = NULL;
  794. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  795. xs *b_acct = mastoapi_account(b_actor);
  796. xs *fake_uri = NULL;
  797. if (snac)
  798. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  799. else
  800. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  801. bst = xs_dict_set(bst, "uri", fake_uri);
  802. bst = xs_dict_set(bst, "url", fake_uri);
  803. bst = xs_dict_set(bst, "account", b_acct);
  804. bst = xs_dict_set(bst, "content", "");
  805. bst = xs_dict_set(bst, "reblog", st);
  806. xs_free(st);
  807. st = bst;
  808. }
  809. }
  810. return st;
  811. }
  812. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  813. {
  814. xs_dict *rel = NULL;
  815. xs *actor_o = NULL;
  816. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  817. rel = xs_dict_new();
  818. const char *actor = xs_dict_get(actor_o, "id");
  819. rel = xs_dict_append(rel, "id", md5);
  820. rel = xs_dict_append(rel, "following",
  821. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  822. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  823. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  824. rel = xs_dict_append(rel, "followed_by",
  825. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  826. rel = xs_dict_append(rel, "blocking",
  827. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  828. rel = xs_dict_append(rel, "muting", xs_stock_false);
  829. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  830. rel = xs_dict_append(rel, "requested", xs_stock_false);
  831. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  832. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  833. rel = xs_dict_append(rel, "note", "");
  834. }
  835. return rel;
  836. }
  837. int process_auth_token(snac *snac, const xs_dict *req)
  838. /* processes an authorization token, if there is one */
  839. {
  840. int logged_in = 0;
  841. char *v;
  842. /* if there is an authorization field, try to validate it */
  843. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  844. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  845. xs *token = token_get(tokid);
  846. if (token != NULL) {
  847. const char *uid = xs_dict_get(token, "uid");
  848. if (!xs_is_null(uid) && user_open(snac, uid)) {
  849. logged_in = 1;
  850. /* this counts as a 'login' */
  851. lastlog_write(snac, "mastoapi");
  852. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  853. }
  854. else
  855. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  856. }
  857. else
  858. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  859. }
  860. return logged_in;
  861. }
  862. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  863. char **body, int *b_size, char **ctype)
  864. {
  865. (void)b_size;
  866. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  867. return 0;
  868. int status = 404;
  869. xs_dict *args = xs_dict_get(req, "q_vars");
  870. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  871. snac snac1 = {0};
  872. int logged_in = process_auth_token(&snac1, req);
  873. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  874. if (logged_in) {
  875. xs *acct = xs_dict_new();
  876. acct = xs_dict_append(acct, "id", snac1.md5);
  877. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  878. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  879. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  880. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  881. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac1.config, "published"));
  882. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  883. acct = xs_dict_append(acct, "url", snac1.actor);
  884. acct = xs_dict_append(acct, "header", "");
  885. acct = xs_dict_append(acct, "header_static", "");
  886. acct = xs_dict_append(acct, "locked", xs_stock_false);
  887. acct = xs_dict_append(acct, "bot", xs_dict_get(snac1.config, "bot"));
  888. xs *src = xs_json_loads("{\"privacy\":\"public\","
  889. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  890. acct = xs_dict_append(acct, "source", src);
  891. xs *avatar = NULL;
  892. char *av = xs_dict_get(snac1.config, "avatar");
  893. if (xs_is_null(av) || *av == '\0')
  894. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  895. else
  896. avatar = xs_dup(av);
  897. acct = xs_dict_append(acct, "avatar", avatar);
  898. acct = xs_dict_append(acct, "avatar_static", avatar);
  899. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  900. if (xs_type(metadata) == XSTYPE_DICT) {
  901. xs *fields = xs_list_new();
  902. xs_str *k;
  903. xs_str *v;
  904. xs_dict *val_links = snac1.links;
  905. if (xs_is_null(val_links))
  906. val_links = xs_stock_dict;
  907. int c = 0;
  908. while (xs_dict_next(metadata, &k, &v, &c)) {
  909. xs *val_date = NULL;
  910. xs_number *verified_time = xs_dict_get(val_links, v);
  911. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  912. time_t t = xs_number_get(verified_time);
  913. if (t > 0)
  914. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  915. }
  916. xs *d = xs_dict_new();
  917. d = xs_dict_append(d, "name", k);
  918. d = xs_dict_append(d, "value", v);
  919. d = xs_dict_append(d, "verified_at",
  920. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  921. val_date : xs_stock_null);
  922. fields = xs_list_append(fields, d);
  923. }
  924. acct = xs_dict_set(acct, "fields", fields);
  925. }
  926. acct = xs_dict_append(acct, "followers_count", xs_stock_0);
  927. acct = xs_dict_append(acct, "following_count", xs_stock_0);
  928. acct = xs_dict_append(acct, "statuses_count", xs_stock_0);
  929. *body = xs_json_dumps(acct, 4);
  930. *ctype = "application/json";
  931. status = 200;
  932. }
  933. else {
  934. status = 422; // "Unprocessable entity" (no login)
  935. }
  936. }
  937. else
  938. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  939. /* find if an account is followed, blocked, etc. */
  940. /* the account to get relationships about is in args "id[]" */
  941. if (logged_in) {
  942. xs *res = xs_list_new();
  943. const char *md5 = xs_dict_get(args, "id[]");
  944. if (xs_is_null(md5))
  945. md5 = xs_dict_get(args, "id");
  946. if (!xs_is_null(md5)) {
  947. if (xs_type(md5) == XSTYPE_LIST)
  948. md5 = xs_list_get(md5, 0);
  949. xs *rel = mastoapi_relationship(&snac1, md5);
  950. if (rel != NULL)
  951. res = xs_list_append(res, rel);
  952. }
  953. *body = xs_json_dumps(res, 4);
  954. *ctype = "application/json";
  955. status = 200;
  956. }
  957. else
  958. status = 422;
  959. }
  960. else
  961. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  962. /* lookup an account */
  963. char *acct = xs_dict_get(args, "acct");
  964. if (!xs_is_null(acct)) {
  965. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  966. xs *l = xs_split_n(s, "@", 1);
  967. char *uid = xs_list_get(l, 0);
  968. char *host = xs_list_get(l, 1);
  969. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  970. snac user;
  971. if (user_open(&user, uid)) {
  972. xs *actor = msg_actor(&user);
  973. xs *macct = mastoapi_account(actor);
  974. *body = xs_json_dumps(macct, 4);
  975. *ctype = "application/json";
  976. status = 200;
  977. user_free(&user);
  978. }
  979. }
  980. }
  981. }
  982. else
  983. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  984. /* account-related information */
  985. xs *l = xs_split(cmd, "/");
  986. const char *uid = xs_list_get(l, 3);
  987. const char *opt = xs_list_get(l, 4);
  988. if (uid != NULL) {
  989. snac snac2;
  990. xs *out = NULL;
  991. xs *actor = NULL;
  992. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  993. /* search for accounts starting with q */
  994. const char *aq = xs_dict_get(args, "q");
  995. if (!xs_is_null(aq)) {
  996. xs *q = xs_tolower_i(xs_dup(aq));
  997. out = xs_list_new();
  998. xs *wing = following_list(&snac1);
  999. xs *wers = follower_list(&snac1);
  1000. xs *ulst = user_list();
  1001. xs_list *p;
  1002. xs_str *v;
  1003. xs_set seen;
  1004. xs_set_init(&seen);
  1005. /* user relations */
  1006. xs_list *lsts[] = { wing, wers, NULL };
  1007. int n;
  1008. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1009. while (xs_list_iter(&p, &v)) {
  1010. /* already seen? skip */
  1011. if (xs_set_add(&seen, v) == 0)
  1012. continue;
  1013. xs *actor = NULL;
  1014. if (valid_status(object_get(v, &actor))) {
  1015. const char *uname = xs_dict_get(actor, "preferredUsername");
  1016. if (!xs_is_null(uname)) {
  1017. xs *luname = xs_tolower_i(xs_dup(uname));
  1018. if (xs_startswith(luname, q)) {
  1019. xs *acct = mastoapi_account(actor);
  1020. out = xs_list_append(out, acct);
  1021. }
  1022. }
  1023. }
  1024. }
  1025. }
  1026. /* local users */
  1027. p = ulst;
  1028. while (xs_list_iter(&p, &v)) {
  1029. snac user;
  1030. /* skip this same user */
  1031. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1032. continue;
  1033. /* skip if the uid does not start with the query */
  1034. xs *v2 = xs_tolower_i(xs_dup(v));
  1035. if (!xs_startswith(v2, q))
  1036. continue;
  1037. if (user_open(&user, v)) {
  1038. /* if it's not already seen, add it */
  1039. if (xs_set_add(&seen, user.actor) == 1) {
  1040. xs *actor = msg_actor(&user);
  1041. xs *acct = mastoapi_account(actor);
  1042. out = xs_list_append(out, acct);
  1043. }
  1044. user_free(&user);
  1045. }
  1046. }
  1047. xs_set_free(&seen);
  1048. }
  1049. }
  1050. else
  1051. /* is it a local user? */
  1052. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1053. if (opt == NULL) {
  1054. /* account information */
  1055. actor = msg_actor(&snac2);
  1056. out = mastoapi_account(actor);
  1057. }
  1058. else
  1059. if (strcmp(opt, "statuses") == 0) { /** **/
  1060. /* the public list of posts of a user */
  1061. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1062. xs_list *p = timeline;
  1063. xs_str *v;
  1064. out = xs_list_new();
  1065. while (xs_list_iter(&p, &v)) {
  1066. xs *msg = NULL;
  1067. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1068. /* add only posts by the author */
  1069. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1070. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1071. xs *st = mastoapi_status(&snac2, msg);
  1072. if (st)
  1073. out = xs_list_append(out, st);
  1074. }
  1075. }
  1076. }
  1077. }
  1078. user_free(&snac2);
  1079. }
  1080. else {
  1081. /* try the uid as the md5 of a possibly loaded actor */
  1082. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1083. if (opt == NULL) {
  1084. /* account information */
  1085. out = mastoapi_account(actor);
  1086. }
  1087. else
  1088. if (strcmp(opt, "statuses") == 0) {
  1089. /* we don't serve statuses of others; return the empty list */
  1090. out = xs_list_new();
  1091. }
  1092. }
  1093. }
  1094. if (out != NULL) {
  1095. *body = xs_json_dumps(out, 4);
  1096. *ctype = "application/json";
  1097. status = 200;
  1098. }
  1099. }
  1100. }
  1101. else
  1102. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1103. /* the private timeline */
  1104. if (logged_in) {
  1105. const char *max_id = xs_dict_get(args, "max_id");
  1106. const char *since_id = xs_dict_get(args, "since_id");
  1107. const char *min_id = xs_dict_get(args, "min_id");
  1108. const char *limit_s = xs_dict_get(args, "limit");
  1109. int limit = 0;
  1110. int cnt = 0;
  1111. if (!xs_is_null(limit_s))
  1112. limit = atoi(limit_s);
  1113. if (limit == 0)
  1114. limit = 20;
  1115. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1116. xs *out = xs_list_new();
  1117. xs_list *p = timeline;
  1118. xs_str *v;
  1119. while (xs_list_iter(&p, &v) && cnt < limit) {
  1120. xs *msg = NULL;
  1121. /* only return entries older that max_id */
  1122. if (max_id) {
  1123. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1124. max_id = NULL;
  1125. continue;
  1126. }
  1127. /* only returns entries newer than since_id */
  1128. if (since_id) {
  1129. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1130. break;
  1131. }
  1132. /* only returns entries newer than min_id */
  1133. /* what does really "Return results immediately newer than ID" mean? */
  1134. if (min_id) {
  1135. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1136. break;
  1137. }
  1138. /* get the entry */
  1139. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1140. continue;
  1141. /* discard non-Notes */
  1142. const char *id = xs_dict_get(msg, "id");
  1143. const char *type = xs_dict_get(msg, "type");
  1144. if (!xs_match(type, "Note|Question|Page|Article|Video"))
  1145. continue;
  1146. const char *from = NULL;
  1147. if (strcmp(type, "Page") == 0)
  1148. from = xs_dict_get(msg, "audience");
  1149. if (from == NULL)
  1150. from = get_atto(msg);
  1151. if (from == NULL)
  1152. continue;
  1153. /* is this message from a person we don't follow? */
  1154. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1155. /* discard if it was not boosted */
  1156. xs *idx = object_announces(id);
  1157. if (xs_list_len(idx) == 0)
  1158. continue;
  1159. }
  1160. /* discard notes from muted morons */
  1161. if (is_muted(&snac1, from))
  1162. continue;
  1163. /* discard hidden notes */
  1164. if (is_hidden(&snac1, id))
  1165. continue;
  1166. /* if it has a name and it's not a Page or a Video,
  1167. it's a poll vote, so discard it */
  1168. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
  1169. continue;
  1170. /* convert the Note into a Mastodon status */
  1171. xs *st = mastoapi_status(&snac1, msg);
  1172. if (st != NULL)
  1173. out = xs_list_append(out, st);
  1174. cnt++;
  1175. }
  1176. *body = xs_json_dumps(out, 4);
  1177. *ctype = "application/json";
  1178. status = 200;
  1179. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1180. }
  1181. else {
  1182. status = 401; // unauthorized
  1183. }
  1184. }
  1185. else
  1186. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1187. /* the instance public timeline (public timelines for all users) */
  1188. const char *limit_s = xs_dict_get(args, "limit");
  1189. int limit = 0;
  1190. int cnt = 0;
  1191. if (!xs_is_null(limit_s))
  1192. limit = atoi(limit_s);
  1193. if (limit == 0)
  1194. limit = 20;
  1195. xs *timeline = timeline_instance_list(0, limit);
  1196. xs *out = xs_list_new();
  1197. xs_list *p = timeline;
  1198. xs_str *md5;
  1199. snac *user = NULL;
  1200. if (logged_in)
  1201. user = &snac1;
  1202. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1203. xs *msg = NULL;
  1204. /* get the entry */
  1205. if (!valid_status(object_get_by_md5(md5, &msg)))
  1206. continue;
  1207. /* discard non-Notes */
  1208. const char *type = xs_dict_get(msg, "type");
  1209. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1210. continue;
  1211. /* discard messages from private users */
  1212. if (is_msg_from_private_user(msg))
  1213. continue;
  1214. /* convert the Note into a Mastodon status */
  1215. xs *st = mastoapi_status(user, msg);
  1216. if (st != NULL) {
  1217. out = xs_list_append(out, st);
  1218. cnt++;
  1219. }
  1220. }
  1221. *body = xs_json_dumps(out, 4);
  1222. *ctype = "application/json";
  1223. status = 200;
  1224. }
  1225. else
  1226. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1227. const char *limit_s = xs_dict_get(args, "limit");
  1228. int limit = 0;
  1229. int cnt = 0;
  1230. if (!xs_is_null(limit_s))
  1231. limit = atoi(limit_s);
  1232. if (limit == 0)
  1233. limit = 20;
  1234. /* get the tag */
  1235. xs *l = xs_split(cmd, "/");
  1236. char *tag = xs_list_get(l, -1);
  1237. xs *timeline = tag_search(tag, 0, limit);
  1238. xs *out = xs_list_new();
  1239. xs_list *p = timeline;
  1240. xs_str *md5;
  1241. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1242. xs *msg = NULL;
  1243. /* get the entry */
  1244. if (!valid_status(object_get_by_md5(md5, &msg)))
  1245. continue;
  1246. /* skip non-public messages */
  1247. if (!is_msg_public(msg))
  1248. continue;
  1249. /* discard messages from private users */
  1250. if (is_msg_from_private_user(msg))
  1251. continue;
  1252. /* convert the Note into a Mastodon status */
  1253. xs *st = mastoapi_status(NULL, msg);
  1254. if (st != NULL) {
  1255. out = xs_list_append(out, st);
  1256. cnt++;
  1257. }
  1258. }
  1259. *body = xs_json_dumps(out, 4);
  1260. *ctype = "application/json";
  1261. status = 200;
  1262. }
  1263. else
  1264. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1265. /* TBD */
  1266. *body = xs_dup("[]");
  1267. *ctype = "application/json";
  1268. status = 200;
  1269. }
  1270. else
  1271. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1272. if (logged_in) {
  1273. xs *l = notify_list(&snac1, 0, 64);
  1274. xs *out = xs_list_new();
  1275. xs_list *p = l;
  1276. xs_dict *v;
  1277. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1278. while (xs_list_iter(&p, &v)) {
  1279. xs *noti = notify_get(&snac1, v);
  1280. if (noti == NULL)
  1281. continue;
  1282. const char *type = xs_dict_get(noti, "type");
  1283. const char *utype = xs_dict_get(noti, "utype");
  1284. const char *objid = xs_dict_get(noti, "objid");
  1285. xs *actor = NULL;
  1286. xs *entry = NULL;
  1287. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1288. continue;
  1289. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1290. continue;
  1291. if (is_hidden(&snac1, objid))
  1292. continue;
  1293. /* convert the type */
  1294. if (strcmp(type, "Like") == 0)
  1295. type = "favourite";
  1296. else
  1297. if (strcmp(type, "Announce") == 0)
  1298. type = "reblog";
  1299. else
  1300. if (strcmp(type, "Follow") == 0)
  1301. type = "follow";
  1302. else
  1303. if (strcmp(type, "Create") == 0)
  1304. type = "mention";
  1305. else
  1306. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1307. type = "poll";
  1308. else
  1309. continue;
  1310. /* excluded type? */
  1311. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1312. continue;
  1313. xs *mn = xs_dict_new();
  1314. mn = xs_dict_append(mn, "type", type);
  1315. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1316. mn = xs_dict_append(mn, "id", id);
  1317. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1318. xs *acct = mastoapi_account(actor);
  1319. mn = xs_dict_append(mn, "account", acct);
  1320. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1321. xs *st = mastoapi_status(&snac1, entry);
  1322. if (st)
  1323. mn = xs_dict_append(mn, "status", st);
  1324. }
  1325. out = xs_list_append(out, mn);
  1326. }
  1327. *body = xs_json_dumps(out, 4);
  1328. *ctype = "application/json";
  1329. status = 200;
  1330. }
  1331. else
  1332. status = 401;
  1333. }
  1334. else
  1335. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1336. /* snac will never have filters */
  1337. *body = xs_dup("[]");
  1338. *ctype = "application/json";
  1339. status = 200;
  1340. }
  1341. else
  1342. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1343. /* snac will never have filters
  1344. * but still, without a v2 endpoint a short delay is introduced
  1345. * in some apps */
  1346. *body = xs_dup("[]");
  1347. *ctype = "application/json";
  1348. status = 200;
  1349. }
  1350. else
  1351. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1352. /* snac will never support a list of favourites */
  1353. *body = xs_dup("[]");
  1354. *ctype = "application/json";
  1355. status = 200;
  1356. }
  1357. else
  1358. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1359. /* snac does not support bookmarks */
  1360. *body = xs_dup("[]");
  1361. *ctype = "application/json";
  1362. status = 200;
  1363. }
  1364. else
  1365. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1366. /* snac does not support lists */
  1367. *body = xs_dup("[]");
  1368. *ctype = "application/json";
  1369. status = 200;
  1370. }
  1371. else
  1372. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1373. /* snac does not schedule notes */
  1374. *body = xs_dup("[]");
  1375. *ctype = "application/json";
  1376. status = 200;
  1377. }
  1378. else
  1379. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1380. /* snac does not support optional follow confirmations */
  1381. *body = xs_dup("[]");
  1382. *ctype = "application/json";
  1383. status = 200;
  1384. }
  1385. else
  1386. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1387. /* snac has no announcements (yet?) */
  1388. *body = xs_dup("[]");
  1389. *ctype = "application/json";
  1390. status = 200;
  1391. }
  1392. else
  1393. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1394. /* are you kidding me? */
  1395. *body = xs_dup("[]");
  1396. *ctype = "application/json";
  1397. status = 200;
  1398. }
  1399. else
  1400. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1401. /* returns an instance object */
  1402. xs *ins = xs_dict_new();
  1403. const char *host = xs_dict_get(srv_config, "host");
  1404. const char *title = xs_dict_get(srv_config, "title");
  1405. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1406. ins = xs_dict_append(ins, "uri", host);
  1407. ins = xs_dict_append(ins, "domain", host);
  1408. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1409. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1410. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1411. ins = xs_dict_append(ins, "description", host);
  1412. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1413. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1414. ins = xs_dict_append(ins, "thumbnail", susie);
  1415. const char *v = xs_dict_get(srv_config, "admin_email");
  1416. if (xs_is_null(v) || *v == '\0')
  1417. v = "admin@localhost";
  1418. ins = xs_dict_append(ins, "email", v);
  1419. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1420. xs *l1 = xs_list_append(xs_list_new(), "en");
  1421. ins = xs_dict_append(ins, "languages", l1);
  1422. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1423. xs *urls = xs_dict_new();
  1424. urls = xs_dict_append(urls, "streaming_api", wss);
  1425. ins = xs_dict_append(ins, "urls", urls);
  1426. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1427. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1428. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1429. ins = xs_dict_append(ins, "stats", d2);
  1430. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1431. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1432. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1433. xs *cfg = xs_dict_new();
  1434. {
  1435. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1436. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1437. cfg = xs_dict_append(cfg, "statuses", d11);
  1438. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1439. cfg = xs_dict_append(cfg, "accounts", d12);
  1440. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1441. "\"image_size_limit\":16777216,"
  1442. "\"video_frame_rate_limit\":120,"
  1443. "\"video_matrix_limit\":8294400,"
  1444. "\"video_size_limit\":103809024}"
  1445. );
  1446. {
  1447. /* get the supported mime types from the internal list */
  1448. const char **p = xs_mime_types;
  1449. xs_set mtypes;
  1450. xs_set_init(&mtypes);
  1451. while (*p) {
  1452. const char *type = p[1];
  1453. if (xs_startswith(type, "image/") ||
  1454. xs_startswith(type, "video/") ||
  1455. xs_startswith(type, "audio/"))
  1456. xs_set_add(&mtypes, type);
  1457. p += 2;
  1458. }
  1459. xs *l = xs_set_result(&mtypes);
  1460. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1461. }
  1462. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1463. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1464. "\"max_expiration\":2629746,"
  1465. "\"max_options\":8,\"min_expiration\":300}");
  1466. cfg = xs_dict_append(cfg, "polls", d14);
  1467. }
  1468. ins = xs_dict_append(ins, "configuration", cfg);
  1469. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1470. if (!xs_is_null(admin_account) && *admin_account) {
  1471. snac admin;
  1472. if (user_open(&admin, admin_account)) {
  1473. xs *actor = msg_actor(&admin);
  1474. xs *acct = mastoapi_account(actor);
  1475. ins = xs_dict_append(ins, "contact_account", acct);
  1476. user_free(&admin);
  1477. }
  1478. }
  1479. *body = xs_json_dumps(ins, 4);
  1480. *ctype = "application/json";
  1481. status = 200;
  1482. }
  1483. else
  1484. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1485. /* information about a status */
  1486. if (logged_in) {
  1487. xs *l = xs_split(cmd, "/");
  1488. const char *id = xs_list_get(l, 3);
  1489. const char *op = xs_list_get(l, 4);
  1490. if (!xs_is_null(id)) {
  1491. xs *msg = NULL;
  1492. xs *out = NULL;
  1493. /* skip the 'fake' part of the id */
  1494. id = MID_TO_MD5(id);
  1495. if (valid_status(object_get_by_md5(id, &msg))) {
  1496. if (op == NULL) {
  1497. if (!is_muted(&snac1, get_atto(msg))) {
  1498. /* return the status itself */
  1499. out = mastoapi_status(&snac1, msg);
  1500. }
  1501. }
  1502. else
  1503. if (strcmp(op, "context") == 0) { /** **/
  1504. /* return ancestors and children */
  1505. xs *anc = xs_list_new();
  1506. xs *des = xs_list_new();
  1507. xs_list *p;
  1508. xs_str *v;
  1509. char pid[64];
  1510. /* build the [grand]parent list, moving up */
  1511. strncpy(pid, id, sizeof(pid));
  1512. while (object_parent(pid, pid, sizeof(pid))) {
  1513. xs *m2 = NULL;
  1514. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1515. xs *st = mastoapi_status(&snac1, m2);
  1516. if (st)
  1517. anc = xs_list_insert(anc, 0, st);
  1518. }
  1519. else
  1520. break;
  1521. }
  1522. /* build the children list */
  1523. xs *children = object_children(xs_dict_get(msg, "id"));
  1524. p = children;
  1525. while (xs_list_iter(&p, &v)) {
  1526. xs *m2 = NULL;
  1527. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1528. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1529. xs *st = mastoapi_status(&snac1, m2);
  1530. if (st)
  1531. des = xs_list_append(des, st);
  1532. }
  1533. }
  1534. }
  1535. out = xs_dict_new();
  1536. out = xs_dict_append(out, "ancestors", anc);
  1537. out = xs_dict_append(out, "descendants", des);
  1538. }
  1539. else
  1540. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1541. strcmp(op, "favourited_by") == 0) { /** **/
  1542. /* return the list of people who liked or boosted this */
  1543. out = xs_list_new();
  1544. xs *l = NULL;
  1545. if (op[0] == 'r')
  1546. l = object_announces(xs_dict_get(msg, "id"));
  1547. else
  1548. l = object_likes(xs_dict_get(msg, "id"));
  1549. xs_list *p = l;
  1550. xs_str *v;
  1551. while (xs_list_iter(&p, &v)) {
  1552. xs *actor2 = NULL;
  1553. if (valid_status(object_get_by_md5(v, &actor2))) {
  1554. xs *acct2 = mastoapi_account(actor2);
  1555. out = xs_list_append(out, acct2);
  1556. }
  1557. }
  1558. }
  1559. else
  1560. if (strcmp(op, "source") == 0) { /** **/
  1561. out = xs_dict_new();
  1562. /* get the mastoapi status id */
  1563. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1564. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1565. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1566. }
  1567. }
  1568. else
  1569. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1570. if (out != NULL) {
  1571. *body = xs_json_dumps(out, 4);
  1572. *ctype = "application/json";
  1573. status = 200;
  1574. }
  1575. }
  1576. }
  1577. else
  1578. status = 401;
  1579. }
  1580. else
  1581. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1582. *body = xs_dup("{}");
  1583. *ctype = "application/json";
  1584. status = 200;
  1585. }
  1586. else
  1587. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1588. *body = xs_dup("{}");
  1589. *ctype = "application/json";
  1590. status = 200;
  1591. }
  1592. else
  1593. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1594. *body = xs_dup("[]");
  1595. *ctype = "application/json";
  1596. status = 200;
  1597. }
  1598. else
  1599. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1600. if (logged_in) {
  1601. const char *q = xs_dict_get(args, "q");
  1602. const char *type = xs_dict_get(args, "type");
  1603. const char *offset = xs_dict_get(args, "offset");
  1604. xs *acl = xs_list_new();
  1605. xs *stl = xs_list_new();
  1606. xs *htl = xs_list_new();
  1607. xs *res = xs_dict_new();
  1608. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1609. /* reply something only for offset 0; otherwise,
  1610. apps like Tusky keep asking again and again */
  1611. if (!xs_is_null(q) && !xs_is_null(type)) {
  1612. if (strcmp(type, "accounts") == 0) {
  1613. /* do a webfinger query */
  1614. char *actor = NULL;
  1615. char *user = NULL;
  1616. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  1617. xs *actor_o = NULL;
  1618. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1619. xs *acct = mastoapi_account(actor_o);
  1620. acl = xs_list_append(acl, acct);
  1621. }
  1622. }
  1623. }
  1624. else
  1625. if (strcmp(type, "hashtags") == 0) {
  1626. /* search this tag */
  1627. xs *tl = tag_search((char *)q, 0, 1);
  1628. if (xs_list_len(tl)) {
  1629. xs *d = xs_dict_new();
  1630. d = xs_dict_append(d, "name", q);
  1631. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  1632. d = xs_dict_append(d, "url", url);
  1633. d = xs_dict_append(d, "history", xs_stock_list);
  1634. htl = xs_list_append(htl, d);
  1635. }
  1636. }
  1637. }
  1638. }
  1639. res = xs_dict_append(res, "accounts", acl);
  1640. res = xs_dict_append(res, "statuses", stl);
  1641. res = xs_dict_append(res, "hashtags", htl);
  1642. *body = xs_json_dumps(res, 4);
  1643. *ctype = "application/json";
  1644. status = 200;
  1645. }
  1646. else
  1647. status = 401;
  1648. }
  1649. /* user cleanup */
  1650. if (logged_in)
  1651. user_free(&snac1);
  1652. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1653. return status;
  1654. }
  1655. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1656. const char *payload, int p_size,
  1657. char **body, int *b_size, char **ctype)
  1658. {
  1659. (void)p_size;
  1660. (void)b_size;
  1661. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1662. return 0;
  1663. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1664. int status = 404;
  1665. xs *args = NULL;
  1666. char *i_ctype = xs_dict_get(req, "content-type");
  1667. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  1668. if (!xs_is_null(payload))
  1669. args = xs_json_loads(payload);
  1670. }
  1671. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  1672. {
  1673. // Some apps send form data instead of json so we should cater for those
  1674. if (!xs_is_null(payload)) {
  1675. xs *upl = xs_url_dec(payload);
  1676. args = xs_url_vars(upl);
  1677. }
  1678. }
  1679. else
  1680. args = xs_dup(xs_dict_get(req, "p_vars"));
  1681. if (args == NULL)
  1682. return 400;
  1683. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1684. snac snac = {0};
  1685. int logged_in = process_auth_token(&snac, req);
  1686. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1687. const char *name = xs_dict_get(args, "client_name");
  1688. const char *ruri = xs_dict_get(args, "redirect_uris");
  1689. const char *scope = xs_dict_get(args, "scope");
  1690. if (xs_type(ruri) == XSTYPE_LIST)
  1691. ruri = xs_dict_get(ruri, 0);
  1692. if (name && ruri) {
  1693. xs *app = xs_dict_new();
  1694. xs *id = xs_replace_i(tid(0), ".", "");
  1695. xs *csec = random_str();
  1696. xs *vkey = random_str();
  1697. xs *cid = NULL;
  1698. /* pick a non-existent random cid */
  1699. for (;;) {
  1700. cid = random_str();
  1701. xs *p_app = app_get(cid);
  1702. if (p_app == NULL)
  1703. break;
  1704. xs_free(cid);
  1705. }
  1706. app = xs_dict_append(app, "name", name);
  1707. app = xs_dict_append(app, "redirect_uri", ruri);
  1708. app = xs_dict_append(app, "client_id", cid);
  1709. app = xs_dict_append(app, "client_secret", csec);
  1710. app = xs_dict_append(app, "vapid_key", vkey);
  1711. app = xs_dict_append(app, "id", id);
  1712. *body = xs_json_dumps(app, 4);
  1713. *ctype = "application/json";
  1714. status = 200;
  1715. app = xs_dict_append(app, "code", "");
  1716. if (scope)
  1717. app = xs_dict_append(app, "scope", scope);
  1718. app_add(cid, app);
  1719. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1720. }
  1721. }
  1722. else
  1723. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1724. if (logged_in) {
  1725. /* post a new Note */
  1726. const char *content = xs_dict_get(args, "status");
  1727. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1728. const char *visibility = xs_dict_get(args, "visibility");
  1729. const char *summary = xs_dict_get(args, "spoiler_text");
  1730. const char *media_ids = xs_dict_get(args, "media_ids");
  1731. if (xs_is_null(media_ids))
  1732. media_ids = xs_dict_get(args, "media_ids[]");
  1733. if (xs_is_null(media_ids))
  1734. media_ids = xs_dict_get(args, "media_ids");
  1735. if (xs_is_null(visibility))
  1736. visibility = "public";
  1737. xs *attach_list = xs_list_new();
  1738. xs *irt = NULL;
  1739. /* is it a reply? */
  1740. if (mid != NULL) {
  1741. xs *r_msg = NULL;
  1742. const char *md5 = MID_TO_MD5(mid);
  1743. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1744. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1745. }
  1746. /* does it have attachments? */
  1747. if (!xs_is_null(media_ids)) {
  1748. xs *mi = NULL;
  1749. if (xs_type(media_ids) == XSTYPE_LIST)
  1750. mi = xs_dup(media_ids);
  1751. else {
  1752. mi = xs_list_new();
  1753. mi = xs_list_append(mi, media_ids);
  1754. }
  1755. xs_list *p = mi;
  1756. xs_str *v;
  1757. while (xs_list_iter(&p, &v)) {
  1758. xs *l = xs_list_new();
  1759. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1760. xs *desc = static_get_meta(&snac, v);
  1761. l = xs_list_append(l, url);
  1762. l = xs_list_append(l, desc);
  1763. attach_list = xs_list_append(attach_list, l);
  1764. }
  1765. }
  1766. /* prepare the message */
  1767. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1768. strcmp(visibility, "public") == 0 ? 0 : 1);
  1769. if (!xs_is_null(summary) && *summary) {
  1770. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1771. msg = xs_dict_set(msg, "summary", summary);
  1772. }
  1773. /* store */
  1774. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1775. /* 'Create' message */
  1776. xs *c_msg = msg_create(&snac, msg);
  1777. enqueue_message(&snac, c_msg);
  1778. timeline_touch(&snac);
  1779. /* convert to a mastodon status as a response code */
  1780. xs *st = mastoapi_status(&snac, msg);
  1781. *body = xs_json_dumps(st, 4);
  1782. *ctype = "application/json";
  1783. status = 200;
  1784. }
  1785. else
  1786. status = 401;
  1787. }
  1788. else
  1789. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1790. if (logged_in) {
  1791. /* operations on a status */
  1792. xs *l = xs_split(cmd, "/");
  1793. const char *mid = xs_list_get(l, 3);
  1794. const char *op = xs_list_get(l, 4);
  1795. if (!xs_is_null(mid)) {
  1796. xs *msg = NULL;
  1797. xs *out = NULL;
  1798. /* skip the 'fake' part of the id */
  1799. mid = MID_TO_MD5(mid);
  1800. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1801. char *id = xs_dict_get(msg, "id");
  1802. if (op == NULL) {
  1803. /* no operation (?) */
  1804. }
  1805. else
  1806. if (strcmp(op, "favourite") == 0) { /** **/
  1807. xs *n_msg = msg_admiration(&snac, id, "Like");
  1808. if (n_msg != NULL) {
  1809. enqueue_message(&snac, n_msg);
  1810. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1811. out = mastoapi_status(&snac, msg);
  1812. }
  1813. }
  1814. else
  1815. if (strcmp(op, "unfavourite") == 0) { /** **/
  1816. /* partial support: as the original Like message
  1817. is not stored anywhere here, it's not possible
  1818. to send an Undo + Like; the only thing done here
  1819. is to delete the actor from the list of likes */
  1820. object_unadmire(id, snac.actor, 1);
  1821. }
  1822. else
  1823. if (strcmp(op, "reblog") == 0) { /** **/
  1824. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1825. if (n_msg != NULL) {
  1826. enqueue_message(&snac, n_msg);
  1827. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1828. out = mastoapi_status(&snac, msg);
  1829. }
  1830. }
  1831. else
  1832. if (strcmp(op, "unreblog") == 0) { /** **/
  1833. /* partial support: see comment in 'unfavourite' */
  1834. object_unadmire(id, snac.actor, 0);
  1835. }
  1836. else
  1837. if (strcmp(op, "bookmark") == 0) { /** **/
  1838. /* snac does not support bookmarks */
  1839. }
  1840. else
  1841. if (strcmp(op, "unbookmark") == 0) { /** **/
  1842. /* snac does not support bookmarks */
  1843. }
  1844. else
  1845. if (strcmp(op, "pin") == 0) { /** **/
  1846. /* pin this message */
  1847. if (pin(&snac, id))
  1848. out = mastoapi_status(&snac, msg);
  1849. else
  1850. status = 422;
  1851. }
  1852. else
  1853. if (strcmp(op, "unpin") == 0) { /** **/
  1854. /* unpin this message */
  1855. unpin(&snac, id);
  1856. out = mastoapi_status(&snac, msg);
  1857. }
  1858. else
  1859. if (strcmp(op, "mute") == 0) { /** **/
  1860. /* Mastodon's mute is snac's hide */
  1861. }
  1862. else
  1863. if (strcmp(op, "unmute") == 0) { /** **/
  1864. /* Mastodon's unmute is snac's unhide */
  1865. }
  1866. }
  1867. if (out != NULL) {
  1868. *body = xs_json_dumps(out, 4);
  1869. *ctype = "application/json";
  1870. status = 200;
  1871. }
  1872. }
  1873. }
  1874. else
  1875. status = 401;
  1876. }
  1877. else
  1878. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1879. if (logged_in) {
  1880. notify_clear(&snac);
  1881. timeline_touch(&snac);
  1882. *body = xs_dup("{}");
  1883. *ctype = "application/json";
  1884. status = 200;
  1885. }
  1886. else
  1887. status = 401;
  1888. }
  1889. else
  1890. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1891. /* I don't know what I'm doing */
  1892. if (logged_in) {
  1893. char *v;
  1894. xs *wpush = xs_dict_new();
  1895. wpush = xs_dict_append(wpush, "id", "1");
  1896. v = xs_dict_get(args, "data");
  1897. v = xs_dict_get(v, "alerts");
  1898. wpush = xs_dict_append(wpush, "alerts", v);
  1899. v = xs_dict_get(args, "subscription");
  1900. v = xs_dict_get(v, "endpoint");
  1901. wpush = xs_dict_append(wpush, "endpoint", v);
  1902. xs *server_key = random_str();
  1903. wpush = xs_dict_append(wpush, "server_key", server_key);
  1904. *body = xs_json_dumps(wpush, 4);
  1905. *ctype = "application/json";
  1906. status = 200;
  1907. }
  1908. else
  1909. status = 401;
  1910. }
  1911. else
  1912. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1913. if (logged_in) {
  1914. const xs_list *file = xs_dict_get(args, "file");
  1915. const char *desc = xs_dict_get(args, "description");
  1916. if (xs_is_null(desc))
  1917. desc = "";
  1918. status = 400;
  1919. if (xs_type(file) == XSTYPE_LIST) {
  1920. const char *fn = xs_list_get(file, 0);
  1921. if (*fn != '\0') {
  1922. char *ext = strrchr(fn, '.');
  1923. xs *hash = xs_md5_hex(fn, strlen(fn));
  1924. xs *id = xs_fmt("%s%s", hash, ext);
  1925. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1926. int fo = xs_number_get(xs_list_get(file, 1));
  1927. int fs = xs_number_get(xs_list_get(file, 2));
  1928. /* store */
  1929. static_put(&snac, id, payload + fo, fs);
  1930. static_put_meta(&snac, id, desc);
  1931. /* prepare a response */
  1932. xs *rsp = xs_dict_new();
  1933. rsp = xs_dict_append(rsp, "id", id);
  1934. rsp = xs_dict_append(rsp, "type", "image");
  1935. rsp = xs_dict_append(rsp, "url", url);
  1936. rsp = xs_dict_append(rsp, "preview_url", url);
  1937. rsp = xs_dict_append(rsp, "remote_url", url);
  1938. rsp = xs_dict_append(rsp, "description", desc);
  1939. *body = xs_json_dumps(rsp, 4);
  1940. *ctype = "application/json";
  1941. status = 200;
  1942. }
  1943. }
  1944. }
  1945. else
  1946. status = 401;
  1947. }
  1948. else
  1949. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1950. if (logged_in) {
  1951. /* account-related information */
  1952. xs *l = xs_split(cmd, "/");
  1953. const char *md5 = xs_list_get(l, 3);
  1954. const char *opt = xs_list_get(l, 4);
  1955. xs *rsp = NULL;
  1956. if (!xs_is_null(md5) && *md5) {
  1957. xs *actor_o = NULL;
  1958. if (xs_is_null(opt)) {
  1959. /* ? */
  1960. }
  1961. else
  1962. if (strcmp(opt, "follow") == 0) { /** **/
  1963. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1964. const char *actor = xs_dict_get(actor_o, "id");
  1965. xs *msg = msg_follow(&snac, actor);
  1966. if (msg != NULL) {
  1967. /* reload the actor from the message, in may be different */
  1968. actor = xs_dict_get(msg, "object");
  1969. following_add(&snac, actor, msg);
  1970. enqueue_output_by_actor(&snac, msg, actor, 0);
  1971. rsp = mastoapi_relationship(&snac, md5);
  1972. }
  1973. }
  1974. }
  1975. else
  1976. if (strcmp(opt, "unfollow") == 0) { /** **/
  1977. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1978. const char *actor = xs_dict_get(actor_o, "id");
  1979. /* get the following object */
  1980. xs *object = NULL;
  1981. if (valid_status(following_get(&snac, actor, &object))) {
  1982. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1983. following_del(&snac, actor);
  1984. enqueue_output_by_actor(&snac, msg, actor, 0);
  1985. rsp = mastoapi_relationship(&snac, md5);
  1986. }
  1987. }
  1988. }
  1989. else
  1990. if (strcmp(opt, "block") == 0) { /** **/
  1991. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1992. const char *actor = xs_dict_get(actor_o, "id");
  1993. mute(&snac, actor);
  1994. rsp = mastoapi_relationship(&snac, md5);
  1995. }
  1996. }
  1997. else
  1998. if (strcmp(opt, "unblock") == 0) { /** **/
  1999. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2000. const char *actor = xs_dict_get(actor_o, "id");
  2001. unmute(&snac, actor);
  2002. rsp = mastoapi_relationship(&snac, md5);
  2003. }
  2004. }
  2005. }
  2006. if (rsp != NULL) {
  2007. *body = xs_json_dumps(rsp, 4);
  2008. *ctype = "application/json";
  2009. status = 200;
  2010. }
  2011. }
  2012. else
  2013. status = 401;
  2014. }
  2015. else
  2016. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2017. if (logged_in) {
  2018. /* operations on a status */
  2019. xs *l = xs_split(cmd, "/");
  2020. const char *mid = xs_list_get(l, 3);
  2021. const char *op = xs_list_get(l, 4);
  2022. if (!xs_is_null(mid)) {
  2023. xs *msg = NULL;
  2024. xs *out = NULL;
  2025. /* skip the 'fake' part of the id */
  2026. mid = MID_TO_MD5(mid);
  2027. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2028. const char *id = xs_dict_get(msg, "id");
  2029. const char *atto = get_atto(msg);
  2030. xs_list *opts = xs_dict_get(msg, "oneOf");
  2031. if (opts == NULL)
  2032. opts = xs_dict_get(msg, "anyOf");
  2033. if (op == NULL) {
  2034. }
  2035. else
  2036. if (strcmp(op, "votes") == 0) {
  2037. xs_list *choices = xs_dict_get(args, "choices[]");
  2038. if (xs_is_null(choices))
  2039. choices = xs_dict_get(args, "choices");
  2040. if (xs_type(choices) == XSTYPE_LIST) {
  2041. xs_str *v;
  2042. while (xs_list_iter(&choices, &v)) {
  2043. int io = atoi(v);
  2044. const xs_dict *o = xs_list_get(opts, io);
  2045. if (o) {
  2046. const char *name = xs_dict_get(o, "name");
  2047. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2048. msg = xs_dict_append(msg, "name", name);
  2049. xs *c_msg = msg_create(&snac, msg);
  2050. enqueue_message(&snac, c_msg);
  2051. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2052. }
  2053. }
  2054. out = mastoapi_poll(&snac, msg);
  2055. }
  2056. }
  2057. }
  2058. if (out != NULL) {
  2059. *body = xs_json_dumps(out, 4);
  2060. *ctype = "application/json";
  2061. status = 200;
  2062. }
  2063. }
  2064. }
  2065. else
  2066. status = 401;
  2067. }
  2068. /* user cleanup */
  2069. if (logged_in)
  2070. user_free(&snac);
  2071. return status;
  2072. }
  2073. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2074. char **body, int *b_size, char **ctype) {
  2075. (void)req;
  2076. (void)body;
  2077. (void)b_size;
  2078. (void)ctype;
  2079. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2080. return 0;
  2081. srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
  2082. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2083. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2084. // pretend we deleted it, since it doesn't exist anyway
  2085. return 200;
  2086. }
  2087. return 0;
  2088. }
  2089. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2090. const char *payload, int p_size,
  2091. char **body, int *b_size, char **ctype)
  2092. {
  2093. (void)p_size;
  2094. (void)b_size;
  2095. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2096. return 0;
  2097. int status = 404;
  2098. xs *args = NULL;
  2099. char *i_ctype = xs_dict_get(req, "content-type");
  2100. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2101. if (!xs_is_null(payload))
  2102. args = xs_json_loads(payload);
  2103. }
  2104. else
  2105. args = xs_dup(xs_dict_get(req, "p_vars"));
  2106. if (args == NULL)
  2107. return 400;
  2108. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2109. snac snac = {0};
  2110. int logged_in = process_auth_token(&snac, req);
  2111. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2112. if (logged_in) {
  2113. xs *l = xs_split(cmd, "/");
  2114. const char *stid = xs_list_get(l, 3);
  2115. if (!xs_is_null(stid)) {
  2116. const char *desc = xs_dict_get(args, "description");
  2117. /* set the image metadata */
  2118. static_put_meta(&snac, stid, desc);
  2119. /* prepare a response */
  2120. xs *rsp = xs_dict_new();
  2121. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2122. rsp = xs_dict_append(rsp, "id", stid);
  2123. rsp = xs_dict_append(rsp, "type", "image");
  2124. rsp = xs_dict_append(rsp, "url", url);
  2125. rsp = xs_dict_append(rsp, "preview_url", url);
  2126. rsp = xs_dict_append(rsp, "remote_url", url);
  2127. rsp = xs_dict_append(rsp, "description", desc);
  2128. *body = xs_json_dumps(rsp, 4);
  2129. *ctype = "application/json";
  2130. status = 200;
  2131. }
  2132. }
  2133. else
  2134. status = 401;
  2135. }
  2136. else
  2137. if (xs_startswith(cmd, "/v1/statuses")) {
  2138. if (logged_in) {
  2139. xs *l = xs_split(cmd, "/");
  2140. const char *mid = xs_list_get(l, 3);
  2141. if (!xs_is_null(mid)) {
  2142. const char *md5 = MID_TO_MD5(mid);
  2143. xs *rsp = NULL;
  2144. xs *msg = NULL;
  2145. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2146. const char *content = xs_dict_get(args, "status");
  2147. xs *atls = xs_list_new();
  2148. xs *f_content = not_really_markdown(content, &atls);
  2149. /* replace fields with new content */
  2150. msg = xs_dict_set(msg, "sourceContent", content);
  2151. msg = xs_dict_set(msg, "content", f_content);
  2152. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2153. msg = xs_dict_set(msg, "updated", updated);
  2154. /* overwrite object, not updating the indexes */
  2155. object_add_ow(xs_dict_get(msg, "id"), msg);
  2156. /* update message */
  2157. xs *c_msg = msg_update(&snac, msg);
  2158. enqueue_message(&snac, c_msg);
  2159. rsp = mastoapi_status(&snac, msg);
  2160. }
  2161. if (rsp != NULL) {
  2162. *body = xs_json_dumps(rsp, 4);
  2163. *ctype = "application/json";
  2164. status = 200;
  2165. }
  2166. }
  2167. }
  2168. else
  2169. status = 401;
  2170. }
  2171. /* user cleanup */
  2172. if (logged_in)
  2173. user_free(&snac);
  2174. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2175. return status;
  2176. }
  2177. void mastoapi_purge(void)
  2178. {
  2179. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2180. xs *files = xs_glob(spec, 1, 0);
  2181. xs_list *p = files;
  2182. xs_str *v;
  2183. time_t mt = time(NULL) - 3600;
  2184. while (xs_list_iter(&p, &v)) {
  2185. xs *cid = xs_replace(v, ".json", "");
  2186. xs *fn = _app_fn(cid);
  2187. if (mtime(fn) < mt) {
  2188. /* get the app */
  2189. xs *app = app_get(cid);
  2190. if (app) {
  2191. /* old apps with no uid are incomplete cruft */
  2192. const char *uid = xs_dict_get(app, "uid");
  2193. if (xs_is_null(uid) || *uid == '\0') {
  2194. unlink(fn);
  2195. srv_debug(2, xs_fmt("purged %s", fn));
  2196. }
  2197. }
  2198. }
  2199. }
  2200. }
  2201. #endif /* #ifndef NO_MASTODON_API */