mastoapi.c 117 KB

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