/[cvs]/eggdrop1.9/src/tclmisc.c
ViewVC logotype

Contents of /eggdrop1.9/src/tclmisc.c

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


Revision 1.29 - (show annotations) (download) (as text)
Wed Oct 10 14:50:01 2001 UTC (18 years, 1 month ago) by tothwolf
Branch: MAIN
Changes since 1.28: +2 -2 lines
File MIME type: text/x-chdr
moved md5.c and md5.h from src/md5/ to src/
added header files in src/ to src/Makefile.am
removed redundant egglib/libegg.la entry for eggdrop_LDADD
fixed -DEBUG_ASSERT typo in acinclude.m4

1 /*
2 * tclmisc.c -- handles:
3 * Tcl stubs for file system commands
4 * Tcl stubs for everything else
5 *
6 * $Id: tclmisc.c,v 1.28 2001/08/19 02:36:23 drummer Exp $
7 */
8 /*
9 * Copyright (C) 1997 Robey Pointer
10 * Copyright (C) 1999, 2000, 2001 Eggheads Development Team
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 #include <sys/stat.h>
28 #include "main.h"
29 #include "modules.h"
30 #include "tandem.h"
31 #include "md5.h"
32 #ifdef HAVE_UNAME
33 #include <sys/utsname.h>
34 #endif
35
36 extern p_tcl_bind_list bind_table_list;
37 extern tcl_timer_t *timer, *utimer;
38 extern struct dcc_t *dcc;
39 extern char origbotname[], botnetnick[], quit_msg[];
40 extern struct userrec *userlist;
41 extern time_t now;
42 extern module_entry *module_list;
43
44
45 static int tcl_putlog STDVAR
46 {
47 char logtext[501];
48
49 BADARGS(2, 2, " text");
50 strncpyz(logtext, argv[1], sizeof logtext);
51 putlog(LOG_MISC, "*", "%s", logtext);
52 return TCL_OK;
53 }
54
55 static int tcl_putcmdlog STDVAR
56 {
57 char logtext[501];
58
59 BADARGS(2, 2, " text");
60 strncpyz(logtext, argv[1], sizeof logtext);
61 putlog(LOG_CMDS, "*", "%s", logtext);
62 return TCL_OK;
63 }
64
65 static int tcl_putxferlog STDVAR
66 {
67 char logtext[501];
68
69 BADARGS(2, 2, " text");
70 strncpyz(logtext, argv[1], sizeof logtext);
71 putlog(LOG_FILES, "*", "%s", logtext);
72 return TCL_OK;
73 }
74
75 static int tcl_putloglev STDVAR
76 {
77 int lev = 0;
78 char logtext[501];
79
80 BADARGS(4, 4, " level channel text");
81 lev = logmodes(argv[1]);
82 if (!lev) {
83 Tcl_AppendResult(irp, "No valid log-level given", NULL);
84 return TCL_ERROR;
85 }
86 strncpyz(logtext, argv[3], sizeof logtext);
87 putlog(lev, argv[2], "%s", logtext);
88 return TCL_OK;
89 }
90
91 static int tcl_binds STDVAR
92 {
93 tcl_bind_list_t *tl, *tl_kind;
94 tcl_bind_mask_t *tm;
95 tcl_cmd_t *tc;
96 char *list[5], *g, flg[100], hits[11];
97 int matching = 0;
98
99 BADARGS(1, 2, " ?type/mask?");
100 if (argv[1])
101 tl_kind = find_bind_table(argv[1]);
102 else
103 tl_kind = NULL;
104 if (!tl_kind && argv[1])
105 matching = 1;
106 for (tl = tl_kind ? tl_kind : bind_table_list; tl;
107 tl = tl_kind ? 0 : tl->next) {
108 if (tl->flags & HT_DELETED)
109 continue;
110 for (tm = tl->first; tm; tm = tm->next) {
111 if (tm->flags & TBM_DELETED)
112 continue;
113 for (tc = tm->first; tc; tc = tc->next) {
114 if (tc->attributes & TC_DELETED)
115 continue;
116 if (matching &&
117 !wild_match(argv[1], tl->name) &&
118 !wild_match(argv[1], tm->mask) &&
119 !wild_match(argv[1], tc->func_name))
120 continue;
121 build_flags(flg, &(tc->flags), NULL);
122 egg_snprintf(hits, sizeof hits, "%i", (int) tc->hits);
123 list[0] = tl->name;
124 list[1] = flg;
125 list[2] = tm->mask;
126 list[3] = hits;
127 list[4] = tc->func_name;
128 g = Tcl_Merge(5, list);
129 Tcl_AppendElement(irp, g);
130 Tcl_Free((char *) g);
131 }
132 }
133 }
134 return TCL_OK;
135 }
136
137 static int tcl_timer STDVAR
138 {
139 unsigned long x;
140 char s[16];
141
142 BADARGS(3, 3, " minutes command");
143 if (atoi(argv[1]) < 0) {
144 Tcl_AppendResult(irp, "time value must be positive", NULL);
145 return TCL_ERROR;
146 }
147 if (argv[2][0] != '#') {
148 x = add_timer(&timer, atoi(argv[1]), argv[2], 0L);
149 egg_snprintf(s, sizeof s, "timer%lu", x);
150 Tcl_AppendResult(irp, s, NULL);
151 }
152 return TCL_OK;
153 }
154
155 static int tcl_utimer STDVAR
156 {
157 unsigned long x;
158 char s[16];
159
160 BADARGS(3, 3, " seconds command");
161 if (atoi(argv[1]) < 0) {
162 Tcl_AppendResult(irp, "time value must be positive", NULL);
163 return TCL_ERROR;
164 }
165 if (argv[2][0] != '#') {
166 x = add_timer(&utimer, atoi(argv[1]), argv[2], 0L);
167 egg_snprintf(s, sizeof s, "timer%lu", x);
168 Tcl_AppendResult(irp, s, NULL);
169 }
170 return TCL_OK;
171 }
172
173 static int tcl_killtimer STDVAR
174 {
175 BADARGS(2, 2, " timerID");
176 if (strncmp(argv[1], "timer", 5)) {
177 Tcl_AppendResult(irp, "argument is not a timerID", NULL);
178 return TCL_ERROR;
179 }
180 if (remove_timer(&timer, atol(&argv[1][5])))
181 return TCL_OK;
182 Tcl_AppendResult(irp, "invalid timerID", NULL);
183 return TCL_ERROR;
184 }
185
186 static int tcl_killutimer STDVAR
187 {
188 BADARGS(2, 2, " timerID");
189 if (strncmp(argv[1], "timer", 5)) {
190 Tcl_AppendResult(irp, "argument is not a timerID", NULL);
191 return TCL_ERROR;
192 }
193 if (remove_timer(&utimer, atol(&argv[1][5])))
194 return TCL_OK;
195 Tcl_AppendResult(irp, "invalid timerID", NULL);
196 return TCL_ERROR;
197 }
198
199 static int tcl_timers STDVAR
200 {
201 BADARGS(1, 1, "");
202 list_timers(irp, timer);
203 return TCL_OK;
204 }
205
206 static int tcl_utimers STDVAR
207 {
208 BADARGS(1, 1, "");
209 list_timers(irp, utimer);
210 return TCL_OK;
211 }
212
213 static int tcl_duration STDVAR
214 {
215 char s[70];
216 unsigned long sec, tmp;
217
218 BADARGS(2, 2, " seconds");
219 if (atol(argv[1]) <= 0) {
220 Tcl_AppendResult(irp, "0 seconds", NULL);
221 return TCL_OK;
222 }
223 sec = atol(argv[1]);
224 s[0] = 0;
225 if (sec >= 31536000) {
226 tmp = (sec / 31536000);
227 sprintf(s, "%lu year%s ", tmp, (tmp == 1) ? "" : "s");
228 sec -= (tmp * 31536000);
229 }
230 if (sec >= 604800) {
231 tmp = (sec / 604800);
232 sprintf(&s[strlen(s)], "%lu week%s ", tmp, (tmp == 1) ? "" : "s");
233 sec -= (tmp * 604800);
234 }
235 if (sec >= 86400) {
236 tmp = (sec / 86400);
237 sprintf(&s[strlen(s)], "%lu day%s ", tmp, (tmp == 1) ? "" : "s");
238 sec -= (tmp * 86400);
239 }
240 if (sec >= 3600) {
241 tmp = (sec / 3600);
242 sprintf(&s[strlen(s)], "%lu hour%s ", tmp, (tmp == 1) ? "" : "s");
243 sec -= (tmp * 3600);
244 }
245 if (sec >= 60) {
246 tmp = (sec / 60);
247 sprintf(&s[strlen(s)], "%lu minute%s ", tmp, (tmp == 1) ? "" : "s");
248 sec -= (tmp * 60);
249 }
250 if (sec > 0) {
251 tmp = (sec);
252 sprintf(&s[strlen(s)], "%lu second%s", tmp, (tmp == 1) ? "" : "s");
253 }
254 Tcl_AppendResult(irp, s, NULL);
255 return TCL_OK;
256 }
257
258 static int tcl_unixtime STDVAR
259 {
260 char s[11];
261
262 BADARGS(1, 1, "");
263 egg_snprintf(s, sizeof s, "%lu", (unsigned long) now);
264 Tcl_AppendResult(irp, s, NULL);
265 return TCL_OK;
266 }
267
268 static int tcl_ctime STDVAR
269 {
270 time_t tt;
271 char s[25];
272
273 BADARGS(2, 2, " unixtime");
274 tt = (time_t) atol(argv[1]);
275 strncpyz(s, ctime(&tt), sizeof s);
276 Tcl_AppendResult(irp, s, NULL);
277 return TCL_OK;
278 }
279
280 static int tcl_strftime STDVAR
281 {
282 char buf[512];
283 struct tm *tm1;
284 time_t t;
285
286 BADARGS(2, 3, " format ?time?");
287 if (argc == 3)
288 t = atol(argv[2]);
289 else
290 t = now;
291 tm1 = localtime(&t);
292 if (egg_strftime(buf, sizeof(buf) - 1, argv[1], tm1)) {
293 Tcl_AppendResult(irp, buf, NULL);
294 return TCL_OK;
295 }
296 Tcl_AppendResult(irp, " error with strftime", NULL);
297 return TCL_ERROR;
298 }
299
300 static int tcl_myip STDVAR
301 {
302 char s[16];
303
304 BADARGS(1, 1, "");
305 egg_snprintf(s, sizeof s, "%lu", iptolong(getmyip()));
306 Tcl_AppendResult(irp, s, NULL);
307 return TCL_OK;
308 }
309
310 static int tcl_myip6 STDVAR
311 {
312 BADARGS(1, 1, "");
313 #ifdef IPV6
314 Tcl_AppendResult(irp, "", NULL); /* FIXME!! */
315 #else
316 Tcl_AppendResult(irp, "", NULL);
317 #endif
318 return TCL_OK;
319 }
320
321 static int tcl_rand STDVAR
322 {
323 unsigned long x;
324 char s[11];
325
326 BADARGS(2, 2, " limit");
327 if (atol(argv[1]) <= 0) {
328 Tcl_AppendResult(irp, "random limit must be greater than zero", NULL);
329 return TCL_ERROR;
330 }
331 x = random() % (atol(argv[1]));
332 egg_snprintf(s, sizeof s, "%lu", x);
333 Tcl_AppendResult(irp, s, NULL);
334 return TCL_OK;
335 }
336
337 static int tcl_sendnote STDVAR
338 {
339 char s[5], from[NOTENAMELEN + 1], to[NOTENAMELEN + 1], msg[451];
340
341 BADARGS(4, 4, " from to message");
342 strncpyz(from, argv[1], sizeof from);
343 strncpyz(to, argv[2], sizeof to);
344 strncpyz(msg, argv[3], sizeof msg);
345 egg_snprintf(s, sizeof s, "%d", add_note(to, from, msg, -1, 0));
346 Tcl_AppendResult(irp, s, NULL);
347 return TCL_OK;
348 }
349
350 static int tcl_dumpfile STDVAR
351 {
352 char nick[NICKLEN];
353 struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
354
355 BADARGS(3, 3, " nickname filename");
356 strncpyz(nick, argv[1], sizeof nick);
357 get_user_flagrec(get_user_by_nick(nick), &fr, NULL);
358 showhelp(argv[1], argv[2], &fr, HELP_TEXT);
359 return TCL_OK;
360 }
361
362 static int tcl_dccdumpfile STDVAR
363 {
364 int idx, i;
365 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
366
367 BADARGS(3, 3, " idx filename");
368 i = atoi(argv[1]);
369 idx = findidx(i);
370 if (idx < 0) {
371 Tcl_AppendResult(irp, "illegal idx", NULL);
372 return TCL_ERROR;
373 }
374 get_user_flagrec(get_user_by_handle(userlist, dcc[idx].nick), &fr, NULL);
375 tellhelp(idx, argv[2], &fr, HELP_TEXT);
376 return TCL_OK;
377 }
378
379 static int tcl_backup STDVAR
380 {
381 BADARGS(1, 1, "");
382 call_hook(HOOK_BACKUP);
383 return TCL_OK;
384 }
385
386 static int tcl_die STDVAR
387 {
388 char s[1024];
389
390 BADARGS(1, 2, " ?reason?");
391 if (argc == 2) {
392 egg_snprintf(s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
393 strncpyz(quit_msg, argv[1], 1024);
394 } else {
395 strncpyz(s, "BOT SHUTDOWN (No reason)", sizeof s);
396 quit_msg[0] = 0;
397 }
398 kill_bot(s, quit_msg[0] ? quit_msg : "EXIT");
399 return TCL_OK;
400 }
401
402 static int tcl_loadmodule STDVAR
403 {
404 const char *p;
405
406 BADARGS(2, 2, " module-name");
407 p = module_load(argv[1]);
408 if (p && strcmp(p, _("Already loaded.")))
409 putlog(LOG_MISC, "*", "%s %s: %s", _("Cant load modules"), argv[1], p);
410 Tcl_AppendResult(irp, p, NULL);
411 return TCL_OK;
412 }
413
414 static int tcl_unloadmodule STDVAR
415 {
416 BADARGS(2, 2, " module-name");
417 Tcl_AppendResult(irp, module_unload(argv[1], origbotname), NULL);
418 return TCL_OK;
419 }
420
421 static int tcl_unames STDVAR
422 {
423 char *unix_n, *vers_n;
424 #ifdef HAVE_UNAME
425 struct utsname un;
426
427 if (uname(&un) < 0) {
428 #endif
429 unix_n = "*unkown*";
430 vers_n = "";
431 #ifdef HAVE_UNAME
432 } else {
433 unix_n = un.sysname;
434 vers_n = un.release;
435 }
436 #endif
437 Tcl_AppendResult(irp, unix_n, " ", vers_n, NULL);
438 return TCL_OK;
439 }
440
441 static int tcl_modules STDVAR
442 {
443 module_entry *current;
444 dependancy *dep;
445 char *list[100], *list2[2], *p;
446 char s[24], s2[24];
447 int i;
448
449 BADARGS(1, 1, "");
450 for (current = module_list; current; current = current->next) {
451 list[0] = current->name;
452 egg_snprintf(s, sizeof s, "%d.%d", current->major, current->minor);
453 list[1] = s;
454 i = 2;
455 for (dep = dependancy_list; dep && (i < 100); dep = dep->next) {
456 if (dep->needing == current) {
457 list2[0] = dep->needed->name;
458 egg_snprintf(s2, sizeof s2, "%d.%d", dep->major, dep->minor);
459 list2[1] = s2;
460 list[i] = Tcl_Merge(2, list2);
461 i++;
462 }
463 }
464 p = Tcl_Merge(i, list);
465 Tcl_AppendElement(irp, p);
466 Tcl_Free((char *) p);
467 while (i > 2) {
468 i--;
469 Tcl_Free((char *) list[i]);
470 }
471 }
472 return TCL_OK;
473 }
474
475 static int tcl_loadhelp STDVAR
476 {
477 BADARGS(2, 2, " helpfile-name");
478 add_help_reference(argv[1]);
479 return TCL_OK;
480 }
481
482 static int tcl_unloadhelp STDVAR
483 {
484 BADARGS(2, 2, " helpfile-name");
485 rem_help_reference(argv[1]);
486 return TCL_OK;
487 }
488
489 static int tcl_reloadhelp STDVAR
490 {
491 BADARGS(1, 1, "");
492 reload_help_data();
493 return TCL_OK;
494 }
495
496 static int tcl_callevent STDVAR
497 {
498 BADARGS(2, 2, " event");
499 check_tcl_event(argv[1]);
500 return TCL_OK;
501 }
502
503 #if (TCL_MAJOR_VERSION >= 8)
504 static int tcl_md5(cd, irp, objc, objv)
505 ClientData cd;
506 Tcl_Interp *irp;
507 int objc;
508 Tcl_Obj *CONST objv[];
509 {
510 #else
511 static int tcl_md5 STDVAR
512 {
513 #endif
514 MD5_CTX md5context;
515 char digest_string[33], *string;
516 unsigned char digest[16];
517 int i, len;
518
519 #if (TCL_MAJOR_VERSION >= 8)
520 if (objc != 2) {
521 Tcl_WrongNumArgs(irp, 1, objv, "string");
522 return TCL_ERROR;
523 }
524
525 #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 1)
526 string = Tcl_GetStringFromObj(objv[1], &len);
527 #else
528 string = Tcl_GetByteArrayFromObj(objv[1], &len);
529 #endif
530
531 #else
532 BADARGS(2, 2, " string");
533 string = argv[1];
534 len = strlen(argv[1]);
535 #endif
536
537 MD5_Init(&md5context);
538 MD5_Update(&md5context, (unsigned char *)string, len);
539 MD5_Final(digest, &md5context);
540 for(i=0; i<16; i++)
541 sprintf(digest_string + (i*2), "%.2x", digest[i]);
542 Tcl_AppendResult(irp, digest_string, NULL);
543 return TCL_OK;
544 }
545
546 tcl_cmds tclmisc_objcmds[] =
547 {
548 #if (TCL_MAJOR_VERSION >= 8)
549 {"md5", tcl_md5},
550 #endif
551 {NULL, NULL}
552 };
553
554 tcl_cmds tclmisc_cmds[] =
555 {
556 {"putlog", tcl_putlog},
557 {"putcmdlog", tcl_putcmdlog},
558 {"putxferlog", tcl_putxferlog},
559 {"putloglev", tcl_putloglev},
560 {"timer", tcl_timer},
561 {"utimer", tcl_utimer},
562 {"killtimer", tcl_killtimer},
563 {"killutimer", tcl_killutimer},
564 {"timers", tcl_timers},
565 {"utimers", tcl_utimers},
566 {"unixtime", tcl_unixtime},
567 {"strftime", tcl_strftime},
568 {"ctime", tcl_ctime},
569 {"myip", tcl_myip},
570 {"rand", tcl_rand},
571 {"sendnote", tcl_sendnote},
572 {"dumpfile", tcl_dumpfile},
573 {"dccdumpfile", tcl_dccdumpfile},
574 {"backup", tcl_backup},
575 {"exit", tcl_die},
576 {"die", tcl_die},
577 {"unames", tcl_unames},
578 {"unloadmodule", tcl_unloadmodule},
579 {"loadmodule", tcl_loadmodule},
580 {"modules", tcl_modules},
581 {"loadhelp", tcl_loadhelp},
582 {"unloadhelp", tcl_unloadhelp},
583 {"reloadhelp", tcl_reloadhelp},
584 {"duration", tcl_duration},
585 #if (TCL_MAJOR_VERSION < 8)
586 {"md5", tcl_md5},
587 #endif
588 {"binds", tcl_binds},
589 {"callevent", tcl_callevent},
590 {"myip6", tcl_myip6},
591 {NULL, NULL}
592 };

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23