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

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

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


Revision 1.11 - (show annotations) (download) (as text)
Wed Aug 2 05:05:41 2000 UTC (19 years, 3 months ago) by guppy
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +41 -1 lines
File MIME type: text/x-chdr
tcl_binds

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23