modules.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873
  1. /*
  2. * modules.c -- handles:
  3. * support for modules in eggdrop
  4. *
  5. * by Darrin Smith (beldin@light.iinet.net.au)
  6. *
  7. */
  8. #include "common.h"
  9. #include "modules.h"
  10. #include "tclhash.h"
  11. #include "misc_file.h"
  12. #include "rfc1459.h"
  13. #include "net.h"
  14. #include "misc.h"
  15. #include "userent.h"
  16. #include "userrec.h"
  17. #include "cfg.h"
  18. #include "match.h"
  19. #include "auth.h"
  20. #include "main.h"
  21. #include "dccutil.h"
  22. #include "debug.h"
  23. #include "dcc.h"
  24. #include "dns.h"
  25. #include "cmds.h"
  26. #include "crypt.h"
  27. #include "chanprog.h"
  28. #include "botmsg.h"
  29. #include "botcmd.h"
  30. #include "botnet.h"
  31. #include "tandem.h"
  32. #include <ctype.h>
  33. #include "core_binds.h"
  34. extern struct dcc_t *dcc;
  35. #ifdef S_AUTHCMDS
  36. extern struct auth_t *auth;
  37. #endif /* S_AUTHCMDS */
  38. #include "users.h"
  39. extern struct userrec *userlist, *lastuser;
  40. extern char tempdir[], botname[], natip[], cmdprefix[],
  41. origbotname[], botuser[], admin[],
  42. userfile[], ver[], kickprefix[], bankickprefix[],
  43. #ifdef S_AUTHHASH
  44. authkey[],
  45. #endif /* S_AUTHHASH */
  46. version[], quit_msg[], bdhash[], dcc_prefix[];
  47. extern int noshare, loading, role, server_lag,
  48. #ifdef S_AUTHCMDS
  49. auth_total,
  50. #endif /* S_AUTHCMDS */
  51. dcc_total, userfile_perm,
  52. use_console_r, ignore_time,
  53. debug_output, default_flags,
  54. max_dcc, password_timeout, localhub,
  55. use_invites, use_exempts,
  56. do_restart, timesync;
  57. extern time_t now, online_since, buildts;
  58. extern struct chanset_t *chanset;
  59. extern tand_t *tandbot;
  60. extern party_t *party;
  61. extern int parties;
  62. extern sock_list *socklist;
  63. #ifdef S_MSGCMDS
  64. extern struct cfg_entry CFG_MSGOP, CFG_MSGPASS, CFG_MSGINVITE, CFG_MSGIDENT;
  65. #endif /* S_MSGCMDS */
  66. static int module_rename(char *name, char *newname);
  67. struct static_list {
  68. struct static_list *next;
  69. char *name;
  70. char *(*func) ();
  71. } *static_modules = NULL;
  72. void check_static(char *name, char *(*func) ())
  73. {
  74. struct static_list *p = NULL;
  75. p = calloc(1, sizeof(struct static_list));
  76. p->name = strdup(name);
  77. p->func = func;
  78. p->next = static_modules;
  79. static_modules = p;
  80. }
  81. void *mod_killsock(int size, const char *modname, const char *filename, int line)
  82. {
  83. char x[100] = "", *p = NULL;
  84. p = strrchr(filename, '/');
  85. egg_snprintf(x, sizeof x, "%s:%s", modname, p ? p + 1 : filename);
  86. x[19] = 0;
  87. real_killsock(size, x, line);
  88. return NULL;
  89. }
  90. /* The null functions */
  91. void null_func()
  92. {
  93. }
  94. char *charp_func()
  95. {
  96. return NULL;
  97. }
  98. int minus_func()
  99. {
  100. return -1;
  101. }
  102. int false_func()
  103. {
  104. return 0;
  105. }
  106. /*
  107. * Various hooks & things
  108. */
  109. /* The REAL hooks, when these are called, a return of 0 indicates unhandled
  110. * 1 is handled
  111. */
  112. struct hook_entry *hook_list[REAL_HOOKS];
  113. void (*qserver) (int, char *, int) = (void (*)(int, char *, int)) null_func;
  114. void (*add_mode) () = null_func;
  115. int (*rfc_casecmp) (const char *, const char *) = _rfc_casecmp;
  116. int (*rfc_ncasecmp) (const char *, const char *, int) = _rfc_ncasecmp;
  117. int (*rfc_toupper) (int) = _rfc_toupper;
  118. int (*rfc_tolower) (int) = _rfc_tolower;
  119. module_entry *module_list;
  120. dependancy *dependancy_list = NULL;
  121. /* The horrible global lookup table for functions
  122. * BUT it makes the whole thing *much* more portable than letting each
  123. * OS screw up the symbols their own special way :/
  124. */
  125. Function global_table[] =
  126. {
  127. /* 0 - 3 */
  128. (Function) 0,
  129. (Function) 0,
  130. #ifdef DEBUG_CONTEXT
  131. (Function) eggContext,
  132. #else
  133. (Function) 0,
  134. #endif
  135. (Function) module_rename,
  136. /* 4 - 7 */
  137. (Function) module_register,
  138. (Function) module_find,
  139. (Function) module_depend,
  140. (Function) module_undepend,
  141. /* 8 - 11 */
  142. (Function) 0,
  143. (Function) 0,
  144. (Function) 0,
  145. (Function) 0,
  146. /* 12 - 15 */
  147. (Function) 0,
  148. (Function) 0,
  149. (Function) 0,
  150. (Function) 0,
  151. /* 16 - 19 */
  152. (Function) 0,
  153. (Function) 0,
  154. (Function) 0,
  155. (Function) 0,
  156. /* 20 - 23 */
  157. (Function) base64_to_int,
  158. (Function) int_to_base64,
  159. (Function) int_to_base10,
  160. (Function) simple_sprintf,
  161. /* 24 - 27 */
  162. (Function) botnet_send_zapf,
  163. (Function) botnet_send_zapf_broad,
  164. (Function) botnet_send_unlinked,
  165. (Function) botnet_send_bye,
  166. /* 28 - 31 */
  167. (Function) botnet_send_chat,
  168. (Function) & server_lag, /* int */
  169. (Function) remove_crlf,
  170. (Function) shuffle,
  171. /* 32 - 35 */
  172. (Function) botnet_send_join_idx,
  173. (Function) botnet_send_part_idx,
  174. (Function) updatebot,
  175. (Function) nextbot,
  176. /* 36 - 39 */
  177. (Function) zapfbot,
  178. (Function) 0,
  179. (Function) u_pass_match,
  180. (Function) 0,
  181. /* 40 - 43 */
  182. (Function) get_user,
  183. (Function) set_user,
  184. (Function) add_entry_type,
  185. (Function) del_entry_type,
  186. /* 44 - 47 */
  187. (Function) get_user_flagrec,
  188. (Function) set_user_flagrec,
  189. (Function) get_user_by_host,
  190. (Function) get_user_by_handle,
  191. /* 48 - 51 */
  192. (Function) find_entry_type,
  193. (Function) find_user_entry,
  194. (Function) adduser,
  195. (Function) deluser,
  196. /* 52 - 55 */
  197. (Function) addhost_by_handle,
  198. (Function) delhost_by_handle,
  199. (Function) readuserfile,
  200. (Function) write_userfile,
  201. /* 56 - 59 */
  202. (Function) geticon,
  203. (Function) clear_chanlist,
  204. (Function) reaffirm_owners,
  205. (Function) change_handle,
  206. /* 60 - 63 */
  207. (Function) write_user,
  208. (Function) clear_userlist,
  209. (Function) count_users,
  210. (Function) sanity_check,
  211. /* 64 - 67 */
  212. (Function) break_down_flags,
  213. (Function) build_flags,
  214. (Function) flagrec_eq,
  215. (Function) flagrec_ok,
  216. /* 68 - 71 */
  217. (Function) 0,
  218. (Function) dprintf,
  219. (Function) chatout,
  220. (Function) chanout_but,
  221. /* 72 - 75 */
  222. (Function) 0,
  223. (Function) list_delete,
  224. (Function) list_append,
  225. (Function) list_contains,
  226. /* 76 - 79 */
  227. (Function) answer,
  228. (Function) getmyip,
  229. (Function) neterror,
  230. (Function) tputs,
  231. /* 80 - 83 */
  232. (Function) new_dcc,
  233. (Function) lostdcc,
  234. (Function) getsock,
  235. (Function) mod_killsock,
  236. /* 84 - 87 */
  237. (Function) open_listen_by_af,
  238. (Function) open_telnet_dcc,
  239. (Function) 0,
  240. (Function) open_telnet,
  241. /* 88 - 91 */
  242. (Function) check_bind_event,
  243. (Function) egg_memcpy,
  244. (Function) my_atoul,
  245. (Function) my_strcpy,
  246. /* 92 - 95 */
  247. (Function) & dcc, /* struct dcc_t * */
  248. (Function) & chanset, /* struct chanset_t * */
  249. (Function) & userlist, /* struct userrec * */
  250. (Function) & lastuser, /* struct userrec * */
  251. /* 96 - 99 */
  252. (Function) & global_bans, /* struct banrec * */
  253. (Function) & global_ign, /* struct igrec * */
  254. (Function) & password_timeout, /* int */
  255. (Function) md5,
  256. /* 100 - 103 */
  257. (Function) & max_dcc, /* int */
  258. (Function) shouldjoin,
  259. (Function) & ignore_time, /* int */
  260. (Function) & use_console_r, /* int */
  261. /* 104 - 107 */
  262. (Function) 0,
  263. (Function) 0,
  264. (Function) & debug_output, /* int */
  265. (Function) & noshare, /* int */
  266. /* 108 - 111 */
  267. (Function) do_chanset,
  268. (Function) str_isdigit,
  269. (Function) & default_flags, /* int */
  270. (Function) & dcc_total, /* int */
  271. /* 112 - 115 */
  272. (Function) tempdir, /* char * */
  273. (Function) natip, /* char * */
  274. (Function) 0,
  275. (Function) origbotname, /* char * */
  276. /* 116 - 119 */
  277. (Function) botuser, /* char * */
  278. (Function) admin, /* char * */
  279. (Function) userfile, /* char * */
  280. (Function) ver, /* char * */
  281. /* 120 - 123 */
  282. (Function) 0,
  283. (Function) dovoice,
  284. (Function) version, /* char * */
  285. (Function) 0,
  286. /* 124 - 127 */
  287. (Function) & DCC_CHAT_PASS, /* struct dcc_table * */
  288. (Function) & DCC_BOT, /* struct dcc_table * */
  289. (Function) & DCC_LOST, /* struct dcc_table * */
  290. (Function) & DCC_CHAT, /* struct dcc_table * */
  291. /* 128 - 131 */
  292. (Function) 0,
  293. (Function) & now, /* time_t */
  294. (Function) 0,
  295. (Function) findchan,
  296. /* 132 - 135 */
  297. (Function) dolimit,
  298. (Function) days,
  299. (Function) daysago,
  300. (Function) daysdur,
  301. /* 136 - 139 */
  302. (Function) ismember,
  303. (Function) newsplit,
  304. (Function) splitnick,
  305. (Function) splitc,
  306. /* 140 - 143 */
  307. (Function) addignore,
  308. (Function) match_ignore,
  309. (Function) delignore,
  310. (Function) fatal,
  311. /* 144 - 147 */
  312. (Function) 0,
  313. (Function) 0,
  314. (Function) movefile,
  315. (Function) copyfile,
  316. /* 148 - 151 */
  317. (Function) 0,
  318. (Function) encrypt_string,
  319. (Function) decrypt_string,
  320. (Function) def_get,
  321. /* 152 - 155 */
  322. (Function) makepass,
  323. (Function) _wild_match,
  324. (Function) maskhost,
  325. (Function) private,
  326. /* 156 - 159 */
  327. (Function) chk_op,
  328. (Function) chk_deop,
  329. (Function) chk_voice,
  330. (Function) chk_devoice,
  331. /* 160 - 163 */
  332. (Function) touch_laston,
  333. (Function) & add_mode, /* Function * */
  334. (Function) rmspace,
  335. (Function) in_chain,
  336. /* 164 - 167 */
  337. (Function) add_note,
  338. (Function) btoh,
  339. (Function) detect_dcc_flood,
  340. (Function) flush_lines,
  341. /* 168 - 171 */
  342. (Function) 0,
  343. (Function) 0,
  344. (Function) & do_restart, /* int */
  345. (Function) 0,
  346. /* 172 - 175 */
  347. (Function) add_hook,
  348. (Function) del_hook,
  349. (Function) 0,
  350. (Function) 0,
  351. /* 176 - 179 */
  352. (Function) 0,
  353. (Function) 0,
  354. (Function) 0,
  355. (Function) 0,
  356. /* 180 - 183 */
  357. (Function) 0,
  358. (Function) 0,
  359. (Function) 0,
  360. (Function) 0,
  361. /* 184 - 187 */
  362. (Function) 0,
  363. (Function) 0,
  364. (Function) 0,
  365. (Function) 0,
  366. /* 188 - 191 */
  367. (Function) & USERENTRY_BOTADDR, /* struct user_entry_type * */
  368. (Function) & USERENTRY_BOTFL, /* struct user_entry_type * */
  369. (Function) & USERENTRY_HOSTS, /* struct user_entry_type * */
  370. (Function) & USERENTRY_PASS, /* struct user_entry_type * */
  371. /* 192 - 195 */
  372. (Function) 0,
  373. (Function) user_del_chan,
  374. (Function) & USERENTRY_INFO, /* struct user_entry_type * */
  375. (Function) & USERENTRY_COMMENT, /* struct user_entry_type * */
  376. /* 196 - 199 */
  377. (Function) & USERENTRY_LASTON, /* struct user_entry_type * */
  378. (Function) putlog,
  379. (Function) botnet_send_chan,
  380. (Function) list_type_kill,
  381. /* 200 - 203 */
  382. (Function) logmodes,
  383. (Function) masktype,
  384. (Function) stripmodes,
  385. (Function) stripmasktype,
  386. /* 204 - 207 */
  387. (Function) & online_since, /* time_t * */
  388. (Function) & buildts, /* time_t * */
  389. (Function) color,
  390. (Function) check_dcc_attrs,
  391. /* 208 - 211 */
  392. (Function) check_dcc_chanattrs,
  393. (Function) 0,
  394. (Function) 0,
  395. (Function) botname,
  396. /* 212 - 215 */
  397. (Function) 0, /* remove_gunk() -- UNUSED! (drummer) */
  398. (Function) check_bind_chjn,
  399. (Function) sanitycheck_dcc,
  400. (Function) isowner,
  401. /* 216 - 219 */
  402. (Function) 0, /* min_dcc_port -- UNUSED! (guppy) */
  403. (Function) 0, /* max_dcc_port -- UNUSED! (guppy) */
  404. (Function) & rfc_casecmp, /* Function * */
  405. (Function) & rfc_ncasecmp, /* Function * */
  406. /* 220 - 223 */
  407. (Function) & global_exempts, /* struct exemptrec * */
  408. (Function) & global_invites, /* struct inviterec * */
  409. (Function) 0, /* ginvite_total -- UNUSED! (Eule) */
  410. (Function) 0, /* gexempt_total -- UNUSED! (Eule) */
  411. /* 224 - 227 */
  412. (Function) 0,
  413. (Function) & use_exempts, /* int */
  414. (Function) & use_invites, /* int */
  415. (Function) 0,
  416. /* 228 - 231 */
  417. (Function) 0,
  418. (Function) 0,
  419. (Function) 0,
  420. (Function) 0,
  421. /* 232 - 235 */
  422. #ifdef DEBUG_CONTEXT
  423. (Function) eggContextNote,
  424. #else
  425. (Function) 0,
  426. #endif
  427. #ifdef DEBUG_ASSERT
  428. (Function) eggAssert,
  429. #else
  430. (Function) 0,
  431. #endif
  432. (Function) allocsock,
  433. (Function) call_hostbyip,
  434. /* 236 - 239 */
  435. (Function) call_ipbyhost,
  436. (Function) iptostr,
  437. (Function) & DCC_DNSWAIT, /* struct dcc_table * */
  438. (Function) hostsanitycheck_dcc,
  439. /* 240 - 243 */
  440. (Function) dcc_dnsipbyhost,
  441. (Function) dcc_dnshostbyip,
  442. (Function) changeover_dcc,
  443. (Function) make_rand_str,
  444. /* 244 - 247 */
  445. (Function) 0,
  446. (Function) findchan_by_dname,
  447. (Function) 0,
  448. (Function) & userfile_perm, /* int */
  449. /* 248 - 251 */
  450. (Function) sock_has_data,
  451. (Function) bots_in_subtree,
  452. (Function) users_in_subtree,
  453. (Function) egg_inet_aton,
  454. /* 252 - 255 */
  455. (Function) egg_snprintf,
  456. (Function) egg_vsnprintf,
  457. (Function) egg_memset,
  458. (Function) egg_strcasecmp,
  459. /* 256 - 259 */
  460. (Function) egg_strncasecmp,
  461. (Function) is_file,
  462. (Function) 0,
  463. (Function) & tandbot, /* tand_t * */
  464. /* 260 - 263 */
  465. (Function) & party, /* party_t * */
  466. (Function) open_address_listen,
  467. (Function) str_escape,
  468. (Function) strchr_unescape,
  469. /* 264 - 267 */
  470. (Function) str_unescape,
  471. (Function) egg_strcatn,
  472. (Function) clear_chanlist_member,
  473. (Function) fixfrom,
  474. /* 268 - 272 */
  475. (Function) sockprotocol,
  476. (Function) & socklist, /* sock_list * */
  477. (Function) sockoptions,
  478. (Function) flush_inbuf,
  479. (Function) kill_bot,
  480. /* 273 - 276 */
  481. (Function) quit_msg, /* char * */
  482. (Function) module_load,
  483. (Function) 0,
  484. (Function) & parties, /* int */
  485. /* 277 - 280 */
  486. (Function) ischanhub,
  487. (Function) rand_dccresp,
  488. (Function) 0,
  489. #ifdef LEAF
  490. (Function) listen_all,
  491. #else
  492. (Function) 0, /* listen_all */
  493. #endif
  494. /* 281 - 284 */
  495. /* gay.
  496. (Function) MD5_Init,
  497. (Function) MD5_Update,
  498. (Function) MD5_Final,
  499. */
  500. (Function) 0,
  501. (Function) 0,
  502. (Function) 0,
  503. (Function) _wild_match_per,
  504. /* 285 - 288 */
  505. (Function) & role, /* int */
  506. (Function) & loading, /* int */
  507. (Function) & localhub, /* int */
  508. (Function) updatebin,
  509. (Function) stats_add,
  510. (Function) lower_bot_linked,
  511. (Function) add_cfg,
  512. (Function) set_cfg_str,
  513. (Function) trigger_cfg_changed,
  514. (Function) higher_bot_linked,
  515. (Function) bot_aggressive_to,
  516. (Function) botunlink,
  517. (Function) 0,
  518. (Function) & timesync, /* int */
  519. (Function) 0,
  520. (Function) kickreason,
  521. (Function) getting_users,
  522. (Function) 0,
  523. (Function) 0,
  524. (Function) & USERENTRY_ADDED, /* struct user_entry_type * */
  525. (Function) bdhash,
  526. (Function) isupdatehub,
  527. (Function) 0,
  528. (Function) botlink,
  529. (Function) makeplaincookie,
  530. (Function) bankickprefix,
  531. (Function) kickprefix,
  532. (Function) deflag_user,
  533. (Function) dcc_prefix,
  534. (Function) goodpass,
  535. #ifdef S_AUTHCMDS
  536. (Function) & auth, /* struct auth_t *auth */
  537. (Function) & auth_total,
  538. (Function) new_auth,
  539. (Function) findauth,
  540. (Function) removeauth,
  541. #else
  542. (Function) 0,
  543. (Function) 0,
  544. (Function) 0,
  545. (Function) 0,
  546. (Function) 0,
  547. #endif /* S_AUTHCMDS */
  548. #ifdef S_AUTHHASH
  549. (Function) makehash,
  550. (Function) authkey,
  551. #else /* !S_AUTHHASH */
  552. (Function) 0,
  553. (Function) 0,
  554. #endif /* S_AUTHHASH */
  555. (Function) & USERENTRY_SECPASS,
  556. (Function) 0,
  557. (Function) 0,
  558. (Function) cmdprefix,
  559. (Function) replace,
  560. (Function) degarble,
  561. (Function) egg_inet_ntop,
  562. (Function) open_listen,
  563. (Function) hostprotocol,
  564. (Function) sdprintf,
  565. (Function) putbot,
  566. (Function) putallbots,
  567. (Function) ssl_link,
  568. (Function) dropssl,
  569. (Function) myipstr,
  570. (Function) checkchans,
  571. #ifdef S_MSGCMDS
  572. (Function) & CFG_MSGOP,
  573. (Function) & CFG_MSGPASS,
  574. (Function) & CFG_MSGINVITE,
  575. (Function) & CFG_MSGIDENT,
  576. #else /* !S_MSGCMDS */
  577. (Function) 0,
  578. (Function) 0,
  579. (Function) 0,
  580. (Function) 0,
  581. #endif /* S_MSGCMDS */
  582. (Function) bind_table_add,
  583. (Function) bind_table_add,
  584. (Function) add_builtins,
  585. (Function) rem_builtins,
  586. (Function) bind_table_lookup,
  587. (Function) check_bind
  588. };
  589. static bind_table_t *BT_load = NULL;
  590. void init_modules(void)
  591. {
  592. int i;
  593. BT_load = bind_table_add("load", 1, "s", MATCH_MASK, 0);
  594. module_list = calloc(1, sizeof(module_entry));
  595. module_list->name = strdup("eggdrop");
  596. module_list->next = NULL;
  597. module_list->funcs = NULL;
  598. for (i = 0; i < REAL_HOOKS; i++)
  599. hook_list[i] = NULL;
  600. }
  601. int module_register(char *name, Function * funcs, int major, int minor)
  602. {
  603. module_entry *p = NULL;
  604. for (p = module_list; p && p->name; p = p->next)
  605. if (!egg_strcasecmp(name, p->name)) {
  606. p->funcs = funcs;
  607. return 1;
  608. }
  609. return 0;
  610. }
  611. const char *module_load(char *name)
  612. {
  613. module_entry *p = NULL;
  614. char *e = NULL;
  615. Function f;
  616. struct static_list *sl = NULL;
  617. sdprintf("module_load(\"%s\")", name);
  618. if (module_find(name, 0, 0) != NULL)
  619. return "Already loaded";
  620. for (sl = static_modules; sl && egg_strcasecmp(sl->name, name); sl = sl->next);
  621. if (!sl)
  622. fatal("Unknown module.", 0);
  623. f = (Function) sl->func;
  624. p = calloc(1, sizeof(module_entry));
  625. p->name = strdup(name);
  626. p->funcs = 0;
  627. p->next = module_list;
  628. module_list = p;
  629. e = (((char *(*)()) f) (global_table));
  630. if (e) {
  631. module_list = module_list->next;
  632. free(p->name);
  633. free(p);
  634. return e;
  635. }
  636. check_bind(BT_load, name, NULL, name);
  637. return NULL;
  638. }
  639. module_entry *module_find(char *name, int major, int minor)
  640. {
  641. module_entry *p;
  642. for (p = module_list; p && p->name; p = p->next)
  643. if (!egg_strcasecmp(name, p->name))
  644. return p;
  645. return NULL;
  646. }
  647. static int module_rename(char *name, char *newname)
  648. {
  649. module_entry *p;
  650. for (p = module_list; p; p = p->next)
  651. if (!egg_strcasecmp(newname, p->name))
  652. return 0;
  653. for (p = module_list; p && p->name; p = p->next)
  654. if (!egg_strcasecmp(name, p->name)) {
  655. free(p->name);
  656. p->name = strdup(newname);
  657. return 1;
  658. }
  659. return 0;
  660. }
  661. Function *module_depend(char *name1, char *name2, int major, int minor)
  662. {
  663. module_entry *p = module_find(name2, major, minor);
  664. module_entry *o = module_find(name1, 0, 0);
  665. dependancy *d = NULL;
  666. if (!p) {
  667. if (module_load(name2))
  668. return 0;
  669. p = module_find(name2, major, minor);
  670. }
  671. if (!p || !o)
  672. return 0;
  673. d = calloc(1, sizeof(dependancy));
  674. d->needed = p;
  675. d->needing = o;
  676. d->next = dependancy_list;
  677. dependancy_list = d;
  678. return p->funcs ? p->funcs : (Function *) 1;
  679. }
  680. int module_undepend(char *name1)
  681. {
  682. int ok = 0;
  683. module_entry *p = module_find(name1, 0, 0);
  684. dependancy *d = dependancy_list, *o = NULL;
  685. if (p == NULL)
  686. return 0;
  687. while (d != NULL) {
  688. if (d->needing == p) {
  689. if (o == NULL) {
  690. dependancy_list = d->next;
  691. } else {
  692. o->next = d->next;
  693. }
  694. free(d);
  695. if (o == NULL)
  696. d = dependancy_list;
  697. else
  698. d = o->next;
  699. ok++;
  700. } else {
  701. o = d;
  702. d = d->next;
  703. }
  704. }
  705. return ok;
  706. }
  707. /* Hooks, various tables of functions to call on ceratin events
  708. */
  709. void add_hook(int hook_num, Function func)
  710. {
  711. if (hook_num < REAL_HOOKS) {
  712. struct hook_entry *p = NULL;
  713. for (p = hook_list[hook_num]; p; p = p->next)
  714. if (p->func == func)
  715. return; /* Don't add it if it's already there */
  716. p = calloc(1, sizeof(struct hook_entry));
  717. p->next = hook_list[hook_num];
  718. hook_list[hook_num] = p;
  719. p->func = func;
  720. } else
  721. switch (hook_num) {
  722. case HOOK_QSERV:
  723. if (qserver == (void (*)(int, char *, int)) null_func)
  724. qserver = (void (*)(int, char *, int)) func;
  725. break;
  726. case HOOK_ADD_MODE:
  727. if (add_mode == (void (*)()) null_func)
  728. add_mode = (void (*)()) func;
  729. break;
  730. /* special hook <drummer> */
  731. case HOOK_RFC_CASECMP:
  732. if (func == NULL) {
  733. rfc_casecmp = egg_strcasecmp;
  734. rfc_ncasecmp = (int (*)(const char *, const char *, int)) egg_strncasecmp;
  735. rfc_tolower = tolower;
  736. rfc_toupper = toupper;
  737. } else {
  738. rfc_casecmp = _rfc_casecmp;
  739. rfc_ncasecmp = _rfc_ncasecmp;
  740. rfc_tolower = _rfc_tolower;
  741. rfc_toupper = _rfc_toupper;
  742. }
  743. break;
  744. }
  745. }
  746. void del_hook(int hook_num, Function func)
  747. {
  748. if (hook_num < REAL_HOOKS) {
  749. struct hook_entry *p = hook_list[hook_num], *o = NULL;
  750. while (p) {
  751. if (p->func == func) {
  752. if (o == NULL)
  753. hook_list[hook_num] = p->next;
  754. else
  755. o->next = p->next;
  756. free(p);
  757. break;
  758. }
  759. o = p;
  760. p = p->next;
  761. }
  762. } else
  763. switch (hook_num) {
  764. case HOOK_QSERV:
  765. if (qserver == (void (*)(int, char *, int)) func)
  766. qserver = null_func;
  767. break;
  768. case HOOK_ADD_MODE:
  769. if (add_mode == (void (*)()) func)
  770. add_mode = null_func;
  771. break;
  772. }
  773. }
  774. int call_hook_cccc(int hooknum, char *a, char *b, char *c, char *d)
  775. {
  776. struct hook_entry *p, *pn;
  777. int f = 0;
  778. if (hooknum >= REAL_HOOKS)
  779. return 0;
  780. p = hook_list[hooknum];
  781. for (p = hook_list[hooknum]; p && !f; p = pn) {
  782. pn = p->next;
  783. f = p->func(a, b, c, d);
  784. }
  785. return f;
  786. }
  787. void do_module_report(int idx, int details, char *which)
  788. {
  789. module_entry *p = module_list;
  790. if (p && !which && details)
  791. dprintf(idx, "MODULES LOADED:\n");
  792. for (; p; p = p->next) {
  793. if (!which || !egg_strcasecmp(which, p->name)) {
  794. dependancy *d;
  795. if (details)
  796. dprintf(idx, "Module: %s\n", p->name ? p->name : "CORE");
  797. if (details > 1) {
  798. for (d = dependancy_list; d; d = d->next)
  799. if (d->needing == p)
  800. dprintf(idx, " requires: %s\n", d->needed->name);
  801. }
  802. if (p->funcs) {
  803. Function f = p->funcs[MODCALL_REPORT];
  804. if (f != NULL)
  805. f(idx, details);
  806. }
  807. if (which)
  808. return;
  809. }
  810. }
  811. if (which)
  812. dprintf(idx, "No such module.\n");
  813. }