mastoapi.c 128 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838
  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 * const 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 *b_id = xs_toupper_i(xs_dup(xs_dict_get(st, "id")));
  884. bst = xs_dict_set(bst, "id", b_id);
  885. xs_free(st);
  886. st = bst;
  887. }
  888. else
  889. xs_free(bst);
  890. }
  891. return st;
  892. }
  893. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  894. {
  895. xs_dict *rel = NULL;
  896. xs *actor_o = NULL;
  897. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  898. rel = xs_dict_new();
  899. const char *actor = xs_dict_get(actor_o, "id");
  900. rel = xs_dict_append(rel, "id", md5);
  901. rel = xs_dict_append(rel, "following",
  902. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  903. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  904. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  905. rel = xs_dict_append(rel, "followed_by",
  906. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  907. rel = xs_dict_append(rel, "blocking",
  908. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  909. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  910. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  911. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  912. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  913. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  914. rel = xs_dict_append(rel, "note", "");
  915. }
  916. return rel;
  917. }
  918. int process_auth_token(snac *snac, const xs_dict *req)
  919. /* processes an authorization token, if there is one */
  920. {
  921. int logged_in = 0;
  922. const char *v;
  923. /* if there is an authorization field, try to validate it */
  924. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  925. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  926. xs *token = token_get(tokid);
  927. if (token != NULL) {
  928. const char *uid = xs_dict_get(token, "uid");
  929. if (!xs_is_null(uid) && user_open(snac, uid)) {
  930. logged_in = 1;
  931. /* this counts as a 'login' */
  932. lastlog_write(snac, "mastoapi");
  933. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  934. }
  935. else
  936. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  937. }
  938. else
  939. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  940. }
  941. return logged_in;
  942. }
  943. void credentials_get(char **body, char **ctype, int *status, snac snac)
  944. {
  945. xs *acct = xs_dict_new();
  946. const xs_val *bot = xs_dict_get(snac.config, "bot");
  947. acct = xs_dict_append(acct, "id", snac.md5);
  948. acct = xs_dict_append(acct, "username", xs_dict_get(snac.config, "uid"));
  949. acct = xs_dict_append(acct, "acct", xs_dict_get(snac.config, "uid"));
  950. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac.config, "name"));
  951. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac.config, "published"));
  952. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac.config, "published"));
  953. acct = xs_dict_append(acct, "note", xs_dict_get(snac.config, "bio"));
  954. acct = xs_dict_append(acct, "url", snac.actor);
  955. acct = xs_dict_append(acct, "locked",
  956. xs_stock(xs_is_true(xs_dict_get(snac.config, "approve_followers")) ? XSTYPE_TRUE : XSTYPE_FALSE));
  957. acct = xs_dict_append(acct, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  958. acct = xs_dict_append(acct, "emojis", xs_stock(XSTYPE_LIST));
  959. xs *src = xs_json_loads("{\"privacy\":\"public\", \"language\":\"en\","
  960. "\"follow_requests_count\": 0,"
  961. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  962. /* some apps take the note from the source object */
  963. src = xs_dict_set(src, "note", xs_dict_get(snac.config, "bio"));
  964. src = xs_dict_set(src, "privacy", xs_type(xs_dict_get(snac.config, "private")) == XSTYPE_TRUE ? "private" : "public");
  965. const xs_str *cw = xs_dict_get(snac.config, "cw");
  966. src = xs_dict_set(src, "sensitive",
  967. strcmp(cw, "open") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  968. src = xs_dict_set(src, "bot", xs_stock(xs_is_true(bot) ? XSTYPE_TRUE : XSTYPE_FALSE));
  969. xs *avatar = NULL;
  970. const char *av = xs_dict_get(snac.config, "avatar");
  971. if (xs_is_null(av) || *av == '\0')
  972. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  973. else
  974. avatar = xs_dup(av);
  975. acct = xs_dict_append(acct, "avatar", avatar);
  976. acct = xs_dict_append(acct, "avatar_static", avatar);
  977. xs *header = NULL;
  978. const char *hd = xs_dict_get(snac.config, "header");
  979. if (!xs_is_null(hd))
  980. header = xs_dup(hd);
  981. else
  982. header = xs_str_new(NULL);
  983. acct = xs_dict_append(acct, "header", header);
  984. acct = xs_dict_append(acct, "header_static", header);
  985. xs *metadata = NULL;
  986. const xs_dict *md = xs_dict_get(snac.config, "metadata");
  987. if (xs_is_dict(md))
  988. metadata = xs_dup(md);
  989. else
  990. if (xs_is_string(md)) {
  991. metadata = xs_dict_new();
  992. xs *l = xs_split(md, "\n");
  993. const char *ll;
  994. xs_list_foreach(l, ll) {
  995. xs *kv = xs_split_n(ll, "=", 1);
  996. const char *k = xs_list_get(kv, 0);
  997. const char *v = xs_list_get(kv, 1);
  998. if (k && v) {
  999. xs *kk = xs_strip_i(xs_dup(k));
  1000. xs *vv = xs_strip_i(xs_dup(v));
  1001. metadata = xs_dict_set(metadata, kk, vv);
  1002. }
  1003. }
  1004. }
  1005. if (xs_is_dict(metadata)) {
  1006. xs *fields = xs_list_new();
  1007. const xs_str *k;
  1008. const xs_str *v;
  1009. xs_dict *val_links = snac.links;
  1010. if (xs_is_null(val_links))
  1011. val_links = xs_stock(XSTYPE_DICT);
  1012. int c = 0;
  1013. while (xs_dict_next(metadata, &k, &v, &c)) {
  1014. xs *val_date = NULL;
  1015. const xs_number *verified_time = xs_dict_get(val_links, v);
  1016. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  1017. time_t t = xs_number_get(verified_time);
  1018. if (t > 0)
  1019. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  1020. }
  1021. xs *d = xs_dict_new();
  1022. d = xs_dict_append(d, "name", k);
  1023. d = xs_dict_append(d, "value", v);
  1024. d = xs_dict_append(d, "verified_at",
  1025. xs_type(val_date) == XSTYPE_STRING && *val_date ? val_date : xs_stock(XSTYPE_NULL));
  1026. fields = xs_list_append(fields, d);
  1027. }
  1028. acct = xs_dict_set(acct, "fields", fields);
  1029. /* some apps take the fields from the source object */
  1030. src = xs_dict_set(src, "fields", fields);
  1031. }
  1032. acct = xs_dict_append(acct, "source", src);
  1033. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  1034. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  1035. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  1036. /* does this user want to publish their contact metrics? */
  1037. if (xs_is_true(xs_dict_get(snac.config, "show_contact_metrics"))) {
  1038. int fwing = following_list_len(&snac);
  1039. int fwers = follower_list_len(&snac);
  1040. xs *ni = xs_number_new(fwing);
  1041. xs *ne = xs_number_new(fwers);
  1042. acct = xs_dict_append(acct, "followers_count", ne);
  1043. acct = xs_dict_append(acct, "following_count", ni);
  1044. }
  1045. *body = xs_json_dumps(acct, 4);
  1046. *ctype = "application/json";
  1047. *status = HTTP_STATUS_OK;
  1048. }
  1049. xs_list *mastoapi_timeline(snac *user, const xs_dict *args, const char *index_fn)
  1050. {
  1051. xs_list *out = xs_list_new();
  1052. FILE *f;
  1053. char md5[MD5_HEX_SIZE];
  1054. if (dbglevel) {
  1055. xs *js = xs_json_dumps(args, 0);
  1056. srv_debug(1, xs_fmt("mastoapi_timeline args %s", js));
  1057. }
  1058. if ((f = fopen(index_fn, "r")) == NULL)
  1059. return out;
  1060. const char *o_max_id = xs_dict_get(args, "max_id");
  1061. const char *o_since_id = xs_dict_get(args, "since_id");
  1062. const char *o_min_id = xs_dict_get(args, "min_id"); /* unsupported old-to-new navigation */
  1063. const char *limit_s = xs_dict_get(args, "limit");
  1064. int (*iterator)(FILE *, char *);
  1065. int initial_status = 0;
  1066. int ascending = 0;
  1067. int limit = 0;
  1068. int cnt = 0;
  1069. xs *max_id = o_max_id ? xs_tolower_i(xs_dup(o_max_id)) : NULL;
  1070. xs *since_id = o_since_id ? xs_tolower_i(xs_dup(o_since_id)) : NULL;
  1071. xs *min_id = o_min_id ? xs_tolower_i(xs_dup(o_min_id)) : NULL;
  1072. if (!xs_is_null(limit_s))
  1073. limit = atoi(limit_s);
  1074. if (limit == 0)
  1075. limit = 20;
  1076. if (min_id) {
  1077. iterator = &index_asc_next;
  1078. initial_status = index_asc_first(f, md5, MID_TO_MD5(min_id));
  1079. ascending = 1;
  1080. }
  1081. else {
  1082. iterator = &index_desc_next;
  1083. initial_status = index_desc_first(f, md5, 0);
  1084. }
  1085. xs_set entries;
  1086. xs_set_init(&entries);
  1087. if (initial_status) {
  1088. do {
  1089. xs *msg = NULL;
  1090. /* only return entries older that max_id */
  1091. if (max_id) {
  1092. if (strcmp(md5, MID_TO_MD5(max_id)) == 0) {
  1093. max_id = xs_free(max_id);
  1094. if (ascending)
  1095. break;
  1096. }
  1097. if (!ascending)
  1098. continue;
  1099. }
  1100. /* only returns entries newer than since_id */
  1101. if (since_id) {
  1102. if (strcmp(md5, MID_TO_MD5(since_id)) == 0) {
  1103. if (!ascending)
  1104. break;
  1105. since_id = xs_free(since_id);
  1106. }
  1107. if (ascending)
  1108. continue;
  1109. }
  1110. /* get the entry */
  1111. if (user) {
  1112. if (!valid_status(timeline_get_by_md5(user, md5, &msg)))
  1113. continue;
  1114. }
  1115. else {
  1116. if (!valid_status(object_get_by_md5(md5, &msg)))
  1117. continue;
  1118. }
  1119. /* discard non-Notes */
  1120. const char *id = xs_dict_get(msg, "id");
  1121. const char *type = xs_dict_get(msg, "type");
  1122. if (!xs_match(type, POSTLIKE_OBJECT_TYPE))
  1123. continue;
  1124. if (id && is_instance_blocked(id))
  1125. continue;
  1126. const char *from = NULL;
  1127. if (strcmp(type, "Page") == 0)
  1128. from = xs_dict_get(msg, "audience");
  1129. if (from == NULL)
  1130. from = get_atto(msg);
  1131. if (from == NULL)
  1132. continue;
  1133. if (user) {
  1134. /* is this message from a person we don't follow? */
  1135. if (strcmp(from, user->actor) && !following_check(user, from)) {
  1136. /* discard if it was not boosted */
  1137. xs *idx = object_announces(id);
  1138. if (xs_list_len(idx) == 0)
  1139. continue;
  1140. }
  1141. /* discard notes from muted morons */
  1142. if (is_muted(user, from))
  1143. continue;
  1144. /* discard hidden notes */
  1145. if (is_hidden(user, id))
  1146. continue;
  1147. }
  1148. else {
  1149. /* skip non-public messages */
  1150. if (!is_msg_public(msg))
  1151. continue;
  1152. /* discard messages from private users */
  1153. if (is_msg_from_private_user(msg))
  1154. continue;
  1155. }
  1156. /* if it has a name and it's not an object that may have one,
  1157. it's a poll vote, so discard it */
  1158. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video|Audio|Event"))
  1159. continue;
  1160. /* convert the Note into a Mastodon status */
  1161. xs *st = mastoapi_status(user, msg);
  1162. if (st != NULL && xs_set_add(&entries, md5) == 1) {
  1163. if (ascending)
  1164. out = xs_list_insert(out, 0, st);
  1165. else
  1166. out = xs_list_append(out, st);
  1167. cnt++;
  1168. }
  1169. } while ((cnt < limit) && (*iterator)(f, md5));
  1170. }
  1171. xs_set_free(&entries);
  1172. int more = index_desc_next(f, md5);
  1173. fclose(f);
  1174. srv_debug(1, xs_fmt("mastoapi_timeline ret %d%s", cnt, more ? " (+)" : ""));
  1175. return out;
  1176. }
  1177. xs_str *timeline_link_header(const char *endpoint, xs_list *timeline)
  1178. /* returns a Link header with paging information */
  1179. {
  1180. xs_str *s = NULL;
  1181. if (xs_list_len(timeline) == 0)
  1182. return NULL;
  1183. const xs_dict *first_st = xs_list_get(timeline, 0);
  1184. const xs_dict *last_st = xs_list_get(timeline, -1);
  1185. const char *first_id = xs_dict_get(first_st, "id");
  1186. const char *last_id = xs_dict_get(last_st, "id");
  1187. const char *host = xs_dict_get(srv_config, "host");
  1188. const char *protocol = xs_dict_get_def(srv_config, "protocol", "https");
  1189. s = xs_fmt(
  1190. "<%s:/" "/%s%s?max_id=%s>; rel=\"next\", "
  1191. "<%s:/" "/%s%s?since_id=%s>; rel=\"prev\"",
  1192. protocol, host, endpoint, last_id,
  1193. protocol, host, endpoint, first_id);
  1194. srv_debug(1, xs_fmt("timeline_link_header %s", s));
  1195. return s;
  1196. }
  1197. xs_list *mastoapi_account_lists(snac *user, const char *uid)
  1198. /* returns the list of list an user is in */
  1199. {
  1200. xs_list *out = xs_list_new();
  1201. xs *actor_md5 = NULL;
  1202. xs *lol = list_maint(user, NULL, 0);
  1203. if (uid) {
  1204. if (!xs_is_hex(uid))
  1205. actor_md5 = xs_md5_hex(uid, strlen(uid));
  1206. else
  1207. actor_md5 = xs_dup(uid);
  1208. }
  1209. const xs_list *li;
  1210. xs_list_foreach(lol, li) {
  1211. const char *list_id = xs_list_get(li, 0);
  1212. const char *list_title = xs_list_get(li, 1);
  1213. if (uid) {
  1214. xs *users = list_members(user, list_id, NULL, 0);
  1215. if (xs_list_in(users, actor_md5) == -1)
  1216. continue;
  1217. }
  1218. xs *d = xs_dict_new();
  1219. d = xs_dict_append(d, "id", list_id);
  1220. d = xs_dict_append(d, "title", list_title);
  1221. d = xs_dict_append(d, "replies_policy", "list");
  1222. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1223. out = xs_list_append(out, d);
  1224. }
  1225. return out;
  1226. }
  1227. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  1228. char **body, int *b_size, char **ctype, xs_str **link)
  1229. {
  1230. (void)b_size;
  1231. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1232. return 0;
  1233. int status = HTTP_STATUS_NOT_FOUND;
  1234. const xs_dict *args = xs_dict_get(req, "q_vars");
  1235. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1236. snac snac1 = {0};
  1237. int logged_in = process_auth_token(&snac1, req);
  1238. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  1239. if (logged_in) {
  1240. credentials_get(body, ctype, &status, snac1);
  1241. }
  1242. else {
  1243. status = HTTP_STATUS_UNPROCESSABLE_CONTENT; // (no login)
  1244. }
  1245. }
  1246. else
  1247. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  1248. /* find if an account is followed, blocked, etc. */
  1249. /* the account to get relationships about is in args "id[]" */
  1250. if (logged_in) {
  1251. xs *res = xs_list_new();
  1252. const char *md5 = xs_dict_get(args, "id[]");
  1253. if (xs_is_null(md5))
  1254. md5 = xs_dict_get(args, "id");
  1255. if (!xs_is_null(md5)) {
  1256. if (xs_type(md5) == XSTYPE_LIST)
  1257. md5 = xs_list_get(md5, 0);
  1258. xs *rel = mastoapi_relationship(&snac1, md5);
  1259. if (rel != NULL)
  1260. res = xs_list_append(res, rel);
  1261. }
  1262. *body = xs_json_dumps(res, 4);
  1263. *ctype = "application/json";
  1264. status = HTTP_STATUS_OK;
  1265. }
  1266. else
  1267. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  1268. }
  1269. else
  1270. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  1271. /* lookup an account */
  1272. const char *acct = xs_dict_get(args, "acct");
  1273. if (!xs_is_null(acct)) {
  1274. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  1275. xs *l = xs_split_n(s, "@", 1);
  1276. const char *uid = xs_list_get(l, 0);
  1277. const char *host = xs_list_get(l, 1);
  1278. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  1279. snac user;
  1280. if (user_open(&user, uid)) {
  1281. xs *actor = msg_actor(&user);
  1282. xs *macct = mastoapi_account(NULL, actor);
  1283. *body = xs_json_dumps(macct, 4);
  1284. *ctype = "application/json";
  1285. status = HTTP_STATUS_OK;
  1286. user_free(&user);
  1287. }
  1288. }
  1289. }
  1290. }
  1291. else
  1292. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  1293. /* account-related information */
  1294. xs *l = xs_split(cmd, "/");
  1295. const char *uid = xs_list_get(l, 3);
  1296. const char *opt = xs_list_get(l, 4);
  1297. if (uid != NULL) {
  1298. snac snac2;
  1299. xs *out = NULL;
  1300. xs *actor = NULL;
  1301. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1302. /* search for accounts starting with q */
  1303. const char *aq = xs_dict_get(args, "q");
  1304. if (!xs_is_null(aq)) {
  1305. xs *q = xs_utf8_to_lower(aq);
  1306. out = xs_list_new();
  1307. xs *wing = following_list(&snac1);
  1308. xs *wers = follower_list(&snac1);
  1309. xs *ulst = user_list();
  1310. xs_list *p;
  1311. const xs_str *v;
  1312. xs_set seen;
  1313. xs_set_init(&seen);
  1314. /* user relations */
  1315. xs_list *lsts[] = { wing, wers, NULL };
  1316. int n;
  1317. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1318. while (xs_list_iter(&p, &v)) {
  1319. /* already seen? skip */
  1320. if (xs_set_add(&seen, v) == 0)
  1321. continue;
  1322. xs *actor = NULL;
  1323. if (valid_status(object_get(v, &actor))) {
  1324. const char *uname = xs_dict_get(actor, "preferredUsername");
  1325. if (!xs_is_null(uname)) {
  1326. xs *luname = xs_tolower_i(xs_dup(uname));
  1327. if (xs_startswith(luname, q)) {
  1328. xs *acct = mastoapi_account(&snac1, actor);
  1329. out = xs_list_append(out, acct);
  1330. }
  1331. }
  1332. }
  1333. }
  1334. }
  1335. /* local users */
  1336. p = ulst;
  1337. while (xs_list_iter(&p, &v)) {
  1338. snac user;
  1339. /* skip this same user */
  1340. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1341. continue;
  1342. /* skip if the uid does not start with the query */
  1343. xs *v2 = xs_tolower_i(xs_dup(v));
  1344. if (!xs_startswith(v2, q))
  1345. continue;
  1346. if (user_open(&user, v)) {
  1347. /* if it's not already seen, add it */
  1348. if (xs_set_add(&seen, user.actor) == 1) {
  1349. xs *actor = msg_actor(&user);
  1350. xs *acct = mastoapi_account(&snac1, actor);
  1351. out = xs_list_append(out, acct);
  1352. }
  1353. user_free(&user);
  1354. }
  1355. }
  1356. xs_set_free(&seen);
  1357. }
  1358. }
  1359. else
  1360. /* is it a local user? */
  1361. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1362. if (opt == NULL) {
  1363. /* account information */
  1364. actor = msg_actor(&snac2);
  1365. out = mastoapi_account(NULL, actor);
  1366. }
  1367. else
  1368. if (strcmp(opt, "statuses") == 0) { /** **/
  1369. /* the public list of posts of a user */
  1370. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256, NULL);
  1371. xs_list *p = timeline;
  1372. const xs_str *v;
  1373. out = xs_list_new();
  1374. while (xs_list_iter(&p, &v)) {
  1375. xs *msg = NULL;
  1376. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1377. /* add only posts by the author */
  1378. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1379. xs_startswith(xs_dict_get(msg, "id"), snac2.actor) && is_msg_public(msg)) {
  1380. xs *st = mastoapi_status(&snac2, msg);
  1381. if (st)
  1382. out = xs_list_append(out, st);
  1383. }
  1384. }
  1385. }
  1386. }
  1387. else
  1388. if (strcmp(opt, "featured_tags") == 0) {
  1389. /* snac doesn't have features tags, yet? */
  1390. /* implement empty response so apps like Tokodon don't show an error */
  1391. out = xs_list_new();
  1392. }
  1393. else
  1394. if (strcmp(opt, "following") == 0) {
  1395. xs *wing = following_list(&snac1);
  1396. out = xs_list_new();
  1397. int c = 0;
  1398. const char *v;
  1399. while (xs_list_next(wing, &v, &c)) {
  1400. xs *actor = NULL;
  1401. if (valid_status(object_get(v, &actor))) {
  1402. xs *acct = mastoapi_account(NULL, actor);
  1403. out = xs_list_append(out, acct);
  1404. }
  1405. }
  1406. }
  1407. else
  1408. if (strcmp(opt, "followers") == 0) {
  1409. out = xs_list_new();
  1410. }
  1411. else
  1412. if (strcmp(opt, "lists") == 0) {
  1413. out = mastoapi_account_lists(&snac1, uid);
  1414. }
  1415. user_free(&snac2);
  1416. }
  1417. else {
  1418. /* try the uid as the md5 of a possibly loaded actor */
  1419. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1420. if (opt == NULL) {
  1421. /* account information */
  1422. out = mastoapi_account(&snac1, actor);
  1423. }
  1424. else
  1425. if (strcmp(opt, "statuses") == 0) {
  1426. /* we don't serve statuses of others; return the empty list */
  1427. out = xs_list_new();
  1428. }
  1429. else
  1430. if (strcmp(opt, "featured_tags") == 0) {
  1431. /* snac doesn't have features tags, yet? */
  1432. /* implement empty response so apps like Tokodon don't show an error */
  1433. out = xs_list_new();
  1434. }
  1435. else
  1436. if (strcmp(opt, "lists") == 0) {
  1437. out = mastoapi_account_lists(&snac1, uid);
  1438. }
  1439. }
  1440. }
  1441. if (out != NULL) {
  1442. *body = xs_json_dumps(out, 4);
  1443. *ctype = "application/json";
  1444. status = HTTP_STATUS_OK;
  1445. }
  1446. }
  1447. }
  1448. else
  1449. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1450. /* the private timeline */
  1451. if (logged_in) {
  1452. xs *ifn = user_index_fn(&snac1, "private");
  1453. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1454. *link = timeline_link_header("/api/v1/timelines/home", out);
  1455. *body = xs_json_dumps(out, 4);
  1456. *ctype = "application/json";
  1457. status = HTTP_STATUS_OK;
  1458. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1459. }
  1460. else {
  1461. status = HTTP_STATUS_UNAUTHORIZED;
  1462. }
  1463. }
  1464. else
  1465. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1466. /* the instance public timeline (public timelines for all users) */
  1467. xs *ifn = instance_index_fn();
  1468. xs *out = mastoapi_timeline(NULL, args, ifn);
  1469. *body = xs_json_dumps(out, 4);
  1470. *ctype = "application/json";
  1471. status = HTTP_STATUS_OK;
  1472. }
  1473. else
  1474. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1475. /* get the tag */
  1476. xs *l = xs_split(cmd, "/");
  1477. const char *tag = xs_list_get(l, -1);
  1478. xs *ifn = tag_fn(tag);
  1479. xs *out = mastoapi_timeline(NULL, args, ifn);
  1480. *body = xs_json_dumps(out, 4);
  1481. *ctype = "application/json";
  1482. status = HTTP_STATUS_OK;
  1483. }
  1484. else
  1485. if (xs_startswith(cmd, "/v1/timelines/list/")) { /** **/
  1486. /* get the list id */
  1487. if (logged_in) {
  1488. xs *l = xs_split(cmd, "/");
  1489. const char *list = xs_list_get(l, -1);
  1490. xs *ifn = list_timeline_fn(&snac1, list);
  1491. xs *out = mastoapi_timeline(NULL, args, ifn);
  1492. *body = xs_json_dumps(out, 4);
  1493. *ctype = "application/json";
  1494. status = HTTP_STATUS_OK;
  1495. }
  1496. else
  1497. status = HTTP_STATUS_MISDIRECTED_REQUEST;
  1498. }
  1499. else
  1500. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1501. /* TBD */
  1502. *body = xs_dup("[]");
  1503. *ctype = "application/json";
  1504. status = HTTP_STATUS_OK;
  1505. }
  1506. else
  1507. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1508. if (logged_in) {
  1509. xs *l = notify_list(&snac1, 0, 64);
  1510. xs *out = xs_list_new();
  1511. const char *v;
  1512. const xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1513. const xs_list *incl = xs_dict_get(args, "types[]");
  1514. const char *min_id = xs_dict_get(args, "min_id");
  1515. const char *max_id = xs_dict_get(args, "max_id");
  1516. const char *limit = xs_dict_get(args, "limit");
  1517. int limit_count = 0;
  1518. if (xs_is_string(limit)) {
  1519. limit_count = atoi(limit);
  1520. }
  1521. if (dbglevel) {
  1522. xs *js = xs_json_dumps(args, 0);
  1523. srv_debug(1, xs_fmt("mastoapi_notifications args %s", js));
  1524. }
  1525. xs_list_foreach(l, v) {
  1526. xs *noti = notify_get(&snac1, v);
  1527. if (noti == NULL)
  1528. continue;
  1529. const char *type = xs_dict_get(noti, "type");
  1530. const char *utype = xs_dict_get(noti, "utype");
  1531. const char *objid = xs_dict_get(noti, "objid");
  1532. const char *id = xs_dict_get(noti, "id");
  1533. const char *actid = xs_dict_get(noti, "actor");
  1534. xs *fid = xs_replace(id, ".", "");
  1535. xs *actor = NULL;
  1536. xs *entry = NULL;
  1537. if (!valid_status(actor_get(actid, &actor)))
  1538. continue;
  1539. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1540. continue;
  1541. if (is_hidden(&snac1, objid))
  1542. continue;
  1543. if (max_id) {
  1544. if (strcmp(fid, max_id) == 0)
  1545. max_id = NULL;
  1546. continue;
  1547. }
  1548. if (min_id) {
  1549. if (strcmp(fid, min_id) <= 0) {
  1550. continue;
  1551. }
  1552. }
  1553. /* convert the type */
  1554. if (strcmp(type, "Like") == 0 || strcmp(type, "EmojiReact") == 0)
  1555. type = "favourite";
  1556. else
  1557. if (strcmp(type, "Announce") == 0)
  1558. type = "reblog";
  1559. else
  1560. if (strcmp(type, "Follow") == 0)
  1561. type = "follow";
  1562. else
  1563. if (strcmp(type, "Create") == 0)
  1564. type = "mention";
  1565. else
  1566. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1567. type = "poll";
  1568. else
  1569. continue;
  1570. /* excluded type? */
  1571. if (xs_is_list(excl) && xs_list_in(excl, type) != -1)
  1572. continue;
  1573. /* included type? */
  1574. if (xs_is_list(incl) && xs_list_in(incl, type) == -1)
  1575. continue;
  1576. xs *mn = xs_dict_new();
  1577. mn = xs_dict_append(mn, "type", type);
  1578. mn = xs_dict_append(mn, "id", fid);
  1579. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1580. xs *acct = mastoapi_account(&snac1, actor);
  1581. if (acct == NULL)
  1582. continue;
  1583. mn = xs_dict_append(mn, "account", acct);
  1584. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1585. xs *st = mastoapi_status(&snac1, entry);
  1586. if (st)
  1587. mn = xs_dict_append(mn, "status", st);
  1588. }
  1589. out = xs_list_append(out, mn);
  1590. if (--limit_count <= 0)
  1591. break;
  1592. }
  1593. srv_debug(1, xs_fmt("mastoapi_notifications count %d", xs_list_len(out)));
  1594. *body = xs_json_dumps(out, 4);
  1595. *ctype = "application/json";
  1596. status = HTTP_STATUS_OK;
  1597. }
  1598. else
  1599. status = HTTP_STATUS_UNAUTHORIZED;
  1600. }
  1601. else
  1602. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1603. /* snac will never have filters */
  1604. *body = xs_dup("[]");
  1605. *ctype = "application/json";
  1606. status = HTTP_STATUS_OK;
  1607. }
  1608. else
  1609. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1610. /* snac will never have filters
  1611. * but still, without a v2 endpoint a short delay is introduced
  1612. * in some apps */
  1613. *body = xs_dup("[]");
  1614. *ctype = "application/json";
  1615. status = HTTP_STATUS_OK;
  1616. }
  1617. else
  1618. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1619. /* snac will never support a list of favourites */
  1620. *body = xs_dup("[]");
  1621. *ctype = "application/json";
  1622. status = HTTP_STATUS_OK;
  1623. }
  1624. else
  1625. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1626. if (logged_in) {
  1627. xs *ifn = bookmark_index_fn(&snac1);
  1628. xs *out = mastoapi_timeline(&snac1, args, ifn);
  1629. *body = xs_json_dumps(out, 4);
  1630. *ctype = "application/json";
  1631. status = HTTP_STATUS_OK;
  1632. }
  1633. else
  1634. status = HTTP_STATUS_UNAUTHORIZED;
  1635. }
  1636. else
  1637. if (strcmp(cmd, "/v1/lists") == 0) { /** list of lists **/
  1638. if (logged_in) {
  1639. xs *l = mastoapi_account_lists(&snac1, NULL);
  1640. *body = xs_json_dumps(l, 4);
  1641. *ctype = "application/json";
  1642. status = HTTP_STATUS_OK;
  1643. }
  1644. else
  1645. status = HTTP_STATUS_UNAUTHORIZED;
  1646. }
  1647. else
  1648. if (xs_startswith(cmd, "/v1/lists/")) { /** list information **/
  1649. if (logged_in) {
  1650. xs *l = xs_split(cmd, "/");
  1651. const char *p = xs_list_get(l, -1);
  1652. if (p) {
  1653. if (strcmp(p, "accounts") == 0) {
  1654. p = xs_list_get(l, -2);
  1655. if (p && xs_is_hex(p)) {
  1656. xs *actors = list_members(&snac1, p, NULL, 0);
  1657. xs *out = xs_list_new();
  1658. int c = 0;
  1659. const char *v;
  1660. while (xs_list_next(actors, &v, &c)) {
  1661. xs *actor = NULL;
  1662. if (valid_status(object_get_by_md5(v, &actor))) {
  1663. xs *acct = mastoapi_account(&snac1, actor);
  1664. out = xs_list_append(out, acct);
  1665. }
  1666. }
  1667. *body = xs_json_dumps(out, 4);
  1668. *ctype = "application/json";
  1669. status = HTTP_STATUS_OK;
  1670. }
  1671. }
  1672. else
  1673. if (xs_is_hex(p)) {
  1674. xs *out = xs_list_new();
  1675. xs *lol = list_maint(&snac1, NULL, 0);
  1676. int c = 0;
  1677. const xs_list *v;
  1678. while (xs_list_next(lol, &v, &c)) {
  1679. const char *id = xs_list_get(v, 0);
  1680. if (id && strcmp(id, p) == 0) {
  1681. xs *d = xs_dict_new();
  1682. d = xs_dict_append(d, "id", p);
  1683. d = xs_dict_append(d, "title", xs_list_get(v, 1));
  1684. d = xs_dict_append(d, "replies_policy", "list");
  1685. d = xs_dict_append(d, "exclusive", xs_stock(XSTYPE_FALSE));
  1686. out = xs_dup(d);
  1687. break;
  1688. }
  1689. }
  1690. *body = xs_json_dumps(out, 4);
  1691. *ctype = "application/json";
  1692. status = HTTP_STATUS_OK;
  1693. }
  1694. }
  1695. }
  1696. else
  1697. status = HTTP_STATUS_UNAUTHORIZED;
  1698. }
  1699. else
  1700. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1701. /* TBD */
  1702. *body = xs_dup("[]");
  1703. *ctype = "application/json";
  1704. status = HTTP_STATUS_OK;
  1705. }
  1706. else
  1707. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1708. if (logged_in) {
  1709. xs *pend = pending_list(&snac1);
  1710. xs *resp = xs_list_new();
  1711. const char *id;
  1712. xs_list_foreach(pend, id) {
  1713. xs *actor = NULL;
  1714. if (valid_status(object_get(id, &actor))) {
  1715. xs *acct = mastoapi_account(&snac1, actor);
  1716. if (acct)
  1717. resp = xs_list_append(resp, acct);
  1718. }
  1719. }
  1720. *body = xs_json_dumps(resp, 4);
  1721. *ctype = "application/json";
  1722. status = HTTP_STATUS_OK;
  1723. }
  1724. }
  1725. else
  1726. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1727. if (logged_in) {
  1728. xs *resp = xs_list_new();
  1729. double la = 0.0;
  1730. xs *user_la = xs_dup(xs_dict_get(snac1.config, "last_announcement"));
  1731. if (user_la != NULL)
  1732. la = xs_number_get(user_la);
  1733. xs *val_date = xs_str_utctime(la, ISO_DATE_SPEC);
  1734. /* contrary to html, we always send the announcement and set the read flag instead */
  1735. const t_announcement *annce = announcement(la);
  1736. if (annce != NULL && annce->text != NULL) {
  1737. xs *an = xs_dict_new();
  1738. xs *id = xs_fmt("%d", annce->timestamp);
  1739. xs *ct = xs_fmt("<p>%s</p>", annce->text);
  1740. an = xs_dict_set(an, "id", id);
  1741. an = xs_dict_set(an, "content", ct);
  1742. an = xs_dict_set(an, "starts_at", xs_stock(XSTYPE_NULL));
  1743. an = xs_dict_set(an, "ends_at", xs_stock(XSTYPE_NULL));
  1744. an = xs_dict_set(an, "all_day", xs_stock(XSTYPE_TRUE));
  1745. an = xs_dict_set(an, "published_at", val_date);
  1746. an = xs_dict_set(an, "updated_at", val_date);
  1747. an = xs_dict_set(an, "read", (annce->timestamp >= la)
  1748. ? xs_stock(XSTYPE_FALSE) : xs_stock(XSTYPE_TRUE));
  1749. an = xs_dict_set(an, "mentions", xs_stock(XSTYPE_LIST));
  1750. an = xs_dict_set(an, "statuses", xs_stock(XSTYPE_LIST));
  1751. an = xs_dict_set(an, "tags", xs_stock(XSTYPE_LIST));
  1752. an = xs_dict_set(an, "emojis", xs_stock(XSTYPE_LIST));
  1753. an = xs_dict_set(an, "reactions", xs_stock(XSTYPE_LIST));
  1754. resp = xs_list_append(resp, an);
  1755. }
  1756. *body = xs_json_dumps(resp, 4);
  1757. *ctype = "application/json";
  1758. status = HTTP_STATUS_OK;
  1759. }
  1760. }
  1761. else
  1762. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1763. xs *emo = emojis();
  1764. xs *list = xs_list_new();
  1765. int c = 0;
  1766. const xs_str *k;
  1767. const xs_val *v;
  1768. while(xs_dict_next(emo, &k, &v, &c)) {
  1769. xs *current = xs_dict_new();
  1770. if (xs_startswith(v, "https://") && xs_startswith((xs_mime_by_ext(v)), "image/")) {
  1771. /* remove first and last colon */
  1772. xs *shortcode = xs_replace(k, ":", "");
  1773. current = xs_dict_append(current, "shortcode", shortcode);
  1774. current = xs_dict_append(current, "url", v);
  1775. current = xs_dict_append(current, "static_url", v);
  1776. current = xs_dict_append(current, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  1777. list = xs_list_append(list, current);
  1778. }
  1779. }
  1780. *body = xs_json_dumps(list, 0);
  1781. *ctype = "application/json";
  1782. status = HTTP_STATUS_OK;
  1783. }
  1784. else
  1785. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1786. /* returns an instance object */
  1787. xs *ins = xs_dict_new();
  1788. const char *host = xs_dict_get(srv_config, "host");
  1789. const char *title = xs_dict_get(srv_config, "title");
  1790. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1791. ins = xs_dict_append(ins, "uri", host);
  1792. ins = xs_dict_append(ins, "domain", host);
  1793. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1794. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1795. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1796. ins = xs_dict_append(ins, "description", host);
  1797. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1798. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1799. ins = xs_dict_append(ins, "thumbnail", susie);
  1800. const char *v = xs_dict_get(srv_config, "admin_email");
  1801. if (xs_is_null(v) || *v == '\0')
  1802. v = "admin@localhost";
  1803. ins = xs_dict_append(ins, "email", v);
  1804. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1805. xs *l1 = xs_list_append(xs_list_new(), "en");
  1806. ins = xs_dict_append(ins, "languages", l1);
  1807. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1808. xs *urls = xs_dict_new();
  1809. urls = xs_dict_append(urls, "streaming_api", wss);
  1810. ins = xs_dict_append(ins, "urls", urls);
  1811. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1812. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1813. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1814. ins = xs_dict_append(ins, "stats", d2);
  1815. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1816. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1817. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1818. xs *cfg = xs_dict_new();
  1819. {
  1820. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1821. "\"max_characters\":100000,\"max_media_attachments\":4}");
  1822. const xs_number *max_attachments = xs_dict_get(srv_config, "max_attachments");
  1823. if (xs_type(max_attachments) == XSTYPE_NUMBER)
  1824. d11 = xs_dict_set(d11, "max_media_attachments", max_attachments);
  1825. cfg = xs_dict_append(cfg, "statuses", d11);
  1826. xs *d12 = xs_json_loads("{\"max_featured_tags\":0}");
  1827. cfg = xs_dict_append(cfg, "accounts", d12);
  1828. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1829. "\"image_size_limit\":16777216,"
  1830. "\"video_frame_rate_limit\":120,"
  1831. "\"video_matrix_limit\":8294400,"
  1832. "\"video_size_limit\":103809024}"
  1833. );
  1834. {
  1835. /* get the supported mime types from the internal list */
  1836. const char **p = xs_mime_types;
  1837. xs_set mtypes;
  1838. xs_set_init(&mtypes);
  1839. while (*p) {
  1840. const char *type = p[1];
  1841. if (xs_startswith(type, "image/") ||
  1842. xs_startswith(type, "video/") ||
  1843. xs_startswith(type, "audio/"))
  1844. xs_set_add(&mtypes, type);
  1845. p += 2;
  1846. }
  1847. xs *l = xs_set_result(&mtypes);
  1848. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1849. }
  1850. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1851. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1852. "\"max_expiration\":2629746,"
  1853. "\"max_options\":8,\"min_expiration\":300}");
  1854. cfg = xs_dict_append(cfg, "polls", d14);
  1855. }
  1856. ins = xs_dict_append(ins, "configuration", cfg);
  1857. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1858. if (!xs_is_null(admin_account) && *admin_account) {
  1859. snac admin;
  1860. if (user_open(&admin, admin_account)) {
  1861. xs *actor = msg_actor(&admin);
  1862. xs *acct = mastoapi_account(NULL, actor);
  1863. ins = xs_dict_append(ins, "contact_account", acct);
  1864. user_free(&admin);
  1865. }
  1866. }
  1867. *body = xs_json_dumps(ins, 4);
  1868. *ctype = "application/json";
  1869. status = HTTP_STATUS_OK;
  1870. }
  1871. else
  1872. if (strcmp(cmd, "/v1/instance/peers") == 0) { /** **/
  1873. /* get the collected inbox list as the instances "this domain is aware of" */
  1874. xs *list = inbox_list();
  1875. xs *peers = xs_list_new();
  1876. const char *inbox;
  1877. xs_list_foreach(list, inbox) {
  1878. xs *l = xs_split(inbox, "/");
  1879. const char *domain = xs_list_get(l, 2);
  1880. if (xs_is_string(domain))
  1881. peers = xs_list_append(peers, domain);
  1882. }
  1883. *body = xs_json_dumps(peers, 4);
  1884. *ctype = "application/json";
  1885. status = HTTP_STATUS_OK;
  1886. }
  1887. else
  1888. if (strcmp(cmd, "/v1/instance/extended_description") == 0) { /** **/
  1889. xs *d = xs_dict_new();
  1890. xs *greeting = xs_fmt("%s/greeting.html", srv_basedir);
  1891. time_t t = mtime(greeting);
  1892. xs *updated_at = xs_str_iso_date(t);
  1893. xs *content = xs_replace(snac_blurb, "%host%", xs_dict_get(srv_config, "host"));
  1894. d = xs_dict_set(d, "updated_at", updated_at);
  1895. d = xs_dict_set(d, "content", content);
  1896. *body = xs_json_dumps(d, 4);
  1897. *ctype = "application/json";
  1898. status = HTTP_STATUS_OK;
  1899. }
  1900. else
  1901. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1902. /* information about a status */
  1903. if (logged_in) {
  1904. xs *l = xs_split(cmd, "/");
  1905. const char *oid = xs_list_get(l, 3);
  1906. const char *op = xs_list_get(l, 4);
  1907. if (!xs_is_null(oid)) {
  1908. xs *msg = NULL;
  1909. xs *out = NULL;
  1910. /* skip the 'fake' part of the id */
  1911. oid = MID_TO_MD5(oid);
  1912. xs *id = xs_tolower_i(xs_dup(oid));
  1913. if (valid_status(object_get_by_md5(id, &msg))) {
  1914. if (op == NULL) {
  1915. if (!is_muted(&snac1, get_atto(msg))) {
  1916. /* return the status itself */
  1917. out = mastoapi_status(&snac1, msg);
  1918. }
  1919. }
  1920. else
  1921. if (strcmp(op, "context") == 0) { /** **/
  1922. /* return ancestors and children */
  1923. xs *anc = xs_list_new();
  1924. xs *des = xs_list_new();
  1925. xs_list *p;
  1926. const xs_str *v;
  1927. char pid[MD5_HEX_SIZE];
  1928. /* build the [grand]parent list, moving up */
  1929. strncpy(pid, id, sizeof(pid));
  1930. while (object_parent(pid, pid)) {
  1931. xs *m2 = NULL;
  1932. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1933. xs *st = mastoapi_status(&snac1, m2);
  1934. if (st)
  1935. anc = xs_list_insert(anc, 0, st);
  1936. }
  1937. else
  1938. break;
  1939. }
  1940. /* build the children list */
  1941. xs *children = object_children(xs_dict_get(msg, "id"));
  1942. p = children;
  1943. while (xs_list_iter(&p, &v)) {
  1944. xs *m2 = NULL;
  1945. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1946. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1947. xs *st = mastoapi_status(&snac1, m2);
  1948. if (st)
  1949. des = xs_list_append(des, st);
  1950. }
  1951. }
  1952. }
  1953. out = xs_dict_new();
  1954. out = xs_dict_append(out, "ancestors", anc);
  1955. out = xs_dict_append(out, "descendants", des);
  1956. }
  1957. else
  1958. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1959. strcmp(op, "favourited_by") == 0) { /** **/
  1960. /* return the list of people who liked or boosted this */
  1961. out = xs_list_new();
  1962. xs *l = NULL;
  1963. if (op[0] == 'r')
  1964. l = object_announces(xs_dict_get(msg, "id"));
  1965. else
  1966. l = object_likes(xs_dict_get(msg, "id"));
  1967. xs_list *p = l;
  1968. const xs_str *v;
  1969. while (xs_list_iter(&p, &v)) {
  1970. xs *actor2 = NULL;
  1971. if (valid_status(object_get_by_md5(v, &actor2))) {
  1972. xs *acct2 = mastoapi_account(&snac1, actor2);
  1973. out = xs_list_append(out, acct2);
  1974. }
  1975. }
  1976. }
  1977. else
  1978. if (strcmp(op, "source") == 0) { /** **/
  1979. out = xs_dict_new();
  1980. /* get the mastoapi status id */
  1981. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1982. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1983. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1984. }
  1985. }
  1986. else
  1987. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1988. if (out != NULL) {
  1989. *body = xs_json_dumps(out, 4);
  1990. *ctype = "application/json";
  1991. status = HTTP_STATUS_OK;
  1992. }
  1993. }
  1994. }
  1995. else
  1996. status = HTTP_STATUS_UNAUTHORIZED;
  1997. }
  1998. else
  1999. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  2000. /* TBD */
  2001. *body = xs_dup("{}");
  2002. *ctype = "application/json";
  2003. status = HTTP_STATUS_OK;
  2004. }
  2005. else
  2006. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  2007. if (logged_in) {
  2008. const xs_list *timeline = xs_dict_get(args, "timeline[]");
  2009. xs_str *json = NULL;
  2010. if (!xs_is_null(timeline))
  2011. json = xs_json_dumps(markers_get(&snac1, timeline), 4);
  2012. if (!xs_is_null(json))
  2013. *body = json;
  2014. else
  2015. *body = xs_dup("{}");
  2016. *ctype = "application/json";
  2017. status = HTTP_STATUS_OK;
  2018. }
  2019. else
  2020. status = HTTP_STATUS_UNAUTHORIZED;
  2021. }
  2022. else
  2023. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  2024. if (logged_in) {
  2025. xs *r = xs_list_new();
  2026. const xs_list *followed_hashtags = xs_dict_get_def(snac1.config,
  2027. "followed_hashtags", xs_stock(XSTYPE_LIST));
  2028. const char *hashtag;
  2029. xs_list_foreach(followed_hashtags, hashtag) {
  2030. if (*hashtag == '#') {
  2031. xs *d = xs_dict_new();
  2032. xs *s = xs_fmt("%s?t=%s", srv_baseurl, hashtag + 1);
  2033. d = xs_dict_set(d, "name", hashtag + 1);
  2034. d = xs_dict_set(d, "url", s);
  2035. d = xs_dict_set(d, "history", xs_stock(XSTYPE_LIST));
  2036. r = xs_list_append(r, d);
  2037. }
  2038. }
  2039. *body = xs_json_dumps(r, 4);
  2040. *ctype = "application/json";
  2041. status = HTTP_STATUS_OK;
  2042. }
  2043. else
  2044. status = HTTP_STATUS_UNAUTHORIZED;
  2045. }
  2046. else
  2047. if (strcmp(cmd, "/v1/blocks") == 0) { /** **/
  2048. *body = xs_dup("[]");
  2049. *ctype = "application/json";
  2050. status = HTTP_STATUS_OK;
  2051. }
  2052. else
  2053. if (strcmp(cmd, "/v1/mutes") == 0) { /** **/
  2054. *body = xs_dup("[]");
  2055. *ctype = "application/json";
  2056. status = HTTP_STATUS_OK;
  2057. }
  2058. else
  2059. if (strcmp(cmd, "/v1/trends/tags") == 0) { /** **/
  2060. *body = xs_dup("[]");
  2061. *ctype = "application/json";
  2062. status = HTTP_STATUS_OK;
  2063. }
  2064. else
  2065. if (strcmp(cmd, "/v1/trends/statuses") == 0) { /** **/
  2066. *body = xs_dup("[]");
  2067. *ctype = "application/json";
  2068. status = HTTP_STATUS_OK;
  2069. }
  2070. else
  2071. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  2072. if (logged_in) {
  2073. const char *q = xs_dict_get(args, "q");
  2074. const char *type = xs_dict_get(args, "type");
  2075. const char *offset = xs_dict_get(args, "offset");
  2076. xs *acl = xs_list_new();
  2077. xs *stl = xs_list_new();
  2078. xs *htl = xs_list_new();
  2079. xs *res = xs_dict_new();
  2080. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  2081. /* reply something only for offset 0; otherwise,
  2082. apps like Tusky keep asking again and again */
  2083. if (xs_startswith(q, "https://")) {
  2084. if (!timeline_here(&snac1, q)) {
  2085. xs *object = NULL;
  2086. int status;
  2087. status = activitypub_request(&snac1, q, &object);
  2088. snac_debug(&snac1, 1, xs_fmt("Request searched URL %s %d", q, status));
  2089. if (valid_status(status)) {
  2090. /* got it; also request the actor */
  2091. const char *attr_to = get_atto(object);
  2092. xs *actor_obj = NULL;
  2093. if (!xs_is_null(attr_to)) {
  2094. status = actor_request(&snac1, attr_to, &actor_obj);
  2095. snac_debug(&snac1, 1, xs_fmt("Request author %s of %s %d", attr_to, q, status));
  2096. if (valid_status(status)) {
  2097. /* add the actor */
  2098. actor_add(attr_to, actor_obj);
  2099. /* add the post to the timeline */
  2100. timeline_add(&snac1, q, object);
  2101. }
  2102. }
  2103. }
  2104. }
  2105. }
  2106. if (!xs_is_null(q)) {
  2107. if (xs_is_null(type) || strcmp(type, "accounts") == 0) {
  2108. /* do a webfinger query */
  2109. char *actor = NULL;
  2110. char *user = NULL;
  2111. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  2112. xs *actor_o = NULL;
  2113. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  2114. xs *acct = mastoapi_account(NULL, actor_o);
  2115. acl = xs_list_append(acl, acct);
  2116. if (!object_here(actor))
  2117. object_add(actor, actor_o);
  2118. }
  2119. }
  2120. }
  2121. if (xs_is_null(type) || strcmp(type, "hashtags") == 0) {
  2122. /* search this tag */
  2123. xs *tl = tag_search((char *)q, 0, 1);
  2124. if (xs_list_len(tl)) {
  2125. xs *d = xs_dict_new();
  2126. d = xs_dict_append(d, "name", q);
  2127. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  2128. d = xs_dict_append(d, "url", url);
  2129. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  2130. htl = xs_list_append(htl, d);
  2131. }
  2132. }
  2133. if (xs_is_null(type) || strcmp(type, "statuses") == 0) {
  2134. int to = 0;
  2135. int cnt = 40;
  2136. xs *tl = content_search(&snac1, q, 1, 0, cnt, 0, &to);
  2137. int c = 0;
  2138. const char *v;
  2139. while (xs_list_next(tl, &v, &c) && --cnt) {
  2140. xs *post = NULL;
  2141. if (!valid_status(timeline_get_by_md5(&snac1, v, &post)))
  2142. continue;
  2143. xs *s = mastoapi_status(&snac1, post);
  2144. if (!xs_is_null(s))
  2145. stl = xs_list_append(stl, s);
  2146. }
  2147. }
  2148. }
  2149. }
  2150. res = xs_dict_append(res, "accounts", acl);
  2151. res = xs_dict_append(res, "statuses", stl);
  2152. res = xs_dict_append(res, "hashtags", htl);
  2153. *body = xs_json_dumps(res, 4);
  2154. *ctype = "application/json";
  2155. status = HTTP_STATUS_OK;
  2156. }
  2157. else
  2158. status = HTTP_STATUS_UNAUTHORIZED;
  2159. }
  2160. /* user cleanup */
  2161. if (logged_in)
  2162. user_free(&snac1);
  2163. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  2164. return status;
  2165. }
  2166. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  2167. const char *payload, int p_size,
  2168. char **body, int *b_size, char **ctype)
  2169. {
  2170. (void)p_size;
  2171. (void)b_size;
  2172. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2173. return 0;
  2174. int status = HTTP_STATUS_NOT_FOUND;
  2175. xs *args = NULL;
  2176. const char *i_ctype = xs_dict_get(req, "content-type");
  2177. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2178. if (!xs_is_null(payload))
  2179. args = xs_json_loads(payload);
  2180. }
  2181. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2182. {
  2183. // Some apps send form data instead of json so we should cater for those
  2184. if (!xs_is_null(payload)) {
  2185. args = xs_url_vars(payload);
  2186. }
  2187. }
  2188. else
  2189. args = xs_dup(xs_dict_get(req, "p_vars"));
  2190. if (args == NULL)
  2191. return HTTP_STATUS_BAD_REQUEST;
  2192. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2193. snac snac = {0};
  2194. int logged_in = process_auth_token(&snac, req);
  2195. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  2196. const char *name = xs_dict_get(args, "client_name");
  2197. const char *ruri = xs_dict_get(args, "redirect_uris");
  2198. const char *scope = xs_dict_get(args, "scope");
  2199. /* Ice Cubes sends these values as query parameters, so try these */
  2200. if (name == NULL && ruri == NULL && scope == NULL) {
  2201. args = xs_dup(xs_dict_get(req, "q_vars"));
  2202. name = xs_dict_get(args, "client_name");
  2203. ruri = xs_dict_get(args, "redirect_uris");
  2204. scope = xs_dict_get(args, "scope");
  2205. }
  2206. if (xs_type(ruri) == XSTYPE_LIST)
  2207. ruri = xs_dict_get(ruri, 0);
  2208. if (name && ruri) {
  2209. xs *app = xs_dict_new();
  2210. xs *id = xs_replace_i(tid(0), ".", "");
  2211. xs *csec = random_str();
  2212. xs *vkey = random_str();
  2213. xs *cid = NULL;
  2214. /* pick a non-existent random cid */
  2215. for (;;) {
  2216. cid = random_str();
  2217. xs *p_app = app_get(cid);
  2218. if (p_app == NULL)
  2219. break;
  2220. xs_free(cid);
  2221. }
  2222. app = xs_dict_append(app, "name", name);
  2223. app = xs_dict_append(app, "redirect_uri", ruri);
  2224. app = xs_dict_append(app, "client_id", cid);
  2225. app = xs_dict_append(app, "client_secret", csec);
  2226. app = xs_dict_append(app, "vapid_key", vkey);
  2227. app = xs_dict_append(app, "id", id);
  2228. *body = xs_json_dumps(app, 4);
  2229. *ctype = "application/json";
  2230. status = HTTP_STATUS_OK;
  2231. app = xs_dict_append(app, "code", "");
  2232. if (scope)
  2233. app = xs_dict_append(app, "scope", scope);
  2234. app_add(cid, app);
  2235. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  2236. }
  2237. }
  2238. else
  2239. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  2240. if (logged_in) {
  2241. /* post a new Note */
  2242. const char *content = xs_dict_get(args, "status");
  2243. const char *mid = xs_dict_get(args, "in_reply_to_id");
  2244. const char *visibility = xs_dict_get(args, "visibility");
  2245. const char *summary = xs_dict_get(args, "spoiler_text");
  2246. const char *media_ids = xs_dict_get(args, "media_ids");
  2247. const char *language = xs_dict_get(args, "language");
  2248. if (xs_is_null(media_ids))
  2249. media_ids = xs_dict_get(args, "media_ids[]");
  2250. if (xs_is_null(media_ids))
  2251. media_ids = xs_dict_get(args, "media_ids");
  2252. if (xs_is_null(visibility))
  2253. visibility = "public";
  2254. xs *attach_list = xs_list_new();
  2255. xs *irt = NULL;
  2256. /* is it a reply? */
  2257. if (mid != NULL) {
  2258. xs *r_msg = NULL;
  2259. const char *md5 = MID_TO_MD5(mid);
  2260. if (valid_status(object_get_by_md5(md5, &r_msg)))
  2261. irt = xs_dup(xs_dict_get(r_msg, "id"));
  2262. }
  2263. /* does it have attachments? */
  2264. if (!xs_is_null(media_ids)) {
  2265. xs *mi = NULL;
  2266. if (xs_type(media_ids) == XSTYPE_LIST)
  2267. mi = xs_dup(media_ids);
  2268. else {
  2269. mi = xs_list_new();
  2270. mi = xs_list_append(mi, media_ids);
  2271. }
  2272. xs_list *p = mi;
  2273. const xs_str *v;
  2274. while (xs_list_iter(&p, &v)) {
  2275. xs *l = xs_list_new();
  2276. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  2277. xs *desc = static_get_meta(&snac, v);
  2278. l = xs_list_append(l, url);
  2279. l = xs_list_append(l, desc);
  2280. attach_list = xs_list_append(attach_list, l);
  2281. }
  2282. }
  2283. /* prepare the message */
  2284. int scope = 1;
  2285. if (strcmp(visibility, "unlisted") == 0)
  2286. scope = 2;
  2287. else
  2288. if (strcmp(visibility, "public") == 0)
  2289. scope = 0;
  2290. xs *msg = msg_note(&snac, content, NULL, irt, attach_list, scope, language, NULL);
  2291. if (!xs_is_null(summary) && *summary) {
  2292. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  2293. msg = xs_dict_set(msg, "summary", summary);
  2294. }
  2295. /* scheduled? */
  2296. const char *scheduled_at = xs_dict_get(args, "scheduled_at");
  2297. if (xs_is_string(scheduled_at) && *scheduled_at) {
  2298. msg = xs_dict_set(msg, "published", scheduled_at);
  2299. schedule_add(&snac, xs_dict_get(msg, "id"), msg);
  2300. }
  2301. else {
  2302. /* store */
  2303. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2304. /* 'Create' message */
  2305. xs *c_msg = msg_create(&snac, msg);
  2306. enqueue_message(&snac, c_msg);
  2307. timeline_touch(&snac);
  2308. }
  2309. /* convert to a mastodon status as a response code */
  2310. xs *st = mastoapi_status(&snac, msg);
  2311. *body = xs_json_dumps(st, 4);
  2312. *ctype = "application/json";
  2313. status = HTTP_STATUS_OK;
  2314. }
  2315. else
  2316. status = HTTP_STATUS_UNAUTHORIZED;
  2317. }
  2318. else
  2319. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  2320. if (logged_in) {
  2321. /* operations on a status */
  2322. xs *l = xs_split(cmd, "/");
  2323. const char *mid = xs_list_get(l, 3);
  2324. const char *op = xs_list_get(l, 4);
  2325. if (!xs_is_null(mid)) {
  2326. xs *msg = NULL;
  2327. xs *out = NULL;
  2328. /* skip the 'fake' part of the id */
  2329. mid = MID_TO_MD5(mid);
  2330. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2331. const char *id = xs_dict_get(msg, "id");
  2332. if (op == NULL) {
  2333. /* no operation (?) */
  2334. }
  2335. else
  2336. if (strcmp(op, "favourite") == 0) { /** **/
  2337. xs *n_msg = msg_admiration(&snac, id, "Like");
  2338. if (n_msg != NULL) {
  2339. enqueue_message(&snac, n_msg);
  2340. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  2341. out = mastoapi_status(&snac, msg);
  2342. }
  2343. }
  2344. else
  2345. if (strcmp(op, "unfavourite") == 0) { /** **/
  2346. xs *n_msg = msg_repulsion(&snac, id, "Like");
  2347. if (n_msg != NULL) {
  2348. enqueue_message(&snac, n_msg);
  2349. out = mastoapi_status(&snac, msg);
  2350. }
  2351. }
  2352. else
  2353. if (strcmp(op, "reblog") == 0) { /** **/
  2354. xs *n_msg = msg_admiration(&snac, id, "Announce");
  2355. if (n_msg != NULL) {
  2356. enqueue_message(&snac, n_msg);
  2357. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  2358. out = mastoapi_status(&snac, msg);
  2359. }
  2360. }
  2361. else
  2362. if (strcmp(op, "unreblog") == 0) { /** **/
  2363. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  2364. if (n_msg != NULL) {
  2365. enqueue_message(&snac, n_msg);
  2366. out = mastoapi_status(&snac, msg);
  2367. }
  2368. }
  2369. else
  2370. if (strcmp(op, "bookmark") == 0) { /** **/
  2371. /* bookmark this message */
  2372. if (bookmark(&snac, id) == 0)
  2373. out = mastoapi_status(&snac, msg);
  2374. else
  2375. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2376. }
  2377. else
  2378. if (strcmp(op, "unbookmark") == 0) { /** **/
  2379. /* unbookmark this message */
  2380. unbookmark(&snac, id);
  2381. out = mastoapi_status(&snac, msg);
  2382. }
  2383. else
  2384. if (strcmp(op, "pin") == 0) { /** **/
  2385. /* pin this message */
  2386. if (pin(&snac, id) == 0)
  2387. out = mastoapi_status(&snac, msg);
  2388. else
  2389. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2390. }
  2391. else
  2392. if (strcmp(op, "unpin") == 0) { /** **/
  2393. /* unpin this message */
  2394. unpin(&snac, id);
  2395. out = mastoapi_status(&snac, msg);
  2396. }
  2397. else
  2398. if (strcmp(op, "mute") == 0) { /** **/
  2399. /* Mastodon's mute is snac's hide */
  2400. }
  2401. else
  2402. if (strcmp(op, "unmute") == 0) { /** **/
  2403. /* Mastodon's unmute is snac's unhide */
  2404. }
  2405. }
  2406. if (out != NULL) {
  2407. *body = xs_json_dumps(out, 4);
  2408. *ctype = "application/json";
  2409. status = HTTP_STATUS_OK;
  2410. }
  2411. }
  2412. }
  2413. else
  2414. status = HTTP_STATUS_UNAUTHORIZED;
  2415. }
  2416. else
  2417. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  2418. if (logged_in) {
  2419. notify_clear(&snac);
  2420. timeline_touch(&snac);
  2421. *body = xs_dup("{}");
  2422. *ctype = "application/json";
  2423. status = HTTP_STATUS_OK;
  2424. }
  2425. else
  2426. status = HTTP_STATUS_UNAUTHORIZED;
  2427. }
  2428. else
  2429. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  2430. /* I don't know what I'm doing */
  2431. if (logged_in) {
  2432. const char *v;
  2433. xs *wpush = xs_dict_new();
  2434. wpush = xs_dict_append(wpush, "id", "1");
  2435. v = xs_dict_get(args, "data");
  2436. v = xs_dict_get(v, "alerts");
  2437. wpush = xs_dict_append(wpush, "alerts", v);
  2438. v = xs_dict_get(args, "subscription");
  2439. v = xs_dict_get(v, "endpoint");
  2440. wpush = xs_dict_append(wpush, "endpoint", v);
  2441. xs *server_key = random_str();
  2442. wpush = xs_dict_append(wpush, "server_key", server_key);
  2443. *body = xs_json_dumps(wpush, 4);
  2444. *ctype = "application/json";
  2445. status = HTTP_STATUS_OK;
  2446. }
  2447. else
  2448. status = HTTP_STATUS_UNAUTHORIZED;
  2449. }
  2450. else
  2451. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  2452. if (logged_in) {
  2453. const xs_list *file = xs_dict_get(args, "file");
  2454. const char *desc = xs_dict_get(args, "description");
  2455. if (xs_is_null(desc))
  2456. desc = "";
  2457. status = HTTP_STATUS_BAD_REQUEST;
  2458. if (xs_type(file) == XSTYPE_LIST) {
  2459. const char *fn = xs_list_get(file, 0);
  2460. if (*fn != '\0') {
  2461. char *ext = strrchr(fn, '.');
  2462. char rnd[32];
  2463. xs_rnd_buf(rnd, sizeof(rnd));
  2464. xs *hash = xs_md5_hex(rnd, sizeof(rnd));
  2465. xs *id = xs_fmt("post-%s%s", hash, ext ? ext : "");
  2466. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  2467. int fo = xs_number_get(xs_list_get(file, 1));
  2468. int fs = xs_number_get(xs_list_get(file, 2));
  2469. /* store */
  2470. static_put(&snac, id, payload + fo, fs);
  2471. static_put_meta(&snac, id, desc);
  2472. /* prepare a response */
  2473. xs *rsp = xs_dict_new();
  2474. rsp = xs_dict_append(rsp, "id", id);
  2475. rsp = xs_dict_append(rsp, "type", "image");
  2476. rsp = xs_dict_append(rsp, "url", url);
  2477. rsp = xs_dict_append(rsp, "preview_url", url);
  2478. rsp = xs_dict_append(rsp, "remote_url", url);
  2479. rsp = xs_dict_append(rsp, "description", desc);
  2480. *body = xs_json_dumps(rsp, 4);
  2481. *ctype = "application/json";
  2482. status = HTTP_STATUS_OK;
  2483. }
  2484. }
  2485. }
  2486. else
  2487. status = HTTP_STATUS_UNAUTHORIZED;
  2488. }
  2489. else
  2490. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  2491. if (logged_in) {
  2492. /* account-related information */
  2493. xs *l = xs_split(cmd, "/");
  2494. const char *md5 = xs_list_get(l, 3);
  2495. const char *opt = xs_list_get(l, 4);
  2496. xs *rsp = NULL;
  2497. if (!xs_is_null(md5) && *md5) {
  2498. xs *actor_o = NULL;
  2499. if (xs_is_null(opt)) {
  2500. /* ? */
  2501. }
  2502. else
  2503. if (strcmp(opt, "follow") == 0) { /** **/
  2504. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2505. const char *actor = xs_dict_get(actor_o, "id");
  2506. xs *msg = msg_follow(&snac, actor);
  2507. if (msg != NULL) {
  2508. /* reload the actor from the message, in may be different */
  2509. actor = xs_dict_get(msg, "object");
  2510. following_add(&snac, actor, msg);
  2511. enqueue_output_by_actor(&snac, msg, actor, 0);
  2512. rsp = mastoapi_relationship(&snac, md5);
  2513. }
  2514. }
  2515. }
  2516. else
  2517. if (strcmp(opt, "unfollow") == 0) { /** **/
  2518. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2519. const char *actor = xs_dict_get(actor_o, "id");
  2520. /* get the following object */
  2521. xs *object = NULL;
  2522. if (valid_status(following_get(&snac, actor, &object))) {
  2523. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  2524. following_del(&snac, actor);
  2525. enqueue_output_by_actor(&snac, msg, actor, 0);
  2526. rsp = mastoapi_relationship(&snac, md5);
  2527. }
  2528. }
  2529. }
  2530. else
  2531. if (strcmp(opt, "block") == 0) { /** **/
  2532. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2533. const char *actor = xs_dict_get(actor_o, "id");
  2534. mute(&snac, actor);
  2535. rsp = mastoapi_relationship(&snac, md5);
  2536. }
  2537. }
  2538. else
  2539. if (strcmp(opt, "unblock") == 0) { /** **/
  2540. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2541. const char *actor = xs_dict_get(actor_o, "id");
  2542. unmute(&snac, actor);
  2543. rsp = mastoapi_relationship(&snac, md5);
  2544. }
  2545. }
  2546. }
  2547. if (rsp != NULL) {
  2548. *body = xs_json_dumps(rsp, 4);
  2549. *ctype = "application/json";
  2550. status = HTTP_STATUS_OK;
  2551. }
  2552. }
  2553. else
  2554. status = HTTP_STATUS_UNAUTHORIZED;
  2555. }
  2556. else
  2557. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2558. if (logged_in) {
  2559. /* operations on a status */
  2560. xs *l = xs_split(cmd, "/");
  2561. const char *mid = xs_list_get(l, 3);
  2562. const char *op = xs_list_get(l, 4);
  2563. if (!xs_is_null(mid)) {
  2564. xs *msg = NULL;
  2565. xs *out = NULL;
  2566. /* skip the 'fake' part of the id */
  2567. mid = MID_TO_MD5(mid);
  2568. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2569. const char *id = xs_dict_get(msg, "id");
  2570. const char *atto = get_atto(msg);
  2571. const xs_list *opts = xs_dict_get(msg, "oneOf");
  2572. if (opts == NULL)
  2573. opts = xs_dict_get(msg, "anyOf");
  2574. if (op == NULL) {
  2575. }
  2576. else
  2577. if (strcmp(op, "votes") == 0) {
  2578. const xs_list *choices = xs_dict_get(args, "choices[]");
  2579. if (xs_is_null(choices))
  2580. choices = xs_dict_get(args, "choices");
  2581. if (xs_type(choices) == XSTYPE_LIST) {
  2582. const xs_str *v;
  2583. int c = 0;
  2584. while (xs_list_next(choices, &v, &c)) {
  2585. int io = atoi(v);
  2586. const xs_dict *o = xs_list_get(opts, io);
  2587. if (o) {
  2588. const char *name = xs_dict_get(o, "name");
  2589. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1, NULL, NULL);
  2590. msg = xs_dict_append(msg, "name", name);
  2591. xs *c_msg = msg_create(&snac, msg);
  2592. enqueue_message(&snac, c_msg);
  2593. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2594. }
  2595. }
  2596. out = mastoapi_poll(&snac, msg);
  2597. }
  2598. }
  2599. }
  2600. if (out != NULL) {
  2601. *body = xs_json_dumps(out, 4);
  2602. *ctype = "application/json";
  2603. status = HTTP_STATUS_OK;
  2604. }
  2605. }
  2606. }
  2607. else
  2608. status = HTTP_STATUS_UNAUTHORIZED;
  2609. }
  2610. else
  2611. if (strcmp(cmd, "/v1/lists") == 0) {
  2612. if (logged_in) {
  2613. const char *title = xs_dict_get(args, "title");
  2614. if (xs_type(title) == XSTYPE_STRING) {
  2615. /* add the list */
  2616. xs *out = xs_dict_new();
  2617. xs *lid = list_maint(&snac, title, 1);
  2618. if (!xs_is_null(lid)) {
  2619. out = xs_dict_append(out, "id", lid);
  2620. out = xs_dict_append(out, "title", title);
  2621. out = xs_dict_append(out, "replies_policy", xs_dict_get_def(args, "replies_policy", "list"));
  2622. out = xs_dict_append(out, "exclusive", xs_stock(XSTYPE_FALSE));
  2623. status = HTTP_STATUS_OK;
  2624. }
  2625. else {
  2626. out = xs_dict_append(out, "error", "cannot create list");
  2627. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2628. }
  2629. *body = xs_json_dumps(out, 4);
  2630. *ctype = "application/json";
  2631. }
  2632. else
  2633. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2634. }
  2635. }
  2636. else
  2637. if (xs_startswith(cmd, "/v1/lists/")) { /** list maintenance **/
  2638. if (logged_in) {
  2639. xs *l = xs_split(cmd, "/");
  2640. const char *op = xs_list_get(l, -1);
  2641. const char *id = xs_list_get(l, -2);
  2642. if (op && id && xs_is_hex(id)) {
  2643. if (strcmp(op, "accounts") == 0) {
  2644. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2645. if (xs_is_null(accts))
  2646. accts = xs_dict_get(args, "account_ids");
  2647. int c = 0;
  2648. const char *v;
  2649. while (xs_list_next(accts, &v, &c)) {
  2650. list_members(&snac, id, v, 1);
  2651. }
  2652. xs *out = xs_dict_new();
  2653. *body = xs_json_dumps(out, 4);
  2654. *ctype = "application/json";
  2655. status = HTTP_STATUS_OK;
  2656. }
  2657. }
  2658. }
  2659. }
  2660. else if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  2661. xs_str *json = NULL;
  2662. if (logged_in) {
  2663. const xs_str *home_marker = xs_dict_get(args, "home[last_read_id]");
  2664. if (xs_is_null(home_marker)) {
  2665. const xs_dict *home = xs_dict_get(args, "home");
  2666. if (!xs_is_null(home))
  2667. home_marker = xs_dict_get(home, "last_read_id");
  2668. }
  2669. const xs_str *notify_marker = xs_dict_get(args, "notifications[last_read_id]");
  2670. if (xs_is_null(notify_marker)) {
  2671. const xs_dict *notify = xs_dict_get(args, "notifications");
  2672. if (!xs_is_null(notify))
  2673. notify_marker = xs_dict_get(notify, "last_read_id");
  2674. }
  2675. json = xs_json_dumps(markers_set(&snac, home_marker, notify_marker), 4);
  2676. }
  2677. if (!xs_is_null(json))
  2678. *body = json;
  2679. else
  2680. *body = xs_dup("{}");
  2681. *ctype = "application/json";
  2682. status = HTTP_STATUS_OK;
  2683. }
  2684. else
  2685. if (xs_startswith(cmd, "/v1/follow_requests")) { /** **/
  2686. if (logged_in) {
  2687. /* "authorize" or "reject" */
  2688. xs *rel = NULL;
  2689. xs *l = xs_split(cmd, "/");
  2690. const char *md5 = xs_list_get(l, -2);
  2691. const char *s_cmd = xs_list_get(l, -1);
  2692. if (xs_is_string(md5) && xs_is_string(s_cmd)) {
  2693. xs *actor = NULL;
  2694. if (valid_status(object_get_by_md5(md5, &actor))) {
  2695. const char *actor_id = xs_dict_get(actor, "id");
  2696. if (strcmp(s_cmd, "authorize") == 0) {
  2697. xs *fwreq = pending_get(&snac, actor_id);
  2698. if (fwreq != NULL) {
  2699. xs *reply = msg_accept(&snac, fwreq, actor_id);
  2700. enqueue_message(&snac, reply);
  2701. if (xs_is_null(xs_dict_get(fwreq, "published"))) {
  2702. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  2703. fwreq = xs_dict_set(fwreq, "published", date);
  2704. }
  2705. timeline_add(&snac, xs_dict_get(fwreq, "id"), fwreq);
  2706. follower_add(&snac, actor_id);
  2707. pending_del(&snac, actor_id);
  2708. rel = mastoapi_relationship(&snac, md5);
  2709. }
  2710. }
  2711. else
  2712. if (strcmp(s_cmd, "reject") == 0) {
  2713. pending_del(&snac, actor_id);
  2714. rel = mastoapi_relationship(&snac, md5);
  2715. }
  2716. }
  2717. }
  2718. if (rel != NULL) {
  2719. *body = xs_json_dumps(rel, 4);
  2720. *ctype = "application/json";
  2721. status = HTTP_STATUS_OK;
  2722. }
  2723. }
  2724. }
  2725. else
  2726. if (xs_startswith(cmd, "/v1/tags/")) { /** **/
  2727. if (logged_in) {
  2728. xs *l = xs_split(cmd, "/");
  2729. const char *i_tag = xs_list_get(l, 3);
  2730. const char *cmd = xs_list_get(l, 4);
  2731. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2732. if (xs_is_string(i_tag) && xs_is_string(cmd)) {
  2733. int ok = 0;
  2734. xs *tag = xs_fmt("#%s", i_tag);
  2735. xs *followed_hashtags = xs_dup(xs_dict_get_def(snac.config,
  2736. "followed_hashtags", xs_stock(XSTYPE_LIST)));
  2737. if (strcmp(cmd, "follow") == 0) {
  2738. followed_hashtags = xs_list_append(followed_hashtags, tag);
  2739. ok = 1;
  2740. }
  2741. else
  2742. if (strcmp(cmd, "unfollow") == 0) {
  2743. int off = xs_list_in(followed_hashtags, tag);
  2744. if (off != -1)
  2745. followed_hashtags = xs_list_del(followed_hashtags, off);
  2746. ok = 1;
  2747. }
  2748. if (ok) {
  2749. /* update */
  2750. xs_dict_set(snac.config, "followed_hashtags", followed_hashtags);
  2751. user_persist(&snac, 0);
  2752. xs *d = xs_dict_new();
  2753. xs *s = xs_fmt("%s?t=%s", srv_baseurl, i_tag);
  2754. d = xs_dict_set(d, "name", i_tag);
  2755. d = xs_dict_set(d, "url", s);
  2756. d = xs_dict_set(d, "history", xs_stock(XSTYPE_LIST));
  2757. *body = xs_json_dumps(d, 4);
  2758. *ctype = "application/json";
  2759. status = HTTP_STATUS_OK;
  2760. }
  2761. }
  2762. }
  2763. }
  2764. else
  2765. status = HTTP_STATUS_UNPROCESSABLE_CONTENT;
  2766. /* user cleanup */
  2767. if (logged_in)
  2768. user_free(&snac);
  2769. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2770. return status;
  2771. }
  2772. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2773. const char *payload, int p_size,
  2774. char **body, int *b_size, char **ctype)
  2775. {
  2776. (void)p_size;
  2777. (void)body;
  2778. (void)b_size;
  2779. (void)ctype;
  2780. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2781. return 0;
  2782. int status = HTTP_STATUS_NOT_FOUND;
  2783. xs *args = NULL;
  2784. const char *i_ctype = xs_dict_get(req, "content-type");
  2785. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2786. if (!xs_is_null(payload))
  2787. args = xs_json_loads(payload);
  2788. }
  2789. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2790. {
  2791. // Some apps send form data instead of json so we should cater for those
  2792. if (!xs_is_null(payload)) {
  2793. args = xs_url_vars(payload);
  2794. }
  2795. }
  2796. else
  2797. args = xs_dup(xs_dict_get(req, "p_vars"));
  2798. if (args == NULL)
  2799. return HTTP_STATUS_BAD_REQUEST;
  2800. snac snac = {0};
  2801. int logged_in = process_auth_token(&snac, req);
  2802. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2803. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2804. // pretend we deleted it, since it doesn't exist anyway
  2805. status = HTTP_STATUS_OK;
  2806. }
  2807. else
  2808. if (xs_startswith(cmd, "/v1/lists/")) {
  2809. if (logged_in) {
  2810. xs *l = xs_split(cmd, "/");
  2811. const char *p = xs_list_get(l, -1);
  2812. if (p) {
  2813. if (strcmp(p, "accounts") == 0) {
  2814. /* delete account from list */
  2815. p = xs_list_get(l, -2);
  2816. const xs_list *accts = xs_dict_get(args, "account_ids[]");
  2817. if (xs_is_null(accts))
  2818. accts = xs_dict_get(args, "account_ids");
  2819. int c = 0;
  2820. const char *v;
  2821. while (xs_list_next(accts, &v, &c)) {
  2822. list_members(&snac, p, v, 2);
  2823. }
  2824. }
  2825. else {
  2826. /* delete list */
  2827. if (xs_is_hex(p)) {
  2828. list_maint(&snac, p, 2);
  2829. }
  2830. }
  2831. }
  2832. *ctype = "application/json";
  2833. status = HTTP_STATUS_OK;
  2834. }
  2835. else
  2836. status = HTTP_STATUS_UNAUTHORIZED;
  2837. }
  2838. /* user cleanup */
  2839. if (logged_in)
  2840. user_free(&snac);
  2841. srv_debug(1, xs_fmt("mastoapi_delete_handler %s %d", q_path, status));
  2842. return status;
  2843. }
  2844. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2845. const char *payload, int p_size,
  2846. char **body, int *b_size, char **ctype)
  2847. {
  2848. (void)p_size;
  2849. (void)b_size;
  2850. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2851. return 0;
  2852. int status = HTTP_STATUS_NOT_FOUND;
  2853. xs *args = NULL;
  2854. const char *i_ctype = xs_dict_get(req, "content-type");
  2855. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2856. if (!xs_is_null(payload))
  2857. args = xs_json_loads(payload);
  2858. }
  2859. else
  2860. args = xs_dup(xs_dict_get(req, "p_vars"));
  2861. if (args == NULL)
  2862. return HTTP_STATUS_BAD_REQUEST;
  2863. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2864. snac snac = {0};
  2865. int logged_in = process_auth_token(&snac, req);
  2866. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2867. if (logged_in) {
  2868. xs *l = xs_split(cmd, "/");
  2869. const char *stid = xs_list_get(l, 3);
  2870. if (!xs_is_null(stid)) {
  2871. const char *desc = xs_dict_get(args, "description");
  2872. /* set the image metadata */
  2873. static_put_meta(&snac, stid, desc);
  2874. /* prepare a response */
  2875. xs *rsp = xs_dict_new();
  2876. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2877. rsp = xs_dict_append(rsp, "id", stid);
  2878. rsp = xs_dict_append(rsp, "type", "image");
  2879. rsp = xs_dict_append(rsp, "url", url);
  2880. rsp = xs_dict_append(rsp, "preview_url", url);
  2881. rsp = xs_dict_append(rsp, "remote_url", url);
  2882. rsp = xs_dict_append(rsp, "description", desc);
  2883. *body = xs_json_dumps(rsp, 4);
  2884. *ctype = "application/json";
  2885. status = HTTP_STATUS_OK;
  2886. }
  2887. }
  2888. else
  2889. status = HTTP_STATUS_UNAUTHORIZED;
  2890. }
  2891. else
  2892. if (xs_startswith(cmd, "/v1/statuses")) {
  2893. if (logged_in) {
  2894. xs *l = xs_split(cmd, "/");
  2895. const char *mid = xs_list_get(l, 3);
  2896. if (!xs_is_null(mid)) {
  2897. const char *md5 = MID_TO_MD5(mid);
  2898. xs *rsp = NULL;
  2899. xs *msg = NULL;
  2900. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2901. const char *content = xs_dict_get(args, "status");
  2902. xs *atls = xs_list_new();
  2903. xs *f_content = not_really_markdown(content, &atls, NULL);
  2904. /* replace fields with new content */
  2905. msg = xs_dict_set(msg, "sourceContent", content);
  2906. msg = xs_dict_set(msg, "content", f_content);
  2907. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2908. msg = xs_dict_set(msg, "updated", updated);
  2909. /* overwrite object, not updating the indexes */
  2910. object_add_ow(xs_dict_get(msg, "id"), msg);
  2911. /* update message */
  2912. xs *c_msg = msg_update(&snac, msg);
  2913. enqueue_message(&snac, c_msg);
  2914. rsp = mastoapi_status(&snac, msg);
  2915. }
  2916. if (rsp != NULL) {
  2917. *body = xs_json_dumps(rsp, 4);
  2918. *ctype = "application/json";
  2919. status = HTTP_STATUS_OK;
  2920. }
  2921. }
  2922. }
  2923. else
  2924. status = HTTP_STATUS_UNAUTHORIZED;
  2925. }
  2926. /* user cleanup */
  2927. if (logged_in)
  2928. user_free(&snac);
  2929. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2930. return status;
  2931. }
  2932. void persist_image(const char *key, const xs_val *data, const char *payload, snac *snac)
  2933. /* Store header or avatar */
  2934. {
  2935. if (data != NULL) {
  2936. if (xs_type(data) == XSTYPE_LIST) {
  2937. const char *fn = xs_list_get(data, 0);
  2938. if (fn && *fn) {
  2939. const char *ext = strrchr(fn, '.');
  2940. /* Mona iOS sends always jpg as application/octet-stream with no filename */
  2941. if (ext == NULL || strcmp(fn, key) == 0) {
  2942. ext = ".jpg";
  2943. }
  2944. /* Make sure we have a unique file name, otherwise updated images will not be
  2945. * re-loaded by clients. */
  2946. xs *rnd = random_str();
  2947. xs *hash = xs_md5_hex(rnd, strlen(rnd));
  2948. xs *id = xs_fmt("%s%s", hash, ext);
  2949. xs *url = xs_fmt("%s/s/%s", snac->actor, id);
  2950. int fo = xs_number_get(xs_list_get(data, 1));
  2951. int fs = xs_number_get(xs_list_get(data, 2));
  2952. /* store */
  2953. static_put(snac, id, payload + fo, fs);
  2954. snac->config = xs_dict_set(snac->config, key, url);
  2955. }
  2956. }
  2957. }
  2958. }
  2959. int mastoapi_patch_handler(const xs_dict *req, const char *q_path,
  2960. const char *payload, int p_size,
  2961. char **body, int *b_size, char **ctype)
  2962. /* Handle profile updates */
  2963. {
  2964. (void)p_size;
  2965. (void)b_size;
  2966. if (!xs_startswith(q_path, "/api/v1/"))
  2967. return 0;
  2968. int status = HTTP_STATUS_NOT_FOUND;
  2969. xs *args = NULL;
  2970. const char *i_ctype = xs_dict_get(req, "content-type");
  2971. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2972. if (!xs_is_null(payload))
  2973. args = xs_json_loads(payload);
  2974. }
  2975. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  2976. {
  2977. // Some apps send form data instead of json so we should cater for those
  2978. if (!xs_is_null(payload)) {
  2979. args = xs_url_vars(payload);
  2980. }
  2981. }
  2982. else
  2983. args = xs_dup(xs_dict_get(req, "p_vars"));
  2984. if (args == NULL)
  2985. return HTTP_STATUS_BAD_REQUEST;
  2986. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2987. snac snac = {0};
  2988. int logged_in = process_auth_token(&snac, req);
  2989. if (xs_startswith(cmd, "/v1/accounts/update_credentials")) { /** **/
  2990. /* Update user profile fields */
  2991. if (logged_in) {
  2992. int c = 0;
  2993. const xs_str *k;
  2994. const xs_val *v;
  2995. const xs_str *field_name = NULL;
  2996. xs *new_fields = xs_dict_new();
  2997. while (xs_dict_next(args, &k, &v, &c)) {
  2998. if (strcmp(k, "display_name") == 0) {
  2999. if (v != NULL)
  3000. snac.config = xs_dict_set(snac.config, "name", v);
  3001. }
  3002. else
  3003. if (strcmp(k, "note") == 0) {
  3004. if (v != NULL)
  3005. snac.config = xs_dict_set(snac.config, "bio", v);
  3006. }
  3007. else
  3008. if (strcmp(k, "bot") == 0) {
  3009. if (v != NULL)
  3010. snac.config = xs_dict_set(snac.config, "bot",
  3011. (strcmp(v, "true") == 0 ||
  3012. strcmp(v, "1") == 0) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  3013. }
  3014. else
  3015. if (strcmp(k, "source[sensitive]") == 0) {
  3016. if (v != NULL)
  3017. snac.config = xs_dict_set(snac.config, "cw",
  3018. strcmp(v, "true") == 0 ? "open" : "");
  3019. }
  3020. else
  3021. if (strcmp(k, "source[privacy]") == 0) {
  3022. if (v != NULL)
  3023. snac.config = xs_dict_set(snac.config, "private",
  3024. strcmp(v, "private") == 0 ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  3025. }
  3026. else
  3027. if (strcmp(k, "header") == 0) {
  3028. persist_image("header", v, payload, &snac);
  3029. }
  3030. else
  3031. if (strcmp(k, "avatar") == 0) {
  3032. persist_image("avatar", v, payload, &snac);
  3033. }
  3034. else
  3035. if (xs_between("fields_attributes", k, "[name]")) {
  3036. field_name = strcmp(v, "") != 0 ? v : NULL;
  3037. }
  3038. else
  3039. if (xs_between("fields_attributes", k, "[value]")) {
  3040. if (field_name != NULL) {
  3041. new_fields = xs_dict_set(new_fields, field_name, v);
  3042. snac.config = xs_dict_set(snac.config, "metadata", new_fields);
  3043. }
  3044. }
  3045. else
  3046. if (strcmp(k, "locked") == 0) {
  3047. snac.config = xs_dict_set(snac.config, "approve_followers",
  3048. xs_stock(strcmp(v, "true") == 0 ? XSTYPE_TRUE : XSTYPE_FALSE));
  3049. }
  3050. /* we don't have support for the following options, yet
  3051. - discoverable (0/1)
  3052. */
  3053. }
  3054. /* Persist profile */
  3055. if (user_persist(&snac, 1) == 0)
  3056. credentials_get(body, ctype, &status, snac);
  3057. else
  3058. status = HTTP_STATUS_INTERNAL_SERVER_ERROR;
  3059. }
  3060. else
  3061. status = HTTP_STATUS_UNAUTHORIZED;
  3062. }
  3063. /* user cleanup */
  3064. if (logged_in)
  3065. user_free(&snac);
  3066. srv_debug(1, xs_fmt("mastoapi_patch_handler %s %d", q_path, status));
  3067. return status;
  3068. }
  3069. void mastoapi_purge(void)
  3070. {
  3071. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  3072. xs *files = xs_glob(spec, 1, 0);
  3073. xs_list *p = files;
  3074. const xs_str *v;
  3075. time_t mt = time(NULL) - 3600;
  3076. while (xs_list_iter(&p, &v)) {
  3077. xs *cid = xs_replace(v, ".json", "");
  3078. xs *fn = _app_fn(cid);
  3079. if (mtime(fn) < mt) {
  3080. /* get the app */
  3081. xs *app = app_get(cid);
  3082. if (app) {
  3083. /* old apps with no uid are incomplete cruft */
  3084. const char *uid = xs_dict_get(app, "uid");
  3085. if (xs_is_null(uid) || *uid == '\0') {
  3086. unlink(fn);
  3087. srv_debug(2, xs_fmt("purged %s", fn));
  3088. }
  3089. }
  3090. }
  3091. }
  3092. }
  3093. #endif /* #ifndef NO_MASTODON_API */