data.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590
  1. /* snac - A simple, minimalistic ActivityPub instance */
  2. /* copyright (c) 2022 - 2023 grunfink / MIT license */
  3. #include "xs.h"
  4. #include "xs_io.h"
  5. #include "xs_json.h"
  6. #include "xs_openssl.h"
  7. #include "xs_glob.h"
  8. #include "xs_set.h"
  9. #include "snac.h"
  10. #include <time.h>
  11. #include <sys/stat.h>
  12. #include <sys/file.h>
  13. #include <fcntl.h>
  14. double disk_layout = 2.7;
  15. int snac_upgrade(d_char **error);
  16. int srv_open(char *basedir, int auto_upgrade)
  17. /* opens a server */
  18. {
  19. int ret = 0;
  20. xs *cfg_file = NULL;
  21. FILE *f;
  22. d_char *error = NULL;
  23. srv_basedir = xs_str_new(basedir);
  24. if (xs_endswith(srv_basedir, "/"))
  25. srv_basedir = xs_crop_i(srv_basedir, 0, -1);
  26. cfg_file = xs_fmt("%s/server.json", basedir);
  27. if ((f = fopen(cfg_file, "r")) == NULL)
  28. error = xs_fmt("ERROR: cannot opening '%s'", cfg_file);
  29. else {
  30. xs *cfg_data;
  31. /* read full config file */
  32. cfg_data = xs_readall(f);
  33. fclose(f);
  34. /* parse */
  35. srv_config = xs_json_loads(cfg_data);
  36. if (srv_config == NULL)
  37. error = xs_fmt("ERROR: cannot parse '%s'", cfg_file);
  38. else {
  39. char *host;
  40. char *prefix;
  41. char *dbglvl;
  42. host = xs_dict_get(srv_config, "host");
  43. prefix = xs_dict_get(srv_config, "prefix");
  44. dbglvl = xs_dict_get(srv_config, "dbglevel");
  45. if (host == NULL || prefix == NULL)
  46. error = xs_str_new("ERROR: cannot get server data");
  47. else {
  48. srv_baseurl = xs_fmt("https://%s%s", host, prefix);
  49. dbglevel = (int) xs_number_get(dbglvl);
  50. if ((dbglvl = getenv("DEBUG")) != NULL) {
  51. dbglevel = atoi(dbglvl);
  52. error = xs_fmt("DEBUG level set to %d from environment", dbglevel);
  53. }
  54. if (auto_upgrade)
  55. ret = snac_upgrade(&error);
  56. else {
  57. if (xs_number_get(xs_dict_get(srv_config, "layout")) < disk_layout)
  58. error = xs_fmt("ERROR: disk layout changed - execute 'snac upgrade' first");
  59. else
  60. ret = 1;
  61. }
  62. }
  63. }
  64. }
  65. if (error != NULL)
  66. srv_log(error);
  67. /* create the queue/ subdir, just in case */
  68. xs *qdir = xs_fmt("%s/queue", srv_basedir);
  69. mkdir(qdir, 0755);
  70. #ifdef __OpenBSD__
  71. char *v = xs_dict_get(srv_config, "disable_openbsd_security");
  72. if (v && xs_type(v) == XSTYPE_TRUE) {
  73. srv_debug(1, xs_dup("OpenBSD security disabled by admin"));
  74. }
  75. else {
  76. srv_debug(1, xs_fmt("Calling unveil()"));
  77. unveil(basedir, "rwc");
  78. unveil("/usr/sbin/sendmail", "x");
  79. unveil("/etc/resolv.conf", "r");
  80. unveil("/etc/hosts", "r");
  81. unveil("/etc/ssl/openssl.cnf", "r");
  82. unveil("/etc/ssl/cert.pem", "r");
  83. unveil("/usr/share/zoneinfo", "r");
  84. unveil(NULL, NULL);
  85. srv_debug(1, xs_fmt("Calling pledge()"));
  86. pledge("stdio rpath wpath cpath flock inet proc exec dns", NULL);
  87. }
  88. #endif /* __OpenBSD__ */
  89. return ret;
  90. }
  91. void srv_free(void)
  92. {
  93. xs_free(srv_basedir);
  94. xs_free(srv_config);
  95. xs_free(srv_baseurl);
  96. }
  97. void user_free(snac *snac)
  98. /* frees a user snac */
  99. {
  100. xs_free(snac->uid);
  101. xs_free(snac->basedir);
  102. xs_free(snac->config);
  103. xs_free(snac->key);
  104. xs_free(snac->actor);
  105. xs_free(snac->md5);
  106. }
  107. int user_open(snac *snac, const char *uid)
  108. /* opens a user */
  109. {
  110. int ret = 0;
  111. memset(snac, '\0', sizeof(struct _snac));
  112. if (validate_uid(uid)) {
  113. xs *cfg_file;
  114. FILE *f;
  115. snac->uid = xs_str_new(uid);
  116. snac->basedir = xs_fmt("%s/user/%s", srv_basedir, uid);
  117. cfg_file = xs_fmt("%s/user.json", snac->basedir);
  118. if ((f = fopen(cfg_file, "r")) != NULL) {
  119. xs *cfg_data;
  120. // if (fileno(f) > 100)
  121. // snac_log(snac, xs_fmt("CAUTION: fileno() > 100"));
  122. /* read full config file */
  123. cfg_data = xs_readall(f);
  124. fclose(f);
  125. if ((snac->config = xs_json_loads(cfg_data)) != NULL) {
  126. xs *key_file = xs_fmt("%s/key.json", snac->basedir);
  127. if ((f = fopen(key_file, "r")) != NULL) {
  128. xs *key_data;
  129. key_data = xs_readall(f);
  130. fclose(f);
  131. if ((snac->key = xs_json_loads(key_data)) != NULL) {
  132. snac->actor = xs_fmt("%s/%s", srv_baseurl, uid);
  133. snac->md5 = xs_md5_hex(snac->actor, strlen(snac->actor));
  134. ret = 1;
  135. }
  136. else
  137. srv_log(xs_fmt("cannot parse '%s'", key_file));
  138. }
  139. else
  140. srv_log(xs_fmt("error opening '%s'", key_file));
  141. }
  142. else
  143. srv_log(xs_fmt("cannot parse '%s'", cfg_file));
  144. }
  145. else
  146. srv_debug(2, xs_fmt("error opening '%s'", cfg_file));
  147. }
  148. else
  149. srv_debug(1, xs_fmt("invalid user '%s'", uid));
  150. if (!ret)
  151. user_free(snac);
  152. return ret;
  153. }
  154. d_char *user_list(void)
  155. /* returns the list of user ids */
  156. {
  157. xs *spec = xs_fmt("%s/user/" "*", srv_basedir);
  158. return xs_glob(spec, 1, 0);
  159. }
  160. double mtime_nl(const char *fn, int *n_link)
  161. /* returns the mtime and number of links of a file or directory, or 0.0 */
  162. {
  163. struct stat st;
  164. double r = 0.0;
  165. int n = 0;
  166. if (fn && stat(fn, &st) != -1) {
  167. r = (double) st.st_mtim.tv_sec;
  168. n = st.st_nlink;
  169. }
  170. if (n_link)
  171. *n_link = n;
  172. return r;
  173. }
  174. /** database 2.1+ **/
  175. /** indexes **/
  176. int index_add_md5(const char *fn, const char *md5)
  177. /* adds an md5 to an index */
  178. {
  179. int status = 201; /* Created */
  180. FILE *f;
  181. if ((f = fopen(fn, "a")) != NULL) {
  182. flock(fileno(f), LOCK_EX);
  183. /* ensure the position is at the end after getting the lock */
  184. fseek(f, 0, SEEK_END);
  185. fprintf(f, "%s\n", md5);
  186. fclose(f);
  187. }
  188. else
  189. status = 500;
  190. return status;
  191. }
  192. int index_add(const char *fn, const char *id)
  193. /* adds an id to an index */
  194. {
  195. xs *md5 = xs_md5_hex(id, strlen(id));
  196. return index_add_md5(fn, md5);
  197. }
  198. int index_del_md5(const char *fn, const char *md5)
  199. /* deletes an md5 from an index */
  200. {
  201. int status = 404;
  202. FILE *i, *o;
  203. if ((i = fopen(fn, "r")) != NULL) {
  204. flock(fileno(i), LOCK_EX);
  205. xs *nfn = xs_fmt("%s.new", fn);
  206. char line[256];
  207. if ((o = fopen(nfn, "w")) != NULL) {
  208. while (fgets(line, sizeof(line), i) != NULL) {
  209. line[32] = '\0';
  210. if (memcmp(line, md5, 32) != 0)
  211. fprintf(o, "%s\n", line);
  212. }
  213. fclose(o);
  214. xs *ofn = xs_fmt("%s.bak", fn);
  215. link(fn, ofn);
  216. rename(nfn, fn);
  217. }
  218. else
  219. status = 500;
  220. fclose(i);
  221. }
  222. else
  223. status = 500;
  224. return status;
  225. }
  226. int index_del(const char *fn, const char *id)
  227. /* deletes an id from an index */
  228. {
  229. xs *md5 = xs_md5_hex(id, strlen(id));
  230. return index_del_md5(fn, md5);
  231. }
  232. int index_in_md5(const char *fn, const char *md5)
  233. /* checks if the md5 is already in the index */
  234. {
  235. FILE *f;
  236. int ret = 0;
  237. if ((f = fopen(fn, "r")) != NULL) {
  238. flock(fileno(f), LOCK_SH);
  239. char line[256];
  240. while (!ret && fgets(line, sizeof(line), f) != NULL) {
  241. line[32] = '\0';
  242. if (strcmp(line, md5) == 0)
  243. ret = 1;
  244. }
  245. fclose(f);
  246. }
  247. return ret;
  248. }
  249. int index_in(const char *fn, const char *id)
  250. /* checks if the object id is already in the index */
  251. {
  252. xs *md5 = xs_md5_hex(id, strlen(id));
  253. return index_in_md5(fn, md5);
  254. }
  255. int index_first(const char *fn, char *line, int size)
  256. /* reads the first entry of an index */
  257. {
  258. FILE *f;
  259. int ret = 0;
  260. if ((f = fopen(fn, "r")) != NULL) {
  261. flock(fileno(f), LOCK_SH);
  262. if (fgets(line, size, f) != NULL) {
  263. line[32] = '\0';
  264. ret = 1;
  265. }
  266. fclose(f);
  267. }
  268. return ret;
  269. }
  270. int index_len(const char *fn)
  271. /* returns the number of elements in an index */
  272. {
  273. struct stat st;
  274. int len = 0;
  275. if (stat(fn, &st) != -1)
  276. len = st.st_size / 33;
  277. return len;
  278. }
  279. d_char *index_list(const char *fn, int max)
  280. /* returns an index as a list */
  281. {
  282. d_char *list = NULL;
  283. FILE *f;
  284. int n = 0;
  285. if ((f = fopen(fn, "r")) != NULL) {
  286. flock(fileno(f), LOCK_SH);
  287. char line[256];
  288. list = xs_list_new();
  289. while (n < max && fgets(line, sizeof(line), f) != NULL) {
  290. line[32] = '\0';
  291. list = xs_list_append(list, line);
  292. n++;
  293. }
  294. fclose(f);
  295. }
  296. return list;
  297. }
  298. d_char *index_list_desc(const char *fn, int skip, int show)
  299. /* returns an index as a list, in reverse order */
  300. {
  301. d_char *list = NULL;
  302. FILE *f;
  303. int n = 0;
  304. if ((f = fopen(fn, "r")) != NULL) {
  305. flock(fileno(f), LOCK_SH);
  306. char line[256];
  307. list = xs_list_new();
  308. /* move to the end minus one entry (or more, if skipping entries) */
  309. if (!fseek(f, 0, SEEK_END) && !fseek(f, (skip + 1) * -33, SEEK_CUR)) {
  310. while (n < show && fgets(line, sizeof(line), f) != NULL) {
  311. line[32] = '\0';
  312. list = xs_list_append(list, line);
  313. n++;
  314. /* move backwards 2 entries */
  315. if (fseek(f, -66, SEEK_CUR) == -1)
  316. break;
  317. }
  318. }
  319. fclose(f);
  320. }
  321. return list;
  322. }
  323. /** objects **/
  324. d_char *_object_fn_by_md5(const char *md5)
  325. {
  326. xs *bfn = xs_fmt("%s/object/%c%c", srv_basedir, md5[0], md5[1]);
  327. mkdir(bfn, 0755);
  328. return xs_fmt("%s/%s.json", bfn, md5);
  329. }
  330. d_char *_object_fn(const char *id)
  331. {
  332. xs *md5 = xs_md5_hex(id, strlen(id));
  333. return _object_fn_by_md5(md5);
  334. }
  335. int object_here_by_md5(char *id)
  336. /* checks if an object is already downloaded */
  337. {
  338. xs *fn = _object_fn_by_md5(id);
  339. return mtime(fn) > 0.0;
  340. }
  341. int object_here(char *id)
  342. /* checks if an object is already downloaded */
  343. {
  344. xs *fn = _object_fn(id);
  345. return mtime(fn) > 0.0;
  346. }
  347. int object_get_by_md5(const char *md5, d_char **obj, const char *type)
  348. /* returns a stored object, optionally of the requested type */
  349. {
  350. int status = 404;
  351. xs *fn = _object_fn_by_md5(md5);
  352. FILE *f;
  353. if ((f = fopen(fn, "r")) != NULL) {
  354. flock(fileno(f), LOCK_SH);
  355. xs *j = xs_readall(f);
  356. fclose(f);
  357. *obj = xs_json_loads(j);
  358. if (*obj) {
  359. status = 200;
  360. /* specific type requested? */
  361. if (!xs_is_null(type)) {
  362. char *v = xs_dict_get(*obj, "type");
  363. if (xs_is_null(v) || strcmp(v, type) != 0) {
  364. status = 404;
  365. *obj = xs_free(*obj);
  366. }
  367. }
  368. }
  369. }
  370. else
  371. *obj = NULL;
  372. return status;
  373. }
  374. int object_get(const char *id, d_char **obj, const char *type)
  375. /* returns a stored object, optionally of the requested type */
  376. {
  377. xs *md5 = xs_md5_hex(id, strlen(id));
  378. return object_get_by_md5(md5, obj, type);
  379. }
  380. int _object_add(const char *id, d_char *obj, int ow)
  381. /* stores an object */
  382. {
  383. int status = 201; /* Created */
  384. xs *fn = _object_fn(id);
  385. FILE *f;
  386. if (!ow && mtime(fn) > 0.0) {
  387. /* object already here */
  388. srv_debug(1, xs_fmt("object_add object already here %s", id));
  389. return 204; /* No content */
  390. }
  391. if ((f = fopen(fn, "w")) != NULL) {
  392. flock(fileno(f), LOCK_EX);
  393. xs *j = xs_json_dumps_pp(obj, 4);
  394. fwrite(j, strlen(j), 1, f);
  395. fclose(f);
  396. /* does this object has a parent? */
  397. char *in_reply_to = xs_dict_get(obj, "inReplyTo");
  398. if (!xs_is_null(in_reply_to) && *in_reply_to) {
  399. /* update the children index of the parent */
  400. xs *c_idx = _object_fn(in_reply_to);
  401. c_idx = xs_replace_i(c_idx, ".json", "_c.idx");
  402. if (!index_in(c_idx, id)) {
  403. index_add(c_idx, id);
  404. srv_debug(1, xs_fmt("object_add added child %s to %s", id, c_idx));
  405. }
  406. else
  407. srv_debug(1, xs_fmt("object_add %s child already in %s", id, c_idx));
  408. /* create a one-element index with the parent */
  409. xs *p_idx = xs_replace(fn, ".json", "_p.idx");
  410. if (mtime(p_idx) == 0.0) {
  411. index_add(p_idx, in_reply_to);
  412. srv_debug(1, xs_fmt("object_add added parent %s to %s", in_reply_to, p_idx));
  413. }
  414. }
  415. }
  416. else {
  417. srv_log(xs_fmt("object_add error writing %s (errno: %d)", fn, errno));
  418. status = 500;
  419. }
  420. srv_debug(1, xs_fmt("object_add %s %s %d", id, fn, status));
  421. return status;
  422. }
  423. int object_add(const char *id, d_char *obj)
  424. /* stores an object */
  425. {
  426. return _object_add(id, obj, 0);
  427. }
  428. int object_add_ow(const char *id, d_char *obj)
  429. /* stores an object (overwriting allowed) */
  430. {
  431. return _object_add(id, obj, 1);
  432. }
  433. int object_del_by_md5(const char *md5)
  434. /* deletes an object by its md5 */
  435. {
  436. int status = 404;
  437. xs *fn = _object_fn_by_md5(md5);
  438. if (fn != NULL && unlink(fn) != -1) {
  439. status = 200;
  440. /* also delete associated indexes */
  441. xs *spec = xs_dup(fn);
  442. spec = xs_replace_i(spec, ".json", "*.idx");
  443. xs *files = xs_glob(spec, 0, 0);
  444. char *p, *v;
  445. p = files;
  446. while (xs_list_iter(&p, &v)) {
  447. srv_debug(1, xs_fmt("object_del index %s", v));
  448. unlink(v);
  449. }
  450. }
  451. srv_debug(1, xs_fmt("object_del %s %d", fn, status));
  452. return status;
  453. }
  454. int object_del(const char *id)
  455. /* deletes an object */
  456. {
  457. xs *md5 = xs_md5_hex(id, strlen(id));
  458. return object_del_by_md5(md5);
  459. }
  460. int object_del_if_unref(const char *id)
  461. /* deletes an object if its n_links < 2 */
  462. {
  463. xs *fn = _object_fn(id);
  464. int n_links;
  465. int ret = 0;
  466. if (mtime_nl(fn, &n_links) > 0.0 && n_links < 2)
  467. ret = object_del(id);
  468. return ret;
  469. }
  470. d_char *_object_index_fn(const char *id, const char *idxsfx)
  471. /* returns the filename of an object's index */
  472. {
  473. d_char *fn = _object_fn(id);
  474. return xs_replace_i(fn, ".json", idxsfx);
  475. }
  476. int object_likes_len(const char *id)
  477. /* returns the number of likes (without reading the index) */
  478. {
  479. xs *fn = _object_index_fn(id, "_l.idx");
  480. return index_len(fn);
  481. }
  482. int object_announces_len(const char *id)
  483. /* returns the number of announces (without reading the index) */
  484. {
  485. xs *fn = _object_index_fn(id, "_a.idx");
  486. return index_len(fn);
  487. }
  488. d_char *object_children(const char *id)
  489. /* returns the list of an object's children */
  490. {
  491. xs *fn = _object_index_fn(id, "_c.idx");
  492. return index_list(fn, XS_ALL);
  493. }
  494. d_char *object_likes(const char *id)
  495. {
  496. xs *fn = _object_index_fn(id, "_l.idx");
  497. return index_list(fn, XS_ALL);
  498. }
  499. d_char *object_announces(const char *id)
  500. {
  501. xs *fn = _object_index_fn(id, "_a.idx");
  502. return index_list(fn, XS_ALL);
  503. }
  504. int object_parent(const char *id, char *buf, int size)
  505. /* returns the object parent, if any */
  506. {
  507. xs *fn = _object_fn_by_md5(id);
  508. fn = xs_replace_i(fn, ".json", "_p.idx");
  509. return index_first(fn, buf, size);
  510. }
  511. int object_admire(const char *id, const char *actor, int like)
  512. /* actor likes or announces this object */
  513. {
  514. int status = 200;
  515. xs *fn = _object_fn(id);
  516. fn = xs_replace_i(fn, ".json", like ? "_l.idx" : "_a.idx");
  517. if (!index_in(fn, actor)) {
  518. status = index_add(fn, actor);
  519. srv_debug(1, xs_fmt("object_admire (%s) %s %s", like ? "Like" : "Announce", actor, fn));
  520. }
  521. return status;
  522. }
  523. int _object_user_cache(snac *snac, const char *id, const char *cachedir, int del)
  524. /* adds or deletes from a user cache */
  525. {
  526. xs *ofn = _object_fn(id);
  527. xs *l = xs_split(ofn, "/");
  528. xs *cfn = xs_fmt("%s/%s/%s", snac->basedir, cachedir, xs_list_get(l, -1));
  529. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  530. int ret;
  531. if (del) {
  532. if ((ret = unlink(cfn)) != -1)
  533. index_del(idx, id);
  534. }
  535. else {
  536. if ((ret = link(ofn, cfn)) != -1)
  537. index_add(idx, id);
  538. }
  539. return ret;
  540. }
  541. int object_user_cache_add(snac *snac, const char *id, const char *cachedir)
  542. /* caches an object into a user cache */
  543. {
  544. return _object_user_cache(snac, id, cachedir, 0);
  545. }
  546. int object_user_cache_del(snac *snac, const char *id, const char *cachedir)
  547. /* deletes an object from a user cache */
  548. {
  549. return _object_user_cache(snac, id, cachedir, 1);
  550. }
  551. int object_user_cache_in(snac *snac, const char *id, const char *cachedir)
  552. /* checks if an object is stored in a cache */
  553. {
  554. xs *md5 = xs_md5_hex(id, strlen(id));
  555. xs *cfn = xs_fmt("%s/%s/%s.json", snac->basedir, cachedir, md5);
  556. return !!(mtime(cfn) != 0.0);
  557. }
  558. d_char *object_user_cache_list(snac *snac, const char *cachedir, int max)
  559. /* returns the objects in a cache as a list */
  560. {
  561. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, cachedir);
  562. return index_list(idx, max);
  563. }
  564. /** specialized functions **/
  565. /** followers **/
  566. int follower_add(snac *snac, const char *actor)
  567. /* adds a follower */
  568. {
  569. int ret = object_user_cache_add(snac, actor, "followers");
  570. snac_debug(snac, 2, xs_fmt("follower_add %s", actor));
  571. return ret == -1 ? 500 : 200;
  572. }
  573. int follower_del(snac *snac, const char *actor)
  574. /* deletes a follower */
  575. {
  576. int ret = object_user_cache_del(snac, actor, "followers");
  577. snac_debug(snac, 2, xs_fmt("follower_del %s", actor));
  578. return ret == -1 ? 404 : 200;
  579. }
  580. int follower_check(snac *snac, const char *actor)
  581. /* checks if someone is a follower */
  582. {
  583. return object_user_cache_in(snac, actor, "followers");
  584. }
  585. d_char *follower_list(snac *snac)
  586. /* returns the list of followers */
  587. {
  588. xs *list = object_user_cache_list(snac, "followers", XS_ALL);
  589. d_char *fwers = xs_list_new();
  590. char *p, *v;
  591. /* resolve the list of md5 to be a list of actors */
  592. p = list;
  593. while (xs_list_iter(&p, &v)) {
  594. xs *a_obj = NULL;
  595. if (valid_status(object_get_by_md5(v, &a_obj, NULL))) {
  596. char *actor = xs_dict_get(a_obj, "id");
  597. if (!xs_is_null(actor))
  598. fwers = xs_list_append(fwers, actor);
  599. }
  600. }
  601. return fwers;
  602. }
  603. /** timeline **/
  604. double timeline_mtime(snac *snac)
  605. {
  606. xs *fn = xs_fmt("%s/private.idx", snac->basedir);
  607. return mtime(fn);
  608. }
  609. int timeline_del(snac *snac, char *id)
  610. /* deletes a message from the timeline */
  611. {
  612. /* delete from the user's caches */
  613. object_user_cache_del(snac, id, "public");
  614. object_user_cache_del(snac, id, "private");
  615. /* try to delete the object if it's not used elsewhere */
  616. return object_del_if_unref(id);
  617. }
  618. void timeline_update_indexes(snac *snac, const char *id)
  619. /* updates the indexes */
  620. {
  621. object_user_cache_add(snac, id, "private");
  622. if (xs_startswith(id, snac->actor)) {
  623. xs *msg = NULL;
  624. if (valid_status(object_get(id, &msg, NULL))) {
  625. /* if its ours and is public, also store in public */
  626. if (is_msg_public(snac, msg))
  627. object_user_cache_add(snac, id, "public");
  628. }
  629. }
  630. }
  631. int timeline_add(snac *snac, char *id, char *o_msg)
  632. /* adds a message to the timeline */
  633. {
  634. int ret = object_add(id, o_msg);
  635. timeline_update_indexes(snac, id);
  636. snac_debug(snac, 1, xs_fmt("timeline_add %s", id));
  637. return ret;
  638. }
  639. void timeline_admire(snac *snac, char *id, char *admirer, int like)
  640. /* updates a timeline entry with a new admiration */
  641. {
  642. /* if we are admiring this, add to both timelines */
  643. if (!like && strcmp(admirer, snac->actor) == 0) {
  644. object_user_cache_add(snac, id, "public");
  645. object_user_cache_add(snac, id, "private");
  646. }
  647. object_admire(id, admirer, like);
  648. snac_debug(snac, 1, xs_fmt("timeline_admire (%s) %s %s",
  649. like ? "Like" : "Announce", id, admirer));
  650. }
  651. d_char *timeline_top_level(d_char *list)
  652. /* returns the top level md5 entries from this index */
  653. {
  654. xs_set seen;
  655. char *p, *v;
  656. xs_set_init(&seen);
  657. p = list;
  658. while (xs_list_iter(&p, &v)) {
  659. char line[256] = "";
  660. strcpy(line, v);
  661. for (;;) {
  662. char line2[256];
  663. /* if it doesn't have a parent, use this */
  664. if (!object_parent(line, line2, sizeof(line2)))
  665. break;
  666. /* well, there is a parent... but if it's not there, use this */
  667. if (!object_here_by_md5(line2))
  668. break;
  669. /* it's here! try again with its own parent */
  670. strcpy(line, line2);
  671. }
  672. xs_set_add(&seen, line);
  673. }
  674. return xs_set_result(&seen);
  675. }
  676. d_char *timeline_simple_list(snac *snac, const char *idx_name, int skip, int show)
  677. /* returns a timeline (with all entries) */
  678. {
  679. int c_max;
  680. /* maximum number of items in the timeline */
  681. c_max = xs_number_get(xs_dict_get(srv_config, "max_timeline_entries"));
  682. /* never more timeline entries than the configured maximum */
  683. if (show > c_max)
  684. show = c_max;
  685. xs *idx = xs_fmt("%s/%s.idx", snac->basedir, idx_name);
  686. return index_list_desc(idx, skip, show);
  687. }
  688. d_char *timeline_list(snac *snac, const char *idx_name, int skip, int show)
  689. /* returns a timeline (only top level entries) */
  690. {
  691. xs *list = timeline_simple_list(snac, idx_name, skip, show);
  692. return timeline_top_level(list);
  693. }
  694. /** following **/
  695. /* this needs special treatment and cannot use the object db as is,
  696. with a link to a cached author, because we need the Follow object
  697. in case we need to unfollow (Undo + original Follow) */
  698. d_char *_following_fn(snac *snac, char *actor)
  699. {
  700. xs *md5 = xs_md5_hex(actor, strlen(actor));
  701. return xs_fmt("%s/following/%s.json", snac->basedir, md5);
  702. }
  703. int following_add(snac *snac, char *actor, char *msg)
  704. /* adds to the following list */
  705. {
  706. int ret = 201; /* created */
  707. xs *fn = _following_fn(snac, actor);
  708. FILE *f;
  709. if ((f = fopen(fn, "w")) != NULL) {
  710. xs *j = xs_json_dumps_pp(msg, 4);
  711. fwrite(j, 1, strlen(j), f);
  712. fclose(f);
  713. }
  714. else
  715. ret = 500;
  716. snac_debug(snac, 2, xs_fmt("following_add %s %s", actor, fn));
  717. return ret;
  718. }
  719. int following_del(snac *snac, char *actor)
  720. /* we're not following this actor any longer */
  721. {
  722. xs *fn = _following_fn(snac, actor);
  723. unlink(fn);
  724. snac_debug(snac, 2, xs_fmt("following_del %s %s", actor, fn));
  725. return 200;
  726. }
  727. int following_check(snac *snac, char *actor)
  728. /* checks if we are following this actor */
  729. {
  730. xs *fn = _following_fn(snac, actor);
  731. return !!(mtime(fn) != 0.0);
  732. }
  733. int following_get(snac *snac, char *actor, d_char **data)
  734. /* returns the 'Follow' object */
  735. {
  736. xs *fn = _following_fn(snac, actor);
  737. FILE *f;
  738. int status = 200;
  739. if ((f = fopen(fn, "r")) != NULL) {
  740. xs *j = xs_readall(f);
  741. fclose(f);
  742. *data = xs_json_loads(j);
  743. }
  744. else
  745. status = 404;
  746. return status;
  747. }
  748. d_char *following_list(snac *snac)
  749. /* returns the list of people being followed */
  750. {
  751. xs *spec = xs_fmt("%s/following/" "*.json", snac->basedir);
  752. xs *glist = xs_glob(spec, 0, 0);
  753. char *p, *v;
  754. d_char *list = xs_list_new();
  755. /* iterate the list of files */
  756. p = glist;
  757. while (xs_list_iter(&p, &v)) {
  758. FILE *f;
  759. /* load the follower data */
  760. if ((f = fopen(v, "r")) != NULL) {
  761. xs *j = xs_readall(f);
  762. fclose(f);
  763. if (j != NULL) {
  764. xs *o = xs_json_loads(j);
  765. if (o != NULL) {
  766. char *type = xs_dict_get(o, "type");
  767. if (!xs_is_null(type) && strcmp(type, "Accept") == 0) {
  768. char *actor = xs_dict_get(o, "actor");
  769. if (!xs_is_null(actor))
  770. list = xs_list_append(list, actor);
  771. }
  772. }
  773. }
  774. }
  775. }
  776. return list;
  777. }
  778. d_char *_muted_fn(snac *snac, char *actor)
  779. {
  780. xs *md5 = xs_md5_hex(actor, strlen(actor));
  781. return xs_fmt("%s/muted/%s", snac->basedir, md5);
  782. }
  783. void mute(snac *snac, char *actor)
  784. /* mutes a moron */
  785. {
  786. xs *fn = _muted_fn(snac, actor);
  787. FILE *f;
  788. if ((f = fopen(fn, "w")) != NULL) {
  789. fprintf(f, "%s\n", actor);
  790. fclose(f);
  791. snac_debug(snac, 2, xs_fmt("muted %s %s", actor, fn));
  792. }
  793. }
  794. void unmute(snac *snac, char *actor)
  795. /* actor is no longer a moron */
  796. {
  797. xs *fn = _muted_fn(snac, actor);
  798. unlink(fn);
  799. snac_debug(snac, 2, xs_fmt("unmuted %s %s", actor, fn));
  800. }
  801. int is_muted(snac *snac, char *actor)
  802. /* check if someone is muted */
  803. {
  804. xs *fn = _muted_fn(snac, actor);
  805. return !!(mtime(fn) != 0.0);
  806. }
  807. d_char *_hidden_fn(snac *snac, const char *id)
  808. {
  809. xs *md5 = xs_md5_hex(id, strlen(id));
  810. return xs_fmt("%s/hidden/%s", snac->basedir, md5);
  811. }
  812. void hide(snac *snac, const char *id)
  813. /* hides a message tree */
  814. {
  815. xs *fn = _hidden_fn(snac, id);
  816. FILE *f;
  817. if ((f = fopen(fn, "w")) != NULL) {
  818. fprintf(f, "%s\n", id);
  819. fclose(f);
  820. snac_debug(snac, 2, xs_fmt("hidden %s %s", id, fn));
  821. /* hide all the children */
  822. xs *chld = object_children(id);
  823. char *p, *v;
  824. p = chld;
  825. while (xs_list_iter(&p, &v)) {
  826. xs *co = NULL;
  827. /* resolve to get the id */
  828. if (valid_status(object_get_by_md5(v, &co, NULL))) {
  829. if ((v = xs_dict_get(co, "id")) != NULL)
  830. hide(snac, v);
  831. }
  832. }
  833. }
  834. }
  835. int is_hidden(snac *snac, const char *id)
  836. /* check is id is hidden */
  837. {
  838. xs *fn = _hidden_fn(snac, id);
  839. return !!(mtime(fn) != 0.0);
  840. }
  841. int actor_add(snac *snac, const char *actor, d_char *msg)
  842. /* adds an actor */
  843. {
  844. return object_add_ow(actor, msg);
  845. }
  846. int actor_get(snac *snac, const char *actor, d_char **data)
  847. /* returns an already downloaded actor */
  848. {
  849. int status = 200;
  850. d_char *d;
  851. if (strcmp(actor, snac->actor) == 0) {
  852. /* this actor */
  853. if (data)
  854. *data = msg_actor(snac);
  855. return status;
  856. }
  857. /* read the object */
  858. if (!valid_status(status = object_get(actor, &d, NULL)))
  859. return status;
  860. if (data)
  861. *data = d;
  862. else
  863. d = xs_free(d);
  864. xs *fn = _object_fn(actor);
  865. double max_time;
  866. /* maximum time for the actor data to be considered stale */
  867. max_time = 3600.0 * 36.0;
  868. if (mtime(fn) + max_time < (double) time(NULL)) {
  869. /* actor data exists but also stinks */
  870. FILE *f;
  871. if ((f = fopen(fn, "a")) != NULL) {
  872. /* write a blank at the end to 'touch' the file */
  873. fwrite(" ", 1, 1, f);
  874. fclose(f);
  875. }
  876. status = 205; /* "205: Reset Content" "110: Response Is Stale" */
  877. }
  878. return status;
  879. }
  880. d_char *_static_fn(snac *snac, const char *id)
  881. /* gets the filename for a static file */
  882. {
  883. return xs_fmt("%s/static/%s", snac->basedir, id);
  884. }
  885. int static_get(snac *snac, const char *id, d_char **data, int *size)
  886. /* returns static content */
  887. {
  888. xs *fn = _static_fn(snac, id);
  889. FILE *f;
  890. int status = 404;
  891. *size = XS_ALL;
  892. if ((f = fopen(fn, "rb")) != NULL) {
  893. *data = xs_read(f, size);
  894. fclose(f);
  895. status = 200;
  896. }
  897. return status;
  898. }
  899. void static_put(snac *snac, const char *id, const char *data, int size)
  900. /* writes status content */
  901. {
  902. xs *fn = _static_fn(snac, id);
  903. FILE *f;
  904. if ((f = fopen(fn, "wb")) != NULL) {
  905. fwrite(data, size, 1, f);
  906. fclose(f);
  907. }
  908. }
  909. d_char *_history_fn(snac *snac, char *id)
  910. /* gets the filename for the history */
  911. {
  912. return xs_fmt("%s/history/%s", snac->basedir, id);
  913. }
  914. double history_mtime(snac *snac, char * id)
  915. {
  916. double t = 0.0;
  917. xs *fn = _history_fn(snac, id);
  918. if (fn != NULL)
  919. t = mtime(fn);
  920. return t;
  921. }
  922. void history_add(snac *snac, char *id, char *content, int size)
  923. /* adds something to the history */
  924. {
  925. xs *fn = _history_fn(snac, id);
  926. FILE *f;
  927. if ((f = fopen(fn, "w")) != NULL) {
  928. fwrite(content, size, 1, f);
  929. fclose(f);
  930. }
  931. }
  932. d_char *history_get(snac *snac, char *id)
  933. {
  934. d_char *content = NULL;
  935. xs *fn = _history_fn(snac, id);
  936. FILE *f;
  937. if ((f = fopen(fn, "r")) != NULL) {
  938. content = xs_readall(f);
  939. fclose(f);
  940. }
  941. return content;
  942. }
  943. int history_del(snac *snac, char *id)
  944. {
  945. xs *fn = _history_fn(snac, id);
  946. return unlink(fn);
  947. }
  948. d_char *history_list(snac *snac)
  949. {
  950. xs *spec = xs_fmt("%s/history/" "*.html", snac->basedir);
  951. return xs_glob(spec, 1, 0);
  952. }
  953. /** the queue **/
  954. static xs_dict *_enqueue_put(const char *fn, xs_dict *msg)
  955. /* writes safely to the queue */
  956. {
  957. xs *tfn = xs_fmt("%s.tmp", fn);
  958. FILE *f;
  959. if ((f = fopen(tfn, "w")) != NULL) {
  960. xs *j = xs_json_dumps_pp(msg, 4);
  961. fwrite(j, strlen(j), 1, f);
  962. fclose(f);
  963. rename(tfn, fn);
  964. }
  965. return msg;
  966. }
  967. static xs_dict *_new_qmsg(const char *type, const xs_val *msg, int retries)
  968. /* creates a queue message */
  969. {
  970. int qrt = xs_number_get(xs_dict_get(srv_config, "queue_retry_minutes"));
  971. xs *ntid = tid(retries * 60 * qrt);
  972. xs *rn = xs_number_new(retries);
  973. xs_dict *qmsg = xs_dict_new();
  974. qmsg = xs_dict_append(qmsg, "type", type);
  975. qmsg = xs_dict_append(qmsg, "message", msg);
  976. qmsg = xs_dict_append(qmsg, "retries", rn);
  977. qmsg = xs_dict_append(qmsg, "ntid", ntid);
  978. return qmsg;
  979. }
  980. void enqueue_input(snac *snac, xs_dict *msg, xs_dict *req, int retries)
  981. /* enqueues an input message */
  982. {
  983. xs *qmsg = _new_qmsg("input", msg, retries);
  984. char *ntid = xs_dict_get(qmsg, "ntid");
  985. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  986. qmsg = xs_dict_append(qmsg, "req", req);
  987. qmsg = _enqueue_put(fn, qmsg);
  988. snac_debug(snac, 1, xs_fmt("enqueue_input %s", fn));
  989. }
  990. void enqueue_output(snac *snac, xs_dict *msg, xs_str *inbox, int retries)
  991. /* enqueues an output message to an inbox */
  992. {
  993. if (xs_startswith(inbox, snac->actor)) {
  994. snac_debug(snac, 1, xs_str_new("refusing enqueue to myself"));
  995. return;
  996. }
  997. xs *qmsg = _new_qmsg("output", msg, retries);
  998. char *ntid = xs_dict_get(qmsg, "ntid");
  999. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1000. qmsg = xs_dict_append(qmsg, "inbox", inbox);
  1001. qmsg = xs_dict_append(qmsg, "keyid", snac->actor);
  1002. qmsg = xs_dict_append(qmsg, "seckey", xs_dict_get(snac->key, "secret"));
  1003. qmsg = _enqueue_put(fn, qmsg);
  1004. snac_debug(snac, 1, xs_fmt("enqueue_output %s %s %d", inbox, fn, retries));
  1005. }
  1006. void enqueue_output_by_actor(snac *snac, xs_dict *msg, xs_str *actor, int retries)
  1007. /* enqueues an output message for an actor */
  1008. {
  1009. xs *inbox = get_actor_inbox(snac, actor);
  1010. if (!xs_is_null(inbox))
  1011. enqueue_output(snac, msg, inbox, retries);
  1012. else
  1013. snac_log(snac, xs_fmt("enqueue_output_by_actor cannot get inbox %s", actor));
  1014. }
  1015. void enqueue_email(xs_str *msg, int retries)
  1016. /* enqueues an email message to be sent */
  1017. {
  1018. xs *qmsg = _new_qmsg("email", msg, retries);
  1019. char *ntid = xs_dict_get(qmsg, "ntid");
  1020. xs *fn = xs_fmt("%s/queue/%s.json", srv_basedir, ntid);
  1021. qmsg = _enqueue_put(fn, qmsg);
  1022. srv_debug(1, xs_fmt("enqueue_email %d", retries));
  1023. }
  1024. void enqueue_message(snac *snac, xs_dict *msg)
  1025. /* enqueues an output message */
  1026. {
  1027. xs *qmsg = _new_qmsg("message", msg, 0);
  1028. char *ntid = xs_dict_get(qmsg, "ntid");
  1029. xs *fn = xs_fmt("%s/queue/%s.json", snac->basedir, ntid);
  1030. qmsg = _enqueue_put(fn, qmsg);
  1031. snac_debug(snac, 0, xs_fmt("enqueue_message %s", xs_dict_get(msg, "id")));
  1032. }
  1033. xs_list *user_queue(snac *snac)
  1034. /* returns a list with filenames that can be dequeued */
  1035. {
  1036. xs *spec = xs_fmt("%s/queue/" "*.json", snac->basedir);
  1037. xs_list *list = xs_list_new();
  1038. time_t t = time(NULL);
  1039. xs_list *p;
  1040. xs_val *v;
  1041. xs *fns = xs_glob(spec, 0, 0);
  1042. p = fns;
  1043. while (xs_list_iter(&p, &v)) {
  1044. /* get the retry time from the basename */
  1045. char *bn = strrchr(v, '/');
  1046. time_t t2 = atol(bn + 1);
  1047. if (t2 > t)
  1048. snac_debug(snac, 2, xs_fmt("user_queue not yet time for %s [%ld]", v, t));
  1049. else {
  1050. list = xs_list_append(list, v);
  1051. snac_debug(snac, 2, xs_fmt("user_queue ready for %s", v));
  1052. }
  1053. }
  1054. return list;
  1055. }
  1056. xs_list *queue(void)
  1057. /* returns a list with filenames that can be dequeued */
  1058. {
  1059. xs *spec = xs_fmt("%s/queue/" "*.json", srv_basedir);
  1060. xs_list *list = xs_list_new();
  1061. time_t t = time(NULL);
  1062. xs_list *p;
  1063. xs_val *v;
  1064. xs *fns = xs_glob(spec, 0, 0);
  1065. p = fns;
  1066. while (xs_list_iter(&p, &v)) {
  1067. /* get the retry time from the basename */
  1068. char *bn = strrchr(v, '/');
  1069. time_t t2 = atol(bn + 1);
  1070. if (t2 > t)
  1071. srv_debug(2, xs_fmt("queue not yet time for %s [%ld]", v, t));
  1072. else {
  1073. list = xs_list_append(list, v);
  1074. srv_debug(2, xs_fmt("queue ready for %s", v));
  1075. }
  1076. }
  1077. return list;
  1078. }
  1079. xs_dict *dequeue(const char *fn)
  1080. /* dequeues a message */
  1081. {
  1082. FILE *f;
  1083. xs_dict *obj = NULL;
  1084. if ((f = fopen(fn, "r")) != NULL) {
  1085. /* delete right now */
  1086. unlink(fn);
  1087. xs *j = xs_readall(f);
  1088. obj = xs_json_loads(j);
  1089. fclose(f);
  1090. }
  1091. return obj;
  1092. }
  1093. /** the purge **/
  1094. static void _purge_file(const char *fn, time_t mt)
  1095. /* purge fn if it's older than days */
  1096. {
  1097. if (mtime(fn) < mt) {
  1098. /* older than the minimum time: delete it */
  1099. unlink(fn);
  1100. srv_debug(1, xs_fmt("purged %s", fn));
  1101. }
  1102. }
  1103. static void _purge_subdir(snac *snac, const char *subdir, int days)
  1104. /* purges all files in subdir older than days */
  1105. {
  1106. if (days) {
  1107. time_t mt = time(NULL) - days * 24 * 3600;
  1108. xs *spec = xs_fmt("%s/%s/" "*", snac->basedir, subdir);
  1109. xs *list = xs_glob(spec, 0, 0);
  1110. char *p, *v;
  1111. p = list;
  1112. while (xs_list_iter(&p, &v))
  1113. _purge_file(v, mt);
  1114. }
  1115. }
  1116. void purge_server(void)
  1117. /* purge global server data */
  1118. {
  1119. xs *spec = xs_fmt("%s/object/??", srv_basedir);
  1120. xs *dirs = xs_glob(spec, 0, 0);
  1121. char *p, *v;
  1122. time_t mt = time(NULL) - 7 * 24 * 3600;
  1123. p = dirs;
  1124. while (xs_list_iter(&p, &v)) {
  1125. xs *spec2 = xs_fmt("%s/" "*.json", v);
  1126. xs *files = xs_glob(spec2, 0, 0);
  1127. char *p2, *v2;
  1128. p2 = files;
  1129. while (xs_list_iter(&p2, &v2)) {
  1130. int n_link;
  1131. /* old and with no hard links? */
  1132. if (mtime_nl(v2, &n_link) < mt && n_link < 2) {
  1133. xs *s1 = xs_replace(v2, ".json", "");
  1134. xs *l = xs_split(s1, "/");
  1135. char *md5 = xs_list_get(l, -1);
  1136. object_del_by_md5(md5);
  1137. }
  1138. }
  1139. }
  1140. }
  1141. void purge_user(snac *snac)
  1142. /* do the purge for this user */
  1143. {
  1144. int days;
  1145. days = xs_number_get(xs_dict_get(srv_config, "timeline_purge_days"));
  1146. _purge_subdir(snac, "hidden", days);
  1147. _purge_subdir(snac, "private", days);
  1148. days = xs_number_get(xs_dict_get(srv_config, "local_purge_days"));
  1149. _purge_subdir(snac, "public", days);
  1150. }
  1151. void purge_all(void)
  1152. /* purge all users */
  1153. {
  1154. snac snac;
  1155. xs *list = user_list();
  1156. char *p, *uid;
  1157. p = list;
  1158. while (xs_list_iter(&p, &uid)) {
  1159. if (user_open(&snac, uid)) {
  1160. purge_user(&snac);
  1161. user_free(&snac);
  1162. }
  1163. }
  1164. purge_server();
  1165. }