mastoapi.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283
  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 HTTP_STATUS_INTERNAL_SERVER_ERROR;
  29. int status = HTTP_STATUS_CREATED;
  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 = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  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 HTTP_STATUS_INTERNAL_SERVER_ERROR;
  74. int status = HTTP_STATUS_CREATED;
  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 = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  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=\"%s:/" "/%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 = HTTP_STATUS_NOT_FOUND;
  135. const 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 = HTTP_STATUS_BAD_REQUEST;
  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. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  149. if (xs_is_null(state))
  150. state = "";
  151. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-login",
  152. ruri, cid, state, USER_AGENT);
  153. *ctype = "text/html";
  154. status = HTTP_STATUS_OK;
  155. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  156. }
  157. else
  158. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  159. }
  160. else
  161. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  162. }
  163. else
  164. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  165. const char *host = xs_dict_get(srv_config, "host");
  166. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  167. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-get-token",
  168. "", "", "", USER_AGENT);
  169. *ctype = "text/html";
  170. status = HTTP_STATUS_OK;
  171. }
  172. return status;
  173. }
  174. int oauth_post_handler(const xs_dict *req, const char *q_path,
  175. const char *payload, int p_size,
  176. char **body, int *b_size, char **ctype)
  177. {
  178. (void)p_size;
  179. (void)b_size;
  180. if (!xs_startswith(q_path, "/oauth/"))
  181. return 0;
  182. int status = HTTP_STATUS_NOT_FOUND;
  183. const char *i_ctype = xs_dict_get(req, "content-type");
  184. xs *args = NULL;
  185. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  186. if (!xs_is_null(payload))
  187. args = xs_json_loads(payload);
  188. }
  189. else
  190. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  191. xs *upl = xs_url_dec(payload);
  192. args = xs_url_vars(upl);
  193. }
  194. else
  195. args = xs_dup(xs_dict_get(req, "p_vars"));
  196. if (args == NULL)
  197. return HTTP_STATUS_BAD_REQUEST;
  198. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  199. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  200. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  201. const char *login = xs_dict_get(args, "login");
  202. const char *passwd = xs_dict_get(args, "passwd");
  203. const char *redir = xs_dict_get(args, "redir");
  204. const char *cid = xs_dict_get(args, "cid");
  205. const char *state = xs_dict_get(args, "state");
  206. const char *host = xs_dict_get(srv_config, "host");
  207. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  208. /* by default, generate another login form with an error */
  209. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-login",
  210. redir, cid, state, USER_AGENT);
  211. *ctype = "text/html";
  212. status = HTTP_STATUS_OK;
  213. if (login && passwd && redir && cid) {
  214. snac snac;
  215. if (user_open(&snac, login)) {
  216. /* check the login + password */
  217. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  218. /* success! redirect to the desired uri */
  219. xs *code = random_str();
  220. xs_free(*body);
  221. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  222. *body = xs_dup(code);
  223. }
  224. else {
  225. if (xs_str_in(redir, "?") != -1)
  226. *body = xs_fmt("%s&code=%s", redir, code);
  227. else
  228. *body = xs_fmt("%s?code=%s", redir, code);
  229. status = HTTP_STATUS_SEE_OTHER;
  230. }
  231. /* if there is a state, add it */
  232. if (!xs_is_null(state) && *state) {
  233. *body = xs_str_cat(*body, "&state=");
  234. *body = xs_str_cat(*body, state);
  235. }
  236. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  237. login, *body));
  238. /* assign the login to the app */
  239. xs *app = app_get(cid);
  240. if (app != NULL) {
  241. app = xs_dict_set(app, "uid", login);
  242. app = xs_dict_set(app, "code", code);
  243. app_add(cid, app);
  244. }
  245. else
  246. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  247. }
  248. else
  249. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  250. user_free(&snac);
  251. }
  252. else
  253. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  254. }
  255. else
  256. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  257. }
  258. else
  259. if (strcmp(cmd, "/token") == 0) { /** **/
  260. xs *wrk = NULL;
  261. const char *gtype = xs_dict_get(args, "grant_type");
  262. const char *code = xs_dict_get(args, "code");
  263. const char *cid = xs_dict_get(args, "client_id");
  264. const char *csec = xs_dict_get(args, "client_secret");
  265. const char *ruri = xs_dict_get(args, "redirect_uri");
  266. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  267. but if it's enabled, it makes it crash after some more steps, which
  268. is FAR WORSE */
  269. const char *scope = NULL;
  270. // scope = xs_dict_get(args, "scope");
  271. /* no client_secret? check if it's inside an authorization header
  272. (AndStatus does it this way) */
  273. if (xs_is_null(csec)) {
  274. const char *auhdr = xs_dict_get(req, "authorization");
  275. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  276. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  277. int size;
  278. xs *s2 = xs_base64_dec(s1, &size);
  279. if (!xs_is_null(s2)) {
  280. xs *l1 = xs_split(s2, ":");
  281. if (xs_list_len(l1) == 2) {
  282. wrk = xs_dup(xs_list_get(l1, 1));
  283. csec = wrk;
  284. }
  285. }
  286. }
  287. }
  288. /* no code?
  289. I'm not sure of the impacts of this right now, but Subway Tooter does not
  290. provide a code so one must be generated */
  291. if (xs_is_null(code)){
  292. code = random_str();
  293. }
  294. if (gtype && code && cid && csec && ruri) {
  295. xs *app = app_get(cid);
  296. if (app == NULL) {
  297. status = HTTP_STATUS_UNAUTHORIZED;
  298. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  299. }
  300. else
  301. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  302. status = HTTP_STATUS_UNAUTHORIZED;
  303. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  304. }
  305. else {
  306. xs *rsp = xs_dict_new();
  307. xs *cat = xs_number_new(time(NULL));
  308. xs *tokid = random_str();
  309. rsp = xs_dict_append(rsp, "access_token", tokid);
  310. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  311. rsp = xs_dict_append(rsp, "created_at", cat);
  312. if (!xs_is_null(scope))
  313. rsp = xs_dict_append(rsp, "scope", scope);
  314. *body = xs_json_dumps(rsp, 4);
  315. *ctype = "application/json";
  316. status = HTTP_STATUS_OK;
  317. const char *uid = xs_dict_get(app, "uid");
  318. srv_debug(1, xs_fmt("oauth token: "
  319. "successful login for %s, new token %s", uid, tokid));
  320. xs *token = xs_dict_new();
  321. token = xs_dict_append(token, "token", tokid);
  322. token = xs_dict_append(token, "client_id", cid);
  323. token = xs_dict_append(token, "client_secret", csec);
  324. token = xs_dict_append(token, "uid", uid);
  325. token = xs_dict_append(token, "code", code);
  326. token_add(tokid, token);
  327. }
  328. }
  329. else {
  330. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  331. status = HTTP_STATUS_BAD_REQUEST;
  332. }
  333. }
  334. else
  335. if (strcmp(cmd, "/revoke") == 0) { /** **/
  336. const char *cid = xs_dict_get(args, "client_id");
  337. const char *csec = xs_dict_get(args, "client_secret");
  338. const char *tokid = xs_dict_get(args, "token");
  339. if (cid && csec && tokid) {
  340. xs *token = token_get(tokid);
  341. *body = xs_str_new("{}");
  342. *ctype = "application/json";
  343. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  344. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  345. status = HTTP_STATUS_FORBIDDEN;
  346. }
  347. else {
  348. token_del(tokid);
  349. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  350. status = HTTP_STATUS_OK;
  351. /* also delete the app, as it serves no purpose from now on */
  352. app_del(cid);
  353. }
  354. }
  355. else {
  356. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  357. status = HTTP_STATUS_FORBIDDEN;
  358. }
  359. }
  360. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  361. const char *login = xs_dict_get(args, "login");
  362. const char *passwd = xs_dict_get(args, "passwd");
  363. const char *host = xs_dict_get(srv_config, "host");
  364. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  365. /* by default, generate another login form with an error */
  366. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-get-token",
  367. "", "", "", USER_AGENT);
  368. *ctype = "text/html";
  369. status = HTTP_STATUS_OK;
  370. if (login && passwd) {
  371. snac user;
  372. if (user_open(&user, login)) {
  373. /* check the login + password */
  374. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  375. /* success! create a new token */
  376. xs *tokid = random_str();
  377. srv_debug(1, xs_fmt("x-snac-new-token: "
  378. "successful login for %s, new token %s", login, tokid));
  379. xs *token = xs_dict_new();
  380. token = xs_dict_append(token, "token", tokid);
  381. token = xs_dict_append(token, "client_id", "snac-client");
  382. token = xs_dict_append(token, "client_secret", "");
  383. token = xs_dict_append(token, "uid", login);
  384. token = xs_dict_append(token, "code", "");
  385. token_add(tokid, token);
  386. *ctype = "text/plain";
  387. xs_free(*body);
  388. *body = xs_dup(tokid);
  389. }
  390. user_free(&user);
  391. }
  392. }
  393. }
  394. return status;
  395. }
  396. xs_str *mastoapi_id(const xs_dict *msg)
  397. /* returns a somewhat Mastodon-compatible status id */
  398. {
  399. const char *id = xs_dict_get(msg, "id");
  400. xs *md5 = xs_md5_hex(id, strlen(id));
  401. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  402. }
  403. #define MID_TO_MD5(id) (id + 10)
  404. xs_dict *mastoapi_account(const xs_dict *actor)
  405. /* converts an ActivityPub actor to a Mastodon account */
  406. {
  407. const char *id = xs_dict_get(actor, "id");
  408. const char *pub = xs_dict_get(actor, "published");
  409. if (xs_type(id) != XSTYPE_STRING)
  410. return NULL;
  411. const char *prefu = xs_dict_get(actor, "preferredUsername");
  412. const char *display_name = xs_dict_get(actor, "name");
  413. if (xs_is_null(display_name) || *display_name == '\0')
  414. display_name = prefu;
  415. xs_dict *acct = xs_dict_new();
  416. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  417. acct = xs_dict_append(acct, "id", acct_md5);
  418. acct = xs_dict_append(acct, "username", prefu);
  419. acct = xs_dict_append(acct, "display_name", display_name);
  420. acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE));
  421. acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE));
  422. acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE));
  423. acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE));
  424. acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE));
  425. acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST));
  426. {
  427. /* create the acct field as user@host */
  428. xs *l = xs_split(id, "/");
  429. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  430. acct = xs_dict_append(acct, "acct", fquid);
  431. }
  432. if (pub)
  433. acct = xs_dict_append(acct, "created_at", pub);
  434. else {
  435. /* unset created_at crashes Tusky, so lie like a mf */
  436. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  437. acct = xs_dict_append(acct, "created_at", date);
  438. }
  439. xs *last_status_at = xs_str_utctime(0, "%Y-%m-%d");
  440. acct = xs_dict_append(acct, "last_status_at", last_status_at);
  441. const char *note = xs_dict_get(actor, "summary");
  442. if (xs_is_null(note))
  443. note = "";
  444. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  445. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE));
  446. else
  447. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE));
  448. acct = xs_dict_append(acct, "note", note);
  449. acct = xs_dict_append(acct, "url", id);
  450. acct = xs_dict_append(acct, "uri", id);
  451. xs *avatar = NULL;
  452. const xs_dict *av = xs_dict_get(actor, "icon");
  453. if (xs_type(av) == XSTYPE_DICT) {
  454. const char *url = xs_dict_get(av, "url");
  455. if (url != NULL)
  456. avatar = xs_dup(url);
  457. }
  458. if (avatar == NULL)
  459. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  460. acct = xs_dict_append(acct, "avatar", avatar);
  461. acct = xs_dict_append(acct, "avatar_static", avatar);
  462. xs *header = NULL;
  463. const xs_dict *hd = xs_dict_get(actor, "image");
  464. if (xs_type(hd) == XSTYPE_DICT)
  465. header = xs_dup(xs_dict_get(hd, "url"));
  466. if (xs_is_null(header))
  467. header = xs_fmt("%s/header.png", srv_baseurl);
  468. acct = xs_dict_append(acct, "header", header);
  469. acct = xs_dict_append(acct, "header_static", header);
  470. /* emojis */
  471. const xs_list *p;
  472. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  473. xs *eml = xs_list_new();
  474. const xs_dict *v;
  475. int c = 0;
  476. while (xs_list_next(p, &v, &c)) {
  477. const char *type = xs_dict_get(v, "type");
  478. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  479. const char *name = xs_dict_get(v, "name");
  480. const xs_dict *icon = xs_dict_get(v, "icon");
  481. if (!xs_is_null(name) && !xs_is_null(icon)) {
  482. const char *url = xs_dict_get(icon, "url");
  483. if (!xs_is_null(url)) {
  484. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  485. xs *d1 = xs_dict_new();
  486. d1 = xs_dict_append(d1, "shortcode", nm);
  487. d1 = xs_dict_append(d1, "url", url);
  488. d1 = xs_dict_append(d1, "static_url", url);
  489. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  490. eml = xs_list_append(eml, d1);
  491. }
  492. }
  493. }
  494. }
  495. acct = xs_dict_append(acct, "emojis", eml);
  496. }
  497. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  498. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  499. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  500. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  501. xs *fields = xs_list_new();
  502. p = xs_dict_get(actor, "attachment");
  503. const xs_dict *v;
  504. /* dict of validated links */
  505. xs_dict *val_links = NULL;
  506. const xs_dict *metadata = xs_stock(XSTYPE_DICT);
  507. snac user = {0};
  508. if (xs_startswith(id, srv_baseurl)) {
  509. /* if it's a local user, open it and pick its validated links */
  510. if (user_open(&user, prefu)) {
  511. val_links = user.links;
  512. metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
  513. }
  514. }
  515. if (xs_is_null(val_links))
  516. val_links = xs_stock(XSTYPE_DICT);
  517. int c = 0;
  518. while (xs_list_next(p, &v, &c)) {
  519. const char *type = xs_dict_get(v, "type");
  520. const char *name = xs_dict_get(v, "name");
  521. const char *value = xs_dict_get(v, "value");
  522. if (!xs_is_null(type) && !xs_is_null(name) &&
  523. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  524. xs *val_date = NULL;
  525. const char *url = xs_dict_get(metadata, name);
  526. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  527. const xs_number *verified_time = xs_dict_get(val_links, url);
  528. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  529. time_t t = xs_number_get(verified_time);
  530. if (t > 0)
  531. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  532. }
  533. }
  534. xs *d = xs_dict_new();
  535. d = xs_dict_append(d, "name", name);
  536. d = xs_dict_append(d, "value", value);
  537. d = xs_dict_append(d, "verified_at",
  538. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  539. val_date : xs_stock(XSTYPE_NULL));
  540. fields = xs_list_append(fields, d);
  541. }
  542. }
  543. user_free(&user);
  544. acct = xs_dict_append(acct, "fields", fields);
  545. return acct;
  546. }
  547. xs_str *mastoapi_date(const char *date)
  548. /* converts an ISO 8601 date to whatever format Mastodon uses */
  549. {
  550. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  551. s = xs_str_cat(s, ".000Z");
  552. return s;
  553. }
  554. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  555. /* creates a mastoapi Poll object */
  556. {
  557. xs_dict *poll = xs_dict_new();
  558. xs *mid = mastoapi_id(msg);
  559. const xs_list *opts = NULL;
  560. const xs_val *v;
  561. int num_votes = 0;
  562. xs *options = xs_list_new();
  563. poll = xs_dict_append(poll, "id", mid);
  564. const char *date = xs_dict_get(msg, "endTime");
  565. if (date == NULL)
  566. date = xs_dict_get(msg, "closed");
  567. if (date == NULL)
  568. return NULL;
  569. xs *fd = mastoapi_date(date);
  570. poll = xs_dict_append(poll, "expires_at", fd);
  571. date = xs_dict_get(msg, "closed");
  572. time_t t = 0;
  573. if (date != NULL)
  574. t = xs_parse_iso_date(date, 0);
  575. poll = xs_dict_append(poll, "expired",
  576. t < time(NULL) ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  577. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  578. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE));
  579. else {
  580. opts = xs_dict_get(msg, "anyOf");
  581. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE));
  582. }
  583. int c = 0;
  584. while (xs_list_next(opts, &v, &c)) {
  585. const char *title = xs_dict_get(v, "name");
  586. const char *replies = xs_dict_get(v, "replies");
  587. if (title && replies) {
  588. const char *votes_count = xs_dict_get(replies, "totalItems");
  589. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  590. xs *d = xs_dict_new();
  591. d = xs_dict_append(d, "title", title);
  592. d = xs_dict_append(d, "votes_count", votes_count);
  593. options = xs_list_append(options, d);
  594. num_votes += xs_number_get(votes_count);
  595. }
  596. }
  597. }
  598. poll = xs_dict_append(poll, "options", options);
  599. xs *vc = xs_number_new(num_votes);
  600. poll = xs_dict_append(poll, "votes_count", vc);
  601. poll = xs_dict_append(poll, "voted",
  602. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  603. xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  604. return poll;
  605. }
  606. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  607. /* converts an ActivityPub note to a Mastodon status */
  608. {
  609. xs *actor = NULL;
  610. actor_get_refresh(snac, get_atto(msg), &actor);
  611. /* if the author is not here, discard */
  612. if (actor == NULL)
  613. return NULL;
  614. const char *type = xs_dict_get(msg, "type");
  615. const char *id = xs_dict_get(msg, "id");
  616. /* fail if it's not a valid actor */
  617. if (xs_is_null(type) || xs_is_null(id))
  618. return NULL;
  619. xs *acct = mastoapi_account(actor);
  620. if (acct == NULL)
  621. return NULL;
  622. xs *idx = NULL;
  623. xs *ixc = NULL;
  624. const char *tmp;
  625. xs *mid = mastoapi_id(msg);
  626. xs_dict *st = xs_dict_new();
  627. st = xs_dict_append(st, "id", mid);
  628. st = xs_dict_append(st, "uri", id);
  629. st = xs_dict_append(st, "url", id);
  630. st = xs_dict_append(st, "account", acct);
  631. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  632. st = xs_dict_append(st, "created_at", fd);
  633. {
  634. const char *content = xs_dict_get(msg, "content");
  635. const char *name = xs_dict_get(msg, "name");
  636. xs *s1 = NULL;
  637. if (name && content)
  638. s1 = xs_fmt("%s<br><br>%s", name, content);
  639. else
  640. if (name)
  641. s1 = xs_dup(name);
  642. else
  643. if (content)
  644. s1 = xs_dup(content);
  645. else
  646. s1 = xs_str_new(NULL);
  647. st = xs_dict_append(st, "content", s1);
  648. }
  649. st = xs_dict_append(st, "visibility",
  650. is_msg_public(msg) ? "public" : "private");
  651. tmp = xs_dict_get(msg, "sensitive");
  652. if (xs_is_null(tmp))
  653. tmp = xs_stock(XSTYPE_FALSE);
  654. st = xs_dict_append(st, "sensitive", tmp);
  655. tmp = xs_dict_get(msg, "summary");
  656. if (xs_is_null(tmp))
  657. tmp = "";
  658. st = xs_dict_append(st, "spoiler_text", tmp);
  659. /* create the list of attachments */
  660. xs *attach = get_attachments(msg);
  661. {
  662. xs_list *p = attach;
  663. const xs_dict *v;
  664. xs *matt = xs_list_new();
  665. while (xs_list_iter(&p, &v)) {
  666. const char *type = xs_dict_get(v, "type");
  667. const char *href = xs_dict_get(v, "href");
  668. const char *name = xs_dict_get(v, "name");
  669. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  670. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  671. xs *d = xs_dict_new();
  672. d = xs_dict_append(d, "id", matteid);
  673. d = xs_dict_append(d, "url", href);
  674. d = xs_dict_append(d, "preview_url", href);
  675. d = xs_dict_append(d, "remote_url", href);
  676. d = xs_dict_append(d, "description", name);
  677. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  678. matt = xs_list_append(matt, d);
  679. }
  680. }
  681. st = xs_dict_append(st, "media_attachments", matt);
  682. }
  683. {
  684. xs *ml = xs_list_new();
  685. xs *htl = xs_list_new();
  686. xs *eml = xs_list_new();
  687. const xs_list *tag = xs_dict_get(msg, "tag");
  688. int n = 0;
  689. xs *tag_list = NULL;
  690. if (xs_type(tag) == XSTYPE_DICT) {
  691. tag_list = xs_list_new();
  692. tag_list = xs_list_append(tag_list, tag);
  693. }
  694. else
  695. if (xs_type(tag) == XSTYPE_LIST)
  696. tag_list = xs_dup(tag);
  697. else
  698. tag_list = xs_list_new();
  699. tag = tag_list;
  700. const xs_dict *v;
  701. int c = 0;
  702. while (xs_list_next(tag, &v, &c)) {
  703. const char *type = xs_dict_get(v, "type");
  704. if (xs_is_null(type))
  705. continue;
  706. xs *d1 = xs_dict_new();
  707. if (strcmp(type, "Mention") == 0) {
  708. const char *name = xs_dict_get(v, "name");
  709. const char *href = xs_dict_get(v, "href");
  710. if (!xs_is_null(name) && !xs_is_null(href) &&
  711. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  712. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  713. xs *id = xs_fmt("%d", n++);
  714. d1 = xs_dict_append(d1, "id", id);
  715. d1 = xs_dict_append(d1, "username", nm);
  716. d1 = xs_dict_append(d1, "acct", nm);
  717. d1 = xs_dict_append(d1, "url", href);
  718. ml = xs_list_append(ml, d1);
  719. }
  720. }
  721. else
  722. if (strcmp(type, "Hashtag") == 0) {
  723. const char *name = xs_dict_get(v, "name");
  724. const char *href = xs_dict_get(v, "href");
  725. if (!xs_is_null(name) && !xs_is_null(href)) {
  726. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  727. d1 = xs_dict_append(d1, "name", nm);
  728. d1 = xs_dict_append(d1, "url", href);
  729. htl = xs_list_append(htl, d1);
  730. }
  731. }
  732. else
  733. if (strcmp(type, "Emoji") == 0) {
  734. const char *name = xs_dict_get(v, "name");
  735. const xs_dict *icon = xs_dict_get(v, "icon");
  736. if (!xs_is_null(name) && !xs_is_null(icon)) {
  737. const char *url = xs_dict_get(icon, "url");
  738. if (!xs_is_null(url)) {
  739. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  740. d1 = xs_dict_append(d1, "shortcode", nm);
  741. d1 = xs_dict_append(d1, "url", url);
  742. d1 = xs_dict_append(d1, "static_url", url);
  743. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  744. d1 = xs_dict_append(d1, "category", "Emojis");
  745. eml = xs_list_append(eml, d1);
  746. }
  747. }
  748. }
  749. }
  750. st = xs_dict_append(st, "mentions", ml);
  751. st = xs_dict_append(st, "tags", htl);
  752. st = xs_dict_append(st, "emojis", eml);
  753. }
  754. xs_free(idx);
  755. xs_free(ixc);
  756. idx = object_likes(id);
  757. ixc = xs_number_new(xs_list_len(idx));
  758. st = xs_dict_append(st, "favourites_count", ixc);
  759. st = xs_dict_append(st, "favourited",
  760. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  761. xs_free(idx);
  762. xs_free(ixc);
  763. idx = object_announces(id);
  764. ixc = xs_number_new(xs_list_len(idx));
  765. st = xs_dict_append(st, "reblogs_count", ixc);
  766. st = xs_dict_append(st, "reblogged",
  767. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  768. /* get the last person who boosted this */
  769. xs *boosted_by_md5 = NULL;
  770. if (xs_list_len(idx))
  771. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  772. xs_free(idx);
  773. xs_free(ixc);
  774. idx = object_children(id);
  775. ixc = xs_number_new(xs_list_len(idx));
  776. st = xs_dict_append(st, "replies_count", ixc);
  777. /* default in_reply_to values */
  778. st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL));
  779. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL));
  780. tmp = xs_dict_get(msg, "inReplyTo");
  781. if (!xs_is_null(tmp)) {
  782. xs *irto = NULL;
  783. if (valid_status(object_get(tmp, &irto))) {
  784. xs *irt_mid = mastoapi_id(irto);
  785. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  786. const char *at = NULL;
  787. if (!xs_is_null(at = get_atto(irto))) {
  788. xs *at_md5 = xs_md5_hex(at, strlen(at));
  789. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  790. }
  791. }
  792. }
  793. st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL));
  794. st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL));
  795. st = xs_dict_append(st, "language", "en");
  796. st = xs_dict_append(st, "filtered", xs_stock(XSTYPE_LIST));
  797. st = xs_dict_append(st, "muted", xs_stock(XSTYPE_FALSE));
  798. tmp = xs_dict_get(msg, "sourceContent");
  799. if (xs_is_null(tmp))
  800. tmp = "";
  801. st = xs_dict_append(st, "text", tmp);
  802. tmp = xs_dict_get(msg, "updated");
  803. xs *fd2 = NULL;
  804. if (xs_is_null(tmp))
  805. tmp = xs_stock(XSTYPE_NULL);
  806. else {
  807. fd2 = mastoapi_date(tmp);
  808. tmp = fd2;
  809. }
  810. st = xs_dict_append(st, "edited_at", tmp);
  811. if (strcmp(type, "Question") == 0) {
  812. xs *poll = mastoapi_poll(snac, msg);
  813. st = xs_dict_append(st, "poll", poll);
  814. }
  815. else
  816. st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL));
  817. st = xs_dict_append(st, "bookmarked", xs_stock(XSTYPE_FALSE));
  818. st = xs_dict_append(st, "pinned",
  819. (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  820. /* is it a boost? */
  821. if (!xs_is_null(boosted_by_md5)) {
  822. /* create a new dummy status, using st as the 'reblog' field */
  823. xs_dict *bst = xs_dup(st);
  824. xs *b_actor = NULL;
  825. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  826. xs *b_acct = mastoapi_account(b_actor);
  827. xs *fake_uri = NULL;
  828. if (snac)
  829. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  830. else
  831. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  832. bst = xs_dict_set(bst, "uri", fake_uri);
  833. bst = xs_dict_set(bst, "url", fake_uri);
  834. bst = xs_dict_set(bst, "account", b_acct);
  835. bst = xs_dict_set(bst, "content", "");
  836. bst = xs_dict_set(bst, "reblog", st);
  837. xs_free(st);
  838. st = bst;
  839. }
  840. }
  841. return st;
  842. }
  843. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  844. {
  845. xs_dict *rel = NULL;
  846. xs *actor_o = NULL;
  847. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  848. rel = xs_dict_new();
  849. const char *actor = xs_dict_get(actor_o, "id");
  850. rel = xs_dict_append(rel, "id", md5);
  851. rel = xs_dict_append(rel, "following",
  852. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  853. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  854. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  855. rel = xs_dict_append(rel, "followed_by",
  856. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  857. rel = xs_dict_append(rel, "blocking",
  858. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  859. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  860. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  861. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  862. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  863. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  864. rel = xs_dict_append(rel, "note", "");
  865. }
  866. return rel;
  867. }
  868. int process_auth_token(snac *snac, const xs_dict *req)
  869. /* processes an authorization token, if there is one */
  870. {
  871. int logged_in = 0;
  872. const char *v;
  873. /* if there is an authorization field, try to validate it */
  874. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  875. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  876. xs *token = token_get(tokid);
  877. if (token != NULL) {
  878. const char *uid = xs_dict_get(token, "uid");
  879. if (!xs_is_null(uid) && user_open(snac, uid)) {
  880. logged_in = 1;
  881. /* this counts as a 'login' */
  882. lastlog_write(snac, "mastoapi");
  883. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  884. }
  885. else
  886. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  887. }
  888. else
  889. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  890. }
  891. return logged_in;
  892. }
  893. void credentials_get(char **body, char **ctype, int *status, snac snac)
  894. {
  895. xs *acct = xs_dict_new();
  896. acct = xs_dict_append(acct, "id", snac.md5);
  897. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  898. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  899. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  900. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  901. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  902. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  903. acct = xs_dict_append(acct, "url", snac.actor);
  904. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  905. acct = xs_dict_append(acct, "bot", xs_dict_get(snac.config, "bot"));
  906. xs *src = xs_json_loads("{\"privacy\":\"public\","
  907. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  908. /* some apps take the note from the source object */
  909. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  910. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  911. const xs_str *cw = xs_dict_get(snac.config, "cw");
  912. src = xs_dict_set(src, "sensitive",
  913. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  914. src = xs_dict_set(src, "bot", xs_dict_get(snac.config, "bot"));
  915. xs *avatar = NULL;
  916. const char *av = xs_dict_get(snac.config, "avatar");
  917. if (xs_is_null(av) || *av == '\0')
  918. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  919. else
  920. avatar = xs_dup(av);
  921. acct = xs_dict_append(acct, "avatar", avatar);
  922. acct = xs_dict_append(acct, "avatar_static", avatar);
  923. xs *header = NULL;
  924. const char *hd = xs_dict_get(snac.config, "header");
  925. if (!xs_is_null(hd))
  926. header = xs_dup(hd);
  927. else
  928. header = xs_fmt("%s/header.png", srv_baseurl);
  929. acct = xs_dict_append(acct, "header", header);
  930. acct = xs_dict_append(acct, "header_static", header);
  931. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  932. if (xs_type(metadata) == XSTYPE_DICT) {
  933. xs *fields = xs_list_new();
  934. const xs_str *k;
  935. const xs_str *v;
  936. xs_dict *val_links = snac.links;
  937. if (xs_is_null(val_links))
  938. val_links = xs_stock(XSTYPE_DICT);
  939. int c = 0;
  940. while (xs_dict_next(metadata, &k, &v, &c)) {
  941. xs *val_date = NULL;
  942. const xs_number *verified_time = xs_dict_get(val_links, v);
  943. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  944. time_t t = xs_number_get(verified_time);
  945. if (t > 0)
  946. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  947. }
  948. xs *d = xs_dict_new();
  949. d = xs_dict_append(d, "name", k);
  950. d = xs_dict_append(d, "value", v);
  951. d = xs_dict_append(d, "verified_at",
  952. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  953. fields = xs_list_append(fields, d);
  954. }
  955. acct = xs_dict_set(acct, "fields", fields);
  956. /* some apps take the fields from the source object */
  957. src = xs_dict_set(src, "fields", fields);
  958. }
  959. acct = xs_dict_append(acct, "source", src);
  960. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  961. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  962. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  963. *body = xs_json_dumps(acct, 4);
  964. *ctype = "application/json";
  965. *status = HTTP_STATUS_OK;
  966. }
  967. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  968. char **body, int *b_size, char **ctype)
  969. {
  970. (void)b_size;
  971. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  972. return 0;
  973. int status = HTTP_STATUS_NOT_FOUND;
  974. const xs_dict *args = xs_dict_get(req, "q_vars");
  975. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  976. snac snac1 = {0};
  977. int logged_in = process_auth_token(&snac1, req);
  978. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  979. if (logged_in) {
  980. credentials_get(body, ctype, &status, snac1);
  981. }
  982. else {
  983. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  984. }
  985. }
  986. else
  987. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  988. /* find if an account is followed, blocked, etc. */
  989. /* the account to get relationships about is in args "id[]" */
  990. if (logged_in) {
  991. xs *res = xs_list_new();
  992. const char *md5 = xs_dict_get(args, "id[]");
  993. if (xs_is_null(md5))
  994. md5 = xs_dict_get(args, "id");
  995. if (!xs_is_null(md5)) {
  996. if (xs_type(md5) == XSTYPE_LIST)
  997. md5 = xs_list_get(md5, 0);
  998. xs *rel = mastoapi_relationship(&snac1, md5);
  999. if (rel != NULL)
  1000. res = xs_list_append(res, rel);
  1001. }
  1002. *body = xs_json_dumps(res, 4);
  1003. *ctype = "application/json";
  1004. status = HTTP_STATUS_OK;
  1005. }
  1006. else
  1007. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1008. }
  1009. else
  1010. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1011. /* lookup an account */
  1012. const char *acct = xs_dict_get(args, "acct");
  1013. if (!xs_is_null(acct)) {
  1014. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1015. xs *l = xs_split_n(s, "@", 1);
  1016. const char *uid = xs_list_get(l, 0);
  1017. const char *host = xs_list_get(l, 1);
  1018. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1019. snac user;
  1020. if (user_open(&user, uid)) {
  1021. xs *actor = msg_actor(&user);
  1022. xs *macct = mastoapi_account(actor);
  1023. *body = xs_json_dumps(macct, 4);
  1024. *ctype = "application/json";
  1025. status = HTTP_STATUS_OK;
  1026. user_free(&user);
  1027. }
  1028. }
  1029. }
  1030. }
  1031. else
  1032. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1033. /* account-related information */
  1034. xs *l = xs_split(cmd, "/");
  1035. const char *uid = xs_list_get(l, 3);
  1036. const char *opt = xs_list_get(l, 4);
  1037. if (uid != NULL) {
  1038. snac snac2;
  1039. xs *out = NULL;
  1040. xs *actor = NULL;
  1041. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1042. /* search for accounts starting with q */
  1043. const char *aq = xs_dict_get(args, "q");
  1044. if (!xs_is_null(aq)) {
  1045. xs *q = xs_tolower_i(xs_dup(aq));
  1046. out = xs_list_new();
  1047. xs *wing = following_list(&snac1);
  1048. xs *wers = follower_list(&snac1);
  1049. xs *ulst = user_list();
  1050. xs_list *p;
  1051. const xs_str *v;
  1052. xs_set seen;
  1053. xs_set_init(&seen);
  1054. /* user relations */
  1055. xs_list *lsts[] = { wing, wers, NULL };
  1056. int n;
  1057. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1058. while (xs_list_iter(&p, &v)) {
  1059. /* already seen? skip */
  1060. if (xs_set_add(&seen, v) == 0)
  1061. continue;
  1062. xs *actor = NULL;
  1063. if (valid_status(object_get(v, &actor))) {
  1064. const char *uname = xs_dict_get(actor, "preferredUsername");
  1065. if (!xs_is_null(uname)) {
  1066. xs *luname = xs_tolower_i(xs_dup(uname));
  1067. if (xs_startswith(luname, q)) {
  1068. xs *acct = mastoapi_account(actor);
  1069. out = xs_list_append(out, acct);
  1070. }
  1071. }
  1072. }
  1073. }
  1074. }
  1075. /* local users */
  1076. p = ulst;
  1077. while (xs_list_iter(&p, &v)) {
  1078. snac user;
  1079. /* skip this same user */
  1080. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1081. continue;
  1082. /* skip if the uid does not start with the query */
  1083. xs *v2 = xs_tolower_i(xs_dup(v));
  1084. if (!xs_startswith(v2, q))
  1085. continue;
  1086. if (user_open(&user, v)) {
  1087. /* if it's not already seen, add it */
  1088. if (xs_set_add(&seen, user.actor) == 1) {
  1089. xs *actor = msg_actor(&user);
  1090. xs *acct = mastoapi_account(actor);
  1091. out = xs_list_append(out, acct);
  1092. }
  1093. user_free(&user);
  1094. }
  1095. }
  1096. xs_set_free(&seen);
  1097. }
  1098. }
  1099. else
  1100. /* is it a local user? */
  1101. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1102. if (opt == NULL) {
  1103. /* account information */
  1104. actor = msg_actor(&snac2);
  1105. out = mastoapi_account(actor);
  1106. }
  1107. else
  1108. if (strcmp(opt, "statuses") == 0) { /** **/
  1109. /* the public list of posts of a user */
  1110. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1111. xs_list *p = timeline;
  1112. const xs_str *v;
  1113. out = xs_list_new();
  1114. while (xs_list_iter(&p, &v)) {
  1115. xs *msg = NULL;
  1116. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1117. /* add only posts by the author */
  1118. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1119. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1120. xs *st = mastoapi_status(&snac2, msg);
  1121. if (st)
  1122. out = xs_list_append(out, st);
  1123. }
  1124. }
  1125. }
  1126. }
  1127. else
  1128. if (strcmp(opt, "featured_tags") == 0) {
  1129. /* snac doesn't have features tags, yet? */
  1130. /* implement empty response so apps like Tokodon don't show an error */
  1131. *body = xs_dup("[]");
  1132. *ctype = "application/json";
  1133. status = HTTP_STATUS_OK;
  1134. }
  1135. user_free(&snac2);
  1136. }
  1137. else {
  1138. /* try the uid as the md5 of a possibly loaded actor */
  1139. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1140. if (opt == NULL) {
  1141. /* account information */
  1142. out = mastoapi_account(actor);
  1143. }
  1144. else
  1145. if (strcmp(opt, "statuses") == 0) {
  1146. /* we don't serve statuses of others; return the empty list */
  1147. out = xs_list_new();
  1148. }
  1149. if (strcmp(opt, "featured_tags") == 0) {
  1150. /* snac doesn't have features tags, yet? */
  1151. /* implement empty response so apps like Tokodon don't show an error */
  1152. *body = xs_dup("[]");
  1153. *ctype = "application/json";
  1154. status = HTTP_STATUS_OK;
  1155. }
  1156. }
  1157. }
  1158. if (out != NULL) {
  1159. *body = xs_json_dumps(out, 4);
  1160. *ctype = "application/json";
  1161. status = HTTP_STATUS_OK;
  1162. }
  1163. }
  1164. }
  1165. else
  1166. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1167. /* the private timeline */
  1168. if (logged_in) {
  1169. const char *max_id = xs_dict_get(args, "max_id");
  1170. const char *since_id = xs_dict_get(args, "since_id");
  1171. const char *min_id = xs_dict_get(args, "min_id");
  1172. const char *limit_s = xs_dict_get(args, "limit");
  1173. int limit = 0;
  1174. int cnt = 0;
  1175. if (!xs_is_null(limit_s))
  1176. limit = atoi(limit_s);
  1177. if (limit == 0)
  1178. limit = 20;
  1179. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1180. xs *out = xs_list_new();
  1181. xs_list *p = timeline;
  1182. const xs_str *v;
  1183. while (xs_list_iter(&p, &v) && cnt < limit) {
  1184. xs *msg = NULL;
  1185. /* only return entries older that max_id */
  1186. if (max_id) {
  1187. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1188. max_id = NULL;
  1189. continue;
  1190. }
  1191. /* only returns entries newer than since_id */
  1192. if (since_id) {
  1193. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1194. break;
  1195. }
  1196. /* only returns entries newer than min_id */
  1197. /* what does really "Return results immediately newer than ID" mean? */
  1198. if (min_id) {
  1199. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1200. break;
  1201. }
  1202. /* get the entry */
  1203. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1204. continue;
  1205. /* discard non-Notes */
  1206. const char *id = xs_dict_get(msg, "id");
  1207. const char *type = xs_dict_get(msg, "type");
  1208. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1209. continue;
  1210. const char *from = NULL;
  1211. if (strcmp(type, "Page") == 0)
  1212. from = xs_dict_get(msg, "audience");
  1213. if (from == NULL)
  1214. from = get_atto(msg);
  1215. if (from == NULL)
  1216. continue;
  1217. /* is this message from a person we don't follow? */
  1218. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1219. /* discard if it was not boosted */
  1220. xs *idx = object_announces(id);
  1221. if (xs_list_len(idx) == 0)
  1222. continue;
  1223. }
  1224. /* discard notes from muted morons */
  1225. if (is_muted(&snac1, from))
  1226. continue;
  1227. /* discard hidden notes */
  1228. if (is_hidden(&snac1, id))
  1229. continue;
  1230. /* if it has a name and it's not a Page or a Video,
  1231. it's a poll vote, so discard it */
  1232. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
  1233. continue;
  1234. /* convert the Note into a Mastodon status */
  1235. xs *st = mastoapi_status(&snac1, msg);
  1236. if (st != NULL)
  1237. out = xs_list_append(out, st);
  1238. cnt++;
  1239. }
  1240. *body = xs_json_dumps(out, 4);
  1241. *ctype = "application/json";
  1242. status = HTTP_STATUS_OK;
  1243. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1244. }
  1245. else {
  1246. status = HTTP_STATUS_UNAUTHORIZED;
  1247. }
  1248. }
  1249. else
  1250. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1251. /* the instance public timeline (public timelines for all users) */
  1252. const char *limit_s = xs_dict_get(args, "limit");
  1253. int limit = 0;
  1254. int cnt = 0;
  1255. if (!xs_is_null(limit_s))
  1256. limit = atoi(limit_s);
  1257. if (limit == 0)
  1258. limit = 20;
  1259. xs *timeline = timeline_instance_list(0, limit);
  1260. xs *out = xs_list_new();
  1261. xs_list *p = timeline;
  1262. const xs_str *md5;
  1263. snac *user = NULL;
  1264. if (logged_in)
  1265. user = &snac1;
  1266. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1267. xs *msg = NULL;
  1268. /* get the entry */
  1269. if (!valid_status(object_get_by_md5(md5, &msg)))
  1270. continue;
  1271. /* discard non-Notes */
  1272. const char *type = xs_dict_get(msg, "type");
  1273. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1274. continue;
  1275. /* discard messages from private users */
  1276. if (is_msg_from_private_user(msg))
  1277. continue;
  1278. /* convert the Note into a Mastodon status */
  1279. xs *st = mastoapi_status(user, msg);
  1280. if (st != NULL) {
  1281. out = xs_list_append(out, st);
  1282. cnt++;
  1283. }
  1284. }
  1285. *body = xs_json_dumps(out, 4);
  1286. *ctype = "application/json";
  1287. status = HTTP_STATUS_OK;
  1288. }
  1289. else
  1290. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1291. const char *limit_s = xs_dict_get(args, "limit");
  1292. int limit = 0;
  1293. int cnt = 0;
  1294. if (!xs_is_null(limit_s))
  1295. limit = atoi(limit_s);
  1296. if (limit == 0)
  1297. limit = 20;
  1298. /* get the tag */
  1299. xs *l = xs_split(cmd, "/");
  1300. const char *tag = xs_list_get(l, -1);
  1301. xs *timeline = tag_search(tag, 0, limit);
  1302. xs *out = xs_list_new();
  1303. xs_list *p = timeline;
  1304. const xs_str *md5;
  1305. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1306. xs *msg = NULL;
  1307. /* get the entry */
  1308. if (!valid_status(object_get_by_md5(md5, &msg)))
  1309. continue;
  1310. /* skip non-public messages */
  1311. if (!is_msg_public(msg))
  1312. continue;
  1313. /* discard messages from private users */
  1314. if (is_msg_from_private_user(msg))
  1315. continue;
  1316. /* convert the Note into a Mastodon status */
  1317. xs *st = mastoapi_status(NULL, msg);
  1318. if (st != NULL) {
  1319. out = xs_list_append(out, st);
  1320. cnt++;
  1321. }
  1322. }
  1323. *body = xs_json_dumps(out, 4);
  1324. *ctype = "application/json";
  1325. status = HTTP_STATUS_OK;
  1326. }
  1327. else
  1328. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1329. /* get the list id */
  1330. if (logged_in) {
  1331. xs *l = xs_split(cmd, "/");
  1332. const char *list = xs_list_get(l, -1);
  1333. xs *timeline = list_timeline(&snac1, list, 0, 2048);
  1334. xs *out = xs_list_new();
  1335. int c = 0;
  1336. const char *md5;
  1337. while (xs_list_next(timeline, &md5, &c)) {
  1338. xs *msg = NULL;
  1339. /* get the entry */
  1340. if (!valid_status(timeline_get_by_md5(&snac1, md5, &msg)))
  1341. continue;
  1342. /* discard non-Notes */
  1343. const char *id = xs_dict_get(msg, "id");
  1344. const char *type = xs_dict_get(msg, "type");
  1345. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1346. continue;
  1347. const char *from = NULL;
  1348. if (strcmp(type, "Page") == 0)
  1349. from = xs_dict_get(msg, "audience");
  1350. if (from == NULL)
  1351. from = get_atto(msg);
  1352. if (from == NULL)
  1353. continue;
  1354. /* is this message from a person we don't follow? */
  1355. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1356. /* discard if it was not boosted */
  1357. xs *idx = object_announces(id);
  1358. if (xs_list_len(idx) == 0)
  1359. continue;
  1360. }
  1361. /* discard notes from muted morons */
  1362. if (is_muted(&snac1, from))
  1363. continue;
  1364. /* discard hidden notes */
  1365. if (is_hidden(&snac1, id))
  1366. continue;
  1367. /* if it has a name and it's not a Page or a Video,
  1368. it's a poll vote, so discard it */
  1369. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
  1370. continue;
  1371. /* convert the Note into a Mastodon status */
  1372. xs *st = mastoapi_status(&snac1, msg);
  1373. if (st != NULL)
  1374. out = xs_list_append(out, st);
  1375. }
  1376. *body = xs_json_dumps(out, 4);
  1377. *ctype = "application/json";
  1378. status = HTTP_STATUS_OK;
  1379. }
  1380. else
  1381. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1382. }
  1383. else
  1384. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1385. /* TBD */
  1386. *body = xs_dup("[]");
  1387. *ctype = "application/json";
  1388. status = HTTP_STATUS_OK;
  1389. }
  1390. else
  1391. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1392. if (logged_in) {
  1393. xs *l = notify_list(&snac1, 0, 64);
  1394. xs *out = xs_list_new();
  1395. xs_list *p = l;
  1396. const xs_dict *v;
  1397. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1398. while (xs_list_iter(&p, &v)) {
  1399. xs *noti = notify_get(&snac1, v);
  1400. if (noti == NULL)
  1401. continue;
  1402. const char *type = xs_dict_get(noti, "type");
  1403. const char *utype = xs_dict_get(noti, "utype");
  1404. const char *objid = xs_dict_get(noti, "objid");
  1405. xs *actor = NULL;
  1406. xs *entry = NULL;
  1407. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1408. continue;
  1409. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1410. continue;
  1411. if (is_hidden(&snac1, objid))
  1412. continue;
  1413. /* convert the type */
  1414. if (strcmp(type, "Like") == 0)
  1415. type = "favourite";
  1416. else
  1417. if (strcmp(type, "Announce") == 0)
  1418. type = "reblog";
  1419. else
  1420. if (strcmp(type, "Follow") == 0)
  1421. type = "follow";
  1422. else
  1423. if (strcmp(type, "Create") == 0)
  1424. type = "mention";
  1425. else
  1426. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1427. type = "poll";
  1428. else
  1429. continue;
  1430. /* excluded type? */
  1431. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1432. continue;
  1433. xs *mn = xs_dict_new();
  1434. mn = xs_dict_append(mn, "type", type);
  1435. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1436. mn = xs_dict_append(mn, "id", id);
  1437. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1438. xs *acct = mastoapi_account(actor);
  1439. mn = xs_dict_append(mn, "account", acct);
  1440. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1441. xs *st = mastoapi_status(&snac1, entry);
  1442. if (st)
  1443. mn = xs_dict_append(mn, "status", st);
  1444. }
  1445. out = xs_list_append(out, mn);
  1446. }
  1447. *body = xs_json_dumps(out, 4);
  1448. *ctype = "application/json";
  1449. status = HTTP_STATUS_OK;
  1450. }
  1451. else
  1452. status = HTTP_STATUS_UNAUTHORIZED;
  1453. }
  1454. else
  1455. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1456. /* snac will never have filters */
  1457. *body = xs_dup("[]");
  1458. *ctype = "application/json";
  1459. status = HTTP_STATUS_OK;
  1460. }
  1461. else
  1462. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1463. /* snac will never have filters
  1464. * but still, without a v2 endpoint a short delay is introduced
  1465. * in some apps */
  1466. *body = xs_dup("[]");
  1467. *ctype = "application/json";
  1468. status = HTTP_STATUS_OK;
  1469. }
  1470. else
  1471. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1472. /* snac will never support a list of favourites */
  1473. *body = xs_dup("[]");
  1474. *ctype = "application/json";
  1475. status = HTTP_STATUS_OK;
  1476. }
  1477. else
  1478. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1479. /* snac does not support bookmarks */
  1480. *body = xs_dup("[]");
  1481. *ctype = "application/json";
  1482. status = HTTP_STATUS_OK;
  1483. }
  1484. else
  1485. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1486. if (logged_in) {
  1487. xs *lol = list_maint(&snac1, NULL, 0);
  1488. xs *l = xs_list_new();
  1489. int c = 0;
  1490. const xs_list *li;
  1491. while (xs_list_next(lol, &li, &c)) {
  1492. xs *d = xs_dict_new();
  1493. d = xs_dict_append(d, "id", xs_list_get(li, 0));
  1494. d = xs_dict_append(d, "title", xs_list_get(li, 1));
  1495. d = xs_dict_append(d, "replies_policy", "list");
  1496. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1497. l = xs_list_append(l, d);
  1498. }
  1499. *body = xs_json_dumps(l, 4);
  1500. *ctype = "application/json";
  1501. status = HTTP_STATUS_OK;
  1502. }
  1503. }
  1504. else
  1505. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1506. if (logged_in) {
  1507. xs *l = xs_split(cmd, "/");
  1508. const char *p = xs_list_get(l, -1);
  1509. if (p) {
  1510. if (strcmp(p, "accounts") == 0) {
  1511. p = xs_list_get(l, -2);
  1512. if (p && xs_is_hex(p)) {
  1513. xs *actors = list_content(&snac1, p, NULL, 0);
  1514. xs *out = xs_list_new();
  1515. int c = 0;
  1516. const char *v;
  1517. while (xs_list_next(actors, &v, &c)) {
  1518. xs *actor = NULL;
  1519. if (valid_status(object_get_by_md5(v, &actor))) {
  1520. xs *acct = mastoapi_account(actor);
  1521. out = xs_list_append(out, acct);
  1522. }
  1523. }
  1524. *body = xs_json_dumps(out, 4);
  1525. *ctype = "application/json";
  1526. status = HTTP_STATUS_OK;
  1527. }
  1528. }
  1529. else
  1530. if (xs_is_hex(p)) {
  1531. xs *out = xs_list_new();
  1532. xs *lol = list_maint(&snac1, NULL, 0);
  1533. int c = 0;
  1534. const xs_list *v;
  1535. while (xs_list_next(lol, &v, &c)) {
  1536. const char *id = xs_list_get(v, 0);
  1537. if (id && strcmp(id, p) == 0) {
  1538. xs *d = xs_dict_new();
  1539. d = xs_dict_append(d, "id", p);
  1540. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1541. d = xs_dict_append(d, "replies_policy", "list");
  1542. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1543. out = xs_list_append(out, d);
  1544. break;
  1545. }
  1546. }
  1547. *body = xs_json_dumps(out, 4);
  1548. *ctype = "application/json";
  1549. status = HTTP_STATUS_OK;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. else
  1555. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1556. /* snac does not schedule notes */
  1557. *body = xs_dup("[]");
  1558. *ctype = "application/json";
  1559. status = HTTP_STATUS_OK;
  1560. }
  1561. else
  1562. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1563. /* snac does not support optional follow confirmations */
  1564. *body = xs_dup("[]");
  1565. *ctype = "application/json";
  1566. status = HTTP_STATUS_OK;
  1567. }
  1568. else
  1569. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1570. /* snac has no announcements (yet?) */
  1571. *body = xs_dup("[]");
  1572. *ctype = "application/json";
  1573. status = HTTP_STATUS_OK;
  1574. }
  1575. else
  1576. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1577. /* are you kidding me? */
  1578. *body = xs_dup("[]");
  1579. *ctype = "application/json";
  1580. status = HTTP_STATUS_OK;
  1581. }
  1582. else
  1583. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1584. /* returns an instance object */
  1585. xs *ins = xs_dict_new();
  1586. const char *host = xs_dict_get(srv_config, "host");
  1587. const char *title = xs_dict_get(srv_config, "title");
  1588. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1589. ins = xs_dict_append(ins, "uri", host);
  1590. ins = xs_dict_append(ins, "domain", host);
  1591. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1592. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1593. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1594. ins = xs_dict_append(ins, "description", host);
  1595. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1596. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1597. ins = xs_dict_append(ins, "thumbnail", susie);
  1598. const char *v = xs_dict_get(srv_config, "admin_email");
  1599. if (xs_is_null(v) || *v == '\0')
  1600. v = "admin@localhost";
  1601. ins = xs_dict_append(ins, "email", v);
  1602. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1603. xs *l1 = xs_list_append(xs_list_new(), "en");
  1604. ins = xs_dict_append(ins, "languages", l1);
  1605. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1606. xs *urls = xs_dict_new();
  1607. urls = xs_dict_append(urls, "streaming_api", wss);
  1608. ins = xs_dict_append(ins, "urls", urls);
  1609. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1610. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1611. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1612. ins = xs_dict_append(ins, "stats", d2);
  1613. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1614. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1615. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1616. xs *cfg = xs_dict_new();
  1617. {
  1618. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1619. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1620. cfg = xs_dict_append(cfg, "statuses", d11);
  1621. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1622. cfg = xs_dict_append(cfg, "accounts", d12);
  1623. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1624. "\"image_size_limit\":16777216,"
  1625. "\"video_frame_rate_limit\":120,"
  1626. "\"video_matrix_limit\":8294400,"
  1627. "\"video_size_limit\":103809024}"
  1628. );
  1629. {
  1630. /* get the supported mime types from the internal list */
  1631. const char **p = xs_mime_types;
  1632. xs_set mtypes;
  1633. xs_set_init(&mtypes);
  1634. while (*p) {
  1635. const char *type = p[1];
  1636. if (xs_startswith(type, "image/") ||
  1637. xs_startswith(type, "video/") ||
  1638. xs_startswith(type, "audio/"))
  1639. xs_set_add(&mtypes, type);
  1640. p += 2;
  1641. }
  1642. xs *l = xs_set_result(&mtypes);
  1643. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1644. }
  1645. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1646. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1647. "\"max_expiration\":2629746,"
  1648. "\"max_options\":8,\"min_expiration\":300}");
  1649. cfg = xs_dict_append(cfg, "polls", d14);
  1650. }
  1651. ins = xs_dict_append(ins, "configuration", cfg);
  1652. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1653. if (!xs_is_null(admin_account) && *admin_account) {
  1654. snac admin;
  1655. if (user_open(&admin, admin_account)) {
  1656. xs *actor = msg_actor(&admin);
  1657. xs *acct = mastoapi_account(actor);
  1658. ins = xs_dict_append(ins, "contact_account", acct);
  1659. user_free(&admin);
  1660. }
  1661. }
  1662. *body = xs_json_dumps(ins, 4);
  1663. *ctype = "application/json";
  1664. status = HTTP_STATUS_OK;
  1665. }
  1666. else
  1667. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1668. /* information about a status */
  1669. if (logged_in) {
  1670. xs *l = xs_split(cmd, "/");
  1671. const char *id = xs_list_get(l, 3);
  1672. const char *op = xs_list_get(l, 4);
  1673. if (!xs_is_null(id)) {
  1674. xs *msg = NULL;
  1675. xs *out = NULL;
  1676. /* skip the 'fake' part of the id */
  1677. id = MID_TO_MD5(id);
  1678. if (valid_status(object_get_by_md5(id, &msg))) {
  1679. if (op == NULL) {
  1680. if (!is_muted(&snac1, get_atto(msg))) {
  1681. /* return the status itself */
  1682. out = mastoapi_status(&snac1, msg);
  1683. }
  1684. }
  1685. else
  1686. if (strcmp(op, "context") == 0) { /** **/
  1687. /* return ancestors and children */
  1688. xs *anc = xs_list_new();
  1689. xs *des = xs_list_new();
  1690. xs_list *p;
  1691. const xs_str *v;
  1692. char pid[64];
  1693. /* build the [grand]parent list, moving up */
  1694. strncpy(pid, id, sizeof(pid));
  1695. while (object_parent(pid, pid, sizeof(pid))) {
  1696. xs *m2 = NULL;
  1697. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1698. xs *st = mastoapi_status(&snac1, m2);
  1699. if (st)
  1700. anc = xs_list_insert(anc, 0, st);
  1701. }
  1702. else
  1703. break;
  1704. }
  1705. /* build the children list */
  1706. xs *children = object_children(xs_dict_get(msg, "id"));
  1707. p = children;
  1708. while (xs_list_iter(&p, &v)) {
  1709. xs *m2 = NULL;
  1710. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1711. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1712. xs *st = mastoapi_status(&snac1, m2);
  1713. if (st)
  1714. des = xs_list_append(des, st);
  1715. }
  1716. }
  1717. }
  1718. out = xs_dict_new();
  1719. out = xs_dict_append(out, "ancestors", anc);
  1720. out = xs_dict_append(out, "descendants", des);
  1721. }
  1722. else
  1723. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1724. strcmp(op, "favourited_by") == 0) { /** **/
  1725. /* return the list of people who liked or boosted this */
  1726. out = xs_list_new();
  1727. xs *l = NULL;
  1728. if (op[0] == 'r')
  1729. l = object_announces(xs_dict_get(msg, "id"));
  1730. else
  1731. l = object_likes(xs_dict_get(msg, "id"));
  1732. xs_list *p = l;
  1733. const xs_str *v;
  1734. while (xs_list_iter(&p, &v)) {
  1735. xs *actor2 = NULL;
  1736. if (valid_status(object_get_by_md5(v, &actor2))) {
  1737. xs *acct2 = mastoapi_account(actor2);
  1738. out = xs_list_append(out, acct2);
  1739. }
  1740. }
  1741. }
  1742. else
  1743. if (strcmp(op, "source") == 0) { /** **/
  1744. out = xs_dict_new();
  1745. /* get the mastoapi status id */
  1746. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1747. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1748. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1749. }
  1750. }
  1751. else
  1752. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1753. if (out != NULL) {
  1754. *body = xs_json_dumps(out, 4);
  1755. *ctype = "application/json";
  1756. status = HTTP_STATUS_OK;
  1757. }
  1758. }
  1759. }
  1760. else
  1761. status = HTTP_STATUS_UNAUTHORIZED;
  1762. }
  1763. else
  1764. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1765. *body = xs_dup("{}");
  1766. *ctype = "application/json";
  1767. status = HTTP_STATUS_OK;
  1768. }
  1769. else
  1770. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1771. *body = xs_dup("{}");
  1772. *ctype = "application/json";
  1773. status = HTTP_STATUS_OK;
  1774. }
  1775. else
  1776. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1777. *body = xs_dup("[]");
  1778. *ctype = "application/json";
  1779. status = HTTP_STATUS_OK;
  1780. }
  1781. else
  1782. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1783. if (logged_in) {
  1784. const char *q = xs_dict_get(args, "q");
  1785. const char *type = xs_dict_get(args, "type");
  1786. const char *offset = xs_dict_get(args, "offset");
  1787. xs *acl = xs_list_new();
  1788. xs *stl = xs_list_new();
  1789. xs *htl = xs_list_new();
  1790. xs *res = xs_dict_new();
  1791. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1792. /* reply something only for offset 0; otherwise,
  1793. apps like Tusky keep asking again and again */
  1794. if (!xs_is_null(q)) {
  1795. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  1796. /* do a webfinger query */
  1797. char *actor = NULL;
  1798. char *user = NULL;
  1799. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  1800. xs *actor_o = NULL;
  1801. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1802. xs *acct = mastoapi_account(actor_o);
  1803. acl = xs_list_append(acl, acct);
  1804. }
  1805. }
  1806. }
  1807. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  1808. /* search this tag */
  1809. xs *tl = tag_search((char *)q, 0, 1);
  1810. if (xs_list_len(tl)) {
  1811. xs *d = xs_dict_new();
  1812. d = xs_dict_append(d, "name", q);
  1813. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  1814. d = xs_dict_append(d, "url", url);
  1815. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  1816. htl = xs_list_append(htl, d);
  1817. }
  1818. }
  1819. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  1820. int to = 0;
  1821. int cnt = 40;
  1822. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  1823. int c = 0;
  1824. const char *v;
  1825. while (xs_list_next(tl, &v, &c) && --cnt) {
  1826. xs *post = NULL;
  1827. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  1828. continue;
  1829. xs *s = mastoapi_status(&snac1, post);
  1830. if (!xs_is_null(s))
  1831. stl = xs_list_append(stl, s);
  1832. }
  1833. }
  1834. }
  1835. }
  1836. res = xs_dict_append(res, "accounts", acl);
  1837. res = xs_dict_append(res, "statuses", stl);
  1838. res = xs_dict_append(res, "hashtags", htl);
  1839. *body = xs_json_dumps(res, 4);
  1840. *ctype = "application/json";
  1841. status = HTTP_STATUS_OK;
  1842. }
  1843. else
  1844. status = HTTP_STATUS_UNAUTHORIZED;
  1845. }
  1846. /* user cleanup */
  1847. if (logged_in)
  1848. user_free(&snac1);
  1849. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1850. return status;
  1851. }
  1852. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1853. const char *payload, int p_size,
  1854. char **body, int *b_size, char **ctype)
  1855. {
  1856. (void)p_size;
  1857. (void)b_size;
  1858. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1859. return 0;
  1860. int status = HTTP_STATUS_NOT_FOUND;
  1861. xs *args = NULL;
  1862. const char *i_ctype = xs_dict_get(req, "content-type");
  1863. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  1864. if (!xs_is_null(payload))
  1865. args = xs_json_loads(payload);
  1866. }
  1867. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  1868. {
  1869. // Some apps send form data instead of json so we should cater for those
  1870. if (!xs_is_null(payload)) {
  1871. xs *upl = xs_url_dec(payload);
  1872. args = xs_url_vars(upl);
  1873. }
  1874. }
  1875. else
  1876. args = xs_dup(xs_dict_get(req, "p_vars"));
  1877. if (args == NULL)
  1878. return HTTP_STATUS_BAD_REQUEST;
  1879. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1880. snac snac = {0};
  1881. int logged_in = process_auth_token(&snac, req);
  1882. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1883. const char *name = xs_dict_get(args, "client_name");
  1884. const char *ruri = xs_dict_get(args, "redirect_uris");
  1885. const char *scope = xs_dict_get(args, "scope");
  1886. if (xs_type(ruri) == XSTYPE_LIST)
  1887. ruri = xs_dict_get(ruri, 0);
  1888. if (name && ruri) {
  1889. xs *app = xs_dict_new();
  1890. xs *id = xs_replace_i(tid(0), ".", "");
  1891. xs *csec = random_str();
  1892. xs *vkey = random_str();
  1893. xs *cid = NULL;
  1894. /* pick a non-existent random cid */
  1895. for (;;) {
  1896. cid = random_str();
  1897. xs *p_app = app_get(cid);
  1898. if (p_app == NULL)
  1899. break;
  1900. xs_free(cid);
  1901. }
  1902. app = xs_dict_append(app, "name", name);
  1903. app = xs_dict_append(app, "redirect_uri", ruri);
  1904. app = xs_dict_append(app, "client_id", cid);
  1905. app = xs_dict_append(app, "client_secret", csec);
  1906. app = xs_dict_append(app, "vapid_key", vkey);
  1907. app = xs_dict_append(app, "id", id);
  1908. *body = xs_json_dumps(app, 4);
  1909. *ctype = "application/json";
  1910. status = HTTP_STATUS_OK;
  1911. app = xs_dict_append(app, "code", "");
  1912. if (scope)
  1913. app = xs_dict_append(app, "scope", scope);
  1914. app_add(cid, app);
  1915. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1916. }
  1917. }
  1918. else
  1919. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1920. if (logged_in) {
  1921. /* post a new Note */
  1922. const char *content = xs_dict_get(args, "status");
  1923. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1924. const char *visibility = xs_dict_get(args, "visibility");
  1925. const char *summary = xs_dict_get(args, "spoiler_text");
  1926. const char *media_ids = xs_dict_get(args, "media_ids");
  1927. if (xs_is_null(media_ids))
  1928. media_ids = xs_dict_get(args, "media_ids[]");
  1929. if (xs_is_null(media_ids))
  1930. media_ids = xs_dict_get(args, "media_ids");
  1931. if (xs_is_null(visibility))
  1932. visibility = "public";
  1933. xs *attach_list = xs_list_new();
  1934. xs *irt = NULL;
  1935. /* is it a reply? */
  1936. if (mid != NULL) {
  1937. xs *r_msg = NULL;
  1938. const char *md5 = MID_TO_MD5(mid);
  1939. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1940. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1941. }
  1942. /* does it have attachments? */
  1943. if (!xs_is_null(media_ids)) {
  1944. xs *mi = NULL;
  1945. if (xs_type(media_ids) == XSTYPE_LIST)
  1946. mi = xs_dup(media_ids);
  1947. else {
  1948. mi = xs_list_new();
  1949. mi = xs_list_append(mi, media_ids);
  1950. }
  1951. xs_list *p = mi;
  1952. const xs_str *v;
  1953. while (xs_list_iter(&p, &v)) {
  1954. xs *l = xs_list_new();
  1955. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1956. xs *desc = static_get_meta(&snac, v);
  1957. l = xs_list_append(l, url);
  1958. l = xs_list_append(l, desc);
  1959. attach_list = xs_list_append(attach_list, l);
  1960. }
  1961. }
  1962. /* prepare the message */
  1963. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1964. strcmp(visibility, "public") == 0 ? 0 : 1);
  1965. if (!xs_is_null(summary) && *summary) {
  1966. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  1967. msg = xs_dict_set(msg, "summary", summary);
  1968. }
  1969. /* store */
  1970. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1971. /* 'Create' message */
  1972. xs *c_msg = msg_create(&snac, msg);
  1973. enqueue_message(&snac, c_msg);
  1974. timeline_touch(&snac);
  1975. /* convert to a mastodon status as a response code */
  1976. xs *st = mastoapi_status(&snac, msg);
  1977. *body = xs_json_dumps(st, 4);
  1978. *ctype = "application/json";
  1979. status = HTTP_STATUS_OK;
  1980. }
  1981. else
  1982. status = HTTP_STATUS_UNAUTHORIZED;
  1983. }
  1984. else
  1985. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1986. if (logged_in) {
  1987. /* operations on a status */
  1988. xs *l = xs_split(cmd, "/");
  1989. const char *mid = xs_list_get(l, 3);
  1990. const char *op = xs_list_get(l, 4);
  1991. if (!xs_is_null(mid)) {
  1992. xs *msg = NULL;
  1993. xs *out = NULL;
  1994. /* skip the 'fake' part of the id */
  1995. mid = MID_TO_MD5(mid);
  1996. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1997. const char *id = xs_dict_get(msg, "id");
  1998. if (op == NULL) {
  1999. /* no operation (?) */
  2000. }
  2001. else
  2002. if (strcmp(op, "favourite") == 0) { /** **/
  2003. xs *n_msg = msg_admiration(&snac, id, "Like");
  2004. if (n_msg != NULL) {
  2005. enqueue_message(&snac, n_msg);
  2006. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2007. out = mastoapi_status(&snac, msg);
  2008. }
  2009. }
  2010. else
  2011. if (strcmp(op, "unfavourite") == 0) { /** **/
  2012. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2013. if (n_msg != NULL) {
  2014. enqueue_message(&snac, n_msg);
  2015. out = mastoapi_status(&snac, msg);
  2016. }
  2017. }
  2018. else
  2019. if (strcmp(op, "reblog") == 0) { /** **/
  2020. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2021. if (n_msg != NULL) {
  2022. enqueue_message(&snac, n_msg);
  2023. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2024. out = mastoapi_status(&snac, msg);
  2025. }
  2026. }
  2027. else
  2028. if (strcmp(op, "unreblog") == 0) { /** **/
  2029. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2030. if (n_msg != NULL) {
  2031. enqueue_message(&snac, n_msg);
  2032. out = mastoapi_status(&snac, msg);
  2033. }
  2034. }
  2035. else
  2036. if (strcmp(op, "bookmark") == 0) { /** **/
  2037. /* snac does not support bookmarks */
  2038. }
  2039. else
  2040. if (strcmp(op, "unbookmark") == 0) { /** **/
  2041. /* snac does not support bookmarks */
  2042. }
  2043. else
  2044. if (strcmp(op, "pin") == 0) { /** **/
  2045. /* pin this message */
  2046. if (pin(&snac, id))
  2047. out = mastoapi_status(&snac, msg);
  2048. else
  2049. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2050. }
  2051. else
  2052. if (strcmp(op, "unpin") == 0) { /** **/
  2053. /* unpin this message */
  2054. unpin(&snac, id);
  2055. out = mastoapi_status(&snac, msg);
  2056. }
  2057. else
  2058. if (strcmp(op, "mute") == 0) { /** **/
  2059. /* Mastodon's mute is snac's hide */
  2060. }
  2061. else
  2062. if (strcmp(op, "unmute") == 0) { /** **/
  2063. /* Mastodon's unmute is snac's unhide */
  2064. }
  2065. }
  2066. if (out != NULL) {
  2067. *body = xs_json_dumps(out, 4);
  2068. *ctype = "application/json";
  2069. status = HTTP_STATUS_OK;
  2070. }
  2071. }
  2072. }
  2073. else
  2074. status = HTTP_STATUS_UNAUTHORIZED;
  2075. }
  2076. else
  2077. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2078. if (logged_in) {
  2079. notify_clear(&snac);
  2080. timeline_touch(&snac);
  2081. *body = xs_dup("{}");
  2082. *ctype = "application/json";
  2083. status = HTTP_STATUS_OK;
  2084. }
  2085. else
  2086. status = HTTP_STATUS_UNAUTHORIZED;
  2087. }
  2088. else
  2089. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2090. /* I don't know what I'm doing */
  2091. if (logged_in) {
  2092. const char *v;
  2093. xs *wpush = xs_dict_new();
  2094. wpush = xs_dict_append(wpush, "id", "1");
  2095. v = xs_dict_get(args, "data");
  2096. v = xs_dict_get(v, "alerts");
  2097. wpush = xs_dict_append(wpush, "alerts", v);
  2098. v = xs_dict_get(args, "subscription");
  2099. v = xs_dict_get(v, "endpoint");
  2100. wpush = xs_dict_append(wpush, "endpoint", v);
  2101. xs *server_key = random_str();
  2102. wpush = xs_dict_append(wpush, "server_key", server_key);
  2103. *body = xs_json_dumps(wpush, 4);
  2104. *ctype = "application/json";
  2105. status = HTTP_STATUS_OK;
  2106. }
  2107. else
  2108. status = HTTP_STATUS_UNAUTHORIZED;
  2109. }
  2110. else
  2111. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2112. if (logged_in) {
  2113. const xs_list *file = xs_dict_get(args, "file");
  2114. const char *desc = xs_dict_get(args, "description");
  2115. if (xs_is_null(desc))
  2116. desc = "";
  2117. status = HTTP_STATUS_BAD_REQUEST;
  2118. if (xs_type(file) == XSTYPE_LIST) {
  2119. const char *fn = xs_list_get(file, 0);
  2120. if (*fn != '\0') {
  2121. char *ext = strrchr(fn, '.');
  2122. xs *hash = xs_md5_hex(fn, strlen(fn));
  2123. xs *id = xs_fmt("%s%s", hash, ext);
  2124. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2125. int fo = xs_number_get(xs_list_get(file, 1));
  2126. int fs = xs_number_get(xs_list_get(file, 2));
  2127. /* store */
  2128. static_put(&snac, id, payload + fo, fs);
  2129. static_put_meta(&snac, id, desc);
  2130. /* prepare a response */
  2131. xs *rsp = xs_dict_new();
  2132. rsp = xs_dict_append(rsp, "id", id);
  2133. rsp = xs_dict_append(rsp, "type", "image");
  2134. rsp = xs_dict_append(rsp, "url", url);
  2135. rsp = xs_dict_append(rsp, "preview_url", url);
  2136. rsp = xs_dict_append(rsp, "remote_url", url);
  2137. rsp = xs_dict_append(rsp, "description", desc);
  2138. *body = xs_json_dumps(rsp, 4);
  2139. *ctype = "application/json";
  2140. status = HTTP_STATUS_OK;
  2141. }
  2142. }
  2143. }
  2144. else
  2145. status = HTTP_STATUS_UNAUTHORIZED;
  2146. }
  2147. else
  2148. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2149. if (logged_in) {
  2150. /* account-related information */
  2151. xs *l = xs_split(cmd, "/");
  2152. const char *md5 = xs_list_get(l, 3);
  2153. const char *opt = xs_list_get(l, 4);
  2154. xs *rsp = NULL;
  2155. if (!xs_is_null(md5) && *md5) {
  2156. xs *actor_o = NULL;
  2157. if (xs_is_null(opt)) {
  2158. /* ? */
  2159. }
  2160. else
  2161. if (strcmp(opt, "follow") == 0) { /** **/
  2162. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2163. const char *actor = xs_dict_get(actor_o, "id");
  2164. xs *msg = msg_follow(&snac, actor);
  2165. if (msg != NULL) {
  2166. /* reload the actor from the message, in may be different */
  2167. actor = xs_dict_get(msg, "object");
  2168. following_add(&snac, actor, msg);
  2169. enqueue_output_by_actor(&snac, msg, actor, 0);
  2170. rsp = mastoapi_relationship(&snac, md5);
  2171. }
  2172. }
  2173. }
  2174. else
  2175. if (strcmp(opt, "unfollow") == 0) { /** **/
  2176. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2177. const char *actor = xs_dict_get(actor_o, "id");
  2178. /* get the following object */
  2179. xs *object = NULL;
  2180. if (valid_status(following_get(&snac, actor, &object))) {
  2181. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2182. following_del(&snac, actor);
  2183. enqueue_output_by_actor(&snac, msg, actor, 0);
  2184. rsp = mastoapi_relationship(&snac, md5);
  2185. }
  2186. }
  2187. }
  2188. else
  2189. if (strcmp(opt, "block") == 0) { /** **/
  2190. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2191. const char *actor = xs_dict_get(actor_o, "id");
  2192. mute(&snac, actor);
  2193. rsp = mastoapi_relationship(&snac, md5);
  2194. }
  2195. }
  2196. else
  2197. if (strcmp(opt, "unblock") == 0) { /** **/
  2198. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2199. const char *actor = xs_dict_get(actor_o, "id");
  2200. unmute(&snac, actor);
  2201. rsp = mastoapi_relationship(&snac, md5);
  2202. }
  2203. }
  2204. }
  2205. if (rsp != NULL) {
  2206. *body = xs_json_dumps(rsp, 4);
  2207. *ctype = "application/json";
  2208. status = HTTP_STATUS_OK;
  2209. }
  2210. }
  2211. else
  2212. status = HTTP_STATUS_UNAUTHORIZED;
  2213. }
  2214. else
  2215. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2216. if (logged_in) {
  2217. /* operations on a status */
  2218. xs *l = xs_split(cmd, "/");
  2219. const char *mid = xs_list_get(l, 3);
  2220. const char *op = xs_list_get(l, 4);
  2221. if (!xs_is_null(mid)) {
  2222. xs *msg = NULL;
  2223. xs *out = NULL;
  2224. /* skip the 'fake' part of the id */
  2225. mid = MID_TO_MD5(mid);
  2226. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2227. const char *id = xs_dict_get(msg, "id");
  2228. const char *atto = get_atto(msg);
  2229. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2230. if (opts == NULL)
  2231. opts = xs_dict_get(msg, "anyOf");
  2232. if (op == NULL) {
  2233. }
  2234. else
  2235. if (strcmp(op, "votes") == 0) {
  2236. const xs_list *choices = xs_dict_get(args, "choices[]");
  2237. if (xs_is_null(choices))
  2238. choices = xs_dict_get(args, "choices");
  2239. if (xs_type(choices) == XSTYPE_LIST) {
  2240. const xs_str *v;
  2241. int c = 0;
  2242. while (xs_list_next(choices, &v, &c)) {
  2243. int io = atoi(v);
  2244. const xs_dict *o = xs_list_get(opts, io);
  2245. if (o) {
  2246. const char *name = xs_dict_get(o, "name");
  2247. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2248. msg = xs_dict_append(msg, "name", name);
  2249. xs *c_msg = msg_create(&snac, msg);
  2250. enqueue_message(&snac, c_msg);
  2251. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2252. }
  2253. }
  2254. out = mastoapi_poll(&snac, msg);
  2255. }
  2256. }
  2257. }
  2258. if (out != NULL) {
  2259. *body = xs_json_dumps(out, 4);
  2260. *ctype = "application/json";
  2261. status = HTTP_STATUS_OK;
  2262. }
  2263. }
  2264. }
  2265. else
  2266. status = HTTP_STATUS_UNAUTHORIZED;
  2267. }
  2268. else
  2269. if (strcmp(cmd, "/v1/lists") == 0) {
  2270. if (logged_in) {
  2271. const char *title = xs_dict_get(args, "title");
  2272. if (xs_type(title) == XSTYPE_STRING) {
  2273. /* add the list */
  2274. xs *out = xs_dict_new();
  2275. if (xs_type(list_maint(&snac, title, 1)) == XSTYPE_TRUE) {
  2276. out = xs_dict_append(out, "title", title);
  2277. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2278. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2279. status = HTTP_STATUS_OK;
  2280. }
  2281. else {
  2282. out = xs_dict_append(out, "error", "cannot create list");
  2283. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2284. }
  2285. *body = xs_json_dumps(out, 4);
  2286. *ctype = "application/json";
  2287. }
  2288. else
  2289. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2290. }
  2291. }
  2292. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2293. if (logged_in) {
  2294. xs *l = xs_split(cmd, "/");
  2295. const char *op = xs_list_get(l, -1);
  2296. const char *id = xs_list_get(l, -2);
  2297. if (op && id && xs_is_hex(id)) {
  2298. if (strcmp(op, "accounts") == 0) {
  2299. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2300. int c = 0;
  2301. const char *v;
  2302. while (xs_list_next(accts, &v, &c)) {
  2303. list_content(&snac, id, v, 1);
  2304. }
  2305. status = HTTP_STATUS_OK;
  2306. }
  2307. }
  2308. }
  2309. else
  2310. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2311. }
  2312. /* user cleanup */
  2313. if (logged_in)
  2314. user_free(&snac);
  2315. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2316. return status;
  2317. }
  2318. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2319. const char *payload, int p_size,
  2320. char **body, int *b_size, char **ctype)
  2321. {
  2322. (void)p_size;
  2323. (void)body;
  2324. (void)b_size;
  2325. (void)ctype;
  2326. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2327. return 0;
  2328. int status = HTTP_STATUS_NOT_FOUND;
  2329. xs *args = NULL;
  2330. const char *i_ctype = xs_dict_get(req, "content-type");
  2331. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2332. if (!xs_is_null(payload))
  2333. args = xs_json_loads(payload);
  2334. }
  2335. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2336. {
  2337. // Some apps send form data instead of json so we should cater for those
  2338. if (!xs_is_null(payload)) {
  2339. xs *upl = xs_url_dec(payload);
  2340. args = xs_url_vars(upl);
  2341. }
  2342. }
  2343. else
  2344. args = xs_dup(xs_dict_get(req, "p_vars"));
  2345. if (args == NULL)
  2346. return HTTP_STATUS_BAD_REQUEST;
  2347. snac snac = {0};
  2348. int logged_in = process_auth_token(&snac, req);
  2349. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2350. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2351. // pretend we deleted it, since it doesn't exist anyway
  2352. status = HTTP_STATUS_OK;
  2353. }
  2354. else
  2355. if (xs_startswith(cmd, "/v1/lists/")) {
  2356. if (logged_in) {
  2357. xs *l = xs_split(cmd, "/");
  2358. const char *p = xs_list_get(l, -1);
  2359. if (p) {
  2360. if (strcmp(p, "accounts") == 0) {
  2361. /* delete account from list */
  2362. p = xs_list_get(l, -2);
  2363. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2364. int c = 0;
  2365. const char *v;
  2366. while (xs_list_next(accts, &v, &c)) {
  2367. list_content(&snac, p, v, 2);
  2368. }
  2369. }
  2370. else {
  2371. /* delete list */
  2372. if (xs_is_hex(p)) {
  2373. list_maint(&snac, p, 2);
  2374. }
  2375. }
  2376. }
  2377. status = HTTP_STATUS_OK;
  2378. }
  2379. else
  2380. status = HTTP_STATUS_UNAUTHORIZED;
  2381. }
  2382. /* user cleanup */
  2383. if (logged_in)
  2384. user_free(&snac);
  2385. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2386. return status;
  2387. }
  2388. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2389. const char *payload, int p_size,
  2390. char **body, int *b_size, char **ctype)
  2391. {
  2392. (void)p_size;
  2393. (void)b_size;
  2394. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2395. return 0;
  2396. int status = HTTP_STATUS_NOT_FOUND;
  2397. xs *args = NULL;
  2398. const char *i_ctype = xs_dict_get(req, "content-type");
  2399. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2400. if (!xs_is_null(payload))
  2401. args = xs_json_loads(payload);
  2402. }
  2403. else
  2404. args = xs_dup(xs_dict_get(req, "p_vars"));
  2405. if (args == NULL)
  2406. return HTTP_STATUS_BAD_REQUEST;
  2407. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2408. snac snac = {0};
  2409. int logged_in = process_auth_token(&snac, req);
  2410. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2411. if (logged_in) {
  2412. xs *l = xs_split(cmd, "/");
  2413. const char *stid = xs_list_get(l, 3);
  2414. if (!xs_is_null(stid)) {
  2415. const char *desc = xs_dict_get(args, "description");
  2416. /* set the image metadata */
  2417. static_put_meta(&snac, stid, desc);
  2418. /* prepare a response */
  2419. xs *rsp = xs_dict_new();
  2420. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2421. rsp = xs_dict_append(rsp, "id", stid);
  2422. rsp = xs_dict_append(rsp, "type", "image");
  2423. rsp = xs_dict_append(rsp, "url", url);
  2424. rsp = xs_dict_append(rsp, "preview_url", url);
  2425. rsp = xs_dict_append(rsp, "remote_url", url);
  2426. rsp = xs_dict_append(rsp, "description", desc);
  2427. *body = xs_json_dumps(rsp, 4);
  2428. *ctype = "application/json";
  2429. status = HTTP_STATUS_OK;
  2430. }
  2431. }
  2432. else
  2433. status = HTTP_STATUS_UNAUTHORIZED;
  2434. }
  2435. else
  2436. if (xs_startswith(cmd, "/v1/statuses")) {
  2437. if (logged_in) {
  2438. xs *l = xs_split(cmd, "/");
  2439. const char *mid = xs_list_get(l, 3);
  2440. if (!xs_is_null(mid)) {
  2441. const char *md5 = MID_TO_MD5(mid);
  2442. xs *rsp = NULL;
  2443. xs *msg = NULL;
  2444. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2445. const char *content = xs_dict_get(args, "status");
  2446. xs *atls = xs_list_new();
  2447. xs *f_content = not_really_markdown(content, &atls, NULL);
  2448. /* replace fields with new content */
  2449. msg = xs_dict_set(msg, "sourceContent", content);
  2450. msg = xs_dict_set(msg, "content", f_content);
  2451. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2452. msg = xs_dict_set(msg, "updated", updated);
  2453. /* overwrite object, not updating the indexes */
  2454. object_add_ow(xs_dict_get(msg, "id"), msg);
  2455. /* update message */
  2456. xs *c_msg = msg_update(&snac, msg);
  2457. enqueue_message(&snac, c_msg);
  2458. rsp = mastoapi_status(&snac, msg);
  2459. }
  2460. if (rsp != NULL) {
  2461. *body = xs_json_dumps(rsp, 4);
  2462. *ctype = "application/json";
  2463. status = HTTP_STATUS_OK;
  2464. }
  2465. }
  2466. }
  2467. else
  2468. status = HTTP_STATUS_UNAUTHORIZED;
  2469. }
  2470. /* user cleanup */
  2471. if (logged_in)
  2472. user_free(&snac);
  2473. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2474. return status;
  2475. }
  2476. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2477. /* Store header or avatar */
  2478. {
  2479. if (data != NULL) {
  2480. if (xs_type(data) == XSTYPE_LIST) {
  2481. const char *fn = xs_list_get(data, 0);
  2482. if (fn && *fn) {
  2483. const char *ext = strrchr(fn, '.');
  2484. /* Mona iOS sends JPG file as application/octet-stream with filename "header"
  2485. * Make sure we have a unique file name, otherwise updated images will not be
  2486. * loaded by clients.
  2487. */
  2488. if (ext == NULL || strcmp(fn, key) == 0) {
  2489. fn = random_str();
  2490. ext = ".jpg";
  2491. }
  2492. xs *hash = xs_md5_hex(fn, strlen(fn));
  2493. xs *id = xs_fmt("%s%s", hash, ext);
  2494. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2495. int fo = xs_number_get(xs_list_get(data, 1));
  2496. int fs = xs_number_get(xs_list_get(data, 2));
  2497. /* store */
  2498. static_put(snac, id, payload + fo, fs);
  2499. snac->config = xs_dict_set(snac->config, key, url);
  2500. }
  2501. }
  2502. }
  2503. }
  2504. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2505. const char *payload, int p_size,
  2506. char **body, int *b_size, char **ctype)
  2507. /* Handle profile updates */
  2508. {
  2509. (void)p_size;
  2510. (void)b_size;
  2511. if (!xs_startswith(q_path, "/api/v1/"))
  2512. return 0;
  2513. int status = HTTP_STATUS_NOT_FOUND;
  2514. xs *args = NULL;
  2515. const char *i_ctype = xs_dict_get(req, "content-type");
  2516. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2517. if (!xs_is_null(payload))
  2518. args = xs_json_loads(payload);
  2519. }
  2520. else
  2521. args = xs_dup(xs_dict_get(req, "p_vars"));
  2522. if (args == NULL)
  2523. return HTTP_STATUS_BAD_REQUEST;
  2524. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2525. snac snac = {0};
  2526. int logged_in = process_auth_token(&snac, req);
  2527. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2528. /* Update user profile fields */
  2529. if (logged_in) {
  2530. /*
  2531. xs_str *dump = xs_json_dumps(args, 4);
  2532. printf("%s\n\n", dump);
  2533. */
  2534. int c = 0;
  2535. const xs_str *k;
  2536. const xs_val *v;
  2537. const xs_str *field_name = NULL;
  2538. xs_dict *new_fields = xs_dict_new();
  2539. while (xs_dict_next(args, &k, &v, &c)) {
  2540. if (strcmp(k, "display_name") == 0) {
  2541. if (v != NULL)
  2542. snac.config = xs_dict_set(snac.config, "name", v);
  2543. }
  2544. else
  2545. if (strcmp(k, "note") == 0) {
  2546. if (v != NULL)
  2547. snac.config = xs_dict_set(snac.config, "bio", v);
  2548. }
  2549. else
  2550. if (strcmp(k, "bot") == 0) {
  2551. if (v != NULL)
  2552. snac.config = xs_dict_set(snac.config, "bot",
  2553. strcmp(v, "true") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2554. }
  2555. else
  2556. if (strcmp(k, "source[sensitive]") == 0) {
  2557. if (v != NULL)
  2558. snac.config = xs_dict_set(snac.config, "cw",
  2559. strcmp(v, "true") == 0 ? "open" : "");
  2560. }
  2561. else
  2562. if (strcmp(k, "source[privacy]") == 0) {
  2563. if (v != NULL)
  2564. snac.config = xs_dict_set(snac.config, "private",
  2565. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2566. }
  2567. else
  2568. if (strcmp(k, "header") == 0) {
  2569. persist_image("header", v, payload, &snac);
  2570. }
  2571. else
  2572. if (strcmp(k, "avatar") == 0) {
  2573. persist_image("avatar", v, payload, &snac);
  2574. }
  2575. else
  2576. if (xs_starts_and_ends("fields_attributes", k, "[name]")) {
  2577. field_name = strcmp(v, "") != 0 ? v : NULL;
  2578. }
  2579. else
  2580. if (xs_starts_and_ends("fields_attributes", k, "[value]")) {
  2581. if (field_name != NULL) {
  2582. new_fields = xs_dict_set(new_fields, field_name, v);
  2583. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  2584. }
  2585. }
  2586. }
  2587. /* Persist profile */
  2588. if (user_persist(&snac) == 0)
  2589. credentials_get(body, ctype, &status, snac);
  2590. else
  2591. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  2592. }
  2593. else
  2594. status = HTTP_STATUS_UNAUTHORIZED;
  2595. }
  2596. /* user cleanup */
  2597. if (logged_in)
  2598. user_free(&snac);
  2599. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  2600. return status;
  2601. }
  2602. void mastoapi_purge(void)
  2603. {
  2604. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2605. xs *files = xs_glob(spec, 1, 0);
  2606. xs_list *p = files;
  2607. const xs_str *v;
  2608. time_t mt = time(NULL) - 3600;
  2609. while (xs_list_iter(&p, &v)) {
  2610. xs *cid = xs_replace(v, ".json", "");
  2611. xs *fn = _app_fn(cid);
  2612. if (mtime(fn) < mt) {
  2613. /* get the app */
  2614. xs *app = app_get(cid);
  2615. if (app) {
  2616. /* old apps with no uid are incomplete cruft */
  2617. const char *uid = xs_dict_get(app, "uid");
  2618. if (xs_is_null(uid) || *uid == '\0') {
  2619. unlink(fn);
  2620. srv_debug(2, xs_fmt("purged %s", fn));
  2621. }
  2622. }
  2623. }
  2624. }
  2625. }
  2626. #endif /* #ifndef NO_MASTODON_API */