mastoapi.c 88 KB

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