chanprog.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * chanprog.c -- handles:
  3. * rmspace()
  4. * maintaining the server list
  5. * revenge punishment
  6. * timers, utimers
  7. * telling the current programmed settings
  8. * initializing a lot of stuff and loading the tcl scripts
  9. *
  10. */
  11. #include "main.h"
  12. #if HAVE_GETRUSAGE
  13. #include <sys/resource.h>
  14. #if HAVE_SYS_RUSAGE_H
  15. #include <sys/rusage.h>
  16. #endif
  17. #endif
  18. #ifdef HAVE_UNAME
  19. #include <sys/utsname.h>
  20. #endif
  21. #include "modules.h"
  22. extern struct userrec *userlist;
  23. extern Tcl_Interp *interp;
  24. extern char ver[], botnetnick[], firewall[], myip[], origbotname[],
  25. motdfile[], userfile[], tempdir[],
  26. notify_new[], owner[],
  27. netpass[], botuser[], owners[], hubs[];
  28. extern time_t now, online_since;
  29. extern int backgrd, term_z, con_chan, cache_hit, cache_miss,
  30. firewallport, default_flags, conmask,
  31. protect_readonly, noshare,
  32. #ifdef HUB
  33. my_port,
  34. #endif
  35. ignore_time, loading;
  36. tcl_timer_t *timer = NULL; /* Minutely timer */
  37. tcl_timer_t *utimer = NULL; /* Secondly timer */
  38. unsigned long timer_id = 1; /* Next timer of any sort will
  39. have this number */
  40. struct chanset_t *chanset = NULL; /* Channel list */
  41. char admin[121] = ""; /* Admin info */
  42. char origbotname[NICKLEN + 1];
  43. char botname[NICKLEN + 1]; /* Primary botname */
  44. /* Remove space characters from beginning and end of string
  45. * (more efficent by Fred1)
  46. */
  47. void rmspace(char *s)
  48. {
  49. #define whitespace(c) (((c) == 32) || ((c) == 9) || ((c) == 13) || ((c) == 10))
  50. char *p;
  51. if (*s == '\0')
  52. return;
  53. /* Wipe end of string */
  54. for (p = s + strlen(s) - 1; ((whitespace(*p)) && (p >= s)); p--);
  55. if (p != s + strlen(s) - 1)
  56. *(p + 1) = 0;
  57. for (p = s; ((whitespace(*p)) && (*p)); p++);
  58. if (p != s)
  59. strcpy(s, p);
  60. }
  61. /* Returns memberfields if the nick is in the member list.
  62. */
  63. memberlist *ismember(struct chanset_t *chan, char *nick)
  64. {
  65. register memberlist *x;
  66. for (x = chan->channel.member; x && x->nick[0]; x = x->next)
  67. if (!rfc_casecmp(x->nick, nick))
  68. return x;
  69. return NULL;
  70. }
  71. /* Find a chanset by channel name as the server knows it (ie !ABCDEchannel)
  72. */
  73. struct chanset_t *findchan(const char *name)
  74. {
  75. register struct chanset_t *chan;
  76. for (chan = chanset; chan; chan = chan->next)
  77. if (!rfc_casecmp(chan->name, name))
  78. return chan;
  79. return NULL;
  80. }
  81. /* Find a chanset by display name (ie !channel)
  82. */
  83. struct chanset_t *findchan_by_dname(const char *name)
  84. {
  85. register struct chanset_t *chan;
  86. for (chan = chanset; chan; chan = chan->next)
  87. if (!rfc_casecmp(chan->dname, name))
  88. return chan;
  89. return NULL;
  90. }
  91. /*
  92. * "caching" functions
  93. */
  94. /* Shortcut for get_user_by_host -- might have user record in one
  95. * of the channel caches.
  96. */
  97. struct userrec *check_chanlist(const char *host)
  98. {
  99. char *nick, *uhost, buf[UHOSTLEN];
  100. register memberlist *m;
  101. register struct chanset_t *chan;
  102. strncpyz(buf, host, sizeof buf);
  103. uhost = buf;
  104. nick = splitnick(&uhost);
  105. for (chan = chanset; chan; chan = chan->next)
  106. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  107. if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
  108. return m->user;
  109. return NULL;
  110. }
  111. /* Shortcut for get_user_by_handle -- might have user record in channels
  112. */
  113. struct userrec *check_chanlist_hand(const char *hand)
  114. {
  115. register struct chanset_t *chan;
  116. register memberlist *m;
  117. for (chan = chanset; chan; chan = chan->next)
  118. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  119. if (m->user && !egg_strcasecmp(m->user->handle, hand))
  120. return m->user;
  121. return NULL;
  122. }
  123. /* Clear the user pointers in the chanlists.
  124. *
  125. * Necessary when a hostmask is added/removed, a user is added or a new
  126. * userfile is loaded.
  127. */
  128. void clear_chanlist(void)
  129. {
  130. register memberlist *m;
  131. register struct chanset_t *chan;
  132. for (chan = chanset; chan; chan = chan->next)
  133. for (m = chan->channel.member; m && m->nick[0]; m = m->next) {
  134. m->user = NULL;
  135. m->tried_getuser = 0;
  136. }
  137. }
  138. /* Clear the user pointer of a specific nick in the chanlists.
  139. *
  140. * Necessary when a hostmask is added/removed, a nick changes, etc.
  141. * Does not completely invalidate the channel cache like clear_chanlist().
  142. */
  143. void clear_chanlist_member(const char *nick)
  144. {
  145. register memberlist *m;
  146. register struct chanset_t *chan;
  147. for (chan = chanset; chan; chan = chan->next)
  148. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  149. if (!rfc_casecmp(m->nick, nick)) {
  150. m->user = NULL;
  151. m->tried_getuser = 0;
  152. break;
  153. }
  154. }
  155. /* If this user@host is in a channel, set it (it was null)
  156. */
  157. void set_chanlist(const char *host, struct userrec *rec)
  158. {
  159. char *nick, *uhost, buf[UHOSTLEN];
  160. register memberlist *m;
  161. register struct chanset_t *chan;
  162. strncpyz(buf, host, sizeof buf);
  163. uhost = buf;
  164. nick = splitnick(&uhost);
  165. for (chan = chanset; chan; chan = chan->next)
  166. for (m = chan->channel.member; m && m->nick[0]; m = m->next)
  167. if (!rfc_casecmp(nick, m->nick) && !egg_strcasecmp(uhost, m->userhost))
  168. m->user = rec;
  169. }
  170. /* Calculate the memory we should be using
  171. */
  172. int expmem_chanprog()
  173. {
  174. register int tot = 0;
  175. register tcl_timer_t *t;
  176. for (t = timer; t; t = t->next)
  177. tot += sizeof(tcl_timer_t) + strlen(t->cmd) + 1;
  178. for (t = utimer; t; t = t->next)
  179. tot += sizeof(tcl_timer_t) + strlen(t->cmd) + 1;
  180. return tot;
  181. }
  182. /* 0 marks all channels
  183. * 1 removes marked channels
  184. * 2 unmarks all channels
  185. */
  186. void checkchans(int which)
  187. {
  188. struct chanset_t *chan, *chan_next;
  189. sdprintf(STR("checkchans(%d)"), which);
  190. if (which == 0 || which == 2) {
  191. for (chan = chanset; chan; chan = chan->next) {
  192. if (which == 0) {
  193. chan->status |= CHAN_FLAGGED;
  194. } else if (which == 2) {
  195. chan->status &= ~CHAN_FLAGGED;
  196. }
  197. }
  198. } else if (which == 1) {
  199. #ifdef LEAF
  200. module_entry *me;
  201. #endif /* LEAF */
  202. for (chan = chanset; chan; chan = chan_next) {
  203. chan_next = chan->next;
  204. if (chan->status & CHAN_FLAGGED) {
  205. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  206. #ifdef LEAF
  207. /* remove_channel(chan); */
  208. if ((me = module_find("channels", 0, 0))) {
  209. Function *func = me->funcs;
  210. (func[CHANNEL_REMOVE]) (chan);
  211. }
  212. #endif /* LEAF */
  213. }
  214. }
  215. }
  216. }
  217. /* Dump uptime info out to dcc (guppy 9Jan99)
  218. */
  219. void tell_verbose_uptime(int idx)
  220. {
  221. char s[256], s1[121];
  222. time_t now2, hr, min;
  223. now2 = now - online_since;
  224. s[0] = 0;
  225. if (now2 > 86400) {
  226. /* days */
  227. sprintf(s, "%d day", (int) (now2 / 86400));
  228. if ((int) (now2 / 86400) >= 2)
  229. strcat(s, "s");
  230. strcat(s, ", ");
  231. now2 -= (((int) (now2 / 86400)) * 86400);
  232. }
  233. hr = (time_t) ((int) now2 / 3600);
  234. now2 -= (hr * 3600);
  235. min = (time_t) ((int) now2 / 60);
  236. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  237. s1[0] = 0;
  238. if (backgrd)
  239. strcpy(s1, MISC_BACKGROUND);
  240. else {
  241. if (term_z)
  242. strcpy(s1, MISC_TERMMODE);
  243. else if (con_chan)
  244. strcpy(s1, MISC_STATMODE);
  245. else
  246. strcpy(s1, MISC_LOGMODE);
  247. }
  248. dprintf(idx, "%s %s (%s)\n", MISC_ONLINEFOR, s, s1);
  249. }
  250. /* Dump status info out to dcc
  251. */
  252. void tell_verbose_status(int idx)
  253. {
  254. char s[256], s1[121], s2[81];
  255. char *vers_t, *uni_t;
  256. #ifdef HUB
  257. int i;
  258. #endif
  259. time_t now2, hr, min;
  260. #if HAVE_GETRUSAGE
  261. struct rusage ru;
  262. #else
  263. # if HAVE_CLOCK
  264. clock_t cl;
  265. # endif
  266. #endif
  267. #ifdef HAVE_UNAME
  268. struct utsname un;
  269. if (!uname(&un) < 0) {
  270. #endif
  271. vers_t = " ";
  272. uni_t = "*unknown*";
  273. #ifdef HAVE_UNAME
  274. } else {
  275. vers_t = un.release;
  276. uni_t = un.sysname;
  277. }
  278. #endif
  279. #ifdef HUB
  280. i = count_users(userlist);
  281. dprintf(idx, "I am %s, running %s: %d user%s (mem: %uk)\n",
  282. botnetnick, ver, i, i == 1 ? "" : "s",
  283. (int) (expected_memory() / 1024));
  284. #endif
  285. now2 = now - online_since;
  286. s[0] = 0;
  287. if (now2 > 86400) {
  288. /* days */
  289. sprintf(s, "%d day", (int) (now2 / 86400));
  290. if ((int) (now2 / 86400) >= 2)
  291. strcat(s, "s");
  292. strcat(s, ", ");
  293. now2 -= (((int) (now2 / 86400)) * 86400);
  294. }
  295. hr = (time_t) ((int) now2 / 3600);
  296. now2 -= (hr * 3600);
  297. min = (time_t) ((int) now2 / 60);
  298. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  299. s1[0] = 0;
  300. if (backgrd)
  301. strcpy(s1, MISC_BACKGROUND);
  302. else {
  303. if (term_z)
  304. strcpy(s1, MISC_TERMMODE);
  305. else if (con_chan)
  306. strcpy(s1, MISC_STATMODE);
  307. else
  308. strcpy(s1, MISC_LOGMODE);
  309. }
  310. #if HAVE_GETRUSAGE
  311. getrusage(RUSAGE_SELF, &ru);
  312. hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
  313. min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
  314. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
  315. #else
  316. # if HAVE_CLOCK
  317. cl = (clock() / CLOCKS_PER_SEC);
  318. hr = (int) (cl / 60);
  319. min = (int) (cl - (hr * 60));
  320. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
  321. # else
  322. sprintf(s2, "CPU ???");
  323. # endif
  324. #endif
  325. dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", MISC_ONLINEFOR,
  326. s, s1, s2, MISC_CACHEHIT,
  327. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  328. if (admin[0])
  329. dprintf(idx, "Admin: %s\n", admin);
  330. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  331. /* info library */
  332. dprintf(idx, "%s %s\n", MISC_TCLLIBRARY,
  333. ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
  334. interp->result : "*unknown*");
  335. /* info tclversion/patchlevel */
  336. dprintf(idx, "%s %s (%s %s)\n", MISC_TCLVERSION,
  337. ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
  338. interp->result : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
  339. interp->result : "*unknown*", MISC_TCLHVERSION,
  340. TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL : "*unknown*");
  341. #if HAVE_TCL_THREADS
  342. dprintf(idx, "Tcl is threaded\n");
  343. #endif
  344. }
  345. /* Show all internal state variables
  346. */
  347. void tell_settings(int idx)
  348. {
  349. char s[1024];
  350. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  351. dprintf(idx, "Botnet Nickname: %s\n", botnetnick);
  352. if (firewall[0])
  353. dprintf(idx, "Firewall: %s, port %d\n", firewall, firewallport);
  354. #ifdef HUB
  355. dprintf(idx, "Userfile: %s \n", userfile);
  356. #endif
  357. dprintf(idx, "Directories:\n");
  358. dprintf(idx, " Temp : %s\n", tempdir);
  359. fr.global = default_flags;
  360. build_flags(s, &fr, NULL);
  361. dprintf(idx, "%s [%s], %s: %s\n", MISC_NEWUSERFLAGS, s,
  362. MISC_NOTIFY, notify_new);
  363. #ifdef HUB
  364. if (owner[0])
  365. dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
  366. #endif
  367. dprintf(idx, "Ignores last %d mins\n", ignore_time);
  368. }
  369. void reaffirm_owners()
  370. {
  371. char *p, *q, s[121];
  372. struct userrec *u;
  373. /* Make sure default owners are +a */
  374. if (owner[0]) {
  375. q = owner;
  376. p = strchr(q, ',');
  377. while (p) {
  378. strncpyz(s, q, (p - q) + 1);
  379. rmspace(s);
  380. u = get_user_by_handle(userlist, s);
  381. if (u)
  382. u->flags = sanity_check(u->flags | USER_ADMIN);
  383. q = p + 1;
  384. p = strchr(q, ',');
  385. }
  386. strcpy(s, q);
  387. rmspace(s);
  388. u = get_user_by_handle(userlist, s);
  389. if (u)
  390. u->flags = sanity_check(u->flags | USER_ADMIN);
  391. }
  392. }
  393. void load_internal_users()
  394. {
  395. char *p = NULL,
  396. *ln,
  397. *hand,
  398. *ip,
  399. *port,
  400. *hublevel = NULL,
  401. *pass,
  402. *hosts,
  403. host[250],
  404. buf[2048];
  405. char *attr;
  406. int i;
  407. struct bot_addr *bi;
  408. struct userrec *u;
  409. //struct flag_record fr = {FR_BOT, 0, 0, 0, 0, 0};
  410. //hubs..
  411. sprintf(buf, "%s", hubs);
  412. p = buf;
  413. while (p) {
  414. ln = p;
  415. p = strchr(p, ',');
  416. if (p)
  417. *p++ = 0;
  418. hand = ln;
  419. ip = NULL;
  420. port = NULL;
  421. hosts = NULL;
  422. for (i = 0; ln; i++) {
  423. switch (i) {
  424. case 0:
  425. hand = ln;
  426. break;
  427. case 1:
  428. ip = ln;
  429. break;
  430. case 2:
  431. port = ln;
  432. break;
  433. case 3:
  434. hublevel = ln;
  435. break;
  436. case 4:
  437. if (!get_user_by_handle(userlist, hand)) {
  438. userlist = adduser(userlist, hand, "none", "-", USER_BOT | USER_OP);
  439. bi = user_malloc(sizeof(struct bot_addr));
  440. bi->address = user_malloc(strlen(ip) + 1);
  441. strcpy(bi->address, ip);
  442. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  443. bi->relay_port = bi->telnet_port;
  444. bi->hublevel = atoi(hublevel);
  445. #ifdef HUB
  446. if ((!bi->hublevel) && (!strcmp(hand, botnetnick)))
  447. bi->hublevel = 99;
  448. #endif
  449. bi->uplink = user_malloc(1);
  450. bi->uplink[0] = 0;
  451. set_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, hand), bi);
  452. set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), netpass);
  453. }
  454. default:
  455. // ln = userids for hostlist, add them all
  456. hosts = ln;
  457. ln = strchr(ln, ' ');
  458. if (ln)
  459. *ln++ = 0;
  460. while (hosts) {
  461. sprintf(host, "*!%s@%s", hosts, ip);
  462. set_user(&USERENTRY_HOSTS, get_user_by_handle(userlist, hand), host);
  463. hosts = ln;
  464. if (ln)
  465. ln = strchr(ln, ' ');
  466. if (ln)
  467. *ln++ = 0;
  468. }
  469. break;
  470. }
  471. if (ln)
  472. ln = strchr(ln, ' ');
  473. if (ln) {
  474. *ln++ = 0;
  475. }
  476. }
  477. }
  478. //owners..
  479. owner[0] = 0;
  480. sprintf(buf, "%s", owners);
  481. p = buf;
  482. while (p) {
  483. ln = p;
  484. p = strchr(p, ',');
  485. if (p)
  486. *p++ = 0;
  487. // name pass hostlist
  488. hand = ln;
  489. pass = NULL;
  490. attr = NULL;
  491. hosts = NULL;
  492. for (i = 0; ln; i++) {
  493. switch (i) {
  494. case 0:
  495. hand = ln;
  496. break;
  497. case 1:
  498. pass = ln;
  499. break;
  500. case 2:
  501. hosts = ln;
  502. if (owner[0])
  503. strncat(owner, ",", 120);
  504. strncat(owner, hand, 120);
  505. if (!get_user_by_handle(userlist, hand)) {
  506. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA);
  507. u = get_user_by_handle(userlist, hand);
  508. set_user(&USERENTRY_PASS, u, pass);
  509. while (hosts) {
  510. ln = strchr(ln, ' ');
  511. if (ln)
  512. *ln++ = 0;
  513. set_user(&USERENTRY_HOSTS, u, hosts);
  514. hosts = ln;
  515. }
  516. }
  517. break;
  518. }
  519. if (ln)
  520. ln = strchr(ln, ' ');
  521. if (ln)
  522. *ln++ = 0;
  523. }
  524. }
  525. }
  526. void chanprog()
  527. {
  528. char buf[2048];
  529. struct bot_addr *bi;
  530. struct userrec *u;
  531. admin[0] = 0;
  532. /* cache our ip on load instead of every 30 seconds -zip */
  533. cache_my_ip();
  534. sdprintf("ip4: %s", myipstr(4));
  535. sdprintf("ip6: %s", myipstr(6));
  536. conmask = 0;
  537. /* Turn off read-only variables (make them write-able) for rehash */
  538. protect_readonly = 0;
  539. //now this only checks server shit. (no channels)
  540. call_hook(HOOK_REHASH);
  541. protect_readonly = 1;
  542. if (!botnetnick[0]) {
  543. strncpyz(botnetnick, origbotname, HANDLEN + 1);
  544. }
  545. strcpy(botuser, origbotname);
  546. if (!botnetnick[0])
  547. fatal("I don't have a botnet nick!!\n", 0);
  548. #ifdef HUB
  549. if (!userfile[0])
  550. fatal(MISC_NOUSERFILE2, 0);
  551. loading = 1;
  552. checkchans(0);
  553. readuserfile(userfile, &userlist);
  554. checkchans(1);
  555. loading = 0;
  556. #endif /* HUB */
  557. load_internal_users();
  558. if (!(u = get_user_by_handle(userlist, botnetnick))) {
  559. /* I need to be on the userlist... doh. */
  560. userlist = adduser(userlist, botnetnick, STR("none"), "-", USER_BOT | USER_OP );
  561. u = get_user_by_handle(userlist, botnetnick);
  562. bi = user_malloc(sizeof(struct bot_addr));
  563. bi->address = user_malloc(strlen(myip) + 1);
  564. strcpy(bi->address, myip);
  565. bi->telnet_port = atoi(buf) ? atoi(buf) : 3333;
  566. bi->relay_port = bi->telnet_port;
  567. #ifdef HUB
  568. bi->hublevel = 99;
  569. #else
  570. bi->hublevel = 0;
  571. #endif /* HUB */
  572. bi->uplink = user_malloc(1);
  573. bi->uplink[0] = 0;
  574. set_user(&USERENTRY_BOTADDR, u, bi);
  575. } else {
  576. bi = get_user(&USERENTRY_BOTADDR, u);
  577. }
  578. bi = get_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, botnetnick));
  579. if (!bi)
  580. fatal(STR("I'm added to userlist but without a bot record!"), 0);
  581. if (bi->telnet_port != 3333) {
  582. #ifdef HUB
  583. listen_all(bi->telnet_port, 0);
  584. my_port = bi->telnet_port;
  585. #endif /* HUB */
  586. }
  587. trigger_cfg_changed();
  588. /* We should be safe now */
  589. if (tempdir[0])
  590. if (tempdir[strlen(tempdir) - 1] != '/')
  591. strcat(tempdir, "/");
  592. /* test tempdir: it's vital */
  593. {
  594. FILE *f;
  595. char s[161];
  596. int fd;
  597. /* possible file race condition solved by using a random string
  598. * and the process id in the filename */
  599. /* Let's not even dare to hope... use mkstemp() -dizz */
  600. sprintf(s, STR("%s.test-XXXXXX"), tempdir);
  601. if ((fd = mkstemp(s)) == -1 || (f = fdopen(fd, "w")) == NULL) {
  602. if (fd != -1) {
  603. unlink(s);
  604. close(fd);
  605. }
  606. fatal(STR("Can't write to tempdir!"), 0);
  607. }
  608. unlink(s);
  609. }
  610. reaffirm_owners();
  611. }
  612. #ifdef HUB
  613. /* Reload the user file from disk
  614. */
  615. void reload()
  616. {
  617. FILE *f;
  618. f = fopen(userfile, "r");
  619. if (f == NULL) {
  620. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  621. return;
  622. }
  623. fclose(f);
  624. noshare = 1;
  625. clear_userlist(userlist);
  626. noshare = 0;
  627. userlist = NULL;
  628. loading = 1;
  629. checkchans(0);
  630. if (!readuserfile(userfile, &userlist))
  631. fatal(MISC_MISSINGUSERF, 0);
  632. checkchans(1);
  633. loading = 0;
  634. reaffirm_owners();
  635. call_hook(HOOK_READ_USERFILE);
  636. }
  637. #endif /* HUB */
  638. void rehash()
  639. {
  640. call_hook(HOOK_PRE_REHASH);
  641. #ifdef HUB
  642. noshare = 1;
  643. clear_userlist(userlist);
  644. noshare = 0;
  645. userlist = NULL;
  646. #endif /* HUB */
  647. chanprog();
  648. }
  649. /*
  650. * Brief venture into timers
  651. */
  652. /* Add a timer
  653. */
  654. unsigned long add_timer(tcl_timer_t **stack, int elapse, char *cmd,
  655. unsigned long prev_id)
  656. {
  657. tcl_timer_t *old = (*stack);
  658. *stack = (tcl_timer_t *) nmalloc(sizeof(tcl_timer_t));
  659. (*stack)->next = old;
  660. (*stack)->mins = elapse;
  661. (*stack)->cmd = (char *) nmalloc(strlen(cmd) + 1);
  662. strcpy((*stack)->cmd, cmd);
  663. /* If it's just being added back and already had an id,
  664. * don't create a new one.
  665. */
  666. if (prev_id > 0)
  667. (*stack)->id = prev_id;
  668. else
  669. (*stack)->id = timer_id++;
  670. return (*stack)->id;
  671. }
  672. /* Remove a timer, by id
  673. */
  674. int remove_timer(tcl_timer_t **stack, unsigned long id)
  675. {
  676. tcl_timer_t *old;
  677. int ok = 0;
  678. while (*stack) {
  679. if ((*stack)->id == id) {
  680. ok++;
  681. old = *stack;
  682. *stack = ((*stack)->next);
  683. nfree(old->cmd);
  684. nfree(old);
  685. } else
  686. stack = &((*stack)->next);
  687. }
  688. return ok;
  689. }
  690. /* Check timers, execute the ones that have expired.
  691. */
  692. void do_check_timers(tcl_timer_t **stack)
  693. {
  694. tcl_timer_t *mark = *stack, *old = NULL;
  695. char x[16];
  696. /* New timers could be added by a Tcl script inside a current timer
  697. * so i'll just clear out the timer list completely, and add any
  698. * unexpired timers back on.
  699. */
  700. *stack = NULL;
  701. while (mark) {
  702. if (mark->mins > 0)
  703. mark->mins--;
  704. old = mark;
  705. mark = mark->next;
  706. if (!old->mins) {
  707. egg_snprintf(x, sizeof x, "timer%lu", old->id);
  708. do_tcl(x, old->cmd);
  709. nfree(old->cmd);
  710. nfree(old);
  711. } else {
  712. old->next = *stack;
  713. *stack = old;
  714. }
  715. }
  716. }
  717. /* Wipe all timers.
  718. */
  719. void wipe_timers(Tcl_Interp *irp, tcl_timer_t **stack)
  720. {
  721. tcl_timer_t *mark = *stack, *old;
  722. while (mark) {
  723. old = mark;
  724. mark = mark->next;
  725. nfree(old->cmd);
  726. nfree(old);
  727. }
  728. *stack = NULL;
  729. }
  730. /* Return list of timers
  731. */
  732. void list_timers(Tcl_Interp *irp, tcl_timer_t *stack)
  733. {
  734. tcl_timer_t *mark;
  735. char mins[10], id[16], *x;
  736. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  737. CONST char *argv[3];
  738. #else
  739. char *argv[3];
  740. #endif
  741. for (mark = stack; mark; mark = mark->next) {
  742. egg_snprintf(mins, sizeof mins, "%u", mark->mins);
  743. egg_snprintf(id, sizeof id, "timer%lu", mark->id);
  744. argv[0] = mins;
  745. argv[1] = mark->cmd;
  746. argv[2] = id;
  747. x = Tcl_Merge(3, argv);
  748. Tcl_AppendElement(irp, x);
  749. Tcl_Free((char *) x);
  750. }
  751. }
  752. /* Oddly enough, written by proton (Emech's coder)
  753. */
  754. int isowner(char *name)
  755. {
  756. char *pa, *pb;
  757. char nl, pl;
  758. if (!owner || !*owner)
  759. return (0);
  760. if (!name || !*name)
  761. return (0);
  762. nl = strlen(name);
  763. pa = owner;
  764. pb = owner;
  765. while (1) {
  766. while (1) {
  767. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  768. break;
  769. pb++;
  770. }
  771. pl = (unsigned int) pb - (unsigned int) pa;
  772. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  773. return (1);
  774. while (1) {
  775. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  776. break;
  777. pb++;
  778. }
  779. if (*pb == 0)
  780. return (0);
  781. pa = pb;
  782. }
  783. }