modules.c 25 KB

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