server.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107
  1. #ifdef LEAF
  2. /*
  3. * server.c -- part of server.mod
  4. * basic irc server support
  5. *
  6. */
  7. #define MODULE_NAME "server"
  8. #define MAKING_SERVER
  9. #include "src/mod/module.h"
  10. #include "server.h"
  11. static Function *global = NULL, *encryption_funcs;
  12. extern struct cfg_entry CFG_OPTIMESLACK;
  13. static int ctcp_mode;
  14. static int serv; /* sock # of server currently */
  15. static int strict_host; /* strict masking of hosts ? */
  16. static char newserver[121]; /* new server? */
  17. static int newserverport; /* new server port? */
  18. static char newserverpass[121]; /* new server password? */
  19. static time_t trying_server; /* trying to connect to a server right now? */
  20. static int server_lag; /* how lagged (in seconds) is the server? */
  21. static char altnick[NICKLEN]; /* possible alternate nickname to use */
  22. static char raltnick[NICKLEN]; /* random nick created from altnick */
  23. static int curserv; /* current position in server list: */
  24. static int flud_thr; /* msg flood threshold */
  25. static int flud_time; /* msg flood time */
  26. static int flud_ctcp_thr; /* ctcp flood threshold */
  27. static int flud_ctcp_time; /* ctcp flood time */
  28. static char initserver[121]; /* what, if anything, to send to the
  29. server on connection */
  30. static char botuserhost[121]; /* bot's user@host (refreshed whenever the
  31. bot joins a channel) */
  32. /* may not be correct user@host BUT it's
  33. how the server sees it */
  34. static int keepnick; /* keep trying to regain my intended
  35. nickname? */
  36. static int nick_juped = 0; /* True if origbotname is juped(RPL437) (dw) */
  37. static int check_stoned; /* Check for a stoned server? */
  38. static int serverror_quit; /* Disconnect from server if ERROR
  39. messages received? */
  40. static int quiet_reject; /* Quietly reject dcc chat or sends from
  41. users without access? */
  42. static int waiting_for_awake; /* set when i unidle myself, cleared when
  43. i get the response */
  44. static time_t server_online; /* server connection time */
  45. static time_t server_cycle_wait; /* seconds to wait before
  46. re-beginning the server list */
  47. static char botrealname[121]; /* realname of bot */
  48. static int min_servs; /* minimum number of servers to be around */
  49. static int server_timeout; /* server timeout for connecting */
  50. static int never_give_up; /* never give up when connecting to servers? */
  51. static int strict_servernames; /* don't update server list */
  52. static struct server_list *serverlist; /* old-style queue, still used by
  53. server list */
  54. static int cycle_time; /* cycle time till next server connect */
  55. static int default_port; /* default IRC port */
  56. static char oldnick[NICKLEN]; /* previous nickname *before* rehash */
  57. static int trigger_on_ignore; /* trigger bindings if user is ignored ? */
  58. static int answer_ctcp; /* answer how many stacked ctcp's ? */
  59. static int lowercase_ctcp; /* answer lowercase CTCP's (non-standard) */
  60. static int check_mode_r; /* check for IRCNET +r modes */
  61. static int net_type;
  62. static char connectserver[121]; /* what, if anything, to do before connect
  63. to the server */
  64. static int resolvserv; /* in the process of resolving a server host */
  65. static int double_mode; /* allow a msgs to be twice in a queue? */
  66. static int double_server;
  67. static int double_help;
  68. static int double_warned;
  69. static int lastpingtime; /* IRCNet LAGmeter support -- drummer */
  70. static char stackablecmds[511];
  71. static char stackable2cmds[511];
  72. static time_t last_time;
  73. static int use_penalties;
  74. static int use_fastdeq;
  75. static int nick_len; /* Maximal nick length allowed on the
  76. network. */
  77. static int kick_method;
  78. static int optimize_kicks;
  79. static p_tcl_bind_list H_wall, H_raw, H_notc, H_msgm, H_msg, H_flud,
  80. H_ctcr, H_ctcp;
  81. #ifdef S_AUTH
  82. static p_tcl_bind_list H_msgc;
  83. #endif /* S_AUTH */
  84. static void empty_msgq(void);
  85. static void next_server(int *, char *, unsigned int *, char *);
  86. static void disconnect_server(int);
  87. static char *get_altbotnick(void);
  88. static int calc_penalty(char *);
  89. static int fast_deq(int);
  90. static char *splitnicks(char **);
  91. static void check_queues(char *, char *);
  92. static void parse_q(struct msgq_head *, char *, char *);
  93. static void purge_kicks(struct msgq_head *);
  94. static int deq_kick(int);
  95. static void msgq_clear(struct msgq_head *qh);
  96. static int stack_limit;
  97. #include "servmsg.c"
  98. #define MAXPENALTY 10
  99. /* Number of seconds to wait between transmitting queued lines to the server
  100. * lower this value at your own risk. ircd is known to start flood control
  101. * at 512 bytes/2 seconds.
  102. */
  103. #define msgrate 2
  104. /* Maximum messages to store in each queue. */
  105. static int maxqmsg;
  106. static struct msgq_head mq, hq, modeq;
  107. static int burst;
  108. #include "cmdsserv.c"
  109. #include "tclserv.c"
  110. /*
  111. * Bot server queues
  112. */
  113. /* Called periodically to shove out another queued item.
  114. *
  115. * 'mode' queue gets priority now.
  116. *
  117. * Most servers will allow 'busts' of upto 5 msgs, so let's put something
  118. * in to support flushing modeq a little faster if possible.
  119. * Will send upto 4 msgs from modeq, and then send 1 msg every time
  120. * it will *not* send anything from hq until the 'burst' value drops
  121. * down to 0 again (allowing a sudden mq flood to sneak through).
  122. */
  123. static void deq_msg()
  124. {
  125. struct msgq *q;
  126. int ok = 0;
  127. /* now < last_time tested 'cause clock adjustments could mess it up */
  128. if ((now - last_time) >= msgrate || now < (last_time - 90)) {
  129. last_time = now;
  130. if (burst > 0)
  131. burst--;
  132. ok = 1;
  133. }
  134. if (serv < 0)
  135. return;
  136. /* Send upto 4 msgs to server if the *critical queue* has anything in it */
  137. if (modeq.head) {
  138. while (modeq.head && (burst < 4) && ((last_time - now) < MAXPENALTY)) {
  139. if (deq_kick(DP_MODE)) {
  140. burst++;
  141. continue;
  142. }
  143. if (!modeq.head)
  144. break;
  145. if (fast_deq(DP_MODE)) {
  146. burst++;
  147. continue;
  148. }
  149. tputs(serv, modeq.head->msg, modeq.head->len);
  150. if (debug_output) {
  151. modeq.head->msg[strlen(modeq.head->msg) - 1] = 0; /* delete the "\n" */
  152. putlog(LOG_SRVOUT, "@", "[m->] %s", modeq.head->msg);
  153. }
  154. modeq.tot--;
  155. last_time += calc_penalty(modeq.head->msg);
  156. q = modeq.head->next;
  157. nfree(modeq.head->msg);
  158. nfree(modeq.head);
  159. modeq.head = q;
  160. burst++;
  161. }
  162. if (!modeq.head)
  163. modeq.last = 0;
  164. return;
  165. }
  166. /* Send something from the normal msg q even if we're slightly bursting */
  167. if (burst > 1)
  168. return;
  169. if (mq.head) {
  170. burst++;
  171. if (deq_kick(DP_SERVER))
  172. return;
  173. if (fast_deq(DP_SERVER))
  174. return;
  175. tputs(serv, mq.head->msg, mq.head->len);
  176. if (debug_output) {
  177. mq.head->msg[strlen(mq.head->msg) - 1] = 0; /* delete the "\n" */
  178. putlog(LOG_SRVOUT, "@", "[s->] %s", mq.head->msg);
  179. }
  180. mq.tot--;
  181. last_time += calc_penalty(mq.head->msg);
  182. q = mq.head->next;
  183. nfree(mq.head->msg);
  184. nfree(mq.head);
  185. mq.head = q;
  186. if (!mq.head)
  187. mq.last = NULL;
  188. return;
  189. }
  190. /* Never send anything from the help queue unless everything else is
  191. * finished.
  192. */
  193. if (!hq.head || burst || !ok)
  194. return;
  195. if (deq_kick(DP_HELP))
  196. return;
  197. if (fast_deq(DP_HELP))
  198. return;
  199. tputs(serv, hq.head->msg, hq.head->len);
  200. if (debug_output) {
  201. hq.head->msg[strlen(hq.head->msg) - 1] = 0; /* delete the "\n" */
  202. putlog(LOG_SRVOUT, "*", "[h->] %s", hq.head->msg);
  203. }
  204. hq.tot--;
  205. last_time += calc_penalty(hq.head->msg);
  206. q = hq.head->next;
  207. nfree(hq.head->msg);
  208. nfree(hq.head);
  209. hq.head = q;
  210. if (!hq.head)
  211. hq.last = NULL;
  212. }
  213. static int calc_penalty(char * msg)
  214. {
  215. char *cmd, *par1, *par2, *par3;
  216. register int penalty, i, ii;
  217. if (!use_penalties &&
  218. net_type != NETT_UNDERNET && net_type != NETT_HYBRID_EFNET)
  219. return 0;
  220. if (msg[strlen(msg) - 1] == '\n')
  221. msg[strlen(msg) - 1] = '\0';
  222. cmd = newsplit(&msg);
  223. if (msg)
  224. i = strlen(msg);
  225. else
  226. i = strlen(cmd);
  227. last_time -= 2; /* undo eggdrop standard flood prot */
  228. if (net_type == NETT_UNDERNET || net_type == NETT_HYBRID_EFNET) {
  229. last_time += (2 + i / 120);
  230. return 0;
  231. }
  232. penalty = (1 + i / 100);
  233. if (!egg_strcasecmp(cmd, "KICK")) {
  234. par1 = newsplit(&msg); /* channel */
  235. par2 = newsplit(&msg); /* victim(s) */
  236. par3 = splitnicks(&par2);
  237. penalty++;
  238. while (strlen(par3) > 0) {
  239. par3 = splitnicks(&par2);
  240. penalty++;
  241. }
  242. ii = penalty;
  243. par3 = splitnicks(&par1);
  244. while (strlen(par1) > 0) {
  245. par3 = splitnicks(&par1);
  246. penalty += ii;
  247. }
  248. } else if (!egg_strcasecmp(cmd, "MODE")) {
  249. i = 0;
  250. par1 = newsplit(&msg); /* channel */
  251. par2 = newsplit(&msg); /* mode(s) */
  252. if (!strlen(par2))
  253. i++;
  254. while (strlen(par2) > 0) {
  255. if (strchr("ntimps", par2[0]))
  256. i += 3;
  257. else if (!strchr("+-", par2[0]))
  258. i += 1;
  259. par2++;
  260. }
  261. while (strlen(msg) > 0) {
  262. newsplit(&msg);
  263. i += 2;
  264. }
  265. ii = 0;
  266. while (strlen(par1) > 0) {
  267. splitnicks(&par1);
  268. ii++;
  269. }
  270. penalty += (ii * i);
  271. } else if (!egg_strcasecmp(cmd, "TOPIC")) {
  272. penalty++;
  273. par1 = newsplit(&msg); /* channel */
  274. par2 = newsplit(&msg); /* topic */
  275. if (strlen(par2) > 0) { /* topic manipulation => 2 penalty points */
  276. penalty += 2;
  277. par3 = splitnicks(&par1);
  278. while (strlen(par1) > 0) {
  279. par3 = splitnicks(&par1);
  280. penalty += 2;
  281. }
  282. }
  283. } else if (!egg_strcasecmp(cmd, "PRIVMSG") ||
  284. !egg_strcasecmp(cmd, "NOTICE")) {
  285. par1 = newsplit(&msg); /* channel(s)/nick(s) */
  286. /* Add one sec penalty for each recipient */
  287. while (strlen(par1) > 0) {
  288. splitnicks(&par1);
  289. penalty++;
  290. }
  291. } else if (!egg_strcasecmp(cmd, "WHO")) {
  292. par1 = newsplit(&msg); /* masks */
  293. par2 = par1;
  294. while (strlen(par1) > 0) {
  295. par2 = splitnicks(&par1);
  296. if (strlen(par2) > 4) /* long WHO-masks receive less penalty */
  297. penalty += 3;
  298. else
  299. penalty += 5;
  300. }
  301. } else if (!egg_strcasecmp(cmd, "AWAY")) {
  302. if (strlen(msg) > 0)
  303. penalty += 2;
  304. else
  305. penalty += 1;
  306. } else if (!egg_strcasecmp(cmd, "INVITE")) {
  307. /* Successful invite receives 2 or 3 penalty points. Let's go
  308. * with the maximum.
  309. */
  310. penalty += 3;
  311. } else if (!egg_strcasecmp(cmd, "JOIN")) {
  312. penalty += 2;
  313. } else if (!egg_strcasecmp(cmd, "PART")) {
  314. penalty += 4;
  315. } else if (!egg_strcasecmp(cmd, "VERSION")) {
  316. penalty += 2;
  317. } else if (!egg_strcasecmp(cmd, "TIME")) {
  318. penalty += 2;
  319. } else if (!egg_strcasecmp(cmd, "TRACE")) {
  320. penalty += 2;
  321. } else if (!egg_strcasecmp(cmd, "NICK")) {
  322. penalty += 3;
  323. } else if (!egg_strcasecmp(cmd, "ISON")) {
  324. penalty += 1;
  325. } else if (!egg_strcasecmp(cmd, "WHOIS")) {
  326. penalty += 2;
  327. } else if (!egg_strcasecmp(cmd, "DNS")) {
  328. penalty += 2;
  329. } else
  330. penalty++; /* just add standard-penalty */
  331. /* Shouldn't happen, but you never know... */
  332. if (penalty > 99)
  333. penalty = 99;
  334. if (penalty < 2) {
  335. putlog(LOG_SRVOUT, "*", "Penalty < 2sec, that's impossible!");
  336. penalty = 2;
  337. }
  338. if (debug_output && penalty != 0)
  339. putlog(LOG_SRVOUT, "*", "Adding penalty: %i", penalty);
  340. return penalty;
  341. }
  342. char *splitnicks(char **rest)
  343. {
  344. register char *o, *r;
  345. if (!rest)
  346. return *rest = "";
  347. o = *rest;
  348. while (*o == ' ')
  349. o++;
  350. r = o;
  351. while (*o && *o != ',')
  352. o++;
  353. if (*o)
  354. *o++ = 0;
  355. *rest = o;
  356. return r;
  357. }
  358. static int fast_deq(int which)
  359. {
  360. struct msgq_head *h;
  361. struct msgq *m, *nm;
  362. char msgstr[511], nextmsgstr[511], tosend[511], victims[511], stackable[511],
  363. *msg, *nextmsg, *cmd, *nextcmd, *to, *nextto, *stckbl;
  364. int len, doit = 0, found = 0, cmd_count =0, stack_method = 1;
  365. if (!use_fastdeq)
  366. return 0;
  367. switch (which) {
  368. case DP_MODE:
  369. h = &modeq;
  370. break;
  371. case DP_SERVER:
  372. h = &mq;
  373. break;
  374. case DP_HELP:
  375. h = &hq;
  376. break;
  377. default:
  378. return 0;
  379. }
  380. m = h->head;
  381. strncpyz(msgstr, m->msg, sizeof msgstr);
  382. msg = msgstr;
  383. cmd = newsplit(&msg);
  384. if (use_fastdeq > 1) {
  385. strncpyz(stackable, stackablecmds, sizeof stackable);
  386. stckbl = stackable;
  387. while (strlen(stckbl) > 0)
  388. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  389. found = 1;
  390. break;
  391. }
  392. /* If use_fastdeq is 2, only commands in the list should be stacked. */
  393. if (use_fastdeq == 2 && !found)
  394. return 0;
  395. /* If use_fastdeq is 3, only commands that are _not_ in the list
  396. * should be stacked.
  397. */
  398. if (use_fastdeq == 3 && found)
  399. return 0;
  400. /* we check for the stacking method (default=1) */
  401. strncpyz(stackable, stackable2cmds, sizeof stackable);
  402. stckbl = stackable;
  403. while (strlen(stckbl) > 0)
  404. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  405. stack_method = 2;
  406. break;
  407. }
  408. }
  409. to = newsplit(&msg);
  410. len = strlen(to);
  411. if (to[len - 1] == '\n')
  412. to[len -1] = 0;
  413. simple_sprintf(victims, "%s", to);
  414. while (m) {
  415. nm = m->next;
  416. if (!nm)
  417. break;
  418. strncpyz(nextmsgstr, nm->msg, sizeof nextmsgstr);
  419. nextmsg = nextmsgstr;
  420. nextcmd = newsplit(&nextmsg);
  421. nextto = newsplit(&nextmsg);
  422. len = strlen(nextto);
  423. if (nextto[len - 1] == '\n')
  424. nextto[len - 1] = 0;
  425. if ( strcmp(to, nextto) /* we don't stack to the same recipients */
  426. && !strcmp(cmd, nextcmd) && !strcmp(msg, nextmsg)
  427. && ((strlen(cmd) + strlen(victims) + strlen(nextto)
  428. + strlen(msg) + 2) < 510)
  429. && (!stack_limit || cmd_count < stack_limit - 1)) {
  430. cmd_count++;
  431. if (stack_method == 1)
  432. simple_sprintf(victims, "%s,%s", victims, nextto);
  433. else
  434. simple_sprintf(victims, "%s %s", victims, nextto);
  435. doit = 1;
  436. m->next = nm->next;
  437. if (!nm->next)
  438. h->last = m;
  439. nfree(nm->msg);
  440. nfree(nm);
  441. h->tot--;
  442. } else
  443. m = m->next;
  444. }
  445. if (doit) {
  446. simple_sprintf(tosend, "%s %s %s", cmd, victims, msg);
  447. len = strlen(tosend);
  448. tosend[len - 1] = '\n';
  449. tputs(serv, tosend, len);
  450. m = h->head->next;
  451. nfree(h->head->msg);
  452. nfree(h->head);
  453. h->head = m;
  454. if (!h->head)
  455. h->last = 0;
  456. h->tot--;
  457. if (debug_output) {
  458. tosend[len - 1] = 0;
  459. switch (which) {
  460. case DP_MODE:
  461. putlog(LOG_SRVOUT, "*", "[m=>] %s", tosend);
  462. break;
  463. case DP_SERVER:
  464. putlog(LOG_SRVOUT, "*", "[s=>] %s", tosend);
  465. break;
  466. case DP_HELP:
  467. putlog(LOG_SRVOUT, "*", "[h=>] %s", tosend);
  468. break;
  469. }
  470. }
  471. last_time += calc_penalty(tosend);
  472. return 1;
  473. }
  474. return 0;
  475. }
  476. static void check_queues(char *oldnick, char *newnick)
  477. {
  478. if (optimize_kicks == 2) {
  479. if (modeq.head)
  480. parse_q(&modeq, oldnick, newnick);
  481. if (mq.head)
  482. parse_q(&mq, oldnick, newnick);
  483. if (hq.head)
  484. parse_q(&hq, oldnick, newnick);
  485. }
  486. }
  487. static void parse_q(struct msgq_head *q, char *oldnick, char *newnick)
  488. {
  489. struct msgq *m, *lm = NULL;
  490. char buf[511], *msg, *nicks, *nick, *chan, newnicks[511], newmsg[511];
  491. int changed;
  492. for (m = q->head; m;) {
  493. changed = 0;
  494. if (optimize_kicks == 2 && !egg_strncasecmp(m->msg, "KICK ", 5)) {
  495. newnicks[0] = 0;
  496. strncpyz(buf, m->msg, sizeof buf);
  497. if (buf[0] && (buf[strlen(buf)-1] == '\n'))
  498. buf[strlen(buf)-1] = '\0';
  499. msg = buf;
  500. newsplit(&msg);
  501. chan = newsplit(&msg);
  502. nicks = newsplit(&msg);
  503. while (strlen(nicks) > 0) {
  504. nick = splitnicks(&nicks);
  505. if (!egg_strcasecmp(nick, oldnick) &&
  506. ((9 + strlen(chan) + strlen(newnicks) + strlen(newnick) +
  507. strlen(nicks) + strlen(msg)) < 510)) {
  508. if (newnick)
  509. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, newnick);
  510. changed = 1;
  511. } else
  512. egg_snprintf(newnicks, sizeof newnicks, ",%s", nick);
  513. }
  514. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s\n", chan,
  515. newnicks + 1, msg);
  516. }
  517. if (changed) {
  518. if (newnicks[0] == 0) {
  519. if (!lm)
  520. q->head = m->next;
  521. else
  522. lm->next = m->next;
  523. nfree(m->msg);
  524. nfree(m);
  525. m = lm;
  526. q->tot--;
  527. if (!q->head)
  528. q->last = 0;
  529. } else {
  530. nfree(m->msg);
  531. m->msg = nmalloc(strlen(newmsg) + 1);
  532. m->len = strlen(newmsg);
  533. strcpy(m->msg, newmsg);
  534. }
  535. }
  536. lm = m;
  537. if (m)
  538. m = m->next;
  539. else
  540. m = q->head;
  541. }
  542. }
  543. static void purge_kicks(struct msgq_head *q)
  544. {
  545. struct msgq *m, *lm = NULL;
  546. char buf[511], *reason, *nicks, *nick, *chan, newnicks[511],
  547. newmsg[511], chans[511], *chns, *ch;
  548. int changed, found;
  549. struct chanset_t *cs;
  550. for (m = q->head; m;) {
  551. if (!egg_strncasecmp(m->msg, "KICK", 4)) {
  552. newnicks[0] = 0;
  553. changed = 0;
  554. strncpyz(buf, m->msg, sizeof buf);
  555. if (buf[0] && (buf[strlen(buf)-1] == '\n'))
  556. buf[strlen(buf)-1] = '\0';
  557. reason = buf;
  558. newsplit(&reason);
  559. chan = newsplit(&reason);
  560. nicks = newsplit(&reason);
  561. while (strlen(nicks) > 0) {
  562. found = 0;
  563. nick = splitnicks(&nicks);
  564. strncpyz(chans, chan, sizeof chans);
  565. chns = chans;
  566. while (strlen(chns) > 0) {
  567. ch = newsplit(&chns);
  568. cs = findchan(ch);
  569. if (!cs)
  570. continue;
  571. if (ismember(cs, nick))
  572. found = 1;
  573. }
  574. if (found)
  575. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, nick);
  576. else {
  577. putlog(LOG_SRVOUT, "*", "%s isn't on any target channel, removing "
  578. "kick...", nick);
  579. changed = 1;
  580. }
  581. }
  582. if (changed) {
  583. if (newnicks[0] == 0) {
  584. if (!lm)
  585. q->head = m->next;
  586. else
  587. lm->next = m->next;
  588. nfree(m->msg);
  589. nfree(m);
  590. m = lm;
  591. q->tot--;
  592. if (!q->head)
  593. q->last = 0;
  594. } else {
  595. nfree(m->msg);
  596. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s\n", chan,
  597. newnicks + 1, reason);
  598. m->msg = nmalloc(strlen(newmsg) + 1);
  599. m->len = strlen(newmsg);
  600. strcpy(m->msg, newmsg);
  601. }
  602. }
  603. }
  604. lm = m;
  605. if (m)
  606. m = m->next;
  607. else
  608. m = q->head;
  609. }
  610. }
  611. static int deq_kick(int which)
  612. {
  613. struct msgq_head *h;
  614. struct msgq *msg, *m, *lm;
  615. char buf[511], buf2[511], *reason2, *nicks, *chan, *chan2, *reason, *nick,
  616. newnicks[511], newnicks2[511], newmsg[511];
  617. int changed = 0, nr = 0;
  618. if (!optimize_kicks)
  619. return 0;
  620. newnicks[0] = 0;
  621. switch (which) {
  622. case DP_MODE:
  623. h = &modeq;
  624. break;
  625. case DP_SERVER:
  626. h = &mq;
  627. break;
  628. case DP_HELP:
  629. h = &hq;
  630. break;
  631. default:
  632. return 0;
  633. }
  634. if (egg_strncasecmp(h->head->msg, "KICK", 4))
  635. return 0;
  636. if (optimize_kicks == 2) {
  637. purge_kicks(h);
  638. if (!h->head)
  639. return 1;
  640. }
  641. if (egg_strncasecmp(h->head->msg, "KICK", 4))
  642. return 0;
  643. msg = h->head;
  644. strncpyz(buf, msg->msg, sizeof buf);
  645. reason = buf;
  646. newsplit(&reason);
  647. chan = newsplit(&reason);
  648. nicks = newsplit(&reason);
  649. while (strlen(nicks) > 0) {
  650. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks,
  651. newsplit(&nicks));
  652. nr++;
  653. }
  654. for (m = msg->next, lm = NULL; m && (nr < kick_method);) {
  655. if (!egg_strncasecmp(m->msg, "KICK", 4)) {
  656. changed = 0;
  657. newnicks2[0] = 0;
  658. strncpyz(buf2, m->msg, sizeof buf2);
  659. if (buf2[0] && (buf2[strlen(buf2)-1] == '\n'))
  660. buf2[strlen(buf2)-1] = '\0';
  661. reason2 = buf2;
  662. newsplit(&reason2);
  663. chan2 = newsplit(&reason2);
  664. nicks = newsplit(&reason2);
  665. if (!egg_strcasecmp(chan, chan2) && !egg_strcasecmp(reason, reason2)) {
  666. while (strlen(nicks) > 0) {
  667. nick = splitnicks(&nicks);
  668. if ((nr < kick_method) &&
  669. ((9 + strlen(chan) + strlen(newnicks) + strlen(nick) +
  670. strlen(reason)) < 510)) {
  671. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, nick);
  672. nr++;
  673. changed = 1;
  674. } else
  675. egg_snprintf(newnicks2, sizeof newnicks2, "%s,%s", newnicks2, nick);
  676. }
  677. }
  678. if (changed) {
  679. if (newnicks2[0] == 0) {
  680. if (!lm)
  681. h->head->next = m->next;
  682. else
  683. lm->next = m->next;
  684. nfree(m->msg);
  685. nfree(m);
  686. m = lm;
  687. h->tot--;
  688. if (!h->head)
  689. h->last = 0;
  690. } else {
  691. nfree(m->msg);
  692. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s\n", chan2,
  693. newnicks2 + 1, reason);
  694. m->msg = nmalloc(strlen(newmsg) + 1);
  695. m->len = strlen(newmsg);
  696. strcpy(m->msg, newmsg);
  697. }
  698. }
  699. }
  700. lm = m;
  701. if (m)
  702. m = m->next;
  703. else
  704. m = h->head->next;
  705. }
  706. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s\n", chan, newnicks + 1,
  707. reason);
  708. tputs(serv, newmsg, strlen(newmsg));
  709. if (debug_output) {
  710. newmsg[strlen(newmsg) - 1] = 0;
  711. switch (which) {
  712. case DP_MODE:
  713. putlog(LOG_SRVOUT, "@", "[m->] %s", newmsg);
  714. break;
  715. case DP_SERVER:
  716. putlog(LOG_SRVOUT, "@", "[s->] %s", newmsg);
  717. break;
  718. case DP_HELP:
  719. putlog(LOG_SRVOUT, "@", "[h->] %s", newmsg);
  720. break;
  721. }
  722. debug3("Changed: %d, kick-method: %d, nr: %d", changed, kick_method, nr);
  723. }
  724. h->tot--;
  725. last_time += calc_penalty(newmsg);
  726. m = h->head->next;
  727. nfree(h->head->msg);
  728. nfree(h->head);
  729. h->head = m;
  730. if (!h->head)
  731. h->last = 0;
  732. return 1;
  733. }
  734. /* Clean out the msg queues (like when changing servers).
  735. */
  736. static void empty_msgq()
  737. {
  738. msgq_clear(&modeq);
  739. msgq_clear(&mq);
  740. msgq_clear(&hq);
  741. burst = 0;
  742. }
  743. /* Use when sending msgs... will spread them out so there's no flooding.
  744. */
  745. static void queue_server(int which, char *buf, int len)
  746. {
  747. struct msgq_head *h = NULL,
  748. tempq;
  749. struct msgq *q, *tq, *tqq;
  750. int doublemsg = 0,
  751. qnext = 0;
  752. /* Don't even BOTHER if there's no server online. */
  753. if (serv < 0)
  754. return;
  755. /* No queue for PING and PONG - drummer */
  756. if (!egg_strncasecmp(buf, "PING", 4) || !egg_strncasecmp(buf, "PONG", 4)) {
  757. if (buf[1] == 'I' || buf[1] == 'i')
  758. lastpingtime = now; /* lagmeter */
  759. tputs(serv, buf, len);
  760. if (debug_output) {
  761. if (buf[len - 1] == '\n')
  762. buf[len - 1] = 0;
  763. putlog(LOG_SRVOUT, "@", "[m->] %s", buf);
  764. }
  765. return;
  766. }
  767. switch (which) {
  768. case DP_MODE_NEXT:
  769. qnext = 1;
  770. /* Fallthrough */
  771. case DP_MODE:
  772. h = &modeq;
  773. tempq = modeq;
  774. if (double_mode)
  775. doublemsg = 1;
  776. break;
  777. case DP_SERVER_NEXT:
  778. qnext = 1;
  779. /* Fallthrough */
  780. case DP_SERVER:
  781. h = &mq;
  782. tempq = mq;
  783. if (double_server)
  784. doublemsg = 1;
  785. break;
  786. case DP_HELP_NEXT:
  787. qnext = 1;
  788. /* Fallthrough */
  789. case DP_HELP:
  790. h = &hq;
  791. tempq = hq;
  792. if (double_help)
  793. doublemsg = 1;
  794. break;
  795. default:
  796. putlog(LOG_MISC, "*", "!!! queuing unknown type to server!!");
  797. return;
  798. }
  799. if (h->tot < maxqmsg) {
  800. /* Don't queue msg if it's already queued? */
  801. if (!doublemsg)
  802. for (tq = tempq.head; tq; tq = tqq) {
  803. tqq = tq->next;
  804. if (!egg_strcasecmp(tq->msg, buf)) {
  805. if (!double_warned) {
  806. if (buf[len - 1] == '\n')
  807. buf[len - 1] = 0;
  808. debug1("msg already queued. skipping: %s", buf);
  809. double_warned = 1;
  810. }
  811. return;
  812. }
  813. }
  814. q = nmalloc(sizeof(struct msgq));
  815. if (qnext)
  816. q->next = h->head;
  817. else
  818. q->next = NULL;
  819. if (h->head) {
  820. if (!qnext)
  821. h->last->next = q;
  822. } else
  823. h->head = q;
  824. if (qnext)
  825. h->head = q;
  826. h->last = q;
  827. q->len = len;
  828. q->msg = nmalloc(len + 1);
  829. strncpyz(q->msg, buf, len + 1);
  830. h->tot++;
  831. h->warned = 0;
  832. double_warned = 0;
  833. } else {
  834. if (!h->warned) {
  835. switch (which) {
  836. case DP_MODE_NEXT:
  837. /* Fallthrough */
  838. case DP_MODE:
  839. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM MODE QUEUE");
  840. break;
  841. case DP_SERVER_NEXT:
  842. /* Fallthrough */
  843. case DP_SERVER:
  844. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM SERVER QUEUE");
  845. break;
  846. case DP_HELP_NEXT:
  847. /* Fallthrough */
  848. case DP_HELP:
  849. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM HELP QUEUE");
  850. break;
  851. }
  852. }
  853. h->warned = 1;
  854. }
  855. if (debug_output && !h->warned) {
  856. if (buf[len - 1] == '\n')
  857. buf[len - 1] = 0;
  858. switch (which) {
  859. case DP_MODE:
  860. putlog(LOG_SRVOUT, "@", "[!m] %s", buf);
  861. break;
  862. case DP_SERVER:
  863. putlog(LOG_SRVOUT, "@", "[!s] %s", buf);
  864. break;
  865. case DP_HELP:
  866. putlog(LOG_SRVOUT, "@", "[!h] %s", buf);
  867. break;
  868. case DP_MODE_NEXT:
  869. putlog(LOG_SRVOUT, "@", "[!!m] %s", buf);
  870. break;
  871. case DP_SERVER_NEXT:
  872. putlog(LOG_SRVOUT, "@", "[!!s] %s", buf);
  873. break;
  874. case DP_HELP_NEXT:
  875. putlog(LOG_SRVOUT, "@", "[!!h] %s", buf);
  876. break;
  877. }
  878. }
  879. if (which == DP_MODE || which == DP_MODE_NEXT)
  880. deq_msg(); /* DP_MODE needs to be sent ASAP, flush if
  881. possible. */
  882. }
  883. /* Add a new server to the server_list.
  884. */
  885. static void add_server(char *ss)
  886. {
  887. struct server_list *x, *z;
  888. #ifdef USE_IPV6
  889. char *p, *q, *r;
  890. #else
  891. char *p, *q;
  892. #endif /* USE_IPV6 */
  893. for (z = serverlist; z && z->next; z = z->next);
  894. while (ss) {
  895. p = strchr(ss, ',');
  896. if (p)
  897. *p++ = 0;
  898. x = nmalloc(sizeof(struct server_list));
  899. x->next = 0;
  900. x->realname = 0;
  901. x->port = 0;
  902. if (z)
  903. z->next = x;
  904. else
  905. serverlist = x;
  906. z = x;
  907. q = strchr(ss, ':');
  908. if (!q) {
  909. x->port = default_port;
  910. x->pass = 0;
  911. x->name = nmalloc(strlen(ss) + 1);
  912. strcpy(x->name, ss);
  913. } else {
  914. #ifdef USE_IPV6
  915. if (ss[0] == '[') {
  916. *ss++;
  917. q = strchr(ss, ']');
  918. *q++ = 0; /* intentional */
  919. r = strchr(q, ':');
  920. if (!r)
  921. x->port = default_port;
  922. }
  923. #endif /* USE_IPV6 */
  924. *q++ = 0;
  925. x->name = nmalloc(q - ss);
  926. strcpy(x->name, ss);
  927. ss = q;
  928. q = strchr(ss, ':');
  929. if (!q) {
  930. x->pass = 0;
  931. } else {
  932. *q++ = 0;
  933. x->pass = nmalloc(strlen(q) + 1);
  934. strcpy(x->pass, q);
  935. }
  936. #ifdef USE_IPV6
  937. if (!x->port) {
  938. x->port = atoi(ss);
  939. }
  940. #else
  941. x->port = atoi(ss);
  942. #endif /* USE_IPV6 */
  943. }
  944. ss = p;
  945. }
  946. }
  947. /* Clear out the given server_list.
  948. */
  949. static void clearq(struct server_list *xx)
  950. {
  951. struct server_list *x;
  952. while (xx) {
  953. x = xx->next;
  954. if (xx->name)
  955. nfree(xx->name);
  956. if (xx->pass)
  957. nfree(xx->pass);
  958. if (xx->realname)
  959. nfree(xx->realname);
  960. nfree(xx);
  961. xx = x;
  962. }
  963. }
  964. void servers_describe(struct cfg_entry * entry, int idx) {
  965. }
  966. void servers6_describe(struct cfg_entry * entry, int idx) {
  967. }
  968. int count(const char *s)
  969. {
  970. char *ele;
  971. int i = 0;
  972. char count[1000];
  973. strcpy(count,s);
  974. ele=strtok(count,",");
  975. while(ele && *ele)
  976. {
  977. i++;
  978. ele=strtok((char*)NULL,",");
  979. }
  980. return i-1;
  981. }
  982. int rrand(int a, int b)
  983. {
  984. b++;
  985. return ((random()%(b-a))+a);
  986. }
  987. char *randomize(char *line, char **new)
  988. {
  989. char *str, *words[1000], *bak;
  990. int i,o,b,r,u = 0;
  991. bak = nmalloc(strlen(line)+1);
  992. strcpy(bak, line);
  993. i=count(line);
  994. b=i+1;
  995. str=strtok(line,",");
  996. o=0;
  997. while(str && *str)
  998. {
  999. words[o] = str;
  1000. o++;
  1001. str=strtok((char*)NULL,",");
  1002. }
  1003. while (b) {
  1004. if (u > 200) { //some problem, bail out
  1005. sprintf(*new, "%s", bak);
  1006. nfree(bak);
  1007. return *new;
  1008. }
  1009. r = rrand(0,i);
  1010. if (strstr(*new,words[r]) == NULL) {
  1011. if (b == i+1) sprintf(*new,"%s",words[r]);
  1012. else sprintf(*new,"%s,%s", *new, words[r]);
  1013. b--;
  1014. } else
  1015. u++;
  1016. }
  1017. nfree(bak);
  1018. return *new;
  1019. }
  1020. void servers_changed(struct cfg_entry * entry, char * olddata, int * valid) {
  1021. #ifdef LEAF
  1022. char *slist, *p;
  1023. #ifdef S_RANDSERVERS
  1024. char *new;
  1025. if (hostname6[0] || myip6[0]) //we want to use the servers6 entry.
  1026. return;
  1027. #endif
  1028. slist = (char *) (entry->ldata ? entry->ldata : (entry->gdata ? entry->gdata : ""));
  1029. if (serverlist) {
  1030. clearq(serverlist);
  1031. serverlist = NULL;
  1032. }
  1033. p=nmalloc(strlen(slist)+1);
  1034. strcpy(p, slist);
  1035. #ifdef S_RANDSERVERS
  1036. new = nmalloc(strlen(slist)+1);
  1037. randomize(p,&new);
  1038. strcpy(p, new);
  1039. #endif
  1040. add_server(p);
  1041. nfree(p);
  1042. #ifdef S_RANDSERVERS
  1043. nfree(new);
  1044. #endif
  1045. #endif
  1046. }
  1047. void servers6_changed(struct cfg_entry * entry, char * olddata, int * valid) {
  1048. #ifdef LEAF
  1049. char *slist, *p;
  1050. #ifdef S_RANDSERVERS
  1051. char *new;
  1052. if (!hostname6[0] && !myip6[0]) //we probably want to use the normal server list..
  1053. return;
  1054. #endif
  1055. slist = (char *) (entry->ldata ? entry->ldata : (entry->gdata ? entry->gdata : ""));
  1056. if (serverlist) {
  1057. clearq(serverlist);
  1058. serverlist = NULL;
  1059. }
  1060. p=nmalloc(strlen(slist)+1);
  1061. strcpy(p, slist);
  1062. #ifdef S_RANDSERVERS
  1063. new = nmalloc(strlen(slist)+1);
  1064. randomize(p,&new);
  1065. strcpy(p, new);
  1066. #endif
  1067. add_server(p);
  1068. nfree(p);
  1069. #ifdef S_RANDSERVERS
  1070. nfree(new);
  1071. #endif
  1072. #endif
  1073. }
  1074. void nick_describe(struct cfg_entry * entry, int idx) {
  1075. }
  1076. void nick_changed(struct cfg_entry * entry, char * olddata, int * valid) {
  1077. #ifdef LEAF
  1078. char * p;
  1079. if (entry->ldata)
  1080. p=entry->ldata;
  1081. else if (entry->gdata)
  1082. p=entry->gdata;
  1083. else
  1084. p=NULL;
  1085. if (p && p[0]) {
  1086. strncpy0(origbotname, p, NICKMAX+1);
  1087. // dprintf(DP_MODE, STR("NICK %s\n"), p);
  1088. } else {
  1089. strncpy0(origbotname, botnetnick, NICKMAX+1);
  1090. }
  1091. #endif
  1092. }
  1093. void realname_describe(struct cfg_entry * entry, int idx) {
  1094. }
  1095. void realname_changed(struct cfg_entry * entry, char * olddata, int * valid) {
  1096. #ifdef LEAF
  1097. if (entry->ldata) {
  1098. strncpy0(botrealname, (char *) entry->ldata, 120);
  1099. } else if (entry->gdata) {
  1100. strncpy0(botrealname, (char *) entry->gdata, 120);
  1101. }
  1102. #endif
  1103. }
  1104. struct cfg_entry CFG_SERVERS = {
  1105. "servers", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  1106. servers_changed, servers_changed, servers_describe
  1107. };
  1108. struct cfg_entry CFG_SERVERS6 = {
  1109. "servers6", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  1110. servers6_changed, servers6_changed, servers6_describe
  1111. };
  1112. struct cfg_entry CFG_NICK = {
  1113. "nick", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  1114. nick_changed, nick_changed, nick_describe
  1115. };
  1116. struct cfg_entry CFG_REALNAME = {
  1117. "realname", CFGF_LOCAL | CFGF_GLOBAL, NULL, NULL,
  1118. realname_changed, realname_changed, realname_describe
  1119. };
  1120. /* Set botserver to the next available server.
  1121. *
  1122. * -> if (*ptr == -1) then jump to that particular server
  1123. */
  1124. static void next_server(int *ptr, char *serv, unsigned int *port, char *pass)
  1125. {
  1126. struct server_list *x = serverlist;
  1127. int i = 0;
  1128. if (x == NULL)
  1129. return;
  1130. /* -1 --> Go to specified server */
  1131. if (*ptr == (-1)) {
  1132. for (; x; x = x->next) {
  1133. if (x->port == *port) {
  1134. if (!egg_strcasecmp(x->name, serv)) {
  1135. *ptr = i;
  1136. return;
  1137. } else if (x->realname && !egg_strcasecmp(x->realname, serv)) {
  1138. *ptr = i;
  1139. strncpyz(serv, x->realname, sizeof serv);
  1140. return;
  1141. }
  1142. }
  1143. i++;
  1144. }
  1145. /* Gotta add it: */
  1146. x = nmalloc(sizeof(struct server_list));
  1147. x->next = 0;
  1148. x->realname = 0;
  1149. x->name = nmalloc(strlen(serv) + 1);
  1150. strcpy(x->name, serv);
  1151. x->port = *port ? *port : default_port;
  1152. if (pass && pass[0]) {
  1153. x->pass = nmalloc(strlen(pass) + 1);
  1154. strcpy(x->pass, pass);
  1155. } else
  1156. x->pass = NULL;
  1157. list_append((struct list_type **) (&serverlist), (struct list_type *) x);
  1158. *ptr = i;
  1159. return;
  1160. }
  1161. /* Find where i am and boogie */
  1162. i = (*ptr);
  1163. while (i > 0 && x != NULL) {
  1164. x = x->next;
  1165. i--;
  1166. }
  1167. if (x != NULL) {
  1168. x = x->next;
  1169. (*ptr)++;
  1170. } /* Go to next server */
  1171. if (x == NULL) {
  1172. x = serverlist;
  1173. *ptr = 0;
  1174. } /* Start over at the beginning */
  1175. strcpy(serv, x->name);
  1176. *port = x->port ? x->port : default_port;
  1177. if (x->pass)
  1178. strcpy(pass, x->pass);
  1179. else
  1180. pass[0] = 0;
  1181. }
  1182. static int server_6char STDVAR
  1183. {
  1184. Function F = (Function) cd;
  1185. char x[20];
  1186. BADARGS(7, 7, " nick user@host handle dest/chan keyword/nick text");
  1187. CHECKVALIDITY(server_6char);
  1188. egg_snprintf(x, sizeof x, "%d",
  1189. F(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]));
  1190. Tcl_AppendResult(irp, x, NULL);
  1191. return TCL_OK;
  1192. }
  1193. static int server_5char STDVAR
  1194. {
  1195. Function F = (Function) cd;
  1196. BADARGS(6, 6, " nick user@host handle dest/channel text");
  1197. CHECKVALIDITY(server_5char);
  1198. F(argv[1], argv[2], argv[3], argv[4], argv[5]);
  1199. return TCL_OK;
  1200. }
  1201. static int server_2char STDVAR
  1202. {
  1203. Function F = (Function) cd;
  1204. BADARGS(3, 3, " nick msg");
  1205. CHECKVALIDITY(server_2char);
  1206. F(argv[1], argv[2]);
  1207. return TCL_OK;
  1208. }
  1209. static int server_msg STDVAR
  1210. {
  1211. Function F = (Function) cd;
  1212. BADARGS(5, 5, " nick uhost hand buffer");
  1213. CHECKVALIDITY(server_msg);
  1214. F(argv[1], argv[2], get_user_by_handle(userlist, argv[3]), argv[4]);
  1215. return TCL_OK;
  1216. }
  1217. #ifdef S_AUTH
  1218. static int server_msgc STDVAR
  1219. {
  1220. Function F = (Function) cd;
  1221. BADARGS(6, 6, " nick uhost hand buffer chan");
  1222. CHECKVALIDITY(server_msgc);
  1223. F(argv[1], argv[2], get_user_by_handle(userlist, argv[3]), argv[4], argv[5]);
  1224. return TCL_OK;
  1225. }
  1226. #endif /* S_AUTH */
  1227. static int server_raw STDVAR
  1228. {
  1229. Function F = (Function) cd;
  1230. BADARGS(4, 4, " from code args");
  1231. CHECKVALIDITY(server_raw);
  1232. Tcl_AppendResult(irp, int_to_base10(F(argv[1], argv[3])), NULL);
  1233. return TCL_OK;
  1234. }
  1235. /* Read/write normal string variable.
  1236. */
  1237. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1238. static char *nick_change(ClientData cdata, Tcl_Interp *irp, CONST char *name1,
  1239. CONST char *name2, int flags)
  1240. #else
  1241. static char *nick_change(ClientData cdata, Tcl_Interp *irp, char *name1,
  1242. char *name2, int flags)
  1243. #endif
  1244. {
  1245. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1246. CONST char *new;
  1247. #else
  1248. char *new;
  1249. #endif
  1250. if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
  1251. Tcl_SetVar2(interp, name1, name2, origbotname, TCL_GLOBAL_ONLY);
  1252. if (flags & TCL_TRACE_UNSETS)
  1253. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1254. TCL_TRACE_UNSETS, nick_change, cdata);
  1255. } else { /* writes */
  1256. new = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1257. if (rfc_casecmp(origbotname, (char *)new)) {
  1258. if (origbotname[0]) {
  1259. putlog(LOG_MISC, "*", "* IRC NICK CHANGE: %s -> %s",
  1260. origbotname, new);
  1261. nick_juped = 0;
  1262. }
  1263. strncpyz(origbotname, new, NICKLEN);
  1264. if (server_online)
  1265. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  1266. }
  1267. }
  1268. return NULL;
  1269. }
  1270. /* Replace all '?'s in s with a random number.
  1271. */
  1272. static void rand_nick(char *nick)
  1273. {
  1274. register char *p = nick;
  1275. while ((p = strchr(p, '?')) != NULL) {
  1276. *p = '0' + random() % 10;
  1277. p++;
  1278. }
  1279. }
  1280. /* Return the alternative bot nick.
  1281. */
  1282. static char *get_altbotnick(void)
  1283. {
  1284. /* A random-number nick? */
  1285. if (strchr(altnick, '?')) {
  1286. if (!raltnick[0]) {
  1287. strncpyz(raltnick, altnick, NICKLEN);
  1288. rand_nick(raltnick);
  1289. }
  1290. return raltnick;
  1291. } else
  1292. return altnick;
  1293. }
  1294. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1295. static char *altnick_change(ClientData cdata, Tcl_Interp *irp,
  1296. CONST char *name1, CONST char *name2, int flags)
  1297. #else
  1298. static char *altnick_change(ClientData cdata, Tcl_Interp *irp, char *name1,
  1299. char *name2, int flags)
  1300. #endif
  1301. {
  1302. /* Always unset raltnick. Will be regenerated when needed. */
  1303. raltnick[0] = 0;
  1304. return NULL;
  1305. }
  1306. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1307. static char *traced_server(ClientData cdata, Tcl_Interp *irp,
  1308. CONST char *name1, CONST char *name2, int flags)
  1309. #else
  1310. static char *traced_server(ClientData cdata, Tcl_Interp *irp, char *name1,
  1311. char *name2, int flags)
  1312. #endif
  1313. {
  1314. char s[1024];
  1315. if (server_online) {
  1316. int servidx = findanyidx(serv);
  1317. simple_sprintf(s, "%s:%u", dcc[servidx].host, dcc[servidx].port);
  1318. } else
  1319. s[0] = 0;
  1320. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1321. if (flags & TCL_TRACE_UNSETS)
  1322. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1323. TCL_TRACE_UNSETS, traced_server, cdata);
  1324. return NULL;
  1325. }
  1326. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1327. static char *traced_botname(ClientData cdata, Tcl_Interp *irp,
  1328. CONST char *name1, CONST char *name2, int flags)
  1329. #else
  1330. static char *traced_botname(ClientData cdata, Tcl_Interp *irp, char *name1,
  1331. char *name2, int flags)
  1332. #endif
  1333. {
  1334. char s[1024];
  1335. simple_sprintf(s, "%s%s%s", botname,
  1336. botuserhost[0] ? "!" : "", botuserhost[0] ? botuserhost : "");
  1337. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1338. if (flags & TCL_TRACE_UNSETS)
  1339. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1340. TCL_TRACE_UNSETS, traced_botname, cdata);
  1341. return NULL;
  1342. }
  1343. static void do_nettype(void)
  1344. {
  1345. switch (net_type) {
  1346. case NETT_EFNET:
  1347. check_mode_r = 0;
  1348. nick_len = 9;
  1349. break;
  1350. case NETT_IRCNET:
  1351. check_mode_r = 1;
  1352. use_penalties = 1;
  1353. use_fastdeq = 3;
  1354. nick_len = 9;
  1355. simple_sprintf(stackablecmds, "INVITE AWAY VERSION NICK");
  1356. kick_method = 4;
  1357. break;
  1358. case NETT_UNDERNET:
  1359. check_mode_r = 0;
  1360. use_fastdeq = 2;
  1361. nick_len = 9;
  1362. simple_sprintf(stackablecmds, "PRIVMSG NOTICE TOPIC PART WHOIS USERHOST USERIP ISON");
  1363. simple_sprintf(stackable2cmds, "USERHOST USERIP ISON");
  1364. break;
  1365. case NETT_DALNET:
  1366. check_mode_r = 0;
  1367. use_fastdeq = 2;
  1368. nick_len = 32;
  1369. simple_sprintf(stackablecmds, "PRIVMSG NOTICE PART WHOIS WHOWAS USERHOST ISON WATCH DCCALLOW");
  1370. simple_sprintf(stackable2cmds, "USERHOST ISON WATCH");
  1371. break;
  1372. case NETT_HYBRID_EFNET:
  1373. check_mode_r = 0;
  1374. nick_len = 9;
  1375. break;
  1376. }
  1377. }
  1378. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1379. static char *traced_nettype(ClientData cdata, Tcl_Interp *irp,
  1380. CONST char *name1, CONST char *name2, int flags)
  1381. #else
  1382. static char *traced_nettype(ClientData cdata, Tcl_Interp *irp, char *name1,
  1383. char *name2, int flags)
  1384. #endif
  1385. {
  1386. do_nettype();
  1387. return NULL;
  1388. }
  1389. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1390. static char *traced_nicklen(ClientData cdata, Tcl_Interp *irp,
  1391. CONST char *name1, CONST char *name2, int flags)
  1392. #else
  1393. static char *traced_nicklen(ClientData cdata, Tcl_Interp *irp, char *name1,
  1394. char *name2, int flags)
  1395. #endif
  1396. {
  1397. if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
  1398. char s[40];
  1399. egg_snprintf(s, sizeof s, "%d", nick_len);
  1400. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1401. if (flags & TCL_TRACE_UNSETS)
  1402. Tcl_TraceVar(irp, name1, TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1403. traced_nicklen, cdata);
  1404. } else {
  1405. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1406. CONST char *cval = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1407. #else
  1408. char *cval = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1409. #endif
  1410. long lval = 0;
  1411. Context;
  1412. if (cval && Tcl_ExprLong(interp, cval, &lval) != TCL_ERROR) {
  1413. if (lval > NICKMAX)
  1414. lval = NICKMAX;
  1415. nick_len = (int) lval;
  1416. }
  1417. }
  1418. return NULL;
  1419. }
  1420. static tcl_strings my_tcl_strings[] =
  1421. {
  1422. {"botnick", NULL, 0, STR_PROTECT},
  1423. {"altnick", altnick, NICKMAX, 0},
  1424. {"realname", botrealname, 80, 0},
  1425. {"init-server", initserver, 120, 0},
  1426. {"connect-server", connectserver, 120, 0},
  1427. {"stackable-commands", stackablecmds, 510, 0},
  1428. {"stackable2-commands", stackable2cmds, 510, 0},
  1429. {NULL, NULL, 0, 0}
  1430. };
  1431. static tcl_coups my_tcl_coups[] =
  1432. {
  1433. {"flood-msg", &flud_thr, &flud_time},
  1434. {"flood-ctcp", &flud_ctcp_thr, &flud_ctcp_time},
  1435. {NULL, NULL, NULL}
  1436. };
  1437. static tcl_ints my_tcl_ints[] =
  1438. {
  1439. {"use-console-r", NULL, 1},
  1440. {"servlimit", &min_servs, 0},
  1441. {"server-timeout", &server_timeout, 0},
  1442. {"lowercase-ctcp", &lowercase_ctcp, 0},
  1443. {"server-online", (int *) &server_online, 2},
  1444. {"never-give-up", &never_give_up, 0},
  1445. {"keep-nick", &keepnick, 0},
  1446. {"strict-servernames", &strict_servernames, 0},
  1447. {"check-stoned", &check_stoned, 0},
  1448. {"serverror-quit", &serverror_quit, 0},
  1449. {"quiet-reject", &quiet_reject, 0},
  1450. {"max-queue-msg", &maxqmsg, 0},
  1451. {"trigger-on-ignore", &trigger_on_ignore, 0},
  1452. {"answer-ctcp", &answer_ctcp, 0},
  1453. {"server-cycle-wait", (int *) &server_cycle_wait, 0},
  1454. {"default-port", &default_port, 0},
  1455. {"check-mode-r", &check_mode_r, 0},
  1456. {"net-type", &net_type, 0},
  1457. {"ctcp-mode", &ctcp_mode, 0},
  1458. {"double-mode", &double_mode, 0},/* G`Quann */
  1459. {"double-server", &double_server, 0},
  1460. {"double-help", &double_help, 0},
  1461. {"use-penalties", &use_penalties, 0},
  1462. {"use-fastdeq", &use_fastdeq, 0},
  1463. {"nicklen", &nick_len, 0},
  1464. {"nick-len", &nick_len, 0},
  1465. {"optimize-kicks", &optimize_kicks, 0},
  1466. {"isjuped", &nick_juped, 0},
  1467. {"stack-limit", &stack_limit, 0},
  1468. {NULL, NULL, 0}
  1469. };
  1470. /*
  1471. * CTCP DCC CHAT functions
  1472. */
  1473. static void dcc_chat_hostresolved(int);
  1474. /* This only handles CHAT requests, otherwise it's handled in filesys.
  1475. */
  1476. static int ctcp_DCC_CHAT(char *nick, char *from, char *handle,
  1477. char *object, char *keyword, char *text)
  1478. {
  1479. char *action, *param, *ip, *prt, buf[512], *msg = buf;
  1480. int i, ok;
  1481. struct userrec *u = get_user_by_handle(userlist, handle);
  1482. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  1483. if (!ischanhub())
  1484. return 0;
  1485. strcpy(msg, text);
  1486. action = newsplit(&msg);
  1487. param = newsplit(&msg);
  1488. ip = newsplit(&msg);
  1489. prt = newsplit(&msg);
  1490. if (egg_strcasecmp(action, "CHAT") || egg_strcasecmp(object, botname) || !u)
  1491. return 0;
  1492. get_user_flagrec(u, &fr, 0);
  1493. ok = 1;
  1494. if (ischanhub() && !glob_chuba(fr))
  1495. ok = 0;
  1496. if (dcc_total == max_dcc) {
  1497. if (!quiet_reject)
  1498. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_TOOMANYDCCS1);
  1499. putlog(LOG_MISC, "*", DCC_TOOMANYDCCS2, "CHAT", param, nick, from);
  1500. } else if (!ok) {
  1501. if (!quiet_reject)
  1502. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED2);
  1503. putlog(LOG_MISC, "*", "%s: %s!%s", ischanhub() ? DCC_REFUSED : DCC_REFUSEDNC, nick, from);
  1504. } else if (u_pass_match(u, "-")) {
  1505. if (!quiet_reject)
  1506. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED3);
  1507. putlog(LOG_MISC, "*", "%s: %s!%s", DCC_REFUSED4, nick, from);
  1508. } else if (atoi(prt) < 1024 || atoi(prt) > 65535) {
  1509. /* Invalid port */
  1510. if (!quiet_reject)
  1511. dprintf(DP_HELP, "NOTICE %s :%s (invalid port)\n", nick,
  1512. DCC_CONNECTFAILED1);
  1513. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED3,
  1514. nick, from);
  1515. } else {
  1516. if (!sanitycheck_dcc(nick, from, ip, prt))
  1517. return 1;
  1518. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1519. if (i < 0) {
  1520. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", dcc[i].nick, ip);
  1521. return 1;
  1522. }
  1523. #ifdef USE_IPV6
  1524. if (hostprotocol(ip) == AF_INET6 && sockprotocol(dcc[i].sock) == AF_INET6) {
  1525. debug1("ipv6 addr: %s",ip);
  1526. strcpy(dcc[i].addr6,ip);
  1527. debug1("ipv6 addr: %s",dcc[i].addr6);
  1528. } else
  1529. #endif /* USE_IPV6 */
  1530. dcc[i].addr = my_atoul(ip);
  1531. dcc[i].port = atoi(prt);
  1532. dcc[i].sock = -1;
  1533. strcpy(dcc[i].nick, u->handle);
  1534. strcpy(dcc[i].host, from);
  1535. dcc[i].timeval = now;
  1536. dcc[i].user = u;
  1537. #ifdef USE_IPV6
  1538. if (sockprotocol(dcc[i].sock) != AF_INET6) {
  1539. #endif /* USE_IPV6 */
  1540. /* remove me? */
  1541. dcc[i].addr = my_atoul(ip);
  1542. dcc[i].u.dns->ip = dcc[i].addr;
  1543. dcc[i].u.dns->dns_type = RES_HOSTBYIP;
  1544. dcc[i].u.dns->dns_success = dcc_chat_hostresolved;
  1545. dcc[i].u.dns->dns_failure = dcc_chat_hostresolved;
  1546. dcc[i].u.dns->type = &DCC_CHAT_PASS;
  1547. dcc_dnshostbyip(dcc[i].addr);
  1548. #ifdef USE_IPV6
  1549. } else
  1550. dcc_chat_hostresolved(i); /* Don't try to look it up */
  1551. #endif /* USE_IPV6 */
  1552. }
  1553. return 1;
  1554. }
  1555. static void dcc_chat_hostresolved(int i)
  1556. {
  1557. char buf[512], ip[512];
  1558. int ok;
  1559. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  1560. egg_snprintf(buf, sizeof buf, "%d", dcc[i].port);
  1561. #ifndef USE_IPV6
  1562. if (!hostsanitycheck_dcc(dcc[i].nick, dcc[i].host, dcc[i].addr,
  1563. dcc[i].u.dns->host, buf)) {
  1564. lostdcc(i);
  1565. return;
  1566. }
  1567. #else
  1568. if (sockprotocol(dcc[i].sock) == AF_INET6) {
  1569. strcpy(ip,dcc[i].addr6); /* safe, addr6 is 121 */
  1570. debug0("afinet6, af_type, strcpy");
  1571. } else
  1572. #endif /* !USE_IPV6 */
  1573. egg_snprintf(ip, sizeof ip, "%lu", iptolong(htonl(dcc[i].addr)));
  1574. #ifdef USE_IPV6
  1575. if (sockprotocol(dcc[i].sock) == AF_INET6) {
  1576. # ifdef IPV6_DEBUG
  1577. debug2("af_inet6 %s / %s", dcc[i].addr6, ip);
  1578. # endif /* IPV6_DEBUG */
  1579. dcc[i].sock = getsock(0, AF_INET6);
  1580. } else {
  1581. # ifdef IPV6_DEBUG
  1582. debug0("af_inet");
  1583. # endif /* IPV6_DEBUG */
  1584. dcc[i].sock = getsock(0, AF_INET);
  1585. }
  1586. #else
  1587. dcc[i].sock = getsock(0);
  1588. # ifdef IPV6_DEBUG
  1589. debug2("sock: %d %s", dcc[i].sock, ip);
  1590. # endif /* IPV6_DEBUG */
  1591. #endif /* USE_IPV6 */
  1592. if (dcc[i].sock < 0 || open_telnet_dcc(dcc[i].sock, ip, buf) < 0) {
  1593. neterror(buf);
  1594. if (!quiet_reject)
  1595. dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[i].nick,
  1596. DCC_CONNECTFAILED1, buf);
  1597. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED2,
  1598. dcc[i].nick, dcc[i].host);
  1599. putlog(LOG_MISC, "*", " (%s)", buf);
  1600. killsock(dcc[i].sock);
  1601. lostdcc(i);
  1602. } else {
  1603. #ifdef HAVE_SSL
  1604. ssl_link(dcc[i].sock, CONNECT_SSL);
  1605. #endif /* HAVE_SSL */
  1606. changeover_dcc(i, &DCC_CHAT_PASS, sizeof(struct chat_info));
  1607. dcc[i].status = STAT_ECHO;
  1608. get_user_flagrec(dcc[i].user, &fr, 0);
  1609. ok = 1;
  1610. if (ischanhub() && !glob_chuba(fr))
  1611. ok = 0;
  1612. if (ok)
  1613. dcc[i].status |= STAT_PARTY;
  1614. strcpy(dcc[i].u.chat->con_chan, (chanset) ? chanset->dname : "*");
  1615. dcc[i].timeval = now;
  1616. /* Ok, we're satisfied with them now: attempt the connect */
  1617. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", dcc[i].nick,
  1618. dcc[i].host);
  1619. dprintf(i, "%s", rand_dccresp());
  1620. }
  1621. return;
  1622. }
  1623. /*
  1624. * Server timer functions
  1625. */
  1626. static void server_secondly()
  1627. {
  1628. if (cycle_time)
  1629. cycle_time--;
  1630. deq_msg();
  1631. if (!resolvserv && serv < 0)
  1632. connect_server();
  1633. }
  1634. static void server_5minutely()
  1635. {
  1636. if (check_stoned && server_online) {
  1637. if (waiting_for_awake) {
  1638. /* Uh oh! Never got pong from last time, five minutes ago!
  1639. * Server is probably stoned.
  1640. */
  1641. int servidx = findanyidx(serv);
  1642. disconnect_server(servidx);
  1643. lostdcc(servidx);
  1644. putlog(LOG_SERV, "*", IRC_SERVERSTONED);
  1645. } else if (!trying_server) {
  1646. /* Check for server being stoned. */
  1647. dprintf(DP_MODE, "PING :%lu\n", (unsigned long) now);
  1648. waiting_for_awake = 1;
  1649. }
  1650. }
  1651. }
  1652. static void server_prerehash()
  1653. {
  1654. strcpy(oldnick, botname);
  1655. }
  1656. static void server_postrehash()
  1657. {
  1658. strncpyz(botname, origbotname, NICKLEN);
  1659. // if (!botname[0])
  1660. // fatal("NO BOT NAME.", 0);
  1661. // if (serverlist == NULL)
  1662. // fatal("NO SERVER.", 0);
  1663. if (oldnick[0] && !rfc_casecmp(oldnick, botname)
  1664. && !rfc_casecmp(oldnick, get_altbotnick())) {
  1665. /* Change botname back, don't be premature. */
  1666. strcpy(botname, oldnick);
  1667. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  1668. }
  1669. /* Change botname back incase we were using altnick previous to rehash. */
  1670. else if (oldnick[0])
  1671. strcpy(botname, oldnick);
  1672. }
  1673. static void server_die()
  1674. {
  1675. cycle_time = 100;
  1676. if (server_online) {
  1677. dprintf(-serv, "QUIT :%s\n", quit_msg[0] ? quit_msg : "");
  1678. sleep(3); /* Give the server time to understand */
  1679. }
  1680. nuke_server(NULL);
  1681. }
  1682. /* A report on the module status.
  1683. */
  1684. static void server_report(int idx, int details)
  1685. {
  1686. char s1[64], s[128];
  1687. int servidx;
  1688. if (server_online) {
  1689. dprintf(idx, " Online as: %s%s%s (%s)\n", botname,
  1690. botuserhost[0] ? "!" : "", botuserhost[0] ? botuserhost : "",
  1691. botrealname);
  1692. if (nick_juped)
  1693. dprintf(idx, " NICK IS JUPED: %s %s\n", origbotname,
  1694. keepnick ? "(trying)" : "");
  1695. nick_juped = 0; /* WHY?? -- drummer */
  1696. daysdur(now, server_online, s1);
  1697. egg_snprintf(s, sizeof s, "(connected %s)", s1);
  1698. if (server_lag && !waiting_for_awake) {
  1699. if (server_lag == (-1))
  1700. egg_snprintf(s1, sizeof s1, " (bad pong replies)");
  1701. else
  1702. egg_snprintf(s1, sizeof s1, " (lag: %ds)", server_lag);
  1703. strcat(s, s1);
  1704. }
  1705. }
  1706. if ((trying_server || server_online) &&
  1707. ((servidx = findanyidx(serv)) != (-1))) {
  1708. dprintf(idx, " Server %s:%d %s\n", dcc[servidx].host, dcc[servidx].port,
  1709. trying_server ? "(trying)" : s);
  1710. } else
  1711. dprintf(idx, " %s\n", IRC_NOSERVER);
  1712. if (modeq.tot)
  1713. dprintf(idx, " %s %d%%, %d msgs\n", IRC_MODEQUEUE,
  1714. (int) ((float) (modeq.tot * 100.0) / (float) maxqmsg),
  1715. (int) modeq.tot);
  1716. if (mq.tot)
  1717. dprintf(idx, " %s %d%%, %d msgs\n", IRC_SERVERQUEUE,
  1718. (int) ((float) (mq.tot * 100.0) / (float) maxqmsg), (int) mq.tot);
  1719. if (hq.tot)
  1720. dprintf(idx, " %s %d%%, %d msgs\n", IRC_HELPQUEUE,
  1721. (int) ((float) (hq.tot * 100.0) / (float) maxqmsg), (int) hq.tot);
  1722. if (details) {
  1723. if (min_servs)
  1724. dprintf(idx, " Requiring a net of at least %d server(s)\n", min_servs);
  1725. if (initserver[0])
  1726. dprintf(idx, " On connect, I do: %s\n", initserver);
  1727. if (connectserver[0])
  1728. dprintf(idx, " Before connect, I do: %s\n", connectserver);
  1729. dprintf(idx, " Flood is: %d msg/%ds, %d ctcp/%ds\n",
  1730. flud_thr, flud_time, flud_ctcp_thr, flud_ctcp_time);
  1731. }
  1732. }
  1733. static void msgq_clear(struct msgq_head *qh)
  1734. {
  1735. register struct msgq *q, *qq;
  1736. for (q = qh->head; q; q = qq) {
  1737. qq = q->next;
  1738. nfree(q->msg);
  1739. nfree(q);
  1740. }
  1741. qh->head = qh->last = NULL;
  1742. qh->tot = qh->warned = 0;
  1743. }
  1744. static int msgq_expmem(struct msgq_head *qh)
  1745. {
  1746. register int tot = 0;
  1747. register struct msgq *m;
  1748. for (m = qh->head; m; m = m->next) {
  1749. tot += m->len + 1;
  1750. tot += sizeof(struct msgq);
  1751. }
  1752. return tot;
  1753. }
  1754. static int server_expmem()
  1755. {
  1756. int tot = 0;
  1757. struct server_list *s = serverlist;
  1758. for (; s; s = s->next) {
  1759. if (s->name)
  1760. tot += strlen(s->name) + 1;
  1761. if (s->pass)
  1762. tot += strlen(s->pass) + 1;
  1763. if (s->realname)
  1764. tot += strlen(s->realname) + 1;
  1765. tot += sizeof(struct server_list);
  1766. }
  1767. tot += msgq_expmem(&mq) + msgq_expmem(&hq) + msgq_expmem(&modeq);
  1768. return tot;
  1769. }
  1770. static cmd_t my_ctcps[] =
  1771. {
  1772. {"DCC", "", ctcp_DCC_CHAT, "server:DCC"},
  1773. {NULL, NULL, NULL, NULL}
  1774. };
  1775. EXPORT_SCOPE char *server_start();
  1776. static Function server_table[] =
  1777. {
  1778. (Function) server_start,
  1779. (Function) NULL,
  1780. (Function) server_expmem,
  1781. (Function) server_report,
  1782. /* 4 - 7 */
  1783. (Function) NULL, /* char * (points to botname later on) */
  1784. (Function) botuserhost, /* char * */
  1785. (Function) & quiet_reject, /* int */
  1786. (Function) & serv, /* int */
  1787. /* 8 - 11 */
  1788. (Function) & flud_thr, /* int */
  1789. (Function) & flud_time, /* int */
  1790. (Function) & flud_ctcp_thr, /* int */
  1791. (Function) & flud_ctcp_time, /* int */
  1792. /* 12 - 15 */
  1793. (Function) match_my_nick,
  1794. (Function) check_tcl_flud,
  1795. (Function) NULL, /* fixfrom - moved to the core (drummer) */
  1796. (Function) & answer_ctcp, /* int */
  1797. /* 16 - 19 */
  1798. (Function) & trigger_on_ignore, /* int */
  1799. (Function) check_tcl_ctcpr,
  1800. (Function) detect_avalanche,
  1801. (Function) nuke_server,
  1802. /* 20 - 23 */
  1803. (Function) newserver, /* char * */
  1804. (Function) & newserverport, /* int */
  1805. (Function) newserverpass, /* char * */
  1806. (Function) & cycle_time, /* int */
  1807. /* 24 - 27 */
  1808. (Function) & default_port, /* int */
  1809. (Function) & server_online, /* int */
  1810. (Function) & min_servs, /* int */
  1811. (Function) & H_raw, /* p_tcl_bind_list */
  1812. /* 28 - 31 */
  1813. (Function) & H_wall, /* p_tcl_bind_list */
  1814. (Function) & H_msg, /* p_tcl_bind_list */
  1815. (Function) & H_msgm, /* p_tcl_bind_list */
  1816. (Function) & H_notc, /* p_tcl_bind_list */
  1817. /* 32 - 35 */
  1818. (Function) & H_flud, /* p_tcl_bind_list */
  1819. (Function) & H_ctcp, /* p_tcl_bind_list */
  1820. (Function) & H_ctcr, /* p_tcl_bind_list */
  1821. (Function) ctcp_reply,
  1822. /* 36 - 38 */
  1823. (Function) get_altbotnick, /* char * */
  1824. (Function) & nick_len, /* int */
  1825. (Function) check_tcl_notc,
  1826. (Function) & server_lag, /* int */
  1827. (Function) & curserv,
  1828. (Function) cursrvname,
  1829. (Function) botrealname,
  1830. #ifdef S_AUTH
  1831. (Function) & H_msgc, /* p_tcl_bind_list */
  1832. #endif /* S_AUTH */
  1833. };
  1834. char *server_start(Function *global_funcs)
  1835. {
  1836. #if (((TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION >= 4)) || (TCL_MAJOR_VERSION > 8))
  1837. CONST char *s;
  1838. #else
  1839. char *s;
  1840. #endif
  1841. global = global_funcs;
  1842. /*
  1843. * Init of all the variables *must* be done in _start rather than
  1844. * globally.
  1845. */
  1846. serv = -1;
  1847. strict_host = 1;
  1848. botname[0] = 0;
  1849. trying_server = 0L;
  1850. server_lag = 0;
  1851. altnick[0] = 0;
  1852. raltnick[0] = 0;
  1853. curserv = 0;
  1854. flud_thr = 5;
  1855. flud_time = 60;
  1856. flud_ctcp_thr = 3;
  1857. flud_ctcp_time = 60;
  1858. initserver[0] = 0;
  1859. connectserver[0] = 0; /* drummer */
  1860. botuserhost[0] = 0;
  1861. keepnick = 1;
  1862. check_stoned = 1;
  1863. serverror_quit = 1;
  1864. quiet_reject = 1;
  1865. waiting_for_awake = 0;
  1866. server_online = 0;
  1867. server_cycle_wait = 15;
  1868. strcpy(botrealname, "A deranged product of evil coders");
  1869. min_servs = 0;
  1870. server_timeout = 15;
  1871. never_give_up = 1;
  1872. strict_servernames = 1;
  1873. serverlist = NULL;
  1874. cycle_time = 0;
  1875. default_port = 6667;
  1876. oldnick[0] = 0;
  1877. trigger_on_ignore = 0;
  1878. answer_ctcp = 1;
  1879. lowercase_ctcp = 0;
  1880. check_mode_r = 0;
  1881. maxqmsg = 300;
  1882. burst = 0;
  1883. net_type = NETT_EFNET;
  1884. double_mode = 0;
  1885. double_server = 0;
  1886. double_help = 0;
  1887. use_penalties = 0;
  1888. use_fastdeq = 0;
  1889. stackablecmds[0] = 0;
  1890. strcpy(stackable2cmds, "USERHOST ISON");
  1891. resolvserv = 0;
  1892. lastpingtime = 0;
  1893. last_time = 0;
  1894. nick_len = 9;
  1895. kick_method = 1;
  1896. optimize_kicks = 0;
  1897. stack_limit = 4;
  1898. server_table[4] = (Function) botname;
  1899. module_register(MODULE_NAME, server_table, 1, 2);
  1900. if (!(encryption_funcs = module_depend(MODULE_NAME, "encryption", 0, 0))) {
  1901. module_undepend(MODULE_NAME);
  1902. return "This module requires an encryption modules.";
  1903. }
  1904. /* Fool bot in reading the values. */
  1905. s = Tcl_GetVar(interp, "nick", TCL_GLOBAL_ONLY);
  1906. if (s)
  1907. strncpyz(origbotname, s, NICKLEN);
  1908. Tcl_TraceVar(interp, "nick",
  1909. TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1910. nick_change, NULL);
  1911. Tcl_TraceVar(interp, "altnick",
  1912. TCL_TRACE_WRITES | TCL_TRACE_UNSETS, altnick_change, NULL);
  1913. Tcl_TraceVar(interp, "botname",
  1914. TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1915. traced_botname, NULL);
  1916. Tcl_TraceVar(interp, "server",
  1917. TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1918. traced_server, NULL);
  1919. Tcl_TraceVar(interp, "net-type",
  1920. TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1921. traced_nettype, NULL);
  1922. Tcl_TraceVar(interp, "nick-len",
  1923. TCL_TRACE_READS | TCL_TRACE_WRITES | TCL_TRACE_UNSETS,
  1924. traced_nicklen, NULL);
  1925. H_wall = add_bind_table("wall", HT_STACKABLE, server_2char);
  1926. H_raw = add_bind_table("raw", HT_STACKABLE, server_raw);
  1927. H_notc = add_bind_table("notc", HT_STACKABLE, server_5char);
  1928. H_msgm = add_bind_table("msgm", HT_STACKABLE, server_msg);
  1929. #ifdef S_AUTH
  1930. H_msgc = add_bind_table("msgc", 0, server_msgc);
  1931. #endif /* S_AUTH */
  1932. H_msg = add_bind_table("msg", 0, server_msg);
  1933. H_flud = add_bind_table("flud", HT_STACKABLE, server_5char);
  1934. H_ctcr = add_bind_table("ctcr", HT_STACKABLE, server_6char);
  1935. H_ctcp = add_bind_table("ctcp", HT_STACKABLE, server_6char);
  1936. add_builtins(H_raw, my_raw_binds);
  1937. add_builtins_dcc(H_dcc, C_dcc_serv);
  1938. add_builtins(H_ctcp, my_ctcps);
  1939. my_tcl_strings[0].buf = botname;
  1940. add_tcl_strings(my_tcl_strings);
  1941. my_tcl_ints[0].val = &use_console_r;
  1942. add_tcl_ints(my_tcl_ints);
  1943. add_tcl_commands(my_tcl_cmds);
  1944. add_tcl_coups(my_tcl_coups);
  1945. add_hook(HOOK_SECONDLY, (Function) server_secondly);
  1946. add_hook(HOOK_10SECONDLY, (Function) server_10secondly);
  1947. add_hook(HOOK_5MINUTELY, (Function) server_5minutely);
  1948. add_hook(HOOK_MINUTELY, (Function) minutely_checks);
  1949. add_hook(HOOK_QSERV, (Function) queue_server);
  1950. add_hook(HOOK_PRE_REHASH, (Function) server_prerehash);
  1951. add_hook(HOOK_REHASH, (Function) server_postrehash);
  1952. add_hook(HOOK_DIE, (Function) server_die);
  1953. mq.head = hq.head = modeq.head = NULL;
  1954. mq.last = hq.last = modeq.last = NULL;
  1955. mq.tot = hq.tot = modeq.tot = 0;
  1956. mq.warned = hq.warned = modeq.warned = 0;
  1957. double_warned = 0;
  1958. newserver[0] = 0;
  1959. newserverport = 0;
  1960. curserv = 999;
  1961. do_nettype();
  1962. add_cfg(&CFG_NICK);
  1963. add_cfg(&CFG_SERVERS);
  1964. add_cfg(&CFG_SERVERS6);
  1965. add_cfg(&CFG_REALNAME);
  1966. set_cfg_str(NULL, STR("realname"), "A deranged product of evil coders");
  1967. return NULL;
  1968. }
  1969. #endif