/[cvs]/eggdrop1.8/src/tcl.c
ViewVC logotype

Contents of /eggdrop1.8/src/tcl.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.6 - (show annotations) (download) (as text)
Sun Oct 24 13:22:40 2010 UTC (8 years, 8 months ago) by pseudo
Branch: MAIN
Changes since 1.5: +3 -4 lines
File MIME type: text/x-chdr
Removed the never-give-up, sort-users and kick-fun/ban-fun variables. Sorting users shouldn't be a challenge for CPUs nowadays. The rest are rarely used and can be scripted.

1 /*
2 * tcl.c -- handles:
3 * the code for every command eggdrop adds to Tcl
4 * Tcl initialization
5 * getting and setting Tcl/eggdrop variables
6 *
7 * $Id: tcl.c,v 1.5 2010/10/23 11:16:13 pseudo Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999 - 2010 Eggheads Development Team
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include <stdlib.h> /* getenv() */
29 #include <locale.h> /* setlocale() */
30 #include "main.h"
31
32 /* Used for read/write to internal strings */
33 typedef struct {
34 char *str; /* Pointer to actual string in eggdrop */
35 int max; /* max length (negative: read-only var
36 * when protect is on) (0: read-only ALWAYS) */
37 int flags; /* 1 = directory */
38 } strinfo;
39
40 typedef struct {
41 int *var;
42 int ro;
43 } intinfo;
44
45
46 extern time_t online_since;
47
48 extern char origbotname[], botuser[], motdfile[], admin[], userfile[],
49 firewall[], helpdir[], notify_new[], vhost[], moddir[],
50 tempdir[], owner[], network[], botnetnick[], bannerfile[],
51 egg_version[], natip[], configfile[], logfile_suffix[], log_ts[],
52 textdir[], pid_file[], listen_ip[];
53
54
55 extern int flood_telnet_thr, flood_telnet_time, shtime, share_greet,
56 require_p, keep_all_logs, allow_new_telnets, stealth_telnets,
57 use_telnet_banner, default_flags, conmask, switch_logfiles_at,
58 connect_timeout, firewallport, notify_users_at, flood_thr, tands,
59 ignore_time, reserved_port_min, reserved_port_max, max_logs,
60 max_logsize, dcc_total, raw_log, identtimeout, dcc_sanitycheck,
61 dupwait_timeout, egg_numver, share_unlinks, protect_telnet,
62 strict_host, resolve_timeout, default_uflags, userfile_perm,
63 cidr_support;
64
65 #ifdef IPV6
66 extern char vhost6[];
67 extern int pref_af;
68 #endif
69
70 #ifdef TLS
71 extern int tls_maxdepth, tls_vfybots, tls_vfyclients, tls_vfydcc, tls_auth;
72 extern char tls_capath[], tls_cafile[], tls_certfile[], tls_keyfile[],
73 tls_ciphers[];
74 #endif
75
76 extern struct dcc_t *dcc;
77 extern tcl_timer_t *timer, *utimer;
78
79 Tcl_Interp *interp;
80
81 int protect_readonly = 0; /* Enable read-only protection? */
82 char whois_fields[1025] = "";
83
84 int dcc_flood_thr = 3;
85 int use_invites = 0;
86 int use_exempts = 0;
87 int force_expire = 0;
88 int remote_boots = 2;
89 int allow_dk_cmds = 1;
90 int must_be_owner = 1;
91 int quiet_reject = 1;
92 int copy_to_tmp = 1;
93 int max_socks = 100;
94 int quick_logs = 0;
95 int par_telnet_flood = 1;
96 int quiet_save = 0;
97 int strtot = 0;
98 int handlen = HANDLEN;
99 int utftot = 0;
100 int clientdata_stuff = 0;
101
102 /* Compatability for removed settings.*/
103 int strict_servernames = 0, enable_simul = 1, use_console_r = 0,
104 debug_output = 0;
105
106 /* Prototypes for Tcl */
107 Tcl_Interp *Tcl_CreateInterp();
108
109 int expmem_tcl()
110 {
111 return strtot + utftot + clientdata_stuff;
112 }
113
114 static void botnet_change(char *new)
115 {
116 if (egg_strcasecmp(botnetnick, new)) {
117 /* Trying to change bot's nickname */
118 if (tands > 0) {
119 putlog(LOG_MISC, "*", "* Tried to change my botnet nick, but I'm still "
120 "linked to a botnet.");
121 putlog(LOG_MISC, "*", "* (Unlink and try again.)");
122 return;
123 } else {
124 if (botnetnick[0])
125 putlog(LOG_MISC, "*", "* IDENTITY CHANGE: %s -> %s", botnetnick, new);
126 strcpy(botnetnick, new);
127 }
128 }
129 }
130
131
132 /*
133 * Vars, traces, misc
134 */
135
136 int init_misc();
137
138 /* Used for read/write to integer couplets */
139 typedef struct {
140 int *left; /* left side of couplet */
141 int *right; /* right side */
142 } coupletinfo;
143
144 /* FIXME: tcl_eggcouplet() should be redesigned so we can use
145 * TCL_TRACE_WRITES | TCL_TRACE_READS as the bit mask instead
146 * of 2 calls as is done in add_tcl_coups().
147 */
148 /* Read/write integer couplets (int1:int2) */
149 static char *tcl_eggcouplet(ClientData cdata, Tcl_Interp *irp,
150 EGG_CONST char *name1,
151 EGG_CONST char *name2, int flags)
152 {
153 char *s, s1[41];
154 coupletinfo *cp = (coupletinfo *) cdata;
155
156 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
157 egg_snprintf(s1, sizeof s1, "%d:%d", *(cp->left), *(cp->right));
158 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
159 if (flags & TCL_TRACE_UNSETS)
160 Tcl_TraceVar(interp, name1,
161 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
162 tcl_eggcouplet, cdata);
163 } else { /* writes */
164 s = (char *) Tcl_GetVar2(interp, name1, name2, 0);
165 if (s != NULL) {
166 int nr1, nr2;
167
168 nr1 = nr2 = 0;
169
170 if (strlen(s) > 40)
171 s[40] = 0;
172
173 sscanf(s, "%d%*c%d", &nr1, &nr2);
174 *(cp->left) = nr1;
175 *(cp->right) = nr2;
176 }
177 }
178 return NULL;
179 }
180
181 /* Read or write normal integer.
182 */
183 static char *tcl_eggint(ClientData cdata, Tcl_Interp *irp,
184 EGG_CONST char *name1,
185 EGG_CONST char *name2, int flags)
186 {
187 char *s, s1[40];
188 long l;
189 intinfo *ii = (intinfo *) cdata;
190
191 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
192 /* Special cases */
193 if ((int *) ii->var == &conmask)
194 strcpy(s1, masktype(conmask));
195 else if ((int *) ii->var == &default_flags) {
196 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
197 fr.global = default_flags;
198
199 fr.udef_global = default_uflags;
200 build_flags(s1, &fr, 0);
201 } else if ((int *) ii->var == &userfile_perm) {
202 egg_snprintf(s1, sizeof s1, "0%o", userfile_perm);
203 } else
204 egg_snprintf(s1, sizeof s1, "%d", *(int *) ii->var);
205 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
206 if (flags & TCL_TRACE_UNSETS)
207 Tcl_TraceVar(interp, name1,
208 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
209 tcl_eggint, cdata);
210 return NULL;
211 } else { /* Writes */
212 s = (char *) Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
213 if (s != NULL) {
214 if ((int *) ii->var == &conmask) {
215 if (s[0])
216 conmask = logmodes(s);
217 else
218 conmask = LOG_MODES | LOG_MISC | LOG_CMDS;
219 } else if ((int *) ii->var == &default_flags) {
220 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
221
222 break_down_flags(s, &fr, 0);
223 default_flags = sanity_check(fr.global); /* drummer */
224
225 default_uflags = fr.udef_global;
226 } else if ((int *) ii->var == &userfile_perm) {
227 int p = oatoi(s);
228
229 if (p <= 0)
230 return "invalid userfile permissions";
231 userfile_perm = p;
232 } else if ((ii->ro == 2) || ((ii->ro == 1) && protect_readonly))
233 return "read-only variable";
234 else {
235 if (Tcl_ExprLong(interp, s, &l) == TCL_ERROR)
236 return "variable must have integer value";
237 if ((int *) ii->var == &max_socks) {
238 if (l < threaddata()->MAXSOCKS)
239 return "you can't DECREASE max-socks below current usage";
240 max_socks = l;
241 } else if ((int *) ii->var == &max_logs) {
242 if (l < max_logs)
243 return "you can't DECREASE max-logs";
244 max_logs = l;
245 init_misc();
246 } else
247 *(ii->var) = (int) l;
248 }
249 }
250 return NULL;
251 }
252 }
253
254 /* Read/write normal string variable
255 */
256 static char *tcl_eggstr(ClientData cdata, Tcl_Interp *irp,
257 EGG_CONST char *name1,
258 EGG_CONST char *name2, int flags)
259 {
260 char *s;
261 strinfo *st = (strinfo *) cdata;
262
263 if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
264 if ((st->str == firewall) && (firewall[0])) {
265 char s1[127];
266
267 egg_snprintf(s1, sizeof s1, "%s:%d", firewall, firewallport);
268 Tcl_SetVar2(interp, name1, name2, s1, TCL_GLOBAL_ONLY);
269 } else
270 Tcl_SetVar2(interp, name1, name2, st->str, TCL_GLOBAL_ONLY);
271 if (flags & TCL_TRACE_UNSETS) {
272 Tcl_TraceVar(interp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
273 TCL_TRACE_UNSETS, tcl_eggstr, cdata);
274 if ((st->max <= 0) && (protect_readonly || (st->max == 0)))
275 return "read-only variable"; /* it won't return the error... */
276 }
277 return NULL;
278 } else { /* writes */
279 if ((st->max <= 0) && (protect_readonly || (st->max == 0))) {
280 Tcl_SetVar2(interp, name1, name2, st->str, TCL_GLOBAL_ONLY);
281 return "read-only variable";
282 }
283 #ifdef USE_TCL_BYTE_ARRAYS
284 # undef malloc
285 # undef free
286 {
287 Tcl_Obj *obj;
288 unsigned char *bytes;
289 int len;
290
291 obj = Tcl_GetVar2Ex(interp, name1, name2, 0);
292 if (!obj)
293 return NULL;
294 len = 0;
295 bytes = Tcl_GetByteArrayFromObj(obj, &len);
296 if (!bytes)
297 return NULL;
298 s = malloc(len + 1);
299 egg_memcpy(s, bytes, len);
300 s[len] = 0;
301 }
302 #else
303 s = (char *) Tcl_GetVar2(interp, name1, name2, 0);
304 #endif /* USE_TCL_BYTE_ARRAYS */
305 if (s != NULL) {
306 if (strlen(s) > abs(st->max))
307 s[abs(st->max)] = 0;
308 if (st->str == botnetnick)
309 botnet_change(s);
310 else if (st->str == logfile_suffix)
311 logsuffix_change(s);
312 else if (st->str == firewall) {
313 splitc(firewall, s, ':');
314 if (!firewall[0])
315 strcpy(firewall, s);
316 else
317 firewallport = atoi(s);
318 } else
319 strcpy(st->str, s);
320 if ((st->flags) && (s[0])) {
321 if (st->str[strlen(st->str) - 1] != '/')
322 strcat(st->str, "/");
323 }
324 #ifdef USE_TCL_BYTE_ARRAYS
325 free(s);
326 #endif /* USE_TCL_BYTE_ARRAYS */
327 }
328 return NULL;
329 }
330 }
331
332 /* Add/remove tcl commands
333 */
334
335 #ifdef USE_TCL_BYTE_ARRAYS
336 static int utf_converter(ClientData cdata, Tcl_Interp *myinterp, int objc,
337 Tcl_Obj *CONST objv[])
338 {
339 char **strings, *byteptr;
340 int i, len, retval, diff;
341 void **callback_data;
342 Function func;
343 ClientData cd;
344
345 objc += 5;
346 strings = (char **) nmalloc(sizeof(char *) * objc);
347 egg_memset(strings, 0, sizeof(char *) * objc);
348 diff = utftot;
349 utftot += sizeof(char *) * objc;
350 objc -= 5;
351 for (i = 0; i < objc; i++) {
352 byteptr = (char *) Tcl_GetByteArrayFromObj(objv[i], &len);
353 strings[i] = (char *) nmalloc(len + 1);
354 utftot += len + 1;
355 strncpy(strings[i], byteptr, len);
356 strings[i][len] = 0;
357 }
358 callback_data = (void **) cdata;
359 func = (Function) callback_data[0];
360 cd = (ClientData) callback_data[1];
361 diff -= utftot;
362 retval = func(cd, myinterp, objc, strings);
363 for (i = 0; i < objc; i++)
364 nfree(strings[i]);
365 nfree(strings);
366 utftot += diff;
367 return retval;
368 }
369
370 void cmd_delete_callback(ClientData cdata)
371 {
372 nfree(cdata);
373 clientdata_stuff -= sizeof(void *) * 2;
374 }
375 #endif /* USE_TCL_BYTE_ARRAYS */
376
377 #ifdef USE_TCL_BYTE_ARRAYS
378 void add_tcl_commands(tcl_cmds *table)
379 {
380 void **cdata;
381
382 while (table->name) {
383 cdata = (void **) nmalloc(sizeof(void *) * 2);
384 clientdata_stuff += sizeof(void *) * 2;
385 cdata[0] = (void *)table->func;
386 cdata[1] = NULL;
387 Tcl_CreateObjCommand(interp, table->name, utf_converter, (ClientData) cdata,
388 cmd_delete_callback);
389 table++;
390 }
391 }
392
393 #else /* USE_TCL_BYTE_ARRAYS */
394
395 void add_tcl_commands(tcl_cmds *table)
396 {
397 int i;
398
399 for (i = 0; table[i].name; i++)
400 Tcl_CreateCommand(interp, table[i].name, table[i].func, NULL, NULL);
401 }
402 #endif /* USE_TCL_BYTE_ARRAYS */
403
404 #ifdef USE_TCL_BYTE_ARRAYS
405 void add_cd_tcl_cmds(cd_tcl_cmd *table)
406 {
407 void **cdata;
408
409 while (table->name) {
410 cdata = nmalloc(sizeof(void *) * 2);
411 clientdata_stuff += sizeof(void *) * 2;
412 cdata[0] = (void *)table->callback;
413 cdata[1] = table->cdata;
414 Tcl_CreateObjCommand(interp, table->name, utf_converter, (ClientData) cdata,
415 cmd_delete_callback);
416 table++;
417 }
418 }
419
420 #else /* USE_TCL_BYTE_ARRAYS */
421
422 void add_cd_tcl_cmds(cd_tcl_cmd *table)
423 {
424 while (table->name) {
425 Tcl_CreateCommand(interp, table->name, table->callback,
426 (ClientData) table->cdata, NULL);
427 table++;
428 }
429 }
430 #endif /* USE_TCL_BYTE_ARRAYS */
431
432 void rem_tcl_commands(tcl_cmds *table)
433 {
434 int i;
435
436 for (i = 0; table[i].name; i++)
437 Tcl_DeleteCommand(interp, table[i].name);
438 }
439
440 void rem_cd_tcl_cmds(cd_tcl_cmd *table)
441 {
442 while (table->name) {
443 Tcl_DeleteCommand(interp, table->name);
444 table++;
445 }
446 }
447
448 #ifdef USE_TCL_OBJ
449 void add_tcl_objcommands(tcl_cmds *table)
450 {
451 int i;
452
453 for (i = 0; table[i].name; i++)
454 Tcl_CreateObjCommand(interp, table[i].name, table[i].func, (ClientData) 0,
455 NULL);
456 }
457 #endif
458
459 /* Get the current tcl result string. */
460 const char *tcl_resultstring()
461 {
462 const char *result;
463 #ifdef USE_TCL_OBJ
464 result = Tcl_GetStringResult(interp);
465 #else
466 result = interp->result;
467 #endif
468 return result;
469 }
470
471 int tcl_resultempty() {
472 const char *result;
473 result = tcl_resultstring();
474 return (result && result[0]) ? 0 : 1;
475 }
476
477 /* Get the current tcl result as int. replaces atoi(interp->result) */
478 int tcl_resultint()
479 {
480 int result;
481 #ifdef USE_TCL_OBJ
482 if (Tcl_GetIntFromObj(NULL, Tcl_GetObjResult(interp), &result) != TCL_OK)
483 #else
484 if (Tcl_GetInt(NULL, interp->result, &result) != TCL_OK)
485 #endif
486 result = 0;
487 return result;
488 }
489
490 static tcl_strings def_tcl_strings[] = {
491 {"botnet-nick", botnetnick, HANDLEN, 0},
492 {"userfile", userfile, 120, STR_PROTECT},
493 {"motd", motdfile, 120, STR_PROTECT},
494 {"admin", admin, 120, 0},
495 {"help-path", helpdir, 120, STR_DIR | STR_PROTECT},
496 {"temp-path", tempdir, 120, STR_DIR | STR_PROTECT},
497 {"text-path", textdir, 120, STR_DIR | STR_PROTECT},
498 #ifdef TLS
499 {"ssl-capath", tls_capath, 120, STR_DIR | STR_PROTECT},
500 {"ssl-cafile", tls_cafile, 120, STR_PROTECT},
501 {"ssl-ciphers", tls_ciphers, 120, STR_PROTECT},
502 {"ssl-privatekey", tls_keyfile, 120, STR_PROTECT},
503 {"ssl-certificate", tls_certfile, 120, STR_PROTECT},
504 #endif
505 #ifndef STATIC
506 {"mod-path", moddir, 120, STR_DIR | STR_PROTECT},
507 #endif
508 {"notify-newusers", notify_new, 120, 0},
509 {"owner", owner, 120, STR_PROTECT},
510 {"vhost", vhost, 120, 0},
511 #ifdef IPV6
512 {"vhost6", vhost6, 120, 0},
513 #endif
514 {"listen-addr", listen_ip, 120, 0},
515 {"network", network, 40, 0},
516 {"whois-fields", whois_fields, 1024, 0},
517 {"nat-ip", natip, 120, 0},
518 {"username", botuser, 10, 0},
519 {"version", egg_version, 0, 0},
520 {"firewall", firewall, 120, 0},
521 {"config", configfile, 0, 0},
522 {"telnet-banner", bannerfile, 120, STR_PROTECT},
523 {"logfile-suffix", logfile_suffix, 20, 0},
524 {"timestamp-format",log_ts, 32, 0},
525 {"pidfile", pid_file, 120, STR_PROTECT},
526 {NULL, NULL, 0, 0}
527 };
528
529 static tcl_ints def_tcl_ints[] = {
530 {"ignore-time", &ignore_time, 0},
531 {"handlen", &handlen, 2},
532 #ifdef TLS
533 {"ssl-chain-depth", &tls_maxdepth, 0},
534 {"ssl-verify-dcc", &tls_vfydcc, 0},
535 {"ssl-verify-clients", &tls_vfyclients, 0},
536 {"ssl-verify-bots", &tls_vfybots, 0},
537 {"ssl-cert-auth", &tls_auth, 0},
538 #endif
539 {"dcc-flood-thr", &dcc_flood_thr, 0},
540 {"hourly-updates", &notify_users_at, 0},
541 {"switch-logfiles-at", &switch_logfiles_at, 0},
542 {"connect-timeout", &connect_timeout, 0},
543 {"reserved-port", &reserved_port_min, 0},
544 {"require-p", &require_p, 0},
545 {"keep-all-logs", &keep_all_logs, 0},
546 {"open-telnets", &allow_new_telnets, 0},
547 {"stealth-telnets", &stealth_telnets, 0},
548 {"use-telnet-banner", &use_telnet_banner, 0},
549 {"uptime", (int *) &online_since, 2},
550 {"console", &conmask, 0},
551 {"default-flags", &default_flags, 0},
552 {"numversion", &egg_numver, 2},
553 {"remote-boots", &remote_boots, 1},
554 {"max-socks", &max_socks, 0},
555 {"max-logs", &max_logs, 0},
556 {"max-logsize", &max_logsize, 0},
557 {"quick-logs", &quick_logs, 0},
558 {"raw-log", &raw_log, 1},
559 {"protect-telnet", &protect_telnet, 0},
560 {"dcc-sanitycheck", &dcc_sanitycheck, 0},
561 {"ident-timeout", &identtimeout, 0},
562 {"share-unlinks", &share_unlinks, 0},
563 {"log-time", &shtime, 0},
564 {"allow-dk-cmds", &allow_dk_cmds, 0},
565 {"resolve-timeout", &resolve_timeout, 0},
566 {"must-be-owner", &must_be_owner, 1},
567 {"paranoid-telnet-flood", &par_telnet_flood, 0},
568 {"use-exempts", &use_exempts, 0},
569 {"use-invites", &use_invites, 0},
570 {"quiet-save", &quiet_save, 0},
571 {"force-expire", &force_expire, 0},
572 {"dupwait-timeout", &dupwait_timeout, 0},
573 {"strict-host", &strict_host, 0},
574 {"userfile-perm", &userfile_perm, 0},
575 {"copy-to-tmp", &copy_to_tmp, 0},
576 {"quiet-reject", &quiet_reject, 0},
577 {"cidr-support", &cidr_support, 0},
578 {"strict-servernames", &strict_servernames, 0}, /* compat */
579 {"enable-simul", &enable_simul, 0}, /* compat */
580 {"debug-output", &debug_output, 0}, /* compat */
581 {"use-console-r", &use_console_r, 0}, /* compat */
582 #ifdef IPV6
583 {"prefer-ipv6", &pref_af, 0},
584 #endif
585 {NULL, NULL, 0}
586 };
587
588 static tcl_coups def_tcl_coups[] = {
589 {"telnet-flood", &flood_telnet_thr, &flood_telnet_time},
590 {"reserved-portrange", &reserved_port_min, &reserved_port_max},
591 {NULL, NULL, NULL}
592 };
593
594 /* Set up Tcl variables that will hook into eggdrop internal vars via
595 * trace callbacks.
596 */
597 static void init_traces()
598 {
599 add_tcl_coups(def_tcl_coups);
600 add_tcl_strings(def_tcl_strings);
601 add_tcl_ints(def_tcl_ints);
602 }
603
604 void kill_tcl()
605 {
606 rem_tcl_coups(def_tcl_coups);
607 rem_tcl_strings(def_tcl_strings);
608 rem_tcl_ints(def_tcl_ints);
609 kill_bind();
610 Tcl_DeleteInterp(interp);
611 }
612
613 extern tcl_cmds tcluser_cmds[], tcldcc_cmds[], tclmisc_cmds[],
614 tclmisc_objcmds[], tcldns_cmds[];
615 #ifdef TLS
616 extern tcl_cmds tcltls_cmds[];
617 #endif
618
619 #ifdef REPLACE_NOTIFIER
620 /* The tickle_*() functions replace the Tcl Notifier
621 * The tickle_*() functions can be called by Tcl threads
622 */
623 void tickle_SetTimer (TCL_CONST86 Tcl_Time *timePtr)
624 {
625 struct threaddata *td = threaddata();
626 /* we can block 1 second maximum, because we have SECONDLY events */
627 if (!timePtr || timePtr->sec > 1 || (timePtr->sec == 1 && timePtr->usec > 0)) {
628 td->blocktime.tv_sec = 1;
629 td->blocktime.tv_usec = 0;
630 } else {
631 td->blocktime.tv_sec = timePtr->sec;
632 td->blocktime.tv_usec = timePtr->usec;
633 }
634 }
635
636 int tickle_WaitForEvent (TCL_CONST86 Tcl_Time *timePtr)
637 {
638 struct threaddata *td = threaddata();
639
640 tickle_SetTimer(timePtr);
641 return (*td->mainloopfunc)(0);
642 }
643
644 void tickle_CreateFileHandler(int fd, int mask, Tcl_FileProc *proc, ClientData cd)
645 {
646 alloctclsock(fd, mask, proc, cd);
647 }
648
649 void tickle_DeleteFileHandler(int fd)
650 {
651 killtclsock(fd);
652 }
653
654 void tickle_FinalizeNotifier(ClientData cd)
655 {
656 struct threaddata *td = threaddata();
657 if (td->socklist)
658 nfree(td->socklist);
659 }
660
661 ClientData tickle_InitNotifier()
662 {
663 static int ismainthread = 1;
664 init_threaddata(ismainthread);
665 if (ismainthread)
666 ismainthread = 0;
667 return NULL;
668 }
669
670 int tclthreadmainloop(int zero)
671 {
672 int i;
673 i = sockread(NULL, NULL, threaddata()->socklist, threaddata()->MAXSOCKS, 1);
674 return (i == -4);
675 }
676
677 struct threaddata *threaddata()
678 {
679 static Tcl_ThreadDataKey tdkey;
680 struct threaddata *td = Tcl_GetThreadData(&tdkey, sizeof(struct threaddata));
681 return td;
682 }
683
684 #else /* REPLACE_NOTIFIER */
685
686 int tclthreadmainloop() { return 0; }
687
688 struct threaddata *threaddata()
689 {
690 static struct threaddata tsd;
691 return &tsd;
692 }
693
694 #endif /* REPLACE_NOTIFIER */
695
696 int init_threaddata(int mainthread)
697 {
698 struct threaddata *td = threaddata();
699 td->mainloopfunc = mainthread ? mainloop : tclthreadmainloop;
700 td->socklist = NULL;
701 td->mainthread = mainthread;
702 td->blocktime.tv_sec = 1;
703 td->blocktime.tv_usec = 0;
704 td->MAXSOCKS = 0;
705 increase_socks_max();
706 return 0;
707 }
708
709 /* Not going through Tcl's crazy main() system (what on earth was he
710 * smoking?!) so we gotta initialize the Tcl interpreter
711 */
712 void init_tcl(int argc, char **argv)
713 {
714 #ifdef REPLACE_NOTIFIER
715 Tcl_NotifierProcs notifierprocs;
716 #endif /* REPLACE_NOTIFIER */
717
718 #ifdef USE_TCL_ENCODING
719 const char *encoding;
720 int i;
721 char *langEnv;
722 #endif /* USE_TCL_ENCODING */
723 #ifdef USE_TCL_PACKAGE
724 int j;
725 char pver[1024] = "";
726 #endif /* USE_TCL_PACKAGE */
727
728 #ifdef REPLACE_NOTIFIER
729 egg_bzero(&notifierprocs, sizeof(notifierprocs));
730 notifierprocs.initNotifierProc = tickle_InitNotifier;
731 notifierprocs.createFileHandlerProc = tickle_CreateFileHandler;
732 notifierprocs.deleteFileHandlerProc = tickle_DeleteFileHandler;
733 notifierprocs.setTimerProc = tickle_SetTimer;
734 notifierprocs.waitForEventProc = tickle_WaitForEvent;
735 notifierprocs.finalizeNotifierProc = tickle_FinalizeNotifier;
736
737 Tcl_SetNotifier(&notifierprocs);
738 #endif /* REPLACE_NOTIFIER */
739
740 /* This must be done *BEFORE* Tcl_SetSystemEncoding(),
741 * or Tcl_SetSystemEncoding() will cause a segfault.
742 */
743 #ifdef USE_TCL_FINDEXEC
744 /* This is used for 'info nameofexecutable'.
745 * The filename in argv[0] must exist in a directory listed in
746 * the environment variable PATH for it to register anything.
747 */
748 Tcl_FindExecutable(argv[0]);
749 #endif /* USE_TCL_FINDEXEC */
750
751 /* Initialize the interpreter */
752 interp = Tcl_CreateInterp();
753
754 #ifdef DEBUG_MEM
755 /* Initialize Tcl's memory debugging if we want it */
756 Tcl_InitMemory(interp);
757 #endif
758
759 /* Set Tcl variable tcl_interactive to 0 */
760 Tcl_SetVar(interp, "tcl_interactive", "0", TCL_GLOBAL_ONLY);
761
762 /* Setup script library facility */
763 Tcl_Init(interp);
764 Tcl_SetServiceMode(TCL_SERVICE_ALL);
765
766 /* Code based on Tcl's TclpSetInitialEncodings() */
767 #ifdef USE_TCL_ENCODING
768 /* Determine the current encoding from the LC_* or LANG environment
769 * variables.
770 */
771 langEnv = getenv("LC_ALL");
772 if (langEnv == NULL || langEnv[0] == '\0') {
773 langEnv = getenv("LC_CTYPE");
774 }
775 if (langEnv == NULL || langEnv[0] == '\0') {
776 langEnv = getenv("LANG");
777 }
778 if (langEnv == NULL || langEnv[0] == '\0') {
779 langEnv = NULL;
780 }
781
782 encoding = NULL;
783 if (langEnv != NULL) {
784 for (i = 0; localeTable[i].lang != NULL; i++)
785 if (strcmp(localeTable[i].lang, langEnv) == 0) {
786 encoding = localeTable[i].encoding;
787 break;
788 }
789
790 /* There was no mapping in the locale table. If there is an
791 * encoding subfield, we can try to guess from that.
792 */
793 if (encoding == NULL) {
794 char *p;
795
796 for (p = langEnv; *p != '\0'; p++) {
797 if (*p == '.') {
798 p++;
799 break;
800 }
801 }
802 if (*p != '\0') {
803 Tcl_DString ds;
804
805 Tcl_DStringInit(&ds);
806 Tcl_DStringAppend(&ds, p, -1);
807
808 encoding = Tcl_DStringValue(&ds);
809 Tcl_UtfToLower(Tcl_DStringValue(&ds));
810 if (Tcl_SetSystemEncoding(NULL, encoding) == TCL_OK) {
811 Tcl_DStringFree(&ds);
812 goto resetPath;
813 }
814 Tcl_DStringFree(&ds);
815 encoding = NULL;
816 }
817 }
818 }
819
820 if (encoding == NULL) {
821 encoding = "iso8859-1";
822 }
823
824 Tcl_SetSystemEncoding(NULL, encoding);
825
826 resetPath:
827
828 /* Initialize the C library's locale subsystem. */
829 setlocale(LC_CTYPE, "");
830
831 /* In case the initial locale is not "C", ensure that the numeric
832 * processing is done in "C" locale regardless. */
833 setlocale(LC_NUMERIC, "C");
834
835 /* Keep the iso8859-1 encoding preloaded. The IO package uses it for
836 * gets on a binary channel. */
837 Tcl_GetEncoding(NULL, "iso8859-1");
838 #endif /* USE_TCL_ENCODING */
839
840 #ifdef USE_TCL_PACKAGE
841 /* Add eggdrop to Tcl's package list */
842 for (j = 0; j <= strlen(egg_version); j++) {
843 if ((egg_version[j] == ' ') || (egg_version[j] == '+'))
844 break;
845 pver[strlen(pver)] = egg_version[j];
846 }
847 Tcl_PkgProvide(interp, "eggdrop", pver);
848 #endif /* USE_TCL_PACKAGE */
849
850 /* Initialize binds and traces */
851 init_bind();
852 init_traces();
853
854 /* Add new commands */
855 add_tcl_commands(tcluser_cmds);
856 add_tcl_commands(tcldcc_cmds);
857 add_tcl_commands(tclmisc_cmds);
858 #ifdef USE_TCL_OBJ
859 add_tcl_objcommands(tclmisc_objcmds);
860 #endif
861 add_tcl_commands(tcldns_cmds);
862 #ifdef TLS
863 add_tcl_commands(tcltls_cmds);
864 #endif
865 }
866
867 void do_tcl(char *whatzit, char *script)
868 {
869 int code;
870 char *result;
871 #ifdef USE_TCL_ENCODING
872 Tcl_DString dstr;
873 #endif
874
875 code = Tcl_Eval(interp, script);
876
877 #ifdef USE_TCL_ENCODING
878 /* properly convert string to system encoding. */
879 Tcl_DStringInit(&dstr);
880 Tcl_UtfToExternalDString(NULL, tcl_resultstring(), -1, &dstr);
881 result = Tcl_DStringValue(&dstr);
882 #else
883 /* use old pre-Tcl 8.1 way. */
884 result = tcl_resultstring();
885 #endif
886
887 if (code != TCL_OK) {
888 putlog(LOG_MISC, "*", "Tcl error in script for '%s':", whatzit);
889 putlog(LOG_MISC, "*", "%s", result);
890 }
891
892 #ifdef USE_TCL_ENCODING
893 Tcl_DStringFree(&dstr);
894 #endif
895 }
896
897 /* Interpret tcl file fname.
898 *
899 * returns: 1 - if everything was okay
900 */
901 int readtclprog(char *fname)
902 {
903 int code;
904 EGG_CONST char *result;
905 #ifdef USE_TCL_ENCODING
906 Tcl_DString dstr;
907 #endif
908
909 if (!file_readable(fname))
910 return 0;
911
912 code = Tcl_EvalFile(interp, fname);
913 result = Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY);
914
915 #ifdef USE_TCL_ENCODING
916 /* properly convert string to system encoding. */
917 Tcl_DStringInit(&dstr);
918 Tcl_UtfToExternalDString(NULL, result, -1, &dstr);
919 result = Tcl_DStringValue(&dstr);
920 #endif
921
922 if (code != TCL_OK) {
923 putlog(LOG_MISC, "*", "Tcl error in file '%s':", fname);
924 putlog(LOG_MISC, "*", "%s", result);
925 code = 0; /* JJM: refactored to remove premature return */
926 } else {
927 /* Refresh internal variables */
928 code = 1;
929 }
930
931 #ifdef USE_TCL_ENCODING
932 Tcl_DStringFree(&dstr);
933 #endif
934
935 return code;
936 }
937
938 void add_tcl_strings(tcl_strings *list)
939 {
940 int i;
941 strinfo *st;
942 int tmp;
943
944 for (i = 0; list[i].name; i++) {
945 st = nmalloc(sizeof *st);
946 strtot += sizeof(strinfo);
947 st->max = list[i].length - (list[i].flags & STR_DIR);
948 if (list[i].flags & STR_PROTECT)
949 st->max = -st->max;
950 st->str = list[i].buf;
951 st->flags = (list[i].flags & STR_DIR);
952 tmp = protect_readonly;
953 protect_readonly = 0;
954 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_WRITES);
955 protect_readonly = tmp;
956 tcl_eggstr((ClientData) st, interp, list[i].name, NULL, TCL_TRACE_READS);
957 Tcl_TraceVar(interp, list[i].name, TCL_TRACE_READS | TCL_TRACE_WRITES |
958 TCL_TRACE_UNSETS, tcl_eggstr, (ClientData) st);
959 }
960 }
961
962 void rem_tcl_strings(tcl_strings *list)
963 {
964 int i, f;
965 strinfo *st;
966
967 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
968 for (i = 0; list[i].name; i++) {
969 st = (strinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggstr,
970 NULL);
971 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggstr, st);
972 if (st != NULL) {
973 strtot -= sizeof(strinfo);
974 nfree(st);
975 }
976 }
977 }
978
979 void add_tcl_ints(tcl_ints *list)
980 {
981 int i, tmp;
982 intinfo *ii;
983
984 for (i = 0; list[i].name; i++) {
985 ii = nmalloc(sizeof *ii);
986 strtot += sizeof(intinfo);
987 ii->var = list[i].val;
988 ii->ro = list[i].readonly;
989 tmp = protect_readonly;
990 protect_readonly = 0;
991 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_WRITES);
992 protect_readonly = tmp;
993 tcl_eggint((ClientData) ii, interp, list[i].name, NULL, TCL_TRACE_READS);
994 Tcl_TraceVar(interp, list[i].name,
995 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
996 tcl_eggint, (ClientData) ii);
997 }
998
999 }
1000
1001 void rem_tcl_ints(tcl_ints *list)
1002 {
1003 int i, f;
1004 intinfo *ii;
1005
1006 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
1007 for (i = 0; list[i].name; i++) {
1008 ii = (intinfo *) Tcl_VarTraceInfo(interp, list[i].name, f, tcl_eggint,
1009 NULL);
1010 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggint, (ClientData) ii);
1011 if (ii) {
1012 strtot -= sizeof(intinfo);
1013 nfree(ii);
1014 }
1015 }
1016 }
1017
1018 /* Allocate couplet space for tracing couplets
1019 */
1020 void add_tcl_coups(tcl_coups *list)
1021 {
1022 coupletinfo *cp;
1023 int i;
1024
1025 for (i = 0; list[i].name; i++) {
1026 cp = nmalloc(sizeof *cp);
1027 strtot += sizeof(coupletinfo);
1028 cp->left = list[i].lptr;
1029 cp->right = list[i].rptr;
1030 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
1031 TCL_TRACE_WRITES);
1032 tcl_eggcouplet((ClientData) cp, interp, list[i].name, NULL,
1033 TCL_TRACE_READS);
1034 Tcl_TraceVar(interp, list[i].name,
1035 TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
1036 tcl_eggcouplet, (ClientData) cp);
1037 }
1038 }
1039
1040 void rem_tcl_coups(tcl_coups *list)
1041 {
1042 int i, f;
1043 coupletinfo *cp;
1044
1045 f = TCL_GLOBAL_ONLY | TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS;
1046 for (i = 0; list[i].name; i++) {
1047 cp = (coupletinfo *) Tcl_VarTraceInfo(interp, list[i].name, f,
1048 tcl_eggcouplet, NULL);
1049 strtot -= sizeof(coupletinfo);
1050 Tcl_UntraceVar(interp, list[i].name, f, tcl_eggcouplet, (ClientData) cp);
1051 nfree(cp);
1052 }
1053 }
1054
1055 /* Check if the Tcl library supports threads
1056 */
1057 int tcl_threaded()
1058 {
1059 #ifdef HAVE_TCL_GETCURRENTTHREAD
1060 if (Tcl_GetCurrentThread() != (Tcl_ThreadId)0)
1061 return 1;
1062 #endif
1063
1064 return 0;
1065 }
1066
1067 /* Check if we need to fork before initializing Tcl
1068 */
1069 int fork_before_tcl()
1070 {
1071 #ifndef REPLACE_NOTIFIER
1072 return tcl_threaded();
1073 #endif
1074 return 0;
1075 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23