data.c 35 KB

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