mastoapi.c 72 KB

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