modules.c 25 KB

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