modules.c 23 KB

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