mastoapi.c 85 KB

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