mastoapi.c 61 KB

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