mastoapi.c 74 KB

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