mastoapi.c 111 KB

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