mastoapi.c 124 KB

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