modules.c 26 KB

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