mastoapi.c 109 KB

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