chanprog.c 19 KB

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