server.c 57 KB

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