mastoapi.c 87 KB

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