mastoapi.c 89 KB

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