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[], configfile[],
  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. if (which == 0 || which == 2) {
  190. for (chan = chanset; chan; chan = chan->next)
  191. if (which == 0)
  192. chan->status |= CHAN_FLAGGED;
  193. else
  194. chan->statuc &= ~CHAN_FLAGGED;
  195. } else if (which == 1) {
  196. #ifdef LEAF
  197. module_entry *me;
  198. #endif /* LEAF */
  199. for (chan = chanset; chan; chan = chan_next) {
  200. chan_next = chan->next;
  201. if (chan->status & CHAN_FLAGGED) {
  202. putlog(LOG_MISC, "*", "No longer supporting channel %s", chan->dname);
  203. #ifdef LEAF
  204. /* remove_channel(chan); */
  205. if ((me = module_find("console", 0, 0))) {
  206. Function *func = me->funcs;
  207. (func[CHANNEL_REMOVE]) (chan);
  208. }
  209. #endif /* LEAF */
  210. }
  211. }
  212. }
  213. }
  214. /* Dump uptime info out to dcc (guppy 9Jan99)
  215. */
  216. void tell_verbose_uptime(int idx)
  217. {
  218. char s[256], s1[121];
  219. time_t now2, hr, min;
  220. now2 = now - online_since;
  221. s[0] = 0;
  222. if (now2 > 86400) {
  223. /* days */
  224. sprintf(s, "%d day", (int) (now2 / 86400));
  225. if ((int) (now2 / 86400) >= 2)
  226. strcat(s, "s");
  227. strcat(s, ", ");
  228. now2 -= (((int) (now2 / 86400)) * 86400);
  229. }
  230. hr = (time_t) ((int) now2 / 3600);
  231. now2 -= (hr * 3600);
  232. min = (time_t) ((int) now2 / 60);
  233. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  234. s1[0] = 0;
  235. if (backgrd)
  236. strcpy(s1, MISC_BACKGROUND);
  237. else {
  238. if (term_z)
  239. strcpy(s1, MISC_TERMMODE);
  240. else if (con_chan)
  241. strcpy(s1, MISC_STATMODE);
  242. else
  243. strcpy(s1, MISC_LOGMODE);
  244. }
  245. dprintf(idx, "%s %s (%s)\n", MISC_ONLINEFOR, s, s1);
  246. }
  247. /* Dump status info out to dcc
  248. */
  249. void tell_verbose_status(int idx)
  250. {
  251. char s[256], s1[121], s2[81];
  252. char *vers_t, *uni_t;
  253. #ifdef HUB
  254. int i;
  255. #endif
  256. time_t now2, hr, min;
  257. #if HAVE_GETRUSAGE
  258. struct rusage ru;
  259. #else
  260. # if HAVE_CLOCK
  261. clock_t cl;
  262. # endif
  263. #endif
  264. #ifdef HAVE_UNAME
  265. struct utsname un;
  266. if (!uname(&un) < 0) {
  267. #endif
  268. vers_t = " ";
  269. uni_t = "*unknown*";
  270. #ifdef HAVE_UNAME
  271. } else {
  272. vers_t = un.release;
  273. uni_t = un.sysname;
  274. }
  275. #endif
  276. #ifdef HUB
  277. i = count_users(userlist);
  278. dprintf(idx, "I am %s, running %s: %d user%s (mem: %uk)\n",
  279. botnetnick, ver, i, i == 1 ? "" : "s",
  280. (int) (expected_memory() / 1024));
  281. #endif
  282. now2 = now - online_since;
  283. s[0] = 0;
  284. if (now2 > 86400) {
  285. /* days */
  286. sprintf(s, "%d day", (int) (now2 / 86400));
  287. if ((int) (now2 / 86400) >= 2)
  288. strcat(s, "s");
  289. strcat(s, ", ");
  290. now2 -= (((int) (now2 / 86400)) * 86400);
  291. }
  292. hr = (time_t) ((int) now2 / 3600);
  293. now2 -= (hr * 3600);
  294. min = (time_t) ((int) now2 / 60);
  295. sprintf(&s[strlen(s)], "%02d:%02d", (int) hr, (int) min);
  296. s1[0] = 0;
  297. if (backgrd)
  298. strcpy(s1, MISC_BACKGROUND);
  299. else {
  300. if (term_z)
  301. strcpy(s1, MISC_TERMMODE);
  302. else if (con_chan)
  303. strcpy(s1, MISC_STATMODE);
  304. else
  305. strcpy(s1, MISC_LOGMODE);
  306. }
  307. #if HAVE_GETRUSAGE
  308. getrusage(RUSAGE_SELF, &ru);
  309. hr = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) / 60);
  310. min = (int) ((ru.ru_utime.tv_sec + ru.ru_stime.tv_sec) - (hr * 60));
  311. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actally min/sec */
  312. #else
  313. # if HAVE_CLOCK
  314. cl = (clock() / CLOCKS_PER_SEC);
  315. hr = (int) (cl / 60);
  316. min = (int) (cl - (hr * 60));
  317. sprintf(s2, "CPU %02d:%02d", (int) hr, (int) min); /* Actually min/sec */
  318. # else
  319. sprintf(s2, "CPU ???");
  320. # endif
  321. #endif
  322. dprintf(idx, "%s %s (%s) %s %s %4.1f%%\n", MISC_ONLINEFOR,
  323. s, s1, s2, MISC_CACHEHIT,
  324. 100.0 * ((float) cache_hit) / ((float) (cache_hit + cache_miss)));
  325. if (admin[0])
  326. dprintf(idx, "Admin: %s\n", admin);
  327. dprintf(idx, "OS: %s %s\n", uni_t, vers_t);
  328. /* info library */
  329. dprintf(idx, "%s %s\n", MISC_TCLLIBRARY,
  330. ((interp) && (Tcl_Eval(interp, "info library") == TCL_OK)) ?
  331. interp->result : "*unknown*");
  332. /* info tclversion/patchlevel */
  333. dprintf(idx, "%s %s (%s %s)\n", MISC_TCLVERSION,
  334. ((interp) && (Tcl_Eval(interp, "info patchlevel") == TCL_OK)) ?
  335. interp->result : (Tcl_Eval(interp, "info tclversion") == TCL_OK) ?
  336. interp->result : "*unknown*", MISC_TCLHVERSION,
  337. TCL_PATCH_LEVEL ? TCL_PATCH_LEVEL : "*unknown*");
  338. #if HAVE_TCL_THREADS
  339. dprintf(idx, "Tcl is threaded\n");
  340. #endif
  341. }
  342. /* Show all internal state variables
  343. */
  344. void tell_settings(int idx)
  345. {
  346. char s[1024];
  347. struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};
  348. dprintf(idx, "Botnet Nickname: %s\n", botnetnick);
  349. if (firewall[0])
  350. dprintf(idx, "Firewall: %s, port %d\n", firewall, firewallport);
  351. #ifdef HUB
  352. dprintf(idx, "Userfile: %s \n", userfile);
  353. #endif
  354. dprintf(idx, "Directories:\n");
  355. dprintf(idx, " Temp : %s\n", tempdir);
  356. fr.global = default_flags;
  357. build_flags(s, &fr, NULL);
  358. dprintf(idx, "%s [%s], %s: %s\n", MISC_NEWUSERFLAGS, s,
  359. MISC_NOTIFY, notify_new);
  360. #ifdef HUB
  361. if (owner[0])
  362. dprintf(idx, "%s: %s\n", MISC_PERMOWNER, owner);
  363. #endif
  364. dprintf(idx, "Ignores last %d mins\n", ignore_time);
  365. }
  366. void reaffirm_owners()
  367. {
  368. char *p, *q, s[121];
  369. struct userrec *u;
  370. /* Make sure default owners are +a */
  371. if (owner[0]) {
  372. q = owner;
  373. p = strchr(q, ',');
  374. while (p) {
  375. strncpyz(s, q, (p - q) + 1);
  376. rmspace(s);
  377. u = get_user_by_handle(userlist, s);
  378. if (u)
  379. u->flags = sanity_check(u->flags | USER_ADMIN);
  380. q = p + 1;
  381. p = strchr(q, ',');
  382. }
  383. strcpy(s, q);
  384. rmspace(s);
  385. u = get_user_by_handle(userlist, s);
  386. if (u)
  387. u->flags = sanity_check(u->flags | USER_ADMIN);
  388. }
  389. }
  390. void load_internal_users()
  391. {
  392. char *p = NULL,
  393. *ln,
  394. *hand,
  395. *ip,
  396. *port,
  397. *hublevel = NULL,
  398. *pass,
  399. *hosts,
  400. host[250],
  401. buf[2048];
  402. char *attr;
  403. int i;
  404. struct bot_addr *bi;
  405. struct userrec *u;
  406. //struct flag_record fr = {FR_BOT, 0, 0, 0, 0, 0};
  407. //hubs..
  408. sprintf(buf, "%s", hubs);
  409. p = buf;
  410. while (p) {
  411. ln = p;
  412. p = strchr(p, ',');
  413. if (p)
  414. *p++ = 0;
  415. hand = ln;
  416. ip = NULL;
  417. port = NULL;
  418. hosts = NULL;
  419. for (i = 0; ln; i++) {
  420. switch (i) {
  421. case 0:
  422. hand = ln;
  423. break;
  424. case 1:
  425. ip = ln;
  426. break;
  427. case 2:
  428. port = ln;
  429. break;
  430. case 3:
  431. hublevel = ln;
  432. break;
  433. case 4:
  434. if (!get_user_by_handle(userlist, hand)) {
  435. userlist = adduser(userlist, hand, "none", "-", USER_BOT | USER_OP);
  436. bi = user_malloc(sizeof(struct bot_addr));
  437. bi->address = user_malloc(strlen(ip) + 1);
  438. strcpy(bi->address, ip);
  439. bi->telnet_port = atoi(port) ? atoi(port) : 0;
  440. bi->relay_port = bi->telnet_port;
  441. bi->hublevel = atoi(hublevel);
  442. #ifdef HUB
  443. if ((!bi->hublevel) && (!strcmp(hand, botnetnick)))
  444. bi->hublevel = 99;
  445. #endif
  446. bi->uplink = user_malloc(1);
  447. bi->uplink[0] = 0;
  448. set_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, hand), bi);
  449. set_user(&USERENTRY_PASS, get_user_by_handle(userlist, hand), netpass);
  450. }
  451. default:
  452. // ln = userids for hostlist, add them all
  453. hosts = ln;
  454. ln = strchr(ln, ' ');
  455. if (ln)
  456. *ln++ = 0;
  457. while (hosts) {
  458. sprintf(host, "*!%s@%s", hosts, ip);
  459. set_user(&USERENTRY_HOSTS, get_user_by_handle(userlist, hand), host);
  460. hosts = ln;
  461. if (ln)
  462. ln = strchr(ln, ' ');
  463. if (ln)
  464. *ln++ = 0;
  465. }
  466. break;
  467. }
  468. if (ln)
  469. ln = strchr(ln, ' ');
  470. if (ln) {
  471. *ln++ = 0;
  472. }
  473. }
  474. }
  475. //owners..
  476. owner[0] = 0;
  477. sprintf(buf, "%s", owners);
  478. p = buf;
  479. while (p) {
  480. ln = p;
  481. p = strchr(p, ',');
  482. if (p)
  483. *p++ = 0;
  484. // name pass hostlist
  485. hand = ln;
  486. pass = NULL;
  487. attr = NULL;
  488. hosts = NULL;
  489. for (i = 0; ln; i++) {
  490. switch (i) {
  491. case 0:
  492. hand = ln;
  493. break;
  494. case 1:
  495. pass = ln;
  496. break;
  497. case 2:
  498. hosts = ln;
  499. if (owner[0])
  500. strncat(owner, ",", 120);
  501. strncat(owner, hand, 120);
  502. if (!get_user_by_handle(userlist, hand)) {
  503. userlist = adduser(userlist, hand, "none", "-", USER_ADMIN | USER_OWNER | USER_MASTER | USER_OP | USER_PARTY | USER_HUBA | USER_CHUBA);
  504. u = get_user_by_handle(userlist, hand);
  505. set_user(&USERENTRY_PASS, u, pass);
  506. while (hosts) {
  507. ln = strchr(ln, ' ');
  508. if (ln)
  509. *ln++ = 0;
  510. set_user(&USERENTRY_HOSTS, u, hosts);
  511. hosts = ln;
  512. }
  513. }
  514. break;
  515. }
  516. if (ln)
  517. ln = strchr(ln, ' ');
  518. if (ln)
  519. *ln++ = 0;
  520. }
  521. }
  522. }
  523. void chanprog()
  524. {
  525. char buf[2048];
  526. struct bot_addr *bi;
  527. struct userrec *u;
  528. admin[0] = 0;
  529. /* cache our ip on load instead of every 30 seconds -zip */
  530. cache_my_ip();
  531. sdprintf("ip4: %s", myipstr(4));
  532. sdprintf("ip6: %s", myipstr(6));
  533. conmask = 0;
  534. /* Turn off read-only variables (make them write-able) for rehash */
  535. protect_readonly = 0;
  536. /* Now read it */
  537. if (configfile[0])
  538. if (!readtclprog(configfile))
  539. fatal(MISC_NOCONFIGFILE, 0);
  540. //now this only checks server shit. (no channels)
  541. call_hook(HOOK_REHASH);
  542. protect_readonly = 1;
  543. if (!botnetnick[0]) {
  544. strncpyz(botnetnick, origbotname, HANDLEN + 1);
  545. }
  546. strcpy(botuser, origbotname);
  547. if (!botnetnick[0])
  548. fatal("I don't have a botnet nick!!\n", 0);
  549. #ifdef HUB
  550. if (!userfile[0])
  551. fatal(MISC_NOUSERFILE2, 0);
  552. loading = 1;
  553. checkchans(0);
  554. readuserfile(userfile, &userlist);
  555. checkchans(1);
  556. loading = 0;
  557. #endif /* HUB */
  558. load_internal_users();
  559. if (!(u = get_user_by_handle(userlist, botnetnick))) {
  560. /* I need to be on the userlist... doh. */
  561. userlist = adduser(userlist, botnetnick, STR("none"), "-", USER_BOT | USER_OP );
  562. u = get_user_by_handle(userlist, botnetnick);
  563. bi = user_malloc(sizeof(struct bot_addr));
  564. bi->address = user_malloc(strlen(myip) + 1);
  565. strcpy(bi->address, myip);
  566. bi->telnet_port = atoi(buf) ? atoi(buf) : 3333;
  567. bi->relay_port = bi->telnet_port;
  568. #ifdef HUB
  569. bi->hublevel = 99;
  570. #else
  571. bi->hublevel = 0;
  572. #endif /* HUB */
  573. bi->uplink = user_malloc(1);
  574. bi->uplink[0] = 0;
  575. set_user(&USERENTRY_BOTADDR, u, bi);
  576. } else {
  577. bi = get_user(&USERENTRY_BOTADDR, u);
  578. }
  579. bi = get_user(&USERENTRY_BOTADDR, get_user_by_handle(userlist, botnetnick));
  580. if (!bi)
  581. fatal(STR("I'm added to userlist but without a bot record!"), 0);
  582. if (bi->telnet_port != 3333) {
  583. #ifdef HUB
  584. listen_all(bi->telnet_port, 0);
  585. my_port = bi->telnet_port;
  586. #endif /* HUB */
  587. }
  588. trigger_cfg_changed();
  589. /* We should be safe now */
  590. if (tempdir[0])
  591. if (tempdir[strlen(tempdir) - 1] != '/')
  592. strcat(tempdir, "/");
  593. /* test tempdir: it's vital */
  594. {
  595. FILE *f;
  596. char s[161];
  597. int fd;
  598. /* possible file race condition solved by using a random string
  599. * and the process id in the filename */
  600. /* Let's not even dare to hope... use mkstemp() -dizz */
  601. sprintf(s, STR("%s.test-XXXXXX"), tempdir);
  602. if ((fd = mkstemp(s)) == -1 || (f = fdopen(fd, "w")) == NULL) {
  603. if (fd != -1) {
  604. unlink(s);
  605. close(fd);
  606. }
  607. fatal(STR("Can't write to tempdir!"), 0);
  608. }
  609. unlink(s);
  610. }
  611. reaffirm_owners();
  612. }
  613. #ifdef HUB
  614. /* Reload the user file from disk
  615. */
  616. void reload()
  617. {
  618. FILE *f;
  619. f = fopen(userfile, "r");
  620. if (f == NULL) {
  621. putlog(LOG_MISC, "*", MISC_CANTRELOADUSER);
  622. return;
  623. }
  624. fclose(f);
  625. noshare = 1;
  626. clear_userlist(userlist);
  627. noshare = 0;
  628. userlist = NULL;
  629. loading = 1;
  630. checkchans(0);
  631. if (!readuserfile(userfile, &userlist))
  632. fatal(MISC_MISSINGUSERF, 0);
  633. checkchans(1);
  634. loading = 0;
  635. reaffirm_owners();
  636. call_hook(HOOK_READ_USERFILE);
  637. }
  638. #endif /* HUB */
  639. void rehash()
  640. {
  641. call_hook(HOOK_PRE_REHASH);
  642. #ifdef HUB
  643. noshare = 1;
  644. clear_userlist(userlist);
  645. noshare = 0;
  646. userlist = NULL;
  647. #endif /* HUB */
  648. chanprog();
  649. }
  650. /*
  651. * Brief venture into timers
  652. */
  653. /* Add a timer
  654. */
  655. unsigned long add_timer(tcl_timer_t **stack, int elapse, char *cmd,
  656. unsigned long prev_id)
  657. {
  658. tcl_timer_t *old = (*stack);
  659. *stack = (tcl_timer_t *) nmalloc(sizeof(tcl_timer_t));
  660. (*stack)->next = old;
  661. (*stack)->mins = elapse;
  662. (*stack)->cmd = (char *) nmalloc(strlen(cmd) + 1);
  663. strcpy((*stack)->cmd, cmd);
  664. /* If it's just being added back and already had an id,
  665. * don't create a new one.
  666. */
  667. if (prev_id > 0)
  668. (*stack)->id = prev_id;
  669. else
  670. (*stack)->id = timer_id++;
  671. return (*stack)->id;
  672. }
  673. /* Remove a timer, by id
  674. */
  675. int remove_timer(tcl_timer_t **stack, unsigned long id)
  676. {
  677. tcl_timer_t *old;
  678. int ok = 0;
  679. while (*stack) {
  680. if ((*stack)->id == id) {
  681. ok++;
  682. old = *stack;
  683. *stack = ((*stack)->next);
  684. nfree(old->cmd);
  685. nfree(old);
  686. } else
  687. stack = &((*stack)->next);
  688. }
  689. return ok;
  690. }
  691. /* Check timers, execute the ones that have expired.
  692. */
  693. void do_check_timers(tcl_timer_t **stack)
  694. {
  695. tcl_timer_t *mark = *stack, *old = NULL;
  696. char x[16];
  697. /* New timers could be added by a Tcl script inside a current timer
  698. * so i'll just clear out the timer list completely, and add any
  699. * unexpired timers back on.
  700. */
  701. *stack = NULL;
  702. while (mark) {
  703. if (mark->mins > 0)
  704. mark->mins--;
  705. old = mark;
  706. mark = mark->next;
  707. if (!old->mins) {
  708. egg_snprintf(x, sizeof x, "timer%lu", old->id);
  709. do_tcl(x, old->cmd);
  710. nfree(old->cmd);
  711. nfree(old);
  712. } else {
  713. old->next = *stack;
  714. *stack = old;
  715. }
  716. }
  717. }
  718. /* Wipe all timers.
  719. */
  720. void wipe_timers(Tcl_Interp *irp, tcl_timer_t **stack)
  721. {
  722. tcl_timer_t *mark = *stack, *old;
  723. while (mark) {
  724. old = mark;
  725. mark = mark->next;
  726. nfree(old->cmd);
  727. nfree(old);
  728. }
  729. *stack = NULL;
  730. }
  731. /* Return list of timers
  732. */
  733. void list_timers(Tcl_Interp *irp, tcl_timer_t *stack)
  734. {
  735. tcl_timer_t *mark;
  736. char mins[10], id[16], *x;
  737. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  738. CONST char *argv[3];
  739. #else
  740. char *argv[3];
  741. #endif
  742. for (mark = stack; mark; mark = mark->next) {
  743. egg_snprintf(mins, sizeof mins, "%u", mark->mins);
  744. egg_snprintf(id, sizeof id, "timer%lu", mark->id);
  745. argv[0] = mins;
  746. argv[1] = mark->cmd;
  747. argv[2] = id;
  748. x = Tcl_Merge(3, argv);
  749. Tcl_AppendElement(irp, x);
  750. Tcl_Free((char *) x);
  751. }
  752. }
  753. /* Oddly enough, written by proton (Emech's coder)
  754. */
  755. int isowner(char *name)
  756. {
  757. char *pa, *pb;
  758. char nl, pl;
  759. if (!owner || !*owner)
  760. return (0);
  761. if (!name || !*name)
  762. return (0);
  763. nl = strlen(name);
  764. pa = owner;
  765. pb = owner;
  766. while (1) {
  767. while (1) {
  768. if ((*pb == 0) || (*pb == ',') || (*pb == ' '))
  769. break;
  770. pb++;
  771. }
  772. pl = (unsigned int) pb - (unsigned int) pa;
  773. if (pl == nl && !egg_strncasecmp(pa, name, nl))
  774. return (1);
  775. while (1) {
  776. if ((*pb == 0) || ((*pb != ',') && (*pb != ' ')))
  777. break;
  778. pb++;
  779. }
  780. if (*pb == 0)
  781. return (0);
  782. pa = pb;
  783. }
  784. }