mastoapi.c 101 KB

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