/[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.18 - (show annotations) (download) (as text)
Thu Apr 12 02:39:43 2001 UTC (18 years, 7 months ago) by guppy
Branch: MAIN
Changes since 1.17: +13 -13 lines
File MIME type: text/x-chdr
lets update the copyright info for 2001 k? :P~

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.17 2000/12/10 15:10:27 guppy 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/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[];
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[10];
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_rand STDVAR
311 {
312 unsigned long x;
313 char s[11];
314
315 BADARGS(2, 2, " limit");
316 if (atol(argv[1]) <= 0) {
317 Tcl_AppendResult(irp, "random limit must be greater than zero", NULL);
318 return TCL_ERROR;
319 }
320 x = random() % (atol(argv[1]));
321 egg_snprintf(s, sizeof s, "%lu", x);
322 Tcl_AppendResult(irp, s, NULL);
323 return TCL_OK;
324 }
325
326 static int tcl_sendnote STDVAR
327 {
328 char s[5], from[NOTENAMELEN + 1], to[NOTENAMELEN + 1], msg[451];
329
330 BADARGS(4, 4, " from to message");
331 strncpyz(from, argv[1], sizeof from);
332 strncpyz(to, argv[2], sizeof to);
333 strncpyz(msg, argv[3], sizeof msg);
334 egg_snprintf(s, sizeof s, "%d", add_note(to, from, msg, -1, 0));
335 Tcl_AppendResult(irp, s, NULL);
336 return TCL_OK;
337 }
338
339 static int tcl_dumpfile STDVAR
340 {
341 char nick[NICKLEN];
342 struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
343
344 BADARGS(3, 3, " nickname filename");
345 strncpyz(nick, argv[1], sizeof nick);
346 get_user_flagrec(get_user_by_nick(nick), &fr, NULL);
347 showhelp(argv[1], argv[2], &fr, HELP_TEXT);
348 return TCL_OK;
349 }
350
351 static int tcl_dccdumpfile STDVAR
352 {
353 int idx, i;
354 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
355
356 BADARGS(3, 3, " idx filename");
357 i = atoi(argv[1]);
358 idx = findidx(i);
359 if (idx < 0) {
360 Tcl_AppendResult(irp, "illegal idx", NULL);
361 return TCL_ERROR;
362 }
363 get_user_flagrec(get_user_by_handle(userlist, dcc[idx].nick), &fr, NULL);
364 tellhelp(idx, argv[2], &fr, HELP_TEXT);
365 return TCL_OK;
366 }
367
368 static int tcl_backup STDVAR
369 {
370 BADARGS(1, 1, "");
371 backup_userfile();
372 return TCL_OK;
373 }
374
375 static int tcl_die STDVAR
376 {
377 char s[501];
378 char g[501];
379
380 BADARGS(1, 2, " ?reason?");
381 if (argc == 2) {
382 egg_snprintf(s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
383 egg_snprintf(g, sizeof g, "%s", argv[1]);
384 } else {
385 egg_snprintf(s, sizeof s, "BOT SHUTDOWN (aboot time -- eh?)");
386 egg_snprintf(g, sizeof g, "EXIT");
387 }
388 chatout("*** %s\n", s);
389 botnet_send_chat(-1, botnetnick, s);
390 botnet_send_bye();
391 write_userfile(-1);
392 fatal(g, 0);
393 return TCL_OK;
394 }
395
396 static int tcl_loadmodule STDVAR
397 {
398 const char *p;
399
400 BADARGS(2, 2, " module-name");
401 p = module_load(argv[1]);
402 if (p && strcmp(p, MOD_ALREADYLOAD) && !strcmp(argv[0], "loadmodule"))
403 putlog(LOG_MISC, "*", "%s %s: %s", MOD_CANTLOADMOD, argv[1], p);
404 Tcl_AppendResult(irp, p, NULL);
405 return TCL_OK;
406 }
407
408 static int tcl_unloadmodule STDVAR
409 {
410 BADARGS(2, 2, " module-name");
411 Tcl_AppendResult(irp, module_unload(argv[1], origbotname), NULL);
412 return TCL_OK;
413 }
414
415 static int tcl_unames STDVAR
416 {
417 char *unix_n, *vers_n;
418 #ifdef HAVE_UNAME
419 struct utsname un;
420
421 if (uname(&un) < 0) {
422 #endif
423 unix_n = "*unkown*";
424 vers_n = "";
425 #ifdef HAVE_UNAME
426 } else {
427 unix_n = un.sysname;
428 vers_n = un.release;
429 }
430 #endif
431 Tcl_AppendResult(irp, unix_n, " ", vers_n, NULL);
432 return TCL_OK;
433 }
434
435 static int tcl_modules STDVAR
436 {
437 module_entry *current;
438 dependancy *dep;
439 char *list[100], *list2[2], *p;
440 char s[24], s2[24];
441 int i;
442
443 BADARGS(1, 1, "");
444 for (current = module_list; current; current = current->next) {
445 list[0] = current->name;
446 egg_snprintf(s, sizeof s, "%d.%d", current->major, current->minor);
447 list[1] = s;
448 i = 2;
449 for (dep = dependancy_list; dep && (i < 100); dep = dep->next) {
450 if (dep->needing == current) {
451 list2[0] = dep->needed->name;
452 egg_snprintf(s2, sizeof s2, "%d.%d", dep->major, dep->minor);
453 list2[1] = s2;
454 list[i] = Tcl_Merge(2, list2);
455 i++;
456 }
457 }
458 p = Tcl_Merge(i, list);
459 Tcl_AppendElement(irp, p);
460 Tcl_Free((char *) p);
461 while (i > 2) {
462 i--;
463 Tcl_Free((char *) list[i]);
464 }
465 }
466 return TCL_OK;
467 }
468
469 static int tcl_loadhelp STDVAR
470 {
471 BADARGS(2, 2, " helpfile-name");
472 add_help_reference(argv[1]);
473 return TCL_OK;
474 }
475
476 static int tcl_unloadhelp STDVAR
477 {
478 BADARGS(2, 2, " helpfile-name");
479 rem_help_reference(argv[1]);
480 return TCL_OK;
481 }
482
483 static int tcl_reloadhelp STDVAR
484 {
485 BADARGS(1, 1, "");
486 reload_help_data();
487 return TCL_OK;
488 }
489
490 static int tcl_md5 STDVAR
491 {
492 MD5_CTX md5context;
493 char digest_string[33]; /* 32 for digest in hex + null */
494 unsigned char digest[16];
495 int i;
496
497 BADARGS(2, 2, " string");
498 MD5Init(&md5context);
499 MD5Update(&md5context, (unsigned char *)argv[1], strlen(argv[1]));
500 MD5Final(digest, &md5context);
501 for(i=0; i<16; i++)
502 sprintf(digest_string + (i*2), "%.2x", digest[i]);
503 Tcl_AppendResult(irp, digest_string, NULL);
504 return TCL_OK;
505 }
506
507 tcl_cmds tclmisc_cmds[] =
508 {
509 {"putlog", tcl_putlog},
510 {"putcmdlog", tcl_putcmdlog},
511 {"putxferlog", tcl_putxferlog},
512 {"putloglev", tcl_putloglev},
513 {"timer", tcl_timer},
514 {"utimer", tcl_utimer},
515 {"killtimer", tcl_killtimer},
516 {"killutimer", tcl_killutimer},
517 {"timers", tcl_timers},
518 {"utimers", tcl_utimers},
519 {"unixtime", tcl_unixtime},
520 {"strftime", tcl_strftime},
521 {"ctime", tcl_ctime},
522 {"myip", tcl_myip},
523 {"rand", tcl_rand},
524 {"sendnote", tcl_sendnote},
525 {"dumpfile", tcl_dumpfile},
526 {"dccdumpfile", tcl_dccdumpfile},
527 {"backup", tcl_backup},
528 {"exit", tcl_die},
529 {"die", tcl_die},
530 {"unames", tcl_unames},
531 {"unloadmodule", tcl_unloadmodule},
532 {"loadmodule", tcl_loadmodule},
533 {"checkmodule", tcl_loadmodule},
534 {"modules", tcl_modules},
535 {"loadhelp", tcl_loadhelp},
536 {"unloadhelp", tcl_unloadhelp},
537 {"reloadhelp", tcl_reloadhelp},
538 {"duration", tcl_duration},
539 {"md5", tcl_md5},
540 {"binds", tcl_binds},
541 {NULL, NULL}
542 };

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23