mastoapi.c 111 KB

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