mastoapi.c 114 KB

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