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

Annotation of /eggdrop1.8/src/tclhash.c

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


Revision 1.3 - (hide annotations) (download) (as text)
Sat Oct 23 11:16:13 2010 UTC (10 years ago) by pseudo
Branch: MAIN
Branch point for: gettext
Changes since 1.2: +15 -4 lines
File MIME type: text/x-chdr
Modified SIGQUIT handler to restart the bot by default.
Added support for evnt bind procs to cancel default signal actions.
Removed the die-on-sighup and die-on-sigterm variables.
Added a new bind type DIE triggered before a clean shutdown. Changed the exit status on clean shutdown to 0.

1 simple 1.1 /*
2     * tclhash.c -- handles:
3     * bind and unbind
4     * checking and triggering the various in-bot bindings
5     * listing current bindings
6     * adding/removing new binding tables
7     * (non-Tcl) procedure lookups for msg/dcc/file commands
8     * (Tcl) binding internal procedures to msg/dcc/file commands
9     *
10 pseudo 1.3 * $Id: tclhash.c,v 1.2 2010/10/19 12:13:33 pseudo Exp $
11 simple 1.1 */
12     /*
13     * Copyright (C) 1997 Robey Pointer
14     * Copyright (C) 1999 - 2010 Eggheads Development Team
15     *
16     * This program is free software; you can redistribute it and/or
17     * modify it under the terms of the GNU General Public License
18     * as published by the Free Software Foundation; either version 2
19     * of the License, or (at your option) any later version.
20     *
21     * This program is distributed in the hope that it will be useful,
22     * but WITHOUT ANY WARRANTY; without even the implied warranty of
23     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24     * GNU General Public License for more details.
25     *
26     * You should have received a copy of the GNU General Public License
27     * along with this program; if not, write to the Free Software
28     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29     */
30    
31     #include "main.h"
32     #include "chan.h"
33     #include "users.h"
34    
35     extern Tcl_Interp *interp;
36     extern struct dcc_t *dcc;
37     extern struct userrec *userlist;
38     extern int dcc_total;
39     extern time_t now;
40    
41     p_tcl_bind_list bind_table_list;
42     p_tcl_bind_list H_chat, H_act, H_bcst, H_chon, H_chof, H_load, H_unld, H_link,
43     H_disc, H_dcc, H_chjn, H_chpt, H_bot, H_time, H_nkch, H_away,
44 pseudo 1.3 H_note, H_filt, H_event, H_die, H_cron, H_log = NULL;
45 pseudo 1.2 #ifdef TLS
46     p_tcl_bind_list H_tls = NULL;
47     static int builtin_idx();
48     #endif
49 simple 1.1
50     static int builtin_2char();
51     static int builtin_3char();
52     static int builtin_5int();
53     static int builtin_cron();
54     static int builtin_char();
55     static int builtin_chpt();
56     static int builtin_chjn();
57     static int builtin_idxchar();
58     static int builtin_charidx();
59     static int builtin_chat();
60     static int builtin_dcc();
61     static int builtin_log();
62    
63    
64     /* Allocate and initialise a chunk of memory.
65     */
66     static inline void *n_malloc_null(int size, const char *file, int line)
67     {
68     #ifdef DEBUG_MEM
69     # define nmalloc_null(size) n_malloc_null(size, __FILE__, __LINE__)
70     void *ptr = n_malloc(size, file, line);
71     #else
72     # define nmalloc_null(size) n_malloc_null(size, NULL, 0)
73     void *ptr = nmalloc(size);
74     #endif
75    
76     egg_memset(ptr, 0, size);
77     return ptr;
78     }
79    
80    
81     /* Delete trigger/command.
82     */
83     static inline void tcl_cmd_delete(tcl_cmd_t *tc)
84     {
85     nfree(tc->func_name);
86     nfree(tc);
87     }
88    
89     /* Delete bind and its elements.
90     */
91     static inline void tcl_bind_mask_delete(tcl_bind_mask_t *tm)
92     {
93     tcl_cmd_t *tc, *tc_next;
94    
95     for (tc = tm->first; tc; tc = tc_next) {
96     tc_next = tc->next;
97     tcl_cmd_delete(tc);
98     }
99     nfree(tm->mask);
100     nfree(tm);
101     }
102    
103     /* Delete bind list and its elements.
104     */
105     static inline void tcl_bind_list_delete(tcl_bind_list_t *tl)
106     {
107     tcl_bind_mask_t *tm, *tm_next;
108    
109     for (tm = tl->first; tm; tm = tm_next) {
110     tm_next = tm->next;
111     tcl_bind_mask_delete(tm);
112     }
113     nfree(tl);
114     }
115    
116     inline void garbage_collect_tclhash(void)
117     {
118     tcl_bind_list_t *tl, *tl_next, *tl_prev;
119     tcl_bind_mask_t *tm, *tm_next, *tm_prev;
120     tcl_cmd_t *tc, *tc_next, *tc_prev;
121    
122     for (tl = bind_table_list, tl_prev = NULL; tl; tl = tl_next) {
123     tl_next = tl->next;
124    
125     if (tl->flags & HT_DELETED) {
126     if (tl_prev)
127     tl_prev->next = tl->next;
128     else
129     bind_table_list = tl->next;
130     tcl_bind_list_delete(tl);
131     } else {
132     for (tm = tl->first, tm_prev = NULL; tm; tm = tm_next) {
133     tm_next = tm->next;
134    
135     if (!(tm->flags & TBM_DELETED)) {
136     for (tc = tm->first, tc_prev = NULL; tc; tc = tc_next) {
137     tc_next = tc->next;
138    
139     if (tc->attributes & TC_DELETED) {
140     if (tc_prev)
141     tc_prev->next = tc->next;
142     else
143     tm->first = tc->next;
144     tcl_cmd_delete(tc);
145     } else
146     tc_prev = tc;
147     }
148     }
149    
150     /* Delete the bind when it's marked as deleted or when it's empty. */
151     if ((tm->flags & TBM_DELETED) || tm->first == NULL) {
152     if (tm_prev)
153     tm_prev->next = tm->next;
154     else
155     tl->first = tm_next;
156     tcl_bind_mask_delete(tm);
157     } else
158     tm_prev = tm;
159     }
160     tl_prev = tl;
161     }
162     }
163     }
164    
165     static inline int tcl_cmd_expmem(tcl_cmd_t *tc)
166     {
167     int tot;
168    
169     tot = sizeof(*tc);
170     if (tc->func_name)
171     tot += strlen(tc->func_name) + 1;
172     return tot;
173     }
174    
175     static inline int tcl_bind_mask_expmem(tcl_bind_mask_t *tm)
176     {
177     int tot = 0;
178     tcl_cmd_t *tc;
179    
180     for (tc = tm->first; tc; tc = tc->next)
181     tot += tcl_cmd_expmem(tc);
182     if (tm->mask)
183     tot += strlen(tm->mask) + 1;
184     tot += sizeof(*tm);
185     return tot;
186     }
187    
188     static inline int tcl_bind_list_expmem(tcl_bind_list_t *tl)
189     {
190     int tot = 0;
191     tcl_bind_mask_t *tm;
192    
193     for (tm = tl->first; tm; tm = tm->next)
194     tot += tcl_bind_mask_expmem(tm);
195     tot += sizeof(*tl);
196     return tot;
197     }
198    
199     int expmem_tclhash(void)
200     {
201     int tot = 0;
202     tcl_bind_list_t *tl;
203    
204     for (tl = bind_table_list; tl; tl = tl->next)
205     tot += tcl_bind_list_expmem(tl);
206     return tot;
207     }
208    
209    
210     extern cmd_t C_dcc[];
211     static int tcl_bind();
212    
213     static cd_tcl_cmd cd_cmd_table[] = {
214     {"bind", tcl_bind, (void *) 0},
215     {"unbind", tcl_bind, (void *) 1},
216     {0}
217     };
218    
219     void init_bind(void)
220     {
221     bind_table_list = NULL;
222     Context;
223     add_cd_tcl_cmds(cd_cmd_table);
224     H_unld = add_bind_table("unld", HT_STACKABLE, builtin_char);
225     H_time = add_bind_table("time", HT_STACKABLE, builtin_5int);
226     H_cron = add_bind_table("cron", HT_STACKABLE, builtin_cron);
227     H_note = add_bind_table("note", 0, builtin_3char);
228     H_nkch = add_bind_table("nkch", HT_STACKABLE, builtin_2char);
229     H_load = add_bind_table("load", HT_STACKABLE, builtin_char);
230     H_link = add_bind_table("link", HT_STACKABLE, builtin_2char);
231     H_filt = add_bind_table("filt", HT_STACKABLE, builtin_idxchar);
232     H_disc = add_bind_table("disc", HT_STACKABLE, builtin_char);
233     H_dcc = add_bind_table("dcc", 0, builtin_dcc);
234     H_chpt = add_bind_table("chpt", HT_STACKABLE, builtin_chpt);
235     H_chon = add_bind_table("chon", HT_STACKABLE, builtin_charidx);
236     H_chof = add_bind_table("chof", HT_STACKABLE, builtin_charidx);
237     H_chjn = add_bind_table("chjn", HT_STACKABLE, builtin_chjn);
238     H_chat = add_bind_table("chat", HT_STACKABLE, builtin_chat);
239     H_bot = add_bind_table("bot", 0, builtin_3char);
240     H_bcst = add_bind_table("bcst", HT_STACKABLE, builtin_chat);
241     H_away = add_bind_table("away", HT_STACKABLE, builtin_chat);
242     H_act = add_bind_table("act", HT_STACKABLE, builtin_chat);
243     H_event = add_bind_table("evnt", HT_STACKABLE, builtin_char);
244 pseudo 1.3 H_die = add_bind_table("die", HT_STACKABLE, builtin_char);
245 simple 1.1 H_log = add_bind_table("log", HT_STACKABLE, builtin_log);
246 pseudo 1.2 #ifdef TLS
247     H_tls = add_bind_table("tls", HT_STACKABLE, builtin_idx);
248     #endif
249 simple 1.1 add_builtins(H_dcc, C_dcc);
250     Context;
251     }
252    
253     void kill_bind(void)
254     {
255     tcl_bind_list_t *tl, *tl_next;
256    
257     rem_builtins(H_dcc, C_dcc);
258     for (tl = bind_table_list; tl; tl = tl_next) {
259     tl_next = tl->next;
260    
261     if (!(tl->flags |= HT_DELETED))
262     putlog(LOG_DEBUG, "*", "De-Allocated bind table %s", tl->name);
263     tcl_bind_list_delete(tl);
264     }
265     H_log = NULL;
266     bind_table_list = NULL;
267     }
268    
269     tcl_bind_list_t *add_bind_table(const char *nme, int flg, IntFunc func)
270     {
271     tcl_bind_list_t *tl, *tl_prev;
272     int v;
273    
274     /* Do not allow coders to use bind table names longer than
275     * 4 characters. */
276     Assert(strlen(nme) <= 4);
277    
278     for (tl = bind_table_list, tl_prev = NULL; tl; tl_prev = tl, tl = tl->next) {
279     if (tl->flags & HT_DELETED)
280     continue;
281     v = egg_strcasecmp(tl->name, nme);
282     if (!v)
283     return tl; /* Duplicate, just return old value. */
284     if (v > 0)
285     break; /* New. Insert at start of list. */
286     }
287    
288     tl = nmalloc_null(sizeof *tl);
289     strcpy(tl->name, nme);
290     tl->flags = flg;
291     tl->func = func;
292    
293     if (tl_prev) {
294     tl->next = tl_prev->next;
295     tl_prev->next = tl;
296     } else {
297     tl->next = bind_table_list;
298     bind_table_list = tl;
299     }
300    
301     putlog(LOG_DEBUG, "*", "Allocated bind table %s (flags %d)", nme, flg);
302     return tl;
303     }
304    
305     void del_bind_table(tcl_bind_list_t *tl_which)
306     {
307     tcl_bind_list_t *tl;
308    
309     for (tl = bind_table_list; tl; tl = tl->next) {
310     if (tl->flags & HT_DELETED)
311     continue;
312     if (tl == tl_which) {
313     tl->flags |= HT_DELETED;
314     putlog(LOG_DEBUG, "*", "De-Allocated bind table %s", tl->name);
315     return;
316     }
317     }
318     putlog(LOG_DEBUG, "*", "??? Tried to delete not listed bind table ???");
319     }
320    
321     tcl_bind_list_t *find_bind_table(const char *nme)
322     {
323     tcl_bind_list_t *tl;
324     int v;
325    
326     for (tl = bind_table_list; tl; tl = tl->next) {
327     if (tl->flags & HT_DELETED)
328     continue;
329     v = egg_strcasecmp(tl->name, nme);
330     if (!v)
331     return tl;
332     if (v > 0)
333     return NULL;
334     }
335     return NULL;
336     }
337    
338     static void dump_bind_tables(Tcl_Interp *irp)
339     {
340     tcl_bind_list_t *tl;
341     u_8bit_t i;
342    
343     for (tl = bind_table_list, i = 0; tl; tl = tl->next) {
344     if (tl->flags & HT_DELETED)
345     continue;
346     if (i)
347     Tcl_AppendResult(irp, ", ", NULL);
348     else
349     i = 1;
350     Tcl_AppendResult(irp, tl->name, NULL);
351     }
352     }
353    
354     static int unbind_bind_entry(tcl_bind_list_t *tl, const char *flags,
355     const char *cmd, const char *proc)
356     {
357     tcl_bind_mask_t *tm;
358    
359     /* Search for matching bind in bind list. */
360     for (tm = tl->first; tm; tm = tm->next) {
361     if (tm->flags & TBM_DELETED)
362     continue;
363     if (!strcmp(cmd, tm->mask))
364     break; /* Found it! fall out! */
365     }
366    
367     if (tm) {
368     tcl_cmd_t *tc;
369    
370     /* Search for matching proc in bind. */
371     for (tc = tm->first; tc; tc = tc->next) {
372     if (tc->attributes & TC_DELETED)
373     continue;
374     if (!egg_strcasecmp(tc->func_name, proc)) {
375     /* Erase proc regardless of flags. */
376     tc->attributes |= TC_DELETED;
377     return 1; /* Match. */
378     }
379     }
380     }
381     return 0; /* No match. */
382     }
383    
384     /* Add command (remove old one if necessary)
385     */
386     static int bind_bind_entry(tcl_bind_list_t *tl, const char *flags,
387     const char *cmd, const char *proc)
388     {
389     tcl_cmd_t *tc;
390     tcl_bind_mask_t *tm;
391    
392     /* Search for matching bind in bind list. */
393     for (tm = tl->first; tm; tm = tm->next) {
394     if (tm->flags & TBM_DELETED)
395     continue;
396     if (!strcmp(cmd, tm->mask))
397     break; /* Found it! fall out! */
398     }
399    
400     /* Create bind if it doesn't exist yet. */
401     if (!tm) {
402     tm = nmalloc_null(sizeof *tm);
403     tm->mask = nmalloc(strlen(cmd) + 1);
404     strcpy(tm->mask, cmd);
405    
406     /* Link into linked list of binds. */
407     tm->next = tl->first;
408     tl->first = tm;
409     }
410    
411     /* Proc already defined? If so, replace. */
412     for (tc = tm->first; tc; tc = tc->next) {
413     if (tc->attributes & TC_DELETED)
414     continue;
415     if (!egg_strcasecmp(tc->func_name, proc)) {
416     tc->flags.match = FR_GLOBAL | FR_CHAN;
417     break_down_flags(flags, &(tc->flags), NULL);
418     return 1;
419     }
420     }
421    
422     /* If this bind list is not stackable, remove the
423     * old entry from this bind. */
424     if (!(tl->flags & HT_STACKABLE)) {
425     for (tc = tm->first; tc; tc = tc->next) {
426     if (tc->attributes & TC_DELETED)
427     continue;
428     /* NOTE: We assume there's only one not-yet-deleted entry. */
429     tc->attributes |= TC_DELETED;
430     break;
431     }
432     }
433    
434     tc = nmalloc_null(sizeof *tc);
435     tc->flags.match = FR_GLOBAL | FR_CHAN;
436     break_down_flags(flags, &(tc->flags), NULL);
437     tc->func_name = nmalloc(strlen(proc) + 1);
438     strcpy(tc->func_name, proc);
439    
440     /* Link into linked list of the bind's command list. */
441     tc->next = tm->first;
442     tm->first = tc;
443    
444     return 1;
445     }
446    
447     static int tcl_getbinds(tcl_bind_list_t *tl_kind, const char *name)
448     {
449     tcl_bind_mask_t *tm;
450    
451     for (tm = tl_kind->first; tm; tm = tm->next) {
452     if (tm->flags & TBM_DELETED)
453     continue;
454     if (!egg_strcasecmp(tm->mask, name)) {
455     tcl_cmd_t *tc;
456    
457     for (tc = tm->first; tc; tc = tc->next) {
458     if (tc->attributes & TC_DELETED)
459     continue;
460     Tcl_AppendElement(interp, tc->func_name);
461     }
462     break;
463     }
464     }
465     return TCL_OK;
466     }
467    
468     static int tcl_bind STDVAR
469     {
470     tcl_bind_list_t *tl;
471    
472     /* cd defines what tcl_bind is supposed do: 0 = bind, 1 = unbind. */
473     if ((long int) cd == 1)
474     BADARGS(5, 5, " type flags cmd/mask procname");
475    
476     else
477     BADARGS(4, 5, " type flags cmd/mask ?procname?");
478    
479     tl = find_bind_table(argv[1]);
480     if (!tl) {
481     Tcl_AppendResult(irp, "bad type, should be one of: ", NULL);
482     dump_bind_tables(irp);
483     return TCL_ERROR;
484     }
485     if ((long int) cd == 1) {
486     if (!unbind_bind_entry(tl, argv[2], argv[3], argv[4])) {
487     /* Don't error if trying to re-unbind a builtin */
488     if (argv[4][0] != '*' || argv[4][4] != ':' ||
489     strcmp(argv[3], &argv[4][5]) || strncmp(argv[1], &argv[4][1], 3)) {
490     Tcl_AppendResult(irp, "no such binding", NULL);
491     return TCL_ERROR;
492     }
493     }
494     } else {
495     if (argc == 4)
496     return tcl_getbinds(tl, argv[3]);
497     bind_bind_entry(tl, argv[2], argv[3], argv[4]);
498     }
499     Tcl_AppendResult(irp, argv[3], NULL);
500     return TCL_OK;
501     }
502    
503     int check_validity(char *nme, IntFunc func)
504     {
505     char *p;
506     tcl_bind_list_t *tl;
507    
508     if (*nme != '*')
509     return 0;
510     p = strchr(nme + 1, ':');
511     if (p == NULL)
512     return 0;
513     *p = 0;
514     tl = find_bind_table(nme + 1);
515     *p = ':';
516     if (!tl)
517     return 0;
518     if (tl->func != func)
519     return 0;
520     return 1;
521     }
522    
523     static int builtin_3char STDVAR
524     {
525     Function F = (Function) cd;
526    
527     BADARGS(4, 4, " from to args");
528    
529     CHECKVALIDITY(builtin_3char);
530     F(argv[1], argv[2], argv[3]);
531     return TCL_OK;
532     }
533    
534     static int builtin_2char STDVAR
535     {
536     Function F = (Function) cd;
537    
538     BADARGS(3, 3, " nick msg");
539    
540     CHECKVALIDITY(builtin_2char);
541     F(argv[1], argv[2]);
542     return TCL_OK;
543     }
544    
545     static int builtin_5int STDVAR
546     {
547     Function F = (Function) cd;
548    
549     BADARGS(6, 6, " min hrs dom mon year");
550    
551     CHECKVALIDITY(builtin_5int);
552     F(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
553     return TCL_OK;
554     }
555    
556     static int builtin_cron STDVAR
557     {
558     Function F = (Function) cd;
559    
560     BADARGS(6, 6, " min hrs dom mon weekday");
561    
562     CHECKVALIDITY(builtin_cron);
563     F(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), atoi(argv[5]));
564     return TCL_OK;
565     }
566    
567     static int builtin_char STDVAR
568     {
569     Function F = (Function) cd;
570    
571     BADARGS(2, 2, " handle");
572    
573     CHECKVALIDITY(builtin_char);
574     F(argv[1]);
575     return TCL_OK;
576     }
577    
578     static int builtin_chpt STDVAR
579     {
580     Function F = (Function) cd;
581    
582     BADARGS(3, 3, " bot nick sock");
583    
584     CHECKVALIDITY(builtin_chpt);
585     F(argv[1], argv[2], atoi(argv[3]));
586     return TCL_OK;
587     }
588    
589     static int builtin_chjn STDVAR
590     {
591     Function F = (Function) cd;
592    
593     BADARGS(6, 6, " bot nick chan# flag&sock host");
594    
595     CHECKVALIDITY(builtin_chjn);
596     F(argv[1], argv[2], atoi(argv[3]), argv[4][0],
597     argv[4][0] ? atoi(argv[4] + 1) : 0, argv[5]);
598     return TCL_OK;
599     }
600    
601     static int builtin_idxchar STDVAR
602     {
603     Function F = (Function) cd;
604     int idx;
605     char *r;
606    
607     BADARGS(3, 3, " idx args");
608    
609     CHECKVALIDITY(builtin_idxchar);
610     idx = findidx(atoi(argv[1]));
611     if (idx < 0) {
612     Tcl_AppendResult(irp, "invalid idx", NULL);
613     return TCL_ERROR;
614     }
615     r = (((char *(*)()) F) (idx, argv[2]));
616    
617     Tcl_ResetResult(irp);
618     Tcl_AppendResult(irp, r, NULL);
619     return TCL_OK;
620     }
621    
622     static int builtin_charidx STDVAR
623     {
624     Function F = (Function) cd;
625     int idx;
626    
627     BADARGS(3, 3, " handle idx");
628    
629     CHECKVALIDITY(builtin_charidx);
630     idx = findanyidx(atoi(argv[2]));
631     if (idx < 0) {
632     Tcl_AppendResult(irp, "invalid idx", NULL);
633     return TCL_ERROR;
634     }
635     Tcl_AppendResult(irp, int_to_base10(F(argv[1], idx)), NULL);
636    
637     return TCL_OK;
638     }
639    
640     static int builtin_chat STDVAR
641     {
642     Function F = (Function) cd;
643     int ch;
644    
645     BADARGS(4, 4, " handle idx text");
646    
647     CHECKVALIDITY(builtin_chat);
648     ch = atoi(argv[2]);
649     F(argv[1], ch, argv[3]);
650     return TCL_OK;
651     }
652    
653     static int builtin_dcc STDVAR
654     {
655     int idx;
656     Function F = (Function) cd;
657    
658     BADARGS(4, 4, " hand idx param");
659    
660     CHECKVALIDITY(builtin_dcc);
661     idx = findidx(atoi(argv[2]));
662     if (idx < 0) {
663     Tcl_AppendResult(irp, "invalid idx", NULL);
664     return TCL_ERROR;
665     }
666    
667     /* FIXME: This is an ugly hack. It is not documented as a
668     * 'feature' because it will eventually go away.
669     */
670     if (F == CMD_LEAVE) {
671     Tcl_AppendResult(irp, "break", NULL);
672     return TCL_OK;
673     }
674    
675     /* Check if it's a password change, if so, don't show the password. We
676     * don't need pretty formats here, as it's only for debugging purposes.
677     */
678     debug4("tcl: builtin dcc call: %s %s %s %s", argv[0], argv[1], argv[2],
679     (!strcmp(argv[0] + 5, "newpass") || !strcmp(argv[0] + 5, "chpass")) ?
680     "[something]" : argv[3]);
681     F(dcc[idx].user, idx, argv[3]);
682     Tcl_ResetResult(irp);
683     Tcl_AppendResult(irp, "0", NULL);
684     return TCL_OK;
685     }
686    
687     static int builtin_log STDVAR
688     {
689     Function F = (Function) cd;
690    
691     BADARGS(3, 3, " lvl chan msg");
692    
693     CHECKVALIDITY(builtin_log);
694     F(argv[1], argv[2], argv[3]);
695     return TCL_OK;
696     }
697    
698 pseudo 1.2 #ifdef TLS
699     static int builtin_idx STDVAR
700     {
701     Function F = (Function) cd;
702    
703     BADARGS(2, 2, " idx");
704    
705     CHECKVALIDITY(builtin_idx);
706     F(atoi(argv[1]));
707     return TCL_OK;
708     }
709     #endif
710    
711 simple 1.1 /* Trigger (execute) a Tcl proc
712     *
713     * Note: This is INLINE code for check_tcl_bind().
714     */
715     static inline int trigger_bind(const char *proc, const char *param,
716     char *mask)
717     {
718     int x;
719     #ifdef DEBUG_CONTEXT
720     const char *msg = "Tcl proc: %s, param: %s";
721     char *buf;
722    
723     /* We now try to debug the Tcl_VarEval() call below by remembering both
724     * the called proc name and it's parameters. This should render us a bit
725     * less helpless when we see context dumps.
726     */
727     Context;
728     buf = nmalloc(strlen(msg) + (proc ? strlen(proc) : 6)
729     + (param ? strlen(param) : 6) + 1);
730     sprintf(buf, msg, proc ? proc : "<null>", param ? param : "<null>");
731     ContextNote(buf);
732     nfree(buf);
733     #endif /* DEBUG_CONTEXT */
734    
735     /* Set the lastbind variable before evaluating the proc so that the name
736     * of the command that triggered the bind will be available to the proc.
737     * This feature is used by scripts such as userinfo.tcl
738     */
739     Tcl_SetVar(interp, "lastbind", (char *) mask, TCL_GLOBAL_ONLY);
740    
741     x = Tcl_VarEval(interp, proc, param, NULL);
742     Context;
743    
744     if (x == TCL_ERROR) {
745     /* FIXME: we really should be able to log longer errors */
746     putlog(LOG_MISC, "*", "Tcl error [%s]: %.*s", proc, 400, tcl_resultstring());
747    
748     return BIND_EXECUTED;
749     }
750    
751     /* FIXME: This is an ugly hack. It is not documented as a
752     * 'feature' because it will eventually go away.
753     */
754     if (!strcmp(tcl_resultstring(), "break"))
755     return BIND_QUIT;
756    
757     return (tcl_resultint() > 0) ? BIND_EXEC_LOG : BIND_EXECUTED;
758     }
759    
760    
761     /* Find out whether this bind matches the mask or provides the
762     * requested attributes, depending on the specified requirements.
763     *
764     * Note: This is INLINE code for check_tcl_bind().
765     */
766     static inline int check_bind_match(const char *match, char *mask,
767     int match_type)
768     {
769     switch (match_type & 0x07) {
770     case MATCH_PARTIAL:
771     return (!egg_strncasecmp(match, mask, strlen(match)));
772     break;
773     case MATCH_EXACT:
774     return (!egg_strcasecmp(match, mask));
775     break;
776     case MATCH_CASE:
777     return (!strcmp(match, mask));
778     break;
779     case MATCH_MASK:
780     return (wild_match_per(mask, match));
781     break;
782     case MATCH_MODE:
783     return (wild_match_partial_case(mask, match));
784     break;
785     case MATCH_CRON:
786     return (cron_match(mask, match));
787     break;
788     default:
789     /* Do nothing */
790     break;
791     }
792     return 0;
793     }
794    
795    
796     /* Check if the provided flags suffice for this command/trigger.
797     *
798     * Note: This is INLINE code for check_tcl_bind().
799     */
800     static inline int check_bind_flags(struct flag_record *flags,
801     struct flag_record *atr, int match_type)
802     {
803     if (match_type & BIND_USE_ATTR) {
804     if (match_type & BIND_HAS_BUILTINS)
805     return (flagrec_ok(flags, atr));
806     else
807     return (flagrec_eq(flags, atr));
808     } else
809     return 1;
810     return 0;
811     }
812    
813    
814     /* Check for and process Tcl binds */
815     int check_tcl_bind(tcl_bind_list_t *tl, const char *match,
816     struct flag_record *atr, const char *param, int match_type)
817     {
818     int x, result = 0, cnt = 0, finish = 0;
819     char *proc = NULL, *mask = NULL;
820     tcl_bind_mask_t *tm, *tm_last = NULL, *tm_p = NULL;
821     tcl_cmd_t *tc, *htc = NULL;
822    
823     for (tm = tl->first; tm && !finish; tm_last = tm, tm = tm->next) {
824    
825     if (tm->flags & TBM_DELETED)
826     continue; /* This bind mask was deleted */
827    
828     if (!check_bind_match(match, tm->mask, match_type))
829     continue; /* This bind does not match. */
830    
831     for (tc = tm->first; tc; tc = tc->next) {
832    
833     /* Search for valid entry. */
834     if (!(tc->attributes & TC_DELETED)) {
835    
836     /* Check if the provided flags suffice for this command. */
837     if (check_bind_flags(&tc->flags, atr, match_type)) {
838     cnt++;
839     tm_p = tm_last;
840    
841     /* Not stackable */
842     if (!(match_type & BIND_STACKABLE)) {
843    
844     /* Remember information about this bind. */
845     proc = tc->func_name;
846     mask = tm->mask;
847     htc = tc;
848    
849     /* Either this is a non-partial match, which means we
850     * only want to execute _one_ bind ...
851     */
852     if ((match_type & 0x07) != MATCH_PARTIAL ||
853     /* ... or this happens to be an exact match. */
854     !egg_strcasecmp(match, tm->mask)) {
855     cnt = 1;
856     finish = 1;
857     }
858    
859     /* We found a match so break out of the inner loop. */
860     break;
861     }
862    
863     /*
864     * Stackable; could be multiple commands/triggers.
865     * Note: This code assumes BIND_ALTER_ARGS, BIND_WANTRET, and
866     * BIND_STACKRET will only be used for stackable binds.
867     */
868    
869     /* We will only return if BIND_ALTER_ARGS or BIND_WANTRET was
870     * specified because we want to trigger all binds in a stack.
871     */
872    
873     tc->hits++;
874     x = trigger_bind(tc->func_name, param, tm->mask);
875    
876     if (match_type & BIND_ALTER_ARGS) {
877     if (tcl_resultempty())
878     return x;
879     } else if ((match_type & BIND_STACKRET) && x == BIND_EXEC_LOG) {
880     /* If we have multiple commands/triggers, and if any of the
881     * commands return 1, we store the result so we can return it
882     * after processing all stacked binds.
883     */
884     if (!result)
885     result = x;
886     continue;
887     } else if ((match_type & BIND_WANTRET) && x == BIND_EXEC_LOG)
888     /* Return immediately if any commands return 1 */
889     return x;
890     }
891     }
892     }
893     }
894    
895     if (!cnt)
896     return BIND_NOMATCH;
897    
898     /* Do this before updating the preferred entries information,
899     * since we don't want to change the order of stacked binds
900     */
901     if (result) /* BIND_STACKRET */
902     return result;
903    
904     if ((match_type & 0x07) == MATCH_MASK || (match_type & 0x07) == MATCH_CASE)
905     return BIND_EXECUTED;
906    
907     /* Hit counter */
908     if (htc)
909     htc->hits++;
910    
911     /* Now that we have found at least one bind, we can update the
912     * preferred entries information.
913     */
914     if (tm_p && tm_p->next) {
915     tm = tm_p->next; /* Move mask to front of bind's mask list. */
916     tm_p->next = tm->next; /* Unlink mask from list. */
917     tm->next = tl->first; /* Readd mask to front of list. */
918     tl->first = tm;
919     }
920    
921     if (cnt > 1)
922     return BIND_AMBIGUOUS;
923    
924     return trigger_bind(proc, param, mask);
925     }
926    
927    
928     /* Check for tcl-bound dcc command, return 1 if found
929     * dcc: proc-name <handle> <sock> <args...>
930     */
931     int check_tcl_dcc(const char *cmd, int idx, const char *args)
932     {
933     struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
934     int x;
935     char s[11];
936    
937     get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
938     egg_snprintf(s, sizeof s, "%ld", dcc[idx].sock);
939     Tcl_SetVar(interp, "_dcc1", (char *) dcc[idx].nick, 0);
940     Tcl_SetVar(interp, "_dcc2", (char *) s, 0);
941     Tcl_SetVar(interp, "_dcc3", (char *) args, 0);
942     x = check_tcl_bind(H_dcc, cmd, &fr, " $_dcc1 $_dcc2 $_dcc3",
943     MATCH_PARTIAL | BIND_USE_ATTR | BIND_HAS_BUILTINS);
944     if (x == BIND_AMBIGUOUS) {
945     dprintf(idx, MISC_AMBIGUOUS);
946     return 0;
947     }
948     if (x == BIND_NOMATCH) {
949     dprintf(idx, MISC_NOSUCHCMD);
950     return 0;
951     }
952    
953     /* We return 1 to leave the partyline */
954     if (x == BIND_QUIT) /* CMD_LEAVE, 'quit' */
955     return 1;
956    
957     if (x == BIND_EXEC_LOG)
958     putlog(LOG_CMDS, "*", "#%s# %s %s", dcc[idx].nick, cmd, args);
959     return 0;
960     }
961    
962     void check_tcl_bot(const char *nick, const char *code, const char *param)
963     {
964     Tcl_SetVar(interp, "_bot1", (char *) nick, 0);
965     Tcl_SetVar(interp, "_bot2", (char *) code, 0);
966     Tcl_SetVar(interp, "_bot3", (char *) param, 0);
967     check_tcl_bind(H_bot, code, 0, " $_bot1 $_bot2 $_bot3", MATCH_EXACT);
968     }
969    
970     void check_tcl_chonof(char *hand, int sock, tcl_bind_list_t *tl)
971     {
972     struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
973     char s[11];
974     struct userrec *u;
975    
976     u = get_user_by_handle(userlist, hand);
977     touch_laston(u, "partyline", now);
978     get_user_flagrec(u, &fr, NULL);
979     Tcl_SetVar(interp, "_chonof1", (char *) hand, 0);
980     egg_snprintf(s, sizeof s, "%d", sock);
981     Tcl_SetVar(interp, "_chonof2", (char *) s, 0);
982     check_tcl_bind(tl, hand, &fr, " $_chonof1 $_chonof2", MATCH_MASK |
983     BIND_USE_ATTR | BIND_STACKABLE | BIND_WANTRET);
984     }
985    
986     void check_tcl_chatactbcst(const char *from, int chan, const char *text,
987     tcl_bind_list_t *tl)
988     {
989     char s[11];
990    
991     egg_snprintf(s, sizeof s, "%d", chan);
992     Tcl_SetVar(interp, "_cab1", (char *) from, 0);
993     Tcl_SetVar(interp, "_cab2", (char *) s, 0);
994     Tcl_SetVar(interp, "_cab3", (char *) text, 0);
995     check_tcl_bind(tl, text, 0, " $_cab1 $_cab2 $_cab3",
996     MATCH_MASK | BIND_STACKABLE);
997     }
998    
999     void check_tcl_nkch(const char *ohand, const char *nhand)
1000     {
1001     Tcl_SetVar(interp, "_nkch1", (char *) ohand, 0);
1002     Tcl_SetVar(interp, "_nkch2", (char *) nhand, 0);
1003     check_tcl_bind(H_nkch, ohand, 0, " $_nkch1 $_nkch2",
1004     MATCH_MASK | BIND_STACKABLE);
1005     }
1006    
1007     void check_tcl_link(const char *bot, const char *via)
1008     {
1009     Tcl_SetVar(interp, "_link1", (char *) bot, 0);
1010     Tcl_SetVar(interp, "_link2", (char *) via, 0);
1011     check_tcl_bind(H_link, bot, 0, " $_link1 $_link2",
1012     MATCH_MASK | BIND_STACKABLE);
1013     }
1014    
1015     void check_tcl_disc(const char *bot)
1016     {
1017     Tcl_SetVar(interp, "_disc1", (char *) bot, 0);
1018     check_tcl_bind(H_disc, bot, 0, " $_disc1", MATCH_MASK | BIND_STACKABLE);
1019     }
1020    
1021     void check_tcl_loadunld(const char *mod, tcl_bind_list_t *tl)
1022     {
1023     Tcl_SetVar(interp, "_lu1", (char *) mod, 0);
1024     check_tcl_bind(tl, mod, 0, " $_lu1", MATCH_MASK | BIND_STACKABLE);
1025     }
1026    
1027     const char *check_tcl_filt(int idx, const char *text)
1028     {
1029     char s[11];
1030     int x;
1031     struct flag_record fr = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
1032    
1033     egg_snprintf(s, sizeof s, "%ld", dcc[idx].sock);
1034     get_user_flagrec(dcc[idx].user, &fr, dcc[idx].u.chat->con_chan);
1035     Tcl_SetVar(interp, "_filt1", (char *) s, 0);
1036     Tcl_SetVar(interp, "_filt2", (char *) text, 0);
1037     x = check_tcl_bind(H_filt, text, &fr, " $_filt1 $_filt2",
1038     MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE |
1039     BIND_WANTRET | BIND_ALTER_ARGS);
1040     if (x == BIND_EXECUTED || x == BIND_EXEC_LOG) {
1041     if (tcl_resultempty())
1042     return "";
1043     else
1044     return tcl_resultstring();
1045     } else
1046     return text;
1047     }
1048    
1049     int check_tcl_note(const char *from, const char *to, const char *text)
1050     {
1051     int x;
1052    
1053     Tcl_SetVar(interp, "_note1", (char *) from, 0);
1054     Tcl_SetVar(interp, "_note2", (char *) to, 0);
1055     Tcl_SetVar(interp, "_note3", (char *) text, 0);
1056    
1057     x = check_tcl_bind(H_note, to, 0, " $_note1 $_note2 $_note3",
1058     MATCH_MASK | BIND_STACKABLE | BIND_WANTRET);
1059    
1060     return (x == BIND_EXEC_LOG);
1061     }
1062    
1063     void check_tcl_listen(const char *cmd, int idx)
1064     {
1065     char s[11];
1066     int x;
1067    
1068     egg_snprintf(s, sizeof s, "%d", idx);
1069     Tcl_SetVar(interp, "_n", (char *) s, 0);
1070     x = Tcl_VarEval(interp, cmd, " $_n", NULL);
1071     if (x == TCL_ERROR)
1072     putlog(LOG_MISC, "*", "error on listen: %s", tcl_resultstring());
1073     }
1074    
1075     void check_tcl_chjn(const char *bot, const char *nick, int chan,
1076     const char type, int sock, const char *host)
1077     {
1078     struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
1079     char s[11], t[2], u[11];
1080    
1081     t[0] = type;
1082     t[1] = 0;
1083     switch (type) {
1084     case '*':
1085     fr.global = USER_OWNER;
1086    
1087     break;
1088     case '+':
1089     fr.global = USER_MASTER;
1090    
1091     break;
1092     case '@':
1093     fr.global = USER_OP;
1094    
1095     break;
1096     case '^':
1097     fr.global = USER_HALFOP;
1098    
1099     break;
1100     case '%':
1101     fr.global = USER_BOTMAST;
1102     }
1103     egg_snprintf(s, sizeof s, "%d", chan);
1104     egg_snprintf(u, sizeof u, "%d", sock);
1105     Tcl_SetVar(interp, "_chjn1", (char *) bot, 0);
1106     Tcl_SetVar(interp, "_chjn2", (char *) nick, 0);
1107     Tcl_SetVar(interp, "_chjn3", (char *) s, 0);
1108     Tcl_SetVar(interp, "_chjn4", (char *) t, 0);
1109     Tcl_SetVar(interp, "_chjn5", (char *) u, 0);
1110     Tcl_SetVar(interp, "_chjn6", (char *) host, 0);
1111     check_tcl_bind(H_chjn, s, &fr,
1112     " $_chjn1 $_chjn2 $_chjn3 $_chjn4 $_chjn5 $_chjn6",
1113     MATCH_MASK | BIND_STACKABLE);
1114     }
1115    
1116     void check_tcl_chpt(const char *bot, const char *hand, int sock, int chan)
1117     {
1118     char u[11], v[11];
1119    
1120     egg_snprintf(u, sizeof u, "%d", sock);
1121     egg_snprintf(v, sizeof v, "%d", chan);
1122     Tcl_SetVar(interp, "_chpt1", (char *) bot, 0);
1123     Tcl_SetVar(interp, "_chpt2", (char *) hand, 0);
1124     Tcl_SetVar(interp, "_chpt3", (char *) u, 0);
1125     Tcl_SetVar(interp, "_chpt4", (char *) v, 0);
1126     check_tcl_bind(H_chpt, v, 0, " $_chpt1 $_chpt2 $_chpt3 $_chpt4",
1127     MATCH_MASK | BIND_STACKABLE);
1128     }
1129    
1130     void check_tcl_away(const char *bot, int idx, const char *msg)
1131     {
1132     char u[11];
1133    
1134     egg_snprintf(u, sizeof u, "%d", idx);
1135     Tcl_SetVar(interp, "_away1", (char *) bot, 0);
1136     Tcl_SetVar(interp, "_away2", (char *) u, 0);
1137     Tcl_SetVar(interp, "_away3", msg ? (char *) msg : "", 0);
1138     check_tcl_bind(H_away, bot, 0, " $_away1 $_away2 $_away3",
1139     MATCH_MASK | BIND_STACKABLE);
1140     }
1141    
1142     void check_tcl_time(struct tm *tm)
1143     {
1144     char y[18];
1145    
1146     egg_snprintf(y, sizeof y, "%02d", tm->tm_min);
1147     Tcl_SetVar(interp, "_time1", (char *) y, 0);
1148     egg_snprintf(y, sizeof y, "%02d", tm->tm_hour);
1149     Tcl_SetVar(interp, "_time2", (char *) y, 0);
1150     egg_snprintf(y, sizeof y, "%02d", tm->tm_mday);
1151     Tcl_SetVar(interp, "_time3", (char *) y, 0);
1152     egg_snprintf(y, sizeof y, "%02d", tm->tm_mon);
1153     Tcl_SetVar(interp, "_time4", (char *) y, 0);
1154     egg_snprintf(y, sizeof y, "%04d", tm->tm_year + 1900);
1155     Tcl_SetVar(interp, "_time5", (char *) y, 0);
1156     egg_snprintf(y, sizeof y, "%02d %02d %02d %02d %04d", tm->tm_min, tm->tm_hour,
1157     tm->tm_mday, tm->tm_mon, tm->tm_year + 1900);
1158     check_tcl_bind(H_time, y, 0,
1159     " $_time1 $_time2 $_time3 $_time4 $_time5",
1160     MATCH_MASK | BIND_STACKABLE);
1161     }
1162    
1163     void check_tcl_cron(struct tm *tm)
1164     {
1165     char y[15];
1166    
1167     egg_snprintf(y, sizeof y, "%02d", tm->tm_min);
1168     Tcl_SetVar(interp, "_cron1", (char *) y, 0);
1169     egg_snprintf(y, sizeof y, "%02d", tm->tm_hour);
1170     Tcl_SetVar(interp, "_cron2", (char *) y, 0);
1171     egg_snprintf(y, sizeof y, "%02d", tm->tm_mday);
1172     Tcl_SetVar(interp, "_cron3", (char *) y, 0);
1173     egg_snprintf(y, sizeof y, "%02d", tm->tm_mon + 1);
1174     Tcl_SetVar(interp, "_cron4", (char *) y, 0);
1175     egg_snprintf(y, sizeof y, "%02d", tm->tm_wday);
1176     Tcl_SetVar(interp, "_cron5", (char *) y, 0);
1177     egg_snprintf(y, sizeof y, "%02d %02d %02d %02d %02d", tm->tm_min, tm->tm_hour,
1178     tm->tm_mday, tm->tm_mon + 1, tm->tm_wday);
1179     check_tcl_bind(H_cron, y, 0,
1180     " $_cron1 $_cron2 $_cron3 $_cron4 $_cron5",
1181     MATCH_CRON | BIND_STACKABLE);
1182     }
1183    
1184 pseudo 1.3 int check_tcl_event(const char *event)
1185 simple 1.1 {
1186 pseudo 1.3 int x;
1187    
1188 simple 1.1 Tcl_SetVar(interp, "_event1", (char *) event, 0);
1189 pseudo 1.3 x = check_tcl_bind(H_event, event, 0, " $_event1",
1190     MATCH_EXACT | BIND_STACKABLE | BIND_WANTRET);
1191     return (x == BIND_EXEC_LOG);
1192     }
1193    
1194     void check_tcl_die(char *reason)
1195     {
1196     Tcl_SetVar(interp, "_die1", reason, 0);
1197     check_tcl_bind(H_die, reason, 0, " $_die1", MATCH_MASK | BIND_STACKABLE);
1198 simple 1.1 }
1199    
1200     void check_tcl_log(int lv, char *chan, char *msg)
1201     {
1202     char mask[512];
1203    
1204     snprintf(mask, sizeof mask, "%s %s", chan, msg);
1205     Tcl_SetVar(interp, "_log1", masktype(lv), 0);
1206     Tcl_SetVar(interp, "_log2", chan, 0);
1207     Tcl_SetVar(interp, "_log3", msg, 0);
1208     check_tcl_bind(H_log, mask, 0, " $_log1 $_log2 $_log3",
1209     MATCH_MASK | BIND_STACKABLE);
1210     }
1211    
1212 pseudo 1.2 #ifdef TLS
1213     int check_tcl_tls(int sock)
1214     {
1215     int x;
1216     char s[11];
1217    
1218     egg_snprintf(s, sizeof s, "%d", sock);
1219     Tcl_SetVar(interp, "_tls", s, 0);
1220     x = check_tcl_bind(H_tls, s, 0, " $_tls", MATCH_MASK | BIND_STACKABLE |
1221     BIND_WANTRET);
1222     return (x == BIND_EXEC_LOG);
1223     }
1224     #endif
1225    
1226 simple 1.1 void tell_binds(int idx, char *par)
1227     {
1228     tcl_bind_list_t *tl, *tl_kind;
1229     tcl_bind_mask_t *tm;
1230     int fnd = 0, showall = 0, patmatc = 0;
1231     tcl_cmd_t *tc;
1232     char *name, *proc, *s, flg[100];
1233    
1234     if (par[0])
1235     name = newsplit(&par);
1236     else
1237     name = NULL;
1238     if (par[0])
1239     s = newsplit(&par);
1240     else
1241     s = NULL;
1242    
1243     if (name)
1244     tl_kind = find_bind_table(name);
1245     else
1246     tl_kind = NULL;
1247    
1248     if ((name && name[0] && !egg_strcasecmp(name, "all")) ||
1249     (s && s[0] && !egg_strcasecmp(s, "all")))
1250     showall = 1;
1251     if (tl_kind == NULL && name && name[0] && egg_strcasecmp(name, "all"))
1252     patmatc = 1;
1253    
1254     dprintf(idx, MISC_CMDBINDS);
1255     dprintf(idx, " TYPE FLAGS COMMAND HITS BINDING (TCL)\n");
1256    
1257     for (tl = tl_kind ? tl_kind : bind_table_list; tl;
1258     tl = tl_kind ? 0 : tl->next) {
1259     if (tl->flags & HT_DELETED)
1260     continue;
1261     for (tm = tl->first; tm; tm = tm->next) {
1262     if (tm->flags & TBM_DELETED)
1263     continue;
1264     for (tc = tm->first; tc; tc = tc->next) {
1265     if (tc->attributes & TC_DELETED)
1266     continue;
1267     proc = tc->func_name;
1268     build_flags(flg, &(tc->flags), NULL);
1269     if (showall || proc[0] != '*') {
1270     int ok = 0;
1271    
1272     if (patmatc == 1) {
1273     if (wild_match_per(name, tl->name) ||
1274     wild_match_per(name, tm->mask) ||
1275     wild_match_per(name, tc->func_name))
1276     ok = 1;
1277     } else
1278     ok = 1;
1279    
1280     if (ok) {
1281     dprintf(idx, " %-4s %-8s %-20s %4d %s\n", tl->name, flg, tm->mask,
1282     tc->hits, tc->func_name);
1283     fnd = 1;
1284     }
1285     }
1286     }
1287     }
1288     }
1289     if (!fnd) {
1290     if (patmatc)
1291     dprintf(idx, "No command bindings found that match %s\n", name);
1292     else if (tl_kind)
1293     dprintf(idx, "No command bindings for type: %s.\n", name);
1294     else
1295     dprintf(idx, "No command bindings exist.\n");
1296     }
1297     }
1298    
1299     /* Bring the default msg/dcc/fil commands into the Tcl interpreter */
1300     void add_builtins(tcl_bind_list_t *tl, cmd_t *cc)
1301     {
1302     int k, i;
1303     char p[1024], *l;
1304     cd_tcl_cmd table[2];
1305    
1306     table[0].name = p;
1307     table[0].callback = tl->func;
1308     table[1].name = NULL;
1309     for (i = 0; cc[i].name; i++) {
1310     egg_snprintf(p, sizeof p, "*%s:%s", tl->name,
1311     cc[i].funcname ? cc[i].funcname : cc[i].name);
1312     l = nmalloc(Tcl_ScanElement(p, &k));
1313     Tcl_ConvertElement(p, l, k | TCL_DONT_USE_BRACES);
1314     table[0].cdata = (void *) cc[i].func;
1315     add_cd_tcl_cmds(table);
1316     bind_bind_entry(tl, cc[i].flags, cc[i].name, l);
1317     nfree(l);
1318     }
1319     }
1320    
1321     /* Remove the default msg/dcc/fil commands from the Tcl interpreter */
1322     void rem_builtins(tcl_bind_list_t *table, cmd_t *cc)
1323     {
1324     int k, i;
1325     char p[1024], *l;
1326    
1327     for (i = 0; cc[i].name; i++) {
1328     egg_snprintf(p, sizeof p, "*%s:%s", table->name,
1329     cc[i].funcname ? cc[i].funcname : cc[i].name);
1330     l = nmalloc(Tcl_ScanElement(p, &k));
1331     Tcl_ConvertElement(p, l, k | TCL_DONT_USE_BRACES);
1332     Tcl_DeleteCommand(interp, p);
1333     unbind_bind_entry(table, cc[i].flags, cc[i].name, l);
1334     nfree(l);
1335     }
1336     }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23