mastoapi.c 112 KB

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