mastoapi.c 62 KB

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