data.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
  3. #include "xs.h"
  4. #include "xs_hex.h"
  5. #include "xs_io.h"
  6. #include "xs_json.h"
  7. #include "xs_openssl.h"
  8. #include "xs_glob.h"
  9. #include "xs_set.h"
  10. #include "xs_time.h"
  11. #include "xs_regex.h"
  12. #include "snac.h"
  13. #include <time.h>
  14. #include <sys/stat.h>
  15. #include <sys/file.h>
  16. #include <sys/time.h>
  17. #include <fcntl.h>
  18. #include <pthread.h>
  19. double disk_layout = 2.7;
  20. /* storage serializer */
  21. pthread_mutex_t data_mutex = {0};
  22. int snac_upgrade(xs_str **error);
  23. int srv_open(char *basedir, int auto_upgrade)
  24. /* opens a server */
  25. {
  26. int ret = 0;
  27. xs *cfg_file = NULL;
  28. FILE *f;
  29. xs_str *error = NULL;
  30. pthread_mutex_init(&data_mutex, NULL);
  31. srv_basedir = xs_str_new(basedir);
  32. if (xs_endswith(srv_basedir, "/"))
  33. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  34. cfg_file = xs_fmt("%s/server.json", basedir);
  35. if ((f = fopen(cfg_file, "r")) == NULL)
  36. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  37. else {
  38. /* read full config file */
  39. srv_config = xs_json_load(f);
  40. fclose(f);
  41. /* parse */
  42. if (srv_config == NULL)
  43. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  44. else {
  45. char *host;
  46. char *prefix;
  47. char *dbglvl;
  48. host = xs_dict_get(srv_config, "host");
  49. prefix = xs_dict_get(srv_config, "prefix");
  50. dbglvl = xs_dict_get(srv_config, "dbglevel");
  51. if (host == NULL || prefix == NULL)
  52. error = xs_str_new("ERROR: cannot get server data");
  53. else {
  54. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  55. dbglevel = (int) xs_number_get(dbglvl);
  56. if ((dbglvl = getenv("DEBUG")) != NULL) {
  57. dbglevel = atoi(dbglvl);
  58. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  59. }
  60. if (auto_upgrade)
  61. ret = snac_upgrade(&error);
  62. else {
  63. if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
  64. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  65. else
  66. ret = 1;
  67. }
  68. }
  69. }
  70. }
  71. if (error != NULL)
  72. srv_log(error);
  73. /* create the queue/ subdir, just in case */
  74. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  75. mkdirx(qdir);
  76. xs *ibdir = xs_fmt("%s/inbox", srv_basedir);
  77. mkdirx(ibdir);
  78. xs *tmpdir = xs_fmt("%s/tmp", srv_basedir);
  79. mkdirx(tmpdir);
  80. #ifdef __APPLE__
  81. /* Apple uses st_atimespec instead of st_atim etc */
  82. #define st_atim st_atimespec
  83. #define st_ctim st_ctimespec
  84. #define st_mtim st_mtimespec
  85. #endif
  86. #ifdef __OpenBSD__
  87. char *v = xs_dict_get(srv_config, "disable_openbsd_security");
  88. if (v && xs_type(v) == XSTYPE_TRUE) {
  89. srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
  90. }
  91. else {
  92. srv_debug(1, xs_fmt("Calling unveil()"));
  93. unveil(basedir, "rwc");
  94. unveil("/tmp", "rwc");
  95. unveil("/usr/sbin/sendmail", "x");
  96. unveil("/etc/resolv.conf", "r");
  97. unveil("/etc/hosts", "r");
  98. unveil("/etc/ssl/openssl.cnf", "r");
  99. unveil("/etc/ssl/cert.pem", "r");
  100. unveil("/usr/share/zoneinfo", "r");
  101. unveil(NULL, NULL);
  102. srv_debug(1, xs_fmt("Calling pledge()"));
  103. pledge("stdio rpath wpath cpath flock inet proc exec dns fattr", NULL);
  104. }
  105. #endif /* __OpenBSD__ */
  106. /* read (and drop) emojis.json, possibly creating it */
  107. xs_free(emojis());
  108. return ret;
  109. }
  110. void srv_free(void)
  111. {
  112. xs_free(srv_basedir);
  113. xs_free(srv_config);
  114. xs_free(srv_baseurl);
  115. pthread_mutex_destroy(&data_mutex);
  116. }
  117. void user_free(snac *snac)
  118. /* frees a user snac */
  119. {
  120. xs_free(snac->uid);
  121. xs_free(snac->basedir);
  122. xs_free(snac->config);
  123. xs_free(snac->config_o);
  124. xs_free(snac->key);
  125. xs_free(snac->links);
  126. xs_free(snac->actor);
  127. xs_free(snac->md5);
  128. }
  129. int user_open(snac *user, const char *uid)
  130. /* opens a user */
  131. {
  132. int ret = 0;
  133. *user = (snac){0};
  134. if (validate_uid(uid)) {
  135. xs *cfg_file = NULL;
  136. FILE *f;
  137. xs *t = xs_fmt("%s/user/%s", srv_basedir, uid);
  138. if (mtime(t) == 0.0) {
  139. /* user folder does not exist; try with a different case */
  140. xs *lcuid = xs_tolower_i(xs_dup(uid));
  141. xs *ulist = user_list();
  142. xs_list *p = ulist;
  143. xs_str *v;
  144. while (xs_list_iter(&p, &v)) {
  145. xs *v2 = xs_tolower_i(xs_dup(v));
  146. if (strcmp(lcuid, v2) == 0) {
  147. user->uid = xs_dup(v);
  148. break;
  149. }
  150. }
  151. }
  152. else
  153. user->uid = xs_str_new(uid);
  154. if (user->uid == NULL)
  155. return ret;
  156. user->basedir = xs_fmt("%s/user/%s", srv_basedir, user->uid);
  157. cfg_file = xs_fmt("%s/user.json", user->basedir);
  158. if ((f = fopen(cfg_file, "r")) != NULL) {
  159. /* read full config file */
  160. user->config = xs_json_load(f);
  161. fclose(f);
  162. if (user->config != NULL) {
  163. xs *key_file = xs_fmt("%s/key.json", user->basedir);
  164. if ((f = fopen(key_file, "r")) != NULL) {
  165. user->key = xs_json_load(f);
  166. fclose(f);
  167. if (user->key != NULL) {
  168. user->actor = xs_fmt("%s/%s", srv_baseurl, user->uid);
  169. user->md5 = xs_md5_hex(user->actor, strlen(user->actor));
  170. /* everything is ok right now */
  171. ret = 1;
  172. /* does it have a configuration override? */
  173. xs *cfg_file_o = xs_fmt("%s/user_o.json", user->basedir);
  174. if ((f = fopen(cfg_file_o, "r")) != NULL) {
  175. user->config_o = xs_json_load(f);
  176. fclose(f);
  177. if (user->config_o == NULL)
  178. srv_log(xs_fmt("error parsing '%s'", cfg_file_o));
  179. }
  180. if (user->config_o == NULL)
  181. user->config_o = xs_dict_new();
  182. }
  183. else
  184. srv_log(xs_fmt("error parsing '%s'", key_file));
  185. }
  186. else
  187. srv_log(xs_fmt("error opening '%s' %d", key_file, errno));
  188. }
  189. else
  190. srv_log(xs_fmt("error parsing '%s'", cfg_file));
  191. }
  192. else
  193. srv_debug(2, xs_fmt("error opening '%s' %d", cfg_file, errno));
  194. /* verified links */
  195. xs *links_file = xs_fmt("%s/links.json", user->basedir);
  196. if ((f = fopen(links_file, "r")) != NULL) {
  197. user->links = xs_json_load(f);
  198. fclose(f);
  199. }
  200. }
  201. else
  202. srv_debug(1, xs_fmt("invalid user '%s'", uid));
  203. if (!ret)
  204. user_free(user);
  205. return ret;
  206. }
  207. xs_list *user_list(void)
  208. /* returns the list of user ids */
  209. {
  210. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  211. return xs_glob(spec, 1, 0);
  212. }
  213. int user_open_by_md5(snac *snac, const char *md5)
  214. /* iterates all users searching by md5 */
  215. {
  216. xs *ulist = user_list();
  217. xs_list *p = ulist;
  218. xs_str *v;
  219. while (xs_list_iter(&p, &v)) {
  220. user_open(snac, v);
  221. if (strcmp(snac->md5, md5) == 0)
  222. return 1;
  223. user_free(snac);
  224. }
  225. return 0;
  226. }
  227. double mtime_nl(const char *fn, int *n_link)
  228. /* returns the mtime and number of links of a file or directory, or 0.0 */
  229. {
  230. struct stat st;
  231. double r = 0.0;
  232. int n = 0;
  233. if (fn && stat(fn, &st) != -1) {
  234. r = (double) st.st_mtim.tv_sec;
  235. n = st.st_nlink;
  236. }
  237. if (n_link)
  238. *n_link = n;
  239. return r;
  240. }
  241. #define MIN(v1, v2) ((v1) < (v2) ? (v1) : (v2))
  242. double f_ctime(const char *fn)
  243. /* returns the ctime of a file or directory, or 0.0 */
  244. {
  245. struct stat st;
  246. double r = 0.0;
  247. if (fn && stat(fn, &st) != -1) {
  248. /* return the lowest of ctime and mtime;
  249. there are operations that change the ctime, like link() */
  250. r = (double) MIN(st.st_ctim.tv_sec, st.st_mtim.tv_sec);
  251. }
  252. return r;
  253. }
  254. /** database 2.1+ **/
  255. /** indexes **/
  256. int index_add_md5(const char *fn, const char *md5)
  257. /* adds an md5 to an index */
  258. {
  259. int status = 201; /* Created */
  260. FILE *f;
  261. pthread_mutex_lock(&data_mutex);
  262. if ((f = fopen(fn, "a")) != NULL) {
  263. flock(fileno(f), LOCK_EX);
  264. /* ensure the position is at the end after getting the lock */
  265. fseek(f, 0, SEEK_END);
  266. fprintf(f, "%s\n", md5);
  267. fclose(f);
  268. }
  269. else
  270. status = 500;
  271. pthread_mutex_unlock(&data_mutex);
  272. return status;
  273. }
  274. int index_add(const char *fn, const char *id)
  275. /* adds an id to an index */
  276. {
  277. xs *md5 = xs_md5_hex(id, strlen(id));
  278. return index_add_md5(fn, md5);
  279. }
  280. int index_del_md5(const char *fn, const char *md5)
  281. /* deletes an md5 from an index */
  282. {
  283. int status = 404;
  284. FILE *f;
  285. pthread_mutex_lock(&data_mutex);
  286. if ((f = fopen(fn, "r+")) != NULL) {
  287. char line[256];
  288. while (fgets(line, sizeof(line), f) != NULL) {
  289. line[32] = '\0';
  290. if (strcmp(line, md5) == 0) {
  291. /* found! just rewind, overwrite it with garbage
  292. and an eventual call to index_gc() will clean it
  293. [yes: this breaks index_len()] */
  294. fseek(f, -33, SEEK_CUR);
  295. fwrite("-", 1, 1, f);
  296. status = 200;
  297. break;
  298. }
  299. }
  300. fclose(f);
  301. }
  302. else
  303. status = 500;
  304. pthread_mutex_unlock(&data_mutex);
  305. return status;
  306. }
  307. int index_del(const char *fn, const char *id)
  308. /* deletes an id from an index */
  309. {
  310. xs *md5 = xs_md5_hex(id, strlen(id));
  311. return index_del_md5(fn, md5);
  312. }
  313. int index_gc(const char *fn)
  314. /* garbage-collects an index, deleting objects that are not here */
  315. {
  316. FILE *i, *o;
  317. int gc = -1;
  318. pthread_mutex_lock(&data_mutex);
  319. if ((i = fopen(fn, "r")) != NULL) {
  320. xs *nfn = xs_fmt("%s.new", fn);
  321. char line[256];
  322. if ((o = fopen(nfn, "w")) != NULL) {
  323. gc = 0;
  324. while (fgets(line, sizeof(line), i) != NULL) {
  325. line[32] = '\0';
  326. if (line[0] != '-' && object_here_by_md5(line))
  327. fprintf(o, "%s\n", line);
  328. else
  329. gc++;
  330. }
  331. fclose(o);
  332. xs *ofn = xs_fmt("%s.bak", fn);
  333. unlink(ofn);
  334. link(fn, ofn);
  335. rename(nfn, fn);
  336. }
  337. fclose(i);
  338. }
  339. pthread_mutex_unlock(&data_mutex);
  340. return gc;
  341. }
  342. int index_in_md5(const char *fn, const char *md5)
  343. /* checks if the md5 is already in the index */
  344. {
  345. FILE *f;
  346. int ret = 0;
  347. if ((f = fopen(fn, "r")) != NULL) {
  348. flock(fileno(f), LOCK_SH);
  349. char line[256];
  350. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  351. line[32] = '\0';
  352. if (strcmp(line, md5) == 0)
  353. ret = 1;
  354. }
  355. fclose(f);
  356. }
  357. return ret;
  358. }
  359. int index_in(const char *fn, const char *id)
  360. /* checks if the object id is already in the index */
  361. {
  362. xs *md5 = xs_md5_hex(id, strlen(id));
  363. return index_in_md5(fn, md5);
  364. }
  365. int index_first(const char *fn, char *line, int size)
  366. /* reads the first entry of an index */
  367. {
  368. FILE *f;
  369. int ret = 0;
  370. if ((f = fopen(fn, "r")) != NULL) {
  371. flock(fileno(f), LOCK_SH);
  372. if (fgets(line, size, f) != NULL) {
  373. line[32] = '\0';
  374. ret = 1;
  375. }
  376. fclose(f);
  377. }
  378. return ret;
  379. }
  380. int index_len(const char *fn)
  381. /* returns the number of elements in an index */
  382. {
  383. struct stat st;
  384. int len = 0;
  385. if (stat(fn, &st) != -1)
  386. len = st.st_size / 33;
  387. return len;
  388. }
  389. xs_list *index_list(const char *fn, int max)
  390. /* returns an index as a list */
  391. {
  392. xs_list *list = xs_list_new();
  393. FILE *f;
  394. int n = 0;
  395. if ((f = fopen(fn, "r")) != NULL) {
  396. flock(fileno(f), LOCK_SH);
  397. char line[256];
  398. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  399. if (line[0] != '-') {
  400. line[32] = '\0';
  401. list = xs_list_append(list, line);
  402. n++;
  403. }
  404. }
  405. fclose(f);
  406. }
  407. return list;
  408. }
  409. xs_list *index_list_desc(const char *fn, int skip, int show)
  410. /* returns an index as a list, in reverse order */
  411. {
  412. xs_list *list = xs_list_new();
  413. FILE *f;
  414. int n = 0;
  415. if ((f = fopen(fn, "r")) != NULL) {
  416. flock(fileno(f), LOCK_SH);
  417. char line[256];
  418. /* move to the end minus one entry (or more, if skipping entries) */
  419. if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
  420. while (n < show && fgets(line, sizeof(line), f) != NULL) {
  421. if (line[0] != '-') {
  422. line[32] = '\0';
  423. list = xs_list_append(list, line);
  424. n++;
  425. }
  426. /* move backwards 2 entries */
  427. if (fseek(f, -66, SEEK_CUR) == -1)
  428. break;
  429. }
  430. }
  431. fclose(f);
  432. }
  433. return list;
  434. }
  435. /** objects **/
  436. static xs_str *_object_fn_by_md5(const char *md5, const char *func)
  437. {
  438. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  439. xs_str *ret;
  440. int ok = 1;
  441. /* an object deleted from an index; fail but don't bark */
  442. if (md5[0] == '-')
  443. ok = 0;
  444. else
  445. if (!xs_is_hex(md5) || strlen(md5) != 32) {
  446. srv_log(xs_fmt("_object_fn_by_md5() [from %s()]: bad md5 '%s'", func, md5));
  447. ok = 0;
  448. }
  449. if (ok) {
  450. mkdirx(bfn);
  451. ret = xs_fmt("%s/%s.json", bfn, md5);
  452. }
  453. else
  454. ret = xs_fmt("%s/object/invalid/invalid.json", srv_basedir);
  455. return ret;
  456. }
  457. static xs_str *_object_fn(const char *id)
  458. {
  459. xs *md5 = xs_md5_hex(id, strlen(id));
  460. return _object_fn_by_md5(md5, "_object_fn");
  461. }
  462. int object_here_by_md5(const char *id)
  463. /* checks if an object is already downloaded */
  464. {
  465. xs *fn = _object_fn_by_md5(id, "object_here_by_md5");
  466. return mtime(fn) > 0.0;
  467. }
  468. int object_here(const char *id)
  469. /* checks if an object is already downloaded */
  470. {
  471. xs *fn = _object_fn(id);
  472. return mtime(fn) > 0.0;
  473. }
  474. int object_get_by_md5(const char *md5, xs_dict **obj)
  475. /* returns a stored object, optionally of the requested type */
  476. {
  477. int status = 404;
  478. xs *fn = _object_fn_by_md5(md5, "object_get_by_md5");
  479. FILE *f;
  480. if ((f = fopen(fn, "r")) != NULL) {
  481. *obj = xs_json_load(f);
  482. fclose(f);
  483. if (*obj)
  484. status = 200;
  485. }
  486. else
  487. *obj = NULL;
  488. return status;
  489. }
  490. int object_get(const char *id, xs_dict **obj)
  491. /* returns a stored object, optionally of the requested type */
  492. {
  493. xs *md5 = xs_md5_hex(id, strlen(id));
  494. return object_get_by_md5(md5, obj);
  495. }
  496. int _object_add(const char *id, const xs_dict *obj, int ow)
  497. /* stores an object */
  498. {
  499. int status = 201; /* Created */
  500. xs *fn = _object_fn(id);
  501. FILE *f;
  502. if (mtime(fn) > 0.0) {
  503. if (!ow) {
  504. /* object already here */
  505. srv_debug(1, xs_fmt("object_add object already here %s", id));
  506. return 204; /* No content */
  507. }
  508. else
  509. status = 200;
  510. }
  511. if ((f = fopen(fn, "w")) != NULL) {
  512. flock(fileno(f), LOCK_EX);
  513. xs_json_dump(obj, 4, f);
  514. fclose(f);
  515. /* does this object has a parent? */
  516. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  517. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  518. /* update the children index of the parent */
  519. xs *c_idx = _object_fn(in_reply_to);
  520. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  521. if (!index_in(c_idx, id)) {
  522. index_add(c_idx, id);
  523. srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
  524. }
  525. else
  526. srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
  527. /* create a one-element index with the parent */
  528. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  529. if (mtime(p_idx) == 0.0) {
  530. index_add(p_idx, in_reply_to);
  531. srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  532. }
  533. }
  534. }
  535. else {
  536. srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
  537. status = 500;
  538. }
  539. srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
  540. return status;
  541. }
  542. int object_add(const char *id, const xs_dict *obj)
  543. /* stores an object */
  544. {
  545. return _object_add(id, obj, 0);
  546. }
  547. int object_add_ow(const char *id, const xs_dict *obj)
  548. /* stores an object (overwriting allowed) */
  549. {
  550. return _object_add(id, obj, 1);
  551. }
  552. int object_del_by_md5(const char *md5)
  553. /* deletes an object by its md5 */
  554. {
  555. int status = 404;
  556. xs *fn = _object_fn_by_md5(md5, "object_del_by_md5");
  557. if (unlink(fn) != -1) {
  558. status = 200;
  559. /* also delete associated indexes */
  560. xs *spec = xs_dup(fn);
  561. spec = xs_replace_i(spec, ".json", "*.idx");
  562. xs *files = xs_glob(spec, 0, 0);
  563. char *p, *v;
  564. p = files;
  565. while (xs_list_iter(&p, &v)) {
  566. srv_debug(1, xs_fmt("object_del index %s", v));
  567. unlink(v);
  568. }
  569. }
  570. srv_debug(1, xs_fmt("object_del %s %d", fn, status));
  571. return status;
  572. }
  573. int object_del(const char *id)
  574. /* deletes an object */
  575. {
  576. xs *md5 = xs_md5_hex(id, strlen(id));
  577. return object_del_by_md5(md5);
  578. }
  579. int object_del_if_unref(const char *id)
  580. /* deletes an object if its n_links < 2 */
  581. {
  582. xs *fn = _object_fn(id);
  583. int n_links;
  584. int ret = 0;
  585. if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
  586. ret = object_del(id);
  587. return ret;
  588. }
  589. double object_ctime_by_md5(const char *md5)
  590. {
  591. xs *fn = _object_fn_by_md5(md5, "object_ctime_by_md5");
  592. return f_ctime(fn);
  593. }
  594. double object_ctime(const char *id)
  595. {
  596. xs *md5 = xs_md5_hex(id, strlen(id));
  597. return object_ctime_by_md5(md5);
  598. }
  599. double object_mtime_by_md5(const char *md5)
  600. {
  601. xs *fn = _object_fn_by_md5(md5, "object_mtime_by_md5");
  602. return mtime(fn);
  603. }
  604. double object_mtime(const char *id)
  605. {
  606. xs *md5 = xs_md5_hex(id, strlen(id));
  607. return object_mtime_by_md5(md5);
  608. }
  609. void object_touch(const char *id)
  610. {
  611. xs *md5 = xs_md5_hex(id, strlen(id));
  612. xs *fn = _object_fn_by_md5(md5, "object_touch");
  613. if (mtime(fn))
  614. utimes(fn, NULL);
  615. }
  616. xs_str *_object_index_fn(const char *id, const char *idxsfx)
  617. /* returns the filename of an object's index */
  618. {
  619. xs_str *fn = _object_fn(id);
  620. return xs_replace_i(fn, ".json", idxsfx);
  621. }
  622. int object_likes_len(const char *id)
  623. /* returns the number of likes (without reading the index) */
  624. {
  625. xs *fn = _object_index_fn(id, "_l.idx");
  626. return index_len(fn);
  627. }
  628. int object_announces_len(const char *id)
  629. /* returns the number of announces (without reading the index) */
  630. {
  631. xs *fn = _object_index_fn(id, "_a.idx");
  632. return index_len(fn);
  633. }
  634. xs_list *object_children(const char *id)
  635. /* returns the list of an object's children */
  636. {
  637. xs *fn = _object_index_fn(id, "_c.idx");
  638. return index_list(fn, XS_ALL);
  639. }
  640. xs_list *object_likes(const char *id)
  641. {
  642. xs *fn = _object_index_fn(id, "_l.idx");
  643. return index_list(fn, XS_ALL);
  644. }
  645. xs_list *object_announces(const char *id)
  646. {
  647. xs *fn = _object_index_fn(id, "_a.idx");
  648. return index_list(fn, XS_ALL);
  649. }
  650. int object_parent(const char *md5, char *buf, int size)
  651. /* returns the object parent, if any */
  652. {
  653. xs *fn = _object_fn_by_md5(md5, "object_parent");
  654. fn = xs_replace_i(fn, ".json", "_p.idx");
  655. return index_first(fn, buf, size);
  656. }
  657. int object_admire(const char *id, const char *actor, int like)
  658. /* actor likes or announces this object */
  659. {
  660. int status = 200;
  661. xs *fn = _object_fn(id);
  662. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  663. if (!index_in(fn, actor)) {
  664. status = index_add(fn, actor);
  665. srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  666. }
  667. return status;
  668. }
  669. int object_unadmire(const char *id, const char *actor, int like)
  670. /* actor no longer likes or announces this object */
  671. {
  672. int status;
  673. xs *fn = _object_fn(id);
  674. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  675. status = index_del(fn, actor);
  676. srv_debug(0,
  677. xs_fmt("object_unadmire (%s) %s %s %d", like ? "Like" : "Announce", actor, fn, status));
  678. return status;
  679. }
  680. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  681. /* adds or deletes from a user cache */
  682. {
  683. xs *ofn = _object_fn(id);
  684. xs *l = xs_split(ofn, "/");
  685. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  686. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  687. int ret;
  688. if (del) {
  689. ret = unlink(cfn);
  690. index_del(idx, id);
  691. }
  692. else {
  693. if ((ret = link(ofn, cfn)) != -1)
  694. index_add(idx, id);
  695. }
  696. return ret;
  697. }
  698. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  699. /* caches an object into a user cache */
  700. {
  701. return _object_user_cache(snac, id, cachedir, 0);
  702. }
  703. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  704. /* deletes an object from a user cache */
  705. {
  706. return _object_user_cache(snac, id, cachedir, 1);
  707. }
  708. int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
  709. /* checks if an object is stored in a cache */
  710. {
  711. xs *md5 = xs_md5_hex(id, strlen(id));
  712. xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
  713. return !!(mtime(cfn) != 0.0);
  714. }
  715. xs_list *object_user_cache_list(snac *snac, const char *cachedir, int max, int inv)
  716. /* returns the objects in a cache as a list */
  717. {
  718. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  719. return inv ? index_list_desc(idx, 0, max) : index_list(idx, max);
  720. }
  721. /** specialized functions **/
  722. /** followers **/
  723. int follower_add(snac *snac, const char *actor)
  724. /* adds a follower */
  725. {
  726. int ret = object_user_cache_add(snac, actor, "followers");
  727. snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
  728. return ret == -1 ? 500 : 200;
  729. }
  730. int follower_del(snac *snac, const char *actor)
  731. /* deletes a follower */
  732. {
  733. int ret = object_user_cache_del(snac, actor, "followers");
  734. snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
  735. return ret == -1 ? 404 : 200;
  736. }
  737. int follower_check(snac *snac, const char *actor)
  738. /* checks if someone is a follower */
  739. {
  740. return object_user_cache_in(snac, actor, "followers");
  741. }
  742. xs_list *follower_list(snac *snac)
  743. /* returns the list of followers */
  744. {
  745. xs *list = object_user_cache_list(snac, "followers", XS_ALL, 0);
  746. xs_list *fwers = xs_list_new();
  747. char *p, *v;
  748. /* resolve the list of md5 to be a list of actors */
  749. p = list;
  750. while (xs_list_iter(&p, &v)) {
  751. xs *a_obj = NULL;
  752. if (valid_status(object_get_by_md5(v, &a_obj))) {
  753. const char *actor = xs_dict_get(a_obj, "id");
  754. if (!xs_is_null(actor)) {
  755. /* check if the actor is still cached */
  756. xs *fn = xs_fmt("%s/followers/%s.json", snac->basedir, v);
  757. if (mtime(fn) > 0.0)
  758. fwers = xs_list_append(fwers, actor);
  759. }
  760. }
  761. }
  762. return fwers;
  763. }
  764. /** timeline **/
  765. double timeline_mtime(snac *snac)
  766. {
  767. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  768. return mtime(fn);
  769. }
  770. int timeline_touch(snac *snac)
  771. /* changes the date of the timeline index */
  772. {
  773. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  774. return utimes(fn, NULL);
  775. }
  776. xs_str *timeline_fn_by_md5(snac *snac, const char *md5)
  777. /* get the filename of an entry by md5 from any timeline */
  778. {
  779. xs_str *fn = xs_fmt("%s/private/%s.json", snac->basedir, md5);
  780. if (mtime(fn) == 0.0) {
  781. fn = xs_free(fn);
  782. fn = xs_fmt("%s/public/%s.json", snac->basedir, md5);
  783. if (mtime(fn) == 0.0)
  784. fn = xs_free(fn);
  785. }
  786. return fn;
  787. }
  788. int timeline_here(snac *snac, const char *md5)
  789. /* checks if an object is in the user cache */
  790. {
  791. xs *fn = timeline_fn_by_md5(snac, md5);
  792. return !(fn == NULL);
  793. }
  794. int timeline_get_by_md5(snac *snac, const char *md5, xs_dict **msg)
  795. /* gets a message from the timeline */
  796. {
  797. int status = 404;
  798. FILE *f = NULL;
  799. xs *fn = timeline_fn_by_md5(snac, md5);
  800. if (fn != NULL && (f = fopen(fn, "r")) != NULL) {
  801. *msg = xs_json_load(f);
  802. fclose(f);
  803. if (*msg != NULL)
  804. status = 200;
  805. }
  806. return status;
  807. }
  808. int timeline_del(snac *snac, char *id)
  809. /* deletes a message from the timeline */
  810. {
  811. /* delete from the user's caches */
  812. object_user_cache_del(snac, id, "public");
  813. object_user_cache_del(snac, id, "private");
  814. /* try to delete the object if it's not used elsewhere */
  815. return object_del_if_unref(id);
  816. }
  817. void timeline_update_indexes(snac *snac, const char *id)
  818. /* updates the indexes */
  819. {
  820. object_user_cache_add(snac, id, "private");
  821. if (xs_startswith(id, snac->actor)) {
  822. xs *msg = NULL;
  823. if (valid_status(object_get(id, &msg))) {
  824. /* if its ours and is public, also store in public */
  825. if (is_msg_public(msg)) {
  826. object_user_cache_add(snac, id, "public");
  827. /* also add it to the instance public timeline */
  828. xs *ipt = xs_fmt("%s/public.idx", srv_basedir);
  829. index_add(ipt, id);
  830. }
  831. }
  832. }
  833. }
  834. int timeline_add(snac *snac, const char *id, const xs_dict *o_msg)
  835. /* adds a message to the timeline */
  836. {
  837. int ret = object_add(id, o_msg);
  838. timeline_update_indexes(snac, id);
  839. tag_index(id, o_msg);
  840. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  841. return ret;
  842. }
  843. int timeline_admire(snac *snac, const char *id, const char *admirer, int like)
  844. /* updates a timeline entry with a new admiration */
  845. {
  846. /* if we are admiring this, add to both timelines */
  847. if (!like && strcmp(admirer, snac->actor) == 0) {
  848. object_user_cache_add(snac, id, "public");
  849. object_user_cache_add(snac, id, "private");
  850. }
  851. int ret = object_admire(id, admirer, like);
  852. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  853. like ? "Like" : "Announce", id, admirer));
  854. return ret;
  855. }
  856. xs_list *timeline_top_level(snac *snac, xs_list *list)
  857. /* returns the top level md5 entries from this index */
  858. {
  859. xs_set seen;
  860. xs_list *p;
  861. xs_str *v;
  862. xs_set_init(&seen);
  863. p = list;
  864. while (xs_list_iter(&p, &v)) {
  865. char line[256] = "";
  866. strncpy(line, v, sizeof(line));
  867. for (;;) {
  868. char line2[256];
  869. /* if it doesn't have a parent, use this */
  870. if (!object_parent(line, line2, sizeof(line2)))
  871. break;
  872. /* well, there is a parent... but is it here? */
  873. if (!timeline_here(snac, line2))
  874. break;
  875. /* it's here! try again with its own parent */
  876. strncpy(line, line2, sizeof(line));
  877. }
  878. xs_set_add(&seen, line);
  879. }
  880. return xs_set_result(&seen);
  881. }
  882. xs_list *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show)
  883. /* returns a timeline (with all entries) */
  884. {
  885. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
  886. return index_list_desc(idx, skip, show);
  887. }
  888. xs_list *timeline_list(snac *snac, const char *idx_name, int skip, int show)
  889. /* returns a timeline (only top level entries) */
  890. {
  891. int c_max;
  892. /* maximum number of items in the timeline */
  893. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  894. /* never more timeline entries than the configured maximum */
  895. if (show > c_max)
  896. show = c_max;
  897. xs *list = timeline_simple_list(snac, idx_name, skip, show);
  898. return timeline_top_level(snac, list);
  899. }
  900. xs_list *timeline_instance_list(int skip, int show)
  901. /* returns the timeline for the full instance */
  902. {
  903. xs *idx = xs_fmt("%s/public.idx", srv_basedir);
  904. return index_list_desc(idx, skip, show);
  905. }
  906. /** following **/
  907. /* this needs special treatment and cannot use the object db as is,
  908. with a link to a cached author, because we need the Follow object
  909. in case we need to unfollow (Undo + original Follow) */
  910. xs_str *_following_fn(snac *snac, const char *actor)
  911. {
  912. xs *md5 = xs_md5_hex(actor, strlen(actor));
  913. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  914. }
  915. int following_add(snac *snac, const char *actor, const xs_dict *msg)
  916. /* adds to the following list */
  917. {
  918. int ret = 201; /* created */
  919. xs *fn = _following_fn(snac, actor);
  920. FILE *f;
  921. xs *p_object = NULL;
  922. if (valid_status(following_get(snac, actor, &p_object))) {
  923. /* object already exists; if it's of type Accept,
  924. the actor is already being followed and confirmed,
  925. so do nothing */
  926. char *type = xs_dict_get(p_object, "type");
  927. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  928. snac_debug(snac, 1, xs_fmt("following_add actor already confirmed %s", actor));
  929. return 200;
  930. }
  931. }
  932. if ((f = fopen(fn, "w")) != NULL) {
  933. xs_json_dump(msg, 4, f);
  934. fclose(f);
  935. /* get the filename of the actor object */
  936. xs *actor_fn = _object_fn(actor);
  937. /* increase its reference count */
  938. fn = xs_replace_i(fn, ".json", "_a.json");
  939. link(actor_fn, fn);
  940. }
  941. else
  942. ret = 500;
  943. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  944. return ret;
  945. }
  946. int following_del(snac *snac, const char *actor)
  947. /* we're not following this actor any longer */
  948. {
  949. xs *fn = _following_fn(snac, actor);
  950. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  951. unlink(fn);
  952. /* also delete the reference to the author */
  953. fn = xs_replace_i(fn, ".json", "_a.json");
  954. unlink(fn);
  955. return 200;
  956. }
  957. int following_check(snac *snac, const char *actor)
  958. /* checks if we are following this actor */
  959. {
  960. xs *fn = _following_fn(snac, actor);
  961. return !!(mtime(fn) != 0.0);
  962. }
  963. int following_get(snac *snac, const char *actor, xs_dict **data)
  964. /* returns the 'Follow' object */
  965. {
  966. xs *fn = _following_fn(snac, actor);
  967. FILE *f;
  968. int status = 200;
  969. if ((f = fopen(fn, "r")) != NULL) {
  970. *data = xs_json_load(f);
  971. fclose(f);
  972. }
  973. else
  974. status = 404;
  975. return status;
  976. }
  977. xs_list *following_list(snac *snac)
  978. /* returns the list of people being followed */
  979. {
  980. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  981. xs *glist = xs_glob(spec, 0, 0);
  982. xs_list *p;
  983. xs_str *v;
  984. xs_list *list = xs_list_new();
  985. /* iterate the list of files */
  986. p = glist;
  987. while (xs_list_iter(&p, &v)) {
  988. FILE *f;
  989. /* load the follower data */
  990. if ((f = fopen(v, "r")) != NULL) {
  991. xs *o = xs_json_load(f);
  992. fclose(f);
  993. if (o != NULL) {
  994. const char *type = xs_dict_get(o, "type");
  995. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  996. const char *actor = xs_dict_get(o, "actor");
  997. if (!xs_is_null(actor)) {
  998. list = xs_list_append(list, actor);
  999. /* check if there is a link to the actor object */
  1000. xs *v2 = xs_replace(v, ".json", "_a.json");
  1001. if (mtime(v2) == 0.0) {
  1002. /* no; add a link to it */
  1003. xs *actor_fn = _object_fn(actor);
  1004. link(actor_fn, v2);
  1005. }
  1006. }
  1007. }
  1008. }
  1009. }
  1010. }
  1011. return list;
  1012. }
  1013. xs_str *_muted_fn(snac *snac, const char *actor)
  1014. {
  1015. xs *md5 = xs_md5_hex(actor, strlen(actor));
  1016. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  1017. }
  1018. void mute(snac *snac, const char *actor)
  1019. /* mutes a moron */
  1020. {
  1021. xs *fn = _muted_fn(snac, actor);
  1022. FILE *f;
  1023. if ((f = fopen(fn, "w")) != NULL) {
  1024. fprintf(f, "%s\n", actor);
  1025. fclose(f);
  1026. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  1027. }
  1028. }
  1029. void unmute(snac *snac, const char *actor)
  1030. /* actor is no longer a moron */
  1031. {
  1032. xs *fn = _muted_fn(snac, actor);
  1033. unlink(fn);
  1034. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  1035. }
  1036. int is_muted(snac *snac, const char *actor)
  1037. /* check if someone is muted */
  1038. {
  1039. xs *fn = _muted_fn(snac, actor);
  1040. return !!(mtime(fn) != 0.0);
  1041. }
  1042. /** pinning **/
  1043. xs_str *_pinned_fn(snac *user, const char *id)
  1044. {
  1045. xs *md5 = xs_md5_hex(id, strlen(id));
  1046. return xs_fmt("%s/pinned/%s.json", user->basedir, md5);
  1047. }
  1048. int is_pinned(snac *user, const char *id)
  1049. /* returns true if this note is pinned */
  1050. {
  1051. xs *fn = _pinned_fn(user, id);
  1052. return !!(mtime(fn) != 0.0);
  1053. }
  1054. int is_pinned_by_md5(snac *user, const char *md5)
  1055. {
  1056. xs *fn = xs_fmt("%s/pinned/%s.json", user->basedir, md5);
  1057. return !!(mtime(fn) != 0.0);
  1058. }
  1059. int pin(snac *user, const char *id)
  1060. /* pins a message */
  1061. {
  1062. int ret = -2;
  1063. if (xs_startswith(id, user->actor)) {
  1064. if (is_pinned(user, id))
  1065. ret = -3;
  1066. else {
  1067. /* create the subfolder, if it does not exist */
  1068. xs *fn = xs_fmt("%s/pinned/", user->basedir);
  1069. mkdirx(fn);
  1070. ret = object_user_cache_add(user, id, "pinned");
  1071. }
  1072. }
  1073. return ret;
  1074. }
  1075. int unpin(snac *user, const char *id)
  1076. /* unpin a message */
  1077. {
  1078. int ret = object_user_cache_del(user, id, "pinned");
  1079. if (ret != -1) {
  1080. /* delete from the index */
  1081. xs *idx = xs_fmt("%s/pinned.idx", user->basedir);
  1082. index_del(idx, id);
  1083. }
  1084. return ret;
  1085. }
  1086. xs_list *pinned_list(snac *user)
  1087. /* return the lists of pinned posts */
  1088. {
  1089. return object_user_cache_list(user, "pinned", XS_ALL, 1);
  1090. }
  1091. xs_str *_hidden_fn(snac *snac, const char *id)
  1092. {
  1093. xs *md5 = xs_md5_hex(id, strlen(id));
  1094. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  1095. }
  1096. void hide(snac *snac, const char *id)
  1097. /* hides a message tree */
  1098. {
  1099. xs *fn = _hidden_fn(snac, id);
  1100. FILE *f;
  1101. if ((f = fopen(fn, "w")) != NULL) {
  1102. fprintf(f, "%s\n", id);
  1103. fclose(f);
  1104. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  1105. /* hide all the children */
  1106. xs *chld = object_children(id);
  1107. char *p, *v;
  1108. p = chld;
  1109. while (xs_list_iter(&p, &v)) {
  1110. xs *co = NULL;
  1111. /* resolve to get the id */
  1112. if (valid_status(object_get_by_md5(v, &co))) {
  1113. if ((v = xs_dict_get(co, "id")) != NULL)
  1114. hide(snac, v);
  1115. }
  1116. }
  1117. }
  1118. }
  1119. int is_hidden(snac *snac, const char *id)
  1120. /* check is id is hidden */
  1121. {
  1122. xs *fn = _hidden_fn(snac, id);
  1123. return !!(mtime(fn) != 0.0);
  1124. }
  1125. int actor_add(const char *actor, xs_dict *msg)
  1126. /* adds an actor */
  1127. {
  1128. return object_add_ow(actor, msg);
  1129. }
  1130. int actor_get(const char *actor, xs_dict **data)
  1131. /* returns an already downloaded actor */
  1132. {
  1133. int status = 200;
  1134. xs_dict *d = NULL;
  1135. if (xs_startswith(actor, srv_baseurl)) {
  1136. /* it's a (possible) local user */
  1137. xs *l = xs_split(actor, "/");
  1138. const char *uid = xs_list_get(l, -1);
  1139. snac user;
  1140. if (!xs_is_null(uid) && user_open(&user, uid)) {
  1141. if (data)
  1142. *data = msg_actor(&user);
  1143. user_free(&user);
  1144. return 200;
  1145. }
  1146. else
  1147. return 404;
  1148. }
  1149. /* read the object */
  1150. if (!valid_status(status = object_get(actor, &d))) {
  1151. d = xs_free(d);
  1152. return status;
  1153. }
  1154. /* if the object is corrupted, discard it */
  1155. if (xs_is_null(xs_dict_get(d, "id")) || xs_is_null(xs_dict_get(d, "type"))) {
  1156. srv_debug(1, xs_fmt("corrupted actor object %s", actor));
  1157. d = xs_free(d);
  1158. return 404;
  1159. }
  1160. if (data)
  1161. *data = d;
  1162. else
  1163. d = xs_free(d);
  1164. xs *fn = _object_fn(actor);
  1165. double max_time;
  1166. /* maximum time for the actor data to be considered stale */
  1167. max_time = 3600.0 * 36.0;
  1168. if (mtime(fn) + max_time < (double) time(NULL)) {
  1169. /* actor data exists but also stinks */
  1170. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  1171. }
  1172. return status;
  1173. }
  1174. int actor_get_refresh(snac *user, const char *actor, xs_dict **data)
  1175. /* gets an actor and requests a refresh if it's stale */
  1176. {
  1177. int status = actor_get(actor, data);
  1178. if (status == 205 && user && !xs_startswith(actor, srv_baseurl))
  1179. enqueue_actor_refresh(user, actor);
  1180. return status;
  1181. }
  1182. /** user limiting (announce blocks) **/
  1183. int limited(snac *user, const char *id, int cmd)
  1184. /* announce messages from a followed (0: check, 1: limit; 2: unlimit) */
  1185. {
  1186. int ret = 0;
  1187. xs *dir = xs_fmt("%s/limited", user->basedir);
  1188. xs *md5 = xs_md5_hex(id, strlen(id));
  1189. xs *fn = xs_fmt("%s/%s", dir, md5);
  1190. switch (cmd) {
  1191. case 0: /** check **/
  1192. ret = !!(mtime(fn) > 0.0);
  1193. break;
  1194. case 1: /** limit **/
  1195. mkdirx(dir);
  1196. if (mtime(fn) > 0.0)
  1197. ret = -1;
  1198. else {
  1199. FILE *f;
  1200. if ((f = fopen(fn, "w")) != NULL) {
  1201. fprintf(f, "%s\n", id);
  1202. fclose(f);
  1203. }
  1204. else
  1205. ret = -2;
  1206. }
  1207. break;
  1208. case 2: /** unlimit **/
  1209. if (mtime(fn) > 0.0)
  1210. ret = unlink(fn);
  1211. else
  1212. ret = -1;
  1213. break;
  1214. }
  1215. return ret;
  1216. }
  1217. /** tag indexing **/
  1218. void tag_index(const char *id, const xs_dict *obj)
  1219. /* update the tag indexes for this object */
  1220. {
  1221. xs_list *tags = xs_dict_get(obj, "tag");
  1222. if (is_msg_public(obj) && xs_type(tags) == XSTYPE_LIST && xs_list_len(tags) > 0) {
  1223. xs *g_tag_dir = xs_fmt("%s/tag", srv_basedir);
  1224. mkdirx(g_tag_dir);
  1225. xs_dict *v;
  1226. while (xs_list_iter(&tags, &v)) {
  1227. char *type = xs_dict_get(v, "type");
  1228. char *name = xs_dict_get(v, "name");
  1229. if (!xs_is_null(type) && !xs_is_null(name) && strcmp(type, "Hashtag") == 0) {
  1230. while (*name == '#' || *name == '@')
  1231. name++;
  1232. if (*name == '\0')
  1233. continue;
  1234. name = xs_tolower_i(name);
  1235. xs *md5_tag = xs_md5_hex(name, strlen(name));
  1236. xs *tag_dir = xs_fmt("%s/%c%c", g_tag_dir, md5_tag[0], md5_tag[1]);
  1237. mkdirx(tag_dir);
  1238. xs *g_tag_idx = xs_fmt("%s/%s.idx", tag_dir, md5_tag);
  1239. index_add(g_tag_idx, id);
  1240. FILE *f;
  1241. xs *g_tag_name = xs_replace(g_tag_idx, ".idx", ".tag");
  1242. if ((f = fopen(g_tag_name, "w")) != NULL) {
  1243. fprintf(f, "%s\n", name);
  1244. fclose(f);
  1245. }
  1246. srv_debug(0, xs_fmt("tagged %s #%s (#%s)", id, name, md5_tag));
  1247. }
  1248. }
  1249. }
  1250. }
  1251. xs_list *tag_search(char *tag, int skip, int show)
  1252. /* returns the list of posts tagged with tag */
  1253. {
  1254. if (*tag == '#')
  1255. tag++;
  1256. xs *lw_tag = xs_tolower_i(xs_dup(tag));
  1257. xs *md5 = xs_md5_hex(lw_tag, strlen(lw_tag));
  1258. xs *idx = xs_fmt("%s/tag/%c%c/%s.idx", srv_basedir, md5[0], md5[1], md5);
  1259. return index_list_desc(idx, skip, show);
  1260. }
  1261. /** static data **/
  1262. static int _load_raw_file(const char *fn, xs_val **data, int *size,
  1263. const char *inm, xs_str **etag)
  1264. /* loads a cached file */
  1265. {
  1266. int status = 404;
  1267. if (fn) {
  1268. double tm = mtime(fn);
  1269. if (tm > 0.0) {
  1270. /* file exists; build the etag */
  1271. xs *e = xs_fmt("W/\"snac-%.0lf\"", tm);
  1272. /* if if-none-match is set, check if it's the same */
  1273. if (!xs_is_null(inm) && strcmp(e, inm) == 0) {
  1274. /* client has the newest version */
  1275. status = 304;
  1276. }
  1277. else {
  1278. /* newer or never downloaded; read the full file */
  1279. FILE *f;
  1280. if ((f = fopen(fn, "rb")) != NULL) {
  1281. *size = XS_ALL;
  1282. *data = xs_read(f, size);
  1283. fclose(f);
  1284. status = 200;
  1285. }
  1286. }
  1287. /* if caller wants the etag, return it */
  1288. if (etag != NULL)
  1289. *etag = xs_dup(e);
  1290. srv_debug(1, xs_fmt("_load_raw_file(): %s %d", fn, status));
  1291. }
  1292. }
  1293. return status;
  1294. }
  1295. xs_str *_static_fn(snac *snac, const char *id)
  1296. /* gets the filename for a static file */
  1297. {
  1298. if (strchr(id, '/'))
  1299. return NULL;
  1300. else
  1301. return xs_fmt("%s/static/%s", snac->basedir, id);
  1302. }
  1303. int static_get(snac *snac, const char *id, xs_val **data, int *size,
  1304. const char *inm, xs_str **etag)
  1305. /* returns static content */
  1306. {
  1307. xs *fn = _static_fn(snac, id);
  1308. return _load_raw_file(fn, data, size, inm, etag);
  1309. }
  1310. void static_put(snac *snac, const char *id, const char *data, int size)
  1311. /* writes status content */
  1312. {
  1313. xs *fn = _static_fn(snac, id);
  1314. FILE *f;
  1315. if (fn && (f = fopen(fn, "wb")) != NULL) {
  1316. fwrite(data, size, 1, f);
  1317. fclose(f);
  1318. }
  1319. }
  1320. void static_put_meta(snac *snac, const char *id, const char *str)
  1321. /* puts metadata (i.e. a media description string) to id */
  1322. {
  1323. xs *fn = _static_fn(snac, id);
  1324. if (fn) {
  1325. fn = xs_str_cat(fn, ".txt");
  1326. FILE *f;
  1327. if ((f = fopen(fn, "w")) != NULL) {
  1328. fprintf(f, "%s\n", str);
  1329. fclose(f);
  1330. }
  1331. }
  1332. }
  1333. xs_str *static_get_meta(snac *snac, const char *id)
  1334. /* gets metadata from a media */
  1335. {
  1336. xs *fn = _static_fn(snac, id);
  1337. xs_str *r = NULL;
  1338. if (fn) {
  1339. fn = xs_str_cat(fn, ".txt");
  1340. FILE *f;
  1341. if ((f = fopen(fn, "r")) != NULL) {
  1342. r = xs_strip_i(xs_readline(f));
  1343. fclose(f);
  1344. }
  1345. }
  1346. else
  1347. r = xs_str_new("");
  1348. return r;
  1349. }
  1350. /** history **/
  1351. xs_str *_history_fn(snac *snac, const char *id)
  1352. /* gets the filename for the history */
  1353. {
  1354. if (strchr(id, '/'))
  1355. return NULL;
  1356. else
  1357. return xs_fmt("%s/history/%s", snac->basedir, id);
  1358. }
  1359. double history_mtime(snac *snac, const char *id)
  1360. {
  1361. double t = 0.0;
  1362. xs *fn = _history_fn(snac, id);
  1363. if (fn != NULL)
  1364. t = mtime(fn);
  1365. return t;
  1366. }
  1367. void history_add(snac *snac, const char *id, const char *content, int size,
  1368. xs_str **etag)
  1369. /* adds something to the history */
  1370. {
  1371. xs *fn = _history_fn(snac, id);
  1372. FILE *f;
  1373. if (fn && (f = fopen(fn, "w")) != NULL) {
  1374. fwrite(content, size, 1, f);
  1375. fclose(f);
  1376. if (etag) {
  1377. double tm = mtime(fn);
  1378. *etag = xs_fmt("W/\"snac-%.0lf\"", tm);
  1379. }
  1380. }
  1381. }
  1382. int history_get(snac *snac, const char *id, xs_str **content, int *size,
  1383. const char *inm, xs_str **etag)
  1384. {
  1385. xs *fn = _history_fn(snac, id);
  1386. return _load_raw_file(fn, content, size, inm, etag);
  1387. }
  1388. int history_del(snac *snac, const char *id)
  1389. {
  1390. xs *fn = _history_fn(snac, id);
  1391. if (fn)
  1392. return unlink(fn);
  1393. else
  1394. return -1;
  1395. }
  1396. xs_list *history_list(snac *snac)
  1397. {
  1398. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  1399. return xs_glob(spec, 1, 1);
  1400. }
  1401. void lastlog_write(snac *snac, const char *source)
  1402. /* writes the last time the user logged in */
  1403. {
  1404. xs *fn = xs_fmt("%s/lastlog.txt", snac->basedir);
  1405. FILE *f;
  1406. if ((f = fopen(fn, "w")) != NULL) {
  1407. fprintf(f, "%lf %s\n", ftime(), source);
  1408. fclose(f);
  1409. }
  1410. }
  1411. /** inbox collection **/
  1412. void inbox_add(const char *inbox)
  1413. /* collects a shared inbox */
  1414. {
  1415. /* don't collect ourselves */
  1416. if (xs_startswith(inbox, srv_baseurl))
  1417. return;
  1418. xs *md5 = xs_md5_hex(inbox, strlen(inbox));
  1419. xs *fn = xs_fmt("%s/inbox/%s", srv_basedir, md5);
  1420. FILE *f;
  1421. if ((f = fopen(fn, "w")) != NULL) {
  1422. fprintf(f, "%s\n", inbox);
  1423. fclose(f);
  1424. }
  1425. }
  1426. void inbox_add_by_actor(const xs_dict *actor)
  1427. /* collects an actor's shared inbox, if it has one */
  1428. {
  1429. char *v;
  1430. if (!xs_is_null(v = xs_dict_get(actor, "endpoints")) &&
  1431. !xs_is_null(v = xs_dict_get(v, "sharedInbox"))) {
  1432. /* only collect this inbox if its instance is not blocked */
  1433. if (!is_instance_blocked(v))
  1434. inbox_add(v);
  1435. }
  1436. }
  1437. xs_list *inbox_list(void)
  1438. /* returns the collected inboxes as a list */
  1439. {
  1440. xs_list *ibl = xs_list_new();
  1441. xs *spec = xs_fmt("%s/inbox/" "*", srv_basedir);
  1442. xs *files = xs_glob(spec, 0, 0);
  1443. xs_list *p = files;
  1444. xs_val *v;
  1445. while (xs_list_iter(&p, &v)) {
  1446. FILE *f;
  1447. if ((f = fopen(v, "r")) != NULL) {
  1448. xs *line = xs_readline(f);
  1449. if (line) {
  1450. line = xs_strip_i(line);
  1451. ibl = xs_list_append(ibl, line);
  1452. }
  1453. fclose(f);
  1454. }
  1455. }
  1456. return ibl;
  1457. }
  1458. /** instance-wide operations **/
  1459. xs_str *_instance_block_fn(const char *instance)
  1460. {
  1461. xs *s1 = xs_replace(instance, "https:/" "/", "");
  1462. xs *l = xs_split(s1, "/");
  1463. char *p = xs_list_get(l, 0);
  1464. xs *md5 = xs_md5_hex(p, strlen(p));
  1465. return xs_fmt("%s/block/%s", srv_basedir, md5);
  1466. }
  1467. int is_instance_blocked(const char *instance)
  1468. {
  1469. xs *fn = _instance_block_fn(instance);
  1470. return !!(mtime(fn) != 0.0);
  1471. }
  1472. int instance_block(const char *instance)
  1473. /* blocks a full instance */
  1474. {
  1475. int ret;
  1476. /* create the subdir */
  1477. xs *dir = xs_fmt("%s/block/", srv_basedir);
  1478. mkdirx(dir);
  1479. if (!is_instance_blocked(instance)) {
  1480. xs *fn = _instance_block_fn(instance);
  1481. FILE *f;
  1482. if ((f = fopen(fn, "w")) != NULL) {
  1483. fprintf(f, "%s\n", instance);
  1484. fclose(f);
  1485. ret = 0;
  1486. }
  1487. else
  1488. ret = -1;
  1489. }
  1490. else
  1491. ret = -2;
  1492. return ret;
  1493. }
  1494. int instance_unblock(const char *instance)
  1495. /* unblocks a full instance */
  1496. {
  1497. int ret;
  1498. if (is_instance_blocked(instance)) {
  1499. xs *fn = _instance_block_fn(instance);
  1500. ret = unlink(fn);
  1501. }
  1502. else
  1503. ret = -2;
  1504. return ret;
  1505. }
  1506. /** content filtering **/
  1507. int content_check(const char *file, const xs_dict *msg)
  1508. /* checks if a message's content matches any of the regexes in file */
  1509. /* file format: one regex per line */
  1510. {
  1511. xs *fn = xs_fmt("%s/%s", srv_basedir, file);
  1512. FILE *f;
  1513. int r = 0;
  1514. char *v = xs_dict_get(msg, "content");
  1515. if (xs_type(v) == XSTYPE_STRING && *v) {
  1516. if ((f = fopen(fn, "r")) != NULL) {
  1517. srv_debug(1, xs_fmt("content_check: loading regexes from %s", fn));
  1518. /* massage content (strip HTML tags, etc.) */
  1519. xs *c = xs_regex_replace(v, "<[^>]+>", " ");
  1520. c = xs_regex_replace_i(c, " {2,}", " ");
  1521. c = xs_tolower_i(c);
  1522. while (!r && !feof(f)) {
  1523. xs *rx = xs_strip_i(xs_readline(f));
  1524. if (*rx) {
  1525. xs *l = xs_regex_select_n(c, rx, 1);
  1526. if (xs_list_len(l)) {
  1527. srv_debug(1, xs_fmt("content_check: match for '%s'", rx));
  1528. r = 1;
  1529. }
  1530. }
  1531. }
  1532. fclose(f);
  1533. }
  1534. }
  1535. return r;
  1536. }
  1537. /** notifications **/
  1538. xs_str *notify_check_time(snac *snac, int reset)
  1539. /* gets or resets the latest notification check time */
  1540. {
  1541. xs_str *t = NULL;
  1542. xs *fn = xs_fmt("%s/notifydate.txt", snac->basedir);
  1543. FILE *f;
  1544. if (reset) {
  1545. if ((f = fopen(fn, "w")) != NULL) {
  1546. t = tid(0);
  1547. fprintf(f, "%s\n", t);
  1548. fclose(f);
  1549. }
  1550. }
  1551. else {
  1552. if ((f = fopen(fn, "r")) != NULL) {
  1553. t = xs_readline(f);
  1554. fclose(f);
  1555. }
  1556. else
  1557. /* never set before */
  1558. t = xs_fmt("%16.6f", 0.0);
  1559. }
  1560. return t;
  1561. }
  1562. void notify_add(snac *snac, const char *type, const char *utype,
  1563. const char *actor, const char *objid)
  1564. /* adds a new notification */
  1565. {
  1566. xs *ntid = tid(0);
  1567. xs *fn = xs_fmt("%s/notify/", snac->basedir);
  1568. xs *date = xs_str_utctime(0, ISO_DATE_SPEC);
  1569. FILE *f;
  1570. /* create the directory */
  1571. mkdirx(fn);
  1572. fn = xs_str_cat(fn, ntid);
  1573. fn = xs_str_cat(fn, ".json");
  1574. xs *noti = xs_dict_new();
  1575. noti = xs_dict_append(noti, "id", ntid);
  1576. noti = xs_dict_append(noti, "type", type);
  1577. noti = xs_dict_append(noti, "utype", utype);
  1578. noti = xs_dict_append(noti, "actor", actor);
  1579. noti = xs_dict_append(noti, "date", date);
  1580. if (!xs_is_null(objid))
  1581. noti = xs_dict_append(noti, "objid", objid);
  1582. if ((f = fopen(fn, "w")) != NULL) {
  1583. xs_json_dump(noti, 4, f);
  1584. fclose(f);
  1585. }
  1586. /* add it to the index if it already exists */
  1587. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  1588. if (mtime(idx) != 0.0) {
  1589. pthread_mutex_lock(&data_mutex);
  1590. if ((f = fopen(idx, "a")) != NULL) {
  1591. fprintf(f, "%-32s\n", ntid);
  1592. fclose(f);
  1593. }
  1594. pthread_mutex_unlock(&data_mutex);
  1595. }
  1596. }
  1597. xs_dict *notify_get(snac *snac, const char *id)
  1598. /* gets a notification */
  1599. {
  1600. /* base file */
  1601. xs *fn = xs_fmt("%s/notify/%s", snac->basedir, id);
  1602. /* strip spaces and add extension */
  1603. fn = xs_strip_i(fn);
  1604. fn = xs_str_cat(fn, ".json");
  1605. FILE *f;
  1606. xs_dict *out = NULL;
  1607. if ((f = fopen(fn, "r")) != NULL) {
  1608. out = xs_json_load(f);
  1609. fclose(f);
  1610. }
  1611. return out;
  1612. }
  1613. xs_list *notify_list(snac *snac, int skip, int show)
  1614. /* returns a list of notification ids */
  1615. {
  1616. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  1617. if (mtime(idx) == 0.0) {
  1618. /* create the index from scratch */
  1619. FILE *f;
  1620. pthread_mutex_lock(&data_mutex);
  1621. if ((f = fopen(idx, "w")) != NULL) {
  1622. xs *spec = xs_fmt("%s/notify/" "*.json", snac->basedir);
  1623. xs *lst = xs_glob(spec, 1, 0);
  1624. xs_list *p = lst;
  1625. char *v;
  1626. while (xs_list_iter(&p, &v)) {
  1627. char *p = strrchr(v, '.');
  1628. if (p) {
  1629. *p = '\0';
  1630. fprintf(f, "%-32s\n", v);
  1631. }
  1632. }
  1633. fclose(f);
  1634. }
  1635. pthread_mutex_unlock(&data_mutex);
  1636. }
  1637. return index_list_desc(idx, skip, show);
  1638. }
  1639. int notify_new_num(snac *snac)
  1640. /* counts the number of new notifications */
  1641. {
  1642. xs *t = notify_check_time(snac, 0);
  1643. xs *lst = notify_list(snac, 0, XS_ALL);
  1644. int cnt = 0;
  1645. xs_list *p = lst;
  1646. xs_str *v;
  1647. while (xs_list_iter(&p, &v)) {
  1648. xs *id = xs_strip_i(xs_dup(v));
  1649. /* old? count no more */
  1650. if (strcmp(id, t) < 0)
  1651. break;
  1652. cnt++;
  1653. }
  1654. return cnt;
  1655. }
  1656. void notify_clear(snac *snac)
  1657. /* clears all notifications */
  1658. {
  1659. xs *spec = xs_fmt("%s/notify/" "*", snac->basedir);
  1660. xs *lst = xs_glob(spec, 0, 0);
  1661. xs_list *p = lst;
  1662. xs_str *v;
  1663. while (xs_list_iter(&p, &v))
  1664. unlink(v);
  1665. xs *idx = xs_fmt("%s/notify.idx", snac->basedir);
  1666. if (mtime(idx) != 0.0) {
  1667. pthread_mutex_lock(&data_mutex);
  1668. truncate(idx, 0);
  1669. pthread_mutex_unlock(&data_mutex);
  1670. }
  1671. }
  1672. /** the queue **/
  1673. static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
  1674. /* writes safely to the queue */
  1675. {
  1676. xs *tfn = xs_fmt("%s.tmp", fn);
  1677. FILE *f;
  1678. if ((f = fopen(tfn, "w")) != NULL) {
  1679. xs_json_dump(msg, 4, f);
  1680. fclose(f);
  1681. rename(tfn, fn);
  1682. }
  1683. return msg;
  1684. }
  1685. static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
  1686. /* creates a queue message */
  1687. {
  1688. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  1689. xs *ntid = tid(retries * 60 * qrt);
  1690. xs *rn = xs_number_new(retries);
  1691. xs_dict *qmsg = xs_dict_new();
  1692. qmsg = xs_dict_append(qmsg, "type", type);
  1693. qmsg = xs_dict_append(qmsg, "message", msg);
  1694. qmsg = xs_dict_append(qmsg, "retries", rn);
  1695. qmsg = xs_dict_append(qmsg, "ntid", ntid);
  1696. return qmsg;
  1697. }
  1698. void enqueue_input(snac *snac, const xs_dict *msg, const xs_dict *req, int retries)
  1699. /* enqueues an input message */
  1700. {
  1701. xs *qmsg = _new_qmsg("input", msg, retries);
  1702. char *ntid = xs_dict_get(qmsg, "ntid");
  1703. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1704. qmsg = xs_dict_append(qmsg, "req", req);
  1705. qmsg = _enqueue_put(fn, qmsg);
  1706. snac_debug(snac, 1, xs_fmt("enqueue_input %s", xs_dict_get(msg, "id")));
  1707. }
  1708. void enqueue_shared_input(const xs_dict *msg, const xs_dict *req, int retries)
  1709. /* enqueues an input message from the shared input */
  1710. {
  1711. xs *qmsg = _new_qmsg("input", msg, retries);
  1712. char *ntid = xs_dict_get(qmsg, "ntid");
  1713. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1714. qmsg = xs_dict_append(qmsg, "req", req);
  1715. qmsg = _enqueue_put(fn, qmsg);
  1716. srv_debug(1, xs_fmt("enqueue_shared_input %s", xs_dict_get(msg, "id")));
  1717. }
  1718. void enqueue_output_raw(const char *keyid, const char *seckey,
  1719. xs_dict *msg, xs_str *inbox, int retries, int p_status)
  1720. /* enqueues an output message to an inbox */
  1721. {
  1722. xs *qmsg = _new_qmsg("output", msg, retries);
  1723. char *ntid = xs_dict_get(qmsg, "ntid");
  1724. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1725. xs *ns = xs_number_new(p_status);
  1726. qmsg = xs_dict_append(qmsg, "p_status", ns);
  1727. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1728. qmsg = xs_dict_append(qmsg, "keyid", keyid);
  1729. qmsg = xs_dict_append(qmsg, "seckey", seckey);
  1730. /* if it's to be sent right now, bypass the disk queue and post the job */
  1731. if (retries == 0 && p_state != NULL)
  1732. job_post(qmsg, 0);
  1733. else {
  1734. qmsg = _enqueue_put(fn, qmsg);
  1735. srv_debug(1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1736. }
  1737. }
  1738. void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries, int p_status)
  1739. /* enqueues an output message to an inbox */
  1740. {
  1741. if (xs_startswith(inbox, snac->actor)) {
  1742. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  1743. return;
  1744. }
  1745. char *seckey = xs_dict_get(snac->key, "secret");
  1746. enqueue_output_raw(snac->actor, seckey, msg, inbox, retries, p_status);
  1747. }
  1748. void enqueue_output_by_actor(snac *snac, xs_dict *msg, const xs_str *actor, int retries)
  1749. /* enqueues an output message for an actor */
  1750. {
  1751. xs *inbox = get_actor_inbox(actor);
  1752. if (!xs_is_null(inbox))
  1753. enqueue_output(snac, msg, inbox, retries, 0);
  1754. else
  1755. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1756. }
  1757. void enqueue_email(xs_str *msg, int retries)
  1758. /* enqueues an email message to be sent */
  1759. {
  1760. xs *qmsg = _new_qmsg("email", msg, retries);
  1761. char *ntid = xs_dict_get(qmsg, "ntid");
  1762. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1763. qmsg = _enqueue_put(fn, qmsg);
  1764. srv_debug(1, xs_fmt("enqueue_email %d", retries));
  1765. }
  1766. void enqueue_telegram(const xs_str *msg, const char *bot, const char *chat_id)
  1767. /* enqueues a message to be sent via Telegram */
  1768. {
  1769. xs *qmsg = _new_qmsg("telegram", msg, 0);
  1770. char *ntid = xs_dict_get(qmsg, "ntid");
  1771. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1772. qmsg = xs_dict_append(qmsg, "bot", bot);
  1773. qmsg = xs_dict_append(qmsg, "chat_id", chat_id);
  1774. qmsg = _enqueue_put(fn, qmsg);
  1775. srv_debug(1, xs_fmt("enqueue_telegram %s %s", bot, chat_id));
  1776. }
  1777. void enqueue_ntfy(const xs_str *msg, const char *ntfy_server, const char *ntfy_token)
  1778. /* enqueues a message to be sent via ntfy */
  1779. {
  1780. xs *qmsg = _new_qmsg("ntfy", msg, 0);
  1781. char *ntid = xs_dict_get(qmsg, "ntid");
  1782. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1783. qmsg = xs_dict_append(qmsg, "ntfy_server", ntfy_server);
  1784. qmsg = xs_dict_append(qmsg, "ntfy_token", ntfy_token);
  1785. qmsg = _enqueue_put(fn, qmsg);
  1786. srv_debug(1, xs_fmt("enqueue_ntfy %s %s", ntfy_server, ntfy_token));
  1787. }
  1788. void enqueue_message(snac *snac, const xs_dict *msg)
  1789. /* enqueues an output message */
  1790. {
  1791. xs *qmsg = _new_qmsg("message", msg, 0);
  1792. char *ntid = xs_dict_get(qmsg, "ntid");
  1793. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1794. qmsg = _enqueue_put(fn, qmsg);
  1795. snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
  1796. }
  1797. void enqueue_close_question(snac *user, const char *id, int end_secs)
  1798. /* enqueues the closing of a question */
  1799. {
  1800. xs *qmsg = _new_qmsg("close_question", id, 0);
  1801. xs *ntid = tid(end_secs);
  1802. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  1803. qmsg = xs_dict_set(qmsg, "ntid", ntid);
  1804. qmsg = _enqueue_put(fn, qmsg);
  1805. snac_debug(user, 0, xs_fmt("enqueue_close_question %s", id));
  1806. }
  1807. void enqueue_verify_links(snac *user)
  1808. /* enqueues a link verification */
  1809. {
  1810. xs *qmsg = _new_qmsg("verify_links", "", 0);
  1811. char *ntid = xs_dict_get(qmsg, "ntid");
  1812. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  1813. qmsg = _enqueue_put(fn, qmsg);
  1814. snac_debug(user, 1, xs_fmt("enqueue_verify_links %s", user->actor));
  1815. }
  1816. void enqueue_actor_refresh(snac *user, const char *actor)
  1817. /* enqueues an actor refresh */
  1818. {
  1819. xs *qmsg = _new_qmsg("actor_refresh", "", 0);
  1820. char *ntid = xs_dict_get(qmsg, "ntid");
  1821. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  1822. qmsg = xs_dict_append(qmsg, "actor", actor);
  1823. qmsg = _enqueue_put(fn, qmsg);
  1824. snac_debug(user, 1, xs_fmt("enqueue_actor_refresh %s", actor));
  1825. }
  1826. void enqueue_request_replies(snac *user, const char *id)
  1827. /* enqueues a request for the replies of a message */
  1828. {
  1829. /* test first if this precise request is already in the queue */
  1830. xs *queue = user_queue(user);
  1831. xs_list *p = queue;
  1832. xs_str *v;
  1833. while (xs_list_iter(&p, &v)) {
  1834. xs *q_item = queue_get(v);
  1835. if (q_item != NULL) {
  1836. const char *type = xs_dict_get(q_item, "type");
  1837. const char *msg = xs_dict_get(q_item, "message");
  1838. if (type && msg && strcmp(type, "request_replies") == 0 && strcmp(msg, id) == 0) {
  1839. /* don't requeue */
  1840. snac_debug(user, 1, xs_fmt("enqueue_request_replies already here %s", id));
  1841. return;
  1842. }
  1843. }
  1844. }
  1845. /* not there; enqueue the request with a small delay */
  1846. xs *qmsg = _new_qmsg("request_replies", id, 0);
  1847. xs *ntid = tid(10);
  1848. xs *fn = xs_fmt("%s/queue/%s.json", user->basedir, ntid);
  1849. qmsg = xs_dict_set(qmsg, "ntid", ntid);
  1850. qmsg = _enqueue_put(fn, qmsg);
  1851. snac_debug(user, 2, xs_fmt("enqueue_request_replies %s", id));
  1852. }
  1853. int was_question_voted(snac *user, const char *id)
  1854. /* returns true if the user voted in this poll */
  1855. {
  1856. xs *children = object_children(id);
  1857. int voted = 0;
  1858. xs_list *p;
  1859. xs_str *md5;
  1860. p = children;
  1861. while (xs_list_iter(&p, &md5)) {
  1862. xs *obj = NULL;
  1863. if (valid_status(object_get_by_md5(md5, &obj))) {
  1864. char *atto = get_atto(obj);
  1865. if (atto && strcmp(atto, user->actor) == 0 &&
  1866. !xs_is_null(xs_dict_get(obj, "name"))) {
  1867. voted = 1;
  1868. break;
  1869. }
  1870. }
  1871. }
  1872. return voted;
  1873. }
  1874. xs_list *user_queue(snac *snac)
  1875. /* returns a list with filenames that can be dequeued */
  1876. {
  1877. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1878. xs_list *list = xs_list_new();
  1879. time_t t = time(NULL);
  1880. xs_list *p;
  1881. xs_val *v;
  1882. xs *fns = xs_glob(spec, 0, 0);
  1883. p = fns;
  1884. while (xs_list_iter(&p, &v)) {
  1885. /* get the retry time from the basename */
  1886. char *bn = strrchr(v, '/');
  1887. time_t t2 = atol(bn + 1);
  1888. if (t2 > t)
  1889. snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
  1890. else {
  1891. list = xs_list_append(list, v);
  1892. snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
  1893. }
  1894. }
  1895. return list;
  1896. }
  1897. xs_list *queue(void)
  1898. /* returns a list with filenames that can be dequeued */
  1899. {
  1900. xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
  1901. xs_list *list = xs_list_new();
  1902. time_t t = time(NULL);
  1903. xs_list *p;
  1904. xs_val *v;
  1905. xs *fns = xs_glob(spec, 0, 0);
  1906. p = fns;
  1907. while (xs_list_iter(&p, &v)) {
  1908. /* get the retry time from the basename */
  1909. char *bn = strrchr(v, '/');
  1910. time_t t2 = atol(bn + 1);
  1911. if (t2 > t)
  1912. srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1913. else {
  1914. list = xs_list_append(list, v);
  1915. srv_debug(2, xs_fmt("queue ready for %s", v));
  1916. }
  1917. }
  1918. return list;
  1919. }
  1920. xs_dict *queue_get(const char *fn)
  1921. /* gets a file from a queue */
  1922. {
  1923. FILE *f;
  1924. xs_dict *obj = NULL;
  1925. if ((f = fopen(fn, "r")) != NULL) {
  1926. obj = xs_json_load(f);
  1927. fclose(f);
  1928. }
  1929. return obj;
  1930. }
  1931. xs_dict *dequeue(const char *fn)
  1932. /* dequeues a message */
  1933. {
  1934. xs_dict *obj = queue_get(fn);
  1935. unlink(fn);
  1936. return obj;
  1937. }
  1938. /** the purge **/
  1939. static int _purge_file(const char *fn, time_t mt)
  1940. /* purge fn if it's older than days */
  1941. {
  1942. int ret = 0;
  1943. if (mtime(fn) < mt) {
  1944. /* older than the minimum time: delete it */
  1945. unlink(fn);
  1946. srv_debug(2, xs_fmt("purged %s", fn));
  1947. ret = 1;
  1948. }
  1949. return ret;
  1950. }
  1951. static void _purge_dir(const char *dir, int days)
  1952. /* purges all files in a directory older than days */
  1953. {
  1954. int cnt = 0;
  1955. if (days) {
  1956. time_t mt = time(NULL) - days * 24 * 3600;
  1957. xs *spec = xs_fmt("%s/" "*", dir);
  1958. xs *list = xs_glob(spec, 0, 0);
  1959. xs_list *p;
  1960. xs_str *v;
  1961. p = list;
  1962. while (xs_list_iter(&p, &v))
  1963. cnt += _purge_file(v, mt);
  1964. srv_debug(1, xs_fmt("purge: %s %d", dir, cnt));
  1965. }
  1966. }
  1967. static void _purge_user_subdir(snac *snac, const char *subdir, int days)
  1968. /* purges all files in a user subdir older than days */
  1969. {
  1970. xs *u_subdir = xs_fmt("%s/%s", snac->basedir, subdir);
  1971. _purge_dir(u_subdir, days);
  1972. }
  1973. void purge_server(void)
  1974. /* purge global server data */
  1975. {
  1976. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1977. xs *dirs = xs_glob(spec, 0, 0);
  1978. xs_list *p;
  1979. xs_str *v;
  1980. int cnt = 0;
  1981. int icnt = 0;
  1982. time_t mt = time(NULL) - 7 * 24 * 3600;
  1983. p = dirs;
  1984. while (xs_list_iter(&p, &v)) {
  1985. xs_list *p2;
  1986. xs_str *v2;
  1987. {
  1988. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1989. xs *files = xs_glob(spec2, 0, 0);
  1990. p2 = files;
  1991. while (xs_list_iter(&p2, &v2)) {
  1992. int n_link;
  1993. /* old and with no hard links? */
  1994. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1995. xs *s1 = xs_replace(v2, ".json", "");
  1996. xs *l = xs_split(s1, "/");
  1997. char *md5 = xs_list_get(l, -1);
  1998. object_del_by_md5(md5);
  1999. cnt++;
  2000. }
  2001. }
  2002. }
  2003. {
  2004. /* look for stray indexes */
  2005. xs *speci = xs_fmt("%s/" "*_?.idx", v);
  2006. xs *idxfs = xs_glob(speci, 0, 0);
  2007. p2 = idxfs;
  2008. while (xs_list_iter(&p2, &v2)) {
  2009. /* old enough to consider? */
  2010. if (mtime(v2) < mt) {
  2011. /* check if the indexed object is here */
  2012. xs *o = xs_dup(v2);
  2013. char *ext = strchr(o, '_');
  2014. if (ext) {
  2015. *ext = '\0';
  2016. o = xs_str_cat(o, ".json");
  2017. if (mtime(o) == 0.0) {
  2018. /* delete */
  2019. unlink(v2);
  2020. srv_debug(1, xs_fmt("purged %s", v2));
  2021. icnt++;
  2022. }
  2023. }
  2024. }
  2025. }
  2026. }
  2027. }
  2028. /* purge collected inboxes */
  2029. xs *ib_dir = xs_fmt("%s/inbox", srv_basedir);
  2030. _purge_dir(ib_dir, 7);
  2031. /* purge the instance timeline */
  2032. xs *itl_fn = xs_fmt("%s/public.idx", srv_basedir);
  2033. int itl_gc = index_gc(itl_fn);
  2034. /* purge tag indexes */
  2035. xs *tag_spec = xs_fmt("%s/tag/??", srv_basedir);
  2036. xs *tag_dirs = xs_glob(tag_spec, 0, 0);
  2037. p = tag_dirs;
  2038. int tag_gc = 0;
  2039. while (xs_list_iter(&p, &v)) {
  2040. xs *spec2 = xs_fmt("%s/" "*.idx", v);
  2041. xs *files = xs_glob(spec2, 0, 0);
  2042. xs_list *p2;
  2043. xs_str *v2;
  2044. p2 = files;
  2045. while (xs_list_iter(&p2, &v2)) {
  2046. tag_gc += index_gc(v2);
  2047. xs *bak = xs_fmt("%s.bak", v2);
  2048. unlink(bak);
  2049. if (index_len(v2) == 0) {
  2050. /* there are no longer any entry with this tag;
  2051. purge it completely */
  2052. unlink(v2);
  2053. xs *dottag = xs_replace(v2, ".idx", ".tag");
  2054. unlink(dottag);
  2055. }
  2056. }
  2057. }
  2058. srv_debug(1, xs_fmt("purge: global "
  2059. "(obj: %d, idx: %d, itl: %d, tag: %d)", cnt, icnt, itl_gc, tag_gc));
  2060. }
  2061. void purge_user(snac *snac)
  2062. /* do the purge for this user */
  2063. {
  2064. int priv_days, pub_days, user_days = 0;
  2065. char *v;
  2066. int n;
  2067. priv_days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  2068. pub_days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  2069. if ((v = xs_dict_get(snac->config_o, "purge_days")) != NULL ||
  2070. (v = xs_dict_get(snac->config, "purge_days")) != NULL)
  2071. user_days = xs_number_get(v);
  2072. if (user_days) {
  2073. /* override admin settings only if they are lesser */
  2074. if (priv_days == 0 || user_days < priv_days)
  2075. priv_days = user_days;
  2076. if (pub_days == 0 || user_days < pub_days)
  2077. pub_days = user_days;
  2078. }
  2079. _purge_user_subdir(snac, "hidden", priv_days);
  2080. _purge_user_subdir(snac, "private", priv_days);
  2081. _purge_user_subdir(snac, "public", pub_days);
  2082. const char *idxs[] = { "followers.idx", "private.idx", "public.idx", "pinned.idx", NULL };
  2083. for (n = 0; idxs[n]; n++) {
  2084. xs *idx = xs_fmt("%s/%s", snac->basedir, idxs[n]);
  2085. int gc = index_gc(idx);
  2086. srv_debug(1, xs_fmt("purge: %s %d", idx, gc));
  2087. }
  2088. /* unrelated to purging, but it's a janitorial process, so what the hell */
  2089. verify_links(snac);
  2090. }
  2091. void purge_all(void)
  2092. /* purge all users */
  2093. {
  2094. snac snac;
  2095. xs *list = user_list();
  2096. char *p, *uid;
  2097. p = list;
  2098. while (xs_list_iter(&p, &uid)) {
  2099. if (user_open(&snac, uid)) {
  2100. purge_user(&snac);
  2101. user_free(&snac);
  2102. }
  2103. }
  2104. purge_server();
  2105. #ifndef NO_MASTODON_API
  2106. mastoapi_purge();
  2107. #endif
  2108. }
  2109. /** archive **/
  2110. void srv_archive(const char *direction, const char *url, xs_dict *req,
  2111. const char *payload, int p_size,
  2112. int status, xs_dict *headers,
  2113. const char *body, int b_size)
  2114. /* archives a connection */
  2115. {
  2116. /* obsessive archiving */
  2117. xs *date = tid(0);
  2118. xs *dir = xs_fmt("%s/archive/%s_%s", srv_basedir, date, direction);
  2119. FILE *f;
  2120. if (mkdirx(dir) != -1) {
  2121. xs *meta_fn = xs_fmt("%s/_META", dir);
  2122. if ((f = fopen(meta_fn, "w")) != NULL) {
  2123. xs *j1 = xs_json_dumps(req, 4);
  2124. xs *j2 = xs_json_dumps(headers, 4);
  2125. fprintf(f, "dir: %s\n", direction);
  2126. if (url)
  2127. fprintf(f, "url: %s\n", url);
  2128. fprintf(f, "req: %s\n", j1);
  2129. fprintf(f, "p_size: %d\n", p_size);
  2130. fprintf(f, "status: %d\n", status);
  2131. fprintf(f, "response: %s\n", j2);
  2132. fprintf(f, "b_size: %d\n", b_size);
  2133. fclose(f);
  2134. }
  2135. if (p_size && payload) {
  2136. xs *payload_fn = NULL;
  2137. xs *payload_fn_raw = NULL;
  2138. char *v = xs_dict_get(req, "content-type");
  2139. if (v && xs_str_in(v, "json") != -1) {
  2140. payload_fn = xs_fmt("%s/payload.json", dir);
  2141. if ((f = fopen(payload_fn, "w")) != NULL) {
  2142. xs *v1 = xs_json_loads(payload);
  2143. xs *j1 = NULL;
  2144. if (v1 != NULL)
  2145. j1 = xs_json_dumps(v1, 4);
  2146. if (j1 != NULL)
  2147. fwrite(j1, strlen(j1), 1, f);
  2148. else
  2149. fwrite(payload, p_size, 1, f);
  2150. fclose(f);
  2151. }
  2152. }
  2153. payload_fn_raw = xs_fmt("%s/payload", dir);
  2154. if ((f = fopen(payload_fn_raw, "w")) != NULL) {
  2155. fwrite(payload, p_size, 1, f);
  2156. fclose(f);
  2157. }
  2158. }
  2159. if (b_size && body) {
  2160. xs *body_fn = NULL;
  2161. char *v = xs_dict_get(headers, "content-type");
  2162. if (v && xs_str_in(v, "json") != -1) {
  2163. body_fn = xs_fmt("%s/body.json", dir);
  2164. if ((f = fopen(body_fn, "w")) != NULL) {
  2165. xs *v1 = xs_json_loads(body);
  2166. xs *j1 = NULL;
  2167. if (v1 != NULL)
  2168. j1 = xs_json_dumps(v1, 4);
  2169. if (j1 != NULL)
  2170. fwrite(j1, strlen(j1), 1, f);
  2171. else
  2172. fwrite(body, b_size, 1, f);
  2173. fclose(f);
  2174. }
  2175. }
  2176. else {
  2177. body_fn = xs_fmt("%s/body", dir);
  2178. if ((f = fopen(body_fn, "w")) != NULL) {
  2179. fwrite(body, b_size, 1, f);
  2180. fclose(f);
  2181. }
  2182. }
  2183. }
  2184. }
  2185. }
  2186. void srv_archive_error(const char *prefix, const xs_str *err,
  2187. const xs_dict *req, const xs_val *data)
  2188. /* archives an error */
  2189. {
  2190. xs *ntid = tid(0);
  2191. xs *fn = xs_fmt("%s/error/%s_%s", srv_basedir, ntid, prefix);
  2192. FILE *f;
  2193. if ((f = fopen(fn, "w")) != NULL) {
  2194. fprintf(f, "Error: %s\n", err);
  2195. if (req) {
  2196. fprintf(f, "Request headers:\n");
  2197. xs_json_dump(req, 4, f);
  2198. fprintf(f, "\n");
  2199. }
  2200. if (data) {
  2201. fprintf(f, "Data:\n");
  2202. if (xs_type(data) == XSTYPE_LIST || xs_type(data) == XSTYPE_DICT) {
  2203. xs_json_dump(data, 4, f);
  2204. }
  2205. else
  2206. fprintf(f, "%s", data);
  2207. fprintf(f, "\n");
  2208. }
  2209. fclose(f);
  2210. }
  2211. }
  2212. void srv_archive_qitem(char *prefix, xs_dict *q_item)
  2213. /* archives a q_item in the error folder */
  2214. {
  2215. xs *ntid = tid(0);
  2216. xs *fn = xs_fmt("%s/error/%s_qitem_%s", srv_basedir, ntid, prefix);
  2217. FILE *f;
  2218. if ((f = fopen(fn, "w")) != NULL) {
  2219. xs_json_dump(q_item, 4, f);
  2220. fclose(f);
  2221. }
  2222. }