mastoapi.c 87 KB

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