mastoapi.c 112 KB

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