mastoapi.c 80 KB

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