mastoapi.c 110 KB

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