/[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.1.1.1 - (show annotations) (download) (as text) (vendor branch)
Wed Jun 23 19:51:32 1999 UTC (22 years, 4 months ago) by segfault
Branch: eggdev
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Eggdrop 1.3.28 CVS Code

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23