mastoapi.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  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 && content)
  522. s1 = xs_fmt("%s<br><br>%s", name, content);
  523. else
  524. if (name)
  525. s1 = xs_dup(name);
  526. else
  527. if (content)
  528. s1 = xs_dup(content);
  529. else
  530. s1 = xs_str_new(NULL);
  531. st = xs_dict_append(st, "content", s1);
  532. }
  533. st = xs_dict_append(st, "visibility",
  534. is_msg_public(msg) ? "public" : "private");
  535. tmp = xs_dict_get(msg, "sensitive");
  536. if (xs_is_null(tmp))
  537. tmp = xs_stock_false;
  538. st = xs_dict_append(st, "sensitive", tmp);
  539. tmp = xs_dict_get(msg, "summary");
  540. if (xs_is_null(tmp))
  541. tmp = "";
  542. st = xs_dict_append(st, "spoiler_text", tmp);
  543. /* create the list of attachments */
  544. xs *matt = xs_list_new();
  545. xs_list *att = xs_dict_get(msg, "attachment");
  546. xs_str *aobj;
  547. xs *attr_list = NULL;
  548. if (xs_type(att) == XSTYPE_DICT) {
  549. attr_list = xs_list_new();
  550. attr_list = xs_list_append(attr_list, att);
  551. }
  552. else
  553. if (xs_type(att) == XSTYPE_LIST)
  554. attr_list = xs_dup(att);
  555. else
  556. attr_list = xs_list_new();
  557. /* if it has an image, add it as an attachment */
  558. xs_dict *image = xs_dict_get(msg, "image");
  559. if (!xs_is_null(image))
  560. attr_list = xs_list_append(attr_list, image);
  561. att = attr_list;
  562. while (xs_list_iter(&att, &aobj)) {
  563. const char *mtype = xs_dict_get(aobj, "mediaType");
  564. if (xs_is_null(mtype))
  565. mtype = xs_dict_get(aobj, "type");
  566. const char *url = xs_dict_get(aobj, "url");
  567. if (xs_is_null(url))
  568. url = xs_dict_get(aobj, "href");
  569. if (xs_is_null(url))
  570. continue;
  571. /* if it's a plain Link, check if it can be "rewritten" */
  572. if (xs_list_len(attr_list) < 2 && strcmp(mtype, "Link") == 0) {
  573. const char *mt = xs_mime_by_ext(url);
  574. if (xs_startswith(mt, "image/") ||
  575. xs_startswith(mt, "audio/") ||
  576. xs_startswith(mt, "video/"))
  577. mtype = mt;
  578. }
  579. if (!xs_is_null(mtype)) {
  580. if (xs_startswith(mtype, "image/") || xs_startswith(mtype, "video/") ||
  581. strcmp(mtype, "Image") == 0) {
  582. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  583. xs *matte = xs_dict_new();
  584. matte = xs_dict_append(matte, "id", matteid);
  585. matte = xs_dict_append(matte, "type", *mtype == 'v' ? "video" : "image");
  586. matte = xs_dict_append(matte, "url", url);
  587. matte = xs_dict_append(matte, "preview_url", url);
  588. matte = xs_dict_append(matte, "remote_url", url);
  589. const char *name = xs_dict_get(aobj, "name");
  590. if (xs_is_null(name))
  591. name = "";
  592. matte = xs_dict_append(matte, "description", name);
  593. matt = xs_list_append(matt, matte);
  594. }
  595. }
  596. }
  597. st = xs_dict_append(st, "media_attachments", matt);
  598. {
  599. xs *ml = xs_list_new();
  600. xs *htl = xs_list_new();
  601. xs *eml = xs_list_new();
  602. xs_list *tag = xs_dict_get(msg, "tag");
  603. int n = 0;
  604. xs *tag_list = NULL;
  605. if (xs_type(tag) == XSTYPE_DICT) {
  606. tag_list = xs_list_new();
  607. tag_list = xs_list_append(tag_list, tag);
  608. }
  609. else
  610. if (xs_type(tag) == XSTYPE_LIST)
  611. tag_list = xs_dup(tag);
  612. else
  613. tag_list = xs_list_new();
  614. tag = tag_list;
  615. xs_dict *v;
  616. while (xs_list_iter(&tag, &v)) {
  617. const char *type = xs_dict_get(v, "type");
  618. if (xs_is_null(type))
  619. continue;
  620. xs *d1 = xs_dict_new();
  621. if (strcmp(type, "Mention") == 0) {
  622. const char *name = xs_dict_get(v, "name");
  623. const char *href = xs_dict_get(v, "href");
  624. if (!xs_is_null(name) && !xs_is_null(href) &&
  625. (snac == NULL || strcmp(href, snac->actor) != 0)) {
  626. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  627. xs *id = xs_fmt("%d", n++);
  628. d1 = xs_dict_append(d1, "id", id);
  629. d1 = xs_dict_append(d1, "username", nm);
  630. d1 = xs_dict_append(d1, "acct", nm);
  631. d1 = xs_dict_append(d1, "url", href);
  632. ml = xs_list_append(ml, d1);
  633. }
  634. }
  635. else
  636. if (strcmp(type, "Hashtag") == 0) {
  637. const char *name = xs_dict_get(v, "name");
  638. const char *href = xs_dict_get(v, "href");
  639. if (!xs_is_null(name) && !xs_is_null(href)) {
  640. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  641. d1 = xs_dict_append(d1, "name", nm);
  642. d1 = xs_dict_append(d1, "url", href);
  643. htl = xs_list_append(htl, d1);
  644. }
  645. }
  646. else
  647. if (strcmp(type, "Emoji") == 0) {
  648. const char *name = xs_dict_get(v, "name");
  649. const xs_dict *icon = xs_dict_get(v, "icon");
  650. if (!xs_is_null(name) && !xs_is_null(icon)) {
  651. const char *url = xs_dict_get(icon, "url");
  652. if (!xs_is_null(url)) {
  653. xs *nm = xs_strip_chars_i(xs_dup(name), ":");
  654. d1 = xs_dict_append(d1, "shortcode", nm);
  655. d1 = xs_dict_append(d1, "url", url);
  656. d1 = xs_dict_append(d1, "static_url", url);
  657. d1 = xs_dict_append(d1, "visible_in_picker", xs_stock_true);
  658. d1 = xs_dict_append(d1, "category", "Emojis");
  659. eml = xs_list_append(eml, d1);
  660. }
  661. }
  662. }
  663. }
  664. st = xs_dict_append(st, "mentions", ml);
  665. st = xs_dict_append(st, "tags", htl);
  666. st = xs_dict_append(st, "emojis", eml);
  667. }
  668. xs_free(idx);
  669. xs_free(ixc);
  670. idx = object_likes(id);
  671. ixc = xs_number_new(xs_list_len(idx));
  672. st = xs_dict_append(st, "favourites_count", ixc);
  673. st = xs_dict_append(st, "favourited",
  674. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  675. xs_free(idx);
  676. xs_free(ixc);
  677. idx = object_announces(id);
  678. ixc = xs_number_new(xs_list_len(idx));
  679. st = xs_dict_append(st, "reblogs_count", ixc);
  680. st = xs_dict_append(st, "reblogged",
  681. (snac && xs_list_in(idx, snac->md5) != -1) ? xs_stock_true : xs_stock_false);
  682. xs_free(idx);
  683. xs_free(ixc);
  684. idx = object_children(id);
  685. ixc = xs_number_new(xs_list_len(idx));
  686. st = xs_dict_append(st, "replies_count", ixc);
  687. /* default in_reply_to values */
  688. st = xs_dict_append(st, "in_reply_to_id", xs_stock_null);
  689. st = xs_dict_append(st, "in_reply_to_account_id", xs_stock_null);
  690. tmp = xs_dict_get(msg, "inReplyTo");
  691. if (!xs_is_null(tmp)) {
  692. xs *irto = NULL;
  693. if (valid_status(object_get(tmp, &irto))) {
  694. xs *irt_mid = mastoapi_id(irto);
  695. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  696. char *at = NULL;
  697. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  698. xs *at_md5 = xs_md5_hex(at, strlen(at));
  699. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  700. }
  701. }
  702. }
  703. st = xs_dict_append(st, "reblog", xs_stock_null);
  704. st = xs_dict_append(st, "card", xs_stock_null);
  705. st = xs_dict_append(st, "language", xs_stock_null);
  706. tmp = xs_dict_get(msg, "sourceContent");
  707. if (xs_is_null(tmp))
  708. tmp = "";
  709. st = xs_dict_append(st, "text", tmp);
  710. tmp = xs_dict_get(msg, "updated");
  711. if (xs_is_null(tmp))
  712. tmp = xs_stock_null;
  713. st = xs_dict_append(st, "edited_at", tmp);
  714. if (strcmp(type, "Question") == 0) {
  715. xs *poll = mastoapi_poll(snac, msg);
  716. st = xs_dict_append(st, "poll", poll);
  717. }
  718. else
  719. st = xs_dict_append(st, "poll", xs_stock_null);
  720. st = xs_dict_append(st, "bookmarked", xs_stock_false);
  721. st = xs_dict_append(st, "pinned",
  722. (snac && is_pinned(snac, id)) ? xs_stock_true : xs_stock_false);
  723. return st;
  724. }
  725. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  726. {
  727. xs_dict *rel = NULL;
  728. xs *actor_o = NULL;
  729. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  730. rel = xs_dict_new();
  731. const char *actor = xs_dict_get(actor_o, "id");
  732. rel = xs_dict_append(rel, "id", md5);
  733. rel = xs_dict_append(rel, "following",
  734. following_check(snac, actor) ? xs_stock_true : xs_stock_false);
  735. rel = xs_dict_append(rel, "showing_reblogs", xs_stock_true);
  736. rel = xs_dict_append(rel, "notifying", xs_stock_false);
  737. rel = xs_dict_append(rel, "followed_by",
  738. follower_check(snac, actor) ? xs_stock_true : xs_stock_false);
  739. rel = xs_dict_append(rel, "blocking",
  740. is_muted(snac, actor) ? xs_stock_true : xs_stock_false);
  741. rel = xs_dict_append(rel, "muting", xs_stock_false);
  742. rel = xs_dict_append(rel, "muting_notifications", xs_stock_false);
  743. rel = xs_dict_append(rel, "requested", xs_stock_false);
  744. rel = xs_dict_append(rel, "domain_blocking", xs_stock_false);
  745. rel = xs_dict_append(rel, "endorsed", xs_stock_false);
  746. rel = xs_dict_append(rel, "note", "");
  747. }
  748. return rel;
  749. }
  750. int process_auth_token(snac *snac, const xs_dict *req)
  751. /* processes an authorization token, if there is one */
  752. {
  753. int logged_in = 0;
  754. char *v;
  755. /* if there is an authorization field, try to validate it */
  756. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  757. xs *tokid = xs_replace_n(v, "Bearer ", "", 1);
  758. xs *token = token_get(tokid);
  759. if (token != NULL) {
  760. const char *uid = xs_dict_get(token, "uid");
  761. if (!xs_is_null(uid) && user_open(snac, uid)) {
  762. logged_in = 1;
  763. /* this counts as a 'login' */
  764. lastlog_write(snac, "mastoapi");
  765. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  766. }
  767. else
  768. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  769. }
  770. else
  771. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  772. }
  773. return logged_in;
  774. }
  775. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  776. char **body, int *b_size, char **ctype)
  777. {
  778. (void)b_size;
  779. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  780. return 0;
  781. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  782. int status = 404;
  783. xs_dict *args = xs_dict_get(req, "q_vars");
  784. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  785. snac snac1 = {0};
  786. int logged_in = process_auth_token(&snac1, req);
  787. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) { /** **/
  788. if (logged_in) {
  789. xs *acct = xs_dict_new();
  790. acct = xs_dict_append(acct, "id", snac1.md5);
  791. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  792. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  793. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  794. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  795. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  796. acct = xs_dict_append(acct, "url", snac1.actor);
  797. acct = xs_dict_append(acct, "header", "");
  798. xs *src = xs_json_loads("{\"privacy\":\"public\","
  799. "\"sensitive\":false,\"fields\":[],\"note\":\"\"}");
  800. acct = xs_dict_append(acct, "source", src);
  801. xs *avatar = NULL;
  802. char *av = xs_dict_get(snac1.config, "avatar");
  803. if (xs_is_null(av) || *av == '\0')
  804. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  805. else
  806. avatar = xs_dup(av);
  807. acct = xs_dict_append(acct, "avatar", avatar);
  808. acct = xs_dict_append(acct, "avatar_static", avatar);
  809. *body = xs_json_dumps(acct, 4);
  810. *ctype = "application/json";
  811. status = 200;
  812. }
  813. else {
  814. status = 422; // "Unprocessable entity" (no login)
  815. }
  816. }
  817. else
  818. if (strcmp(cmd, "/v1/accounts/relationships") == 0) { /** **/
  819. /* find if an account is followed, blocked, etc. */
  820. /* the account to get relationships about is in args "id[]" */
  821. if (logged_in) {
  822. xs *res = xs_list_new();
  823. const char *md5 = xs_dict_get(args, "id[]");
  824. if (xs_is_null(md5))
  825. md5 = xs_dict_get(args, "id");
  826. if (!xs_is_null(md5)) {
  827. if (xs_type(md5) == XSTYPE_LIST)
  828. md5 = xs_list_get(md5, 0);
  829. xs *rel = mastoapi_relationship(&snac1, md5);
  830. if (rel != NULL)
  831. res = xs_list_append(res, rel);
  832. }
  833. *body = xs_json_dumps(res, 4);
  834. *ctype = "application/json";
  835. status = 200;
  836. }
  837. else
  838. status = 422;
  839. }
  840. else
  841. if (xs_startswith(cmd, "/v1/accounts/")) { /** **/
  842. /* account-related information */
  843. xs *l = xs_split(cmd, "/");
  844. const char *uid = xs_list_get(l, 3);
  845. const char *opt = xs_list_get(l, 4);
  846. if (uid != NULL) {
  847. snac snac2;
  848. xs *out = NULL;
  849. xs *actor = NULL;
  850. if (logged_in && strcmp(uid, "search") == 0) { /** **/
  851. /* search for accounts starting with q */
  852. const char *aq = xs_dict_get(args, "q");
  853. if (!xs_is_null(aq)) {
  854. xs *q = xs_tolower_i(xs_dup(aq));
  855. out = xs_list_new();
  856. xs *wing = following_list(&snac1);
  857. xs *wers = follower_list(&snac1);
  858. xs *ulst = user_list();
  859. xs_list *p;
  860. xs_str *v;
  861. xs_set seen;
  862. xs_set_init(&seen);
  863. /* user relations */
  864. xs_list *lsts[] = { wing, wers, NULL };
  865. int n;
  866. for (n = 0; (p = lsts[n]) != NULL; n++) {
  867. while (xs_list_iter(&p, &v)) {
  868. /* already seen? skip */
  869. if (xs_set_add(&seen, v) == 0)
  870. continue;
  871. xs *actor = NULL;
  872. if (valid_status(object_get(v, &actor))) {
  873. const char *uname = xs_dict_get(actor, "preferredUsername");
  874. if (!xs_is_null(uname)) {
  875. xs *luname = xs_tolower_i(xs_dup(uname));
  876. if (xs_startswith(luname, q)) {
  877. xs *acct = mastoapi_account(actor);
  878. out = xs_list_append(out, acct);
  879. }
  880. }
  881. }
  882. }
  883. }
  884. /* local users */
  885. p = ulst;
  886. while (xs_list_iter(&p, &v)) {
  887. snac user;
  888. /* skip this same user */
  889. if (strcmp(v, xs_dict_get(snac1.config, "uid")) == 0)
  890. continue;
  891. /* skip if the uid does not start with the query */
  892. xs *v2 = xs_tolower_i(xs_dup(v));
  893. if (!xs_startswith(v2, q))
  894. continue;
  895. if (user_open(&user, v)) {
  896. /* if it's not already seen, add it */
  897. if (xs_set_add(&seen, user.actor) == 1) {
  898. xs *actor = msg_actor(&user);
  899. xs *acct = mastoapi_account(actor);
  900. out = xs_list_append(out, acct);
  901. }
  902. user_free(&user);
  903. }
  904. }
  905. xs_set_free(&seen);
  906. }
  907. }
  908. else
  909. /* is it a local user? */
  910. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  911. if (opt == NULL) {
  912. /* account information */
  913. actor = msg_actor(&snac2);
  914. out = mastoapi_account(actor);
  915. }
  916. else
  917. if (strcmp(opt, "statuses") == 0) { /** **/
  918. /* the public list of posts of a user */
  919. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  920. xs_list *p = timeline;
  921. xs_str *v;
  922. out = xs_list_new();
  923. while (xs_list_iter(&p, &v)) {
  924. xs *msg = NULL;
  925. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  926. /* add only posts by the author */
  927. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  928. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  929. xs *st = mastoapi_status(&snac2, msg);
  930. if (st)
  931. out = xs_list_append(out, st);
  932. }
  933. }
  934. }
  935. }
  936. user_free(&snac2);
  937. }
  938. else {
  939. /* try the uid as the md5 of a possibly loaded actor */
  940. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  941. if (opt == NULL) {
  942. /* account information */
  943. out = mastoapi_account(actor);
  944. }
  945. else
  946. if (strcmp(opt, "statuses") == 0) {
  947. /* we don't serve statuses of others; return the empty list */
  948. out = xs_list_new();
  949. }
  950. }
  951. }
  952. if (out != NULL) {
  953. *body = xs_json_dumps(out, 4);
  954. *ctype = "application/json";
  955. status = 200;
  956. }
  957. }
  958. }
  959. else
  960. if (strcmp(cmd, "/v1/timelines/home") == 0) { /** **/
  961. /* the private timeline */
  962. if (logged_in) {
  963. const char *max_id = xs_dict_get(args, "max_id");
  964. const char *since_id = xs_dict_get(args, "since_id");
  965. const char *min_id = xs_dict_get(args, "min_id");
  966. const char *limit_s = xs_dict_get(args, "limit");
  967. int limit = 0;
  968. int cnt = 0;
  969. if (!xs_is_null(limit_s))
  970. limit = atoi(limit_s);
  971. if (limit == 0)
  972. limit = 20;
  973. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  974. xs *out = xs_list_new();
  975. xs_list *p = timeline;
  976. xs_str *v;
  977. while (xs_list_iter(&p, &v) && cnt < limit) {
  978. xs *msg = NULL;
  979. /* only return entries older that max_id */
  980. if (max_id) {
  981. if (strcmp(v, MID_TO_MD5(max_id)) == 0)
  982. max_id = NULL;
  983. continue;
  984. }
  985. /* only returns entries newer than since_id */
  986. if (since_id) {
  987. if (strcmp(v, MID_TO_MD5(since_id)) == 0)
  988. break;
  989. }
  990. /* only returns entries newer than min_id */
  991. /* what does really "Return results immediately newer than ID" mean? */
  992. if (min_id) {
  993. if (strcmp(v, MID_TO_MD5(min_id)) == 0)
  994. break;
  995. }
  996. /* get the entry */
  997. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  998. continue;
  999. /* discard non-Notes */
  1000. const char *type = xs_dict_get(msg, "type");
  1001. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1002. continue;
  1003. #if 0
  1004. /* discard notes from people we don't follow with no boosts */
  1005. if (!following_check(&snac1, xs_dict_get(msg, "attributedTo")) &&
  1006. object_announces_len(xs_dict_get(msg, "id")) == 0)
  1007. continue;
  1008. #endif
  1009. /* discard notes from muted morons */
  1010. if (is_muted(&snac1, xs_dict_get(msg, "attributedTo")))
  1011. continue;
  1012. /* discard hidden notes */
  1013. if (is_hidden(&snac1, xs_dict_get(msg, "id")))
  1014. continue;
  1015. /* discard poll votes (they have a name) */
  1016. if (!xs_is_null(xs_dict_get(msg, "name")))
  1017. continue;
  1018. /* convert the Note into a Mastodon status */
  1019. xs *st = mastoapi_status(&snac1, msg);
  1020. if (st != NULL)
  1021. out = xs_list_append(out, st);
  1022. cnt++;
  1023. }
  1024. *body = xs_json_dumps(out, 4);
  1025. *ctype = "application/json";
  1026. status = 200;
  1027. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  1028. }
  1029. else {
  1030. status = 401; // unauthorized
  1031. }
  1032. }
  1033. else
  1034. if (strcmp(cmd, "/v1/timelines/public") == 0) { /** **/
  1035. /* the instance public timeline (public timelines for all users) */
  1036. const char *limit_s = xs_dict_get(args, "limit");
  1037. int limit = 0;
  1038. int cnt = 0;
  1039. if (!xs_is_null(limit_s))
  1040. limit = atoi(limit_s);
  1041. if (limit == 0)
  1042. limit = 20;
  1043. xs *timeline = timeline_instance_list(0, limit);
  1044. xs *out = xs_list_new();
  1045. xs_list *p = timeline;
  1046. xs_str *md5;
  1047. snac *user = NULL;
  1048. if (logged_in)
  1049. user = &snac1;
  1050. while (xs_list_iter(&p, &md5) && cnt < limit) {
  1051. xs *msg = NULL;
  1052. /* get the entry */
  1053. if (!valid_status(object_get_by_md5(md5, &msg)))
  1054. continue;
  1055. /* discard non-Notes */
  1056. const char *type = xs_dict_get(msg, "type");
  1057. if (strcmp(type, "Note") != 0 && strcmp(type, "Question") != 0)
  1058. continue;
  1059. /* convert the Note into a Mastodon status */
  1060. xs *st = mastoapi_status(user, msg);
  1061. if (st != NULL) {
  1062. out = xs_list_append(out, st);
  1063. cnt++;
  1064. }
  1065. }
  1066. *body = xs_json_dumps(out, 4);
  1067. *ctype = "application/json";
  1068. status = 200;
  1069. }
  1070. else
  1071. if (strcmp(cmd, "/v1/conversations") == 0) { /** **/
  1072. /* TBD */
  1073. *body = xs_dup("[]");
  1074. *ctype = "application/json";
  1075. status = 200;
  1076. }
  1077. else
  1078. if (strcmp(cmd, "/v1/notifications") == 0) { /** **/
  1079. if (logged_in) {
  1080. xs *l = notify_list(&snac1, 0);
  1081. xs *out = xs_list_new();
  1082. xs_list *p = l;
  1083. xs_dict *v;
  1084. xs_list *excl = xs_dict_get(args, "exclude_types[]");
  1085. while (xs_list_iter(&p, &v)) {
  1086. xs *noti = notify_get(&snac1, v);
  1087. if (noti == NULL)
  1088. continue;
  1089. const char *type = xs_dict_get(noti, "type");
  1090. const char *utype = xs_dict_get(noti, "utype");
  1091. const char *objid = xs_dict_get(noti, "objid");
  1092. xs *actor = NULL;
  1093. xs *entry = NULL;
  1094. if (!valid_status(actor_get(xs_dict_get(noti, "actor"), &actor)))
  1095. continue;
  1096. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  1097. continue;
  1098. if (is_hidden(&snac1, objid))
  1099. continue;
  1100. /* convert the type */
  1101. if (strcmp(type, "Like") == 0)
  1102. type = "favourite";
  1103. else
  1104. if (strcmp(type, "Announce") == 0)
  1105. type = "reblog";
  1106. else
  1107. if (strcmp(type, "Follow") == 0)
  1108. type = "follow";
  1109. else
  1110. if (strcmp(type, "Create") == 0)
  1111. type = "mention";
  1112. else
  1113. if (strcmp(type, "Update") == 0 && strcmp(utype, "Question") == 0)
  1114. type = "poll";
  1115. else
  1116. continue;
  1117. /* excluded type? */
  1118. if (!xs_is_null(excl) && xs_list_in(excl, type) != -1)
  1119. continue;
  1120. xs *mn = xs_dict_new();
  1121. mn = xs_dict_append(mn, "type", type);
  1122. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  1123. mn = xs_dict_append(mn, "id", id);
  1124. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  1125. xs *acct = mastoapi_account(actor);
  1126. mn = xs_dict_append(mn, "account", acct);
  1127. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  1128. xs *st = mastoapi_status(&snac1, entry);
  1129. if (st)
  1130. mn = xs_dict_append(mn, "status", st);
  1131. }
  1132. out = xs_list_append(out, mn);
  1133. }
  1134. *body = xs_json_dumps(out, 4);
  1135. *ctype = "application/json";
  1136. status = 200;
  1137. }
  1138. else
  1139. status = 401;
  1140. }
  1141. else
  1142. if (strcmp(cmd, "/v1/filters") == 0) { /** **/
  1143. /* snac will never have filters */
  1144. *body = xs_dup("[]");
  1145. *ctype = "application/json";
  1146. status = 200;
  1147. }
  1148. else
  1149. if (strcmp(cmd, "/v1/favourites") == 0) { /** **/
  1150. /* snac will never support a list of favourites */
  1151. *body = xs_dup("[]");
  1152. *ctype = "application/json";
  1153. status = 200;
  1154. }
  1155. else
  1156. if (strcmp(cmd, "/v1/bookmarks") == 0) { /** **/
  1157. /* snac does not support bookmarks */
  1158. *body = xs_dup("[]");
  1159. *ctype = "application/json";
  1160. status = 200;
  1161. }
  1162. else
  1163. if (strcmp(cmd, "/v1/lists") == 0) { /** **/
  1164. /* snac does not support lists */
  1165. *body = xs_dup("[]");
  1166. *ctype = "application/json";
  1167. status = 200;
  1168. }
  1169. else
  1170. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) { /** **/
  1171. /* snac does not schedule notes */
  1172. *body = xs_dup("[]");
  1173. *ctype = "application/json";
  1174. status = 200;
  1175. }
  1176. else
  1177. if (strcmp(cmd, "/v1/follow_requests") == 0) { /** **/
  1178. /* snac does not support optional follow confirmations */
  1179. *body = xs_dup("[]");
  1180. *ctype = "application/json";
  1181. status = 200;
  1182. }
  1183. else
  1184. if (strcmp(cmd, "/v1/announcements") == 0) { /** **/
  1185. /* snac has no announcements (yet?) */
  1186. *body = xs_dup("[]");
  1187. *ctype = "application/json";
  1188. status = 200;
  1189. }
  1190. else
  1191. if (strcmp(cmd, "/v1/custom_emojis") == 0) { /** **/
  1192. /* are you kidding me? */
  1193. *body = xs_dup("[]");
  1194. *ctype = "application/json";
  1195. status = 200;
  1196. }
  1197. else
  1198. if (strcmp(cmd, "/v1/instance") == 0) { /** **/
  1199. /* returns an instance object */
  1200. xs *ins = xs_dict_new();
  1201. const char *host = xs_dict_get(srv_config, "host");
  1202. const char *title = xs_dict_get(srv_config, "title");
  1203. const char *sdesc = xs_dict_get(srv_config, "short_description");
  1204. ins = xs_dict_append(ins, "uri", host);
  1205. ins = xs_dict_append(ins, "domain", host);
  1206. ins = xs_dict_append(ins, "title", title && *title ? title : host);
  1207. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  1208. ins = xs_dict_append(ins, "source_url", WHAT_IS_SNAC_URL);
  1209. ins = xs_dict_append(ins, "description", host);
  1210. ins = xs_dict_append(ins, "short_description", sdesc && *sdesc ? sdesc : host);
  1211. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  1212. ins = xs_dict_append(ins, "thumbnail", susie);
  1213. const char *v = xs_dict_get(srv_config, "admin_email");
  1214. if (xs_is_null(v) || *v == '\0')
  1215. v = "admin@localhost";
  1216. ins = xs_dict_append(ins, "email", v);
  1217. ins = xs_dict_append(ins, "rules", xs_stock_list);
  1218. xs *l1 = xs_list_append(xs_list_new(), "en");
  1219. ins = xs_dict_append(ins, "languages", l1);
  1220. ins = xs_dict_append(ins, "urls", xs_stock_dict);
  1221. xs *d2 = xs_dict_append(xs_dict_new(), "user_count", xs_stock_0);
  1222. d2 = xs_dict_append(d2, "status_count", xs_stock_0);
  1223. d2 = xs_dict_append(d2, "domain_count", xs_stock_0);
  1224. ins = xs_dict_append(ins, "stats", d2);
  1225. ins = xs_dict_append(ins, "registrations", xs_stock_false);
  1226. ins = xs_dict_append(ins, "approval_required", xs_stock_false);
  1227. ins = xs_dict_append(ins, "invites_enabled", xs_stock_false);
  1228. xs *cfg = xs_dict_new();
  1229. {
  1230. xs *d11 = xs_json_loads("{\"characters_reserved_per_url\":32,"
  1231. "\"max_characters\":100000,\"max_media_attachments\":8}");
  1232. cfg = xs_dict_append(cfg, "statuses", d11);
  1233. xs *d12 = xs_json_loads("{\"max_featured_tags\":10}");
  1234. cfg = xs_dict_append(cfg, "accounts", d12);
  1235. xs *d13 = xs_json_loads("{\"image_matrix_limit\":33177600,"
  1236. "\"image_size_limit\":16777216,"
  1237. "\"video_frame_rate_limit\":120,"
  1238. "\"video_matrix_limit\":8294400,"
  1239. "\"video_size_limit\":103809024}"
  1240. );
  1241. {
  1242. /* get the supported mime types from the internal list */
  1243. const char **p = xs_mime_types;
  1244. xs_set mtypes;
  1245. xs_set_init(&mtypes);
  1246. while (*p) {
  1247. const char *type = p[1];
  1248. if (xs_startswith(type, "image/") ||
  1249. xs_startswith(type, "video/") ||
  1250. xs_startswith(type, "audio/"))
  1251. xs_set_add(&mtypes, type);
  1252. p += 2;
  1253. }
  1254. xs *l = xs_set_result(&mtypes);
  1255. d13 = xs_dict_append(d13, "supported_mime_types", l);
  1256. }
  1257. cfg = xs_dict_append(cfg, "media_attachments", d13);
  1258. xs *d14 = xs_json_loads("{\"max_characters_per_option\":50,"
  1259. "\"max_expiration\":2629746,"
  1260. "\"max_options\":8,\"min_expiration\":300}");
  1261. cfg = xs_dict_append(cfg, "polls", d14);
  1262. }
  1263. ins = xs_dict_append(ins, "configuration", cfg);
  1264. const char *admin_account = xs_dict_get(srv_config, "admin_account");
  1265. if (!xs_is_null(admin_account) && *admin_account) {
  1266. snac admin;
  1267. if (user_open(&admin, admin_account)) {
  1268. xs *actor = msg_actor(&admin);
  1269. xs *acct = mastoapi_account(actor);
  1270. ins = xs_dict_append(ins, "contact_account", acct);
  1271. user_free(&admin);
  1272. }
  1273. }
  1274. *body = xs_json_dumps(ins, 4);
  1275. *ctype = "application/json";
  1276. status = 200;
  1277. }
  1278. else
  1279. if (xs_startswith(cmd, "/v1/statuses/")) { /** **/
  1280. /* information about a status */
  1281. if (logged_in) {
  1282. xs *l = xs_split(cmd, "/");
  1283. const char *id = xs_list_get(l, 3);
  1284. const char *op = xs_list_get(l, 4);
  1285. if (!xs_is_null(id)) {
  1286. xs *msg = NULL;
  1287. xs *out = NULL;
  1288. /* skip the 'fake' part of the id */
  1289. id = MID_TO_MD5(id);
  1290. if (valid_status(object_get_by_md5(id, &msg))) {
  1291. if (op == NULL) {
  1292. if (!is_muted(&snac1, xs_dict_get(msg, "attributedTo"))) {
  1293. /* return the status itself */
  1294. out = mastoapi_status(&snac1, msg);
  1295. }
  1296. }
  1297. else
  1298. if (strcmp(op, "context") == 0) { /** **/
  1299. /* return ancestors and children */
  1300. xs *anc = xs_list_new();
  1301. xs *des = xs_list_new();
  1302. xs_list *p;
  1303. xs_str *v;
  1304. char pid[64];
  1305. /* build the [grand]parent list, moving up */
  1306. strncpy(pid, id, sizeof(pid));
  1307. while (object_parent(pid, pid, sizeof(pid))) {
  1308. xs *m2 = NULL;
  1309. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1310. xs *st = mastoapi_status(&snac1, m2);
  1311. if (st)
  1312. anc = xs_list_insert(anc, 0, st);
  1313. }
  1314. else
  1315. break;
  1316. }
  1317. /* build the children list */
  1318. xs *children = object_children(xs_dict_get(msg, "id"));
  1319. p = children;
  1320. while (xs_list_iter(&p, &v)) {
  1321. xs *m2 = NULL;
  1322. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1323. if (xs_is_null(xs_dict_get(m2, "name"))) {
  1324. xs *st = mastoapi_status(&snac1, m2);
  1325. if (st)
  1326. des = xs_list_append(des, st);
  1327. }
  1328. }
  1329. }
  1330. out = xs_dict_new();
  1331. out = xs_dict_append(out, "ancestors", anc);
  1332. out = xs_dict_append(out, "descendants", des);
  1333. }
  1334. else
  1335. if (strcmp(op, "reblogged_by") == 0 || /** **/
  1336. strcmp(op, "favourited_by") == 0) { /** **/
  1337. /* return the list of people who liked or boosted this */
  1338. out = xs_list_new();
  1339. xs *l = NULL;
  1340. if (op[0] == 'r')
  1341. l = object_announces(xs_dict_get(msg, "id"));
  1342. else
  1343. l = object_likes(xs_dict_get(msg, "id"));
  1344. xs_list *p = l;
  1345. xs_str *v;
  1346. while (xs_list_iter(&p, &v)) {
  1347. xs *actor2 = NULL;
  1348. if (valid_status(object_get_by_md5(v, &actor2))) {
  1349. xs *acct2 = mastoapi_account(actor2);
  1350. out = xs_list_append(out, acct2);
  1351. }
  1352. }
  1353. }
  1354. }
  1355. else
  1356. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1357. if (out != NULL) {
  1358. *body = xs_json_dumps(out, 4);
  1359. *ctype = "application/json";
  1360. status = 200;
  1361. }
  1362. }
  1363. }
  1364. else
  1365. status = 401;
  1366. }
  1367. else
  1368. if (strcmp(cmd, "/v1/preferences") == 0) { /** **/
  1369. *body = xs_dup("{}");
  1370. *ctype = "application/json";
  1371. status = 200;
  1372. }
  1373. else
  1374. if (strcmp(cmd, "/v1/markers") == 0) { /** **/
  1375. *body = xs_dup("{}");
  1376. *ctype = "application/json";
  1377. status = 200;
  1378. }
  1379. else
  1380. if (strcmp(cmd, "/v1/followed_tags") == 0) { /** **/
  1381. *body = xs_dup("[]");
  1382. *ctype = "application/json";
  1383. status = 200;
  1384. }
  1385. else
  1386. if (strcmp(cmd, "/v2/search") == 0) { /** **/
  1387. if (logged_in) {
  1388. const char *q = xs_dict_get(args, "q");
  1389. const char *type = xs_dict_get(args, "type");
  1390. const char *offset = xs_dict_get(args, "offset");
  1391. xs *acl = xs_list_new();
  1392. xs *stl = xs_list_new();
  1393. xs *htl = xs_list_new();
  1394. xs *res = xs_dict_new();
  1395. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1396. /* reply something only for offset 0; otherwise,
  1397. apps like Tusky keep asking again and again */
  1398. if (!xs_is_null(q) && !xs_is_null(type) && strcmp(type, "accounts") == 0) {
  1399. /* do a webfinger query */
  1400. char *actor = NULL;
  1401. char *user = NULL;
  1402. if (valid_status(webfinger_request(q, &actor, &user))) {
  1403. xs *actor_o = NULL;
  1404. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1405. xs *acct = mastoapi_account(actor_o);
  1406. acl = xs_list_append(acl, acct);
  1407. }
  1408. }
  1409. }
  1410. }
  1411. res = xs_dict_append(res, "accounts", acl);
  1412. res = xs_dict_append(res, "statuses", stl);
  1413. res = xs_dict_append(res, "hashtags", htl);
  1414. *body = xs_json_dumps(res, 4);
  1415. *ctype = "application/json";
  1416. status = 200;
  1417. }
  1418. else
  1419. status = 401;
  1420. }
  1421. /* user cleanup */
  1422. if (logged_in)
  1423. user_free(&snac1);
  1424. return status;
  1425. }
  1426. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1427. const char *payload, int p_size,
  1428. char **body, int *b_size, char **ctype)
  1429. {
  1430. (void)p_size;
  1431. (void)b_size;
  1432. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1433. return 0;
  1434. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1435. int status = 404;
  1436. xs *args = NULL;
  1437. char *i_ctype = xs_dict_get(req, "content-type");
  1438. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1439. args = xs_json_loads(payload);
  1440. else
  1441. args = xs_dup(xs_dict_get(req, "p_vars"));
  1442. if (args == NULL)
  1443. return 400;
  1444. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1445. snac snac = {0};
  1446. int logged_in = process_auth_token(&snac, req);
  1447. if (strcmp(cmd, "/v1/apps") == 0) { /** **/
  1448. const char *name = xs_dict_get(args, "client_name");
  1449. const char *ruri = xs_dict_get(args, "redirect_uris");
  1450. const char *scope = xs_dict_get(args, "scope");
  1451. if (xs_type(ruri) == XSTYPE_LIST)
  1452. ruri = xs_dict_get(ruri, 0);
  1453. if (name && ruri) {
  1454. xs *app = xs_dict_new();
  1455. xs *id = xs_replace_i(tid(0), ".", "");
  1456. xs *csec = random_str();
  1457. xs *vkey = random_str();
  1458. xs *cid = NULL;
  1459. /* pick a non-existent random cid */
  1460. for (;;) {
  1461. cid = random_str();
  1462. xs *p_app = app_get(cid);
  1463. if (p_app == NULL)
  1464. break;
  1465. xs_free(cid);
  1466. }
  1467. app = xs_dict_append(app, "name", name);
  1468. app = xs_dict_append(app, "redirect_uri", ruri);
  1469. app = xs_dict_append(app, "client_id", cid);
  1470. app = xs_dict_append(app, "client_secret", csec);
  1471. app = xs_dict_append(app, "vapid_key", vkey);
  1472. app = xs_dict_append(app, "id", id);
  1473. *body = xs_json_dumps(app, 4);
  1474. *ctype = "application/json";
  1475. status = 200;
  1476. app = xs_dict_append(app, "code", "");
  1477. if (scope)
  1478. app = xs_dict_append(app, "scope", scope);
  1479. app_add(cid, app);
  1480. srv_debug(1, xs_fmt("mastoapi apps: new app %s", cid));
  1481. }
  1482. }
  1483. else
  1484. if (strcmp(cmd, "/v1/statuses") == 0) { /** **/
  1485. if (logged_in) {
  1486. /* post a new Note */
  1487. const char *content = xs_dict_get(args, "status");
  1488. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1489. const char *visibility = xs_dict_get(args, "visibility");
  1490. const char *summary = xs_dict_get(args, "spoiler_text");
  1491. const char *media_ids = xs_dict_get(args, "media_ids");
  1492. if (xs_is_null(media_ids))
  1493. media_ids = xs_dict_get(args, "media_ids[]");
  1494. if (xs_is_null(media_ids))
  1495. media_ids = xs_dict_get(args, "media_ids");
  1496. if (xs_is_null(visibility))
  1497. visibility = "public";
  1498. xs *attach_list = xs_list_new();
  1499. xs *irt = NULL;
  1500. /* is it a reply? */
  1501. if (mid != NULL) {
  1502. xs *r_msg = NULL;
  1503. const char *md5 = MID_TO_MD5(mid);
  1504. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1505. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1506. }
  1507. /* does it have attachments? */
  1508. if (!xs_is_null(media_ids)) {
  1509. xs *mi = NULL;
  1510. if (xs_type(media_ids) == XSTYPE_LIST)
  1511. mi = xs_dup(media_ids);
  1512. else {
  1513. mi = xs_list_new();
  1514. mi = xs_list_append(mi, media_ids);
  1515. }
  1516. xs_list *p = mi;
  1517. xs_str *v;
  1518. while (xs_list_iter(&p, &v)) {
  1519. xs *l = xs_list_new();
  1520. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1521. xs *desc = static_get_meta(&snac, v);
  1522. l = xs_list_append(l, url);
  1523. l = xs_list_append(l, desc);
  1524. attach_list = xs_list_append(attach_list, l);
  1525. }
  1526. }
  1527. /* prepare the message */
  1528. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1529. strcmp(visibility, "public") == 0 ? 0 : 1);
  1530. if (!xs_is_null(summary) && *summary) {
  1531. msg = xs_dict_set(msg, "sensitive", xs_stock_true);
  1532. msg = xs_dict_set(msg, "summary", summary);
  1533. }
  1534. /* store */
  1535. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1536. /* 'Create' message */
  1537. xs *c_msg = msg_create(&snac, msg);
  1538. enqueue_message(&snac, c_msg);
  1539. timeline_touch(&snac);
  1540. /* convert to a mastodon status as a response code */
  1541. xs *st = mastoapi_status(&snac, msg);
  1542. *body = xs_json_dumps(st, 4);
  1543. *ctype = "application/json";
  1544. status = 200;
  1545. }
  1546. else
  1547. status = 401;
  1548. }
  1549. else
  1550. if (xs_startswith(cmd, "/v1/statuses")) { /** **/
  1551. if (logged_in) {
  1552. /* operations on a status */
  1553. xs *l = xs_split(cmd, "/");
  1554. const char *mid = xs_list_get(l, 3);
  1555. const char *op = xs_list_get(l, 4);
  1556. if (!xs_is_null(mid)) {
  1557. xs *msg = NULL;
  1558. xs *out = NULL;
  1559. /* skip the 'fake' part of the id */
  1560. mid = MID_TO_MD5(mid);
  1561. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1562. char *id = xs_dict_get(msg, "id");
  1563. if (op == NULL) {
  1564. /* no operation (?) */
  1565. }
  1566. else
  1567. if (strcmp(op, "favourite") == 0) { /** **/
  1568. xs *n_msg = msg_admiration(&snac, id, "Like");
  1569. if (n_msg != NULL) {
  1570. enqueue_message(&snac, n_msg);
  1571. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1572. out = mastoapi_status(&snac, msg);
  1573. }
  1574. }
  1575. else
  1576. if (strcmp(op, "unfavourite") == 0) { /** **/
  1577. /* partial support: as the original Like message
  1578. is not stored anywhere here, it's not possible
  1579. to send an Undo + Like; the only thing done here
  1580. is to delete the actor from the list of likes */
  1581. object_unadmire(id, snac.actor, 1);
  1582. }
  1583. else
  1584. if (strcmp(op, "reblog") == 0) { /** **/
  1585. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1586. if (n_msg != NULL) {
  1587. enqueue_message(&snac, n_msg);
  1588. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1589. out = mastoapi_status(&snac, msg);
  1590. }
  1591. }
  1592. else
  1593. if (strcmp(op, "unreblog") == 0) { /** **/
  1594. /* partial support: see comment in 'unfavourite' */
  1595. object_unadmire(id, snac.actor, 0);
  1596. }
  1597. else
  1598. if (strcmp(op, "bookmark") == 0) { /** **/
  1599. /* snac does not support bookmarks */
  1600. }
  1601. else
  1602. if (strcmp(op, "unbookmark") == 0) { /** **/
  1603. /* snac does not support bookmarks */
  1604. }
  1605. else
  1606. if (strcmp(op, "pin") == 0) { /** **/
  1607. /* pin this message */
  1608. if (pin(&snac, id))
  1609. out = mastoapi_status(&snac, msg);
  1610. else
  1611. status = 422;
  1612. }
  1613. else
  1614. if (strcmp(op, "unpin") == 0) { /** **/
  1615. /* unpin this message */
  1616. unpin(&snac, id);
  1617. out = mastoapi_status(&snac, msg);
  1618. }
  1619. else
  1620. if (strcmp(op, "mute") == 0) { /** **/
  1621. /* Mastodon's mute is snac's hide */
  1622. }
  1623. else
  1624. if (strcmp(op, "unmute") == 0) { /** **/
  1625. /* Mastodon's unmute is snac's unhide */
  1626. }
  1627. }
  1628. if (out != NULL) {
  1629. *body = xs_json_dumps(out, 4);
  1630. *ctype = "application/json";
  1631. status = 200;
  1632. }
  1633. }
  1634. }
  1635. else
  1636. status = 401;
  1637. }
  1638. else
  1639. if (strcmp(cmd, "/v1/notifications/clear") == 0) { /** **/
  1640. if (logged_in) {
  1641. notify_clear(&snac);
  1642. timeline_touch(&snac);
  1643. *body = xs_dup("{}");
  1644. *ctype = "application/json";
  1645. status = 200;
  1646. }
  1647. else
  1648. status = 401;
  1649. }
  1650. else
  1651. if (strcmp(cmd, "/v1/push/subscription") == 0) { /** **/
  1652. /* I don't know what I'm doing */
  1653. if (logged_in) {
  1654. char *v;
  1655. xs *wpush = xs_dict_new();
  1656. wpush = xs_dict_append(wpush, "id", "1");
  1657. v = xs_dict_get(args, "data");
  1658. v = xs_dict_get(v, "alerts");
  1659. wpush = xs_dict_append(wpush, "alerts", v);
  1660. v = xs_dict_get(args, "subscription");
  1661. v = xs_dict_get(v, "endpoint");
  1662. wpush = xs_dict_append(wpush, "endpoint", v);
  1663. xs *server_key = random_str();
  1664. wpush = xs_dict_append(wpush, "server_key", server_key);
  1665. *body = xs_json_dumps(wpush, 4);
  1666. *ctype = "application/json";
  1667. status = 200;
  1668. }
  1669. else
  1670. status = 401;
  1671. }
  1672. else
  1673. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) { /** **/
  1674. if (logged_in) {
  1675. const xs_list *file = xs_dict_get(args, "file");
  1676. const char *desc = xs_dict_get(args, "description");
  1677. if (xs_is_null(desc))
  1678. desc = "";
  1679. status = 400;
  1680. if (xs_type(file) == XSTYPE_LIST) {
  1681. const char *fn = xs_list_get(file, 0);
  1682. if (*fn != '\0') {
  1683. char *ext = strrchr(fn, '.');
  1684. xs *hash = xs_md5_hex(fn, strlen(fn));
  1685. xs *id = xs_fmt("%s%s", hash, ext);
  1686. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1687. int fo = xs_number_get(xs_list_get(file, 1));
  1688. int fs = xs_number_get(xs_list_get(file, 2));
  1689. /* store */
  1690. static_put(&snac, id, payload + fo, fs);
  1691. static_put_meta(&snac, id, desc);
  1692. /* prepare a response */
  1693. xs *rsp = xs_dict_new();
  1694. rsp = xs_dict_append(rsp, "id", id);
  1695. rsp = xs_dict_append(rsp, "type", "image");
  1696. rsp = xs_dict_append(rsp, "url", url);
  1697. rsp = xs_dict_append(rsp, "preview_url", url);
  1698. rsp = xs_dict_append(rsp, "remote_url", url);
  1699. rsp = xs_dict_append(rsp, "description", desc);
  1700. *body = xs_json_dumps(rsp, 4);
  1701. *ctype = "application/json";
  1702. status = 200;
  1703. }
  1704. }
  1705. }
  1706. else
  1707. status = 401;
  1708. }
  1709. else
  1710. if (xs_startswith(cmd, "/v1/accounts")) { /** **/
  1711. if (logged_in) {
  1712. /* account-related information */
  1713. xs *l = xs_split(cmd, "/");
  1714. const char *md5 = xs_list_get(l, 3);
  1715. const char *opt = xs_list_get(l, 4);
  1716. xs *rsp = NULL;
  1717. if (!xs_is_null(md5) && *md5) {
  1718. xs *actor_o = NULL;
  1719. if (xs_is_null(opt)) {
  1720. /* ? */
  1721. }
  1722. else
  1723. if (strcmp(opt, "follow") == 0) { /** **/
  1724. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1725. const char *actor = xs_dict_get(actor_o, "id");
  1726. xs *msg = msg_follow(&snac, actor);
  1727. if (msg != NULL) {
  1728. /* reload the actor from the message, in may be different */
  1729. actor = xs_dict_get(msg, "object");
  1730. following_add(&snac, actor, msg);
  1731. enqueue_output_by_actor(&snac, msg, actor, 0);
  1732. rsp = mastoapi_relationship(&snac, md5);
  1733. }
  1734. }
  1735. }
  1736. else
  1737. if (strcmp(opt, "unfollow") == 0) { /** **/
  1738. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1739. const char *actor = xs_dict_get(actor_o, "id");
  1740. /* get the following object */
  1741. xs *object = NULL;
  1742. if (valid_status(following_get(&snac, actor, &object))) {
  1743. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1744. following_del(&snac, actor);
  1745. enqueue_output_by_actor(&snac, msg, actor, 0);
  1746. rsp = mastoapi_relationship(&snac, md5);
  1747. }
  1748. }
  1749. }
  1750. else
  1751. if (strcmp(opt, "block") == 0) { /** **/
  1752. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1753. const char *actor = xs_dict_get(actor_o, "id");
  1754. mute(&snac, actor);
  1755. rsp = mastoapi_relationship(&snac, md5);
  1756. }
  1757. }
  1758. else
  1759. if (strcmp(opt, "unblock") == 0) { /** **/
  1760. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1761. const char *actor = xs_dict_get(actor_o, "id");
  1762. unmute(&snac, actor);
  1763. rsp = mastoapi_relationship(&snac, md5);
  1764. }
  1765. }
  1766. }
  1767. if (rsp != NULL) {
  1768. *body = xs_json_dumps(rsp, 4);
  1769. *ctype = "application/json";
  1770. status = 200;
  1771. }
  1772. }
  1773. else
  1774. status = 401;
  1775. }
  1776. else
  1777. if (xs_startswith(cmd, "/v1/polls")) { /** **/
  1778. if (logged_in) {
  1779. /* operations on a status */
  1780. xs *l = xs_split(cmd, "/");
  1781. const char *mid = xs_list_get(l, 3);
  1782. const char *op = xs_list_get(l, 4);
  1783. if (!xs_is_null(mid)) {
  1784. xs *msg = NULL;
  1785. xs *out = NULL;
  1786. /* skip the 'fake' part of the id */
  1787. mid = MID_TO_MD5(mid);
  1788. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1789. const char *id = xs_dict_get(msg, "id");
  1790. const char *atto = xs_dict_get(msg, "attributedTo");
  1791. xs_list *opts = xs_dict_get(msg, "oneOf");
  1792. if (opts == NULL)
  1793. opts = xs_dict_get(msg, "anyOf");
  1794. if (op == NULL) {
  1795. }
  1796. else
  1797. if (strcmp(op, "votes") == 0) {
  1798. xs_list *choices = xs_dict_get(args, "choices[]");
  1799. if (xs_is_null(choices))
  1800. choices = xs_dict_get(args, "choices");
  1801. if (xs_type(choices) == XSTYPE_LIST) {
  1802. xs_str *v;
  1803. while (xs_list_iter(&choices, &v)) {
  1804. int io = atoi(v);
  1805. const xs_dict *o = xs_list_get(opts, io);
  1806. if (o) {
  1807. const char *name = xs_dict_get(o, "name");
  1808. xs *msg = msg_note(&snac, "", atto, (char *)id, NULL, 1);
  1809. msg = xs_dict_append(msg, "name", name);
  1810. xs *c_msg = msg_create(&snac, msg);
  1811. enqueue_message(&snac, c_msg);
  1812. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1813. }
  1814. }
  1815. out = mastoapi_poll(&snac, msg);
  1816. }
  1817. }
  1818. }
  1819. if (out != NULL) {
  1820. *body = xs_json_dumps(out, 4);
  1821. *ctype = "application/json";
  1822. status = 200;
  1823. }
  1824. }
  1825. }
  1826. else
  1827. status = 401;
  1828. }
  1829. /* user cleanup */
  1830. if (logged_in)
  1831. user_free(&snac);
  1832. return status;
  1833. }
  1834. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1835. const char *payload, int p_size,
  1836. char **body, int *b_size, char **ctype)
  1837. {
  1838. (void)p_size;
  1839. (void)b_size;
  1840. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1841. return 0;
  1842. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1843. int status = 404;
  1844. xs *args = NULL;
  1845. char *i_ctype = xs_dict_get(req, "content-type");
  1846. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1847. args = xs_json_loads(payload);
  1848. else
  1849. args = xs_dup(xs_dict_get(req, "p_vars"));
  1850. if (args == NULL)
  1851. return 400;
  1852. xs *cmd = xs_replace_n(q_path, "/api", "", 1);
  1853. snac snac = {0};
  1854. int logged_in = process_auth_token(&snac, req);
  1855. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) { /** **/
  1856. if (logged_in) {
  1857. xs *l = xs_split(cmd, "/");
  1858. const char *stid = xs_list_get(l, 3);
  1859. if (!xs_is_null(stid)) {
  1860. const char *desc = xs_dict_get(args, "description");
  1861. /* set the image metadata */
  1862. static_put_meta(&snac, stid, desc);
  1863. /* prepare a response */
  1864. xs *rsp = xs_dict_new();
  1865. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1866. rsp = xs_dict_append(rsp, "id", stid);
  1867. rsp = xs_dict_append(rsp, "type", "image");
  1868. rsp = xs_dict_append(rsp, "url", url);
  1869. rsp = xs_dict_append(rsp, "preview_url", url);
  1870. rsp = xs_dict_append(rsp, "remote_url", url);
  1871. rsp = xs_dict_append(rsp, "description", desc);
  1872. *body = xs_json_dumps(rsp, 4);
  1873. *ctype = "application/json";
  1874. status = 200;
  1875. }
  1876. }
  1877. else
  1878. status = 401;
  1879. }
  1880. /* user cleanup */
  1881. if (logged_in)
  1882. user_free(&snac);
  1883. return status;
  1884. }
  1885. void mastoapi_purge(void)
  1886. {
  1887. xs *spec = xs_fmt("%s/app/" "*.json", srv_basedir);
  1888. xs *files = xs_glob(spec, 1, 0);
  1889. xs_list *p = files;
  1890. xs_str *v;
  1891. time_t mt = time(NULL) - 3600;
  1892. while (xs_list_iter(&p, &v)) {
  1893. xs *cid = xs_replace(v, ".json", "");
  1894. xs *fn = _app_fn(cid);
  1895. if (mtime(fn) < mt) {
  1896. /* get the app */
  1897. xs *app = app_get(cid);
  1898. if (app) {
  1899. /* old apps with no uid are incomplete cruft */
  1900. const char *uid = xs_dict_get(app, "uid");
  1901. if (xs_is_null(uid) || *uid == '\0') {
  1902. unlink(fn);
  1903. srv_debug(2, xs_fmt("purged %s", fn));
  1904. }
  1905. }
  1906. }
  1907. }
  1908. }
  1909. #endif /* #ifndef NO_MASTODON_API */