mastoapi.c 74 KB

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