/[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.8 - (show annotations) (download) (as text)
Mon Jan 17 21:59:11 2000 UTC (19 years, 10 months ago) by fabian
Branch: MAIN
Changes since 1.7: +2 -3 lines
File MIME type: text/x-chdr
resync with 1.4, 17Jan2000

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23