mastoapi.c 110 KB

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