/[cvs]/eggdrop1.8/src/modules.c
ViewVC logotype

Contents of /eggdrop1.8/src/modules.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)
Mon Jul 26 21:11:06 2010 UTC (8 years, 10 months ago) by simple
Branch: eggheads
CVS Tags: v1
Changes since 1.1: +0 -0 lines
File MIME type: text/x-chdr
Imported Eggdrop 1.6.20

1 /*
2 * modules.c -- handles:
3 * support for modules in eggdrop
4 *
5 * by Darrin Smith (beldin@light.iinet.net.au)
6 *
7 * $Id: modules.c,v 1.108 2010/07/08 19:23:06 pseudo Exp $
8 */
9 /*
10 * Copyright (C) 1997 Robey Pointer
11 * Copyright (C) 1999 - 2010 Eggheads Development Team
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26 */
27
28 #include <ctype.h>
29 #include "main.h"
30 #include "modules.h"
31 #include "tandem.h"
32 #include "md5/md5.h"
33 #include "users.h"
34
35 #ifndef STATIC
36 # ifdef MOD_USE_SHL
37 # include <dl.h>
38 # endif
39 # ifdef MOD_USE_DYLD
40 # include <mach-o/dyld.h>
41 # define DYLDFLAGS NSLINKMODULE_OPTION_BINDNOW|NSLINKMODULE_OPTION_PRIVATE|NSLINKMODULE_OPTION_RETURN_ON_ERROR
42 # endif
43 # ifdef MOD_USE_RLD
44 # ifdef HAVE_MACH_O_RLD_H
45 # include <mach-o/rld.h>
46 # else
47 # ifdef HAVE_RLD_H
48 # indluce <rld.h>
49 # endif
50 # endif
51 # endif
52 # ifdef MOD_USE_LOADER
53 # include <loader.h>
54 # endif
55
56 # ifdef MOD_USE_DL
57 # ifdef DLOPEN_1
58 char *dlerror();
59 void *dlopen(const char *, int);
60 int dlclose(void *);
61 void *dlsym(void *, char *);
62 # define DLFLAGS 1
63 # else /* DLOPEN_1 */
64 # include <dlfcn.h>
65
66 # ifndef RTLD_GLOBAL
67 # define RTLD_GLOBAL 0
68 # endif
69 # ifndef RTLD_NOW
70 # define RTLD_NOW 1
71 # endif
72 # ifdef RTLD_LAZY
73 # define DLFLAGS RTLD_LAZY|RTLD_GLOBAL
74 # else
75 # define DLFLAGS RTLD_NOW|RTLD_GLOBAL
76 # endif
77 # endif /* DLOPEN_1 */
78 # endif /* MOD_USE_DL */
79 #endif /* !STATIC */
80
81
82
83 extern struct dcc_t *dcc;
84 extern struct userrec *userlist, *lastuser;
85 extern struct chanset_t *chanset;
86
87 extern char tempdir[], botnetnick[], botname[], natip[], hostname[],
88 origbotname[], botuser[], admin[], userfile[], ver[], notify_new[],
89 helpdir[], version[], quit_msg[], log_ts[];
90
91 extern int parties, noshare, dcc_total, egg_numver, userfile_perm, do_restart,
92 ignore_time, must_be_owner, raw_log, max_dcc, make_userfile,
93 default_flags, require_p, share_greet, use_invites, use_exempts,
94 password_timeout, force_expire, protect_readonly, reserved_port_min,
95 reserved_port_max, copy_to_tmp, quiet_reject;
96
97 extern party_t *party;
98 extern time_t now, online_since;
99 extern tand_t *tandbot;
100 extern Tcl_Interp *interp;
101 extern sock_list *socklist;
102
103 int cmd_die();
104 int xtra_kill();
105 int xtra_unpack();
106 static int module_rename(char *name, char *newname);
107
108 #ifndef STATIC
109 char moddir[121] = "modules/";
110 #endif
111
112 #ifdef STATIC
113 struct static_list {
114 struct static_list *next;
115 char *name;
116 char *(*func) ();
117 } *static_modules = NULL;
118
119 void check_static(char *name, char *(*func) ())
120 {
121 struct static_list *p = nmalloc(sizeof(struct static_list));
122
123 p->name = nmalloc(strlen(name) + 1);
124 strcpy(p->name, name);
125 p->func = func;
126 p->next = static_modules;
127 static_modules = p;
128 }
129 #endif /* STATIC */
130
131
132 /* The null functions */
133 void null_func()
134 {
135 }
136
137 char *charp_func()
138 {
139 return NULL;
140 }
141
142 int minus_func()
143 {
144 return -1;
145 }
146
147 int false_func()
148 {
149 return 0;
150 }
151
152
153 /* The REAL hooks. When these are called, a return of 0 indicates unhandled;
154 * 1 indicates handled. */
155 struct hook_entry *hook_list[REAL_HOOKS];
156
157 static void null_share(int idx, char *x)
158 {
159 if ((x[0] == 'u') && (x[1] == 'n')) {
160 putlog(LOG_BOTS, "*", "User file rejected by %s: %s", dcc[idx].nick, x + 3);
161 dcc[idx].status &= ~STAT_OFFERED;
162 if (!(dcc[idx].status & STAT_GETTING)) {
163 dcc[idx].status &= ~STAT_SHARE;
164 }
165 } else if ((x[0] != 'v') && (x[0] != 'e')) {
166 dprintf(idx, "s un Not sharing userfile.\n");
167 }
168 }
169
170 void (*encrypt_pass) (char *, char *) = 0;
171 char *(*encrypt_string) (char *, char *) = 0;
172 char *(*decrypt_string) (char *, char *) = 0;
173 void (*shareout) () = null_func;
174 void (*sharein) (int, char *) = null_share;
175 void (*qserver) (int, char *, int) = (void (*)(int, char *, int)) null_func;
176 void (*add_mode) () = null_func;
177 int (*match_noterej) (struct userrec *, char *) =
178 (int (*)(struct userrec *, char *)) false_func;
179 int (*rfc_casecmp) (const char *, const char *) = _rfc_casecmp;
180 int (*rfc_ncasecmp) (const char *, const char *, int) = _rfc_ncasecmp;
181 int (*rfc_toupper) (int) = _rfc_toupper;
182 int (*rfc_tolower) (int) = _rfc_tolower;
183 void (*dns_hostbyip) (IP) = block_dns_hostbyip;
184 void (*dns_ipbyhost) (char *) = block_dns_ipbyhost;
185
186 module_entry *module_list;
187 dependancy *dependancy_list = NULL;
188
189 /* The horrible global lookup table for functions
190 * BUT it makes the whole thing *much* more portable than letting each
191 * OS screw up the symbols their own special way :/
192 */
193 Function global_table[] = {
194 /* 0 - 3 */
195 (Function) mod_malloc,
196 (Function) mod_free,
197 #ifdef DEBUG_CONTEXT
198 (Function) eggContext,
199 #else
200 (Function) 0,
201 #endif
202 (Function) module_rename,
203 /* 4 - 7 */
204 (Function) module_register,
205 (Function) module_find,
206 (Function) module_depend,
207 (Function) module_undepend,
208 /* 8 - 11 */
209 (Function) add_bind_table,
210 (Function) del_bind_table,
211 (Function) find_bind_table,
212 (Function) check_tcl_bind,
213 /* 12 - 15 */
214 (Function) add_builtins,
215 (Function) rem_builtins,
216 (Function) add_tcl_commands,
217 (Function) rem_tcl_commands,
218 /* 16 - 19 */
219 (Function) add_tcl_ints,
220 (Function) rem_tcl_ints,
221 (Function) add_tcl_strings,
222 (Function) rem_tcl_strings,
223 /* 20 - 23 */
224 (Function) base64_to_int,
225 (Function) int_to_base64,
226 (Function) int_to_base10,
227 (Function) simple_sprintf,
228 /* 24 - 27 */
229 (Function) botnet_send_zapf,
230 (Function) botnet_send_zapf_broad,
231 (Function) botnet_send_unlinked,
232 (Function) botnet_send_bye,
233 /* 28 - 31 */
234 (Function) botnet_send_chat,
235 (Function) botnet_send_filereject,
236 (Function) botnet_send_filesend,
237 (Function) botnet_send_filereq,
238 /* 32 - 35 */
239 (Function) botnet_send_join_idx,
240 (Function) botnet_send_part_idx,
241 (Function) updatebot,
242 (Function) nextbot,
243 /* 36 - 39 */
244 (Function) zapfbot,
245 (Function) n_free,
246 (Function) u_pass_match,
247 (Function) _user_malloc,
248 /* 40 - 43 */
249 (Function) get_user,
250 (Function) set_user,
251 (Function) add_entry_type,
252 (Function) del_entry_type,
253 /* 44 - 47 */
254 (Function) get_user_flagrec,
255 (Function) set_user_flagrec,
256 (Function) get_user_by_host,
257 (Function) get_user_by_handle,
258 /* 48 - 51 */
259 (Function) find_entry_type,
260 (Function) find_user_entry,
261 (Function) adduser,
262 (Function) deluser,
263 /* 52 - 55 */
264 (Function) addhost_by_handle,
265 (Function) delhost_by_handle,
266 (Function) readuserfile,
267 (Function) write_userfile,
268 /* 56 - 59 */
269 (Function) geticon,
270 (Function) clear_chanlist,
271 (Function) reaffirm_owners,
272 (Function) change_handle,
273 /* 60 - 63 */
274 (Function) write_user,
275 (Function) clear_userlist,
276 (Function) count_users,
277 (Function) sanity_check,
278 /* 64 - 67 */
279 (Function) break_down_flags,
280 (Function) build_flags,
281 (Function) flagrec_eq,
282 (Function) flagrec_ok,
283 /* 68 - 71 */
284 (Function) & shareout,
285 (Function) dprintf,
286 (Function) chatout,
287 (Function) chanout_but,
288 /* 72 - 75 */
289 (Function) check_validity,
290 (Function) egg_list_delete,
291 (Function) egg_list_append,
292 (Function) egg_list_contains,
293 /* 76 - 79 */
294 (Function) answer,
295 (Function) getmyip,
296 (Function) neterror,
297 (Function) tputs,
298 /* 80 - 83 */
299 (Function) new_dcc,
300 (Function) lostdcc,
301 (Function) getsock,
302 (Function) killsock,
303 /* 84 - 87 */
304 (Function) open_listen,
305 (Function) open_telnet_dcc,
306 (Function) _get_data_ptr,
307 (Function) open_telnet,
308 /* 88 - 91 */
309 (Function) check_tcl_event,
310 (Function) egg_memcpy,
311 (Function) my_atoul,
312 (Function) my_strcpy,
313 /* 92 - 95 */
314 (Function) & dcc, /* struct dcc_t * */
315 (Function) & chanset, /* struct chanset_t * */
316 (Function) & userlist, /* struct userrec * */
317 (Function) & lastuser, /* struct userrec * */
318 /* 96 - 99 */
319 (Function) & global_bans, /* struct banrec * */
320 (Function) & global_ign, /* struct igrec * */
321 (Function) & password_timeout, /* int */
322 (Function) & share_greet, /* int */
323 /* 100 - 103 */
324 (Function) & max_dcc, /* int */
325 (Function) & require_p, /* int */
326 (Function) & ignore_time, /* int */
327 (Function) 0, /* was use_console_r <Wcc[02/02/03]> */
328 /* 104 - 107 */
329 (Function) & reserved_port_min,
330 (Function) & reserved_port_max,
331 (Function) & raw_log, /* int */
332 (Function) & noshare, /* int */
333 /* 108 - 111 */
334 (Function) 0, /* gban_total -- UNUSED! (Eule) */
335 (Function) & make_userfile, /* int */
336 (Function) & default_flags, /* int */
337 (Function) & dcc_total, /* int */
338 /* 112 - 115 */
339 (Function) tempdir, /* char * */
340 (Function) natip, /* char * */
341 (Function) hostname, /* char * */
342 (Function) origbotname, /* char * */
343 /* 116 - 119 */
344 (Function) botuser, /* char * */
345 (Function) admin, /* char * */
346 (Function) userfile, /* char * */
347 (Function) ver, /* char * */
348 /* 120 - 123 */
349 (Function) notify_new, /* char * */
350 (Function) helpdir, /* char * */
351 (Function) version, /* char * */
352 (Function) botnetnick, /* char * */
353 /* 124 - 127 */
354 (Function) & DCC_CHAT_PASS, /* struct dcc_table * */
355 (Function) & DCC_BOT, /* struct dcc_table * */
356 (Function) & DCC_LOST, /* struct dcc_table * */
357 (Function) & DCC_CHAT, /* struct dcc_table * */
358 /* 128 - 131 */
359 (Function) & interp, /* Tcl_Interp * */
360 (Function) & now, /* time_t */
361 (Function) findanyidx,
362 (Function) findchan,
363 /* 132 - 135 */
364 (Function) cmd_die,
365 (Function) days,
366 (Function) daysago,
367 (Function) daysdur,
368 /* 136 - 139 */
369 (Function) ismember,
370 (Function) newsplit,
371 (Function) splitnick,
372 (Function) splitc,
373 /* 140 - 143 */
374 (Function) addignore,
375 (Function) match_ignore,
376 (Function) delignore,
377 (Function) fatal,
378 /* 144 - 147 */
379 (Function) xtra_kill,
380 (Function) xtra_unpack,
381 (Function) movefile,
382 (Function) copyfile,
383 /* 148 - 151 */
384 (Function) do_tcl,
385 (Function) readtclprog,
386 (Function) get_language,
387 (Function) def_get,
388 /* 152 - 155 */
389 (Function) makepass,
390 (Function) _wild_match,
391 (Function) maskaddr,
392 (Function) show_motd,
393 /* 156 - 159 */
394 (Function) tellhelp,
395 (Function) showhelp,
396 (Function) add_help_reference,
397 (Function) rem_help_reference,
398 /* 160 - 163 */
399 (Function) touch_laston,
400 (Function) & add_mode, /* Function * */
401 (Function) rmspace,
402 (Function) in_chain,
403 /* 164 - 167 */
404 (Function) add_note,
405 (Function) del_lang_section,
406 (Function) detect_dcc_flood,
407 (Function) flush_lines,
408 /* 168 - 171 */
409 (Function) expected_memory,
410 (Function) tell_mem_status,
411 (Function) & do_restart, /* int */
412 (Function) check_tcl_filt,
413 /* 172 - 175 */
414 (Function) add_hook,
415 (Function) del_hook,
416 (Function) & H_dcc, /* p_tcl_bind_list * */
417 (Function) & H_filt, /* p_tcl_bind_list * */
418 /* 176 - 179 */
419 (Function) & H_chon, /* p_tcl_bind_list * */
420 (Function) & H_chof, /* p_tcl_bind_list * */
421 (Function) & H_load, /* p_tcl_bind_list * */
422 (Function) & H_unld, /* p_tcl_bind_list * */
423 /* 180 - 183 */
424 (Function) & H_chat, /* p_tcl_bind_list * */
425 (Function) & H_act, /* p_tcl_bind_list * */
426 (Function) & H_bcst, /* p_tcl_bind_list * */
427 (Function) & H_bot, /* p_tcl_bind_list * */
428 /* 184 - 187 */
429 (Function) & H_link, /* p_tcl_bind_list * */
430 (Function) & H_disc, /* p_tcl_bind_list * */
431 (Function) & H_away, /* p_tcl_bind_list * */
432 (Function) & H_nkch, /* p_tcl_bind_list * */
433 /* 188 - 191 */
434 (Function) & USERENTRY_BOTADDR, /* struct user_entry_type * */
435 (Function) & USERENTRY_BOTFL, /* struct user_entry_type * */
436 (Function) & USERENTRY_HOSTS, /* struct user_entry_type * */
437 (Function) & USERENTRY_PASS, /* struct user_entry_type * */
438 /* 192 - 195 */
439 (Function) & USERENTRY_XTRA, /* struct user_entry_type * */
440 (Function) user_del_chan,
441 (Function) & USERENTRY_INFO, /* struct user_entry_type * */
442 (Function) & USERENTRY_COMMENT, /* struct user_entry_type * */
443 /* 196 - 199 */
444 (Function) & USERENTRY_LASTON, /* struct user_entry_type * */
445 (Function) putlog,
446 (Function) botnet_send_chan,
447 (Function) list_type_kill,
448 /* 200 - 203 */
449 (Function) logmodes,
450 (Function) masktype,
451 (Function) stripmodes,
452 (Function) stripmasktype,
453 /* 204 - 207 */
454 (Function) sub_lang,
455 (Function) & online_since, /* time_t * */
456 (Function) cmd_loadlanguage,
457 (Function) check_dcc_attrs,
458 /* 208 - 211 */
459 (Function) check_dcc_chanattrs,
460 (Function) add_tcl_coups,
461 (Function) rem_tcl_coups,
462 (Function) botname,
463 /* 212 - 215 */
464 (Function) 0, /* remove_gunk() -- UNUSED! (drummer) */
465 (Function) check_tcl_chjn,
466 (Function) sanitycheck_dcc,
467 (Function) isowner,
468 /* 216 - 219 */
469 (Function) 0, /* min_dcc_port -- UNUSED! (guppy) */
470 (Function) 0, /* max_dcc_port -- UNUSED! (guppy) */
471 (Function) & rfc_casecmp, /* Function * */
472 (Function) & rfc_ncasecmp, /* Function * */
473 /* 220 - 223 */
474 (Function) & global_exempts, /* struct exemptrec * */
475 (Function) & global_invites, /* struct inviterec * */
476 (Function) 0, /* ginvite_total -- UNUSED! (Eule) */
477 (Function) 0, /* gexempt_total -- UNUSED! (Eule) */
478 /* 224 - 227 */
479 (Function) & H_event, /* p_tcl_bind_list * */
480 (Function) & use_exempts, /* int */
481 (Function) & use_invites, /* int */
482 (Function) & force_expire, /* int */
483 /* 228 - 231 */
484 (Function) add_lang_section,
485 (Function) _user_realloc,
486 (Function) mod_realloc,
487 (Function) xtra_set,
488 /* 232 - 235 */
489 #ifdef DEBUG_CONTEXT
490 (Function) eggContextNote,
491 #else
492 (Function) 0,
493 #endif
494 #ifdef DEBUG_ASSERT
495 (Function) eggAssert,
496 #else
497 (Function) 0,
498 #endif
499 (Function) allocsock,
500 (Function) call_hostbyip,
501 /* 236 - 239 */
502 (Function) call_ipbyhost,
503 (Function) iptostr,
504 (Function) & DCC_DNSWAIT, /* struct dcc_table * */
505 (Function) hostsanitycheck_dcc,
506 /* 240 - 243 */
507 (Function) dcc_dnsipbyhost,
508 (Function) dcc_dnshostbyip,
509 (Function) changeover_dcc,
510 (Function) make_rand_str,
511 /* 244 - 247 */
512 (Function) & protect_readonly, /* int */
513 (Function) findchan_by_dname,
514 (Function) removedcc,
515 (Function) & userfile_perm, /* int */
516 /* 248 - 251 */
517 (Function) sock_has_data,
518 (Function) bots_in_subtree,
519 (Function) users_in_subtree,
520 (Function) egg_inet_aton,
521 /* 252 - 255 */
522 (Function) egg_snprintf,
523 (Function) egg_vsnprintf,
524 (Function) egg_memset,
525 (Function) egg_strcasecmp,
526 /* 256 - 259 */
527 (Function) egg_strncasecmp,
528 (Function) is_file,
529 (Function) & must_be_owner, /* int */
530 (Function) & tandbot, /* tand_t * */
531 /* 260 - 263 */
532 (Function) & party, /* party_t * */
533 (Function) open_address_listen,
534 (Function) str_escape,
535 (Function) strchr_unescape,
536 /* 264 - 267 */
537 (Function) str_unescape,
538 (Function) egg_strcatn,
539 (Function) clear_chanlist_member,
540 (Function) fixfrom,
541 /* 268 - 271 */
542 (Function) & socklist, /* sock_list * */
543 (Function) sockoptions,
544 (Function) flush_inbuf,
545 (Function) kill_bot,
546 /* 272 - 275 */
547 (Function) quit_msg, /* char * */
548 (Function) module_load,
549 (Function) module_unload,
550 (Function) & parties, /* int */
551 /* 276 - 279 */
552 (Function) tell_bottree,
553 (Function) MD5_Init,
554 (Function) MD5_Update,
555 (Function) MD5_Final,
556 /* 280 - 283 */
557 (Function) _wild_match_per,
558 (Function) killtransfer,
559 (Function) write_ignores,
560 (Function) & copy_to_tmp, /* int */
561 /* 284 - 287 */
562 (Function) & quiet_reject, /* int */
563 (Function) file_readable,
564 (Function) 0, /* IPv6 leftovers: 286 */
565 (Function) 0, /* IPv6 leftovers: 287 */
566 /* 288 - 291 */
567 (Function) 0, /* IPv6 leftovers: 288 */
568 (Function) strip_mirc_codes,
569 (Function) check_ansi,
570 (Function) oatoi,
571 /* 292 - 295 */
572 (Function) str_isdigit,
573 (Function) remove_crlf,
574 (Function) addr_match,
575 (Function) mask_match,
576 /* 296 - 299 */
577 (Function) check_conflags,
578 (Function) increase_socks_max,
579 (Function) log_ts
580 };
581
582 void init_modules(void)
583 {
584 int i;
585
586 module_list = nmalloc(sizeof(module_entry));
587 module_list->name = nmalloc(8);
588 strcpy(module_list->name, "eggdrop");
589 module_list->major = (egg_numver) / 10000;
590 module_list->minor = (egg_numver / 100) % 100;
591 #ifndef STATIC
592 module_list->hand = NULL;
593 #endif
594 module_list->next = NULL;
595 module_list->funcs = NULL;
596 for (i = 0; i < REAL_HOOKS; i++)
597 hook_list[i] = NULL;
598 }
599
600 int expmem_modules(int y)
601 {
602 int c = 0, i;
603 module_entry *p;
604 dependancy *d;
605 struct hook_entry *q;
606 Function *f;
607 #ifdef STATIC
608 struct static_list *s;
609
610 for (s = static_modules; s; s = s->next)
611 c += sizeof(struct static_list) + strlen(s->name) + 1;
612 #endif
613
614 for (i = 0; i < REAL_HOOKS; i++)
615 for (q = hook_list[i]; q; q = q->next)
616 c += sizeof(struct hook_entry);
617
618 for (d = dependancy_list; d; d = d->next)
619 c += sizeof(dependancy);
620
621 for (p = module_list; p; p = p->next) {
622 c += sizeof(module_entry);
623 c += strlen(p->name) + 1;
624 f = p->funcs;
625 if (f && f[MODCALL_EXPMEM] && !y)
626 c += (int) (f[MODCALL_EXPMEM] ());
627 }
628 return c;
629 }
630
631 int module_register(char *name, Function *funcs, int major, int minor)
632 {
633 module_entry *p;
634
635 for (p = module_list; p && p->name; p = p->next) {
636 if (!egg_strcasecmp(name, p->name)) {
637 p->major = major;
638 p->minor = minor;
639 p->funcs = funcs;
640 return 1;
641 }
642 }
643
644 return 0;
645 }
646
647 const char *module_load(char *name)
648 {
649 module_entry *p;
650 char *e;
651 Function f;
652 #ifdef STATIC
653 struct static_list *sl;
654 #endif
655
656 #ifndef STATIC
657 char workbuf[1024];
658 # ifdef MOD_USE_SHL
659 shl_t hand;
660 # endif
661 # ifdef MOD_USE_DYLD
662 NSObjectFileImage file;
663 NSObjectFileImageReturnCode ret;
664 NSModule hand;
665 NSSymbol sym;
666 # endif
667 # ifdef MOD_USE_RLD
668 long ret;
669 # endif
670 # ifdef MOD_USE_LOADER
671 ldr_module_t hand;
672 # endif
673 # ifdef MOD_USE_DL
674 void *hand;
675 # endif
676 #endif /* !STATIC */
677
678 if (module_find(name, 0, 0) != NULL)
679 return MOD_ALREADYLOAD;
680
681 #ifndef STATIC
682 if (moddir[0] != '/') {
683 if (getcwd(workbuf, 1024) == NULL)
684 return MOD_BADCWD;
685 sprintf(&(workbuf[strlen(workbuf)]), "/%s%s." EGG_MOD_EXT, moddir, name);
686 } else {
687 sprintf(workbuf, "%s%s." EGG_MOD_EXT, moddir, name);
688 }
689
690 # ifdef MOD_USE_SHL
691 hand = shl_load(workbuf, BIND_IMMEDIATE, 0L);
692 if (!hand)
693 return "Can't load module.";
694 sprintf(workbuf, "%s_start", name);
695 if (shl_findsym(&hand, workbuf, (short) TYPE_PROCEDURE, (void *) &f))
696 f = NULL;
697 if (f == NULL) {
698 /* Some OS's require a _ to be prepended to the symbol name (Darwin, etc). */
699 sprintf(workbuf, "_%s_start", name);
700 if (shl_findsym(&hand, workbuf, (short) TYPE_PROCEDURE, (void *) &f))
701 f = NULL;
702 }
703 if (f == NULL) {
704 shl_unload(hand);
705 return MOD_NOSTARTDEF;
706 }
707 # endif /* MOD_USE_SHL */
708
709 # ifdef MOD_USE_DYLD
710 ret = NSCreateObjectFileImageFromFile(workbuf, &file);
711 if (ret != NSObjectFileImageSuccess)
712 return "Can't load module.";
713 hand = NSLinkModule(file, workbuf, DYLDFLAGS);
714 sprintf(workbuf, "_%s_start", name);
715 sym = NSLookupSymbolInModule(hand, workbuf);
716 if (sym)
717 f = (Function) NSAddressOfSymbol(sym);
718 else
719 f = NULL;
720 if (f == NULL) {
721 NSUnLinkModule(hand, NSUNLINKMODULE_OPTION_NONE);
722 return MOD_NOSTARTDEF;
723 }
724 # endif /* MOD_USE_DYLD */
725
726 # ifdef MOD_USE_RLD
727 ret = rld_load(NULL, (struct mach_header **) 0, workbuf, (const char *) 0);
728 if (!ret)
729 return "Can't load module.";
730 sprintf(workbuf, "_%s_start", name);
731 ret = rld_lookup(NULL, workbuf, &f)
732 if (!ret || f == NULL)
733 return MOD_NOSTARTDEF;
734 /* There isn't a reliable way to unload at this point... just keep it loaded. */
735 # endif /* MOD_USE_DYLD */
736
737 # ifdef MOD_USE_LOADER
738 hand = load(workbuf, LDR_NOFLAGS);
739 if (hand == LDR_NULL_MODULE)
740 return "Can't load module.";
741 sprintf(workbuf, "%s_start", name);
742 f = (Function) ldr_lookup_package(hand, workbuf);
743 if (f == NULL) {
744 sprintf(workbuf, "_%s_start", name);
745 f = (Function) ldr_lookup_package(hand, workbuf);
746 }
747 if (f == NULL) {
748 unload(hand);
749 return MOD_NOSTARTDEF;
750 }
751 # endif /* MOD_USE_LOADER */
752
753 # ifdef MOD_USE_DL
754 hand = dlopen(workbuf, DLFLAGS);
755 if (!hand)
756 return dlerror();
757 sprintf(workbuf, "%s_start", name);
758 f = (Function) dlsym(hand, workbuf);
759 if (f == NULL) {
760 sprintf(workbuf, "_%s_start", name);
761 f = (Function) dlsym(hand, workbuf);
762 }
763 if (f == NULL) {
764 dlclose(hand);
765 return MOD_NOSTARTDEF;
766 }
767 # endif /* MOD_USE_DL */
768 #endif /* !STATIC */
769
770 #ifdef STATIC
771 for (sl = static_modules; sl && egg_strcasecmp(sl->name, name); sl = sl->next);
772 if (!sl)
773 return "Unknown module.";
774 f = (Function) sl->func;
775 #endif /* STATIC */
776
777 p = nmalloc(sizeof(module_entry));
778 if (p == NULL)
779 return "Malloc error";
780 p->name = nmalloc(strlen(name) + 1);
781 strcpy(p->name, name);
782 p->major = 0;
783 p->minor = 0;
784 #ifndef STATIC
785 p->hand = hand;
786 #endif
787 p->funcs = 0;
788 p->next = module_list;
789 module_list = p;
790 e = (((char *(*)()) f) (global_table));
791 if (e) {
792 module_list = module_list->next;
793 nfree(p->name);
794 nfree(p);
795 return e;
796 }
797 check_tcl_load(name);
798
799 if (exist_lang_section(name))
800 putlog(LOG_MISC, "*", MOD_LOADED_WITH_LANG, name);
801 else
802 putlog(LOG_MISC, "*", MOD_LOADED, name);
803
804 return NULL;
805 }
806
807 char *module_unload(char *name, char *user)
808 {
809 module_entry *p = module_list, *o = NULL;
810 char *e;
811 Function *f;
812
813 while (p) {
814 if ((p->name != NULL) && !strcmp(name, p->name)) {
815 dependancy *d;
816
817 for (d = dependancy_list; d; d = d->next)
818 if (d->needed == p)
819 return MOD_NEEDED;
820
821 f = p->funcs;
822 if (f && !f[MODCALL_CLOSE])
823 return MOD_NOCLOSEDEF;
824 if (f) {
825 check_tcl_unld(name);
826 e = (((char *(*)()) f[MODCALL_CLOSE]) (user));
827 if (e != NULL)
828 return e;
829 #ifndef STATIC
830 # ifdef MOD_USE_SHL
831 shl_unload(p->hand);
832 # endif
833 # ifdef MOD_USE_DYLD
834 NSUnLinkModule(p->hand, NSUNLINKMODULE_OPTION_NONE);
835 # endif
836 # ifdef MOD_USE_LOADER
837 unload(p->hand);
838 # endif
839 # ifdef MOD_USE_DL
840 dlclose(p->hand);
841 # endif
842 #endif /* !STATIC */
843 }
844 nfree(p->name);
845 if (o == NULL)
846 module_list = p->next;
847 else
848 o->next = p->next;
849 nfree(p);
850 putlog(LOG_MISC, "*", "%s %s", MOD_UNLOADED, name);
851 return NULL;
852 }
853 o = p;
854 p = p->next;
855 }
856
857 return MOD_NOSUCH;
858 }
859
860 module_entry *module_find(char *name, int major, int minor)
861 {
862 module_entry *p;
863
864 for (p = module_list; p && p->name; p = p->next) {
865 if ((major == p->major || !major) && minor <= p->minor &&
866 !egg_strcasecmp(name, p->name))
867 return p;
868 }
869 return NULL;
870 }
871
872 static int module_rename(char *name, char *newname)
873 {
874 module_entry *p;
875
876 for (p = module_list; p; p = p->next)
877 if (!egg_strcasecmp(newname, p->name))
878 return 0;
879
880 for (p = module_list; p && p->name; p = p->next) {
881 if (!egg_strcasecmp(name, p->name)) {
882 nfree(p->name);
883 p->name = nmalloc(strlen(newname) + 1);
884 strcpy(p->name, newname);
885 return 1;
886 }
887 }
888 return 0;
889 }
890
891 Function *module_depend(char *name1, char *name2, int major, int minor)
892 {
893 module_entry *p = module_find(name2, major, minor);
894 module_entry *o = module_find(name1, 0, 0);
895 dependancy *d;
896
897 if (!p) {
898 if (module_load(name2))
899 return 0;
900 p = module_find(name2, major, minor);
901 }
902 if (!p || !o)
903 return 0;
904 d = nmalloc(sizeof(dependancy));
905
906 d->needed = p;
907 d->needing = o;
908 d->next = dependancy_list;
909 d->major = major;
910 d->minor = minor;
911 dependancy_list = d;
912 return p->funcs ? p->funcs : (Function *) 1;
913 }
914
915 int module_undepend(char *name1)
916 {
917 int ok = 0;
918 module_entry *p = module_find(name1, 0, 0);
919 dependancy *d = dependancy_list, *o = NULL;
920
921 if (p == NULL)
922 return 0;
923 while (d != NULL) {
924 if (d->needing == p) {
925 if (o == NULL) {
926 dependancy_list = d->next;
927 } else {
928 o->next = d->next;
929 }
930 nfree(d);
931 if (o == NULL)
932 d = dependancy_list;
933 else
934 d = o->next;
935 ok++;
936 } else {
937 o = d;
938 d = d->next;
939 }
940 }
941 return ok;
942 }
943
944 void *mod_malloc(int size, const char *modname, const char *filename, int line)
945 {
946 #ifdef DEBUG_MEM
947 char x[100], *p;
948
949 p = strrchr(filename, '/');
950 egg_snprintf(x, sizeof x, "%s:%s", modname, p ? p + 1 : filename);
951 x[19] = 0;
952 return n_malloc(size, x, line);
953 #else
954 return nmalloc(size);
955 #endif
956 }
957
958 void *mod_realloc(void *ptr, int size, const char *modname,
959 const char *filename, int line)
960 {
961 #ifdef DEBUG_MEM
962 char x[100], *p;
963
964 p = strrchr(filename, '/');
965 egg_snprintf(x, sizeof x, "%s:%s", modname, p ? p + 1 : filename);
966 x[19] = 0;
967 return n_realloc(ptr, size, x, line);
968 #else
969 return nrealloc(ptr, size);
970 #endif
971 }
972
973 void mod_free(void *ptr, const char *modname, const char *filename, int line)
974 {
975 char x[100], *p;
976
977 p = strrchr(filename, '/');
978 egg_snprintf(x, sizeof x, "%s:%s", modname, p ? p + 1 : filename);
979 x[19] = 0;
980 n_free(ptr, x, line);
981 }
982
983 /* Hooks, various tables of functions to call on certain events
984 */
985 void add_hook(int hook_num, Function func)
986 {
987 if (hook_num < REAL_HOOKS) {
988 struct hook_entry *p;
989
990 for (p = hook_list[hook_num]; p; p = p->next)
991 if (p->func == func)
992 return; /* Don't add it if it's already there */
993 p = nmalloc(sizeof(struct hook_entry));
994
995 p->next = hook_list[hook_num];
996 hook_list[hook_num] = p;
997 p->func = func;
998 } else
999 switch (hook_num) {
1000 case HOOK_ENCRYPT_PASS:
1001 encrypt_pass = (void (*)(char *, char *)) func;
1002 break;
1003 case HOOK_ENCRYPT_STRING:
1004 encrypt_string = (char *(*)(char *, char *)) func;
1005 break;
1006 case HOOK_DECRYPT_STRING:
1007 decrypt_string = (char *(*)(char *, char *)) func;
1008 break;
1009 case HOOK_SHAREOUT:
1010 shareout = (void (*)()) func;
1011 break;
1012 case HOOK_SHAREIN:
1013 sharein = (void (*)(int, char *)) func;
1014 break;
1015 case HOOK_QSERV:
1016 if (qserver == (void (*)(int, char *, int)) null_func)
1017 qserver = (void (*)(int, char *, int)) func;
1018 break;
1019 case HOOK_ADD_MODE:
1020 if (add_mode == (void (*)()) null_func)
1021 add_mode = (void (*)()) func;
1022 break;
1023 /* special hook <drummer> */
1024 case HOOK_RFC_CASECMP:
1025 if (func == NULL) {
1026 rfc_casecmp = egg_strcasecmp;
1027 rfc_ncasecmp =
1028 (int (*)(const char *, const char *, int)) egg_strncasecmp;
1029 rfc_tolower = tolower;
1030 rfc_toupper = toupper;
1031 } else {
1032 rfc_casecmp = _rfc_casecmp;
1033 rfc_ncasecmp = _rfc_ncasecmp;
1034 rfc_tolower = _rfc_tolower;
1035 rfc_toupper = _rfc_toupper;
1036 }
1037 break;
1038 case HOOK_MATCH_NOTEREJ:
1039 if (match_noterej == false_func)
1040 match_noterej = (int (*)(struct userrec *, char *)) func;
1041 break;
1042 case HOOK_DNS_HOSTBYIP:
1043 if (dns_hostbyip == block_dns_hostbyip)
1044 dns_hostbyip = (void (*)(IP)) func;
1045 break;
1046 case HOOK_DNS_IPBYHOST:
1047 if (dns_ipbyhost == block_dns_ipbyhost)
1048 dns_ipbyhost = (void (*)(char *)) func;
1049 break;
1050 }
1051 }
1052
1053 void del_hook(int hook_num, Function func)
1054 {
1055 if (hook_num < REAL_HOOKS) {
1056 struct hook_entry *p = hook_list[hook_num], *o = NULL;
1057
1058 while (p) {
1059 if (p->func == func) {
1060 if (o == NULL)
1061 hook_list[hook_num] = p->next;
1062 else
1063 o->next = p->next;
1064 nfree(p);
1065 break;
1066 }
1067 o = p;
1068 p = p->next;
1069 }
1070 } else
1071 switch (hook_num) {
1072 case HOOK_ENCRYPT_PASS:
1073 if (encrypt_pass == (void (*)(char *, char *)) func)
1074 encrypt_pass = (void (*)(char *, char *)) null_func;
1075 break;
1076 case HOOK_ENCRYPT_STRING:
1077 if (encrypt_string == (char *(*)(char *, char *)) func)
1078 encrypt_string = (char *(*)(char *, char *)) null_func;
1079 break;
1080 case HOOK_DECRYPT_STRING:
1081 if (decrypt_string == (char *(*)(char *, char *)) func)
1082 decrypt_string = (char *(*)(char *, char *)) null_func;
1083 break;
1084 case HOOK_SHAREOUT:
1085 if (shareout == (void (*)()) func)
1086 shareout = null_func;
1087 break;
1088 case HOOK_SHAREIN:
1089 if (sharein == (void (*)(int, char *)) func)
1090 sharein = null_share;
1091 break;
1092 case HOOK_QSERV:
1093 if (qserver == (void (*)(int, char *, int)) func)
1094 qserver = null_func;
1095 break;
1096 case HOOK_ADD_MODE:
1097 if (add_mode == (void (*)()) func)
1098 add_mode = null_func;
1099 break;
1100 case HOOK_MATCH_NOTEREJ:
1101 if (match_noterej == (int (*)(struct userrec *, char *)) func)
1102 match_noterej = false_func;
1103 break;
1104 case HOOK_DNS_HOSTBYIP:
1105 if (dns_hostbyip == (void (*)(IP)) func)
1106 dns_hostbyip = block_dns_hostbyip;
1107 break;
1108 case HOOK_DNS_IPBYHOST:
1109 if (dns_ipbyhost == (void (*)(char *)) func)
1110 dns_ipbyhost = block_dns_ipbyhost;
1111 break;
1112 }
1113 }
1114
1115 int call_hook_cccc(int hooknum, char *a, char *b, char *c, char *d)
1116 {
1117 struct hook_entry *p, *pn;
1118 int f = 0;
1119
1120 if (hooknum >= REAL_HOOKS)
1121 return 0;
1122 p = hook_list[hooknum];
1123 for (p = hook_list[hooknum]; p && !f; p = pn) {
1124 pn = p->next;
1125 f = p->func(a, b, c, d);
1126 }
1127 return f;
1128 }
1129
1130 void do_module_report(int idx, int details, char *which)
1131 {
1132 module_entry *p = module_list;
1133
1134 if (p && !which)
1135 dprintf(idx, "Loaded module information:\n");
1136 for (; p; p = p->next) {
1137 if (!which || !egg_strcasecmp(which, p->name)) {
1138 dependancy *d;
1139
1140 if (details)
1141 dprintf(idx, " Module: %s, v %d.%d\n", p->name ? p->name : "CORE",
1142 p->major, p->minor);
1143 if (details > 1) {
1144 for (d = dependancy_list; d; d = d->next)
1145 if (d->needing == p)
1146 dprintf(idx, " requires: %s, v %d.%d\n", d->needed->name,
1147 d->major, d->minor);
1148 }
1149 if (p->funcs) {
1150 Function f = p->funcs[MODCALL_REPORT];
1151
1152 if (f != NULL)
1153 f(idx, details);
1154 }
1155 if (which)
1156 return;
1157 }
1158 }
1159 if (which)
1160 dprintf(idx, "No such module.\n");
1161 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23