/[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.35 - (show annotations) (download) (as text)
Fri Oct 19 01:55:05 2001 UTC (18 years, 1 month ago) by tothwolf
Branch: MAIN
Changes since 1.34: +10 -10 lines
File MIME type: text/x-chdr
* removed obsolete EGG_CHECK_FUNC_VSPRINTF autoconf macro
* removed obsolete EGG_HEADER_STDC autoconf macro
* added EGG_C_LONG_LONG autoconf macro
* added EGG_FUNC_C99_VSNPRINTF autoconf macro
* added EGG_REPLACE_SNPRINTF autoconf macro
* added EGG_TYPE_32BIT autoconf macro

* removed unused dlfcn.h header check from configure.ac
* removed AC_CHECK_SIZEOF long and int from configure.ac
* added many required autoconf macros to configure.ac
* added AC_REPLACE_FUNCS to configure.ac for compat functions
* many configure.ac cleanups

* updated compat/snprintf.c with latest version from Samba
* added code to link libm with compat library for snprintf.c
* added support for LIBOBJS to compat/Makefile.am
* added memset back to compat
* added memcpy back to compat
* many compat cleanups

* renamed egg_strcasecmp() to strcasecmp()
* renamed egg_strncasecmp() to strncasecmp()
* renamed egg_snprintf() to snprintf()
* renamed egg_vsnprintf() to vsnprintf()
* renamed egg_strftime() to strftime()
* renamed egg_inet_aton() to inet_aton()
* renamed egg_inet_ntop() to inet_ntop()
* renamed egg_inet_pton() to inet_pton()

* exported strftime() for modules
* exported inet_ntop() for modules
* exported inet_pton() for modules
* exported vasprintf() for modules
* exported asprintf() for modules

* renamed u_32bit_t typedef to u_32int_t
* extended stat.h to support standard bits and checks

* major function changes:
  + egg_strcasecmp -> strcasecmp
  + egg_strncasecmp -> strncasecmp
  + egg_snprintf -> snprintf
  + egg_vsnprintf -> vsnprintf
  + egg_strftime -> strftime
  + egg_inet_aton -> inet_aton
  + egg_inet_ntop -> inet_ntop
  + egg_inet_pton -> inet_pton

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.34 2001/10/18 09:06:43 stdarg 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 struct dcc_t *dcc;
38 extern char origbotname[], botnetnick[], quit_msg[];
39 extern struct userrec *userlist;
40 extern time_t now;
41 extern module_entry *module_list;
42
43 static int tcl_binds STDVAR
44 {
45 tcl_bind_list_t *tl, *tl_kind;
46 tcl_bind_mask_t *tm;
47 tcl_cmd_t *tc;
48 char *list[5], *g, flg[100], hits[11];
49 int matching = 0;
50
51 BADARGS(1, 2, " ?type/mask?");
52 if (argv[1])
53 tl_kind = find_bind_table(argv[1]);
54 else
55 tl_kind = NULL;
56 if (!tl_kind && argv[1])
57 matching = 1;
58 for (tl = tl_kind ? tl_kind : bind_table_list; tl;
59 tl = tl_kind ? 0 : tl->next) {
60 if (tl->flags & HT_DELETED)
61 continue;
62 for (tm = tl->first; tm; tm = tm->next) {
63 if (tm->flags & TBM_DELETED)
64 continue;
65 for (tc = tm->first; tc; tc = tc->next) {
66 if (tc->attributes & TC_DELETED)
67 continue;
68 if (matching &&
69 !wild_match(argv[1], tl->name) &&
70 !wild_match(argv[1], tm->mask) &&
71 !wild_match(argv[1], tc->func_name))
72 continue;
73 build_flags(flg, &(tc->flags), NULL);
74 snprintf(hits, sizeof hits, "%i", (int) tc->hits);
75 list[0] = tl->name;
76 list[1] = flg;
77 list[2] = tm->mask;
78 list[3] = hits;
79 list[4] = tc->func_name;
80 g = Tcl_Merge(5, list);
81 Tcl_AppendElement(irp, g);
82 Tcl_Free((char *) g);
83 }
84 }
85 }
86 return TCL_OK;
87 }
88
89 static int tcl_duration STDVAR
90 {
91 char s[70];
92 unsigned long sec, tmp;
93
94 BADARGS(2, 2, " seconds");
95 if (atol(argv[1]) <= 0) {
96 Tcl_AppendResult(irp, "0 seconds", NULL);
97 return TCL_OK;
98 }
99 sec = atol(argv[1]);
100 s[0] = 0;
101 if (sec >= 31536000) {
102 tmp = (sec / 31536000);
103 sprintf(s, "%lu year%s ", tmp, (tmp == 1) ? "" : "s");
104 sec -= (tmp * 31536000);
105 }
106 if (sec >= 604800) {
107 tmp = (sec / 604800);
108 sprintf(&s[strlen(s)], "%lu week%s ", tmp, (tmp == 1) ? "" : "s");
109 sec -= (tmp * 604800);
110 }
111 if (sec >= 86400) {
112 tmp = (sec / 86400);
113 sprintf(&s[strlen(s)], "%lu day%s ", tmp, (tmp == 1) ? "" : "s");
114 sec -= (tmp * 86400);
115 }
116 if (sec >= 3600) {
117 tmp = (sec / 3600);
118 sprintf(&s[strlen(s)], "%lu hour%s ", tmp, (tmp == 1) ? "" : "s");
119 sec -= (tmp * 3600);
120 }
121 if (sec >= 60) {
122 tmp = (sec / 60);
123 sprintf(&s[strlen(s)], "%lu minute%s ", tmp, (tmp == 1) ? "" : "s");
124 sec -= (tmp * 60);
125 }
126 if (sec > 0) {
127 tmp = (sec);
128 sprintf(&s[strlen(s)], "%lu second%s", tmp, (tmp == 1) ? "" : "s");
129 }
130 Tcl_AppendResult(irp, s, NULL);
131 return TCL_OK;
132 }
133
134 static int tcl_unixtime STDVAR
135 {
136 char s[11];
137
138 BADARGS(1, 1, "");
139 snprintf(s, sizeof s, "%lu", (unsigned long) now);
140 Tcl_AppendResult(irp, s, NULL);
141 return TCL_OK;
142 }
143
144 static int tcl_ctime STDVAR
145 {
146 time_t tt;
147 char s[25];
148
149 BADARGS(2, 2, " unixtime");
150 tt = (time_t) atol(argv[1]);
151 strncpyz(s, ctime(&tt), sizeof s);
152 Tcl_AppendResult(irp, s, NULL);
153 return TCL_OK;
154 }
155
156 static int tcl_strftime STDVAR
157 {
158 char buf[512];
159 struct tm *tm1;
160 time_t t;
161
162 BADARGS(2, 3, " format ?time?");
163 if (argc == 3)
164 t = atol(argv[2]);
165 else
166 t = now;
167 tm1 = localtime(&t);
168 if (strftime(buf, sizeof(buf) - 1, argv[1], tm1)) {
169 Tcl_AppendResult(irp, buf, NULL);
170 return TCL_OK;
171 }
172 Tcl_AppendResult(irp, " error with strftime", NULL);
173 return TCL_ERROR;
174 }
175
176 static int tcl_myip STDVAR
177 {
178 char s[16];
179
180 BADARGS(1, 1, "");
181 snprintf(s, sizeof s, "%lu", iptolong(getmyip()));
182 Tcl_AppendResult(irp, s, NULL);
183 return TCL_OK;
184 }
185
186 static int tcl_myip6 STDVAR
187 {
188 BADARGS(1, 1, "");
189 #ifdef IPV6
190 Tcl_AppendResult(irp, "", NULL); /* FIXME!! */
191 #else
192 Tcl_AppendResult(irp, "", NULL);
193 #endif
194 return TCL_OK;
195 }
196
197 static int tcl_rand STDVAR
198 {
199 unsigned long x;
200 char s[11];
201
202 BADARGS(2, 2, " limit");
203 if (atol(argv[1]) <= 0) {
204 Tcl_AppendResult(irp, "random limit must be greater than zero", NULL);
205 return TCL_ERROR;
206 }
207 x = random() % (atol(argv[1]));
208 snprintf(s, sizeof s, "%lu", x);
209 Tcl_AppendResult(irp, s, NULL);
210 return TCL_OK;
211 }
212
213 static int tcl_sendnote STDVAR
214 {
215 char s[5], from[NOTENAMELEN + 1], to[NOTENAMELEN + 1], msg[451];
216
217 BADARGS(4, 4, " from to message");
218 strncpyz(from, argv[1], sizeof from);
219 strncpyz(to, argv[2], sizeof to);
220 strncpyz(msg, argv[3], sizeof msg);
221 snprintf(s, sizeof s, "%d", add_note(to, from, msg, -1, 0));
222 Tcl_AppendResult(irp, s, NULL);
223 return TCL_OK;
224 }
225
226 static int tcl_dumpfile STDVAR
227 {
228 char nick[NICKLEN];
229 struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
230
231 BADARGS(3, 3, " nickname filename");
232 strncpyz(nick, argv[1], sizeof nick);
233 get_user_flagrec(get_user_by_nick(nick), &fr, NULL);
234 showhelp(argv[1], argv[2], &fr, HELP_TEXT);
235 return TCL_OK;
236 }
237
238 static int tcl_dccdumpfile STDVAR
239 {
240 int idx, i;
241 struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
242
243 BADARGS(3, 3, " idx filename");
244 i = atoi(argv[1]);
245 idx = findidx(i);
246 if (idx < 0) {
247 Tcl_AppendResult(irp, "illegal idx", NULL);
248 return TCL_ERROR;
249 }
250 get_user_flagrec(get_user_by_handle(userlist, dcc[idx].nick), &fr, NULL);
251 tellhelp(idx, argv[2], &fr, HELP_TEXT);
252 return TCL_OK;
253 }
254
255 static int tcl_backup STDVAR
256 {
257 BADARGS(1, 1, "");
258 call_hook(HOOK_BACKUP);
259 return TCL_OK;
260 }
261
262 static int tcl_die STDVAR
263 {
264 char s[1024];
265
266 BADARGS(1, 2, " ?reason?");
267 if (argc == 2) {
268 snprintf(s, sizeof s, "BOT SHUTDOWN (%s)", argv[1]);
269 strncpyz(quit_msg, argv[1], 1024);
270 } else {
271 strncpyz(s, "BOT SHUTDOWN (No reason)", sizeof s);
272 quit_msg[0] = 0;
273 }
274 kill_bot(s, quit_msg[0] ? quit_msg : "EXIT");
275 return TCL_OK;
276 }
277
278 static int tcl_loadmodule STDVAR
279 {
280 const char *p;
281
282 BADARGS(2, 2, " module-name");
283 p = module_load(argv[1]);
284 if (p && strcmp(p, _("Already loaded.")))
285 putlog(LOG_MISC, "*", "%s %s: %s", _("Cant load modules"), argv[1], p);
286 Tcl_AppendResult(irp, p, NULL);
287 return TCL_OK;
288 }
289
290 static int tcl_unloadmodule STDVAR
291 {
292 BADARGS(2, 2, " module-name");
293 Tcl_AppendResult(irp, module_unload(argv[1], origbotname), NULL);
294 return TCL_OK;
295 }
296
297 static int tcl_unames STDVAR
298 {
299 char *unix_n, *vers_n;
300 #ifdef HAVE_UNAME
301 struct utsname un;
302
303 if (uname(&un) < 0) {
304 #endif
305 unix_n = "*unkown*";
306 vers_n = "";
307 #ifdef HAVE_UNAME
308 } else {
309 unix_n = un.sysname;
310 vers_n = un.release;
311 }
312 #endif
313 Tcl_AppendResult(irp, unix_n, " ", vers_n, NULL);
314 return TCL_OK;
315 }
316
317 static int tcl_modules STDVAR
318 {
319 module_entry *current;
320 dependancy *dep;
321 char *list[100], *list2[2], *p;
322 char s[24], s2[24];
323 int i;
324
325 BADARGS(1, 1, "");
326 for (current = module_list; current; current = current->next) {
327 list[0] = current->name;
328 snprintf(s, sizeof s, "%d.%d", current->major, current->minor);
329 list[1] = s;
330 i = 2;
331 for (dep = dependancy_list; dep && (i < 100); dep = dep->next) {
332 if (dep->needing == current) {
333 list2[0] = dep->needed->name;
334 snprintf(s2, sizeof s2, "%d.%d", dep->major, dep->minor);
335 list2[1] = s2;
336 list[i] = Tcl_Merge(2, list2);
337 i++;
338 }
339 }
340 p = Tcl_Merge(i, list);
341 Tcl_AppendElement(irp, p);
342 Tcl_Free((char *) p);
343 while (i > 2) {
344 i--;
345 Tcl_Free((char *) list[i]);
346 }
347 }
348 return TCL_OK;
349 }
350
351 static int tcl_loadhelp STDVAR
352 {
353 BADARGS(2, 2, " helpfile-name");
354 add_help_reference(argv[1]);
355 return TCL_OK;
356 }
357
358 static int tcl_unloadhelp STDVAR
359 {
360 BADARGS(2, 2, " helpfile-name");
361 rem_help_reference(argv[1]);
362 return TCL_OK;
363 }
364
365 static int tcl_reloadhelp STDVAR
366 {
367 BADARGS(1, 1, "");
368 reload_help_data();
369 return TCL_OK;
370 }
371
372 static int tcl_callevent STDVAR
373 {
374 BADARGS(2, 2, " event");
375 check_bind_event(argv[1]);
376 return TCL_OK;
377 }
378
379 #if (TCL_MAJOR_VERSION >= 8)
380 static int tcl_md5(cd, irp, objc, objv)
381 ClientData cd;
382 Tcl_Interp *irp;
383 int objc;
384 Tcl_Obj *CONST objv[];
385 {
386 #else
387 static int tcl_md5 STDVAR
388 {
389 #endif
390 MD5_CTX md5context;
391 char digest_string[33], *string;
392 unsigned char digest[16];
393 int i, len;
394
395 #if (TCL_MAJOR_VERSION >= 8)
396 if (objc != 2) {
397 Tcl_WrongNumArgs(irp, 1, objv, "string");
398 return TCL_ERROR;
399 }
400
401 #if (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION < 1)
402 string = Tcl_GetStringFromObj(objv[1], &len);
403 #else
404 string = Tcl_GetByteArrayFromObj(objv[1], &len);
405 #endif
406
407 #else
408 BADARGS(2, 2, " string");
409 string = argv[1];
410 len = strlen(argv[1]);
411 #endif
412
413 MD5_Init(&md5context);
414 MD5_Update(&md5context, (unsigned char *)string, len);
415 MD5_Final(digest, &md5context);
416 for(i=0; i<16; i++)
417 sprintf(digest_string + (i*2), "%.2x", digest[i]);
418 Tcl_AppendResult(irp, digest_string, NULL);
419 return TCL_OK;
420 }
421
422 tcl_cmds tclmisc_objcmds[] =
423 {
424 #if (TCL_MAJOR_VERSION >= 8)
425 {"md5", tcl_md5},
426 #endif
427 {NULL, NULL}
428 };
429
430 tcl_cmds tclmisc_cmds[] =
431 {
432 {"unixtime", tcl_unixtime},
433 {"strftime", tcl_strftime},
434 {"ctime", tcl_ctime},
435 {"myip", tcl_myip},
436 {"rand", tcl_rand},
437 {"sendnote", tcl_sendnote},
438 {"dumpfile", tcl_dumpfile},
439 {"dccdumpfile", tcl_dccdumpfile},
440 {"backup", tcl_backup},
441 {"exit", tcl_die},
442 {"die", tcl_die},
443 {"unames", tcl_unames},
444 {"unloadmodule", tcl_unloadmodule},
445 {"loadmodule", tcl_loadmodule},
446 {"modules", tcl_modules},
447 {"loadhelp", tcl_loadhelp},
448 {"unloadhelp", tcl_unloadhelp},
449 {"reloadhelp", tcl_reloadhelp},
450 {"duration", tcl_duration},
451 #if (TCL_MAJOR_VERSION < 8)
452 {"md5", tcl_md5},
453 #endif
454 {"binds", tcl_binds},
455 {"callevent", tcl_callevent},
456 {"myip6", tcl_myip6},
457 {NULL, NULL}
458 };

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23