mastoapi.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880
  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. return acct;
  373. }
  374. xs_dict *mastoapi_status(snac *snac, const xs_dict *msg)
  375. /* converts an ActivityPub note to a Mastodon status */
  376. {
  377. xs *actor = NULL;
  378. actor_get(snac, xs_dict_get(msg, "attributedTo"), &actor);
  379. /* if the author is not here, discard */
  380. if (actor == NULL)
  381. return NULL;
  382. xs *acct = mastoapi_account(actor);
  383. /** shave the yak converting an ActivityPub Note to a Mastodon status **/
  384. xs *f = xs_val_new(XSTYPE_FALSE);
  385. xs *t = xs_val_new(XSTYPE_TRUE);
  386. xs *n = xs_val_new(XSTYPE_NULL);
  387. xs *el = xs_list_new();
  388. xs *idx = NULL;
  389. xs *ixc = NULL;
  390. char *tmp;
  391. char *id = xs_dict_get(msg, "id");
  392. xs *mid = mastoapi_id(msg);
  393. xs_dict *st = xs_dict_new();
  394. st = xs_dict_append(st, "id", mid);
  395. st = xs_dict_append(st, "uri", id);
  396. st = xs_dict_append(st, "url", id);
  397. st = xs_dict_append(st, "created_at", xs_dict_get(msg, "published"));
  398. st = xs_dict_append(st, "account", acct);
  399. st = xs_dict_append(st, "content", xs_dict_get(msg, "content"));
  400. st = xs_dict_append(st, "visibility",
  401. is_msg_public(snac, msg) ? "public" : "private");
  402. tmp = xs_dict_get(msg, "sensitive");
  403. if (xs_is_null(tmp))
  404. tmp = f;
  405. st = xs_dict_append(st, "sensitive", tmp);
  406. tmp = xs_dict_get(msg, "summary");
  407. if (xs_is_null(tmp))
  408. tmp = "";
  409. st = xs_dict_append(st, "spoiler_text", tmp);
  410. /* create the list of attachments */
  411. xs *matt = xs_list_new();
  412. xs_list *att = xs_dict_get(msg, "attachment");
  413. xs_str *aobj;
  414. while (xs_list_iter(&att, &aobj)) {
  415. const char *mtype = xs_dict_get(aobj, "mediaType");
  416. if (!xs_is_null(mtype) && xs_startswith(mtype, "image/")) {
  417. xs *matteid = xs_fmt("%s_%d", id, xs_list_len(matt));
  418. xs *matte = xs_dict_new();
  419. matte = xs_dict_append(matte, "id", matteid);
  420. matte = xs_dict_append(matte, "type", "image");
  421. matte = xs_dict_append(matte, "url", xs_dict_get(aobj, "url"));
  422. matte = xs_dict_append(matte, "preview_url", xs_dict_get(aobj, "url"));
  423. matte = xs_dict_append(matte, "remote_url", xs_dict_get(aobj, "url"));
  424. const char *name = xs_dict_get(aobj, "name");
  425. if (xs_is_null(name))
  426. name = "";
  427. matte = xs_dict_append(matte, "description", name);
  428. matt = xs_list_append(matt, matte);
  429. }
  430. }
  431. st = xs_dict_append(st, "media_attachments", matt);
  432. {
  433. xs *ml = xs_list_new();
  434. xs *htl = xs_list_new();
  435. xs *eml = xs_list_new();
  436. xs_list *p = xs_dict_get(msg, "tag");
  437. xs_dict *v;
  438. int n = 0;
  439. while (xs_list_iter(&p, &v)) {
  440. const char *type = xs_dict_get(v, "type");
  441. if (xs_is_null(type))
  442. continue;
  443. xs *d1 = xs_dict_new();
  444. if (strcmp(type, "Mention") == 0) {
  445. const char *name = xs_dict_get(v, "name");
  446. const char *href = xs_dict_get(v, "href");
  447. if (!xs_is_null(name) && !xs_is_null(href) &&
  448. strcmp(href, snac->actor) != 0) {
  449. xs *nm = xs_strip_chars_i(xs_dup(name), "@");
  450. xs *id = xs_fmt("%d", n++);
  451. d1 = xs_dict_append(d1, "id", id);
  452. d1 = xs_dict_append(d1, "username", nm);
  453. d1 = xs_dict_append(d1, "acct", nm);
  454. d1 = xs_dict_append(d1, "url", href);
  455. ml = xs_list_append(ml, d1);
  456. }
  457. }
  458. else
  459. if (strcmp(type, "Hashtag") == 0) {
  460. const char *name = xs_dict_get(v, "name");
  461. const char *href = xs_dict_get(v, "href");
  462. if (!xs_is_null(name) && !xs_is_null(href)) {
  463. xs *nm = xs_strip_chars_i(xs_dup(name), "#");
  464. d1 = xs_dict_append(d1, "name", nm);
  465. d1 = xs_dict_append(d1, "url", href);
  466. htl = xs_list_append(htl, d1);
  467. }
  468. }
  469. }
  470. st = xs_dict_append(st, "mentions", ml);
  471. st = xs_dict_append(st, "tags", htl);
  472. st = xs_dict_append(st, "emojis", eml);
  473. }
  474. xs_free(idx);
  475. xs_free(ixc);
  476. idx = object_likes(id);
  477. ixc = xs_number_new(xs_list_len(idx));
  478. st = xs_dict_append(st, "favourites_count", ixc);
  479. st = xs_dict_append(st, "favourited",
  480. xs_list_in(idx, snac->md5) != -1 ? t : f);
  481. xs_free(idx);
  482. xs_free(ixc);
  483. idx = object_announces(id);
  484. ixc = xs_number_new(xs_list_len(idx));
  485. st = xs_dict_append(st, "reblogs_count", ixc);
  486. st = xs_dict_append(st, "reblogged",
  487. xs_list_in(idx, snac->md5) != -1 ? t : f);
  488. xs_free(idx);
  489. xs_free(ixc);
  490. idx = object_children(id);
  491. ixc = xs_number_new(xs_list_len(idx));
  492. st = xs_dict_append(st, "replies_count", ixc);
  493. /* default in_reply_to values */
  494. st = xs_dict_append(st, "in_reply_to_id", n);
  495. st = xs_dict_append(st, "in_reply_to_account_id", n);
  496. tmp = xs_dict_get(msg, "inReplyTo");
  497. if (!xs_is_null(tmp)) {
  498. xs *irto = NULL;
  499. if (valid_status(object_get(tmp, &irto))) {
  500. xs *irt_mid = mastoapi_id(irto);
  501. st = xs_dict_set(st, "in_reply_to_id", irt_mid);
  502. char *at = NULL;
  503. if (!xs_is_null(at = xs_dict_get(irto, "attributedTo"))) {
  504. xs *at_md5 = xs_md5_hex(at, strlen(at));
  505. st = xs_dict_set(st, "in_reply_to_account_id", at_md5);
  506. }
  507. }
  508. }
  509. st = xs_dict_append(st, "reblog", n);
  510. st = xs_dict_append(st, "poll", n);
  511. st = xs_dict_append(st, "card", n);
  512. st = xs_dict_append(st, "language", n);
  513. tmp = xs_dict_get(msg, "sourceContent");
  514. if (xs_is_null(tmp))
  515. tmp = "";
  516. st = xs_dict_append(st, "text", tmp);
  517. tmp = xs_dict_get(msg, "updated");
  518. if (xs_is_null(tmp))
  519. tmp = n;
  520. st = xs_dict_append(st, "edited_at", tmp);
  521. return st;
  522. }
  523. xs_dict *mastoapi_relationship(snac *snac, const char *md5)
  524. {
  525. xs_dict *rel = NULL;
  526. xs *actor_o = NULL;
  527. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  528. xs *t = xs_val_new(XSTYPE_TRUE);
  529. xs *f = xs_val_new(XSTYPE_FALSE);
  530. rel = xs_dict_new();
  531. const char *actor = xs_dict_get(actor_o, "id");
  532. rel = xs_dict_append(rel, "id", md5);
  533. rel = xs_dict_append(rel, "following",
  534. following_check(snac, actor) ? t : f);
  535. rel = xs_dict_append(rel, "showing_reblogs", t);
  536. rel = xs_dict_append(rel, "notifying", f);
  537. rel = xs_dict_append(rel, "followed_by",
  538. follower_check(snac, actor) ? t : f);
  539. rel = xs_dict_append(rel, "blocking",
  540. is_muted(snac, actor) ? t : f);
  541. rel = xs_dict_append(rel, "muting", f);
  542. rel = xs_dict_append(rel, "muting_notifications", f);
  543. rel = xs_dict_append(rel, "requested", f);
  544. rel = xs_dict_append(rel, "domain_blocking", f);
  545. rel = xs_dict_append(rel, "endorsed", f);
  546. rel = xs_dict_append(rel, "note", "");
  547. }
  548. return rel;
  549. }
  550. int process_auth_token(snac *snac, const xs_dict *req)
  551. /* processes an authorization token, if there is one */
  552. {
  553. int logged_in = 0;
  554. char *v;
  555. /* if there is an authorization field, try to validate it */
  556. if (!xs_is_null(v = xs_dict_get(req, "authorization")) && xs_startswith(v, "Bearer ")) {
  557. xs *tokid = xs_replace(v, "Bearer ", "");
  558. xs *token = token_get(tokid);
  559. if (token != NULL) {
  560. const char *uid = xs_dict_get(token, "uid");
  561. if (!xs_is_null(uid) && user_open(snac, uid)) {
  562. logged_in = 1;
  563. srv_debug(2, xs_fmt("mastoapi auth: valid token for user %s", uid));
  564. }
  565. else
  566. srv_log(xs_fmt("mastoapi auth: corrupted token %s", tokid));
  567. }
  568. else
  569. srv_log(xs_fmt("mastoapi auth: invalid token %s", tokid));
  570. }
  571. return logged_in;
  572. }
  573. int mastoapi_get_handler(const xs_dict *req, const char *q_path,
  574. char **body, int *b_size, char **ctype)
  575. {
  576. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  577. return 0;
  578. srv_debug(1, xs_fmt("mastoapi_get_handler %s", q_path));
  579. /* {
  580. xs *j = xs_json_dumps_pp(req, 4);
  581. printf("mastoapi get:\n%s\n", j);
  582. }*/
  583. int status = 404;
  584. xs_dict *args = xs_dict_get(req, "q_vars");
  585. xs *cmd = xs_replace(q_path, "/api", "");
  586. snac snac1 = {0};
  587. int logged_in = process_auth_token(&snac1, req);
  588. if (strcmp(cmd, "/v1/accounts/verify_credentials") == 0) {
  589. if (logged_in) {
  590. xs *acct = xs_dict_new();
  591. acct = xs_dict_append(acct, "id", xs_dict_get(snac1.config, "uid"));
  592. acct = xs_dict_append(acct, "username", xs_dict_get(snac1.config, "uid"));
  593. acct = xs_dict_append(acct, "acct", xs_dict_get(snac1.config, "uid"));
  594. acct = xs_dict_append(acct, "display_name", xs_dict_get(snac1.config, "name"));
  595. acct = xs_dict_append(acct, "created_at", xs_dict_get(snac1.config, "published"));
  596. acct = xs_dict_append(acct, "note", xs_dict_get(snac1.config, "bio"));
  597. acct = xs_dict_append(acct, "url", snac1.actor);
  598. acct = xs_dict_append(acct, "header", "");
  599. xs *avatar = NULL;
  600. char *av = xs_dict_get(snac1.config, "avatar");
  601. if (xs_is_null(av) || *av == '\0')
  602. avatar = xs_fmt("%s/susie.png", srv_baseurl);
  603. else
  604. avatar = xs_dup(av);
  605. acct = xs_dict_append(acct, "avatar", avatar);
  606. *body = xs_json_dumps_pp(acct, 4);
  607. *ctype = "application/json";
  608. status = 200;
  609. }
  610. else {
  611. status = 422; // "Unprocessable entity" (no login)
  612. }
  613. }
  614. else
  615. if (strcmp(cmd, "/v1/accounts/relationships") == 0) {
  616. /* find if an account is followed, blocked, etc. */
  617. /* the account to get relationships about is in args "id[]" */
  618. if (logged_in) {
  619. xs *res = xs_list_new();
  620. const char *md5 = xs_dict_get(args, "id[]");
  621. if (!xs_is_null(md5)) {
  622. xs *rel = mastoapi_relationship(&snac1, md5);
  623. if (rel != NULL)
  624. res = xs_list_append(res, rel);
  625. }
  626. *body = xs_json_dumps_pp(res, 4);
  627. *ctype = "application/json";
  628. status = 200;
  629. }
  630. else
  631. status = 422;
  632. }
  633. else
  634. if (xs_startswith(cmd, "/v1/accounts/")) {
  635. /* account-related information */
  636. xs *l = xs_split(cmd, "/");
  637. const char *uid = xs_list_get(l, 3);
  638. const char *opt = xs_list_get(l, 4);
  639. if (uid != NULL) {
  640. snac snac2;
  641. xs *out = NULL;
  642. xs *actor = NULL;
  643. /* is it a local user? */
  644. if (user_open(&snac2, uid) || user_open_by_md5(&snac2, uid)) {
  645. if (opt == NULL) {
  646. /* account information */
  647. actor = msg_actor(&snac2);
  648. out = mastoapi_account(actor);
  649. }
  650. else
  651. if (strcmp(opt, "statuses") == 0) {
  652. /* the public list of posts of a user */
  653. xs *timeline = timeline_simple_list(&snac2, "public", 0, 256);
  654. xs_list *p = timeline;
  655. xs_str *v;
  656. out = xs_list_new();
  657. while (xs_list_iter(&p, &v)) {
  658. xs *msg = NULL;
  659. if (valid_status(timeline_get_by_md5(&snac2, v, &msg))) {
  660. /* add only posts by the author */
  661. if (strcmp(xs_dict_get(msg, "type"), "Note") == 0 &&
  662. xs_startswith(xs_dict_get(msg, "id"), snac2.actor)) {
  663. xs *st = mastoapi_status(&snac2, msg);
  664. out = xs_list_append(out, st);
  665. }
  666. }
  667. }
  668. }
  669. user_free(&snac2);
  670. }
  671. else {
  672. /* try the uid as the md5 of a possibly loaded actor */
  673. if (logged_in && valid_status(object_get_by_md5(uid, &actor))) {
  674. if (opt == NULL) {
  675. /* account information */
  676. out = mastoapi_account(actor);
  677. }
  678. else
  679. if (strcmp(opt, "statuses") == 0) {
  680. /* we don't serve statuses of others; return the empty list */
  681. out = xs_list_new();
  682. }
  683. }
  684. }
  685. if (out != NULL) {
  686. *body = xs_json_dumps_pp(out, 4);
  687. *ctype = "application/json";
  688. status = 200;
  689. }
  690. }
  691. }
  692. else
  693. if (strcmp(cmd, "/v1/timelines/home") == 0) {
  694. /* the private timeline */
  695. if (logged_in) {
  696. const char *max_id = xs_dict_get(args, "max_id");
  697. const char *since_id = xs_dict_get(args, "since_id");
  698. const char *min_id = xs_dict_get(args, "min_id");
  699. const char *limit_s = xs_dict_get(args, "limit");
  700. int limit = 0;
  701. int cnt = 0;
  702. if (!xs_is_null(limit_s))
  703. limit = atoi(limit_s);
  704. if (limit == 0)
  705. limit = 20;
  706. xs *timeline = timeline_simple_list(&snac1, "private", 0, 256);
  707. xs *out = xs_list_new();
  708. xs_list *p = timeline;
  709. xs_str *v;
  710. while (xs_list_iter(&p, &v) && cnt < limit) {
  711. xs *msg = NULL;
  712. /* only return entries older that max_id */
  713. if (max_id) {
  714. if (strcmp(v, max_id) == 0)
  715. max_id = NULL;
  716. continue;
  717. }
  718. /* only returns entries newer than since_id */
  719. if (since_id) {
  720. if (strcmp(v, since_id) == 0)
  721. break;
  722. }
  723. /* only returns entries newer than min_id */
  724. /* what does really "Return results immediately newer than ID" mean? */
  725. if (min_id) {
  726. if (strcmp(v, min_id) == 0)
  727. break;
  728. }
  729. /* get the entry */
  730. if (!valid_status(timeline_get_by_md5(&snac1, v, &msg)))
  731. continue;
  732. /* discard non-Notes */
  733. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  734. continue;
  735. /* convert the Note into a Mastodon status */
  736. xs *st = mastoapi_status(&snac1, msg);
  737. if (st != NULL)
  738. out = xs_list_append(out, st);
  739. cnt++;
  740. }
  741. *body = xs_json_dumps_pp(out, 4);
  742. *ctype = "application/json";
  743. status = 200;
  744. srv_debug(2, xs_fmt("mastoapi timeline: returned %d entries", xs_list_len(out)));
  745. }
  746. else {
  747. status = 401; // unauthorized
  748. }
  749. }
  750. else
  751. if (strcmp(cmd, "/v1/timelines/public") == 0) {
  752. /* the public timeline (public timelines for all users) */
  753. /* this is an ugly kludge: first users in the list get all the fame */
  754. const char *limit_s = xs_dict_get(args, "limit");
  755. int limit = 0;
  756. int cnt = 0;
  757. if (!xs_is_null(limit_s))
  758. limit = atoi(limit_s);
  759. if (limit == 0)
  760. limit = 20;
  761. xs *out = xs_list_new();
  762. xs *users = user_list();
  763. xs_list *p = users;
  764. xs_str *uid;
  765. while (xs_list_iter(&p, &uid) && cnt < limit) {
  766. snac user;
  767. if (user_open(&user, uid)) {
  768. xs *timeline = timeline_simple_list(&user, "public", 0, 4);
  769. xs_list *p2 = timeline;
  770. xs_str *v;
  771. while (xs_list_iter(&p2, &v) && cnt < limit) {
  772. xs *msg = NULL;
  773. /* get the entry */
  774. if (!valid_status(timeline_get_by_md5(&user, v, &msg)))
  775. continue;
  776. /* discard non-Notes */
  777. if (strcmp(xs_dict_get(msg, "type"), "Note") != 0)
  778. continue;
  779. /* discard entries not by this user */
  780. if (!xs_startswith(xs_dict_get(msg, "id"), user.actor))
  781. continue;
  782. /* convert the Note into a Mastodon status */
  783. xs *st = mastoapi_status(&user, msg);
  784. if (st != NULL) {
  785. out = xs_list_append(out, st);
  786. cnt++;
  787. }
  788. }
  789. user_free(&user);
  790. }
  791. }
  792. *body = xs_json_dumps_pp(out, 4);
  793. *ctype = "application/json";
  794. status = 200;
  795. }
  796. else
  797. if (strcmp(cmd, "/v1/conversations") == 0) {
  798. /* TBD */
  799. *body = xs_dup("[]");
  800. *ctype = "application/json";
  801. status = 200;
  802. }
  803. else
  804. if (strcmp(cmd, "/v1/notifications") == 0) {
  805. if (logged_in) {
  806. xs *l = notify_list(&snac1, 0);
  807. xs *out = xs_list_new();
  808. xs_list *p = l;
  809. xs_dict *v;
  810. while (xs_list_iter(&p, &v)) {
  811. xs *noti = notify_get(&snac1, v);
  812. if (noti == NULL)
  813. continue;
  814. const char *type = xs_dict_get(noti, "type");
  815. const char *objid = xs_dict_get(noti, "objid");
  816. xs *actor = NULL;
  817. xs *entry = NULL;
  818. if (!valid_status(object_get(xs_dict_get(noti, "actor"), &actor)))
  819. continue;
  820. if (objid != NULL && !valid_status(object_get(objid, &entry)))
  821. continue;
  822. if (is_hidden(&snac1, objid))
  823. continue;
  824. /* convert the type */
  825. if (strcmp(type, "Like") == 0)
  826. type = "favourite";
  827. else
  828. if (strcmp(type, "Announce") == 0)
  829. type = "reblog";
  830. else
  831. if (strcmp(type, "Follow") == 0)
  832. type = "follow";
  833. else
  834. if (strcmp(type, "Create") == 0)
  835. type = "mention";
  836. else
  837. continue;
  838. xs *mn = xs_dict_new();
  839. mn = xs_dict_append(mn, "type", type);
  840. xs *id = xs_replace(xs_dict_get(noti, "id"), ".", "");
  841. mn = xs_dict_append(mn, "id", id);
  842. mn = xs_dict_append(mn, "created_at", xs_dict_get(noti, "date"));
  843. xs *acct = mastoapi_account(actor);
  844. mn = xs_dict_append(mn, "account", acct);
  845. if (strcmp(type, "follow") != 0 && !xs_is_null(objid)) {
  846. xs *st = mastoapi_status(&snac1, entry);
  847. mn = xs_dict_append(mn, "status", st);
  848. }
  849. out = xs_list_append(out, mn);
  850. }
  851. *body = xs_json_dumps_pp(out, 4);
  852. *ctype = "application/json";
  853. status = 200;
  854. }
  855. else
  856. status = 401;
  857. }
  858. else
  859. if (strcmp(cmd, "/v1/filters") == 0) {
  860. /* snac will never have filters */
  861. *body = xs_dup("[]");
  862. *ctype = "application/json";
  863. status = 200;
  864. }
  865. else
  866. if (strcmp(cmd, "/v1/favourites") == 0) {
  867. /* snac will never support a list of favourites */
  868. *body = xs_dup("[]");
  869. *ctype = "application/json";
  870. status = 200;
  871. }
  872. else
  873. if (strcmp(cmd, "/v1/bookmarks") == 0) {
  874. /* snac does not support bookmarks */
  875. *body = xs_dup("[]");
  876. *ctype = "application/json";
  877. status = 200;
  878. }
  879. else
  880. if (strcmp(cmd, "/v1/lists") == 0) {
  881. /* snac does not support lists */
  882. *body = xs_dup("[]");
  883. *ctype = "application/json";
  884. status = 200;
  885. }
  886. else
  887. if (strcmp(cmd, "/v1/scheduled_statuses") == 0) {
  888. /* snac does not scheduled notes */
  889. *body = xs_dup("[]");
  890. *ctype = "application/json";
  891. status = 200;
  892. }
  893. else
  894. if (strcmp(cmd, "/v1/follow_requests") == 0) {
  895. /* snac does not support optional follow confirmations */
  896. *body = xs_dup("[]");
  897. *ctype = "application/json";
  898. status = 200;
  899. }
  900. else
  901. if (strcmp(cmd, "/v1/announcements") == 0) {
  902. /* snac has no announcements (yet?) */
  903. *body = xs_dup("[]");
  904. *ctype = "application/json";
  905. status = 200;
  906. }
  907. else
  908. if (strcmp(cmd, "/v1/custom_emojis") == 0) {
  909. /* are you kidding me? */
  910. *body = xs_dup("[]");
  911. *ctype = "application/json";
  912. status = 200;
  913. }
  914. else
  915. if (strcmp(cmd, "/v1/instance") == 0) {
  916. /* returns an instance object */
  917. xs *ins = xs_dict_new();
  918. const char *host = xs_dict_get(srv_config, "host");
  919. ins = xs_dict_append(ins, "uri", host);
  920. ins = xs_dict_append(ins, "domain", host);
  921. ins = xs_dict_append(ins, "title", host);
  922. ins = xs_dict_append(ins, "version", "4.0.0 (not true; really " USER_AGENT ")");
  923. ins = xs_dict_append(ins, "source_url", "https:/" "/comam.es/what-is-snac");
  924. ins = xs_dict_append(ins, "description", host);
  925. ins = xs_dict_append(ins, "short_description", host);
  926. xs *susie = xs_fmt("%s/susie.png", srv_baseurl);
  927. ins = xs_dict_append(ins, "thumbnail", susie);
  928. const char *v = xs_dict_get(srv_config, "admin_email");
  929. if (xs_is_null(v) || *v == '\0')
  930. v = "admin@localhost";
  931. ins = xs_dict_append(ins, "email", v);
  932. xs *l1 = xs_list_new();
  933. ins = xs_dict_append(ins, "rules", l1);
  934. l1 = xs_list_append(l1, "en");
  935. ins = xs_dict_append(ins, "languages", l1);
  936. xs *d1 = xs_dict_new();
  937. ins = xs_dict_append(ins, "urls", d1);
  938. xs *z = xs_number_new(0);
  939. d1 = xs_dict_append(d1, "user_count", z);
  940. d1 = xs_dict_append(d1, "status_count", z);
  941. d1 = xs_dict_append(d1, "domain_count", z);
  942. ins = xs_dict_append(ins, "stats", d1);
  943. xs *f = xs_val_new(XSTYPE_FALSE);
  944. ins = xs_dict_append(ins, "registrations", f);
  945. ins = xs_dict_append(ins, "approval_required", f);
  946. ins = xs_dict_append(ins, "invites_enabled", f);
  947. xs *cfg = xs_dict_new();
  948. {
  949. xs *d11 = xs_dict_new();
  950. xs *mc = xs_number_new(100000);
  951. xs *mm = xs_number_new(8);
  952. xs *cr = xs_number_new(32);
  953. d11 = xs_dict_append(d11, "max_characters", mc);
  954. d11 = xs_dict_append(d11, "max_media_attachments", mm);
  955. d11 = xs_dict_append(d11, "characters_reserved_per_url", cr);
  956. cfg = xs_dict_append(cfg, "statuses", d11);
  957. }
  958. {
  959. xs *d11 = xs_dict_new();
  960. xs *mt = xs_list_new();
  961. mt = xs_list_append(mt, "image/jpeg");
  962. mt = xs_list_append(mt, "image/png");
  963. mt = xs_list_append(mt, "image/gif");
  964. d11 = xs_dict_append(d11, "supported_mime_types", mt);
  965. d11 = xs_dict_append(d11, "image_size_limit", z);
  966. d11 = xs_dict_append(d11, "image_matrix_limit", z);
  967. d11 = xs_dict_append(d11, "video_size_limit", z);
  968. d11 = xs_dict_append(d11, "video_matrix_limit", z);
  969. d11 = xs_dict_append(d11, "video_frame_rate_limit", z);
  970. cfg = xs_dict_append(cfg, "media_attachments", d11);
  971. }
  972. ins = xs_dict_append(ins, "configuration", cfg);
  973. *body = xs_json_dumps_pp(ins, 4);
  974. *ctype = "application/json";
  975. status = 200;
  976. }
  977. else
  978. if (xs_startswith(cmd, "/v1/statuses/")) {
  979. /* operations on a status */
  980. xs *l = xs_split(cmd, "/");
  981. const char *id = xs_list_get(l, 3);
  982. const char *op = xs_list_get(l, 4);
  983. if (!xs_is_null(id)) {
  984. xs *msg = NULL;
  985. xs *out = NULL;
  986. /* skip the 'fake' part of the id */
  987. id = MID_TO_MD5(id);
  988. if (valid_status(timeline_get_by_md5(&snac1, id, &msg))) {
  989. if (op == NULL) {
  990. /* return the status itself */
  991. out = mastoapi_status(&snac1, msg);
  992. }
  993. else
  994. if (strcmp(op, "context") == 0) {
  995. /* return ancestors and children */
  996. xs *anc = xs_list_new();
  997. xs *des = xs_list_new();
  998. xs_list *p;
  999. xs_str *v;
  1000. char pid[64];
  1001. /* build the [grand]parent list, moving up */
  1002. strncpy(pid, id, sizeof(pid));
  1003. while (object_parent(pid, pid, sizeof(pid))) {
  1004. xs *m2 = NULL;
  1005. if (valid_status(timeline_get_by_md5(&snac1, pid, &m2))) {
  1006. xs *st = mastoapi_status(&snac1, m2);
  1007. anc = xs_list_insert(anc, 0, st);
  1008. }
  1009. else
  1010. break;
  1011. }
  1012. /* build the children list */
  1013. xs *children = object_children(xs_dict_get(msg, "id"));
  1014. p = children;
  1015. while (xs_list_iter(&p, &v)) {
  1016. xs *m2 = NULL;
  1017. if (valid_status(timeline_get_by_md5(&snac1, v, &m2))) {
  1018. xs *st = mastoapi_status(&snac1, m2);
  1019. des = xs_list_append(des, st);
  1020. }
  1021. }
  1022. out = xs_dict_new();
  1023. out = xs_dict_append(out, "ancestors", anc);
  1024. out = xs_dict_append(out, "descendants", des);
  1025. }
  1026. else
  1027. if (strcmp(op, "reblogged_by") == 0 ||
  1028. strcmp(op, "favourited_by") == 0) {
  1029. /* return the list of people who liked or boosted this */
  1030. out = xs_list_new();
  1031. xs *l = NULL;
  1032. if (op[0] == 'r')
  1033. l = object_announces(xs_dict_get(msg, "id"));
  1034. else
  1035. l = object_likes(xs_dict_get(msg, "id"));
  1036. xs_list *p = l;
  1037. xs_str *v;
  1038. while (xs_list_iter(&p, &v)) {
  1039. xs *actor2 = NULL;
  1040. if (valid_status(object_get_by_md5(v, &actor2))) {
  1041. xs *acct2 = mastoapi_account(actor2);
  1042. out = xs_list_append(out, acct2);
  1043. }
  1044. }
  1045. }
  1046. }
  1047. else
  1048. srv_debug(1, xs_fmt("mastoapi status: bad id %s", id));
  1049. if (out != NULL) {
  1050. *body = xs_json_dumps_pp(out, 4);
  1051. *ctype = "application/json";
  1052. status = 200;
  1053. }
  1054. }
  1055. }
  1056. else
  1057. if (strcmp(cmd, "/v1/filters") == 0) {
  1058. *body = xs_dup("[]");
  1059. *ctype = "application/json";
  1060. status = 200;
  1061. }
  1062. else
  1063. if (strcmp(cmd, "/v1/preferences") == 0) {
  1064. *body = xs_dup("{}");
  1065. *ctype = "application/json";
  1066. status = 200;
  1067. }
  1068. else
  1069. if (strcmp(cmd, "/v1/markers") == 0) {
  1070. *body = xs_dup("{}");
  1071. *ctype = "application/json";
  1072. status = 200;
  1073. }
  1074. else
  1075. if (strcmp(cmd, "/v1/followed_tags") == 0) {
  1076. *body = xs_dup("[]");
  1077. *ctype = "application/json";
  1078. status = 200;
  1079. }
  1080. else
  1081. if (strcmp(cmd, "/v2/search") == 0) {
  1082. const char *q = xs_dict_get(args, "q");
  1083. const char *type = xs_dict_get(args, "type");
  1084. const char *offset = xs_dict_get(args, "offset");
  1085. xs *acl = xs_list_new();
  1086. xs *stl = xs_list_new();
  1087. xs *htl = xs_list_new();
  1088. xs *res = xs_dict_new();
  1089. if (xs_is_null(offset) || strcmp(offset, "0") == 0) {
  1090. /* reply something only for offset 0; otherwise,
  1091. apps like Tusky keep asking again and again */
  1092. if (!xs_is_null(q) && strcmp(type, "accounts") == 0) {
  1093. /* do a webfinger query */
  1094. char *actor = NULL;
  1095. char *user = NULL;
  1096. if (valid_status(webfinger_request(q, &actor, &user))) {
  1097. xs *actor_o = NULL;
  1098. if (valid_status(actor_request(&snac1, actor, &actor_o))) {
  1099. xs *acct = mastoapi_account(actor_o);
  1100. acl = xs_list_append(acl, acct);
  1101. }
  1102. }
  1103. }
  1104. }
  1105. res = xs_dict_append(res, "accounts", acl);
  1106. res = xs_dict_append(res, "statuses", stl);
  1107. res = xs_dict_append(res, "hashtags", htl);
  1108. *body = xs_json_dumps_pp(res, 4);
  1109. *ctype = "application/json";
  1110. status = 200;
  1111. }
  1112. /* user cleanup */
  1113. if (logged_in)
  1114. user_free(&snac1);
  1115. return status;
  1116. }
  1117. int mastoapi_post_handler(const xs_dict *req, const char *q_path,
  1118. const char *payload, int p_size,
  1119. char **body, int *b_size, char **ctype)
  1120. {
  1121. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1122. return 0;
  1123. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1124. /* {
  1125. xs *j = xs_json_dumps_pp(req, 4);
  1126. printf("mastoapi post:\n%s\n", j);
  1127. }*/
  1128. int status = 404;
  1129. xs *args = NULL;
  1130. char *i_ctype = xs_dict_get(req, "content-type");
  1131. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1132. args = xs_json_loads(payload);
  1133. else
  1134. args = xs_dup(xs_dict_get(req, "p_vars"));
  1135. if (args == NULL)
  1136. return 400;
  1137. /* {
  1138. xs *j = xs_json_dumps_pp(args, 4);
  1139. printf("%s\n", j);
  1140. }*/
  1141. xs *cmd = xs_replace(q_path, "/api", "");
  1142. snac snac = {0};
  1143. int logged_in = process_auth_token(&snac, req);
  1144. if (strcmp(cmd, "/v1/apps") == 0) {
  1145. const char *name = xs_dict_get(args, "client_name");
  1146. const char *ruri = xs_dict_get(args, "redirect_uris");
  1147. const char *scope = xs_dict_get(args, "scope");
  1148. if (xs_type(ruri) == XSTYPE_LIST)
  1149. ruri = xs_dict_get(ruri, 0);
  1150. if (name && ruri) {
  1151. xs *app = xs_dict_new();
  1152. xs *id = xs_replace_i(tid(0), ".", "");
  1153. xs *cid = random_str();
  1154. xs *csec = random_str();
  1155. xs *vkey = random_str();
  1156. app = xs_dict_append(app, "name", name);
  1157. app = xs_dict_append(app, "redirect_uri", ruri);
  1158. app = xs_dict_append(app, "client_id", cid);
  1159. app = xs_dict_append(app, "client_secret", csec);
  1160. app = xs_dict_append(app, "vapid_key", vkey);
  1161. app = xs_dict_append(app, "id", id);
  1162. *body = xs_json_dumps_pp(app, 4);
  1163. *ctype = "application/json";
  1164. status = 200;
  1165. app = xs_dict_append(app, "code", "");
  1166. if (scope)
  1167. app = xs_dict_append(app, "scope", scope);
  1168. app_add(cid, app);
  1169. srv_debug(0, xs_fmt("mastoapi apps: new app %s", cid));
  1170. }
  1171. }
  1172. else
  1173. if (strcmp(cmd, "/v1/statuses") == 0) {
  1174. if (logged_in) {
  1175. /* post a new Note */
  1176. /* {
  1177. xs *j = xs_json_dumps_pp(args, 4);
  1178. printf("%s\n", j);
  1179. }*/
  1180. const char *content = xs_dict_get(args, "status");
  1181. const char *mid = xs_dict_get(args, "in_reply_to_id");
  1182. const char *visibility = xs_dict_get(args, "visibility");
  1183. const char *summary = xs_dict_get(args, "spoiler_text");
  1184. const char *media_ids = xs_dict_get(args, "media_ids");
  1185. if (xs_is_null(media_ids))
  1186. media_ids = xs_dict_get(args, "media_ids[]");
  1187. xs *attach_list = xs_list_new();
  1188. xs *irt = NULL;
  1189. /* is it a reply? */
  1190. if (mid != NULL) {
  1191. xs *r_msg = NULL;
  1192. const char *md5 = MID_TO_MD5(mid);
  1193. if (valid_status(object_get_by_md5(md5, &r_msg)))
  1194. irt = xs_dup(xs_dict_get(r_msg, "id"));
  1195. }
  1196. /* does it have attachments? */
  1197. if (!xs_is_null(media_ids)) {
  1198. xs *mi = NULL;
  1199. if (xs_type(media_ids) == XSTYPE_LIST)
  1200. mi = xs_dup(media_ids);
  1201. else {
  1202. mi = xs_list_new();
  1203. mi = xs_list_append(mi, media_ids);
  1204. }
  1205. xs_list *p = mi;
  1206. xs_str *v;
  1207. while (xs_list_iter(&p, &v)) {
  1208. xs *l = xs_list_new();
  1209. xs *url = xs_fmt("%s/s/%s", snac.actor, v);
  1210. xs *desc = static_get_meta(&snac, v);
  1211. l = xs_list_append(l, url);
  1212. l = xs_list_append(l, desc);
  1213. attach_list = xs_list_append(attach_list, l);
  1214. }
  1215. }
  1216. /* prepare the message */
  1217. xs *msg = msg_note(&snac, content, NULL, irt, attach_list,
  1218. strcmp(visibility, "public") == 0 ? 0 : 1);
  1219. if (!xs_is_null(summary) && *summary) {
  1220. xs *t = xs_val_new(XSTYPE_TRUE);
  1221. msg = xs_dict_set(msg, "sensitive", t);
  1222. msg = xs_dict_set(msg, "summary", summary);
  1223. }
  1224. /* store */
  1225. timeline_add(&snac, xs_dict_get(msg, "id"), msg);
  1226. /* 'Create' message */
  1227. xs *c_msg = msg_create(&snac, msg);
  1228. enqueue_message(&snac, c_msg);
  1229. timeline_touch(&snac);
  1230. /* convert to a mastodon status as a response code */
  1231. xs *st = mastoapi_status(&snac, msg);
  1232. *body = xs_json_dumps_pp(st, 4);
  1233. *ctype = "application/json";
  1234. status = 200;
  1235. }
  1236. else
  1237. status = 401;
  1238. }
  1239. else
  1240. if (xs_startswith(cmd, "/v1/statuses")) {
  1241. if (logged_in) {
  1242. /* operations on a status */
  1243. xs *l = xs_split(cmd, "/");
  1244. const char *mid = xs_list_get(l, 3);
  1245. const char *op = xs_list_get(l, 4);
  1246. if (!xs_is_null(mid)) {
  1247. xs *msg = NULL;
  1248. xs *out = NULL;
  1249. /* skip the 'fake' part of the id */
  1250. mid = MID_TO_MD5(mid);
  1251. if (valid_status(timeline_get_by_md5(&snac, mid, &msg))) {
  1252. char *id = xs_dict_get(msg, "id");
  1253. if (op == NULL) {
  1254. /* no operation (?) */
  1255. }
  1256. else
  1257. if (strcmp(op, "favourite") == 0) {
  1258. xs *n_msg = msg_admiration(&snac, id, "Like");
  1259. if (n_msg != NULL) {
  1260. enqueue_message(&snac, n_msg);
  1261. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 1);
  1262. out = mastoapi_status(&snac, msg);
  1263. }
  1264. }
  1265. else
  1266. if (strcmp(op, "unfavourite") == 0) {
  1267. /* snac does not support Undo+Like */
  1268. }
  1269. else
  1270. if (strcmp(op, "reblog") == 0) {
  1271. xs *n_msg = msg_admiration(&snac, id, "Announce");
  1272. if (n_msg != NULL) {
  1273. enqueue_message(&snac, n_msg);
  1274. timeline_admire(&snac, xs_dict_get(n_msg, "object"), snac.actor, 0);
  1275. out = mastoapi_status(&snac, msg);
  1276. }
  1277. }
  1278. else
  1279. if (strcmp(op, "unreblog") == 0) {
  1280. /* snac does not support Undo+Announce */
  1281. }
  1282. else
  1283. if (strcmp(op, "bookmark") == 0) {
  1284. /* snac does not support bookmarks */
  1285. }
  1286. else
  1287. if (strcmp(op, "unbookmark") == 0) {
  1288. /* snac does not support bookmarks */
  1289. }
  1290. else
  1291. if (strcmp(op, "pin") == 0) {
  1292. /* snac does not support pinning */
  1293. }
  1294. else
  1295. if (strcmp(op, "unpin") == 0) {
  1296. /* snac does not support pinning */
  1297. }
  1298. else
  1299. if (strcmp(op, "mute") == 0) {
  1300. /* Mastodon's mute is snac's hide */
  1301. }
  1302. else
  1303. if (strcmp(op, "unmute") == 0) {
  1304. /* Mastodon's unmute is snac's unhide */
  1305. }
  1306. }
  1307. if (out != NULL) {
  1308. *body = xs_json_dumps_pp(out, 4);
  1309. *ctype = "application/json";
  1310. status = 200;
  1311. }
  1312. }
  1313. }
  1314. else
  1315. status = 401;
  1316. }
  1317. else
  1318. if (strcmp(cmd, "/v1/notifications/clear") == 0) {
  1319. if (logged_in) {
  1320. notify_clear(&snac);
  1321. timeline_touch(&snac);
  1322. *body = xs_dup("{}");
  1323. *ctype = "application/json";
  1324. status = 200;
  1325. }
  1326. else
  1327. status = 401;
  1328. }
  1329. else
  1330. if (strcmp(cmd, "/v1/push/subscription") == 0) {
  1331. /* I don't know what I'm doing */
  1332. if (logged_in) {
  1333. char *v;
  1334. xs *wpush = xs_dict_new();
  1335. wpush = xs_dict_append(wpush, "id", "1");
  1336. v = xs_dict_get(args, "data");
  1337. v = xs_dict_get(v, "alerts");
  1338. wpush = xs_dict_append(wpush, "alerts", v);
  1339. v = xs_dict_get(args, "subscription");
  1340. v = xs_dict_get(v, "endpoint");
  1341. wpush = xs_dict_append(wpush, "endpoint", v);
  1342. xs *server_key = random_str();
  1343. wpush = xs_dict_append(wpush, "server_key", server_key);
  1344. *body = xs_json_dumps_pp(wpush, 4);
  1345. *ctype = "application/json";
  1346. status = 200;
  1347. }
  1348. else
  1349. status = 401;
  1350. }
  1351. else
  1352. if (strcmp(cmd, "/v1/media") == 0 || strcmp(cmd, "/v2/media") == 0) {
  1353. if (logged_in) {
  1354. /* {
  1355. xs *j = xs_json_dumps_pp(args, 4);
  1356. printf("%s\n", j);
  1357. }*/
  1358. const xs_list *file = xs_dict_get(args, "file");
  1359. const char *desc = xs_dict_get(args, "description");
  1360. if (xs_is_null(desc))
  1361. desc = "";
  1362. status = 400;
  1363. if (xs_type(file) == XSTYPE_LIST) {
  1364. const char *fn = xs_list_get(file, 0);
  1365. if (*fn != '\0') {
  1366. char *ext = strrchr(fn, '.');
  1367. xs *hash = xs_md5_hex(fn, strlen(fn));
  1368. xs *id = xs_fmt("%s%s", hash, ext);
  1369. xs *url = xs_fmt("%s/s/%s", snac.actor, id);
  1370. int fo = xs_number_get(xs_list_get(file, 1));
  1371. int fs = xs_number_get(xs_list_get(file, 2));
  1372. /* store */
  1373. static_put(&snac, id, payload + fo, fs);
  1374. static_put_meta(&snac, id, desc);
  1375. /* prepare a response */
  1376. xs *rsp = xs_dict_new();
  1377. rsp = xs_dict_append(rsp, "id", id);
  1378. rsp = xs_dict_append(rsp, "type", "image");
  1379. rsp = xs_dict_append(rsp, "url", url);
  1380. rsp = xs_dict_append(rsp, "preview_url", url);
  1381. rsp = xs_dict_append(rsp, "remote_url", url);
  1382. rsp = xs_dict_append(rsp, "description", desc);
  1383. *body = xs_json_dumps_pp(rsp, 4);
  1384. *ctype = "application/json";
  1385. status = 200;
  1386. }
  1387. }
  1388. }
  1389. else
  1390. status = 401;
  1391. }
  1392. else
  1393. if (xs_startswith(cmd, "/v1/accounts")) {
  1394. if (logged_in) {
  1395. /* account-related information */
  1396. xs *l = xs_split(cmd, "/");
  1397. const char *md5 = xs_list_get(l, 3);
  1398. const char *opt = xs_list_get(l, 4);
  1399. xs *rsp = NULL;
  1400. if (!xs_is_null(md5) && *md5) {
  1401. xs *actor_o = NULL;
  1402. if (xs_is_null(opt)) {
  1403. /* ? */
  1404. }
  1405. else
  1406. if (strcmp(opt, "follow") == 0) {
  1407. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1408. const char *actor = xs_dict_get(actor_o, "id");
  1409. xs *msg = msg_follow(&snac, actor);
  1410. if (msg != NULL) {
  1411. /* reload the actor from the message, in may be different */
  1412. actor = xs_dict_get(msg, "object");
  1413. following_add(&snac, actor, msg);
  1414. enqueue_output_by_actor(&snac, msg, actor, 0);
  1415. rsp = mastoapi_relationship(&snac, md5);
  1416. }
  1417. }
  1418. }
  1419. else
  1420. if (strcmp(opt, "unfollow") == 0) {
  1421. if (valid_status(object_get_by_md5(md5, &actor_o))) {
  1422. const char *actor = xs_dict_get(actor_o, "id");
  1423. /* get the following object */
  1424. xs *object = NULL;
  1425. if (valid_status(following_get(&snac, actor, &object))) {
  1426. xs *msg = msg_undo(&snac, xs_dict_get(object, "object"));
  1427. following_del(&snac, actor);
  1428. enqueue_output_by_actor(&snac, msg, actor, 0);
  1429. rsp = mastoapi_relationship(&snac, md5);
  1430. }
  1431. }
  1432. }
  1433. }
  1434. if (rsp != NULL) {
  1435. *body = xs_json_dumps_pp(rsp, 4);
  1436. *ctype = "application/json";
  1437. status = 200;
  1438. }
  1439. }
  1440. else
  1441. status = 401;
  1442. }
  1443. /* user cleanup */
  1444. if (logged_in)
  1445. user_free(&snac);
  1446. return status;
  1447. }
  1448. int mastoapi_put_handler(const xs_dict *req, const char *q_path,
  1449. const char *payload, int p_size,
  1450. char **body, int *b_size, char **ctype)
  1451. {
  1452. if (!xs_startswith(q_path, "/api/v1/") && !xs_startswith(q_path, "/api/v2/"))
  1453. return 0;
  1454. srv_debug(1, xs_fmt("mastoapi_post_handler %s", q_path));
  1455. /* {
  1456. xs *j = xs_json_dumps_pp(req, 4);
  1457. printf("mastoapi put:\n%s\n", j);
  1458. }*/
  1459. int status = 404;
  1460. xs *args = NULL;
  1461. char *i_ctype = xs_dict_get(req, "content-type");
  1462. if (i_ctype && xs_startswith(i_ctype, "application/json"))
  1463. args = xs_json_loads(payload);
  1464. else
  1465. args = xs_dup(xs_dict_get(req, "p_vars"));
  1466. if (args == NULL)
  1467. return 400;
  1468. xs *cmd = xs_replace(q_path, "/api", "");
  1469. snac snac = {0};
  1470. int logged_in = process_auth_token(&snac, req);
  1471. if (xs_startswith(cmd, "/v1/media") || xs_startswith(cmd, "/v2/media")) {
  1472. if (logged_in) {
  1473. xs *l = xs_split(cmd, "/");
  1474. const char *stid = xs_list_get(l, 3);
  1475. if (!xs_is_null(stid)) {
  1476. const char *desc = xs_dict_get(args, "description");
  1477. /* set the image metadata */
  1478. static_put_meta(&snac, stid, desc);
  1479. /* prepare a response */
  1480. xs *rsp = xs_dict_new();
  1481. xs *url = xs_fmt("%s/s/%s", snac.actor, stid);
  1482. rsp = xs_dict_append(rsp, "id", stid);
  1483. rsp = xs_dict_append(rsp, "type", "image");
  1484. rsp = xs_dict_append(rsp, "url", url);
  1485. rsp = xs_dict_append(rsp, "preview_url", url);
  1486. rsp = xs_dict_append(rsp, "remote_url", url);
  1487. rsp = xs_dict_append(rsp, "description", desc);
  1488. *body = xs_json_dumps_pp(rsp, 4);
  1489. *ctype = "application/json";
  1490. status = 200;
  1491. }
  1492. }
  1493. else
  1494. status = 401;
  1495. }
  1496. /* user cleanup */
  1497. if (logged_in)
  1498. user_free(&snac);
  1499. return status;
  1500. }
  1501. #endif /* #ifndef NO_MASTODON_API */