mastoapi.c 110 KB

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