mastoapi.c 130 KB

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