mastoapi.c 76 KB

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