mastoapi.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799
  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 500;
  29. int status = 201;
  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 = 500;
  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 500;
  74. int status = 201;
  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 = 500;
  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. "<style>:root {color-scheme: light dark}</style>\n"
  116. "</head>\n"
  117. "<body><h1>%s OAuth identify</h1>\n"
  118. "<div style=\"background-color: red; color: white\">%s</div>\n"
  119. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  120. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  121. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  122. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  123. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  124. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  125. "<input type=\"submit\" value=\"OK\">\n"
  126. "</form><p>%s</p></body></html>\n"
  127. "";
  128. int oauth_get_handler(const xs_dict *req, const char *q_path,
  129. char **body, int *b_size, char **ctype)
  130. {
  131. (void)b_size;
  132. if (!xs_startswith(q_path, "/oauth/"))
  133. return 0;
  134. int status = 404;
  135. xs_dict *msg = xs_dict_get(req, "q_vars");
  136. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  137. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  138. if (strcmp(cmd, "/authorize") == 0) { /** **/
  139. const char *cid = xs_dict_get(msg, "client_id");
  140. const char *ruri = xs_dict_get(msg, "redirect_uri");
  141. const char *rtype = xs_dict_get(msg, "response_type");
  142. const char *state = xs_dict_get(msg, "state");
  143. status = 400;
  144. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  145. xs *app = app_get(cid);
  146. if (app != NULL) {
  147. const char *host = xs_dict_get(srv_config, "host");
  148. if (xs_is_null(state))
  149. state = "";
  150. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-login",
  151. ruri, cid, state, USER_AGENT);
  152. *ctype = "text/html";
  153. status = 200;
  154. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  155. }
  156. else
  157. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  158. }
  159. else
  160. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  161. }
  162. else
  163. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  164. const char *host = xs_dict_get(srv_config, "host");
  165. *body = xs_fmt(login_page, host, host, "", host, "oauth/x-snac-get-token",
  166. "", "", "", USER_AGENT);
  167. *ctype = "text/html";
  168. status = 200;
  169. }
  170. return status;
  171. }
  172. int oauth_post_handler(const xs_dict *req, const char *q_path,
  173. const char *payload, int p_size,
  174. char **body, int *b_size, char **ctype)
  175. {
  176. (void)p_size;
  177. (void)b_size;
  178. if (!xs_startswith(q_path, "/oauth/"))
  179. return 0;
  180. int status = 404;
  181. char *i_ctype = xs_dict_get(req, "content-type");
  182. xs *args = NULL;
  183. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  184. if (!xs_is_null(payload))
  185. args = xs_json_loads(payload);
  186. }
  187. else
  188. if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded") && payload) {
  189. xs *upl = xs_url_dec(payload);
  190. args = xs_url_vars(upl);
  191. }
  192. else
  193. args = xs_dup(xs_dict_get(req, "p_vars"));
  194. if (args == NULL)
  195. return 400;
  196. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  197. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  198. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  199. const char *login = xs_dict_get(args, "login");
  200. const char *passwd = xs_dict_get(args, "passwd");
  201. const char *redir = xs_dict_get(args, "redir");
  202. const char *cid = xs_dict_get(args, "cid");
  203. const char *state = xs_dict_get(args, "state");
  204. const char *host = xs_dict_get(srv_config, "host");
  205. /* by default, generate another login form with an error */
  206. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  207. redir, cid, state, USER_AGENT);
  208. *ctype = "text/html";
  209. status = 200;
  210. if (login && passwd && redir && cid) {
  211. snac snac;
  212. if (user_open(&snac, login)) {
  213. /* check the login + password */
  214. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  215. /* success! redirect to the desired uri */
  216. xs *code = random_str();
  217. xs_free(*body);
  218. if (strcmp(redir, "urn:ietf:wg:oauth:2.0:oob") == 0) {
  219. *body = xs_dup(code);
  220. }
  221. else {
  222. *body = xs_fmt("%s?code=%s", redir, code);
  223. status = 303;
  224. }
  225. /* if there is a state, add it */
  226. if (!xs_is_null(state) && *state) {
  227. *body = xs_str_cat(*body, "&state=");
  228. *body = xs_str_cat(*body, state);
  229. }
  230. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  231. login, *body));
  232. /* assign the login to the app */
  233. xs *app = app_get(cid);
  234. if (app != NULL) {
  235. app = xs_dict_set(app, "uid", login);
  236. app = xs_dict_set(app, "code", code);
  237. app_add(cid, app);
  238. }
  239. else
  240. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  241. }
  242. else
  243. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  244. user_free(&snac);
  245. }
  246. else
  247. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  248. }
  249. else
  250. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  251. }
  252. else
  253. if (strcmp(cmd, "/token") == 0) { /** **/
  254. xs *wrk = NULL;
  255. const char *gtype = xs_dict_get(args, "grant_type");
  256. const char *code = xs_dict_get(args, "code");
  257. const char *cid = xs_dict_get(args, "client_id");
  258. const char *csec = xs_dict_get(args, "client_secret");
  259. const char *ruri = xs_dict_get(args, "redirect_uri");
  260. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  261. but if it's enabled, it makes it crash after some more steps, which
  262. is FAR WORSE */
  263. const char *scope = NULL;
  264. // scope = xs_dict_get(args, "scope");
  265. /* no client_secret? check if it's inside an authorization header
  266. (AndStatus does it this way) */
  267. if (xs_is_null(csec)) {
  268. const char *auhdr = xs_dict_get(req, "authorization");
  269. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  270. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  271. int size;
  272. xs *s2 = xs_base64_dec(s1, &size);
  273. if (!xs_is_null(s2)) {
  274. xs *l1 = xs_split(s2, ":");
  275. if (xs_list_len(l1) == 2) {
  276. wrk = xs_dup(xs_list_get(l1, 1));
  277. csec = wrk;
  278. }
  279. }
  280. }
  281. }
  282. /* no code?
  283. I'm not sure of the impacts of this right now, but Subway Tooter does not
  284. provide a code so one must be generated */
  285. if (xs_is_null(code)){
  286. code = random_str();
  287. }
  288. if (gtype && code && cid && csec && ruri) {
  289. xs *app = app_get(cid);
  290. if (app == NULL) {
  291. status = 401;
  292. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  293. }
  294. else
  295. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  296. status = 401;
  297. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  298. }
  299. else {
  300. xs *rsp = xs_dict_new();
  301. xs *cat = xs_number_new(time(NULL));
  302. xs *tokid = random_str();
  303. rsp = xs_dict_append(rsp, "access_token", tokid);
  304. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  305. rsp = xs_dict_append(rsp, "created_at", cat);
  306. if (!xs_is_null(scope))
  307. rsp = xs_dict_append(rsp, "scope", scope);
  308. *body = xs_json_dumps(rsp, 4);
  309. *ctype = "application/json";
  310. status = 200;
  311. const char *uid = xs_dict_get(app, "uid");
  312. srv_debug(1, xs_fmt("oauth token: "
  313. "successful login for %s, new token %s", uid, tokid));
  314. xs *token = xs_dict_new();
  315. token = xs_dict_append(token, "token", tokid);
  316. token = xs_dict_append(token, "client_id", cid);
  317. token = xs_dict_append(token, "client_secret", csec);
  318. token = xs_dict_append(token, "uid", uid);
  319. token = xs_dict_append(token, "code", code);
  320. token_add(tokid, token);
  321. }
  322. }
  323. else {
  324. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  325. status = 400;
  326. }
  327. }
  328. else
  329. if (strcmp(cmd, "/revoke") == 0) { /** **/
  330. const char *cid = xs_dict_get(args, "client_id");
  331. const char *csec = xs_dict_get(args, "client_secret");
  332. const char *tokid = xs_dict_get(args, "token");
  333. if (cid && csec && tokid) {
  334. xs *token = token_get(tokid);
  335. *body = xs_str_new("{}");
  336. *ctype = "application/json";
  337. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  338. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  339. status = 403;
  340. }
  341. else {
  342. token_del(tokid);
  343. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  344. status = 200;
  345. /* also delete the app, as it serves no purpose from now on */
  346. app_del(cid);
  347. }
  348. }
  349. else {
  350. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  351. status = 403;
  352. }
  353. }
  354. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  355. const char *login = xs_dict_get(args, "login");
  356. const char *passwd = xs_dict_get(args, "passwd");
  357. const char *host = xs_dict_get(srv_config, "host");
  358. /* by default, generate another login form with an error */
  359. *body = xs_fmt(login_page, host, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  360. "", "", "", USER_AGENT);
  361. *ctype = "text/html";
  362. status = 200;
  363. if (login && passwd) {
  364. snac user;
  365. if (user_open(&user, login)) {
  366. /* check the login + password */
  367. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  368. /* success! create a new token */
  369. xs *tokid = random_str();
  370. srv_debug(1, xs_fmt("x-snac-new-token: "
  371. "successful login for %s, new token %s", login, tokid));
  372. xs *token = xs_dict_new();
  373. token = xs_dict_append(token, "token", tokid);
  374. token = xs_dict_append(token, "client_id", "snac-client");
  375. token = xs_dict_append(token, "client_secret", "");
  376. token = xs_dict_append(token, "uid", login);
  377. token = xs_dict_append(token, "code", "");
  378. token_add(tokid, token);
  379. *ctype = "text/plain";
  380. xs_free(*body);
  381. *body = xs_dup(tokid);
  382. }
  383. user_free(&user);
  384. }
  385. }
  386. }
  387. return status;
  388. }
  389. xs_str *mastoapi_id(const xs_dict *msg)
  390. /* returns a somewhat Mastodon-compatible status id */
  391. {
  392. const char *id = xs_dict_get(msg, "id");
  393. xs *md5 = xs_md5_hex(id, strlen(id));
  394. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  395. }
  396. #define MID_TO_MD5(id) (id + 10)
  397. xs_dict *mastoapi_account(const xs_dict *actor)
  398. /* converts an ActivityPub actor to a Mastodon account */
  399. {
  400. const char *id = xs_dict_get(actor, "id");
  401. const char *pub = xs_dict_get(actor, "published");
  402. if (xs_type(id) != XSTYPE_STRING)
  403. return NULL;
  404. const char *prefu = xs_dict_get(actor, "preferredUsername");
  405. const char *display_name = xs_dict_get(actor, "name");
  406. if (xs_is_null(display_name) || *display_name == '\0')
  407. display_name = prefu;
  408. xs_dict *acct = xs_dict_new();
  409. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  410. acct = xs_dict_append(acct, "id", acct_md5);
  411. acct = xs_dict_append(acct, "username", prefu);
  412. acct = xs_dict_append(acct, "display_name", display_name);
  413. acct = xs_dict_append(acct, "discoverable", xs_stock(XSTYPE_TRUE));
  414. acct = xs_dict_append(acct, "group", xs_stock(XSTYPE_FALSE));
  415. acct = xs_dict_append(acct, "hide_collections", xs_stock(XSTYPE_FALSE));
  416. acct = xs_dict_append(acct, "indexable", xs_stock(XSTYPE_TRUE));
  417. acct = xs_dict_append(acct, "noindex", xs_stock(XSTYPE_FALSE));
  418. acct = xs_dict_append(acct, "roles", xs_stock(XSTYPE_LIST));
  419. {
  420. /* create the acct field as user@host */
  421. xs *l = xs_split(id, "/");
  422. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  423. acct = xs_dict_append(acct, "acct", fquid);
  424. }
  425. if (pub)
  426. acct = xs_dict_append(acct, "created_at", pub);
  427. else {
  428. /* unset created_at crashes Tusky, so lie like a mf */
  429. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  430. acct = xs_dict_append(acct, "created_at", date);
  431. }
  432. const char *note = xs_dict_get(actor, "summary");
  433. if (xs_is_null(note))
  434. note = "";
  435. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  436. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_TRUE));
  437. else
  438. acct = xs_dict_append(acct, "bot", xs_stock(XSTYPE_FALSE));
  439. acct = xs_dict_append(acct, "note", note);
  440. acct = xs_dict_append(acct, "url", id);
  441. acct = xs_dict_append(acct, "uri", id);
  442. xs *avatar = NULL;
  443. xs_dict *av = xs_dict_get(actor, "icon");
  444. if (xs_type(av) == XSTYPE_DICT) {
  445. char *url = xs_dict_get(av, "url");
  446. if (url != NULL)
  447. avatar = xs_dup(url);
  448. }
  449. if (avatar == NULL)
  450. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  451. acct = xs_dict_append(acct, "avatar", avatar);
  452. acct = xs_dict_append(acct, "avatar_static", avatar);
  453. xs *header = NULL;
  454. xs_dict *hd = xs_dict_get(actor, "image");
  455. if (xs_type(hd) == XSTYPE_DICT)
  456. header = xs_dup(xs_dict_get(hd, "url"));
  457. if (xs_is_null(header))
  458. header = xs_fmt("%s/header.png", srv_baseurl);
  459. acct = xs_dict_append(acct, "header", header);
  460. acct = xs_dict_append(acct, "header_static", header);
  461. /* emojis */
  462. xs_list *p;
  463. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  464. xs *eml = xs_list_new();
  465. xs_dict *v;
  466. while (xs_list_iter(&p, &v)) {
  467. const char *type = xs_dict_get(v, "type");
  468. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  469. const char *name = xs_dict_get(v, "name");
  470. const xs_dict *icon = xs_dict_get(v, "icon");
  471. if (!xs_is_null(name) && !xs_is_null(icon)) {
  472. const char *url = xs_dict_get(icon, "url");
  473. if (!xs_is_null(url)) {
  474. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  475. xs *d1 = xs_dict_new();
  476. d1 = xs_dict_append(d1, "shortcode", nm);
  477. d1 = xs_dict_append(d1, "url", url);
  478. d1 = xs_dict_append(d1, "static_url", url);
  479. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  480. eml = xs_list_append(eml, d1);
  481. }
  482. }
  483. }
  484. }
  485. acct = xs_dict_append(acct, "emojis", eml);
  486. }
  487. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  488. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  489. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  490. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  491. xs *fields = xs_list_new();
  492. p = xs_dict_get(actor, "attachment");
  493. xs_dict *v;
  494. /* dict of validated links */
  495. xs_dict *val_links = NULL;
  496. xs_dict *metadata = xs_stock(XSTYPE_DICT);
  497. snac user = {0};
  498. if (xs_startswith(id, srv_baseurl)) {
  499. /* if it's a local user, open it and pick its validated links */
  500. if (user_open(&user, prefu)) {
  501. val_links = user.links;
  502. metadata = xs_dict_get_def(user.config, "metadata", xs_stock(XSTYPE_DICT));
  503. }
  504. }
  505. if (xs_is_null(val_links))
  506. val_links = xs_stock(XSTYPE_DICT);
  507. while (xs_list_iter(&p, &v)) {
  508. char *type = xs_dict_get(v, "type");
  509. char *name = xs_dict_get(v, "name");
  510. char *value = xs_dict_get(v, "value");
  511. if (!xs_is_null(type) && !xs_is_null(name) &&
  512. !xs_is_null(value) && strcmp(type, "PropertyValue") == 0) {
  513. xs *val_date = NULL;
  514. char *url = xs_dict_get(metadata, name);
  515. if (!xs_is_null(url) && xs_startswith(url, "https:/" "/")) {
  516. xs_number *verified_time = xs_dict_get(val_links, url);
  517. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  518. time_t t = xs_number_get(verified_time);
  519. if (t > 0)
  520. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  521. }
  522. }
  523. xs *d = xs_dict_new();
  524. d = xs_dict_append(d, "name", name);
  525. d = xs_dict_append(d, "value", value);
  526. d = xs_dict_append(d, "verified_at",
  527. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  528. val_date : xs_stock(XSTYPE_NULL));
  529. fields = xs_list_append(fields, d);
  530. }
  531. }
  532. user_free(&user);
  533. acct = xs_dict_append(acct, "fields", fields);
  534. return acct;
  535. }
  536. xs_str *mastoapi_date(char *date)
  537. /* converts an ISO 8601 date to whatever format Mastodon uses */
  538. {
  539. xs_str *s = xs_crop_i(xs_dup(date), 0, 19);
  540. s = xs_str_cat(s, ".000Z");
  541. return s;
  542. }
  543. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  544. /* creates a mastoapi Poll object */
  545. {
  546. xs_dict *poll = xs_dict_new();
  547. xs *mid = mastoapi_id(msg);
  548. xs_list *opts = NULL;
  549. xs_val *v;
  550. int num_votes = 0;
  551. xs *options = xs_list_new();
  552. poll = xs_dict_append(poll, "id", mid);
  553. xs *fd = mastoapi_date(xs_dict_get(msg, "endTime"));
  554. poll = xs_dict_append(poll, "expires_at", fd);
  555. poll = xs_dict_append(poll, "expired",
  556. xs_dict_get(msg, "closed") != NULL ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  557. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  558. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_FALSE));
  559. else {
  560. opts = xs_dict_get(msg, "anyOf");
  561. poll = xs_dict_append(poll, "multiple", xs_stock(XSTYPE_TRUE));
  562. }
  563. while (xs_list_iter(&opts, &v)) {
  564. const char *title = xs_dict_get(v, "name");
  565. const char *replies = xs_dict_get(v, "replies");
  566. if (title && replies) {
  567. const char *votes_count = xs_dict_get(replies, "totalItems");
  568. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  569. xs *d = xs_dict_new();
  570. d = xs_dict_append(d, "title", title);
  571. d = xs_dict_append(d, "votes_count", votes_count);
  572. options = xs_list_append(options, d);
  573. num_votes += xs_number_get(votes_count);
  574. }
  575. }
  576. }
  577. poll = xs_dict_append(poll, "options", options);
  578. xs *vc = xs_number_new(num_votes);
  579. poll = xs_dict_append(poll, "votes_count", vc);
  580. poll = xs_dict_append(poll, "voted",
  581. (snac && was_question_voted(snac, xs_dict_get(msg, "id"))) ?
  582. xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  583. return poll;
  584. }
  585. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  586. /* converts an ActivityPub note to a Mastodon status */
  587. {
  588. xs *actor = NULL;
  589. actor_get_refresh(snac, get_atto(msg), &actor);
  590. /* if the author is not here, discard */
  591. if (actor == NULL)
  592. return NULL;
  593. const char *type = xs_dict_get(msg, "type");
  594. const char *id = xs_dict_get(msg, "id");
  595. /* fail if it's not a valid actor */
  596. if (xs_is_null(type) || xs_is_null(id))
  597. return NULL;
  598. xs *acct = mastoapi_account(actor);
  599. if (acct == NULL)
  600. return NULL;
  601. xs *idx = NULL;
  602. xs *ixc = NULL;
  603. char *tmp;
  604. xs *mid = mastoapi_id(msg);
  605. xs_dict *st = xs_dict_new();
  606. st = xs_dict_append(st, "id", mid);
  607. st = xs_dict_append(st, "uri", id);
  608. st = xs_dict_append(st, "url", id);
  609. st = xs_dict_append(st, "account", acct);
  610. xs *fd = mastoapi_date(xs_dict_get(msg, "published"));
  611. st = xs_dict_append(st, "created_at", fd);
  612. {
  613. const char *content = xs_dict_get(msg, "content");
  614. const char *name = xs_dict_get(msg, "name");
  615. xs *s1 = NULL;
  616. if (name && content)
  617. s1 = xs_fmt("%s<br><br>%s", name, content);
  618. else
  619. if (name)
  620. s1 = xs_dup(name);
  621. else
  622. if (content)
  623. s1 = xs_dup(content);
  624. else
  625. s1 = xs_str_new(NULL);
  626. st = xs_dict_append(st, "content", s1);
  627. }
  628. st = xs_dict_append(st, "visibility",
  629. is_msg_public(msg) ? "public" : "private");
  630. tmp = xs_dict_get(msg, "sensitive");
  631. if (xs_is_null(tmp))
  632. tmp = xs_stock(XSTYPE_FALSE);
  633. st = xs_dict_append(st, "sensitive", tmp);
  634. tmp = xs_dict_get(msg, "summary");
  635. if (xs_is_null(tmp))
  636. tmp = "";
  637. st = xs_dict_append(st, "spoiler_text", tmp);
  638. /* create the list of attachments */
  639. xs *attach = get_attachments(msg);
  640. {
  641. xs_list *p = attach;
  642. xs_dict *v;
  643. xs *matt = xs_list_new();
  644. while (xs_list_iter(&p, &v)) {
  645. char *type = xs_dict_get(v, "type");
  646. char *href = xs_dict_get(v, "href");
  647. char *name = xs_dict_get(v, "name");
  648. if (xs_match(type, "image/*|video/*|Image|Video")) { /* */
  649. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  650. xs *d = xs_dict_new();
  651. d = xs_dict_append(d, "id", matteid);
  652. d = xs_dict_append(d, "url", href);
  653. d = xs_dict_append(d, "preview_url", href);
  654. d = xs_dict_append(d, "remote_url", href);
  655. d = xs_dict_append(d, "description", name);
  656. d = xs_dict_append(d, "type", (*type == 'v' || *type == 'V') ? "video" : "image");
  657. matt = xs_list_append(matt, d);
  658. }
  659. }
  660. st = xs_dict_append(st, "media_attachments", matt);
  661. }
  662. {
  663. xs *ml = xs_list_new();
  664. xs *htl = xs_list_new();
  665. xs *eml = xs_list_new();
  666. xs_list *tag = xs_dict_get(msg, "tag");
  667. int n = 0;
  668. xs *tag_list = NULL;
  669. if (xs_type(tag) == XSTYPE_DICT) {
  670. tag_list = xs_list_new();
  671. tag_list = xs_list_append(tag_list, tag);
  672. }
  673. else
  674. if (xs_type(tag) == XSTYPE_LIST)
  675. tag_list = xs_dup(tag);
  676. else
  677. tag_list = xs_list_new();
  678. tag = tag_list;
  679. xs_dict *v;
  680. while (xs_list_iter(&tag, &v)) {
  681. const char *type = xs_dict_get(v, "type");
  682. if (xs_is_null(type))
  683. continue;
  684. xs *d1 = xs_dict_new();
  685. if (strcmp(type, "Mention") == 0) {
  686. const char *name = xs_dict_get(v, "name");
  687. const char *href = xs_dict_get(v, "href");
  688. if (!xs_is_null(name) && !xs_is_null(href) &&
  689. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  690. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  691. xs *id = xs_fmt("%d", n++);
  692. d1 = xs_dict_append(d1, "id", id);
  693. d1 = xs_dict_append(d1, "username", nm);
  694. d1 = xs_dict_append(d1, "acct", nm);
  695. d1 = xs_dict_append(d1, "url", href);
  696. ml = xs_list_append(ml, d1);
  697. }
  698. }
  699. else
  700. if (strcmp(type, "Hashtag") == 0) {
  701. const char *name = xs_dict_get(v, "name");
  702. const char *href = xs_dict_get(v, "href");
  703. if (!xs_is_null(name) && !xs_is_null(href)) {
  704. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  705. d1 = xs_dict_append(d1, "name", nm);
  706. d1 = xs_dict_append(d1, "url", href);
  707. htl = xs_list_append(htl, d1);
  708. }
  709. }
  710. else
  711. if (strcmp(type, "Emoji") == 0) {
  712. const char *name = xs_dict_get(v, "name");
  713. const xs_dict *icon = xs_dict_get(v, "icon");
  714. if (!xs_is_null(name) && !xs_is_null(icon)) {
  715. const char *url = xs_dict_get(icon, "url");
  716. if (!xs_is_null(url)) {
  717. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  718. d1 = xs_dict_append(d1, "shortcode", nm);
  719. d1 = xs_dict_append(d1, "url", url);
  720. d1 = xs_dict_append(d1, "static_url", url);
  721. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock(XSTYPE_TRUE));
  722. d1 = xs_dict_append(d1, "category", "Emojis");
  723. eml = xs_list_append(eml, d1);
  724. }
  725. }
  726. }
  727. }
  728. st = xs_dict_append(st, "mentions", ml);
  729. st = xs_dict_append(st, "tags", htl);
  730. st = xs_dict_append(st, "emojis", eml);
  731. }
  732. xs_free(idx);
  733. xs_free(ixc);
  734. idx = object_likes(id);
  735. ixc = xs_number_new(xs_list_len(idx));
  736. st = xs_dict_append(st, "favourites_count", ixc);
  737. st = xs_dict_append(st, "favourited",
  738. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  739. xs_free(idx);
  740. xs_free(ixc);
  741. idx = object_announces(id);
  742. ixc = xs_number_new(xs_list_len(idx));
  743. st = xs_dict_append(st, "reblogs_count", ixc);
  744. st = xs_dict_append(st, "reblogged",
  745. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  746. /* get the last person who boosted this */
  747. xs *boosted_by_md5 = NULL;
  748. if (xs_list_len(idx))
  749. boosted_by_md5 = xs_dup(xs_list_get(idx, -1));
  750. xs_free(idx);
  751. xs_free(ixc);
  752. idx = object_children(id);
  753. ixc = xs_number_new(xs_list_len(idx));
  754. st = xs_dict_append(st, "replies_count", ixc);
  755. /* default in_reply_to values */
  756. st = xs_dict_append(st, "in_reply_to_id", xs_stock(XSTYPE_NULL));
  757. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock(XSTYPE_NULL));
  758. tmp = xs_dict_get(msg, "inReplyTo");
  759. if (!xs_is_null(tmp)) {
  760. xs *irto = NULL;
  761. if (valid_status(object_get(tmp, &irto))) {
  762. xs *irt_mid = mastoapi_id(irto);
  763. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  764. char *at = NULL;
  765. if (!xs_is_null(at = get_atto(irto))) {
  766. xs *at_md5 = xs_md5_hex(at, strlen(at));
  767. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  768. }
  769. }
  770. }
  771. st = xs_dict_append(st, "reblog", xs_stock(XSTYPE_NULL));
  772. st = xs_dict_append(st, "card", xs_stock(XSTYPE_NULL));
  773. st = xs_dict_append(st, "language", xs_stock(XSTYPE_NULL));
  774. tmp = xs_dict_get(msg, "sourceContent");
  775. if (xs_is_null(tmp))
  776. tmp = "";
  777. st = xs_dict_append(st, "text", tmp);
  778. tmp = xs_dict_get(msg, "updated");
  779. xs *fd2 = NULL;
  780. if (xs_is_null(tmp))
  781. tmp = xs_stock(XSTYPE_NULL);
  782. else {
  783. fd2 = mastoapi_date(tmp);
  784. tmp = fd2;
  785. }
  786. st = xs_dict_append(st, "edited_at", tmp);
  787. if (strcmp(type, "Question") == 0) {
  788. xs *poll = mastoapi_poll(snac, msg);
  789. st = xs_dict_append(st, "poll", poll);
  790. }
  791. else
  792. st = xs_dict_append(st, "poll", xs_stock(XSTYPE_NULL));
  793. st = xs_dict_append(st, "bookmarked", xs_stock(XSTYPE_FALSE));
  794. st = xs_dict_append(st, "pinned",
  795. (snac && is_pinned(snac, id)) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  796. /* is it a boost? */
  797. if (!xs_is_null(boosted_by_md5)) {
  798. /* create a new dummy status, using st as the 'reblog' field */
  799. xs_dict *bst = xs_dup(st);
  800. xs *b_actor = NULL;
  801. if (valid_status(object_get_by_md5(boosted_by_md5, &b_actor))) {
  802. xs *b_acct = mastoapi_account(b_actor);
  803. xs *fake_uri = NULL;
  804. if (snac)
  805. fake_uri = xs_fmt("%s/d/%s/Announce", snac->actor, mid);
  806. else
  807. fake_uri = xs_fmt("%s#%s", srv_baseurl, mid);
  808. bst = xs_dict_set(bst, "uri", fake_uri);
  809. bst = xs_dict_set(bst, "url", fake_uri);
  810. bst = xs_dict_set(bst, "account", b_acct);
  811. bst = xs_dict_set(bst, "content", "");
  812. bst = xs_dict_set(bst, "reblog", st);
  813. xs_free(st);
  814. st = bst;
  815. }
  816. }
  817. return st;
  818. }
  819. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  820. {
  821. xs_dict *rel = NULL;
  822. xs *actor_o = NULL;
  823. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  824. rel = xs_dict_new();
  825. const char *actor = xs_dict_get(actor_o, "id");
  826. rel = xs_dict_append(rel, "id", md5);
  827. rel = xs_dict_append(rel, "following",
  828. following_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  829. rel = xs_dict_append(rel, "showing_reblogs", xs_stock(XSTYPE_TRUE));
  830. rel = xs_dict_append(rel, "notifying", xs_stock(XSTYPE_FALSE));
  831. rel = xs_dict_append(rel, "followed_by",
  832. follower_check(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  833. rel = xs_dict_append(rel, "blocking",
  834. is_muted(snac, actor) ? xs_stock(XSTYPE_TRUE) : xs_stock(XSTYPE_FALSE));
  835. rel = xs_dict_append(rel, "muting", xs_stock(XSTYPE_FALSE));
  836. rel = xs_dict_append(rel, "muting_notifications", xs_stock(XSTYPE_FALSE));
  837. rel = xs_dict_append(rel, "requested", xs_stock(XSTYPE_FALSE));
  838. rel = xs_dict_append(rel, "domain_blocking", xs_stock(XSTYPE_FALSE));
  839. rel = xs_dict_append(rel, "endorsed", xs_stock(XSTYPE_FALSE));
  840. rel = xs_dict_append(rel, "note", "");
  841. }
  842. return rel;
  843. }
  844. int process_auth_token(snac *snac, const xs_dict *req)
  845. /* processes an authorization token, if there is one */
  846. {
  847. int logged_in = 0;
  848. char *v;
  849. /* if there is an authorization field, try to validate it */
  850. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  851. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  852. xs *token = token_get(tokid);
  853. if (token != NULL) {
  854. const char *uid = xs_dict_get(token, "uid");
  855. if (!xs_is_null(uid) && user_open(snac, uid)) {
  856. logged_in = 1;
  857. /* this counts as a 'login' */
  858. lastlog_write(snac, "mastoapi");
  859. srv_debug(2, xs_fmt("mastoapi auth: valid token for user '%s'", uid));
  860. }
  861. else
  862. srv_log(xs_fmt("mastoapi auth: corrupted token '%s'", tokid));
  863. }
  864. else
  865. srv_log(xs_fmt("mastoapi auth: invalid token '%s'", tokid));
  866. }
  867. return logged_in;
  868. }
  869. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  870. char **body, int *b_size, char **ctype)
  871. {
  872. (void)b_size;
  873. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  874. return 0;
  875. int status = 404;
  876. xs_dict *args = xs_dict_get(req, "q_vars");
  877. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  878. snac snac1 = {0};
  879. int logged_in = process_auth_token(&snac1, req);
  880. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  881. if (logged_in) {
  882. xs *acct = xs_dict_new();
  883. acct = xs_dict_append(acct, "id", snac1.md5);
  884. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  885. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  886. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  887. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  888. acct = xs_dict_append(acct, "last_status_at", xs_dict_get(snac1.config, "published"));
  889. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  890. acct = xs_dict_append(acct, "url", snac1.actor);
  891. acct = xs_dict_append(acct, "locked", xs_stock(XSTYPE_FALSE));
  892. acct = xs_dict_append(acct, "bot", xs_dict_get(snac1.config, "bot"));
  893. xs *src = xs_json_loads("{\"privacy\":\"public\","
  894. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  895. acct = xs_dict_append(acct, "source", src);
  896. xs *avatar = NULL;
  897. char *av = xs_dict_get(snac1.config, "avatar");
  898. if (xs_is_null(av) || *av == '\0')
  899. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  900. else
  901. avatar = xs_dup(av);
  902. acct = xs_dict_append(acct, "avatar", avatar);
  903. acct = xs_dict_append(acct, "avatar_static", avatar);
  904. xs *header = NULL;
  905. char *hd = xs_dict_get(snac1.config, "header");
  906. if (!xs_is_null(hd))
  907. header = xs_dup(hd);
  908. else
  909. header = xs_fmt("%s/header.png", srv_baseurl);
  910. acct = xs_dict_append(acct, "header", header);
  911. acct = xs_dict_append(acct, "header_static", header);
  912. xs_dict *metadata = xs_dict_get(snac1.config, "metadata");
  913. if (xs_type(metadata) == XSTYPE_DICT) {
  914. xs *fields = xs_list_new();
  915. xs_str *k;
  916. xs_str *v;
  917. xs_dict *val_links = snac1.links;
  918. if (xs_is_null(val_links))
  919. val_links = xs_stock(XSTYPE_DICT);
  920. int c = 0;
  921. while (xs_dict_next(metadata, &k, &v, &c)) {
  922. xs *val_date = NULL;
  923. xs_number *verified_time = xs_dict_get(val_links, v);
  924. if (xs_type(verified_time) == XSTYPE_NUMBER) {
  925. time_t t = xs_number_get(verified_time);
  926. if (t > 0)
  927. val_date = xs_str_utctime(t, ISO_DATE_SPEC);
  928. }
  929. xs *d = xs_dict_new();
  930. d = xs_dict_append(d, "name", k);
  931. d = xs_dict_append(d, "value", v);
  932. d = xs_dict_append(d, "verified_at",
  933. xs_type(val_date) == XSTYPE_STRING && *val_date ?
  934. val_date : xs_stock(XSTYPE_NULL));
  935. fields = xs_list_append(fields, d);
  936. }
  937. acct = xs_dict_set(acct, "fields", fields);
  938. }
  939. acct = xs_dict_append(acct, "followers_count", xs_stock(0));
  940. acct = xs_dict_append(acct, "following_count", xs_stock(0));
  941. acct = xs_dict_append(acct, "statuses_count", xs_stock(0));
  942. *body = xs_json_dumps(acct, 4);
  943. *ctype = "application/json";
  944. status = 200;
  945. }
  946. else {
  947. status = 422; // "Unprocessable entity" (no login)
  948. }
  949. }
  950. else
  951. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  952. /* find if an account is followed, blocked, etc. */
  953. /* the account to get relationships about is in args "id[]" */
  954. if (logged_in) {
  955. xs *res = xs_list_new();
  956. const char *md5 = xs_dict_get(args, "id[]");
  957. if (xs_is_null(md5))
  958. md5 = xs_dict_get(args, "id");
  959. if (!xs_is_null(md5)) {
  960. if (xs_type(md5) == XSTYPE_LIST)
  961. md5 = xs_list_get(md5, 0);
  962. xs *rel = mastoapi_relationship(&snac1, md5);
  963. if (rel != NULL)
  964. res = xs_list_append(res, rel);
  965. }
  966. *body = xs_json_dumps(res, 4);
  967. *ctype = "application/json";
  968. status = 200;
  969. }
  970. else
  971. status = 422;
  972. }
  973. else
  974. if (strcmp(cmd, "/v1/accounts/lookup") == 0) { /** **/
  975. /* lookup an account */
  976. char *acct = xs_dict_get(args, "acct");
  977. if (!xs_is_null(acct)) {
  978. xs *s = xs_strip_chars_i(xs_dup(acct), "@");
  979. xs *l = xs_split_n(s, "@", 1);
  980. char *uid = xs_list_get(l, 0);
  981. char *host = xs_list_get(l, 1);
  982. if (uid && (!host || strcmp(host, xs_dict_get(srv_config, "host")) == 0)) {
  983. snac user;
  984. if (user_open(&user, uid)) {
  985. xs *actor = msg_actor(&user);
  986. xs *macct = mastoapi_account(actor);
  987. *body = xs_json_dumps(macct, 4);
  988. *ctype = "application/json";
  989. status = 200;
  990. user_free(&user);
  991. }
  992. }
  993. }
  994. }
  995. else
  996. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  997. /* account-related information */
  998. xs *l = xs_split(cmd, "/");
  999. const char *uid = xs_list_get(l, 3);
  1000. const char *opt = xs_list_get(l, 4);
  1001. if (uid != NULL) {
  1002. snac snac2;
  1003. xs *out = NULL;
  1004. xs *actor = NULL;
  1005. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  1006. /* search for accounts starting with q */
  1007. const char *aq = xs_dict_get(args, "q");
  1008. if (!xs_is_null(aq)) {
  1009. xs *q = xs_tolower_i(xs_dup(aq));
  1010. out = xs_list_new();
  1011. xs *wing = following_list(&snac1);
  1012. xs *wers = follower_list(&snac1);
  1013. xs *ulst = user_list();
  1014. xs_list *p;
  1015. xs_str *v;
  1016. xs_set seen;
  1017. xs_set_init(&seen);
  1018. /* user relations */
  1019. xs_list *lsts[] = { wing, wers, NULL };
  1020. int n;
  1021. for (n = 0; (p = lsts[n]) != NULL; n++) {
  1022. while (xs_list_iter(&p, &v)) {
  1023. /* already seen? skip */
  1024. if (xs_set_add(&seen, v) == 0)
  1025. continue;
  1026. xs *actor = NULL;
  1027. if (valid_status(object_get(v, &actor))) {
  1028. const char *uname = xs_dict_get(actor, "preferredUsername");
  1029. if (!xs_is_null(uname)) {
  1030. xs *luname = xs_tolower_i(xs_dup(uname));
  1031. if (xs_startswith(luname, q)) {
  1032. xs *acct = mastoapi_account(actor);
  1033. out = xs_list_append(out, acct);
  1034. }
  1035. }
  1036. }
  1037. }
  1038. }
  1039. /* local users */
  1040. p = ulst;
  1041. while (xs_list_iter(&p, &v)) {
  1042. snac user;
  1043. /* skip this same user */
  1044. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  1045. continue;
  1046. /* skip if the uid does not start with the query */
  1047. xs *v2 = xs_tolower_i(xs_dup(v));
  1048. if (!xs_startswith(v2, q))
  1049. continue;
  1050. if (user_open(&user, v)) {
  1051. /* if it's not already seen, add it */
  1052. if (xs_set_add(&seen, user.actor) == 1) {
  1053. xs *actor = msg_actor(&user);
  1054. xs *acct = mastoapi_account(actor);
  1055. out = xs_list_append(out, acct);
  1056. }
  1057. user_free(&user);
  1058. }
  1059. }
  1060. xs_set_free(&seen);
  1061. }
  1062. }
  1063. else
  1064. /* is it a local user? */
  1065. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  1066. if (opt == NULL) {
  1067. /* account information */
  1068. actor = msg_actor(&snac2);
  1069. out = mastoapi_account(actor);
  1070. }
  1071. else
  1072. if (strcmp(opt, "statuses") == 0) { /** **/
  1073. /* the public list of posts of a user */
  1074. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  1075. xs_list *p = timeline;
  1076. xs_str *v;
  1077. out = xs_list_new();
  1078. while (xs_list_iter(&p, &v)) {
  1079. xs *msg = NULL;
  1080. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  1081. /* add only posts by the author */
  1082. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  1083. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  1084. xs *st = mastoapi_status(&snac2, msg);
  1085. if (st)
  1086. out = xs_list_append(out, st);
  1087. }
  1088. }
  1089. }
  1090. }
  1091. user_free(&snac2);
  1092. }
  1093. else {
  1094. /* try the uid as the md5 of a possibly loaded actor */
  1095. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  1096. if (opt == NULL) {
  1097. /* account information */
  1098. out = mastoapi_account(actor);
  1099. }
  1100. else
  1101. if (strcmp(opt, "statuses") == 0) {
  1102. /* we don't serve statuses of others; return the empty list */
  1103. out = xs_list_new();
  1104. }
  1105. }
  1106. }
  1107. if (out != NULL) {
  1108. *body = xs_json_dumps(out, 4);
  1109. *ctype = "application/json";
  1110. status = 200;
  1111. }
  1112. }
  1113. }
  1114. else
  1115. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  1116. /* the private timeline */
  1117. if (logged_in) {
  1118. const char *max_id = xs_dict_get(args, "max_id");
  1119. const char *since_id = xs_dict_get(args, "since_id");
  1120. const char *min_id = xs_dict_get(args, "min_id");
  1121. const char *limit_s = xs_dict_get(args, "limit");
  1122. int limit = 0;
  1123. int cnt = 0;
  1124. if (!xs_is_null(limit_s))
  1125. limit = atoi(limit_s);
  1126. if (limit == 0)
  1127. limit = 20;
  1128. xs *timeline = timeline_simple_list(&snac1, "private", 0, 2048);
  1129. xs *out = xs_list_new();
  1130. xs_list *p = timeline;
  1131. xs_str *v;
  1132. while (xs_list_iter(&p, &v) && cnt < limit) {
  1133. xs *msg = NULL;
  1134. /* only return entries older that max_id */
  1135. if (max_id) {
  1136. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  1137. max_id = NULL;
  1138. continue;
  1139. }
  1140. /* only returns entries newer than since_id */
  1141. if (since_id) {
  1142. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  1143. break;
  1144. }
  1145. /* only returns entries newer than min_id */
  1146. /* what does really "Return results immediately newer than ID" mean? */
  1147. if (min_id) {
  1148. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  1149. break;
  1150. }
  1151. /* get the entry */
  1152. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  1153. continue;
  1154. /* discard non-Notes */
  1155. const char *id = xs_dict_get(msg, "id");
  1156. const char *type = xs_dict_get(msg, "type");
  1157. if (!xs_match(type, "Note|Question|Page|Article|Video"))
  1158. continue;
  1159. const char *from = NULL;
  1160. if (strcmp(type, "Page") == 0)
  1161. from = xs_dict_get(msg, "audience");
  1162. if (from == NULL)
  1163. from = get_atto(msg);
  1164. if (from == NULL)
  1165. continue;
  1166. /* is this message from a person we don't follow? */
  1167. if (strcmp(from, snac1.actor) && !following_check(&snac1, from)) {
  1168. /* discard if it was not boosted */
  1169. xs *idx = object_announces(id);
  1170. if (xs_list_len(idx) == 0)
  1171. continue;
  1172. }
  1173. /* discard notes from muted morons */
  1174. if (is_muted(&snac1, from))
  1175. continue;
  1176. /* discard hidden notes */
  1177. if (is_hidden(&snac1, id))
  1178. continue;
  1179. /* if it has a name and it's not a Page or a Video,
  1180. it's a poll vote, so discard it */
  1181. if (!xs_is_null(xs_dict_get(msg, "name")) && !xs_match(type, "Page|Video"))
  1182. continue;
  1183. /* convert the Note into a Mastodon status */
  1184. xs *st = mastoapi_status(&snac1, msg);
  1185. if (st != NULL)
  1186. out = xs_list_append(out, st);
  1187. cnt++;
  1188. }
  1189. *body = xs_json_dumps(out, 4);
  1190. *ctype = "application/json";
  1191. status = 200;
  1192. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1193. }
  1194. else {
  1195. status = 401; // unauthorized
  1196. }
  1197. }
  1198. else
  1199. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1200. /* the instance public timeline (public timelines for all users) */
  1201. const char *limit_s = xs_dict_get(args, "limit");
  1202. int limit = 0;
  1203. int cnt = 0;
  1204. if (!xs_is_null(limit_s))
  1205. limit = atoi(limit_s);
  1206. if (limit == 0)
  1207. limit = 20;
  1208. xs *timeline = timeline_instance_list(0, limit);
  1209. xs *out = xs_list_new();
  1210. xs_list *p = timeline;
  1211. xs_str *md5;
  1212. snac *user = NULL;
  1213. if (logged_in)
  1214. user = &snac1;
  1215. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1216. xs *msg = NULL;
  1217. /* get the entry */
  1218. if (!valid_status(object_get_by_md5(md5, &msg)))
  1219. continue;
  1220. /* discard non-Notes */
  1221. const char *type = xs_dict_get(msg, "type");
  1222. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1223. continue;
  1224. /* discard messages from private users */
  1225. if (is_msg_from_private_user(msg))
  1226. continue;
  1227. /* convert the Note into a Mastodon status */
  1228. xs *st = mastoapi_status(user, msg);
  1229. if (st != NULL) {
  1230. out = xs_list_append(out, st);
  1231. cnt++;
  1232. }
  1233. }
  1234. *body = xs_json_dumps(out, 4);
  1235. *ctype = "application/json";
  1236. status = 200;
  1237. }
  1238. else
  1239. if (xs_startswith(cmd, "/v1/timelines/tag/")) { /** **/
  1240. const char *limit_s = xs_dict_get(args, "limit");
  1241. int limit = 0;
  1242. int cnt = 0;
  1243. if (!xs_is_null(limit_s))
  1244. limit = atoi(limit_s);
  1245. if (limit == 0)
  1246. limit = 20;
  1247. /* get the tag */
  1248. xs *l = xs_split(cmd, "/");
  1249. char *tag = xs_list_get(l, -1);
  1250. xs *timeline = tag_search(tag, 0, limit);
  1251. xs *out = xs_list_new();
  1252. xs_list *p = timeline;
  1253. xs_str *md5;
  1254. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1255. xs *msg = NULL;
  1256. /* get the entry */
  1257. if (!valid_status(object_get_by_md5(md5, &msg)))
  1258. continue;
  1259. /* skip non-public messages */
  1260. if (!is_msg_public(msg))
  1261. continue;
  1262. /* discard messages from private users */
  1263. if (is_msg_from_private_user(msg))
  1264. continue;
  1265. /* convert the Note into a Mastodon status */
  1266. xs *st = mastoapi_status(NULL, msg);
  1267. if (st != NULL) {
  1268. out = xs_list_append(out, st);
  1269. cnt++;
  1270. }
  1271. }
  1272. *body = xs_json_dumps(out, 4);
  1273. *ctype = "application/json";
  1274. status = 200;
  1275. }
  1276. else
  1277. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1278. /* TBD */
  1279. *body = xs_dup("[]");
  1280. *ctype = "application/json";
  1281. status = 200;
  1282. }
  1283. else
  1284. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1285. if (logged_in) {
  1286. xs *l = notify_list(&snac1, 0, 64);
  1287. xs *out = xs_list_new();
  1288. xs_list *p = l;
  1289. xs_dict *v;
  1290. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1291. while (xs_list_iter(&p, &v)) {
  1292. xs *noti = notify_get(&snac1, v);
  1293. if (noti == NULL)
  1294. continue;
  1295. const char *type = xs_dict_get(noti, "type");
  1296. const char *utype = xs_dict_get(noti, "utype");
  1297. const char *objid = xs_dict_get(noti, "objid");
  1298. xs *actor = NULL;
  1299. xs *entry = NULL;
  1300. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1301. continue;
  1302. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1303. continue;
  1304. if (is_hidden(&snac1, objid))
  1305. continue;
  1306. /* convert the type */
  1307. if (strcmp(type, "Like") == 0)
  1308. type = "favourite";
  1309. else
  1310. if (strcmp(type, "Announce") == 0)
  1311. type = "reblog";
  1312. else
  1313. if (strcmp(type, "Follow") == 0)
  1314. type = "follow";
  1315. else
  1316. if (strcmp(type, "Create") == 0)
  1317. type = "mention";
  1318. else
  1319. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1320. type = "poll";
  1321. else
  1322. continue;
  1323. /* excluded type? */
  1324. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1325. continue;
  1326. xs *mn = xs_dict_new();
  1327. mn = xs_dict_append(mn, "type", type);
  1328. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1329. mn = xs_dict_append(mn, "id", id);
  1330. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1331. xs *acct = mastoapi_account(actor);
  1332. mn = xs_dict_append(mn, "account", acct);
  1333. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1334. xs *st = mastoapi_status(&snac1, entry);
  1335. if (st)
  1336. mn = xs_dict_append(mn, "status", st);
  1337. }
  1338. out = xs_list_append(out, mn);
  1339. }
  1340. *body = xs_json_dumps(out, 4);
  1341. *ctype = "application/json";
  1342. status = 200;
  1343. }
  1344. else
  1345. status = 401;
  1346. }
  1347. else
  1348. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1349. /* snac will never have filters */
  1350. *body = xs_dup("[]");
  1351. *ctype = "application/json";
  1352. status = 200;
  1353. }
  1354. else
  1355. if (strcmp(cmd, "/v2/filters") == 0) { /** **/
  1356. /* snac will never have filters
  1357. * but still, without a v2 endpoint a short delay is introduced
  1358. * in some apps */
  1359. *body = xs_dup("[]");
  1360. *ctype = "application/json";
  1361. status = 200;
  1362. }
  1363. else
  1364. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1365. /* snac will never support a list of favourites */
  1366. *body = xs_dup("[]");
  1367. *ctype = "application/json";
  1368. status = 200;
  1369. }
  1370. else
  1371. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1372. /* snac does not support bookmarks */
  1373. *body = xs_dup("[]");
  1374. *ctype = "application/json";
  1375. status = 200;
  1376. }
  1377. else
  1378. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1379. /* snac does not support lists */
  1380. *body = xs_dup("[]");
  1381. *ctype = "application/json";
  1382. status = 200;
  1383. }
  1384. else
  1385. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1386. /* snac does not schedule notes */
  1387. *body = xs_dup("[]");
  1388. *ctype = "application/json";
  1389. status = 200;
  1390. }
  1391. else
  1392. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1393. /* snac does not support optional follow confirmations */
  1394. *body = xs_dup("[]");
  1395. *ctype = "application/json";
  1396. status = 200;
  1397. }
  1398. else
  1399. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1400. /* snac has no announcements (yet?) */
  1401. *body = xs_dup("[]");
  1402. *ctype = "application/json";
  1403. status = 200;
  1404. }
  1405. else
  1406. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1407. /* are you kidding me? */
  1408. *body = xs_dup("[]");
  1409. *ctype = "application/json";
  1410. status = 200;
  1411. }
  1412. else
  1413. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1414. /* returns an instance object */
  1415. xs *ins = xs_dict_new();
  1416. const char *host = xs_dict_get(srv_config, "host");
  1417. const char *title = xs_dict_get(srv_config, "title");
  1418. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1419. ins = xs_dict_append(ins, "uri", host);
  1420. ins = xs_dict_append(ins, "domain", host);
  1421. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1422. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1423. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1424. ins = xs_dict_append(ins, "description", host);
  1425. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1426. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1427. ins = xs_dict_append(ins, "thumbnail", susie);
  1428. const char *v = xs_dict_get(srv_config, "admin_email");
  1429. if (xs_is_null(v) || *v == '\0')
  1430. v = "admin@localhost";
  1431. ins = xs_dict_append(ins, "email", v);
  1432. ins = xs_dict_append(ins, "rules", xs_stock(XSTYPE_LIST));
  1433. xs *l1 = xs_list_append(xs_list_new(), "en");
  1434. ins = xs_dict_append(ins, "languages", l1);
  1435. xs *wss = xs_fmt("wss:/" "/%s", xs_dict_get(srv_config, "host"));
  1436. xs *urls = xs_dict_new();
  1437. urls = xs_dict_append(urls, "streaming_api", wss);
  1438. ins = xs_dict_append(ins, "urls", urls);
  1439. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock(0));
  1440. d2 = xs_dict_append(d2, "status_count", xs_stock(0));
  1441. d2 = xs_dict_append(d2, "domain_count", xs_stock(0));
  1442. ins = xs_dict_append(ins, "stats", d2);
  1443. ins = xs_dict_append(ins, "registrations", xs_stock(XSTYPE_FALSE));
  1444. ins = xs_dict_append(ins, "approval_required", xs_stock(XSTYPE_FALSE));
  1445. ins = xs_dict_append(ins, "invites_enabled", xs_stock(XSTYPE_FALSE));
  1446. xs *cfg = xs_dict_new();
  1447. {
  1448. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1449. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1450. cfg = xs_dict_append(cfg, "statuses", d11);
  1451. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1452. cfg = xs_dict_append(cfg, "accounts", d12);
  1453. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1454. "\"image_size_limit\":16777216,"
  1455. "\"video_frame_rate_limit\":120,"
  1456. "\"video_matrix_limit\":8294400,"
  1457. "\"video_size_limit\":103809024}"
  1458. );
  1459. {
  1460. /* get the supported mime types from the internal list */
  1461. const char **p = xs_mime_types;
  1462. xs_set mtypes;
  1463. xs_set_init(&mtypes);
  1464. while (*p) {
  1465. const char *type = p[1];
  1466. if (xs_startswith(type, "image/") ||
  1467. xs_startswith(type, "video/") ||
  1468. xs_startswith(type, "audio/"))
  1469. xs_set_add(&mtypes, type);
  1470. p += 2;
  1471. }
  1472. xs *l = xs_set_result(&mtypes);
  1473. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1474. }
  1475. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1476. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1477. "\"max_expiration\":2629746,"
  1478. "\"max_options\":8,\"min_expiration\":300}");
  1479. cfg = xs_dict_append(cfg, "polls", d14);
  1480. }
  1481. ins = xs_dict_append(ins, "configuration", cfg);
  1482. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1483. if (!xs_is_null(admin_account) && *admin_account) {
  1484. snac admin;
  1485. if (user_open(&admin, admin_account)) {
  1486. xs *actor = msg_actor(&admin);
  1487. xs *acct = mastoapi_account(actor);
  1488. ins = xs_dict_append(ins, "contact_account", acct);
  1489. user_free(&admin);
  1490. }
  1491. }
  1492. *body = xs_json_dumps(ins, 4);
  1493. *ctype = "application/json";
  1494. status = 200;
  1495. }
  1496. else
  1497. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1498. /* information about a status */
  1499. if (logged_in) {
  1500. xs *l = xs_split(cmd, "/");
  1501. const char *id = xs_list_get(l, 3);
  1502. const char *op = xs_list_get(l, 4);
  1503. if (!xs_is_null(id)) {
  1504. xs *msg = NULL;
  1505. xs *out = NULL;
  1506. /* skip the 'fake' part of the id */
  1507. id = MID_TO_MD5(id);
  1508. if (valid_status(object_get_by_md5(id, &msg))) {
  1509. if (op == NULL) {
  1510. if (!is_muted(&snac1, get_atto(msg))) {
  1511. /* return the status itself */
  1512. out = mastoapi_status(&snac1, msg);
  1513. }
  1514. }
  1515. else
  1516. if (strcmp(op, "context") == 0) { /** **/
  1517. /* return ancestors and children */
  1518. xs *anc = xs_list_new();
  1519. xs *des = xs_list_new();
  1520. xs_list *p;
  1521. xs_str *v;
  1522. char pid[64];
  1523. /* build the [grand]parent list, moving up */
  1524. strncpy(pid, id, sizeof(pid));
  1525. while (object_parent(pid, pid, sizeof(pid))) {
  1526. xs *m2 = NULL;
  1527. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1528. xs *st = mastoapi_status(&snac1, m2);
  1529. if (st)
  1530. anc = xs_list_insert(anc, 0, st);
  1531. }
  1532. else
  1533. break;
  1534. }
  1535. /* build the children list */
  1536. xs *children = object_children(xs_dict_get(msg, "id"));
  1537. p = children;
  1538. while (xs_list_iter(&p, &v)) {
  1539. xs *m2 = NULL;
  1540. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1541. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1542. xs *st = mastoapi_status(&snac1, m2);
  1543. if (st)
  1544. des = xs_list_append(des, st);
  1545. }
  1546. }
  1547. }
  1548. out = xs_dict_new();
  1549. out = xs_dict_append(out, "ancestors", anc);
  1550. out = xs_dict_append(out, "descendants", des);
  1551. }
  1552. else
  1553. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1554. strcmp(op, "favourited_by") == 0) { /** **/
  1555. /* return the list of people who liked or boosted this */
  1556. out = xs_list_new();
  1557. xs *l = NULL;
  1558. if (op[0] == 'r')
  1559. l = object_announces(xs_dict_get(msg, "id"));
  1560. else
  1561. l = object_likes(xs_dict_get(msg, "id"));
  1562. xs_list *p = l;
  1563. xs_str *v;
  1564. while (xs_list_iter(&p, &v)) {
  1565. xs *actor2 = NULL;
  1566. if (valid_status(object_get_by_md5(v, &actor2))) {
  1567. xs *acct2 = mastoapi_account(actor2);
  1568. out = xs_list_append(out, acct2);
  1569. }
  1570. }
  1571. }
  1572. else
  1573. if (strcmp(op, "source") == 0) { /** **/
  1574. out = xs_dict_new();
  1575. /* get the mastoapi status id */
  1576. out = xs_dict_append(out, "id", xs_list_get(l, 3));
  1577. out = xs_dict_append(out, "text", xs_dict_get(msg, "sourceContent"));
  1578. out = xs_dict_append(out, "spoiler_text", xs_dict_get(msg, "summary"));
  1579. }
  1580. }
  1581. else
  1582. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1583. if (out != NULL) {
  1584. *body = xs_json_dumps(out, 4);
  1585. *ctype = "application/json";
  1586. status = 200;
  1587. }
  1588. }
  1589. }
  1590. else
  1591. status = 401;
  1592. }
  1593. else
  1594. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1595. *body = xs_dup("{}");
  1596. *ctype = "application/json";
  1597. status = 200;
  1598. }
  1599. else
  1600. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1601. *body = xs_dup("{}");
  1602. *ctype = "application/json";
  1603. status = 200;
  1604. }
  1605. else
  1606. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1607. *body = xs_dup("[]");
  1608. *ctype = "application/json";
  1609. status = 200;
  1610. }
  1611. else
  1612. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1613. if (logged_in) {
  1614. const char *q = xs_dict_get(args, "q");
  1615. const char *type = xs_dict_get(args, "type");
  1616. const char *offset = xs_dict_get(args, "offset");
  1617. xs *acl = xs_list_new();
  1618. xs *stl = xs_list_new();
  1619. xs *htl = xs_list_new();
  1620. xs *res = xs_dict_new();
  1621. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1622. /* reply something only for offset 0; otherwise,
  1623. apps like Tusky keep asking again and again */
  1624. if (!xs_is_null(q) && !xs_is_null(type)) {
  1625. if (strcmp(type, "accounts") == 0) {
  1626. /* do a webfinger query */
  1627. char *actor = NULL;
  1628. char *user = NULL;
  1629. if (valid_status(webfinger_request(q, &actor, &user)) && actor) {
  1630. xs *actor_o = NULL;
  1631. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1632. xs *acct = mastoapi_account(actor_o);
  1633. acl = xs_list_append(acl, acct);
  1634. }
  1635. }
  1636. }
  1637. else
  1638. if (strcmp(type, "hashtags") == 0) {
  1639. /* search this tag */
  1640. xs *tl = tag_search((char *)q, 0, 1);
  1641. if (xs_list_len(tl)) {
  1642. xs *d = xs_dict_new();
  1643. d = xs_dict_append(d, "name", q);
  1644. xs *url = xs_fmt("%s?t=%s", srv_baseurl, q);
  1645. d = xs_dict_append(d, "url", url);
  1646. d = xs_dict_append(d, "history", xs_stock(XSTYPE_LIST));
  1647. htl = xs_list_append(htl, d);
  1648. }
  1649. }
  1650. }
  1651. }
  1652. res = xs_dict_append(res, "accounts", acl);
  1653. res = xs_dict_append(res, "statuses", stl);
  1654. res = xs_dict_append(res, "hashtags", htl);
  1655. *body = xs_json_dumps(res, 4);
  1656. *ctype = "application/json";
  1657. status = 200;
  1658. }
  1659. else
  1660. status = 401;
  1661. }
  1662. /* user cleanup */
  1663. if (logged_in)
  1664. user_free(&snac1);
  1665. srv_debug(1, xs_fmt("mastoapi_get_handler %s %d", q_path, status));
  1666. return status;
  1667. }
  1668. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1669. const char *payload, int p_size,
  1670. char **body, int *b_size, char **ctype)
  1671. {
  1672. (void)p_size;
  1673. (void)b_size;
  1674. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1675. return 0;
  1676. int status = 404;
  1677. xs *args = NULL;
  1678. char *i_ctype = xs_dict_get(req, "content-type");
  1679. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  1680. if (!xs_is_null(payload))
  1681. args = xs_json_loads(payload);
  1682. }
  1683. else if (i_ctype && xs_startswith(i_ctype, "application/x-www-form-urlencoded"))
  1684. {
  1685. // Some apps send form data instead of json so we should cater for those
  1686. if (!xs_is_null(payload)) {
  1687. xs *upl = xs_url_dec(payload);
  1688. args = xs_url_vars(upl);
  1689. }
  1690. }
  1691. else
  1692. args = xs_dup(xs_dict_get(req, "p_vars"));
  1693. if (args == NULL)
  1694. return 400;
  1695. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1696. snac snac = {0};
  1697. int logged_in = process_auth_token(&snac, req);
  1698. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1699. const char *name = xs_dict_get(args, "client_name");
  1700. const char *ruri = xs_dict_get(args, "redirect_uris");
  1701. const char *scope = xs_dict_get(args, "scope");
  1702. if (xs_type(ruri) == XSTYPE_LIST)
  1703. ruri = xs_dict_get(ruri, 0);
  1704. if (name && ruri) {
  1705. xs *app = xs_dict_new();
  1706. xs *id = xs_replace_i(tid(0), ".", "");
  1707. xs *csec = random_str();
  1708. xs *vkey = random_str();
  1709. xs *cid = NULL;
  1710. /* pick a non-existent random cid */
  1711. for (;;) {
  1712. cid = random_str();
  1713. xs *p_app = app_get(cid);
  1714. if (p_app == NULL)
  1715. break;
  1716. xs_free(cid);
  1717. }
  1718. app = xs_dict_append(app, "name", name);
  1719. app = xs_dict_append(app, "redirect_uri", ruri);
  1720. app = xs_dict_append(app, "client_id", cid);
  1721. app = xs_dict_append(app, "client_secret", csec);
  1722. app = xs_dict_append(app, "vapid_key", vkey);
  1723. app = xs_dict_append(app, "id", id);
  1724. *body = xs_json_dumps(app, 4);
  1725. *ctype = "application/json";
  1726. status = 200;
  1727. app = xs_dict_append(app, "code", "");
  1728. if (scope)
  1729. app = xs_dict_append(app, "scope", scope);
  1730. app_add(cid, app);
  1731. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1732. }
  1733. }
  1734. else
  1735. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1736. if (logged_in) {
  1737. /* post a new Note */
  1738. const char *content = xs_dict_get(args, "status");
  1739. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1740. const char *visibility = xs_dict_get(args, "visibility");
  1741. const char *summary = xs_dict_get(args, "spoiler_text");
  1742. const char *media_ids = xs_dict_get(args, "media_ids");
  1743. if (xs_is_null(media_ids))
  1744. media_ids = xs_dict_get(args, "media_ids[]");
  1745. if (xs_is_null(media_ids))
  1746. media_ids = xs_dict_get(args, "media_ids");
  1747. if (xs_is_null(visibility))
  1748. visibility = "public";
  1749. xs *attach_list = xs_list_new();
  1750. xs *irt = NULL;
  1751. /* is it a reply? */
  1752. if (mid != NULL) {
  1753. xs *r_msg = NULL;
  1754. const char *md5 = MID_TO_MD5(mid);
  1755. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1756. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1757. }
  1758. /* does it have attachments? */
  1759. if (!xs_is_null(media_ids)) {
  1760. xs *mi = NULL;
  1761. if (xs_type(media_ids) == XSTYPE_LIST)
  1762. mi = xs_dup(media_ids);
  1763. else {
  1764. mi = xs_list_new();
  1765. mi = xs_list_append(mi, media_ids);
  1766. }
  1767. xs_list *p = mi;
  1768. xs_str *v;
  1769. while (xs_list_iter(&p, &v)) {
  1770. xs *l = xs_list_new();
  1771. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1772. xs *desc = static_get_meta(&snac, v);
  1773. l = xs_list_append(l, url);
  1774. l = xs_list_append(l, desc);
  1775. attach_list = xs_list_append(attach_list, l);
  1776. }
  1777. }
  1778. /* prepare the message */
  1779. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1780. strcmp(visibility, "public") == 0 ? 0 : 1);
  1781. if (!xs_is_null(summary) && *summary) {
  1782. msg = xs_dict_set(msg, "sensitive", xs_stock(XSTYPE_TRUE));
  1783. msg = xs_dict_set(msg, "summary", summary);
  1784. }
  1785. /* store */
  1786. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1787. /* 'Create' message */
  1788. xs *c_msg = msg_create(&snac, msg);
  1789. enqueue_message(&snac, c_msg);
  1790. timeline_touch(&snac);
  1791. /* convert to a mastodon status as a response code */
  1792. xs *st = mastoapi_status(&snac, msg);
  1793. *body = xs_json_dumps(st, 4);
  1794. *ctype = "application/json";
  1795. status = 200;
  1796. }
  1797. else
  1798. status = 401;
  1799. }
  1800. else
  1801. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1802. if (logged_in) {
  1803. /* operations on a status */
  1804. xs *l = xs_split(cmd, "/");
  1805. const char *mid = xs_list_get(l, 3);
  1806. const char *op = xs_list_get(l, 4);
  1807. if (!xs_is_null(mid)) {
  1808. xs *msg = NULL;
  1809. xs *out = NULL;
  1810. /* skip the 'fake' part of the id */
  1811. mid = MID_TO_MD5(mid);
  1812. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1813. char *id = xs_dict_get(msg, "id");
  1814. if (op == NULL) {
  1815. /* no operation (?) */
  1816. }
  1817. else
  1818. if (strcmp(op, "favourite") == 0) { /** **/
  1819. xs *n_msg = msg_admiration(&snac, id, "Like");
  1820. if (n_msg != NULL) {
  1821. enqueue_message(&snac, n_msg);
  1822. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1823. out = mastoapi_status(&snac, msg);
  1824. }
  1825. }
  1826. else
  1827. if (strcmp(op, "unfavourite") == 0) { /** **/
  1828. xs *n_msg = msg_repulsion(&snac, id, "Like");
  1829. if (n_msg != NULL) {
  1830. enqueue_message(&snac, n_msg);
  1831. out = mastoapi_status(&snac, msg);
  1832. }
  1833. }
  1834. else
  1835. if (strcmp(op, "reblog") == 0) { /** **/
  1836. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1837. if (n_msg != NULL) {
  1838. enqueue_message(&snac, n_msg);
  1839. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1840. out = mastoapi_status(&snac, msg);
  1841. }
  1842. }
  1843. else
  1844. if (strcmp(op, "unreblog") == 0) { /** **/
  1845. xs *n_msg = msg_repulsion(&snac, id, "Announce");
  1846. if (n_msg != NULL) {
  1847. enqueue_message(&snac, n_msg);
  1848. out = mastoapi_status(&snac, msg);
  1849. }
  1850. }
  1851. else
  1852. if (strcmp(op, "bookmark") == 0) { /** **/
  1853. /* snac does not support bookmarks */
  1854. }
  1855. else
  1856. if (strcmp(op, "unbookmark") == 0) { /** **/
  1857. /* snac does not support bookmarks */
  1858. }
  1859. else
  1860. if (strcmp(op, "pin") == 0) { /** **/
  1861. /* pin this message */
  1862. if (pin(&snac, id))
  1863. out = mastoapi_status(&snac, msg);
  1864. else
  1865. status = 422;
  1866. }
  1867. else
  1868. if (strcmp(op, "unpin") == 0) { /** **/
  1869. /* unpin this message */
  1870. unpin(&snac, id);
  1871. out = mastoapi_status(&snac, msg);
  1872. }
  1873. else
  1874. if (strcmp(op, "mute") == 0) { /** **/
  1875. /* Mastodon's mute is snac's hide */
  1876. }
  1877. else
  1878. if (strcmp(op, "unmute") == 0) { /** **/
  1879. /* Mastodon's unmute is snac's unhide */
  1880. }
  1881. }
  1882. if (out != NULL) {
  1883. *body = xs_json_dumps(out, 4);
  1884. *ctype = "application/json";
  1885. status = 200;
  1886. }
  1887. }
  1888. }
  1889. else
  1890. status = 401;
  1891. }
  1892. else
  1893. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1894. if (logged_in) {
  1895. notify_clear(&snac);
  1896. timeline_touch(&snac);
  1897. *body = xs_dup("{}");
  1898. *ctype = "application/json";
  1899. status = 200;
  1900. }
  1901. else
  1902. status = 401;
  1903. }
  1904. else
  1905. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1906. /* I don't know what I'm doing */
  1907. if (logged_in) {
  1908. char *v;
  1909. xs *wpush = xs_dict_new();
  1910. wpush = xs_dict_append(wpush, "id", "1");
  1911. v = xs_dict_get(args, "data");
  1912. v = xs_dict_get(v, "alerts");
  1913. wpush = xs_dict_append(wpush, "alerts", v);
  1914. v = xs_dict_get(args, "subscription");
  1915. v = xs_dict_get(v, "endpoint");
  1916. wpush = xs_dict_append(wpush, "endpoint", v);
  1917. xs *server_key = random_str();
  1918. wpush = xs_dict_append(wpush, "server_key", server_key);
  1919. *body = xs_json_dumps(wpush, 4);
  1920. *ctype = "application/json";
  1921. status = 200;
  1922. }
  1923. else
  1924. status = 401;
  1925. }
  1926. else
  1927. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1928. if (logged_in) {
  1929. const xs_list *file = xs_dict_get(args, "file");
  1930. const char *desc = xs_dict_get(args, "description");
  1931. if (xs_is_null(desc))
  1932. desc = "";
  1933. status = 400;
  1934. if (xs_type(file) == XSTYPE_LIST) {
  1935. const char *fn = xs_list_get(file, 0);
  1936. if (*fn != '\0') {
  1937. char *ext = strrchr(fn, '.');
  1938. xs *hash = xs_md5_hex(fn, strlen(fn));
  1939. xs *id = xs_fmt("%s%s", hash, ext);
  1940. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1941. int fo = xs_number_get(xs_list_get(file, 1));
  1942. int fs = xs_number_get(xs_list_get(file, 2));
  1943. /* store */
  1944. static_put(&snac, id, payload + fo, fs);
  1945. static_put_meta(&snac, id, desc);
  1946. /* prepare a response */
  1947. xs *rsp = xs_dict_new();
  1948. rsp = xs_dict_append(rsp, "id", id);
  1949. rsp = xs_dict_append(rsp, "type", "image");
  1950. rsp = xs_dict_append(rsp, "url", url);
  1951. rsp = xs_dict_append(rsp, "preview_url", url);
  1952. rsp = xs_dict_append(rsp, "remote_url", url);
  1953. rsp = xs_dict_append(rsp, "description", desc);
  1954. *body = xs_json_dumps(rsp, 4);
  1955. *ctype = "application/json";
  1956. status = 200;
  1957. }
  1958. }
  1959. }
  1960. else
  1961. status = 401;
  1962. }
  1963. else
  1964. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1965. if (logged_in) {
  1966. /* account-related information */
  1967. xs *l = xs_split(cmd, "/");
  1968. const char *md5 = xs_list_get(l, 3);
  1969. const char *opt = xs_list_get(l, 4);
  1970. xs *rsp = NULL;
  1971. if (!xs_is_null(md5) && *md5) {
  1972. xs *actor_o = NULL;
  1973. if (xs_is_null(opt)) {
  1974. /* ? */
  1975. }
  1976. else
  1977. if (strcmp(opt, "follow") == 0) { /** **/
  1978. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1979. const char *actor = xs_dict_get(actor_o, "id");
  1980. xs *msg = msg_follow(&snac, actor);
  1981. if (msg != NULL) {
  1982. /* reload the actor from the message, in may be different */
  1983. actor = xs_dict_get(msg, "object");
  1984. following_add(&snac, actor, msg);
  1985. enqueue_output_by_actor(&snac, msg, actor, 0);
  1986. rsp = mastoapi_relationship(&snac, md5);
  1987. }
  1988. }
  1989. }
  1990. else
  1991. if (strcmp(opt, "unfollow") == 0) { /** **/
  1992. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1993. const char *actor = xs_dict_get(actor_o, "id");
  1994. /* get the following object */
  1995. xs *object = NULL;
  1996. if (valid_status(following_get(&snac, actor, &object))) {
  1997. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1998. following_del(&snac, actor);
  1999. enqueue_output_by_actor(&snac, msg, actor, 0);
  2000. rsp = mastoapi_relationship(&snac, md5);
  2001. }
  2002. }
  2003. }
  2004. else
  2005. if (strcmp(opt, "block") == 0) { /** **/
  2006. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2007. const char *actor = xs_dict_get(actor_o, "id");
  2008. mute(&snac, actor);
  2009. rsp = mastoapi_relationship(&snac, md5);
  2010. }
  2011. }
  2012. else
  2013. if (strcmp(opt, "unblock") == 0) { /** **/
  2014. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  2015. const char *actor = xs_dict_get(actor_o, "id");
  2016. unmute(&snac, actor);
  2017. rsp = mastoapi_relationship(&snac, md5);
  2018. }
  2019. }
  2020. }
  2021. if (rsp != NULL) {
  2022. *body = xs_json_dumps(rsp, 4);
  2023. *ctype = "application/json";
  2024. status = 200;
  2025. }
  2026. }
  2027. else
  2028. status = 401;
  2029. }
  2030. else
  2031. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  2032. if (logged_in) {
  2033. /* operations on a status */
  2034. xs *l = xs_split(cmd, "/");
  2035. const char *mid = xs_list_get(l, 3);
  2036. const char *op = xs_list_get(l, 4);
  2037. if (!xs_is_null(mid)) {
  2038. xs *msg = NULL;
  2039. xs *out = NULL;
  2040. /* skip the 'fake' part of the id */
  2041. mid = MID_TO_MD5(mid);
  2042. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  2043. const char *id = xs_dict_get(msg, "id");
  2044. const char *atto = get_atto(msg);
  2045. xs_list *opts = xs_dict_get(msg, "oneOf");
  2046. if (opts == NULL)
  2047. opts = xs_dict_get(msg, "anyOf");
  2048. if (op == NULL) {
  2049. }
  2050. else
  2051. if (strcmp(op, "votes") == 0) {
  2052. xs_list *choices = xs_dict_get(args, "choices[]");
  2053. if (xs_is_null(choices))
  2054. choices = xs_dict_get(args, "choices");
  2055. if (xs_type(choices) == XSTYPE_LIST) {
  2056. xs_str *v;
  2057. while (xs_list_iter(&choices, &v)) {
  2058. int io = atoi(v);
  2059. const xs_dict *o = xs_list_get(opts, io);
  2060. if (o) {
  2061. const char *name = xs_dict_get(o, "name");
  2062. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  2063. msg = xs_dict_append(msg, "name", name);
  2064. xs *c_msg = msg_create(&snac, msg);
  2065. enqueue_message(&snac, c_msg);
  2066. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  2067. }
  2068. }
  2069. out = mastoapi_poll(&snac, msg);
  2070. }
  2071. }
  2072. }
  2073. if (out != NULL) {
  2074. *body = xs_json_dumps(out, 4);
  2075. *ctype = "application/json";
  2076. status = 200;
  2077. }
  2078. }
  2079. }
  2080. else
  2081. status = 401;
  2082. }
  2083. /* user cleanup */
  2084. if (logged_in)
  2085. user_free(&snac);
  2086. srv_debug(1, xs_fmt("mastoapi_post_handler %s %d", q_path, status));
  2087. return status;
  2088. }
  2089. int mastoapi_delete_handler(const xs_dict *req, const char *q_path,
  2090. char **body, int *b_size, char **ctype) {
  2091. (void)req;
  2092. (void)body;
  2093. (void)b_size;
  2094. (void)ctype;
  2095. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2096. return 0;
  2097. srv_debug(1, xs_fmt("mastoapi_delete_handler %s", q_path));
  2098. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2099. if (xs_startswith(cmd, "/v1/push/subscription") || xs_startswith(cmd, "/v2/push/subscription")) { /** **/
  2100. // pretend we deleted it, since it doesn't exist anyway
  2101. return 200;
  2102. }
  2103. return 0;
  2104. }
  2105. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  2106. const char *payload, int p_size,
  2107. char **body, int *b_size, char **ctype)
  2108. {
  2109. (void)p_size;
  2110. (void)b_size;
  2111. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  2112. return 0;
  2113. int status = 404;
  2114. xs *args = NULL;
  2115. char *i_ctype = xs_dict_get(req, "content-type");
  2116. if (i_ctype && xs_startswith(i_ctype, "application/json")) {
  2117. if (!xs_is_null(payload))
  2118. args = xs_json_loads(payload);
  2119. }
  2120. else
  2121. args = xs_dup(xs_dict_get(req, "p_vars"));
  2122. if (args == NULL)
  2123. return 400;
  2124. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  2125. snac snac = {0};
  2126. int logged_in = process_auth_token(&snac, req);
  2127. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  2128. if (logged_in) {
  2129. xs *l = xs_split(cmd, "/");
  2130. const char *stid = xs_list_get(l, 3);
  2131. if (!xs_is_null(stid)) {
  2132. const char *desc = xs_dict_get(args, "description");
  2133. /* set the image metadata */
  2134. static_put_meta(&snac, stid, desc);
  2135. /* prepare a response */
  2136. xs *rsp = xs_dict_new();
  2137. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  2138. rsp = xs_dict_append(rsp, "id", stid);
  2139. rsp = xs_dict_append(rsp, "type", "image");
  2140. rsp = xs_dict_append(rsp, "url", url);
  2141. rsp = xs_dict_append(rsp, "preview_url", url);
  2142. rsp = xs_dict_append(rsp, "remote_url", url);
  2143. rsp = xs_dict_append(rsp, "description", desc);
  2144. *body = xs_json_dumps(rsp, 4);
  2145. *ctype = "application/json";
  2146. status = 200;
  2147. }
  2148. }
  2149. else
  2150. status = 401;
  2151. }
  2152. else
  2153. if (xs_startswith(cmd, "/v1/statuses")) {
  2154. if (logged_in) {
  2155. xs *l = xs_split(cmd, "/");
  2156. const char *mid = xs_list_get(l, 3);
  2157. if (!xs_is_null(mid)) {
  2158. const char *md5 = MID_TO_MD5(mid);
  2159. xs *rsp = NULL;
  2160. xs *msg = NULL;
  2161. if (valid_status(timeline_get_by_md5(&snac, md5, &msg))) {
  2162. const char *content = xs_dict_get(args, "status");
  2163. xs *atls = xs_list_new();
  2164. xs *f_content = not_really_markdown(content, &atls, NULL);
  2165. /* replace fields with new content */
  2166. msg = xs_dict_set(msg, "sourceContent", content);
  2167. msg = xs_dict_set(msg, "content", f_content);
  2168. xs *updated = xs_str_utctime(0, ISO_DATE_SPEC);
  2169. msg = xs_dict_set(msg, "updated", updated);
  2170. /* overwrite object, not updating the indexes */
  2171. object_add_ow(xs_dict_get(msg, "id"), msg);
  2172. /* update message */
  2173. xs *c_msg = msg_update(&snac, msg);
  2174. enqueue_message(&snac, c_msg);
  2175. rsp = mastoapi_status(&snac, msg);
  2176. }
  2177. if (rsp != NULL) {
  2178. *body = xs_json_dumps(rsp, 4);
  2179. *ctype = "application/json";
  2180. status = 200;
  2181. }
  2182. }
  2183. }
  2184. else
  2185. status = 401;
  2186. }
  2187. /* user cleanup */
  2188. if (logged_in)
  2189. user_free(&snac);
  2190. srv_debug(1, xs_fmt("mastoapi_put_handler %s %d", q_path, status));
  2191. return status;
  2192. }
  2193. void mastoapi_purge(void)
  2194. {
  2195. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  2196. xs *files = xs_glob(spec, 1, 0);
  2197. xs_list *p = files;
  2198. xs_str *v;
  2199. time_t mt = time(NULL) - 3600;
  2200. while (xs_list_iter(&p, &v)) {
  2201. xs *cid = xs_replace(v, ".json", "");
  2202. xs *fn = _app_fn(cid);
  2203. if (mtime(fn) < mt) {
  2204. /* get the app */
  2205. xs *app = app_get(cid);
  2206. if (app) {
  2207. /* old apps with no uid are incomplete cruft */
  2208. const char *uid = xs_dict_get(app, "uid");
  2209. if (xs_is_null(uid) || *uid == '\0') {
  2210. unlink(fn);
  2211. srv_debug(2, xs_fmt("purged %s", fn));
  2212. }
  2213. }
  2214. }
  2215. }
  2216. }
  2217. #endif /* #ifndef NO_MASTODON_API */