mastoapi.c 112 KB

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