mastoapi.c 88 KB

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