mastoapi.c 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink et al. / MIT license */
  3. #ifndef NO_MASTODON_API
  4. #include "xs.h"
  5. #include "xs_openssl.h"
  6. #include "xs_json.h"
  7. #include "xs_io.h"
  8. #include "xs_time.h"
  9. #include "xs_glob.h"
  10. #include "xs_set.h"
  11. #include "xs_random.h"
  12. #include "snac.h"
  13. static xs_str *random_str(void)
  14. /* just what is says in the tin */
  15. {
  16. unsigned int data[4] = {0};
  17. xs_rnd_buf(data, sizeof(data));
  18. return xs_hex_enc((char *)data, sizeof(data));
  19. }
  20. int app_add(const char *id, const xs_dict *app)
  21. /* stores an app */
  22. {
  23. if (!xs_is_hex(id))
  24. return 500;
  25. int status = 201;
  26. xs *fn = xs_fmt("%s/app/", srv_basedir);
  27. FILE *f;
  28. mkdirx(fn);
  29. fn = xs_str_cat(fn, id);
  30. fn = xs_str_cat(fn, ".json");
  31. if ((f = fopen(fn, "w")) != NULL) {
  32. xs_json_dump(app, 4, f);
  33. fclose(f);
  34. }
  35. else
  36. status = 500;
  37. return status;
  38. }
  39. xs_str *_app_fn(const char *id)
  40. {
  41. return xs_fmt("%s/app/%s.json", srv_basedir, id);
  42. }
  43. xs_dict *app_get(const char *id)
  44. /* gets an app */
  45. {
  46. if (!xs_is_hex(id))
  47. return NULL;
  48. xs *fn = _app_fn(id);
  49. xs_dict *app = NULL;
  50. FILE *f;
  51. if ((f = fopen(fn, "r")) != NULL) {
  52. app = xs_json_load(f);
  53. fclose(f);
  54. }
  55. return app;
  56. }
  57. int app_del(const char *id)
  58. /* deletes an app */
  59. {
  60. if (!xs_is_hex(id))
  61. return -1;
  62. xs *fn = _app_fn(id);
  63. return unlink(fn);
  64. }
  65. int token_add(const char *id, const xs_dict *token)
  66. /* stores a token */
  67. {
  68. if (!xs_is_hex(id))
  69. return 500;
  70. int status = 201;
  71. xs *fn = xs_fmt("%s/token/", srv_basedir);
  72. FILE *f;
  73. mkdirx(fn);
  74. fn = xs_str_cat(fn, id);
  75. fn = xs_str_cat(fn, ".json");
  76. if ((f = fopen(fn, "w")) != NULL) {
  77. xs_json_dump(token, 4, f);
  78. fclose(f);
  79. }
  80. else
  81. status = 500;
  82. return status;
  83. }
  84. xs_dict *token_get(const char *id)
  85. /* gets a token */
  86. {
  87. if (!xs_is_hex(id))
  88. return NULL;
  89. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  90. xs_dict *token = NULL;
  91. FILE *f;
  92. if ((f = fopen(fn, "r")) != NULL) {
  93. token = xs_json_load(f);
  94. fclose(f);
  95. }
  96. return token;
  97. }
  98. int token_del(const char *id)
  99. /* deletes a token */
  100. {
  101. if (!xs_is_hex(id))
  102. return -1;
  103. xs *fn = xs_fmt("%s/token/%s.json", srv_basedir, id);
  104. return unlink(fn);
  105. }
  106. const char *login_page = ""
  107. "<!DOCTYPE html>\n"
  108. "<body><h1>%s OAuth identify</h1>\n"
  109. "<div style=\"background-color: red; color: white\">%s</div>\n"
  110. "<form method=\"post\" action=\"https:/" "/%s/%s\">\n"
  111. "<p>Login: <input type=\"text\" name=\"login\"></p>\n"
  112. "<p>Password: <input type=\"password\" name=\"passwd\"></p>\n"
  113. "<input type=\"hidden\" name=\"redir\" value=\"%s\">\n"
  114. "<input type=\"hidden\" name=\"cid\" value=\"%s\">\n"
  115. "<input type=\"hidden\" name=\"state\" value=\"%s\">\n"
  116. "<input type=\"submit\" value=\"OK\">\n"
  117. "</form><p>%s</p></body>\n"
  118. "";
  119. int oauth_get_handler(const xs_dict *req, const char *q_path,
  120. char **body, int *b_size, char **ctype)
  121. {
  122. (void)b_size;
  123. if (!xs_startswith(q_path, "/oauth/"))
  124. return 0;
  125. int status = 404;
  126. xs_dict *msg = xs_dict_get(req, "q_vars");
  127. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  128. srv_debug(1, xs_fmt("oauth_get_handler %s", q_path));
  129. if (strcmp(cmd, "/authorize") == 0) { /** **/
  130. const char *cid = xs_dict_get(msg, "client_id");
  131. const char *ruri = xs_dict_get(msg, "redirect_uri");
  132. const char *rtype = xs_dict_get(msg, "response_type");
  133. const char *state = xs_dict_get(msg, "state");
  134. status = 400;
  135. if (cid && ruri && rtype && strcmp(rtype, "code") == 0) {
  136. xs *app = app_get(cid);
  137. if (app != NULL) {
  138. const char *host = xs_dict_get(srv_config, "host");
  139. if (xs_is_null(state))
  140. state = "";
  141. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-login",
  142. ruri, cid, state, USER_AGENT);
  143. *ctype = "text/html";
  144. status = 200;
  145. srv_debug(1, xs_fmt("oauth authorize: generating login page"));
  146. }
  147. else
  148. srv_debug(1, xs_fmt("oauth authorize: bad client_id %s", cid));
  149. }
  150. else
  151. srv_debug(1, xs_fmt("oauth authorize: invalid or unset arguments"));
  152. }
  153. else
  154. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  155. const char *host = xs_dict_get(srv_config, "host");
  156. *body = xs_fmt(login_page, host, "", host, "oauth/x-snac-get-token",
  157. "", "", "", USER_AGENT);
  158. *ctype = "text/html";
  159. status = 200;
  160. }
  161. return status;
  162. }
  163. int oauth_post_handler(const xs_dict *req, const char *q_path,
  164. const char *payload, int p_size,
  165. char **body, int *b_size, char **ctype)
  166. {
  167. (void)p_size;
  168. (void)b_size;
  169. if (!xs_startswith(q_path, "/oauth/"))
  170. return 0;
  171. int status = 404;
  172. char *i_ctype = xs_dict_get(req, "content-type");
  173. xs *args = NULL;
  174. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  175. args = xs_json_loads(payload);
  176. else
  177. args = xs_dup(xs_dict_get(req, "p_vars"));
  178. xs *cmd = xs_replace_n(q_path, "/oauth", "", 1);
  179. srv_debug(1, xs_fmt("oauth_post_handler %s", q_path));
  180. if (strcmp(cmd, "/x-snac-login") == 0) { /** **/
  181. const char *login = xs_dict_get(args, "login");
  182. const char *passwd = xs_dict_get(args, "passwd");
  183. const char *redir = xs_dict_get(args, "redir");
  184. const char *cid = xs_dict_get(args, "cid");
  185. const char *state = xs_dict_get(args, "state");
  186. const char *host = xs_dict_get(srv_config, "host");
  187. /* by default, generate another login form with an error */
  188. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-login",
  189. redir, cid, state, USER_AGENT);
  190. *ctype = "text/html";
  191. status = 200;
  192. if (login && passwd && redir && cid) {
  193. snac snac;
  194. if (user_open(&snac, login)) {
  195. /* check the login + password */
  196. if (check_password(login, passwd, xs_dict_get(snac.config, "passwd"))) {
  197. /* success! redirect to the desired uri */
  198. xs *code = random_str();
  199. xs_free(*body);
  200. *body = xs_fmt("%s?code=%s", redir, code);
  201. status = 303;
  202. /* if there is a state, add it */
  203. if (!xs_is_null(state) && *state) {
  204. *body = xs_str_cat(*body, "&state=");
  205. *body = xs_str_cat(*body, state);
  206. }
  207. srv_log(xs_fmt("oauth x-snac-login: '%s' success, redirect to %s",
  208. login, *body));
  209. /* assign the login to the app */
  210. xs *app = app_get(cid);
  211. if (app != NULL) {
  212. app = xs_dict_set(app, "uid", login);
  213. app = xs_dict_set(app, "code", code);
  214. app_add(cid, app);
  215. }
  216. else
  217. srv_log(xs_fmt("oauth x-snac-login: error getting app %s", cid));
  218. }
  219. else
  220. srv_debug(1, xs_fmt("oauth x-snac-login: login '%s' incorrect", login));
  221. user_free(&snac);
  222. }
  223. else
  224. srv_debug(1, xs_fmt("oauth x-snac-login: bad user '%s'", login));
  225. }
  226. else
  227. srv_debug(1, xs_fmt("oauth x-snac-login: invalid or unset arguments"));
  228. }
  229. else
  230. if (strcmp(cmd, "/token") == 0) { /** **/
  231. xs *wrk = NULL;
  232. const char *gtype = xs_dict_get(args, "grant_type");
  233. const char *code = xs_dict_get(args, "code");
  234. const char *cid = xs_dict_get(args, "client_id");
  235. const char *csec = xs_dict_get(args, "client_secret");
  236. const char *ruri = xs_dict_get(args, "redirect_uri");
  237. /* FIXME: this 'scope' parameter is mandatory for the official Mastodon API,
  238. but if it's enabled, it makes it crash after some more steps, which
  239. is FAR WORSE */
  240. // const char *scope = xs_dict_get(args, "scope");
  241. const char *scope = NULL;
  242. /* no client_secret? check if it's inside an authorization header
  243. (AndStatus does it this way) */
  244. if (xs_is_null(csec)) {
  245. const char *auhdr = xs_dict_get(req, "authorization");
  246. if (!xs_is_null(auhdr) && xs_startswith(auhdr, "Basic ")) {
  247. xs *s1 = xs_replace_n(auhdr, "Basic ", "", 1);
  248. int size;
  249. xs *s2 = xs_base64_dec(s1, &size);
  250. if (!xs_is_null(s2)) {
  251. xs *l1 = xs_split(s2, ":");
  252. if (xs_list_len(l1) == 2) {
  253. wrk = xs_dup(xs_list_get(l1, 1));
  254. csec = wrk;
  255. }
  256. }
  257. }
  258. }
  259. if (gtype && code && cid && csec && ruri) {
  260. xs *app = app_get(cid);
  261. if (app == NULL) {
  262. status = 401;
  263. srv_log(xs_fmt("oauth token: invalid app %s", cid));
  264. }
  265. else
  266. if (strcmp(csec, xs_dict_get(app, "client_secret")) != 0) {
  267. status = 401;
  268. srv_log(xs_fmt("oauth token: invalid client_secret for app %s", cid));
  269. }
  270. else {
  271. xs *rsp = xs_dict_new();
  272. xs *cat = xs_number_new(time(NULL));
  273. xs *tokid = random_str();
  274. rsp = xs_dict_append(rsp, "access_token", tokid);
  275. rsp = xs_dict_append(rsp, "token_type", "Bearer");
  276. rsp = xs_dict_append(rsp, "created_at", cat);
  277. if (!xs_is_null(scope))
  278. rsp = xs_dict_append(rsp, "scope", scope);
  279. *body = xs_json_dumps(rsp, 4);
  280. *ctype = "application/json";
  281. status = 200;
  282. const char *uid = xs_dict_get(app, "uid");
  283. srv_debug(1, xs_fmt("oauth token: "
  284. "successful login for %s, new token %s", uid, tokid));
  285. xs *token = xs_dict_new();
  286. token = xs_dict_append(token, "token", tokid);
  287. token = xs_dict_append(token, "client_id", cid);
  288. token = xs_dict_append(token, "client_secret", csec);
  289. token = xs_dict_append(token, "uid", uid);
  290. token = xs_dict_append(token, "code", code);
  291. token_add(tokid, token);
  292. }
  293. }
  294. else {
  295. srv_debug(1, xs_fmt("oauth token: invalid or unset arguments"));
  296. status = 400;
  297. }
  298. }
  299. else
  300. if (strcmp(cmd, "/revoke") == 0) { /** **/
  301. const char *cid = xs_dict_get(args, "client_id");
  302. const char *csec = xs_dict_get(args, "client_secret");
  303. const char *tokid = xs_dict_get(args, "token");
  304. if (cid && csec && tokid) {
  305. xs *token = token_get(tokid);
  306. *body = xs_str_new("{}");
  307. *ctype = "application/json";
  308. if (token == NULL || strcmp(csec, xs_dict_get(token, "client_secret")) != 0) {
  309. srv_debug(1, xs_fmt("oauth revoke: bad secret for token %s", tokid));
  310. status = 403;
  311. }
  312. else {
  313. token_del(tokid);
  314. srv_debug(1, xs_fmt("oauth revoke: revoked token %s", tokid));
  315. status = 200;
  316. /* also delete the app, as it serves no purpose from now on */
  317. app_del(cid);
  318. }
  319. }
  320. else {
  321. srv_debug(1, xs_fmt("oauth revoke: invalid or unset arguments"));
  322. status = 403;
  323. }
  324. }
  325. if (strcmp(cmd, "/x-snac-get-token") == 0) { /** **/
  326. const char *login = xs_dict_get(args, "login");
  327. const char *passwd = xs_dict_get(args, "passwd");
  328. const char *host = xs_dict_get(srv_config, "host");
  329. /* by default, generate another login form with an error */
  330. *body = xs_fmt(login_page, host, "LOGIN INCORRECT", host, "oauth/x-snac-get-token",
  331. "", "", "", USER_AGENT);
  332. *ctype = "text/html";
  333. status = 200;
  334. if (login && passwd) {
  335. snac user;
  336. if (user_open(&user, login)) {
  337. /* check the login + password */
  338. if (check_password(login, passwd, xs_dict_get(user.config, "passwd"))) {
  339. /* success! create a new token */
  340. xs *tokid = random_str();
  341. srv_debug(1, xs_fmt("x-snac-new-token: "
  342. "successful login for %s, new token %s", login, tokid));
  343. xs *token = xs_dict_new();
  344. token = xs_dict_append(token, "token", tokid);
  345. token = xs_dict_append(token, "client_id", "snac-client");
  346. token = xs_dict_append(token, "client_secret", "");
  347. token = xs_dict_append(token, "uid", login);
  348. token = xs_dict_append(token, "code", "");
  349. token_add(tokid, token);
  350. *ctype = "text/plain";
  351. xs_free(*body);
  352. *body = xs_dup(tokid);
  353. }
  354. user_free(&user);
  355. }
  356. }
  357. }
  358. return status;
  359. }
  360. xs_str *mastoapi_id(const xs_dict *msg)
  361. /* returns a somewhat Mastodon-compatible status id */
  362. {
  363. const char *id = xs_dict_get(msg, "id");
  364. xs *md5 = xs_md5_hex(id, strlen(id));
  365. return xs_fmt("%10.0f%s", object_ctime_by_md5(md5), md5);
  366. }
  367. #define MID_TO_MD5(id) (id + 10)
  368. xs_dict *mastoapi_account(const xs_dict *actor)
  369. /* converts an ActivityPub actor to a Mastodon account */
  370. {
  371. xs_dict *acct = xs_dict_new();
  372. const char *prefu = xs_dict_get(actor, "preferredUsername");
  373. const char *display_name = xs_dict_get(actor, "name");
  374. if (xs_is_null(display_name) || *display_name == '\0')
  375. display_name = prefu;
  376. const char *id = xs_dict_get(actor, "id");
  377. const char *pub = xs_dict_get(actor, "published");
  378. xs *acct_md5 = xs_md5_hex(id, strlen(id));
  379. acct = xs_dict_append(acct, "id", acct_md5);
  380. acct = xs_dict_append(acct, "username", prefu);
  381. acct = xs_dict_append(acct, "display_name", display_name);
  382. {
  383. /* create the acct field as user@host */
  384. xs *l = xs_split(id, "/");
  385. xs *fquid = xs_fmt("%s@%s", prefu, xs_list_get(l, 2));
  386. acct = xs_dict_append(acct, "acct", fquid);
  387. }
  388. if (pub)
  389. acct = xs_dict_append(acct, "created_at", pub);
  390. else {
  391. /* unset created_at crashes Tusky, so lie like a mf */
  392. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  393. acct = xs_dict_append(acct, "created_at", date);
  394. }
  395. const char *note = xs_dict_get(actor, "summary");
  396. if (xs_is_null(note))
  397. note = "";
  398. if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
  399. acct = xs_dict_append(acct, "bot", xs_stock_true);
  400. else
  401. acct = xs_dict_append(acct, "bot", xs_stock_false);
  402. acct = xs_dict_append(acct, "note", note);
  403. acct = xs_dict_append(acct, "url", id);
  404. xs *avatar = NULL;
  405. xs_dict *av = xs_dict_get(actor, "icon");
  406. if (xs_type(av) == XSTYPE_DICT)
  407. avatar = xs_dup(xs_dict_get(av, "url"));
  408. else
  409. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  410. acct = xs_dict_append(acct, "avatar", avatar);
  411. acct = xs_dict_append(acct, "avatar_static", avatar);
  412. xs *header = NULL;
  413. xs_dict *hd = xs_dict_get(actor, "image");
  414. if (xs_type(hd) == XSTYPE_DICT)
  415. header = xs_dup(xs_dict_get(hd, "url"));
  416. if (xs_is_null(header))
  417. header = xs_dup("");
  418. acct = xs_dict_append(acct, "header", header);
  419. acct = xs_dict_append(acct, "header_static", header);
  420. /* emojis */
  421. xs_list *p;
  422. if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
  423. xs *eml = xs_list_new();
  424. xs_dict *v;
  425. while (xs_list_iter(&p, &v)) {
  426. const char *type = xs_dict_get(v, "type");
  427. if (!xs_is_null(type) && strcmp(type, "Emoji") == 0) {
  428. const char *name = xs_dict_get(v, "name");
  429. const xs_dict *icon = xs_dict_get(v, "icon");
  430. if (!xs_is_null(name) && !xs_is_null(icon)) {
  431. const char *url = xs_dict_get(icon, "url");
  432. if (!xs_is_null(url)) {
  433. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  434. xs *d1 = xs_dict_new();
  435. d1 = xs_dict_append(d1, "shortcode", nm);
  436. d1 = xs_dict_append(d1, "url", url);
  437. d1 = xs_dict_append(d1, "static_url", url);
  438. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  439. eml = xs_list_append(eml, d1);
  440. }
  441. }
  442. }
  443. }
  444. acct = xs_dict_append(acct, "emojis", eml);
  445. }
  446. return acct;
  447. }
  448. xs_dict *mastoapi_poll(snac *snac, const xs_dict *msg)
  449. /* creates a mastoapi Poll object */
  450. {
  451. xs_dict *poll = xs_dict_new();
  452. xs *mid = mastoapi_id(msg);
  453. xs_list *opts = NULL;
  454. xs_val *v;
  455. int num_votes = 0;
  456. xs *options = xs_list_new();
  457. poll = xs_dict_append(poll, "id", mid);
  458. poll = xs_dict_append(poll, "expires_at", xs_dict_get(msg, "endTime"));
  459. poll = xs_dict_append(poll, "expired",
  460. xs_dict_get(msg, "closed") != NULL ? xs_stock_true : xs_stock_false);
  461. if ((opts = xs_dict_get(msg, "oneOf")) != NULL)
  462. poll = xs_dict_append(poll, "multiple", xs_stock_false);
  463. else {
  464. opts = xs_dict_get(msg, "anyOf");
  465. poll = xs_dict_append(poll, "multiple", xs_stock_true);
  466. }
  467. while (xs_list_iter(&opts, &v)) {
  468. const char *title = xs_dict_get(v, "name");
  469. const char *replies = xs_dict_get(v, "replies");
  470. if (title && replies) {
  471. const char *votes_count = xs_dict_get(replies, "totalItems");
  472. if (xs_type(votes_count) == XSTYPE_NUMBER) {
  473. xs *d = xs_dict_new();
  474. d = xs_dict_append(d, "title", title);
  475. d = xs_dict_append(d, "votes_count", votes_count);
  476. options = xs_list_append(options, d);
  477. num_votes += xs_number_get(votes_count);
  478. }
  479. }
  480. }
  481. poll = xs_dict_append(poll, "options", options);
  482. xs *vc = xs_number_new(num_votes);
  483. poll = xs_dict_append(poll, "votes_count", vc);
  484. poll = xs_dict_append(poll, "voted",
  485. was_question_voted(snac, xs_dict_get(msg, "id")) ? xs_stock_true : xs_stock_false);
  486. return poll;
  487. }
  488. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  489. /* converts an ActivityPub note to a Mastodon status */
  490. {
  491. xs *actor = NULL;
  492. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  493. /* if the author is not here, discard */
  494. if (actor == NULL)
  495. return NULL;
  496. const char *type = xs_dict_get(msg, "type");
  497. const char *id = xs_dict_get(msg, "id");
  498. xs *acct = mastoapi_account(actor);
  499. xs *idx = NULL;
  500. xs *ixc = NULL;
  501. char *tmp;
  502. xs *mid = mastoapi_id(msg);
  503. xs_dict *st = xs_dict_new();
  504. st = xs_dict_append(st, "id", mid);
  505. st = xs_dict_append(st, "uri", id);
  506. st = xs_dict_append(st, "url", id);
  507. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  508. st = xs_dict_append(st, "account", acct);
  509. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  510. st = xs_dict_append(st, "visibility",
  511. is_msg_public(snac, msg) ? "public" : "private");
  512. tmp = xs_dict_get(msg, "sensitive");
  513. if (xs_is_null(tmp))
  514. tmp = xs_stock_false;
  515. st = xs_dict_append(st, "sensitive", tmp);
  516. tmp = xs_dict_get(msg, "summary");
  517. if (xs_is_null(tmp))
  518. tmp = "";
  519. st = xs_dict_append(st, "spoiler_text", tmp);
  520. /* create the list of attachments */
  521. xs *matt = xs_list_new();
  522. xs_list *att = xs_dict_get(msg, "attachment");
  523. xs_str *aobj;
  524. while (xs_list_iter(&att, &aobj)) {
  525. const char *mtype = xs_dict_get(aobj, "mediaType");
  526. if (!xs_is_null(mtype)) {
  527. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/")) {
  528. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  529. xs *matte = xs_dict_new();
  530. matte = xs_dict_append(matte, "id", matteid);
  531. matte = xs_dict_append(matte, "type", *mtype == 'i' ? "image" : "video");
  532. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  533. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  534. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  535. const char *name = xs_dict_get(aobj, "name");
  536. if (xs_is_null(name))
  537. name = "";
  538. matte = xs_dict_append(matte, "description", name);
  539. matt = xs_list_append(matt, matte);
  540. }
  541. }
  542. }
  543. st = xs_dict_append(st, "media_attachments", matt);
  544. {
  545. xs *ml = xs_list_new();
  546. xs *htl = xs_list_new();
  547. xs *eml = xs_list_new();
  548. xs_list *p = xs_dict_get(msg, "tag");
  549. xs_dict *v;
  550. int n = 0;
  551. while (xs_list_iter(&p, &v)) {
  552. const char *type = xs_dict_get(v, "type");
  553. if (xs_is_null(type))
  554. continue;
  555. xs *d1 = xs_dict_new();
  556. if (strcmp(type, "Mention") == 0) {
  557. const char *name = xs_dict_get(v, "name");
  558. const char *href = xs_dict_get(v, "href");
  559. if (!xs_is_null(name) && !xs_is_null(href) &&
  560. strcmp(href, snac->actor) != 0) {
  561. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  562. xs *id = xs_fmt("%d", n++);
  563. d1 = xs_dict_append(d1, "id", id);
  564. d1 = xs_dict_append(d1, "username", nm);
  565. d1 = xs_dict_append(d1, "acct", nm);
  566. d1 = xs_dict_append(d1, "url", href);
  567. ml = xs_list_append(ml, d1);
  568. }
  569. }
  570. else
  571. if (strcmp(type, "Hashtag") == 0) {
  572. const char *name = xs_dict_get(v, "name");
  573. const char *href = xs_dict_get(v, "href");
  574. if (!xs_is_null(name) && !xs_is_null(href)) {
  575. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  576. d1 = xs_dict_append(d1, "name", nm);
  577. d1 = xs_dict_append(d1, "url", href);
  578. htl = xs_list_append(htl, d1);
  579. }
  580. }
  581. else
  582. if (strcmp(type, "Emoji") == 0) {
  583. const char *name = xs_dict_get(v, "name");
  584. const xs_dict *icon = xs_dict_get(v, "icon");
  585. if (!xs_is_null(name) && !xs_is_null(icon)) {
  586. const char *url = xs_dict_get(icon, "url");
  587. if (!xs_is_null(url)) {
  588. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  589. d1 = xs_dict_append(d1, "shortcode", nm);
  590. d1 = xs_dict_append(d1, "url", url);
  591. d1 = xs_dict_append(d1, "static_url", url);
  592. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  593. d1 = xs_dict_append(d1, "category", "Emojis");
  594. eml = xs_list_append(eml, d1);
  595. }
  596. }
  597. }
  598. }
  599. st = xs_dict_append(st, "mentions", ml);
  600. st = xs_dict_append(st, "tags", htl);
  601. st = xs_dict_append(st, "emojis", eml);
  602. }
  603. xs_free(idx);
  604. xs_free(ixc);
  605. idx = object_likes(id);
  606. ixc = xs_number_new(xs_list_len(idx));
  607. st = xs_dict_append(st, "favourites_count", ixc);
  608. st = xs_dict_append(st, "favourited",
  609. xs_list_in(idx, snac->md5) != -1 ? xs_stock_true : xs_stock_false);
  610. xs_free(idx);
  611. xs_free(ixc);
  612. idx = object_announces(id);
  613. ixc = xs_number_new(xs_list_len(idx));
  614. st = xs_dict_append(st, "reblogs_count", ixc);
  615. st = xs_dict_append(st, "reblogged",
  616. xs_list_in(idx, snac->md5) != -1 ? xs_stock_true : xs_stock_false);
  617. xs_free(idx);
  618. xs_free(ixc);
  619. idx = object_children(id);
  620. ixc = xs_number_new(xs_list_len(idx));
  621. st = xs_dict_append(st, "replies_count", ixc);
  622. /* default in_reply_to values */
  623. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  624. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  625. tmp = xs_dict_get(msg, "inReplyTo");
  626. if (!xs_is_null(tmp)) {
  627. xs *irto = NULL;
  628. if (valid_status(object_get(tmp, &irto))) {
  629. xs *irt_mid = mastoapi_id(irto);
  630. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  631. char *at = NULL;
  632. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  633. xs *at_md5 = xs_md5_hex(at, strlen(at));
  634. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  635. }
  636. }
  637. }
  638. st = xs_dict_append(st, "reblog", xs_stock_null);
  639. st = xs_dict_append(st, "card", xs_stock_null);
  640. st = xs_dict_append(st, "language", xs_stock_null);
  641. tmp = xs_dict_get(msg, "sourceContent");
  642. if (xs_is_null(tmp))
  643. tmp = "";
  644. st = xs_dict_append(st, "text", tmp);
  645. tmp = xs_dict_get(msg, "updated");
  646. if (xs_is_null(tmp))
  647. tmp = xs_stock_null;
  648. st = xs_dict_append(st, "edited_at", tmp);
  649. if (strcmp(type, "Question") == 0) {
  650. xs *poll = mastoapi_poll(snac, msg);
  651. st = xs_dict_append(st, "poll", poll);
  652. }
  653. else
  654. st = xs_dict_append(st, "poll", xs_stock_null);
  655. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  656. st = xs_dict_append(st, "pinned", is_pinned(snac, id) ? xs_stock_true : xs_stock_false);
  657. return st;
  658. }
  659. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  660. {
  661. xs_dict *rel = NULL;
  662. xs *actor_o = NULL;
  663. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  664. rel = xs_dict_new();
  665. const char *actor = xs_dict_get(actor_o, "id");
  666. rel = xs_dict_append(rel, "id", md5);
  667. rel = xs_dict_append(rel, "following",
  668. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  669. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  670. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  671. rel = xs_dict_append(rel, "followed_by",
  672. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  673. rel = xs_dict_append(rel, "blocking",
  674. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  675. rel = xs_dict_append(rel, "muting", xs_stock_false);
  676. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  677. rel = xs_dict_append(rel, "requested", xs_stock_false);
  678. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  679. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  680. rel = xs_dict_append(rel, "note", "");
  681. }
  682. return rel;
  683. }
  684. int process_auth_token(snac *snac, const xs_dict *req)
  685. /* processes an authorization token, if there is one */
  686. {
  687. int logged_in = 0;
  688. char *v;
  689. /* if there is an authorization field, try to validate it */
  690. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  691. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  692. xs *token = token_get(tokid);
  693. if (token != NULL) {
  694. const char *uid = xs_dict_get(token, "uid");
  695. if (!xs_is_null(uid) && user_open(snac, uid)) {
  696. logged_in = 1;
  697. /* this counts as a 'login' */
  698. lastlog_write(snac, "mastoapi");
  699. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  700. }
  701. else
  702. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  703. }
  704. else
  705. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  706. }
  707. return logged_in;
  708. }
  709. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  710. char **body, int *b_size, char **ctype)
  711. {
  712. (void)b_size;
  713. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  714. return 0;
  715. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  716. int status = 404;
  717. xs_dict *args = xs_dict_get(req, "q_vars");
  718. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  719. snac snac1 = {0};
  720. int logged_in = process_auth_token(&snac1, req);
  721. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  722. if (logged_in) {
  723. xs *acct = xs_dict_new();
  724. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  725. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  726. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  727. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  728. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  729. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  730. acct = xs_dict_append(acct, "url", snac1.actor);
  731. acct = xs_dict_append(acct, "header", "");
  732. xs *avatar = NULL;
  733. char *av = xs_dict_get(snac1.config, "avatar");
  734. if (xs_is_null(av) || *av == '\0')
  735. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  736. else
  737. avatar = xs_dup(av);
  738. acct = xs_dict_append(acct, "avatar", avatar);
  739. *body = xs_json_dumps(acct, 4);
  740. *ctype = "application/json";
  741. status = 200;
  742. }
  743. else {
  744. status = 422; // "Unprocessable entity" (no login)
  745. }
  746. }
  747. else
  748. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  749. /* find if an account is followed, blocked, etc. */
  750. /* the account to get relationships about is in args "id[]" */
  751. if (logged_in) {
  752. xs *res = xs_list_new();
  753. const char *md5 = xs_dict_get(args, "id[]");
  754. if (!xs_is_null(md5)) {
  755. if (xs_type(md5) == XSTYPE_LIST)
  756. md5 = xs_list_get(md5, 0);
  757. xs *rel = mastoapi_relationship(&snac1, md5);
  758. if (rel != NULL)
  759. res = xs_list_append(res, rel);
  760. }
  761. *body = xs_json_dumps(res, 4);
  762. *ctype = "application/json";
  763. status = 200;
  764. }
  765. else
  766. status = 422;
  767. }
  768. else
  769. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  770. /* account-related information */
  771. xs *l = xs_split(cmd, "/");
  772. const char *uid = xs_list_get(l, 3);
  773. const char *opt = xs_list_get(l, 4);
  774. if (uid != NULL) {
  775. snac snac2;
  776. xs *out = NULL;
  777. xs *actor = NULL;
  778. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  779. /* search for accounts starting with q */
  780. const char *aq = xs_dict_get(args, "q");
  781. if (!xs_is_null(aq)) {
  782. xs *q = xs_tolower_i(xs_dup(aq));
  783. out = xs_list_new();
  784. xs *wing = following_list(&snac1);
  785. xs *wers = follower_list(&snac1);
  786. xs *ulst = user_list();
  787. xs_list *p;
  788. xs_str *v;
  789. xs_set seen;
  790. xs_set_init(&seen);
  791. /* user relations */
  792. xs_list *lsts[] = { wing, wers, NULL };
  793. int n;
  794. for (n = 0; (p = lsts[n]) != NULL; n++) {
  795. while (xs_list_iter(&p, &v)) {
  796. /* already seen? skip */
  797. if (xs_set_add(&seen, v) == 0)
  798. continue;
  799. xs *actor = NULL;
  800. if (valid_status(object_get(v, &actor))) {
  801. const char *uname = xs_dict_get(actor, "preferredUsername");
  802. if (!xs_is_null(uname)) {
  803. xs *luname = xs_tolower_i(xs_dup(uname));
  804. if (xs_startswith(luname, q)) {
  805. xs *acct = mastoapi_account(actor);
  806. out = xs_list_append(out, acct);
  807. }
  808. }
  809. }
  810. }
  811. }
  812. /* local users */
  813. p = ulst;
  814. while (xs_list_iter(&p, &v)) {
  815. snac user;
  816. /* skip this same user */
  817. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  818. continue;
  819. /* skip if the uid does not start with the query */
  820. xs *v2 = xs_tolower_i(xs_dup(v));
  821. if (!xs_startswith(v2, q))
  822. continue;
  823. if (user_open(&user, v)) {
  824. /* if it's not already seen, add it */
  825. if (xs_set_add(&seen, user.actor) == 1) {
  826. xs *actor = msg_actor(&user);
  827. xs *acct = mastoapi_account(actor);
  828. out = xs_list_append(out, acct);
  829. }
  830. user_free(&user);
  831. }
  832. }
  833. xs_set_free(&seen);
  834. }
  835. }
  836. else
  837. /* is it a local user? */
  838. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  839. if (opt == NULL) {
  840. /* account information */
  841. actor = msg_actor(&snac2);
  842. out = mastoapi_account(actor);
  843. }
  844. else
  845. if (strcmp(opt, "statuses") == 0) { /** **/
  846. /* the public list of posts of a user */
  847. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  848. xs_list *p = timeline;
  849. xs_str *v;
  850. out = xs_list_new();
  851. while (xs_list_iter(&p, &v)) {
  852. xs *msg = NULL;
  853. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  854. /* add only posts by the author */
  855. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  856. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  857. xs *st = mastoapi_status(&snac2, msg);
  858. out = xs_list_append(out, st);
  859. }
  860. }
  861. }
  862. }
  863. user_free(&snac2);
  864. }
  865. else {
  866. /* try the uid as the md5 of a possibly loaded actor */
  867. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  868. if (opt == NULL) {
  869. /* account information */
  870. out = mastoapi_account(actor);
  871. }
  872. else
  873. if (strcmp(opt, "statuses") == 0) {
  874. /* we don't serve statuses of others; return the empty list */
  875. out = xs_list_new();
  876. }
  877. }
  878. }
  879. if (out != NULL) {
  880. *body = xs_json_dumps(out, 4);
  881. *ctype = "application/json";
  882. status = 200;
  883. }
  884. }
  885. }
  886. else
  887. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  888. /* the private timeline */
  889. if (logged_in) {
  890. const char *max_id = xs_dict_get(args, "max_id");
  891. const char *since_id = xs_dict_get(args, "since_id");
  892. const char *min_id = xs_dict_get(args, "min_id");
  893. const char *limit_s = xs_dict_get(args, "limit");
  894. int limit = 0;
  895. int cnt = 0;
  896. if (!xs_is_null(limit_s))
  897. limit = atoi(limit_s);
  898. if (limit == 0)
  899. limit = 20;
  900. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  901. xs *out = xs_list_new();
  902. xs_list *p = timeline;
  903. xs_str *v;
  904. while (xs_list_iter(&p, &v) && cnt < limit) {
  905. xs *msg = NULL;
  906. /* only return entries older that max_id */
  907. if (max_id) {
  908. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  909. max_id = NULL;
  910. continue;
  911. }
  912. /* only returns entries newer than since_id */
  913. if (since_id) {
  914. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  915. break;
  916. }
  917. /* only returns entries newer than min_id */
  918. /* what does really "Return results immediately newer than ID" mean? */
  919. if (min_id) {
  920. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  921. break;
  922. }
  923. /* get the entry */
  924. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  925. continue;
  926. /* discard non-Notes */
  927. const char *type = xs_dict_get(msg, "type");
  928. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  929. continue;
  930. #if 0
  931. /* discard notes from people we don't follow with no boosts */
  932. if (!following_check(&snac1, xs_dict_get(msg, "attributedTo")) &&
  933. object_announces_len(xs_dict_get(msg, "id")) == 0)
  934. continue;
  935. #endif
  936. /* discard notes from muted morons */
  937. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  938. continue;
  939. /* discard hidden notes */
  940. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  941. continue;
  942. /* discard poll votes (they have a name) */
  943. if (!xs_is_null(xs_dict_get(msg, "name")))
  944. continue;
  945. /* convert the Note into a Mastodon status */
  946. xs *st = mastoapi_status(&snac1, msg);
  947. if (st != NULL)
  948. out = xs_list_append(out, st);
  949. cnt++;
  950. }
  951. *body = xs_json_dumps(out, 4);
  952. *ctype = "application/json";
  953. status = 200;
  954. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  955. }
  956. else {
  957. status = 401; // unauthorized
  958. }
  959. }
  960. else
  961. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  962. /* the instance public timeline (public timelines for all users) */
  963. /* NOTE: this api call needs no authorization; but,
  964. I need a logged-in user in mastoapi_status() for
  965. is_msg_public() and the liked/boosted flags,
  966. so it will silently fail for pure public access */
  967. const char *limit_s = xs_dict_get(args, "limit");
  968. int limit = 0;
  969. int cnt = 0;
  970. if (!xs_is_null(limit_s))
  971. limit = atoi(limit_s);
  972. if (limit == 0)
  973. limit = 20;
  974. xs *timeline = timeline_instance_list(0, limit);
  975. xs *out = xs_list_new();
  976. xs_list *p = timeline;
  977. xs_str *md5;
  978. while (logged_in && xs_list_iter(&p, &md5) && cnt < limit) {
  979. xs *msg = NULL;
  980. /* get the entry */
  981. if (!valid_status(object_get_by_md5(md5, &msg)))
  982. continue;
  983. /* discard non-Notes */
  984. const char *type = xs_dict_get(msg, "type");
  985. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  986. continue;
  987. /* convert the Note into a Mastodon status */
  988. xs *st = mastoapi_status(&snac1, msg);
  989. if (st != NULL) {
  990. out = xs_list_append(out, st);
  991. cnt++;
  992. }
  993. }
  994. *body = xs_json_dumps(out, 4);
  995. *ctype = "application/json";
  996. status = 200;
  997. }
  998. else
  999. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1000. /* TBD */
  1001. *body = xs_dup("[]");
  1002. *ctype = "application/json";
  1003. status = 200;
  1004. }
  1005. else
  1006. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1007. if (logged_in) {
  1008. xs *l = notify_list(&snac1, 0);
  1009. xs *out = xs_list_new();
  1010. xs_list *p = l;
  1011. xs_dict *v;
  1012. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1013. while (xs_list_iter(&p, &v)) {
  1014. xs *noti = notify_get(&snac1, v);
  1015. if (noti == NULL)
  1016. continue;
  1017. const char *type = xs_dict_get(noti, "type");
  1018. const char *utype = xs_dict_get(noti, "utype");
  1019. const char *objid = xs_dict_get(noti, "objid");
  1020. xs *actor = NULL;
  1021. xs *entry = NULL;
  1022. if (!valid_status(actor_get(&snac1, xs_dict_get(noti, "actor"), &actor)))
  1023. continue;
  1024. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1025. continue;
  1026. if (is_hidden(&snac1, objid))
  1027. continue;
  1028. /* convert the type */
  1029. if (strcmp(type, "Like") == 0)
  1030. type = "favourite";
  1031. else
  1032. if (strcmp(type, "Announce") == 0)
  1033. type = "reblog";
  1034. else
  1035. if (strcmp(type, "Follow") == 0)
  1036. type = "follow";
  1037. else
  1038. if (strcmp(type, "Create") == 0)
  1039. type = "mention";
  1040. else
  1041. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1042. type = "poll";
  1043. else
  1044. continue;
  1045. /* excluded type? */
  1046. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1047. continue;
  1048. xs *mn = xs_dict_new();
  1049. mn = xs_dict_append(mn, "type", type);
  1050. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1051. mn = xs_dict_append(mn, "id", id);
  1052. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1053. xs *acct = mastoapi_account(actor);
  1054. mn = xs_dict_append(mn, "account", acct);
  1055. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1056. xs *st = mastoapi_status(&snac1, entry);
  1057. mn = xs_dict_append(mn, "status", st);
  1058. }
  1059. out = xs_list_append(out, mn);
  1060. }
  1061. *body = xs_json_dumps(out, 4);
  1062. *ctype = "application/json";
  1063. status = 200;
  1064. }
  1065. else
  1066. status = 401;
  1067. }
  1068. else
  1069. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1070. /* snac will never have filters */
  1071. *body = xs_dup("[]");
  1072. *ctype = "application/json";
  1073. status = 200;
  1074. }
  1075. else
  1076. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1077. /* snac will never support a list of favourites */
  1078. *body = xs_dup("[]");
  1079. *ctype = "application/json";
  1080. status = 200;
  1081. }
  1082. else
  1083. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1084. /* snac does not support bookmarks */
  1085. *body = xs_dup("[]");
  1086. *ctype = "application/json";
  1087. status = 200;
  1088. }
  1089. else
  1090. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1091. /* snac does not support lists */
  1092. *body = xs_dup("[]");
  1093. *ctype = "application/json";
  1094. status = 200;
  1095. }
  1096. else
  1097. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1098. /* snac does not schedule notes */
  1099. *body = xs_dup("[]");
  1100. *ctype = "application/json";
  1101. status = 200;
  1102. }
  1103. else
  1104. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1105. /* snac does not support optional follow confirmations */
  1106. *body = xs_dup("[]");
  1107. *ctype = "application/json";
  1108. status = 200;
  1109. }
  1110. else
  1111. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1112. /* snac has no announcements (yet?) */
  1113. *body = xs_dup("[]");
  1114. *ctype = "application/json";
  1115. status = 200;
  1116. }
  1117. else
  1118. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1119. /* are you kidding me? */
  1120. *body = xs_dup("[]");
  1121. *ctype = "application/json";
  1122. status = 200;
  1123. }
  1124. else
  1125. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1126. /* returns an instance object */
  1127. xs *ins = xs_dict_new();
  1128. const char *host = xs_dict_get(srv_config, "host");
  1129. ins = xs_dict_append(ins, "uri", host);
  1130. ins = xs_dict_append(ins, "domain", host);
  1131. ins = xs_dict_append(ins, "title", host);
  1132. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1133. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1134. ins = xs_dict_append(ins, "description", host);
  1135. ins = xs_dict_append(ins, "short_description", host);
  1136. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1137. ins = xs_dict_append(ins, "thumbnail", susie);
  1138. const char *v = xs_dict_get(srv_config, "admin_email");
  1139. if (xs_is_null(v) || *v == '\0')
  1140. v = "admin@localhost";
  1141. ins = xs_dict_append(ins, "email", v);
  1142. xs *l1 = xs_list_new();
  1143. ins = xs_dict_append(ins, "rules", l1);
  1144. l1 = xs_list_append(l1, "en");
  1145. ins = xs_dict_append(ins, "languages", l1);
  1146. xs *d1 = xs_dict_new();
  1147. ins = xs_dict_append(ins, "urls", d1);
  1148. xs *z = xs_number_new(0);
  1149. d1 = xs_dict_append(d1, "user_count", z);
  1150. d1 = xs_dict_append(d1, "status_count", z);
  1151. d1 = xs_dict_append(d1, "domain_count", z);
  1152. ins = xs_dict_append(ins, "stats", d1);
  1153. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1154. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1155. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1156. xs *cfg = xs_dict_new();
  1157. {
  1158. xs *d11 = xs_dict_new();
  1159. xs *mc = xs_number_new(100000);
  1160. xs *mm = xs_number_new(8);
  1161. xs *cr = xs_number_new(32);
  1162. d11 = xs_dict_append(d11, "max_characters", mc);
  1163. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  1164. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  1165. cfg = xs_dict_append(cfg, "statuses", d11);
  1166. }
  1167. ins = xs_dict_append(ins, "configuration", cfg);
  1168. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1169. if (!xs_is_null(admin_account) && *admin_account) {
  1170. snac admin;
  1171. if (user_open(&admin, admin_account)) {
  1172. xs *actor = msg_actor(&admin);
  1173. xs *acct = mastoapi_account(actor);
  1174. ins = xs_dict_append(ins, "contact_account", acct);
  1175. user_free(&admin);
  1176. }
  1177. }
  1178. *body = xs_json_dumps(ins, 4);
  1179. *ctype = "application/json";
  1180. status = 200;
  1181. }
  1182. else
  1183. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1184. /* information about a status */
  1185. if (logged_in) {
  1186. xs *l = xs_split(cmd, "/");
  1187. const char *id = xs_list_get(l, 3);
  1188. const char *op = xs_list_get(l, 4);
  1189. if (!xs_is_null(id)) {
  1190. xs *msg = NULL;
  1191. xs *out = NULL;
  1192. /* skip the 'fake' part of the id */
  1193. id = MID_TO_MD5(id);
  1194. if (valid_status(object_get_by_md5(id, &msg))) {
  1195. if (op == NULL) {
  1196. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1197. /* return the status itself */
  1198. out = mastoapi_status(&snac1, msg);
  1199. }
  1200. }
  1201. else
  1202. if (strcmp(op, "context") == 0) { /** **/
  1203. /* return ancestors and children */
  1204. xs *anc = xs_list_new();
  1205. xs *des = xs_list_new();
  1206. xs_list *p;
  1207. xs_str *v;
  1208. char pid[64];
  1209. /* build the [grand]parent list, moving up */
  1210. strncpy(pid, id, sizeof(pid));
  1211. while (object_parent(pid, pid, sizeof(pid))) {
  1212. xs *m2 = NULL;
  1213. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1214. xs *st = mastoapi_status(&snac1, m2);
  1215. anc = xs_list_insert(anc, 0, st);
  1216. }
  1217. else
  1218. break;
  1219. }
  1220. /* build the children list */
  1221. xs *children = object_children(xs_dict_get(msg, "id"));
  1222. p = children;
  1223. while (xs_list_iter(&p, &v)) {
  1224. xs *m2 = NULL;
  1225. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1226. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1227. xs *st = mastoapi_status(&snac1, m2);
  1228. des = xs_list_append(des, st);
  1229. }
  1230. }
  1231. }
  1232. out = xs_dict_new();
  1233. out = xs_dict_append(out, "ancestors", anc);
  1234. out = xs_dict_append(out, "descendants", des);
  1235. }
  1236. else
  1237. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1238. strcmp(op, "favourited_by") == 0) { /** **/
  1239. /* return the list of people who liked or boosted this */
  1240. out = xs_list_new();
  1241. xs *l = NULL;
  1242. if (op[0] == 'r')
  1243. l = object_announces(xs_dict_get(msg, "id"));
  1244. else
  1245. l = object_likes(xs_dict_get(msg, "id"));
  1246. xs_list *p = l;
  1247. xs_str *v;
  1248. while (xs_list_iter(&p, &v)) {
  1249. xs *actor2 = NULL;
  1250. if (valid_status(object_get_by_md5(v, &actor2))) {
  1251. xs *acct2 = mastoapi_account(actor2);
  1252. out = xs_list_append(out, acct2);
  1253. }
  1254. }
  1255. }
  1256. }
  1257. else
  1258. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1259. if (out != NULL) {
  1260. *body = xs_json_dumps(out, 4);
  1261. *ctype = "application/json";
  1262. status = 200;
  1263. }
  1264. }
  1265. }
  1266. else
  1267. status = 401;
  1268. }
  1269. else
  1270. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1271. *body = xs_dup("{}");
  1272. *ctype = "application/json";
  1273. status = 200;
  1274. }
  1275. else
  1276. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1277. *body = xs_dup("{}");
  1278. *ctype = "application/json";
  1279. status = 200;
  1280. }
  1281. else
  1282. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1283. *body = xs_dup("[]");
  1284. *ctype = "application/json";
  1285. status = 200;
  1286. }
  1287. else
  1288. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1289. if (logged_in) {
  1290. const char *q = xs_dict_get(args, "q");
  1291. const char *type = xs_dict_get(args, "type");
  1292. const char *offset = xs_dict_get(args, "offset");
  1293. xs *acl = xs_list_new();
  1294. xs *stl = xs_list_new();
  1295. xs *htl = xs_list_new();
  1296. xs *res = xs_dict_new();
  1297. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1298. /* reply something only for offset 0; otherwise,
  1299. apps like Tusky keep asking again and again */
  1300. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1301. /* do a webfinger query */
  1302. char *actor = NULL;
  1303. char *user = NULL;
  1304. if (valid_status(webfinger_request(q, &actor, &user))) {
  1305. xs *actor_o = NULL;
  1306. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1307. xs *acct = mastoapi_account(actor_o);
  1308. acl = xs_list_append(acl, acct);
  1309. }
  1310. }
  1311. }
  1312. }
  1313. res = xs_dict_append(res, "accounts", acl);
  1314. res = xs_dict_append(res, "statuses", stl);
  1315. res = xs_dict_append(res, "hashtags", htl);
  1316. *body = xs_json_dumps(res, 4);
  1317. *ctype = "application/json";
  1318. status = 200;
  1319. }
  1320. else
  1321. status = 401;
  1322. }
  1323. /* user cleanup */
  1324. if (logged_in)
  1325. user_free(&snac1);
  1326. return status;
  1327. }
  1328. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1329. const char *payload, int p_size,
  1330. char **body, int *b_size, char **ctype)
  1331. {
  1332. (void)p_size;
  1333. (void)b_size;
  1334. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1335. return 0;
  1336. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1337. int status = 404;
  1338. xs *args = NULL;
  1339. char *i_ctype = xs_dict_get(req, "content-type");
  1340. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1341. args = xs_json_loads(payload);
  1342. else
  1343. args = xs_dup(xs_dict_get(req, "p_vars"));
  1344. if (args == NULL)
  1345. return 400;
  1346. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1347. snac snac = {0};
  1348. int logged_in = process_auth_token(&snac, req);
  1349. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1350. const char *name = xs_dict_get(args, "client_name");
  1351. const char *ruri = xs_dict_get(args, "redirect_uris");
  1352. const char *scope = xs_dict_get(args, "scope");
  1353. if (xs_type(ruri) == XSTYPE_LIST)
  1354. ruri = xs_dict_get(ruri, 0);
  1355. if (name && ruri) {
  1356. xs *app = xs_dict_new();
  1357. xs *id = xs_replace_i(tid(0), ".", "");
  1358. xs *csec = random_str();
  1359. xs *vkey = random_str();
  1360. xs *cid = NULL;
  1361. /* pick a non-existent random cid */
  1362. for (;;) {
  1363. cid = random_str();
  1364. xs *p_app = app_get(cid);
  1365. if (p_app == NULL)
  1366. break;
  1367. xs_free(cid);
  1368. }
  1369. app = xs_dict_append(app, "name", name);
  1370. app = xs_dict_append(app, "redirect_uri", ruri);
  1371. app = xs_dict_append(app, "client_id", cid);
  1372. app = xs_dict_append(app, "client_secret", csec);
  1373. app = xs_dict_append(app, "vapid_key", vkey);
  1374. app = xs_dict_append(app, "id", id);
  1375. *body = xs_json_dumps(app, 4);
  1376. *ctype = "application/json";
  1377. status = 200;
  1378. app = xs_dict_append(app, "code", "");
  1379. if (scope)
  1380. app = xs_dict_append(app, "scope", scope);
  1381. app_add(cid, app);
  1382. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1383. }
  1384. }
  1385. else
  1386. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1387. if (logged_in) {
  1388. /* post a new Note */
  1389. const char *content = xs_dict_get(args, "status");
  1390. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1391. const char *visibility = xs_dict_get(args, "visibility");
  1392. const char *summary = xs_dict_get(args, "spoiler_text");
  1393. const char *media_ids = xs_dict_get(args, "media_ids");
  1394. if (xs_is_null(media_ids))
  1395. media_ids = xs_dict_get(args, "media_ids[]");
  1396. if (xs_is_null(visibility))
  1397. visibility = "public";
  1398. xs *attach_list = xs_list_new();
  1399. xs *irt = NULL;
  1400. /* is it a reply? */
  1401. if (mid != NULL) {
  1402. xs *r_msg = NULL;
  1403. const char *md5 = MID_TO_MD5(mid);
  1404. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1405. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1406. }
  1407. /* does it have attachments? */
  1408. if (!xs_is_null(media_ids)) {
  1409. xs *mi = NULL;
  1410. if (xs_type(media_ids) == XSTYPE_LIST)
  1411. mi = xs_dup(media_ids);
  1412. else {
  1413. mi = xs_list_new();
  1414. mi = xs_list_append(mi, media_ids);
  1415. }
  1416. xs_list *p = mi;
  1417. xs_str *v;
  1418. while (xs_list_iter(&p, &v)) {
  1419. xs *l = xs_list_new();
  1420. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1421. xs *desc = static_get_meta(&snac, v);
  1422. l = xs_list_append(l, url);
  1423. l = xs_list_append(l, desc);
  1424. attach_list = xs_list_append(attach_list, l);
  1425. }
  1426. }
  1427. /* prepare the message */
  1428. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1429. strcmp(visibility, "public") == 0 ? 0 : 1);
  1430. if (!xs_is_null(summary) && *summary) {
  1431. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1432. msg = xs_dict_set(msg, "summary", summary);
  1433. }
  1434. /* store */
  1435. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1436. /* 'Create' message */
  1437. xs *c_msg = msg_create(&snac, msg);
  1438. enqueue_message(&snac, c_msg);
  1439. timeline_touch(&snac);
  1440. /* convert to a mastodon status as a response code */
  1441. xs *st = mastoapi_status(&snac, msg);
  1442. *body = xs_json_dumps(st, 4);
  1443. *ctype = "application/json";
  1444. status = 200;
  1445. }
  1446. else
  1447. status = 401;
  1448. }
  1449. else
  1450. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1451. if (logged_in) {
  1452. /* operations on a status */
  1453. xs *l = xs_split(cmd, "/");
  1454. const char *mid = xs_list_get(l, 3);
  1455. const char *op = xs_list_get(l, 4);
  1456. if (!xs_is_null(mid)) {
  1457. xs *msg = NULL;
  1458. xs *out = NULL;
  1459. /* skip the 'fake' part of the id */
  1460. mid = MID_TO_MD5(mid);
  1461. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1462. char *id = xs_dict_get(msg, "id");
  1463. if (op == NULL) {
  1464. /* no operation (?) */
  1465. }
  1466. else
  1467. if (strcmp(op, "favourite") == 0) { /** **/
  1468. xs *n_msg = msg_admiration(&snac, id, "Like");
  1469. if (n_msg != NULL) {
  1470. enqueue_message(&snac, n_msg);
  1471. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1472. out = mastoapi_status(&snac, msg);
  1473. }
  1474. }
  1475. else
  1476. if (strcmp(op, "unfavourite") == 0) { /** **/
  1477. /* partial support: as the original Like message
  1478. is not stored anywhere here, it's not possible
  1479. to send an Undo + Like; the only thing done here
  1480. is to delete the actor from the list of likes */
  1481. object_unadmire(id, snac.actor, 1);
  1482. }
  1483. else
  1484. if (strcmp(op, "reblog") == 0) { /** **/
  1485. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1486. if (n_msg != NULL) {
  1487. enqueue_message(&snac, n_msg);
  1488. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1489. out = mastoapi_status(&snac, msg);
  1490. }
  1491. }
  1492. else
  1493. if (strcmp(op, "unreblog") == 0) { /** **/
  1494. /* partial support: see comment in 'unfavourite' */
  1495. object_unadmire(id, snac.actor, 0);
  1496. }
  1497. else
  1498. if (strcmp(op, "bookmark") == 0) { /** **/
  1499. /* snac does not support bookmarks */
  1500. }
  1501. else
  1502. if (strcmp(op, "unbookmark") == 0) { /** **/
  1503. /* snac does not support bookmarks */
  1504. }
  1505. else
  1506. if (strcmp(op, "pin") == 0) { /** **/
  1507. /* pin this message */
  1508. if (pin(&snac, id))
  1509. out = mastoapi_status(&snac, msg);
  1510. else
  1511. status = 422;
  1512. }
  1513. else
  1514. if (strcmp(op, "unpin") == 0) { /** **/
  1515. /* unpin this message */
  1516. unpin(&snac, id);
  1517. out = mastoapi_status(&snac, msg);
  1518. }
  1519. else
  1520. if (strcmp(op, "mute") == 0) { /** **/
  1521. /* Mastodon's mute is snac's hide */
  1522. }
  1523. else
  1524. if (strcmp(op, "unmute") == 0) { /** **/
  1525. /* Mastodon's unmute is snac's unhide */
  1526. }
  1527. }
  1528. if (out != NULL) {
  1529. *body = xs_json_dumps(out, 4);
  1530. *ctype = "application/json";
  1531. status = 200;
  1532. }
  1533. }
  1534. }
  1535. else
  1536. status = 401;
  1537. }
  1538. else
  1539. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1540. if (logged_in) {
  1541. notify_clear(&snac);
  1542. timeline_touch(&snac);
  1543. *body = xs_dup("{}");
  1544. *ctype = "application/json";
  1545. status = 200;
  1546. }
  1547. else
  1548. status = 401;
  1549. }
  1550. else
  1551. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1552. /* I don't know what I'm doing */
  1553. if (logged_in) {
  1554. char *v;
  1555. xs *wpush = xs_dict_new();
  1556. wpush = xs_dict_append(wpush, "id", "1");
  1557. v = xs_dict_get(args, "data");
  1558. v = xs_dict_get(v, "alerts");
  1559. wpush = xs_dict_append(wpush, "alerts", v);
  1560. v = xs_dict_get(args, "subscription");
  1561. v = xs_dict_get(v, "endpoint");
  1562. wpush = xs_dict_append(wpush, "endpoint", v);
  1563. xs *server_key = random_str();
  1564. wpush = xs_dict_append(wpush, "server_key", server_key);
  1565. *body = xs_json_dumps(wpush, 4);
  1566. *ctype = "application/json";
  1567. status = 200;
  1568. }
  1569. else
  1570. status = 401;
  1571. }
  1572. else
  1573. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1574. if (logged_in) {
  1575. const xs_list *file = xs_dict_get(args, "file");
  1576. const char *desc = xs_dict_get(args, "description");
  1577. if (xs_is_null(desc))
  1578. desc = "";
  1579. status = 400;
  1580. if (xs_type(file) == XSTYPE_LIST) {
  1581. const char *fn = xs_list_get(file, 0);
  1582. if (*fn != '\0') {
  1583. char *ext = strrchr(fn, '.');
  1584. xs *hash = xs_md5_hex(fn, strlen(fn));
  1585. xs *id = xs_fmt("%s%s", hash, ext);
  1586. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1587. int fo = xs_number_get(xs_list_get(file, 1));
  1588. int fs = xs_number_get(xs_list_get(file, 2));
  1589. /* store */
  1590. static_put(&snac, id, payload + fo, fs);
  1591. static_put_meta(&snac, id, desc);
  1592. /* prepare a response */
  1593. xs *rsp = xs_dict_new();
  1594. rsp = xs_dict_append(rsp, "id", id);
  1595. rsp = xs_dict_append(rsp, "type", "image");
  1596. rsp = xs_dict_append(rsp, "url", url);
  1597. rsp = xs_dict_append(rsp, "preview_url", url);
  1598. rsp = xs_dict_append(rsp, "remote_url", url);
  1599. rsp = xs_dict_append(rsp, "description", desc);
  1600. *body = xs_json_dumps(rsp, 4);
  1601. *ctype = "application/json";
  1602. status = 200;
  1603. }
  1604. }
  1605. }
  1606. else
  1607. status = 401;
  1608. }
  1609. else
  1610. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1611. if (logged_in) {
  1612. /* account-related information */
  1613. xs *l = xs_split(cmd, "/");
  1614. const char *md5 = xs_list_get(l, 3);
  1615. const char *opt = xs_list_get(l, 4);
  1616. xs *rsp = NULL;
  1617. if (!xs_is_null(md5) && *md5) {
  1618. xs *actor_o = NULL;
  1619. if (xs_is_null(opt)) {
  1620. /* ? */
  1621. }
  1622. else
  1623. if (strcmp(opt, "follow") == 0) { /** **/
  1624. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1625. const char *actor = xs_dict_get(actor_o, "id");
  1626. xs *msg = msg_follow(&snac, actor);
  1627. if (msg != NULL) {
  1628. /* reload the actor from the message, in may be different */
  1629. actor = xs_dict_get(msg, "object");
  1630. following_add(&snac, actor, msg);
  1631. enqueue_output_by_actor(&snac, msg, actor, 0);
  1632. rsp = mastoapi_relationship(&snac, md5);
  1633. }
  1634. }
  1635. }
  1636. else
  1637. if (strcmp(opt, "unfollow") == 0) { /** **/
  1638. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1639. const char *actor = xs_dict_get(actor_o, "id");
  1640. /* get the following object */
  1641. xs *object = NULL;
  1642. if (valid_status(following_get(&snac, actor, &object))) {
  1643. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1644. following_del(&snac, actor);
  1645. enqueue_output_by_actor(&snac, msg, actor, 0);
  1646. rsp = mastoapi_relationship(&snac, md5);
  1647. }
  1648. }
  1649. }
  1650. else
  1651. if (strcmp(opt, "block") == 0) { /** **/
  1652. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1653. const char *actor = xs_dict_get(actor_o, "id");
  1654. mute(&snac, actor);
  1655. rsp = mastoapi_relationship(&snac, md5);
  1656. }
  1657. }
  1658. else
  1659. if (strcmp(opt, "unblock") == 0) { /** **/
  1660. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1661. const char *actor = xs_dict_get(actor_o, "id");
  1662. unmute(&snac, actor);
  1663. rsp = mastoapi_relationship(&snac, md5);
  1664. }
  1665. }
  1666. }
  1667. if (rsp != NULL) {
  1668. *body = xs_json_dumps(rsp, 4);
  1669. *ctype = "application/json";
  1670. status = 200;
  1671. }
  1672. }
  1673. else
  1674. status = 401;
  1675. }
  1676. else
  1677. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1678. if (logged_in) {
  1679. /* operations on a status */
  1680. xs *l = xs_split(cmd, "/");
  1681. const char *mid = xs_list_get(l, 3);
  1682. const char *op = xs_list_get(l, 4);
  1683. if (!xs_is_null(mid)) {
  1684. xs *msg = NULL;
  1685. xs *out = NULL;
  1686. /* skip the 'fake' part of the id */
  1687. mid = MID_TO_MD5(mid);
  1688. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1689. const char *id = xs_dict_get(msg, "id");
  1690. const char *atto = xs_dict_get(msg, "attributedTo");
  1691. xs_list *opts = xs_dict_get(msg, "oneOf");
  1692. if (opts == NULL)
  1693. opts = xs_dict_get(msg, "anyOf");
  1694. if (op == NULL) {
  1695. }
  1696. else
  1697. if (strcmp(op, "votes") == 0) {
  1698. xs_list *choices = xs_dict_get(args, "choices[]");
  1699. if (xs_type(choices) == XSTYPE_LIST) {
  1700. xs_str *v;
  1701. while (xs_list_iter(&choices, &v)) {
  1702. int io = atoi(v);
  1703. const xs_dict *o = xs_list_get(opts, io);
  1704. if (o) {
  1705. const char *name = xs_dict_get(o, "name");
  1706. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1707. msg = xs_dict_append(msg, "name", name);
  1708. xs *c_msg = msg_create(&snac, msg);
  1709. enqueue_message(&snac, c_msg);
  1710. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1711. }
  1712. }
  1713. out = mastoapi_poll(&snac, msg);
  1714. }
  1715. }
  1716. }
  1717. if (out != NULL) {
  1718. *body = xs_json_dumps(out, 4);
  1719. *ctype = "application/json";
  1720. status = 200;
  1721. }
  1722. }
  1723. }
  1724. else
  1725. status = 401;
  1726. }
  1727. /* user cleanup */
  1728. if (logged_in)
  1729. user_free(&snac);
  1730. return status;
  1731. }
  1732. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1733. const char *payload, int p_size,
  1734. char **body, int *b_size, char **ctype)
  1735. {
  1736. (void)p_size;
  1737. (void)b_size;
  1738. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1739. return 0;
  1740. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1741. int status = 404;
  1742. xs *args = NULL;
  1743. char *i_ctype = xs_dict_get(req, "content-type");
  1744. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1745. args = xs_json_loads(payload);
  1746. else
  1747. args = xs_dup(xs_dict_get(req, "p_vars"));
  1748. if (args == NULL)
  1749. return 400;
  1750. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1751. snac snac = {0};
  1752. int logged_in = process_auth_token(&snac, req);
  1753. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1754. if (logged_in) {
  1755. xs *l = xs_split(cmd, "/");
  1756. const char *stid = xs_list_get(l, 3);
  1757. if (!xs_is_null(stid)) {
  1758. const char *desc = xs_dict_get(args, "description");
  1759. /* set the image metadata */
  1760. static_put_meta(&snac, stid, desc);
  1761. /* prepare a response */
  1762. xs *rsp = xs_dict_new();
  1763. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1764. rsp = xs_dict_append(rsp, "id", stid);
  1765. rsp = xs_dict_append(rsp, "type", "image");
  1766. rsp = xs_dict_append(rsp, "url", url);
  1767. rsp = xs_dict_append(rsp, "preview_url", url);
  1768. rsp = xs_dict_append(rsp, "remote_url", url);
  1769. rsp = xs_dict_append(rsp, "description", desc);
  1770. *body = xs_json_dumps(rsp, 4);
  1771. *ctype = "application/json";
  1772. status = 200;
  1773. }
  1774. }
  1775. else
  1776. status = 401;
  1777. }
  1778. /* user cleanup */
  1779. if (logged_in)
  1780. user_free(&snac);
  1781. return status;
  1782. }
  1783. void mastoapi_purge(void)
  1784. {
  1785. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1786. xs *files = xs_glob(spec, 1, 0);
  1787. xs_list *p = files;
  1788. xs_str *v;
  1789. time_t mt = time(NULL) - 3600;
  1790. while (xs_list_iter(&p, &v)) {
  1791. xs *cid = xs_replace(v, ".json", "");
  1792. xs *fn = _app_fn(cid);
  1793. if (mtime(fn) < mt) {
  1794. /* get the app */
  1795. xs *app = app_get(cid);
  1796. if (app) {
  1797. /* old apps with no uid are incomplete cruft */
  1798. const char *uid = xs_dict_get(app, "uid");
  1799. if (xs_is_null(uid) || *uid == '\0') {
  1800. unlink(fn);
  1801. srv_debug(2, xs_fmt("purged %s", fn));
  1802. }
  1803. }
  1804. }
  1805. }
  1806. }
  1807. #endif /* #ifndef NO_MASTODON_API */