mastoapi.c 74 KB

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