data.c 35 KB

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