data.c 35 KB

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