mastoapi.c 112 KB

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