mastoapi.c 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301
  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. "<meta content=\"width=device-width, initial-scale=1, minimum-scale=1, user-scalable=no\" name=\"viewport\">"
  116. "<style>:root {color-scheme: light dark}</style>\n"
  117. "</head>\n"
  118. "<body><h1>%s OAuth identify</h1>\n"
  119. "<div style=\"background-color: red; color: white\">%s</div>\n"
  120. "<form method=\"post\" action=\"%s:/" "/%s/%s\">\n"
  121. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  122. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  123. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  124. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  125. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  126. "<input type=\"submit\" value=\"OK\">\n"
  127. "</form><p>%s</p></body></html>\n"
  128. "";
  129. int oauth_get_handler(const xs_dict *req, const char *q_path,
  130. char **body, int *b_size, char **ctype)
  131. {
  132. (void)b_size;
  133. if (!xs_startswith(q_path, "/oauth/"))
  134. return 0;
  135. int status = HTTP_STATUS_NOT_FOUND;
  136. const xs_dict *msg = xs_dict_get(req, "q_vars");
  137. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  138. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  139. if (strcmp(cmd, "/authorize") == 0) { /** **/
  140. const char *cid = xs_dict_get(msg, "client_id");
  141. const char *ruri = xs_dict_get(msg, "redirect_uri");
  142. const char *rtype = xs_dict_get(msg, "response_type");
  143. const char *state = xs_dict_get(msg, "state");
  144. status = HTTP_STATUS_BAD_REQUEST;
  145. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  146. xs *app = app_get(cid);
  147. if (app != NULL) {
  148. const char *host = xs_dict_get(srv_config, "host");
  149. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  150. if (xs_is_null(state))
  151. state = "";
  152. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-login",
  153. ruri, cid, state, USER_AGENT);
  154. *ctype = "text/html";
  155. status = HTTP_STATUS_OK;
  156. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  157. }
  158. else
  159. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  160. }
  161. else
  162. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  163. }
  164. else
  165. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  166. const char *host = xs_dict_get(srv_config, "host");
  167. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  168. *body = xs_fmt(login_page, host, host, "", proto, host, "oauth/x-snac-get-token",
  169. "", "", "", USER_AGENT);
  170. *ctype = "text/html";
  171. status = HTTP_STATUS_OK;
  172. }
  173. return status;
  174. }
  175. int oauth_post_handler(const xs_dict *req, const char *q_path,
  176. const char *payload, int p_size,
  177. char **body, int *b_size, char **ctype)
  178. {
  179. (void)p_size;
  180. (void)b_size;
  181. if (!xs_startswith(q_path, "/oauth/"))
  182. return 0;
  183. int status = HTTP_STATUS_NOT_FOUND;
  184. const char *i_ctype = xs_dict_get(req, "content-type");
  185. xs *args = NULL;
  186. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  187. if (!xs_is_null(payload))
  188. args = xs_json_loads(payload);
  189. }
  190. else
  191. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  192. xs *upl = xs_url_dec(payload);
  193. args = xs_url_vars(upl);
  194. }
  195. else
  196. args = xs_dup(xs_dict_get(req, "p_vars"));
  197. if (args == NULL)
  198. return HTTP_STATUS_BAD_REQUEST;
  199. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  200. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  201. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  202. const char *login = xs_dict_get(args, "login");
  203. const char *passwd = xs_dict_get(args, "passwd");
  204. const char *redir = xs_dict_get(args, "redir");
  205. const char *cid = xs_dict_get(args, "cid");
  206. const char *state = xs_dict_get(args, "state");
  207. const char *host = xs_dict_get(srv_config, "host");
  208. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  209. /* by default, generate another login form with an error */
  210. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-login",
  211. redir, cid, state, USER_AGENT);
  212. *ctype = "text/html";
  213. status = HTTP_STATUS_OK;
  214. if (login && passwd && redir && cid) {
  215. snac snac;
  216. if (user_open(&snac, login)) {
  217. /* check the login + password */
  218. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  219. /* success! redirect to the desired uri */
  220. xs *code = random_str();
  221. xs_free(*body);
  222. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  223. *body = xs_dup(code);
  224. }
  225. else {
  226. if (xs_str_in(redir, "?") != -1)
  227. *body = xs_fmt("%s&code=%s", redir, code);
  228. else
  229. *body = xs_fmt("%s?code=%s", redir, code);
  230. status = HTTP_STATUS_SEE_OTHER;
  231. }
  232. /* if there is a state, add it */
  233. if (!xs_is_null(state) && *state) {
  234. *body = xs_str_cat(*body, "&state=");
  235. *body = xs_str_cat(*body, state);
  236. }
  237. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  238. login, *body));
  239. /* assign the login to the app */
  240. xs *app = app_get(cid);
  241. if (app != NULL) {
  242. app = xs_dict_set(app, "uid", login);
  243. app = xs_dict_set(app, "code", code);
  244. app_add(cid, app);
  245. }
  246. else
  247. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  248. }
  249. else
  250. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  251. user_free(&snac);
  252. }
  253. else
  254. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  255. }
  256. else
  257. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  258. }
  259. else
  260. if (strcmp(cmd, "/token") == 0) { /** **/
  261. xs *wrk = NULL;
  262. const char *gtype = xs_dict_get(args, "grant_type");
  263. const char *code = xs_dict_get(args, "code");
  264. const char *cid = xs_dict_get(args, "client_id");
  265. const char *csec = xs_dict_get(args, "client_secret");
  266. const char *ruri = xs_dict_get(args, "redirect_uri");
  267. const char *scope = xs_dict_get(args, "scope");
  268. /* no client_secret? check if it's inside an authorization header
  269. (AndStatus does it this way) */
  270. if (xs_is_null(csec)) {
  271. const char *auhdr = xs_dict_get(req, "authorization");
  272. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  273. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  274. int size;
  275. xs *s2 = xs_base64_dec(s1, &size);
  276. if (!xs_is_null(s2)) {
  277. xs *l1 = xs_split(s2, ":");
  278. if (xs_list_len(l1) == 2) {
  279. wrk = xs_dup(xs_list_get(l1, 1));
  280. csec = wrk;
  281. }
  282. }
  283. }
  284. }
  285. /* no code?
  286. I'm not sure of the impacts of this right now, but Subway Tooter does not
  287. provide a code so one must be generated */
  288. if (xs_is_null(code)){
  289. code = random_str();
  290. }
  291. if (gtype && code && cid && csec && ruri) {
  292. xs *app = app_get(cid);
  293. if (app == NULL) {
  294. status = HTTP_STATUS_UNAUTHORIZED;
  295. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  296. }
  297. else
  298. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  299. status = HTTP_STATUS_UNAUTHORIZED;
  300. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  301. }
  302. else {
  303. xs *rsp = xs_dict_new();
  304. xs *cat = xs_number_new(time(NULL));
  305. xs *tokid = random_str();
  306. rsp = xs_dict_append(rsp, "access_token", tokid);
  307. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  308. rsp = xs_dict_append(rsp, "created_at", cat);
  309. if (!xs_is_null(scope))
  310. rsp = xs_dict_append(rsp, "scope", scope);
  311. *body = xs_json_dumps(rsp, 4);
  312. *ctype = "application/json";
  313. status = HTTP_STATUS_OK;
  314. const char *uid = xs_dict_get(app, "uid");
  315. srv_debug(1, xs_fmt("oauth token: "
  316. "successful login for %s, new token %s", uid, tokid));
  317. xs *token = xs_dict_new();
  318. token = xs_dict_append(token, "token", tokid);
  319. token = xs_dict_append(token, "client_id", cid);
  320. token = xs_dict_append(token, "client_secret", csec);
  321. token = xs_dict_append(token, "uid", uid);
  322. token = xs_dict_append(token, "code", code);
  323. token_add(tokid, token);
  324. }
  325. }
  326. else {
  327. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  328. status = HTTP_STATUS_BAD_REQUEST;
  329. }
  330. }
  331. else
  332. if (strcmp(cmd, "/revoke") == 0) { /** **/
  333. const char *cid = xs_dict_get(args, "client_id");
  334. const char *csec = xs_dict_get(args, "client_secret");
  335. const char *tokid = xs_dict_get(args, "token");
  336. if (cid && csec && tokid) {
  337. xs *token = token_get(tokid);
  338. *body = xs_str_new("{}");
  339. *ctype = "application/json";
  340. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  341. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  342. status = HTTP_STATUS_FORBIDDEN;
  343. }
  344. else {
  345. token_del(tokid);
  346. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  347. status = HTTP_STATUS_OK;
  348. /* also delete the app, as it serves no purpose from now on */
  349. app_del(cid);
  350. }
  351. }
  352. else {
  353. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  354. status = HTTP_STATUS_FORBIDDEN;
  355. }
  356. }
  357. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  358. const char *login = xs_dict_get(args, "login");
  359. const char *passwd = xs_dict_get(args, "passwd");
  360. const char *host = xs_dict_get(srv_config, "host");
  361. const char *proto = xs_dict_get_def(srv_config, "protocol", "https");
  362. /* by default, generate another login form with an error */
  363. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", proto, host, "oauth/x-snac-get-token",
  364. "", "", "", USER_AGENT);
  365. *ctype = "text/html";
  366. status = HTTP_STATUS_OK;
  367. if (login && passwd) {
  368. snac user;
  369. if (user_open(&user, login)) {
  370. /* check the login + password */
  371. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  372. /* success! create a new token */
  373. xs *tokid = random_str();
  374. srv_debug(1, xs_fmt("x-snac-new-token: "
  375. "successful login for %s, new token %s", login, tokid));
  376. xs *token = xs_dict_new();
  377. token = xs_dict_append(token, "token", tokid);
  378. token = xs_dict_append(token, "client_id", "snac-client");
  379. token = xs_dict_append(token, "client_secret", "");
  380. token = xs_dict_append(token, "uid", login);
  381. token = xs_dict_append(token, "code", "");
  382. token_add(tokid, token);
  383. *ctype = "text/plain";
  384. xs_free(*body);
  385. *body = xs_dup(tokid);
  386. }
  387. user_free(&user);
  388. }
  389. }
  390. }
  391. return status;
  392. }
  393. xs_str *mastoapi_id(const xs_dict *msg)
  394. /* returns a somewhat Mastodon-compatible status id */
  395. {
  396. const char *id = xs_dict_get(msg, "id");
  397. xs *md5 = xs_md5_hex(id, strlen(id));
  398. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  399. }
  400. #define MID_TO_MD5(id) (id + 10)
  401. xs_dict *mastoapi_account(const xs_dict *actor)
  402. /* converts an ActivityPub actor to a Mastodon account */
  403. {
  404. const char *id = xs_dict_get(actor, "id");
  405. const char *pub = xs_dict_get(actor, "published");
  406. if (xs_type(id) != XSTYPE_STRING)
  407. return NULL;
  408. const char *prefu = xs_dict_get(actor, "preferredUsername");
  409. const char *display_name = xs_dict_get(actor, "name");
  410. if (xs_is_null(display_name) || *display_name == '\0')
  411. display_name = prefu;
  412. xs_dict *acct = xs_dict_new();
  413. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  414. acct = xs_dict_append(acct, "id", acct_md5);
  415. acct = xs_dict_append(acct, "username", prefu);
  416. acct = xs_dict_append(acct, "display_name", display_name);
  417. acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE));
  418. acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE));
  419. acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE));
  420. acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE));
  421. acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE));
  422. acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST));
  423. {
  424. /* create the acct field as user@host */
  425. xs *l = xs_split(id, "/");
  426. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  427. acct = xs_dict_append(acct, "acct", fquid);
  428. }
  429. if (pub)
  430. acct = xs_dict_append(acct, "created_at", pub);
  431. else {
  432. /* unset created_at crashes Tusky, so lie like a mf */
  433. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  434. acct = xs_dict_append(acct, "created_at", date);
  435. }
  436. xs *last_status_at = xs_str_utctime(0, "%Y-%m-%d");
  437. acct = xs_dict_append(acct, "last_status_at", last_status_at);
  438. const char *note = xs_dict_get(actor, "summary");
  439. if (xs_is_null(note))
  440. note = "";
  441. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  442. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE));
  443. else
  444. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE));
  445. acct = xs_dict_append(acct, "note", note);
  446. acct = xs_dict_append(acct, "url", id);
  447. acct = xs_dict_append(acct, "uri", id);
  448. xs *avatar = NULL;
  449. const xs_dict *av = xs_dict_get(actor, "icon");
  450. if (xs_type(av) == XSTYPE_DICT) {
  451. const char *url = xs_dict_get(av, "url");
  452. if (url != NULL)
  453. avatar = xs_dup(url);
  454. }
  455. if (avatar == NULL)
  456. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  457. acct = xs_dict_append(acct, "avatar", avatar);
  458. acct = xs_dict_append(acct, "avatar_static", avatar);
  459. xs *header = NULL;
  460. const xs_dict *hd = xs_dict_get(actor, "image");
  461. if (xs_type(hd) == XSTYPE_DICT)
  462. header = xs_dup(xs_dict_get(hd, "url"));
  463. if (xs_is_null(header))
  464. header = xs_fmt("%s/header.png", srv_baseurl);
  465. acct = xs_dict_append(acct, "header", header);
  466. acct = xs_dict_append(acct, "header_static", header);
  467. /* emojis */
  468. const xs_list *p;
  469. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  470. xs *eml = xs_list_new();
  471. const xs_dict *v;
  472. int c = 0;
  473. while (xs_list_next(p, &v, &c)) {
  474. const char *type = xs_dict_get(v, "type");
  475. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  476. const char *name = xs_dict_get(v, "name");
  477. const xs_dict *icon = xs_dict_get(v, "icon");
  478. if (!xs_is_null(name) && !xs_is_null(icon)) {
  479. const char *url = xs_dict_get(icon, "url");
  480. if (!xs_is_null(url)) {
  481. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  482. xs *d1 = xs_dict_new();
  483. d1 = xs_dict_append(d1, "shortcode", nm);
  484. d1 = xs_dict_append(d1, "url", url);
  485. d1 = xs_dict_append(d1, "static_url", url);
  486. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  487. eml = xs_list_append(eml, d1);
  488. }
  489. }
  490. }
  491. }
  492. acct = xs_dict_append(acct, "emojis", eml);
  493. }
  494. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  495. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  496. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  497. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  498. xs *fields = xs_list_new();
  499. p = xs_dict_get(actor, "attachment");
  500. const xs_dict *v;
  501. /* dict of validated links */
  502. xs_dict *val_links = NULL;
  503. const xs_dict *metadata = xs_stock(XSTYPE_DICT);
  504. snac user = {0};
  505. if (xs_startswith(id, srv_baseurl)) {
  506. /* if it's a local user, open it and pick its validated links */
  507. if (user_open(&user, prefu)) {
  508. val_links = user.links;
  509. metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
  510. }
  511. }
  512. if (xs_is_null(val_links))
  513. val_links = xs_stock(XSTYPE_DICT);
  514. int c = 0;
  515. while (xs_list_next(p, &v, &c)) {
  516. const char *type = xs_dict_get(v, "type");
  517. const char *name = xs_dict_get(v, "name");
  518. const char *value = xs_dict_get(v, "value");
  519. if (!xs_is_null(type) && !xs_is_null(name) &&
  520. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  521. xs *val_date = NULL;
  522. const char *url = xs_dict_get(metadata, name);
  523. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  524. const xs_number *verified_time = xs_dict_get(val_links, url);
  525. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  526. time_t t = xs_number_get(verified_time);
  527. if (t > 0)
  528. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  529. }
  530. }
  531. xs *d = xs_dict_new();
  532. d = xs_dict_append(d, "name", name);
  533. d = xs_dict_append(d, "value", value);
  534. d = xs_dict_append(d, "verified_at",
  535. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  536. val_date : xs_stock(XSTYPE_NULL));
  537. fields = xs_list_append(fields, d);
  538. }
  539. }
  540. user_free(&user);
  541. acct = xs_dict_append(acct, "fields", fields);
  542. return acct;
  543. }
  544. xs_str *mastoapi_date(const char *date)
  545. /* converts an ISO 8601 date to whatever format Mastodon uses */
  546. {
  547. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  548. s = xs_str_cat(s, ".000Z");
  549. return s;
  550. }
  551. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  552. /* creates a mastoapi Poll object */
  553. {
  554. xs_dict *poll = xs_dict_new();
  555. xs *mid = mastoapi_id(msg);
  556. const xs_list *opts = NULL;
  557. const xs_val *v;
  558. int num_votes = 0;
  559. xs *options = xs_list_new();
  560. poll = xs_dict_append(poll, "id", mid);
  561. const char *date = xs_dict_get(msg, "endTime");
  562. if (date == NULL)
  563. date = xs_dict_get(msg, "closed");
  564. if (date == NULL)
  565. return NULL;
  566. xs *fd = mastoapi_date(date);
  567. poll = xs_dict_append(poll, "expires_at", fd);
  568. date = xs_dict_get(msg, "closed");
  569. time_t t = 0;
  570. if (date != NULL)
  571. t = xs_parse_iso_date(date, 0);
  572. poll = xs_dict_append(poll, "expired",
  573. t < time(NULL) ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  574. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  575. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE));
  576. else {
  577. opts = xs_dict_get(msg, "anyOf");
  578. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE));
  579. }
  580. int c = 0;
  581. while (xs_list_next(opts, &v, &c)) {
  582. const char *title = xs_dict_get(v, "name");
  583. const char *replies = xs_dict_get(v, "replies");
  584. if (title && replies) {
  585. const char *votes_count = xs_dict_get(replies, "totalItems");
  586. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  587. xs *d = xs_dict_new();
  588. d = xs_dict_append(d, "title", title);
  589. d = xs_dict_append(d, "votes_count", votes_count);
  590. options = xs_list_append(options, d);
  591. num_votes += xs_number_get(votes_count);
  592. }
  593. }
  594. }
  595. poll = xs_dict_append(poll, "options", options);
  596. xs *vc = xs_number_new(num_votes);
  597. poll = xs_dict_append(poll, "votes_count", vc);
  598. poll = xs_dict_append(poll, "voted",
  599. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  600. xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  601. return poll;
  602. }
  603. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  604. /* converts an ActivityPub note to a Mastodon status */
  605. {
  606. xs *actor = NULL;
  607. actor_get_refresh(snac, get_atto(msg), &actor);
  608. /* if the author is not here, discard */
  609. if (actor == NULL)
  610. return NULL;
  611. const char *type = xs_dict_get(msg, "type");
  612. const char *id = xs_dict_get(msg, "id");
  613. /* fail if it's not a valid actor */
  614. if (xs_is_null(type) || xs_is_null(id))
  615. return NULL;
  616. xs *acct = mastoapi_account(actor);
  617. if (acct == NULL)
  618. return NULL;
  619. xs *idx = NULL;
  620. xs *ixc = NULL;
  621. const char *tmp;
  622. xs *mid = mastoapi_id(msg);
  623. xs_dict *st = xs_dict_new();
  624. st = xs_dict_append(st, "id", mid);
  625. st = xs_dict_append(st, "uri", id);
  626. st = xs_dict_append(st, "url", id);
  627. st = xs_dict_append(st, "account", acct);
  628. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  629. st = xs_dict_append(st, "created_at", fd);
  630. {
  631. const char *content = xs_dict_get(msg, "content");
  632. const char *name = xs_dict_get(msg, "name");
  633. xs *s1 = NULL;
  634. if (name && content)
  635. s1 = xs_fmt("%s<br><br>%s", name, content);
  636. else
  637. if (name)
  638. s1 = xs_dup(name);
  639. else
  640. if (content)
  641. s1 = xs_dup(content);
  642. else
  643. s1 = xs_str_new(NULL);
  644. st = xs_dict_append(st, "content", s1);
  645. }
  646. st = xs_dict_append(st, "visibility",
  647. is_msg_public(msg) ? "public" : "private");
  648. tmp = xs_dict_get(msg, "sensitive");
  649. if (xs_is_null(tmp))
  650. tmp = xs_stock(XSTYPE_FALSE);
  651. st = xs_dict_append(st, "sensitive", tmp);
  652. tmp = xs_dict_get(msg, "summary");
  653. if (xs_is_null(tmp))
  654. tmp = "";
  655. st = xs_dict_append(st, "spoiler_text", tmp);
  656. /* create the list of attachments */
  657. xs *attach = get_attachments(msg);
  658. {
  659. xs_list *p = attach;
  660. const xs_dict *v;
  661. xs *matt = xs_list_new();
  662. while (xs_list_iter(&p, &v)) {
  663. const char *type = xs_dict_get(v, "type");
  664. const char *href = xs_dict_get(v, "href");
  665. const char *name = xs_dict_get(v, "name");
  666. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  667. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  668. xs *d = xs_dict_new();
  669. d = xs_dict_append(d, "id", matteid);
  670. d = xs_dict_append(d, "url", href);
  671. d = xs_dict_append(d, "preview_url", href);
  672. d = xs_dict_append(d, "remote_url", href);
  673. d = xs_dict_append(d, "description", name);
  674. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  675. matt = xs_list_append(matt, d);
  676. }
  677. }
  678. st = xs_dict_append(st, "media_attachments", matt);
  679. }
  680. {
  681. xs *ml = xs_list_new();
  682. xs *htl = xs_list_new();
  683. xs *eml = xs_list_new();
  684. const xs_list *tag = xs_dict_get(msg, "tag");
  685. int n = 0;
  686. xs *tag_list = NULL;
  687. if (xs_type(tag) == XSTYPE_DICT) {
  688. tag_list = xs_list_new();
  689. tag_list = xs_list_append(tag_list, tag);
  690. }
  691. else
  692. if (xs_type(tag) == XSTYPE_LIST)
  693. tag_list = xs_dup(tag);
  694. else
  695. tag_list = xs_list_new();
  696. tag = tag_list;
  697. const xs_dict *v;
  698. int c = 0;
  699. while (xs_list_next(tag, &v, &c)) {
  700. const char *type = xs_dict_get(v, "type");
  701. if (xs_is_null(type))
  702. continue;
  703. xs *d1 = xs_dict_new();
  704. if (strcmp(type, "Mention") == 0) {
  705. const char *name = xs_dict_get(v, "name");
  706. const char *href = xs_dict_get(v, "href");
  707. if (!xs_is_null(name) && !xs_is_null(href) &&
  708. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  709. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  710. xs *id = xs_fmt("%d", n++);
  711. d1 = xs_dict_append(d1, "id", id);
  712. d1 = xs_dict_append(d1, "username", nm);
  713. d1 = xs_dict_append(d1, "acct", nm);
  714. d1 = xs_dict_append(d1, "url", href);
  715. ml = xs_list_append(ml, d1);
  716. }
  717. }
  718. else
  719. if (strcmp(type, "Hashtag") == 0) {
  720. const char *name = xs_dict_get(v, "name");
  721. const char *href = xs_dict_get(v, "href");
  722. if (!xs_is_null(name) && !xs_is_null(href)) {
  723. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  724. d1 = xs_dict_append(d1, "name", nm);
  725. d1 = xs_dict_append(d1, "url", href);
  726. htl = xs_list_append(htl, d1);
  727. }
  728. }
  729. else
  730. if (strcmp(type, "Emoji") == 0) {
  731. const char *name = xs_dict_get(v, "name");
  732. const xs_dict *icon = xs_dict_get(v, "icon");
  733. if (!xs_is_null(name) && !xs_is_null(icon)) {
  734. const char *url = xs_dict_get(icon, "url");
  735. if (!xs_is_null(url)) {
  736. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  737. d1 = xs_dict_append(d1, "shortcode", nm);
  738. d1 = xs_dict_append(d1, "url", url);
  739. d1 = xs_dict_append(d1, "static_url", url);
  740. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  741. d1 = xs_dict_append(d1, "category", "Emojis");
  742. eml = xs_list_append(eml, d1);
  743. }
  744. }
  745. }
  746. }
  747. st = xs_dict_append(st, "mentions", ml);
  748. st = xs_dict_append(st, "tags", htl);
  749. st = xs_dict_append(st, "emojis", eml);
  750. }
  751. xs_free(idx);
  752. xs_free(ixc);
  753. idx = object_likes(id);
  754. ixc = xs_number_new(xs_list_len(idx));
  755. st = xs_dict_append(st, "favourites_count", ixc);
  756. st = xs_dict_append(st, "favourited",
  757. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  758. xs_free(idx);
  759. xs_free(ixc);
  760. idx = object_announces(id);
  761. ixc = xs_number_new(xs_list_len(idx));
  762. st = xs_dict_append(st, "reblogs_count", ixc);
  763. st = xs_dict_append(st, "reblogged",
  764. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  765. /* get the last person who boosted this */
  766. xs *boosted_by_md5 = NULL;
  767. if (xs_list_len(idx))
  768. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  769. xs_free(idx);
  770. xs_free(ixc);
  771. idx = object_children(id);
  772. ixc = xs_number_new(xs_list_len(idx));
  773. st = xs_dict_append(st, "replies_count", ixc);
  774. /* default in_reply_to values */
  775. st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL));
  776. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL));
  777. tmp = xs_dict_get(msg, "inReplyTo");
  778. if (!xs_is_null(tmp)) {
  779. xs *irto = NULL;
  780. if (valid_status(object_get(tmp, &irto))) {
  781. xs *irt_mid = mastoapi_id(irto);
  782. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  783. const char *at = NULL;
  784. if (!xs_is_null(at = get_atto(irto))) {
  785. xs *at_md5 = xs_md5_hex(at, strlen(at));
  786. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  787. }
  788. }
  789. }
  790. st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL));
  791. st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL));
  792. st = xs_dict_append(st, "language", "en");
  793. st = xs_dict_append(st, "filtered", xs_stock(XSTYPE_LIST));
  794. st = xs_dict_append(st, "muted", xs_stock(XSTYPE_FALSE));
  795. tmp = xs_dict_get(msg, "sourceContent");
  796. if (xs_is_null(tmp))
  797. tmp = "";
  798. st = xs_dict_append(st, "text", tmp);
  799. tmp = xs_dict_get(msg, "updated");
  800. xs *fd2 = NULL;
  801. if (xs_is_null(tmp))
  802. tmp = xs_stock(XSTYPE_NULL);
  803. else {
  804. fd2 = mastoapi_date(tmp);
  805. tmp = fd2;
  806. }
  807. st = xs_dict_append(st, "edited_at", tmp);
  808. if (strcmp(type, "Question") == 0) {
  809. xs *poll = mastoapi_poll(snac, msg);
  810. st = xs_dict_append(st, "poll", poll);
  811. }
  812. else
  813. st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL));
  814. st = xs_dict_append(st, "bookmarked", xs_stock(XSTYPE_FALSE));
  815. st = xs_dict_append(st, "pinned",
  816. (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  817. /* is it a boost? */
  818. if (!xs_is_null(boosted_by_md5)) {
  819. /* create a new dummy status, using st as the 'reblog' field */
  820. xs_dict *bst = xs_dup(st);
  821. xs *b_actor = NULL;
  822. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  823. xs *b_acct = mastoapi_account(b_actor);
  824. xs *fake_uri = NULL;
  825. if (snac)
  826. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  827. else
  828. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  829. bst = xs_dict_set(bst, "uri", fake_uri);
  830. bst = xs_dict_set(bst, "url", fake_uri);
  831. bst = xs_dict_set(bst, "account", b_acct);
  832. bst = xs_dict_set(bst, "content", "");
  833. bst = xs_dict_set(bst, "reblog", st);
  834. xs_free(st);
  835. st = bst;
  836. }
  837. }
  838. return st;
  839. }
  840. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  841. {
  842. xs_dict *rel = NULL;
  843. xs *actor_o = NULL;
  844. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  845. rel = xs_dict_new();
  846. const char *actor = xs_dict_get(actor_o, "id");
  847. rel = xs_dict_append(rel, "id", md5);
  848. rel = xs_dict_append(rel, "following",
  849. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  850. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  851. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  852. rel = xs_dict_append(rel, "followed_by",
  853. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  854. rel = xs_dict_append(rel, "blocking",
  855. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  856. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  857. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  858. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  859. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  860. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  861. rel = xs_dict_append(rel, "note", "");
  862. }
  863. return rel;
  864. }
  865. int process_auth_token(snac *snac, const xs_dict *req)
  866. /* processes an authorization token, if there is one */
  867. {
  868. int logged_in = 0;
  869. const char *v;
  870. /* if there is an authorization field, try to validate it */
  871. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  872. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  873. xs *token = token_get(tokid);
  874. if (token != NULL) {
  875. const char *uid = xs_dict_get(token, "uid");
  876. if (!xs_is_null(uid) && user_open(snac, uid)) {
  877. logged_in = 1;
  878. /* this counts as a 'login' */
  879. lastlog_write(snac, "mastoapi");
  880. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  881. }
  882. else
  883. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  884. }
  885. else
  886. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  887. }
  888. return logged_in;
  889. }
  890. void credentials_get(char **body, char **ctype, int *status, snac snac)
  891. {
  892. xs *acct = xs_dict_new();
  893. acct = xs_dict_append(acct, "id", snac.md5);
  894. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  895. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  896. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  897. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  898. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  899. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  900. acct = xs_dict_append(acct, "url", snac.actor);
  901. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  902. acct = xs_dict_append(acct, "bot", xs_dict_get(snac.config, "bot"));
  903. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  904. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  905. "\"follow_requests_count\": 0,"
  906. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  907. /* some apps take the note from the source object */
  908. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  909. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  910. const xs_str *cw = xs_dict_get(snac.config, "cw");
  911. src = xs_dict_set(src, "sensitive",
  912. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  913. src = xs_dict_set(src, "bot", xs_dict_get(snac.config, "bot"));
  914. xs *avatar = NULL;
  915. const char *av = xs_dict_get(snac.config, "avatar");
  916. if (xs_is_null(av) || *av == '\0')
  917. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  918. else
  919. avatar = xs_dup(av);
  920. acct = xs_dict_append(acct, "avatar", avatar);
  921. acct = xs_dict_append(acct, "avatar_static", avatar);
  922. xs *header = NULL;
  923. const char *hd = xs_dict_get(snac.config, "header");
  924. if (!xs_is_null(hd))
  925. header = xs_dup(hd);
  926. else
  927. header = xs_fmt("%s/header.png", srv_baseurl);
  928. acct = xs_dict_append(acct, "header", header);
  929. acct = xs_dict_append(acct, "header_static", header);
  930. const xs_dict *metadata = xs_dict_get(snac.config, "metadata");
  931. if (xs_type(metadata) == XSTYPE_DICT) {
  932. xs *fields = xs_list_new();
  933. const xs_str *k;
  934. const xs_str *v;
  935. xs_dict *val_links = snac.links;
  936. if (xs_is_null(val_links))
  937. val_links = xs_stock(XSTYPE_DICT);
  938. int c = 0;
  939. while (xs_dict_next(metadata, &k, &v, &c)) {
  940. xs *val_date = NULL;
  941. const xs_number *verified_time = xs_dict_get(val_links, v);
  942. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  943. time_t t = xs_number_get(verified_time);
  944. if (t > 0)
  945. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  946. }
  947. xs *d = xs_dict_new();
  948. d = xs_dict_append(d, "name", k);
  949. d = xs_dict_append(d, "value", v);
  950. d = xs_dict_append(d, "verified_at",
  951. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  952. fields = xs_list_append(fields, d);
  953. }
  954. acct = xs_dict_set(acct, "fields", fields);
  955. /* some apps take the fields from the source object */
  956. src = xs_dict_set(src, "fields", fields);
  957. }
  958. acct = xs_dict_append(acct, "source", src);
  959. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  960. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  961. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  962. *body = xs_json_dumps(acct, 4);
  963. *ctype = "application/json";
  964. *status = HTTP_STATUS_OK;
  965. }
  966. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  967. char **body, int *b_size, char **ctype)
  968. {
  969. (void)b_size;
  970. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  971. return 0;
  972. int status = HTTP_STATUS_NOT_FOUND;
  973. const xs_dict *args = xs_dict_get(req, "q_vars");
  974. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  975. snac snac1 = {0};
  976. int logged_in = process_auth_token(&snac1, req);
  977. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  978. if (logged_in) {
  979. credentials_get(body, ctype, &status, snac1);
  980. }
  981. else {
  982. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  983. }
  984. }
  985. else
  986. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  987. /* find if an account is followed, blocked, etc. */
  988. /* the account to get relationships about is in args "id[]" */
  989. if (logged_in) {
  990. xs *res = xs_list_new();
  991. const char *md5 = xs_dict_get(args, "id[]");
  992. if (xs_is_null(md5))
  993. md5 = xs_dict_get(args, "id");
  994. if (!xs_is_null(md5)) {
  995. if (xs_type(md5) == XSTYPE_LIST)
  996. md5 = xs_list_get(md5, 0);
  997. xs *rel = mastoapi_relationship(&snac1, md5);
  998. if (rel != NULL)
  999. res = xs_list_append(res, rel);
  1000. }
  1001. *body = xs_json_dumps(res, 4);
  1002. *ctype = "application/json";
  1003. status = HTTP_STATUS_OK;
  1004. }
  1005. else
  1006. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1007. }
  1008. else
  1009. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1010. /* lookup an account */
  1011. const char *acct = xs_dict_get(args, "acct");
  1012. if (!xs_is_null(acct)) {
  1013. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1014. xs *l = xs_split_n(s, "@", 1);
  1015. const char *uid = xs_list_get(l, 0);
  1016. const char *host = xs_list_get(l, 1);
  1017. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1018. snac user;
  1019. if (user_open(&user, uid)) {
  1020. xs *actor = msg_actor(&user);
  1021. xs *macct = mastoapi_account(actor);
  1022. *body = xs_json_dumps(macct, 4);
  1023. *ctype = "application/json";
  1024. status = HTTP_STATUS_OK;
  1025. user_free(&user);
  1026. }
  1027. }
  1028. }
  1029. }
  1030. else
  1031. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1032. /* account-related information */
  1033. xs *l = xs_split(cmd, "/");
  1034. const char *uid = xs_list_get(l, 3);
  1035. const char *opt = xs_list_get(l, 4);
  1036. if (uid != NULL) {
  1037. snac snac2;
  1038. xs *out = NULL;
  1039. xs *actor = NULL;
  1040. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1041. /* search for accounts starting with q */
  1042. const char *aq = xs_dict_get(args, "q");
  1043. if (!xs_is_null(aq)) {
  1044. xs *q = xs_tolower_i(xs_dup(aq));
  1045. out = xs_list_new();
  1046. xs *wing = following_list(&snac1);
  1047. xs *wers = follower_list(&snac1);
  1048. xs *ulst = user_list();
  1049. xs_list *p;
  1050. const xs_str *v;
  1051. xs_set seen;
  1052. xs_set_init(&seen);
  1053. /* user relations */
  1054. xs_list *lsts[] = { wing, wers, NULL };
  1055. int n;
  1056. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1057. while (xs_list_iter(&p, &v)) {
  1058. /* already seen? skip */
  1059. if (xs_set_add(&seen, v) == 0)
  1060. continue;
  1061. xs *actor = NULL;
  1062. if (valid_status(object_get(v, &actor))) {
  1063. const char *uname = xs_dict_get(actor, "preferredUsername");
  1064. if (!xs_is_null(uname)) {
  1065. xs *luname = xs_tolower_i(xs_dup(uname));
  1066. if (xs_startswith(luname, q)) {
  1067. xs *acct = mastoapi_account(actor);
  1068. out = xs_list_append(out, acct);
  1069. }
  1070. }
  1071. }
  1072. }
  1073. }
  1074. /* local users */
  1075. p = ulst;
  1076. while (xs_list_iter(&p, &v)) {
  1077. snac user;
  1078. /* skip this same user */
  1079. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1080. continue;
  1081. /* skip if the uid does not start with the query */
  1082. xs *v2 = xs_tolower_i(xs_dup(v));
  1083. if (!xs_startswith(v2, q))
  1084. continue;
  1085. if (user_open(&user, v)) {
  1086. /* if it's not already seen, add it */
  1087. if (xs_set_add(&seen, user.actor) == 1) {
  1088. xs *actor = msg_actor(&user);
  1089. xs *acct = mastoapi_account(actor);
  1090. out = xs_list_append(out, acct);
  1091. }
  1092. user_free(&user);
  1093. }
  1094. }
  1095. xs_set_free(&seen);
  1096. }
  1097. }
  1098. else
  1099. /* is it a local user? */
  1100. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1101. if (opt == NULL) {
  1102. /* account information */
  1103. actor = msg_actor(&snac2);
  1104. out = mastoapi_account(actor);
  1105. }
  1106. else
  1107. if (strcmp(opt, "statuses") == 0) { /** **/
  1108. /* the public list of posts of a user */
  1109. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1110. xs_list *p = timeline;
  1111. const xs_str *v;
  1112. out = xs_list_new();
  1113. while (xs_list_iter(&p, &v)) {
  1114. xs *msg = NULL;
  1115. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1116. /* add only posts by the author */
  1117. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1118. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1119. xs *st = mastoapi_status(&snac2, msg);
  1120. if (st)
  1121. out = xs_list_append(out, st);
  1122. }
  1123. }
  1124. }
  1125. }
  1126. else
  1127. if (strcmp(opt, "featured_tags") == 0) {
  1128. /* snac doesn't have features tags, yet? */
  1129. /* implement empty response so apps like Tokodon don't show an error */
  1130. *body = xs_dup("[]");
  1131. *ctype = "application/json";
  1132. status = HTTP_STATUS_OK;
  1133. }
  1134. user_free(&snac2);
  1135. }
  1136. else {
  1137. /* try the uid as the md5 of a possibly loaded actor */
  1138. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1139. if (opt == NULL) {
  1140. /* account information */
  1141. out = mastoapi_account(actor);
  1142. }
  1143. else
  1144. if (strcmp(opt, "statuses") == 0) {
  1145. /* we don't serve statuses of others; return the empty list */
  1146. out = xs_list_new();
  1147. }
  1148. else
  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. /* Ice Cubes sends these values as query parameters, so try these */
  1887. if (name == NULL && ruri == NULL && scope == NULL) {
  1888. args = xs_dup(xs_dict_get(req, "q_vars"));
  1889. name = xs_dict_get(args, "client_name");
  1890. ruri = xs_dict_get(args, "redirect_uris");
  1891. scope = xs_dict_get(args, "scope");
  1892. }
  1893. if (xs_type(ruri) == XSTYPE_LIST)
  1894. ruri = xs_dict_get(ruri, 0);
  1895. if (name && ruri) {
  1896. xs *app = xs_dict_new();
  1897. xs *id = xs_replace_i(tid(0), ".", "");
  1898. xs *csec = random_str();
  1899. xs *vkey = random_str();
  1900. xs *cid = NULL;
  1901. /* pick a non-existent random cid */
  1902. for (;;) {
  1903. cid = random_str();
  1904. xs *p_app = app_get(cid);
  1905. if (p_app == NULL)
  1906. break;
  1907. xs_free(cid);
  1908. }
  1909. app = xs_dict_append(app, "name", name);
  1910. app = xs_dict_append(app, "redirect_uri", ruri);
  1911. app = xs_dict_append(app, "client_id", cid);
  1912. app = xs_dict_append(app, "client_secret", csec);
  1913. app = xs_dict_append(app, "vapid_key", vkey);
  1914. app = xs_dict_append(app, "id", id);
  1915. *body = xs_json_dumps(app, 4);
  1916. *ctype = "application/json";
  1917. status = HTTP_STATUS_OK;
  1918. app = xs_dict_append(app, "code", "");
  1919. if (scope)
  1920. app = xs_dict_append(app, "scope", scope);
  1921. app_add(cid, app);
  1922. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1923. }
  1924. }
  1925. else
  1926. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1927. if (logged_in) {
  1928. /* post a new Note */
  1929. const char *content = xs_dict_get(args, "status");
  1930. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1931. const char *visibility = xs_dict_get(args, "visibility");
  1932. const char *summary = xs_dict_get(args, "spoiler_text");
  1933. const char *media_ids = xs_dict_get(args, "media_ids");
  1934. if (xs_is_null(media_ids))
  1935. media_ids = xs_dict_get(args, "media_ids[]");
  1936. if (xs_is_null(media_ids))
  1937. media_ids = xs_dict_get(args, "media_ids");
  1938. if (xs_is_null(visibility))
  1939. visibility = "public";
  1940. xs *attach_list = xs_list_new();
  1941. xs *irt = NULL;
  1942. /* is it a reply? */
  1943. if (mid != NULL) {
  1944. xs *r_msg = NULL;
  1945. const char *md5 = MID_TO_MD5(mid);
  1946. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1947. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1948. }
  1949. /* does it have attachments? */
  1950. if (!xs_is_null(media_ids)) {
  1951. xs *mi = NULL;
  1952. if (xs_type(media_ids) == XSTYPE_LIST)
  1953. mi = xs_dup(media_ids);
  1954. else {
  1955. mi = xs_list_new();
  1956. mi = xs_list_append(mi, media_ids);
  1957. }
  1958. xs_list *p = mi;
  1959. const xs_str *v;
  1960. while (xs_list_iter(&p, &v)) {
  1961. xs *l = xs_list_new();
  1962. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1963. xs *desc = static_get_meta(&snac, v);
  1964. l = xs_list_append(l, url);
  1965. l = xs_list_append(l, desc);
  1966. attach_list = xs_list_append(attach_list, l);
  1967. }
  1968. }
  1969. /* prepare the message */
  1970. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1971. strcmp(visibility, "public") == 0 ? 0 : 1);
  1972. if (!xs_is_null(summary) && *summary) {
  1973. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  1974. msg = xs_dict_set(msg, "summary", summary);
  1975. }
  1976. /* store */
  1977. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1978. /* 'Create' message */
  1979. xs *c_msg = msg_create(&snac, msg);
  1980. enqueue_message(&snac, c_msg);
  1981. timeline_touch(&snac);
  1982. /* convert to a mastodon status as a response code */
  1983. xs *st = mastoapi_status(&snac, msg);
  1984. *body = xs_json_dumps(st, 4);
  1985. *ctype = "application/json";
  1986. status = HTTP_STATUS_OK;
  1987. }
  1988. else
  1989. status = HTTP_STATUS_UNAUTHORIZED;
  1990. }
  1991. else
  1992. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1993. if (logged_in) {
  1994. /* operations on a status */
  1995. xs *l = xs_split(cmd, "/");
  1996. const char *mid = xs_list_get(l, 3);
  1997. const char *op = xs_list_get(l, 4);
  1998. if (!xs_is_null(mid)) {
  1999. xs *msg = NULL;
  2000. xs *out = NULL;
  2001. /* skip the 'fake' part of the id */
  2002. mid = MID_TO_MD5(mid);
  2003. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2004. const char *id = xs_dict_get(msg, "id");
  2005. if (op == NULL) {
  2006. /* no operation (?) */
  2007. }
  2008. else
  2009. if (strcmp(op, "favourite") == 0) { /** **/
  2010. xs *n_msg = msg_admiration(&snac, id, "Like");
  2011. if (n_msg != NULL) {
  2012. enqueue_message(&snac, n_msg);
  2013. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2014. out = mastoapi_status(&snac, msg);
  2015. }
  2016. }
  2017. else
  2018. if (strcmp(op, "unfavourite") == 0) { /** **/
  2019. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2020. if (n_msg != NULL) {
  2021. enqueue_message(&snac, n_msg);
  2022. out = mastoapi_status(&snac, msg);
  2023. }
  2024. }
  2025. else
  2026. if (strcmp(op, "reblog") == 0) { /** **/
  2027. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2028. if (n_msg != NULL) {
  2029. enqueue_message(&snac, n_msg);
  2030. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2031. out = mastoapi_status(&snac, msg);
  2032. }
  2033. }
  2034. else
  2035. if (strcmp(op, "unreblog") == 0) { /** **/
  2036. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2037. if (n_msg != NULL) {
  2038. enqueue_message(&snac, n_msg);
  2039. out = mastoapi_status(&snac, msg);
  2040. }
  2041. }
  2042. else
  2043. if (strcmp(op, "bookmark") == 0) { /** **/
  2044. /* snac does not support bookmarks */
  2045. }
  2046. else
  2047. if (strcmp(op, "unbookmark") == 0) { /** **/
  2048. /* snac does not support bookmarks */
  2049. }
  2050. else
  2051. if (strcmp(op, "pin") == 0) { /** **/
  2052. /* pin this message */
  2053. if (pin(&snac, id))
  2054. out = mastoapi_status(&snac, msg);
  2055. else
  2056. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2057. }
  2058. else
  2059. if (strcmp(op, "unpin") == 0) { /** **/
  2060. /* unpin this message */
  2061. unpin(&snac, id);
  2062. out = mastoapi_status(&snac, msg);
  2063. }
  2064. else
  2065. if (strcmp(op, "mute") == 0) { /** **/
  2066. /* Mastodon's mute is snac's hide */
  2067. }
  2068. else
  2069. if (strcmp(op, "unmute") == 0) { /** **/
  2070. /* Mastodon's unmute is snac's unhide */
  2071. }
  2072. }
  2073. if (out != NULL) {
  2074. *body = xs_json_dumps(out, 4);
  2075. *ctype = "application/json";
  2076. status = HTTP_STATUS_OK;
  2077. }
  2078. }
  2079. }
  2080. else
  2081. status = HTTP_STATUS_UNAUTHORIZED;
  2082. }
  2083. else
  2084. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2085. if (logged_in) {
  2086. notify_clear(&snac);
  2087. timeline_touch(&snac);
  2088. *body = xs_dup("{}");
  2089. *ctype = "application/json";
  2090. status = HTTP_STATUS_OK;
  2091. }
  2092. else
  2093. status = HTTP_STATUS_UNAUTHORIZED;
  2094. }
  2095. else
  2096. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2097. /* I don't know what I'm doing */
  2098. if (logged_in) {
  2099. const char *v;
  2100. xs *wpush = xs_dict_new();
  2101. wpush = xs_dict_append(wpush, "id", "1");
  2102. v = xs_dict_get(args, "data");
  2103. v = xs_dict_get(v, "alerts");
  2104. wpush = xs_dict_append(wpush, "alerts", v);
  2105. v = xs_dict_get(args, "subscription");
  2106. v = xs_dict_get(v, "endpoint");
  2107. wpush = xs_dict_append(wpush, "endpoint", v);
  2108. xs *server_key = random_str();
  2109. wpush = xs_dict_append(wpush, "server_key", server_key);
  2110. *body = xs_json_dumps(wpush, 4);
  2111. *ctype = "application/json";
  2112. status = HTTP_STATUS_OK;
  2113. }
  2114. else
  2115. status = HTTP_STATUS_UNAUTHORIZED;
  2116. }
  2117. else
  2118. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2119. if (logged_in) {
  2120. const xs_list *file = xs_dict_get(args, "file");
  2121. const char *desc = xs_dict_get(args, "description");
  2122. if (xs_is_null(desc))
  2123. desc = "";
  2124. status = HTTP_STATUS_BAD_REQUEST;
  2125. if (xs_type(file) == XSTYPE_LIST) {
  2126. const char *fn = xs_list_get(file, 0);
  2127. if (*fn != '\0') {
  2128. char *ext = strrchr(fn, '.');
  2129. xs *hash = xs_md5_hex(fn, strlen(fn));
  2130. xs *id = xs_fmt("%s%s", hash, ext);
  2131. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2132. int fo = xs_number_get(xs_list_get(file, 1));
  2133. int fs = xs_number_get(xs_list_get(file, 2));
  2134. /* store */
  2135. static_put(&snac, id, payload + fo, fs);
  2136. static_put_meta(&snac, id, desc);
  2137. /* prepare a response */
  2138. xs *rsp = xs_dict_new();
  2139. rsp = xs_dict_append(rsp, "id", id);
  2140. rsp = xs_dict_append(rsp, "type", "image");
  2141. rsp = xs_dict_append(rsp, "url", url);
  2142. rsp = xs_dict_append(rsp, "preview_url", url);
  2143. rsp = xs_dict_append(rsp, "remote_url", url);
  2144. rsp = xs_dict_append(rsp, "description", desc);
  2145. *body = xs_json_dumps(rsp, 4);
  2146. *ctype = "application/json";
  2147. status = HTTP_STATUS_OK;
  2148. }
  2149. }
  2150. }
  2151. else
  2152. status = HTTP_STATUS_UNAUTHORIZED;
  2153. }
  2154. else
  2155. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2156. if (logged_in) {
  2157. /* account-related information */
  2158. xs *l = xs_split(cmd, "/");
  2159. const char *md5 = xs_list_get(l, 3);
  2160. const char *opt = xs_list_get(l, 4);
  2161. xs *rsp = NULL;
  2162. if (!xs_is_null(md5) && *md5) {
  2163. xs *actor_o = NULL;
  2164. if (xs_is_null(opt)) {
  2165. /* ? */
  2166. }
  2167. else
  2168. if (strcmp(opt, "follow") == 0) { /** **/
  2169. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2170. const char *actor = xs_dict_get(actor_o, "id");
  2171. xs *msg = msg_follow(&snac, actor);
  2172. if (msg != NULL) {
  2173. /* reload the actor from the message, in may be different */
  2174. actor = xs_dict_get(msg, "object");
  2175. following_add(&snac, actor, msg);
  2176. enqueue_output_by_actor(&snac, msg, actor, 0);
  2177. rsp = mastoapi_relationship(&snac, md5);
  2178. }
  2179. }
  2180. }
  2181. else
  2182. if (strcmp(opt, "unfollow") == 0) { /** **/
  2183. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2184. const char *actor = xs_dict_get(actor_o, "id");
  2185. /* get the following object */
  2186. xs *object = NULL;
  2187. if (valid_status(following_get(&snac, actor, &object))) {
  2188. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2189. following_del(&snac, actor);
  2190. enqueue_output_by_actor(&snac, msg, actor, 0);
  2191. rsp = mastoapi_relationship(&snac, md5);
  2192. }
  2193. }
  2194. }
  2195. else
  2196. if (strcmp(opt, "block") == 0) { /** **/
  2197. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2198. const char *actor = xs_dict_get(actor_o, "id");
  2199. mute(&snac, actor);
  2200. rsp = mastoapi_relationship(&snac, md5);
  2201. }
  2202. }
  2203. else
  2204. if (strcmp(opt, "unblock") == 0) { /** **/
  2205. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2206. const char *actor = xs_dict_get(actor_o, "id");
  2207. unmute(&snac, actor);
  2208. rsp = mastoapi_relationship(&snac, md5);
  2209. }
  2210. }
  2211. }
  2212. if (rsp != NULL) {
  2213. *body = xs_json_dumps(rsp, 4);
  2214. *ctype = "application/json";
  2215. status = HTTP_STATUS_OK;
  2216. }
  2217. }
  2218. else
  2219. status = HTTP_STATUS_UNAUTHORIZED;
  2220. }
  2221. else
  2222. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2223. if (logged_in) {
  2224. /* operations on a status */
  2225. xs *l = xs_split(cmd, "/");
  2226. const char *mid = xs_list_get(l, 3);
  2227. const char *op = xs_list_get(l, 4);
  2228. if (!xs_is_null(mid)) {
  2229. xs *msg = NULL;
  2230. xs *out = NULL;
  2231. /* skip the 'fake' part of the id */
  2232. mid = MID_TO_MD5(mid);
  2233. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2234. const char *id = xs_dict_get(msg, "id");
  2235. const char *atto = get_atto(msg);
  2236. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2237. if (opts == NULL)
  2238. opts = xs_dict_get(msg, "anyOf");
  2239. if (op == NULL) {
  2240. }
  2241. else
  2242. if (strcmp(op, "votes") == 0) {
  2243. const xs_list *choices = xs_dict_get(args, "choices[]");
  2244. if (xs_is_null(choices))
  2245. choices = xs_dict_get(args, "choices");
  2246. if (xs_type(choices) == XSTYPE_LIST) {
  2247. const xs_str *v;
  2248. int c = 0;
  2249. while (xs_list_next(choices, &v, &c)) {
  2250. int io = atoi(v);
  2251. const xs_dict *o = xs_list_get(opts, io);
  2252. if (o) {
  2253. const char *name = xs_dict_get(o, "name");
  2254. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2255. msg = xs_dict_append(msg, "name", name);
  2256. xs *c_msg = msg_create(&snac, msg);
  2257. enqueue_message(&snac, c_msg);
  2258. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2259. }
  2260. }
  2261. out = mastoapi_poll(&snac, msg);
  2262. }
  2263. }
  2264. }
  2265. if (out != NULL) {
  2266. *body = xs_json_dumps(out, 4);
  2267. *ctype = "application/json";
  2268. status = HTTP_STATUS_OK;
  2269. }
  2270. }
  2271. }
  2272. else
  2273. status = HTTP_STATUS_UNAUTHORIZED;
  2274. }
  2275. else
  2276. if (strcmp(cmd, "/v1/lists") == 0) {
  2277. if (logged_in) {
  2278. const char *title = xs_dict_get(args, "title");
  2279. if (xs_type(title) == XSTYPE_STRING) {
  2280. /* add the list */
  2281. xs *out = xs_dict_new();
  2282. if (xs_type(list_maint(&snac, title, 1)) == XSTYPE_TRUE) {
  2283. out = xs_dict_append(out, "title", title);
  2284. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2285. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2286. status = HTTP_STATUS_OK;
  2287. }
  2288. else {
  2289. out = xs_dict_append(out, "error", "cannot create list");
  2290. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2291. }
  2292. *body = xs_json_dumps(out, 4);
  2293. *ctype = "application/json";
  2294. }
  2295. else
  2296. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2297. }
  2298. }
  2299. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2300. if (logged_in) {
  2301. xs *l = xs_split(cmd, "/");
  2302. const char *op = xs_list_get(l, -1);
  2303. const char *id = xs_list_get(l, -2);
  2304. if (op && id && xs_is_hex(id)) {
  2305. if (strcmp(op, "accounts") == 0) {
  2306. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2307. int c = 0;
  2308. const char *v;
  2309. while (xs_list_next(accts, &v, &c)) {
  2310. list_content(&snac, id, v, 1);
  2311. }
  2312. status = HTTP_STATUS_OK;
  2313. }
  2314. }
  2315. }
  2316. else
  2317. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2318. }
  2319. /* user cleanup */
  2320. if (logged_in)
  2321. user_free(&snac);
  2322. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2323. return status;
  2324. }
  2325. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2326. const char *payload, int p_size,
  2327. char **body, int *b_size, char **ctype)
  2328. {
  2329. (void)p_size;
  2330. (void)body;
  2331. (void)b_size;
  2332. (void)ctype;
  2333. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2334. return 0;
  2335. int status = HTTP_STATUS_NOT_FOUND;
  2336. xs *args = NULL;
  2337. const char *i_ctype = xs_dict_get(req, "content-type");
  2338. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2339. if (!xs_is_null(payload))
  2340. args = xs_json_loads(payload);
  2341. }
  2342. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2343. {
  2344. // Some apps send form data instead of json so we should cater for those
  2345. if (!xs_is_null(payload)) {
  2346. xs *upl = xs_url_dec(payload);
  2347. args = xs_url_vars(upl);
  2348. }
  2349. }
  2350. else
  2351. args = xs_dup(xs_dict_get(req, "p_vars"));
  2352. if (args == NULL)
  2353. return HTTP_STATUS_BAD_REQUEST;
  2354. snac snac = {0};
  2355. int logged_in = process_auth_token(&snac, req);
  2356. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2357. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2358. // pretend we deleted it, since it doesn't exist anyway
  2359. status = HTTP_STATUS_OK;
  2360. }
  2361. else
  2362. if (xs_startswith(cmd, "/v1/lists/")) {
  2363. if (logged_in) {
  2364. xs *l = xs_split(cmd, "/");
  2365. const char *p = xs_list_get(l, -1);
  2366. if (p) {
  2367. if (strcmp(p, "accounts") == 0) {
  2368. /* delete account from list */
  2369. p = xs_list_get(l, -2);
  2370. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2371. int c = 0;
  2372. const char *v;
  2373. while (xs_list_next(accts, &v, &c)) {
  2374. list_content(&snac, p, v, 2);
  2375. }
  2376. }
  2377. else {
  2378. /* delete list */
  2379. if (xs_is_hex(p)) {
  2380. list_maint(&snac, p, 2);
  2381. }
  2382. }
  2383. }
  2384. status = HTTP_STATUS_OK;
  2385. }
  2386. else
  2387. status = HTTP_STATUS_UNAUTHORIZED;
  2388. }
  2389. /* user cleanup */
  2390. if (logged_in)
  2391. user_free(&snac);
  2392. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2393. return status;
  2394. }
  2395. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2396. const char *payload, int p_size,
  2397. char **body, int *b_size, char **ctype)
  2398. {
  2399. (void)p_size;
  2400. (void)b_size;
  2401. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2402. return 0;
  2403. int status = HTTP_STATUS_NOT_FOUND;
  2404. xs *args = NULL;
  2405. const char *i_ctype = xs_dict_get(req, "content-type");
  2406. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2407. if (!xs_is_null(payload))
  2408. args = xs_json_loads(payload);
  2409. }
  2410. else
  2411. args = xs_dup(xs_dict_get(req, "p_vars"));
  2412. if (args == NULL)
  2413. return HTTP_STATUS_BAD_REQUEST;
  2414. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2415. snac snac = {0};
  2416. int logged_in = process_auth_token(&snac, req);
  2417. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2418. if (logged_in) {
  2419. xs *l = xs_split(cmd, "/");
  2420. const char *stid = xs_list_get(l, 3);
  2421. if (!xs_is_null(stid)) {
  2422. const char *desc = xs_dict_get(args, "description");
  2423. /* set the image metadata */
  2424. static_put_meta(&snac, stid, desc);
  2425. /* prepare a response */
  2426. xs *rsp = xs_dict_new();
  2427. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2428. rsp = xs_dict_append(rsp, "id", stid);
  2429. rsp = xs_dict_append(rsp, "type", "image");
  2430. rsp = xs_dict_append(rsp, "url", url);
  2431. rsp = xs_dict_append(rsp, "preview_url", url);
  2432. rsp = xs_dict_append(rsp, "remote_url", url);
  2433. rsp = xs_dict_append(rsp, "description", desc);
  2434. *body = xs_json_dumps(rsp, 4);
  2435. *ctype = "application/json";
  2436. status = HTTP_STATUS_OK;
  2437. }
  2438. }
  2439. else
  2440. status = HTTP_STATUS_UNAUTHORIZED;
  2441. }
  2442. else
  2443. if (xs_startswith(cmd, "/v1/statuses")) {
  2444. if (logged_in) {
  2445. xs *l = xs_split(cmd, "/");
  2446. const char *mid = xs_list_get(l, 3);
  2447. if (!xs_is_null(mid)) {
  2448. const char *md5 = MID_TO_MD5(mid);
  2449. xs *rsp = NULL;
  2450. xs *msg = NULL;
  2451. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2452. const char *content = xs_dict_get(args, "status");
  2453. xs *atls = xs_list_new();
  2454. xs *f_content = not_really_markdown(content, &atls, NULL);
  2455. /* replace fields with new content */
  2456. msg = xs_dict_set(msg, "sourceContent", content);
  2457. msg = xs_dict_set(msg, "content", f_content);
  2458. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2459. msg = xs_dict_set(msg, "updated", updated);
  2460. /* overwrite object, not updating the indexes */
  2461. object_add_ow(xs_dict_get(msg, "id"), msg);
  2462. /* update message */
  2463. xs *c_msg = msg_update(&snac, msg);
  2464. enqueue_message(&snac, c_msg);
  2465. rsp = mastoapi_status(&snac, msg);
  2466. }
  2467. if (rsp != NULL) {
  2468. *body = xs_json_dumps(rsp, 4);
  2469. *ctype = "application/json";
  2470. status = HTTP_STATUS_OK;
  2471. }
  2472. }
  2473. }
  2474. else
  2475. status = HTTP_STATUS_UNAUTHORIZED;
  2476. }
  2477. /* user cleanup */
  2478. if (logged_in)
  2479. user_free(&snac);
  2480. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2481. return status;
  2482. }
  2483. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2484. /* Store header or avatar */
  2485. {
  2486. if (data != NULL) {
  2487. if (xs_type(data) == XSTYPE_LIST) {
  2488. const char *fn = xs_list_get(data, 0);
  2489. if (fn && *fn) {
  2490. const char *ext = strrchr(fn, '.');
  2491. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2492. if (ext == NULL || strcmp(fn, key) == 0) {
  2493. ext = ".jpg";
  2494. }
  2495. /* Make sure we have a unique file name, otherwise updated images will not be
  2496. * re-loaded by clients. */
  2497. xs *rnd = random_str();
  2498. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2499. xs *id = xs_fmt("%s%s", hash, ext);
  2500. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2501. int fo = xs_number_get(xs_list_get(data, 1));
  2502. int fs = xs_number_get(xs_list_get(data, 2));
  2503. /* store */
  2504. static_put(snac, id, payload + fo, fs);
  2505. snac->config = xs_dict_set(snac->config, key, url);
  2506. }
  2507. }
  2508. }
  2509. }
  2510. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2511. const char *payload, int p_size,
  2512. char **body, int *b_size, char **ctype)
  2513. /* Handle profile updates */
  2514. {
  2515. (void)p_size;
  2516. (void)b_size;
  2517. if (!xs_startswith(q_path, "/api/v1/"))
  2518. return 0;
  2519. int status = HTTP_STATUS_NOT_FOUND;
  2520. xs *args = NULL;
  2521. const char *i_ctype = xs_dict_get(req, "content-type");
  2522. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2523. if (!xs_is_null(payload))
  2524. args = xs_json_loads(payload);
  2525. }
  2526. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2527. {
  2528. // Some apps send form data instead of json so we should cater for those
  2529. if (!xs_is_null(payload)) {
  2530. xs *upl = xs_url_dec(payload);
  2531. args = xs_url_vars(upl);
  2532. }
  2533. }
  2534. else
  2535. args = xs_dup(xs_dict_get(req, "p_vars"));
  2536. if (args == NULL)
  2537. return HTTP_STATUS_BAD_REQUEST;
  2538. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2539. snac snac = {0};
  2540. int logged_in = process_auth_token(&snac, req);
  2541. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) {
  2542. /* Update user profile fields */
  2543. if (logged_in) {
  2544. int c = 0;
  2545. const xs_str *k;
  2546. const xs_val *v;
  2547. const xs_str *field_name = NULL;
  2548. xs *new_fields = xs_dict_new();
  2549. while (xs_dict_next(args, &k, &v, &c)) {
  2550. if (strcmp(k, "display_name") == 0) {
  2551. if (v != NULL)
  2552. snac.config = xs_dict_set(snac.config, "name", v);
  2553. }
  2554. else
  2555. if (strcmp(k, "note") == 0) {
  2556. if (v != NULL)
  2557. snac.config = xs_dict_set(snac.config, "bio", v);
  2558. }
  2559. else
  2560. if (strcmp(k, "bot") == 0) {
  2561. if (v != NULL)
  2562. snac.config = xs_dict_set(snac.config, "bot",
  2563. (strcmp(v, "true") == 0 ||
  2564. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2565. }
  2566. else
  2567. if (strcmp(k, "source[sensitive]") == 0) {
  2568. if (v != NULL)
  2569. snac.config = xs_dict_set(snac.config, "cw",
  2570. strcmp(v, "true") == 0 ? "open" : "");
  2571. }
  2572. else
  2573. if (strcmp(k, "source[privacy]") == 0) {
  2574. if (v != NULL)
  2575. snac.config = xs_dict_set(snac.config, "private",
  2576. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  2577. }
  2578. else
  2579. if (strcmp(k, "header") == 0) {
  2580. persist_image("header", v, payload, &snac);
  2581. }
  2582. else
  2583. if (strcmp(k, "avatar") == 0) {
  2584. persist_image("avatar", v, payload, &snac);
  2585. }
  2586. else
  2587. if (xs_starts_and_ends("fields_attributes", k, "[name]")) {
  2588. field_name = strcmp(v, "") != 0 ? v : NULL;
  2589. }
  2590. else
  2591. if (xs_starts_and_ends("fields_attributes", k, "[value]")) {
  2592. if (field_name != NULL) {
  2593. new_fields = xs_dict_set(new_fields, field_name, v);
  2594. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  2595. }
  2596. }
  2597. /* we don't have support for the following options, yet
  2598. - discoverable (0/1)
  2599. - locked (0/1)
  2600. */
  2601. }
  2602. /* Persist profile */
  2603. if (user_persist(&snac) == 0)
  2604. credentials_get(body, ctype, &status, snac);
  2605. else
  2606. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  2607. }
  2608. else
  2609. status = HTTP_STATUS_UNAUTHORIZED;
  2610. }
  2611. /* user cleanup */
  2612. if (logged_in)
  2613. user_free(&snac);
  2614. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  2615. return status;
  2616. }
  2617. void mastoapi_purge(void)
  2618. {
  2619. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2620. xs *files = xs_glob(spec, 1, 0);
  2621. xs_list *p = files;
  2622. const xs_str *v;
  2623. time_t mt = time(NULL) - 3600;
  2624. while (xs_list_iter(&p, &v)) {
  2625. xs *cid = xs_replace(v, ".json", "");
  2626. xs *fn = _app_fn(cid);
  2627. if (mtime(fn) < mt) {
  2628. /* get the app */
  2629. xs *app = app_get(cid);
  2630. if (app) {
  2631. /* old apps with no uid are incomplete cruft */
  2632. const char *uid = xs_dict_get(app, "uid");
  2633. if (xs_is_null(uid) || *uid == '\0') {
  2634. unlink(fn);
  2635. srv_debug(2, xs_fmt("purged %s", fn));
  2636. }
  2637. }
  2638. }
  2639. }
  2640. }
  2641. #endif /* #ifndef NO_MASTODON_API */