PageRenderTime 62ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/src/mod/server.mod/server.c

https://github.com/eggheads/eggdrop-1.8
C | 2067 lines | 1748 code | 172 blank | 147 comment | 368 complexity | 22fb0d735decd9d4f7fa867494c9f103 MD5 | raw file
Possible License(s): GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * server.c -- part of server.mod
  3. * basic irc server support
  4. *
  5. * $Id: server.c,v 1.10 2014/10/08 20:08:27 thommey Exp $
  6. */
  7. /*
  8. * Copyright (C) 1997 Robey Pointer
  9. * Copyright (C) 1999 - 2010 Eggheads Development Team
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version 2
  14. * of the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  24. */
  25. #define MODULE_NAME "server"
  26. #define MAKING_SERVER
  27. #include <errno.h>
  28. #include "src/mod/module.h"
  29. #include "server.h"
  30. static Function *global = NULL;
  31. static int ctcp_mode;
  32. static int serv; /* sock # of server currently */
  33. static char newserver[121]; /* new server? */
  34. static int newserverport; /* new server port? */
  35. static char newserverpass[121]; /* new server password? */
  36. static time_t trying_server; /* trying to connect to a server right now? */
  37. static int server_lag; /* how lagged (in seconds) is the server? */
  38. static char altnick[NICKLEN]; /* possible alternate nickname to use */
  39. static char raltnick[NICKLEN]; /* random nick created from altnick */
  40. static int curserv; /* current position in server list: */
  41. static int flud_thr; /* msg flood threshold */
  42. static int flud_time; /* msg flood time */
  43. static int flud_ctcp_thr; /* ctcp flood threshold */
  44. static int flud_ctcp_time; /* ctcp flood time */
  45. static char initserver[121]; /* what, if anything, to send to the
  46. * server on connection */
  47. static char botuserhost[121]; /* bot's user@host (refreshed whenever the
  48. * bot joins a channel) */
  49. /* may not be correct user@host BUT it's
  50. * how the server sees it */
  51. static int keepnick; /* keep trying to regain my intended
  52. * nickname? */
  53. static int nick_juped = 0; /* True if origbotname is juped(RPL437) (dw) */
  54. static int check_stoned; /* Check for a stoned server? */
  55. static int serverror_quit; /* Disconnect from server if ERROR
  56. * messages received? */
  57. static time_t lastpingcheck; /* set when i unidle myself, cleared when
  58. * i get the response */
  59. static time_t server_online; /* server connection time */
  60. static time_t server_cycle_wait; /* seconds to wait before
  61. * re-beginning the server list */
  62. static char botrealname[121]; /* realname of bot */
  63. static int server_timeout; /* server timeout for connecting */
  64. static struct server_list *serverlist; /* old-style queue, still used by
  65. * server list */
  66. static int cycle_time; /* cycle time till next server connect */
  67. static int default_port; /* default IRC port */
  68. static char oldnick[NICKLEN]; /* previous nickname *before* rehash */
  69. static int trigger_on_ignore; /* trigger bindings if user is ignored ? */
  70. static int exclusive_binds; /* configures PUBM and MSGM binds to be
  71. * exclusive of PUB and MSG binds. */
  72. static int answer_ctcp; /* answer how many stacked ctcp's ? */
  73. static int lowercase_ctcp; /* answer lowercase CTCP's (non-standard) */
  74. static int check_mode_r; /* check for IRCnet +r modes */
  75. static int net_type;
  76. static char connectserver[121]; /* what, if anything, to do before connect
  77. * to the server */
  78. static int resolvserv; /* in the process of resolving a server host */
  79. static int double_mode; /* allow a msgs to be twice in a queue? */
  80. static int double_server;
  81. static int double_help;
  82. static int double_warned;
  83. static int lastpingtime; /* IRCnet LAGmeter support -- drummer */
  84. static char stackablecmds[511];
  85. static char stackable2cmds[511];
  86. static time_t last_time;
  87. static int use_penalties;
  88. static int use_fastdeq;
  89. static int nick_len; /* Maximal nick length allowed on the
  90. * network. */
  91. static int kick_method;
  92. static int optimize_kicks;
  93. static int msgrate; /* Number of seconds between sending
  94. * queued lines to server. */
  95. #ifdef TLS
  96. static int use_ssl; /* Use SSL for the next server connection? */
  97. static int tls_vfyserver; /* Certificate validation mode for servrs */
  98. #endif
  99. static p_tcl_bind_list H_wall, H_raw, H_notc, H_msgm, H_msg, H_flud, H_ctcr,
  100. H_ctcp, H_out;
  101. static void empty_msgq(void);
  102. static void next_server(int *, char *, unsigned int *, char *);
  103. static void disconnect_server(int);
  104. static char *get_altbotnick(void);
  105. static int calc_penalty(char *);
  106. static int fast_deq(int);
  107. static char *splitnicks(char **);
  108. static void check_queues(char *, char *);
  109. static void parse_q(struct msgq_head *, char *, char *);
  110. static void purge_kicks(struct msgq_head *);
  111. static int deq_kick(int);
  112. static void msgq_clear(struct msgq_head *qh);
  113. static int stack_limit;
  114. static char *realservername;
  115. #include "servmsg.c"
  116. #define MAXPENALTY 10
  117. /* Maximum messages to store in each queue. */
  118. static int maxqmsg;
  119. static struct msgq_head mq, hq, modeq;
  120. static int burst;
  121. #include "cmdsserv.c"
  122. #include "tclserv.c"
  123. /*
  124. * Bot server queues
  125. */
  126. /* Called periodically to shove out another queued item.
  127. *
  128. * 'mode' queue gets priority now.
  129. *
  130. * Most servers will allow 'bursts' of upto 5 msgs, so let's put something
  131. * in to support flushing modeq a little faster if possible.
  132. * Will send upto 4 msgs from modeq, and then send 1 msg every time
  133. * it will *not* send anything from hq until the 'burst' value drops
  134. * down to 0 again (allowing a sudden mq flood to sneak through).
  135. */
  136. static void deq_msg()
  137. {
  138. struct msgq *q;
  139. int ok = 0;
  140. /* now < last_time tested 'cause clock adjustments could mess it up */
  141. if ((now - last_time) >= msgrate || now < (last_time - 90)) {
  142. last_time = now;
  143. if (burst > 0)
  144. burst--;
  145. ok = 1;
  146. }
  147. if (serv < 0)
  148. return;
  149. /* Send upto 4 msgs to server if the *critical queue* has anything in it */
  150. if (modeq.head) {
  151. while (modeq.head && (burst < 4) && ((last_time - now) < MAXPENALTY)) {
  152. if (deq_kick(DP_MODE)) {
  153. burst++;
  154. continue;
  155. }
  156. if (!modeq.head)
  157. break;
  158. if (fast_deq(DP_MODE)) {
  159. burst++;
  160. continue;
  161. }
  162. check_tcl_out(DP_MODE, modeq.head->msg, 1);
  163. write_to_server(modeq.head->msg, modeq.head->len);
  164. if (raw_log)
  165. putlog(LOG_SRVOUT, "*", "[m->] %s", modeq.head->msg);
  166. modeq.tot--;
  167. last_time += calc_penalty(modeq.head->msg);
  168. q = modeq.head->next;
  169. nfree(modeq.head->msg);
  170. nfree(modeq.head);
  171. modeq.head = q;
  172. burst++;
  173. }
  174. if (!modeq.head)
  175. modeq.last = 0;
  176. return;
  177. }
  178. /* Send something from the normal msg q even if we're slightly bursting */
  179. if (burst > 1)
  180. return;
  181. if (mq.head) {
  182. burst++;
  183. if (deq_kick(DP_SERVER))
  184. return;
  185. if (fast_deq(DP_SERVER))
  186. return;
  187. check_tcl_out(DP_SERVER, mq.head->msg, 1);
  188. write_to_server(mq.head->msg, mq.head->len);
  189. if (raw_log)
  190. putlog(LOG_SRVOUT, "*", "[s->] %s", mq.head->msg);
  191. mq.tot--;
  192. last_time += calc_penalty(mq.head->msg);
  193. q = mq.head->next;
  194. nfree(mq.head->msg);
  195. nfree(mq.head);
  196. mq.head = q;
  197. if (!mq.head)
  198. mq.last = NULL;
  199. return;
  200. }
  201. /* Never send anything from the help queue unless everything else is
  202. * finished.
  203. */
  204. if (!hq.head || burst || !ok)
  205. return;
  206. if (deq_kick(DP_HELP))
  207. return;
  208. if (fast_deq(DP_HELP))
  209. return;
  210. check_tcl_out(DP_HELP, hq.head->msg, 1);
  211. write_to_server(hq.head->msg, hq.head->len);
  212. if (raw_log)
  213. putlog(LOG_SRVOUT, "*", "[h->] %s", hq.head->msg);
  214. hq.tot--;
  215. last_time += calc_penalty(hq.head->msg);
  216. q = hq.head->next;
  217. nfree(hq.head->msg);
  218. nfree(hq.head);
  219. hq.head = q;
  220. if (!hq.head)
  221. hq.last = NULL;
  222. }
  223. static int calc_penalty(char *msg)
  224. {
  225. char *cmd, *par1, *par2, *par3;
  226. register int penalty, i, ii;
  227. if (!use_penalties && net_type != NETT_UNDERNET &&
  228. net_type != NETT_HYBRID_EFNET)
  229. return 0;
  230. cmd = newsplit(&msg);
  231. if (msg)
  232. i = strlen(msg);
  233. else
  234. i = strlen(cmd);
  235. last_time -= 2; /* undo eggdrop standard flood prot */
  236. if (net_type == NETT_UNDERNET || net_type == NETT_HYBRID_EFNET) {
  237. last_time += (2 + i / 120);
  238. return 0;
  239. }
  240. penalty = (1 + i / 100);
  241. if (!egg_strcasecmp(cmd, "KICK")) {
  242. par1 = newsplit(&msg); /* channel */
  243. par2 = newsplit(&msg); /* victim(s) */
  244. par3 = splitnicks(&par2);
  245. penalty++;
  246. while (strlen(par3) > 0) {
  247. par3 = splitnicks(&par2);
  248. penalty++;
  249. }
  250. ii = penalty;
  251. par3 = splitnicks(&par1);
  252. while (strlen(par1) > 0) {
  253. par3 = splitnicks(&par1);
  254. penalty += ii;
  255. }
  256. } else if (!egg_strcasecmp(cmd, "MODE")) {
  257. i = 0;
  258. par1 = newsplit(&msg); /* channel */
  259. par2 = newsplit(&msg); /* mode(s) */
  260. if (!strlen(par2))
  261. i++;
  262. while (strlen(par2) > 0) {
  263. if (strchr("ntimps", par2[0]))
  264. i += 3;
  265. else if (!strchr("+-", par2[0]))
  266. i += 1;
  267. par2++;
  268. }
  269. while (strlen(msg) > 0) {
  270. newsplit(&msg);
  271. i += 2;
  272. }
  273. ii = 0;
  274. while (strlen(par1) > 0) {
  275. splitnicks(&par1);
  276. ii++;
  277. }
  278. penalty += (ii * i);
  279. } else if (!egg_strcasecmp(cmd, "TOPIC")) {
  280. penalty++;
  281. par1 = newsplit(&msg); /* channel */
  282. par2 = newsplit(&msg); /* topic */
  283. if (strlen(par2) > 0) { /* topic manipulation => 2 penalty points */
  284. penalty += 2;
  285. par3 = splitnicks(&par1);
  286. while (strlen(par1) > 0) {
  287. par3 = splitnicks(&par1);
  288. penalty += 2;
  289. }
  290. }
  291. } else if (!egg_strcasecmp(cmd, "PRIVMSG") ||
  292. !egg_strcasecmp(cmd, "NOTICE")) {
  293. par1 = newsplit(&msg); /* channel(s)/nick(s) */
  294. /* Add one sec penalty for each recipient */
  295. while (strlen(par1) > 0) {
  296. splitnicks(&par1);
  297. penalty++;
  298. }
  299. } else if (!egg_strcasecmp(cmd, "WHO")) {
  300. par1 = newsplit(&msg); /* masks */
  301. par2 = par1;
  302. while (strlen(par1) > 0) {
  303. par2 = splitnicks(&par1);
  304. if (strlen(par2) > 4) /* long WHO-masks receive less penalty */
  305. penalty += 3;
  306. else
  307. penalty += 5;
  308. }
  309. } else if (!egg_strcasecmp(cmd, "AWAY")) {
  310. if (strlen(msg) > 0)
  311. penalty += 2;
  312. else
  313. penalty += 1;
  314. } else if (!egg_strcasecmp(cmd, "INVITE")) {
  315. /* Successful invite receives 2 or 3 penalty points. Let's go
  316. * with the maximum.
  317. */
  318. penalty += 3;
  319. } else if (!egg_strcasecmp(cmd, "JOIN")) {
  320. penalty += 2;
  321. } else if (!egg_strcasecmp(cmd, "PART")) {
  322. penalty += 4;
  323. } else if (!egg_strcasecmp(cmd, "VERSION")) {
  324. penalty += 2;
  325. } else if (!egg_strcasecmp(cmd, "TIME")) {
  326. penalty += 2;
  327. } else if (!egg_strcasecmp(cmd, "TRACE")) {
  328. penalty += 2;
  329. } else if (!egg_strcasecmp(cmd, "NICK")) {
  330. penalty += 3;
  331. } else if (!egg_strcasecmp(cmd, "ISON")) {
  332. penalty += 1;
  333. } else if (!egg_strcasecmp(cmd, "WHOIS")) {
  334. penalty += 2;
  335. } else if (!egg_strcasecmp(cmd, "DNS")) {
  336. penalty += 2;
  337. } else
  338. penalty++; /* just add standard-penalty */
  339. /* Shouldn't happen, but you never know... */
  340. if (penalty > 99)
  341. penalty = 99;
  342. if (penalty < 2) {
  343. putlog(LOG_SRVOUT, "*", "Penalty < 2sec; that's impossible!");
  344. penalty = 2;
  345. }
  346. if (raw_log && penalty != 0)
  347. putlog(LOG_SRVOUT, "*", "Adding penalty: %i", penalty);
  348. return penalty;
  349. }
  350. char *splitnicks(char **rest)
  351. {
  352. register char *o, *r;
  353. if (!rest)
  354. return *rest = "";
  355. o = *rest;
  356. while (*o == ' ')
  357. o++;
  358. r = o;
  359. while (*o && *o != ',')
  360. o++;
  361. if (*o)
  362. *o++ = 0;
  363. *rest = o;
  364. return r;
  365. }
  366. static int fast_deq(int which)
  367. {
  368. struct msgq_head *h;
  369. struct msgq *m, *nm;
  370. char msgstr[511], nextmsgstr[511], tosend[511], victims[511], stackable[511],
  371. *msg, *nextmsg, *cmd, *nextcmd, *to, *nextto, *stckbl;
  372. int len, doit = 0, found = 0, cmd_count = 0, stack_method = 1;
  373. if (!use_fastdeq)
  374. return 0;
  375. switch (which) {
  376. case DP_MODE:
  377. h = &modeq;
  378. break;
  379. case DP_SERVER:
  380. h = &mq;
  381. break;
  382. case DP_HELP:
  383. h = &hq;
  384. break;
  385. default:
  386. return 0;
  387. }
  388. m = h->head;
  389. strncpyz(msgstr, m->msg, sizeof msgstr);
  390. msg = msgstr;
  391. cmd = newsplit(&msg);
  392. if (use_fastdeq > 1) {
  393. strncpyz(stackable, stackablecmds, sizeof stackable);
  394. stckbl = stackable;
  395. while (strlen(stckbl) > 0) {
  396. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  397. found = 1;
  398. break;
  399. }
  400. }
  401. /* If use_fastdeq is 2, only commands in the list should be stacked. */
  402. if (use_fastdeq == 2 && !found)
  403. return 0;
  404. /* If use_fastdeq is 3, only commands _not_ in the list should be stacked. */
  405. if (use_fastdeq == 3 && found)
  406. return 0;
  407. /* we check for the stacking method (default=1) */
  408. strncpyz(stackable, stackable2cmds, sizeof stackable);
  409. stckbl = stackable;
  410. while (strlen(stckbl) > 0)
  411. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  412. stack_method = 2;
  413. break;
  414. }
  415. }
  416. to = newsplit(&msg);
  417. len = strlen(to);
  418. simple_sprintf(victims, "%s", to);
  419. while (m) {
  420. nm = m->next;
  421. if (!nm)
  422. break;
  423. strncpyz(nextmsgstr, nm->msg, sizeof nextmsgstr);
  424. nextmsg = nextmsgstr;
  425. nextcmd = newsplit(&nextmsg);
  426. nextto = newsplit(&nextmsg);
  427. len = strlen(nextto);
  428. if (strcmp(to, nextto) && !strcmp(cmd, nextcmd) && !strcmp(msg, nextmsg) &&
  429. ((strlen(cmd) + strlen(victims) + strlen(nextto) + strlen(msg) + 2) <
  430. 510) && (!stack_limit || cmd_count < stack_limit - 1)) {
  431. cmd_count++;
  432. if (stack_method == 1)
  433. simple_sprintf(victims, "%s,%s", victims, nextto);
  434. else
  435. simple_sprintf(victims, "%s %s", victims, nextto);
  436. doit = 1;
  437. m->next = nm->next;
  438. if (!nm->next)
  439. h->last = m;
  440. nfree(nm->msg);
  441. nfree(nm);
  442. h->tot--;
  443. } else
  444. m = m->next;
  445. }
  446. if (doit) {
  447. simple_sprintf(tosend, "%s %s %s", cmd, victims, msg);
  448. len = strlen(tosend);
  449. check_tcl_out(which, tosend, 1);
  450. write_to_server(tosend, len);
  451. if (raw_log) {
  452. switch (which) {
  453. case DP_MODE:
  454. putlog(LOG_SRVOUT, "*", "[m=>] %s", tosend);
  455. break;
  456. case DP_SERVER:
  457. putlog(LOG_SRVOUT, "*", "[s=>] %s", tosend);
  458. break;
  459. case DP_HELP:
  460. putlog(LOG_SRVOUT, "*", "[h=>] %s", tosend);
  461. break;
  462. }
  463. }
  464. m = h->head->next;
  465. nfree(h->head->msg);
  466. nfree(h->head);
  467. h->head = m;
  468. if (!h->head)
  469. h->last = 0;
  470. h->tot--;
  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. return;
  480. if (modeq.head)
  481. parse_q(&modeq, oldnick, newnick);
  482. if (mq.head)
  483. parse_q(&mq, oldnick, newnick);
  484. if (hq.head)
  485. parse_q(&hq, oldnick, newnick);
  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. msg = buf;
  498. newsplit(&msg);
  499. chan = newsplit(&msg);
  500. nicks = newsplit(&msg);
  501. while (strlen(nicks) > 0) {
  502. nick = splitnicks(&nicks);
  503. if (!egg_strcasecmp(nick, oldnick) &&
  504. ((9 + strlen(chan) + strlen(newnicks) + strlen(newnick) +
  505. strlen(nicks) + strlen(msg)) < 510)) {
  506. if (newnick)
  507. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, newnick);
  508. changed = 1;
  509. } else
  510. egg_snprintf(newnicks, sizeof newnicks, ",%s", nick);
  511. }
  512. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s", chan,
  513. newnicks + 1, msg);
  514. }
  515. if (changed) {
  516. if (newnicks[0] == 0) {
  517. if (!lm)
  518. q->head = m->next;
  519. else
  520. lm->next = m->next;
  521. nfree(m->msg);
  522. nfree(m);
  523. m = lm;
  524. q->tot--;
  525. if (!q->head)
  526. q->last = 0;
  527. } else {
  528. nfree(m->msg);
  529. m->msg = nmalloc(strlen(newmsg) + 1);
  530. m->len = strlen(newmsg);
  531. strcpy(m->msg, newmsg);
  532. }
  533. }
  534. lm = m;
  535. if (m)
  536. m = m->next;
  537. else
  538. m = q->head;
  539. }
  540. }
  541. static void purge_kicks(struct msgq_head *q)
  542. {
  543. struct msgq *m, *lm = NULL;
  544. char buf[511], *reason, *nicks, *nick, *chan, newnicks[511],
  545. newmsg[511], chans[511], *chns, *ch;
  546. int changed, found;
  547. struct chanset_t *cs;
  548. for (m = q->head; m;) {
  549. if (!egg_strncasecmp(m->msg, "KICK", 4)) {
  550. newnicks[0] = 0;
  551. changed = 0;
  552. strncpyz(buf, m->msg, sizeof buf);
  553. reason = buf;
  554. newsplit(&reason);
  555. chan = newsplit(&reason);
  556. nicks = newsplit(&reason);
  557. while (strlen(nicks) > 0) {
  558. found = 0;
  559. nick = splitnicks(&nicks);
  560. strncpyz(chans, chan, sizeof chans);
  561. chns = chans;
  562. while (strlen(chns) > 0) {
  563. ch = newsplit(&chns);
  564. cs = findchan(ch);
  565. if (!cs)
  566. continue;
  567. if (ismember(cs, nick))
  568. found = 1;
  569. }
  570. if (found)
  571. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, nick);
  572. else {
  573. putlog(LOG_SRVOUT, "*", "%s isn't on any target channel; removing "
  574. "kick.", nick);
  575. changed = 1;
  576. }
  577. }
  578. if (changed) {
  579. if (newnicks[0] == 0) {
  580. if (!lm)
  581. q->head = m->next;
  582. else
  583. lm->next = m->next;
  584. nfree(m->msg);
  585. nfree(m);
  586. m = lm;
  587. q->tot--;
  588. if (!q->head)
  589. q->last = 0;
  590. } else {
  591. nfree(m->msg);
  592. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s", chan,
  593. newnicks + 1, reason);
  594. m->msg = nmalloc(strlen(newmsg) + 1);
  595. m->len = strlen(newmsg);
  596. strcpy(m->msg, newmsg);
  597. }
  598. }
  599. }
  600. lm = m;
  601. if (m)
  602. m = m->next;
  603. else
  604. m = q->head;
  605. }
  606. }
  607. static int deq_kick(int which)
  608. {
  609. struct msgq_head *h;
  610. struct msgq *msg, *m, *lm;
  611. char buf[511], buf2[511], *reason2, *nicks, *chan, *chan2, *reason, *nick,
  612. newnicks[511], newnicks2[511], newmsg[511];
  613. int changed = 0, nr = 0;
  614. if (!optimize_kicks)
  615. return 0;
  616. newnicks[0] = 0;
  617. switch (which) {
  618. case DP_MODE:
  619. h = &modeq;
  620. break;
  621. case DP_SERVER:
  622. h = &mq;
  623. break;
  624. case DP_HELP:
  625. h = &hq;
  626. break;
  627. default:
  628. return 0;
  629. }
  630. if (egg_strncasecmp(h->head->msg, "KICK", 4))
  631. return 0;
  632. if (optimize_kicks == 2) {
  633. purge_kicks(h);
  634. if (!h->head)
  635. return 1;
  636. }
  637. if (egg_strncasecmp(h->head->msg, "KICK", 4))
  638. return 0;
  639. msg = h->head;
  640. strncpyz(buf, msg->msg, sizeof buf);
  641. reason = buf;
  642. newsplit(&reason);
  643. chan = newsplit(&reason);
  644. nicks = newsplit(&reason);
  645. while (strlen(nicks) > 0) {
  646. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks,
  647. newsplit(&nicks));
  648. nr++;
  649. }
  650. for (m = msg->next, lm = NULL; m && (nr < kick_method);) {
  651. if (!egg_strncasecmp(m->msg, "KICK", 4)) {
  652. changed = 0;
  653. newnicks2[0] = 0;
  654. strncpyz(buf2, m->msg, sizeof buf2);
  655. reason2 = buf2;
  656. newsplit(&reason2);
  657. chan2 = newsplit(&reason2);
  658. nicks = newsplit(&reason2);
  659. if (!egg_strcasecmp(chan, chan2) && !egg_strcasecmp(reason, reason2)) {
  660. while (strlen(nicks) > 0) {
  661. nick = splitnicks(&nicks);
  662. if ((nr < kick_method) && ((9 + strlen(chan) + strlen(newnicks) +
  663. strlen(nick) + strlen(reason)) < 510)) {
  664. egg_snprintf(newnicks, sizeof newnicks, "%s,%s", newnicks, nick);
  665. nr++;
  666. changed = 1;
  667. } else
  668. egg_snprintf(newnicks2, sizeof newnicks2, "%s,%s", newnicks2, nick);
  669. }
  670. }
  671. if (changed) {
  672. if (newnicks2[0] == 0) {
  673. if (!lm)
  674. h->head->next = m->next;
  675. else
  676. lm->next = m->next;
  677. nfree(m->msg);
  678. nfree(m);
  679. m = lm;
  680. h->tot--;
  681. if (!h->head)
  682. h->last = 0;
  683. } else {
  684. nfree(m->msg);
  685. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s", chan2,
  686. newnicks2 + 1, reason);
  687. m->msg = nmalloc(strlen(newmsg) + 1);
  688. m->len = strlen(newmsg);
  689. strcpy(m->msg, newmsg);
  690. }
  691. }
  692. }
  693. lm = m;
  694. if (m)
  695. m = m->next;
  696. else
  697. m = h->head->next;
  698. }
  699. egg_snprintf(newmsg, sizeof newmsg, "KICK %s %s %s", chan, newnicks + 1,
  700. reason);
  701. check_tcl_out(which, newmsg, 1);
  702. write_to_server(newmsg, strlen(newmsg));
  703. if (raw_log) {
  704. switch (which) {
  705. case DP_MODE:
  706. putlog(LOG_SRVOUT, "*", "[m->] %s", newmsg);
  707. break;
  708. case DP_SERVER:
  709. putlog(LOG_SRVOUT, "*", "[s->] %s", newmsg);
  710. break;
  711. case DP_HELP:
  712. putlog(LOG_SRVOUT, "*", "[h->] %s", newmsg);
  713. break;
  714. }
  715. debug3("Changed: %d, kick-method: %d, nr: %d", changed, kick_method, nr);
  716. }
  717. h->tot--;
  718. last_time += calc_penalty(newmsg);
  719. m = h->head->next;
  720. nfree(h->head->msg);
  721. nfree(h->head);
  722. h->head = m;
  723. if (!h->head)
  724. h->last = 0;
  725. return 1;
  726. }
  727. /* Clean out the msg queues (like when changing servers).
  728. */
  729. static void empty_msgq()
  730. {
  731. msgq_clear(&modeq);
  732. msgq_clear(&mq);
  733. msgq_clear(&hq);
  734. burst = 0;
  735. }
  736. /* Queues outgoing messages so there's no flooding.
  737. */
  738. static void queue_server(int which, char *msg, int len)
  739. {
  740. struct msgq_head *h = NULL, tempq;
  741. struct msgq *q, *tq, *tqq;
  742. int doublemsg = 0, qnext = 0;
  743. char buf[511];
  744. /* Don't even BOTHER if there's no server online. */
  745. if (serv < 0)
  746. return;
  747. /* Remove \r\n. We will add these back when we send the text to the server.
  748. * - Wcc [01/09/2004]
  749. */
  750. strncpy(buf, msg, sizeof buf);
  751. msg = buf;
  752. remove_crlf(&msg);
  753. buf[510] = 0;
  754. len = strlen(buf);
  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;
  759. check_tcl_out(which, buf, 1);
  760. write_to_server(buf, len);
  761. if (raw_log)
  762. putlog(LOG_SRVOUT, "*", "[m->] %s", buf);
  763. return;
  764. }
  765. switch (which) {
  766. case DP_MODE_NEXT:
  767. qnext = 1;
  768. /* Fallthrough */
  769. case DP_MODE:
  770. h = &modeq;
  771. tempq = modeq;
  772. if (double_mode)
  773. doublemsg = 1;
  774. break;
  775. case DP_SERVER_NEXT:
  776. qnext = 1;
  777. /* Fallthrough */
  778. case DP_SERVER:
  779. h = &mq;
  780. tempq = mq;
  781. if (double_server)
  782. doublemsg = 1;
  783. break;
  784. case DP_HELP_NEXT:
  785. qnext = 1;
  786. /* Fallthrough */
  787. case DP_HELP:
  788. h = &hq;
  789. tempq = hq;
  790. if (double_help)
  791. doublemsg = 1;
  792. break;
  793. default:
  794. putlog(LOG_MISC, "*", "Warning: queuing unknown type to server!");
  795. return;
  796. }
  797. if (h->tot < maxqmsg) {
  798. /* Don't queue msg if it's already queued? */
  799. if (!doublemsg) {
  800. for (tq = tempq.head; tq; tq = tqq) {
  801. tqq = tq->next;
  802. if (!egg_strcasecmp(tq->msg, buf)) {
  803. if (!double_warned) {
  804. debug1("Message already queued; skipping: %s", buf);
  805. double_warned = 1;
  806. }
  807. return;
  808. }
  809. }
  810. }
  811. if (check_tcl_out(which, buf, 0))
  812. return; /* a Tcl proc requested not to send the message */
  813. q = nmalloc(sizeof(struct msgq));
  814. /* Insert into queue. */
  815. if (qnext) {
  816. q->next = h->head;
  817. h->head = q;
  818. if (!h->last)
  819. h->last = q;
  820. }
  821. else {
  822. q->next = NULL;
  823. if (h->last)
  824. h->last->next = q;
  825. else
  826. h->head = q;
  827. h->last = q;
  828. }
  829. q->len = len;
  830. q->msg = nmalloc(len + 1);
  831. egg_memcpy(q->msg, buf, len);
  832. q->msg[len] = 0;
  833. h->tot++;
  834. h->warned = 0;
  835. double_warned = 0;
  836. if (raw_log) {
  837. switch (which) {
  838. case DP_MODE:
  839. putlog(LOG_SRVOUT, "*", "[!m] %s", buf);
  840. break;
  841. case DP_SERVER:
  842. putlog(LOG_SRVOUT, "*", "[!s] %s", buf);
  843. break;
  844. case DP_HELP:
  845. putlog(LOG_SRVOUT, "*", "[!h] %s", buf);
  846. break;
  847. case DP_MODE_NEXT:
  848. putlog(LOG_SRVOUT, "*", "[!!m] %s", buf);
  849. break;
  850. case DP_SERVER_NEXT:
  851. putlog(LOG_SRVOUT, "*", "[!!s] %s", buf);
  852. break;
  853. case DP_HELP_NEXT:
  854. putlog(LOG_SRVOUT, "*", "[!!h] %s", buf);
  855. break;
  856. }
  857. }
  858. } else {
  859. if (!h->warned) {
  860. switch (which) {
  861. case DP_MODE_NEXT:
  862. /* Fallthrough */
  863. case DP_MODE:
  864. putlog(LOG_MISC, "*", "Warning: over maximum mode queue!");
  865. break;
  866. case DP_SERVER_NEXT:
  867. /* Fallthrough */
  868. case DP_SERVER:
  869. putlog(LOG_MISC, "*", "Warning: over maximum server queue!");
  870. break;
  871. case DP_HELP_NEXT:
  872. /* Fallthrough */
  873. case DP_HELP:
  874. putlog(LOG_MISC, "*", "Warning: over maximum help queue!");
  875. break;
  876. }
  877. }
  878. h->warned = 1;
  879. }
  880. if (which == DP_MODE || which == DP_MODE_NEXT)
  881. deq_msg(); /* DP_MODE needs to be sent ASAP, flush if possible. */
  882. }
  883. /* Add a new server to the server_list.
  884. */
  885. static void add_server(const char *ss)
  886. {
  887. struct server_list *x, *z;
  888. char name[256] = "", port[11] = "", pass[121] = "";
  889. for (z = serverlist; z && z->next; z = z->next);
  890. /* Allow IPv6 and IPv4-mapped addresses in [] */
  891. if (!sscanf(ss, "[%255[0-9.A-F:a-f]]:%10[+0-9]:%120s", name, port, pass) &&
  892. !sscanf(ss, "%255[^:]:%10[+0-9]:%120s", name, port, pass))
  893. return;
  894. x = nmalloc(sizeof(struct server_list));
  895. x->next = 0;
  896. x->realname = 0;
  897. x->port = default_port;
  898. if (z)
  899. z->next = x;
  900. else
  901. serverlist = x;
  902. z = x;
  903. x->name = nmalloc(strlen(name) + 1);
  904. strcpy(x->name, name);
  905. if (pass[0]) {
  906. x->pass = nmalloc(strlen(pass) + 1);
  907. strcpy(x->pass, pass);
  908. } else
  909. x->pass = NULL;
  910. if (port[0])
  911. x->port = atoi(port);
  912. #ifdef TLS
  913. x->ssl = (port[0] == '+') ? 1 : 0;
  914. #endif
  915. }
  916. /* Clear out the given server_list.
  917. */
  918. static void clearq(struct server_list *xx)
  919. {
  920. struct server_list *x;
  921. while (xx) {
  922. x = xx->next;
  923. if (xx->name)
  924. nfree(xx->name);
  925. if (xx->pass)
  926. nfree(xx->pass);
  927. if (xx->realname)
  928. nfree(xx->realname);
  929. nfree(xx);
  930. xx = x;
  931. }
  932. }
  933. /* Set botserver to the next available server.
  934. *
  935. * -> if (*ptr == -1) then jump to that particular server
  936. */
  937. static void next_server(int *ptr, char *serv, unsigned int *port, char *pass)
  938. {
  939. struct server_list *x = serverlist;
  940. int i = 0;
  941. /* -1 --> Go to specified server */
  942. if (*ptr == -1) {
  943. for (; x; x = x->next) {
  944. if (x->port == *port) {
  945. if (!egg_strcasecmp(x->name, serv)) {
  946. *ptr = i;
  947. #ifdef TLS
  948. x->ssl = use_ssl;
  949. #endif
  950. return;
  951. } else if (x->realname && !egg_strcasecmp(x->realname, serv)) {
  952. *ptr = i;
  953. strncpyz(serv, x->realname, sizeof serv);
  954. #ifdef TLS
  955. use_ssl = x->ssl;
  956. #endif
  957. return;
  958. }
  959. }
  960. i++;
  961. }
  962. /* Gotta add it: */
  963. x = nmalloc(sizeof(struct server_list));
  964. x->next = 0;
  965. x->realname = 0;
  966. x->name = nmalloc(strlen(serv) + 1);
  967. strcpy(x->name, serv);
  968. x->port = *port ? *port : default_port;
  969. if (pass && pass[0]) {
  970. x->pass = nmalloc(strlen(pass) + 1);
  971. strcpy(x->pass, pass);
  972. } else
  973. x->pass = NULL;
  974. #ifdef TLS
  975. x->ssl = use_ssl;
  976. #endif
  977. egg_list_append((struct list_type **) (&serverlist),
  978. (struct list_type *) x);
  979. *ptr = i;
  980. return;
  981. }
  982. /* Find where i am and boogie */
  983. if (x == NULL)
  984. return;
  985. i = (*ptr);
  986. while (i > 0 && x != NULL) {
  987. x = x->next;
  988. i--;
  989. }
  990. if (x != NULL) {
  991. x = x->next;
  992. (*ptr)++;
  993. } /* Go to next server */
  994. if (x == NULL) {
  995. x = serverlist;
  996. *ptr = 0;
  997. } /* Start over at the beginning */
  998. #ifdef TLS
  999. use_ssl = x->ssl;
  1000. #endif
  1001. strcpy(serv, x->name);
  1002. *port = x->port ? x->port : default_port;
  1003. if (x->pass)
  1004. strcpy(pass, x->pass);
  1005. else
  1006. pass[0] = 0;
  1007. }
  1008. static int server_6char STDVAR
  1009. {
  1010. Function F = (Function) cd;
  1011. char x[20];
  1012. BADARGS(7, 7, " nick user@host handle dest/chan keyword text");
  1013. CHECKVALIDITY(server_6char);
  1014. egg_snprintf(x, sizeof x, "%d",
  1015. F(argv[1], argv[2], argv[3], argv[4], argv[5], argv[6]));
  1016. Tcl_AppendResult(irp, x, NULL);
  1017. return TCL_OK;
  1018. }
  1019. static int server_5char STDVAR
  1020. {
  1021. Function F = (Function) cd;
  1022. BADARGS(6, 6, " nick user@host handle dest/channel text");
  1023. CHECKVALIDITY(server_5char);
  1024. F(argv[1], argv[2], argv[3], argv[4], argv[5]);
  1025. return TCL_OK;
  1026. }
  1027. static int server_2char STDVAR
  1028. {
  1029. Function F = (Function) cd;
  1030. BADARGS(3, 3, " nick msg");
  1031. CHECKVALIDITY(server_2char);
  1032. F(argv[1], argv[2]);
  1033. return TCL_OK;
  1034. }
  1035. static int server_msg STDVAR
  1036. {
  1037. Function F = (Function) cd;
  1038. BADARGS(5, 5, " nick uhost hand buffer");
  1039. CHECKVALIDITY(server_msg);
  1040. F(argv[1], argv[2], get_user_by_handle(userlist, argv[3]), argv[4]);
  1041. return TCL_OK;
  1042. }
  1043. static int server_raw STDVAR
  1044. {
  1045. Function F = (Function) cd;
  1046. BADARGS(4, 4, " from code args");
  1047. CHECKVALIDITY(server_raw);
  1048. Tcl_AppendResult(irp, int_to_base10(F(argv[1], argv[3])), NULL);
  1049. return TCL_OK;
  1050. }
  1051. static int server_out STDVAR
  1052. {
  1053. Function F = (Function) cd;
  1054. BADARGS(4, 4, " queue message sent");
  1055. CHECKVALIDITY(server_out);
  1056. F(argv[1], argv[2], argv[3]);
  1057. return TCL_OK;
  1058. }
  1059. /* Read/write normal string variable.
  1060. */
  1061. static char *nick_change(ClientData cdata, Tcl_Interp *irp,
  1062. EGG_CONST char *name1,
  1063. EGG_CONST char *name2, int flags)
  1064. {
  1065. EGG_CONST char *new;
  1066. if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
  1067. Tcl_SetVar2(interp, name1, name2, origbotname, TCL_GLOBAL_ONLY);
  1068. if (flags & TCL_TRACE_UNSETS)
  1069. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1070. TCL_TRACE_UNSETS, nick_change, cdata);
  1071. } else { /* writes */
  1072. new = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1073. if (strcmp(origbotname, (char *) new)) {
  1074. if (origbotname[0]) {
  1075. putlog(LOG_MISC, "*", "* IRC NICK CHANGE: %s -> %s", origbotname, new);
  1076. nick_juped = 0;
  1077. }
  1078. strncpyz(origbotname, new, NICKLEN);
  1079. if (server_online)
  1080. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  1081. }
  1082. }
  1083. return NULL;
  1084. }
  1085. /* Replace all '?'s in s with a random number.
  1086. */
  1087. static void rand_nick(char *nick)
  1088. {
  1089. register char *p = nick;
  1090. while ((p = strchr(p, '?')) != NULL) {
  1091. *p = '0' + randint(10);
  1092. p++;
  1093. }
  1094. }
  1095. /* Return the alternative bot nick.
  1096. */
  1097. static char *get_altbotnick(void)
  1098. {
  1099. /* A random-number nick? */
  1100. if (strchr(altnick, '?')) {
  1101. if (!raltnick[0]) {
  1102. strncpyz(raltnick, altnick, NICKLEN);
  1103. rand_nick(raltnick);
  1104. }
  1105. return raltnick;
  1106. } else
  1107. return altnick;
  1108. }
  1109. static char *altnick_change(ClientData cdata, Tcl_Interp *irp,
  1110. EGG_CONST char *name1,
  1111. EGG_CONST char *name2, int flags)
  1112. {
  1113. /* Always unset raltnick. Will be regenerated when needed. */
  1114. raltnick[0] = 0;
  1115. return NULL;
  1116. }
  1117. static char *traced_serveraddress(ClientData cdata, Tcl_Interp *irp,
  1118. EGG_CONST char *name1,
  1119. EGG_CONST char *name2, int flags)
  1120. {
  1121. char s[1024];
  1122. if (server_online) {
  1123. int servidx = findanyidx(serv);
  1124. #ifdef TLS
  1125. simple_sprintf(s, "%s:%s%u", dcc[servidx].host, dcc[servidx].ssl ? "+" : "",
  1126. dcc[servidx].port);
  1127. #else
  1128. simple_sprintf(s, "%s:%u", dcc[servidx].host, dcc[servidx].port);
  1129. #endif
  1130. } else
  1131. s[0] = 0;
  1132. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1133. if (flags & TCL_TRACE_UNSETS)
  1134. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1135. TCL_TRACE_UNSETS, traced_serveraddress, cdata);
  1136. if (flags & TCL_TRACE_WRITES)
  1137. return "read-only variable";
  1138. return NULL;
  1139. }
  1140. static char *traced_server(ClientData cdata, Tcl_Interp *irp,
  1141. EGG_CONST char *name1,
  1142. EGG_CONST char *name2, int flags)
  1143. {
  1144. char s[1024];
  1145. if (server_online && realservername) {
  1146. int servidx = findanyidx(serv);
  1147. /* return real server name */
  1148. simple_sprintf(s, "%s:%u", realservername, dcc[servidx].port);
  1149. } else
  1150. s[0] = 0;
  1151. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1152. if (flags & TCL_TRACE_UNSETS)
  1153. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1154. TCL_TRACE_UNSETS, traced_server, cdata);
  1155. if (flags & TCL_TRACE_WRITES)
  1156. return "read-only variable";
  1157. return NULL;
  1158. }
  1159. static char *traced_botname(ClientData cdata, Tcl_Interp *irp,
  1160. EGG_CONST char *name1,
  1161. EGG_CONST char *name2, int flags)
  1162. {
  1163. char s[1024];
  1164. simple_sprintf(s, "%s%s%s", botname,
  1165. botuserhost[0] ? "!" : "", botuserhost[0] ? botuserhost : "");
  1166. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1167. if (flags & TCL_TRACE_UNSETS)
  1168. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1169. TCL_TRACE_UNSETS, traced_botname, cdata);
  1170. if (flags & TCL_TRACE_WRITES)
  1171. return "read-only variable";
  1172. return NULL;
  1173. }
  1174. static void do_nettype(void)
  1175. {
  1176. switch (net_type) {
  1177. case NETT_EFNET:
  1178. check_mode_r = 0;
  1179. nick_len = 9;
  1180. break;
  1181. case NETT_IRCNET:
  1182. check_mode_r = 1;
  1183. use_penalties = 1;
  1184. use_fastdeq = 3;
  1185. nick_len = 9;
  1186. simple_sprintf(stackablecmds, "INVITE AWAY VERSION NICK");
  1187. kick_method = 4;
  1188. break;
  1189. case NETT_UNDERNET:
  1190. check_mode_r = 0;
  1191. use_fastdeq = 2;
  1192. nick_len = 12;
  1193. simple_sprintf(stackablecmds,
  1194. "PRIVMSG NOTICE TOPIC PART WHOIS USERHOST USERIP ISON");
  1195. simple_sprintf(stackable2cmds, "USERHOST USERIP ISON");
  1196. break;
  1197. case NETT_DALNET:
  1198. check_mode_r = 0;
  1199. use_fastdeq = 2;
  1200. nick_len = 32;
  1201. simple_sprintf(stackablecmds,
  1202. "PRIVMSG NOTICE PART WHOIS WHOWAS USERHOST ISON WATCH DCCALLOW");
  1203. simple_sprintf(stackable2cmds, "USERHOST ISON WATCH");
  1204. break;
  1205. case NETT_HYBRID_EFNET:
  1206. check_mode_r = 0;
  1207. nick_len = 9;
  1208. break;
  1209. }
  1210. }
  1211. static char *traced_nettype(ClientData cdata, Tcl_Interp *irp,
  1212. EGG_CONST char *name1,
  1213. EGG_CONST char *name2, int flags)
  1214. {
  1215. do_nettype();
  1216. return NULL;
  1217. }
  1218. static char *traced_nicklen(ClientData cdata, Tcl_Interp *irp,
  1219. EGG_CONST char *name1,
  1220. EGG_CONST char *name2, int flags)
  1221. {
  1222. if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
  1223. char s[40];
  1224. egg_snprintf(s, sizeof s, "%d", nick_len);
  1225. Tcl_SetVar2(interp, name1, name2, s, TCL_GLOBAL_ONLY);
  1226. if (flags & TCL_TRACE_UNSETS)
  1227. Tcl_TraceVar(irp, name1, TCL_TRACE_READS | TCL_TRACE_WRITES |
  1228. TCL_TRACE_UNSETS, traced_nicklen, cdata);
  1229. } else {
  1230. EGG_CONST char *cval = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1231. long lval = 0;
  1232. if (cval && Tcl_ExprLong(interp, cval, &lval) != TCL_ERROR) {
  1233. if (lval > NICKMAX)
  1234. lval = NICKMAX;
  1235. nick_len = (int) lval;
  1236. }
  1237. }
  1238. return NULL;
  1239. }
  1240. static tcl_strings my_tcl_strings[] = {
  1241. {"botnick", NULL, 0, STR_PROTECT},
  1242. {"altnick", altnick, NICKMAX, 0},
  1243. {"realname", botrealname, 80, 0},
  1244. {"init-server", initserver, 120, 0},
  1245. {"connect-server", connectserver, 120, 0},
  1246. {"stackable-commands", stackablecmds, 510, 0},
  1247. {"stackable2-commands", stackable2cmds, 510, 0},
  1248. {NULL, NULL, 0, 0}
  1249. };
  1250. static tcl_coups my_tcl_coups[] = {
  1251. {"flood-ctcp", &flud_ctcp_thr, &flud_ctcp_time},
  1252. {"flood-msg", &flud_thr, &flud_time},
  1253. {NULL, NULL, NULL}
  1254. };
  1255. static tcl_ints my_tcl_ints[] = {
  1256. {"server-timeout", &server_timeout, 0},
  1257. {"lowercase-ctcp", &lowercase_ctcp, 0},
  1258. {"server-online", (int *) &server_online, 2},
  1259. {"keep-nick", &keepnick, 0},
  1260. {"check-stoned", &check_stoned, 0},
  1261. {"serverror-quit", &serverror_quit, 0},
  1262. {"max-queue-msg", &maxqmsg, 0},
  1263. {"trigger-on-ignore", &trigger_on_ignore, 0},
  1264. {"answer-ctcp", &answer_ctcp, 0},
  1265. {"server-cycle-wait", (int *) &server_cycle_wait, 0},
  1266. {"default-port", &default_port, 0},
  1267. {"check-mode-r", &check_mode_r, 0},
  1268. {"net-type", &net_type, 0},
  1269. {"ctcp-mode", &ctcp_mode, 0},
  1270. {"double-mode", &double_mode, 0},
  1271. {"double-server", &double_server, 0},
  1272. {"double-help", &double_help, 0},
  1273. {"use-penalties", &use_penalties, 0},
  1274. {"use-fastdeq", &use_fastdeq, 0},
  1275. {"nicklen", &nick_len, 0},
  1276. {"nick-len", &nick_len, 0},
  1277. {"optimize-kicks", &optimize_kicks, 0},
  1278. {"isjuped", &nick_juped, 0},
  1279. {"stack-limit", &stack_limit, 0},
  1280. {"exclusive-binds", &exclusive_binds, 0},
  1281. {"msg-rate", &msgrate, 0},
  1282. #ifdef TLS
  1283. {"ssl-verify-server", &tls_vfyserver, 0},
  1284. #endif
  1285. {NULL, NULL, 0}
  1286. };
  1287. /*
  1288. * Tcl variable trace functions
  1289. */
  1290. /* Read or write the server list.
  1291. */
  1292. static char *tcl_eggserver(ClientData cdata, Tcl_Interp *irp,
  1293. EGG_CONST char *name1,
  1294. EGG_CONST char *name2, int flags)
  1295. {
  1296. int lc, code, i;
  1297. char x[1024];
  1298. EGG_CONST char **list, *slist;
  1299. struct server_list *q;
  1300. Tcl_DString ds;
  1301. if (flags & (TCL_TRACE_READS | TCL_TRACE_UNSETS)) {
  1302. /* Create server list */
  1303. Tcl_DStringInit(&ds);
  1304. for (q = serverlist; q; q = q->next) {
  1305. #ifdef TLS
  1306. egg_snprintf(x, sizeof x, "%s%s%s:%s%d%s%s %s", strchr(q->name, ':') ?
  1307. "[" : "", q->name, strchr(q->name, ':') ? "]" : "",
  1308. q->ssl ? "+" : "", q->port ? q->port : default_port,
  1309. q->pass ? ":" : "", q->pass ? q->pass : "",
  1310. q->realname ? q->realname : "");
  1311. #else
  1312. egg_snprintf(x, sizeof x, "%s%s%s:%d%s%s %s", strchr(q->name, ':') ?
  1313. "[" : "", q->name, strchr(q->name, ':') ? "]" : "",
  1314. q->port ? q->port : default_port, q->pass ? ":" : "",
  1315. q->pass ? q->pass : "", q->realname ? q->realname : "");
  1316. #endif
  1317. Tcl_DStringAppendElement(&ds, x);
  1318. }
  1319. slist = Tcl_DStringValue(&ds);
  1320. Tcl_SetVar2(interp, name1, name2, slist, TCL_GLOBAL_ONLY);
  1321. Tcl_DStringFree(&ds);
  1322. } else { /* TCL_TRACE_WRITES */
  1323. if (serverlist) {
  1324. clearq(serverlist);
  1325. serverlist = NULL;
  1326. }
  1327. slist = Tcl_GetVar2(interp, name1, name2, TCL_GLOBAL_ONLY);
  1328. if (slist != NULL) {
  1329. code = Tcl_SplitList(interp, slist, &lc, &list);
  1330. if (code == TCL_ERROR)
  1331. return "variable must be a list";
  1332. for (i = 0; i < lc && i < 50; i++)
  1333. add_server((char *) list[i]);
  1334. /* Tricky way to make the bot reset its server pointers
  1335. * perform part of a '.jump <current-server>':
  1336. */
  1337. if (server_online) {
  1338. int servidx = findanyidx(serv);
  1339. curserv = -1;
  1340. if (serverlist)
  1341. next_server(&curserv, dcc[servidx].host, &dcc[servidx].port, "");
  1342. }
  1343. Tcl_Free((char *) list);
  1344. }
  1345. }
  1346. return NULL;
  1347. }
  1348. /* Trace the servers */
  1349. #define tcl_traceserver(name, ptr) \
  1350. Tcl_TraceVar(interp, name, TCL_TRACE_READS | TCL_TRACE_WRITES | \
  1351. TCL_TRACE_UNSETS, tcl_eggserver, (ClientData) ptr)
  1352. #define tcl_untraceserver(name, ptr) \
  1353. Tcl_UntraceVar(interp, name, TCL_TRACE_READS | TCL_TRACE_WRITES | \
  1354. TCL_TRACE_UNSETS, tcl_eggserver, (ClientData) ptr)
  1355. /*
  1356. * CTCP DCC CHAT functions
  1357. */
  1358. static void dcc_chat_hostresolved(int);
  1359. /* This only handles CHAT requests, otherwise it's handled in filesys.
  1360. */
  1361. static int ctcp_DCC_CHAT(char *nick, char *from, char *handle,
  1362. char *object, char *keyword, char *text)
  1363. {
  1364. char *action, *param, *ip, *prt, buf[512], *msg = buf;
  1365. int i;
  1366. #ifdef TLS
  1367. int ssl = 0;
  1368. #endif
  1369. struct userrec *u = get_user_by_handle(userlist, handle);
  1370. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
  1371. strcpy(msg, text);
  1372. action = newsplit(&msg);
  1373. param = newsplit(&msg);
  1374. ip = newsplit(&msg);
  1375. prt = newsplit(&msg);
  1376. #ifdef TLS
  1377. if (egg_strcasecmp(action, "CHAT") || egg_strcasecmp(object, botname) || !u)
  1378. {
  1379. if (!egg_strcasecmp(action, "SCHAT"))
  1380. ssl = 1;
  1381. else
  1382. return 0;
  1383. }
  1384. #else
  1385. if (egg_strcasecmp(action, "CHAT") || egg_strcasecmp(object, botname) || !u)
  1386. return 0;
  1387. #endif
  1388. get_user_flagrec(u, &fr, 0);
  1389. if (dcc_total == max_dcc && increase_socks_max()) {
  1390. if (!quiet_reject)
  1391. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_TOOMANYDCCS1);
  1392. putlog(LOG_MISC, "*", DCC_TOOMANYDCCS2, "CHAT", param, nick, from);
  1393. } else if (!(glob_party(fr) || (!require_p && chan_op(fr)))) {
  1394. if (glob_xfer(fr))
  1395. return 0; /* Allow filesys to pick up the chat */
  1396. if (!quiet_reject)
  1397. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED2);
  1398. putlog(LOG_MISC, "*", "%s: %s!%s", DCC_REFUSED, nick, from);
  1399. } else if (u_pass_match(u, "-")) {
  1400. if (!quiet_reject)
  1401. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED3);
  1402. putlog(LOG_MISC, "*", "%s: %s!%s", DCC_REFUSED4, nick, from);
  1403. } else if (atoi(prt) < 1024 || atoi(prt) > 65535) {
  1404. /* Invalid port */
  1405. if (!quiet_reject)
  1406. dprintf(DP_HELP, "NOTICE %s :%s (invalid port)\n", nick,
  1407. DCC_CONNECTFAILED1);
  1408. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED3, nick, from);
  1409. } else {
  1410. if (!sanitycheck_dcc(nick, from, ip, prt))
  1411. return 1;
  1412. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1413. if (i < 0) {
  1414. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", nick, ip);
  1415. return 1;
  1416. }
  1417. #ifdef TLS
  1418. dcc[i].ssl = ssl;
  1419. #endif
  1420. dcc[i].port = atoi(prt);
  1421. (void) setsockname(&dcc[i].sockname, ip, dcc[i].port, 0);
  1422. dcc[i].u.dns->ip = &dcc[i].sockname;
  1423. dcc[i].sock = -1;
  1424. strcpy(dcc[i].nick, u->handle);
  1425. strcpy(dcc[i].host, from);
  1426. dcc[i].timeval = now;
  1427. dcc[i].user = u;
  1428. dcc[i].u.dns->dns_type = RES_HOSTBYIP;
  1429. dcc[i].u.dns->dns_success = dcc_chat_hostresolved;
  1430. dcc[i].u.dns->dns_failure = dcc_chat_hostresolved;
  1431. dcc[i].u.dns->type = &DCC_CHAT_PASS;
  1432. dcc_dnshostbyip(&dcc[i].sockname);
  1433. }
  1434. return 1;
  1435. }
  1436. #ifdef TLS
  1437. int dcc_chat_sslcb(int sock)
  1438. {
  1439. int idx;
  1440. idx = findanyidx(sock);
  1441. if ((idx >= 0) && dcc_fingerprint(idx))
  1442. dprintf(idx, "%s\n", DCC_ENTERPASS);
  1443. return 0;
  1444. }
  1445. #endif
  1446. static void dcc_chat_hostresolved(int i)
  1447. {
  1448. char buf[512];
  1449. struct flag_record fr = { FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0 };
  1450. egg_snprintf(buf, sizeof buf, "%d", dcc[i].port);
  1451. if (!hostsanitycheck_dcc(dcc[i].nick, dcc[i].host, &dcc[i].sockname,
  1452. dcc[i].u.dns->host, buf)) {
  1453. lostdcc(i);
  1454. return;
  1455. }
  1456. buf[0] = 0;
  1457. dcc[i].sock = getsock(dcc[i].sockname.family, 0);
  1458. if (dcc[i].sock < 0 || open_telnet_raw(dcc[i].sock, &dcc[i].sockname) < 0)
  1459. snprintf(buf, sizeof buf, strerror(errno));
  1460. #ifdef TLS
  1461. else if (dcc[i].ssl && ssl_handshake(dcc[i].sock, TLS_CONNECT, tls_vfydcc,
  1462. LOG_MISC, dcc[i].host, &dcc_chat_sslcb))
  1463. snprintf(buf, sizeof buf, "TLS negotiation error");
  1464. #endif
  1465. if (buf[0]) {
  1466. if (!quiet_reject)
  1467. dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[i].nick,
  1468. DCC_CONNECTFAILED1, buf);
  1469. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED2,
  1470. dcc[i].nick, dcc[i].host);
  1471. putlog(LOG_MISC, "*", " (%s)", buf);
  1472. killsock(dcc[i].sock);
  1473. lostdcc(i);
  1474. } else {
  1475. changeover_dcc(i, &DCC_CHAT_PASS, sizeof(struct chat_info));
  1476. dcc[i].status = STAT_ECHO;
  1477. get_user_flagrec(dcc[i].user, &fr, 0);
  1478. if (glob_party(fr))
  1479. dcc[i].status |= STAT_PARTY;
  1480. strcpy(dcc[i].u.chat->con_chan, (chanset) ? chanset->dname : "*");
  1481. dcc[i].timeval = now;
  1482. /* Ok, we're satisfied with them now: attempt the connect */
  1483. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", dcc[i].nick,
  1484. dcc[i].host);
  1485. #ifdef TLS
  1486. /* For SSL connections, the handshake callback will determine
  1487. if we should request a password */
  1488. if (!dcc[i].ssl)
  1489. #endif
  1490. dprintf(i, "%s\n", DCC_ENTERPASS);
  1491. }
  1492. return;
  1493. }
  1494. /*
  1495. * Server timer functions
  1496. */
  1497. static void server_secondly()
  1498. {
  1499. if (cycle_time)
  1500. cycle_time--;
  1501. deq_msg();
  1502. if (!resolvserv && serv < 0)
  1503. connect_server();
  1504. }
  1505. static void server_5minutely()
  1506. {
  1507. if (check_stoned && server_online) {
  1508. if (lastpingcheck && (now - lastpingcheck >= 240)) {
  1509. /* Still waiting for activity, requested longer than 4 minutes ago.
  1510. * Server is probably stoned.
  1511. */
  1512. int servidx = findanyidx(serv);
  1513. disconnect_server(servidx);
  1514. lostdcc(servidx);
  1515. putlog(LOG_SERV, "*", IRC_SERVERSTONED);
  1516. } else if (!trying_server) {
  1517. /* Check for server being stoned. */
  1518. dprintf(DP_MODE, "PING :%li\n", now);
  1519. lastpingcheck = now;
  1520. }
  1521. }
  1522. }
  1523. static void server_prerehash()
  1524. {
  1525. strcpy(oldnick, botname);
  1526. }
  1527. static void server_postrehash()
  1528. {
  1529. strncpyz(botname, origbotname, NICKLEN);
  1530. if (!botname[0])
  1531. fatal("NO BOT NAME.", 0);
  1532. if (serverlist == NULL)
  1533. fatal("NO SERVER.", 0);
  1534. if (oldnick[0] && !rfc_casecmp(oldnick, botname) &&
  1535. !rfc_casecmp(oldnick, get_altbotnick())) {
  1536. /* Change botname back, don't be premature. */
  1537. strcpy(botname, oldnick);
  1538. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  1539. }
  1540. /* Change botname back incase we were using altnick previous to rehash. */
  1541. else if (oldnick[0])
  1542. strcpy(botname, oldnick);
  1543. }
  1544. static void server_die()
  1545. {
  1546. cycle_time = 100;
  1547. if (server_online) {
  1548. dprintf(-serv, "QUIT :%s\n", quit_msg[0] ? quit_msg : "");
  1549. sleep(3); /* Give the server time to understand */
  1550. }
  1551. nuke_server(NULL);
  1552. }
  1553. static void msgq_clear(struct msgq_head *qh)
  1554. {
  1555. register struct msgq *q, *qq;
  1556. for (q = qh->head; q; q = qq) {
  1557. qq = q->next;
  1558. nfree(q->msg);
  1559. nfree(q);
  1560. }
  1561. qh->head = qh->last = NULL;
  1562. qh->tot = qh->warned = 0;
  1563. }
  1564. static int msgq_expmem(struct msgq_head *qh)
  1565. {
  1566. register int tot = 0;
  1567. register struct msgq *m;
  1568. for (m = qh->head; m; m = m->next) {
  1569. tot += m->len + 1;
  1570. tot += sizeof(struct msgq);
  1571. }
  1572. return tot;
  1573. }
  1574. static int server_expmem()
  1575. {
  1576. int tot = 0;
  1577. struct server_list *s = serverlist;
  1578. for (; s; s = s->next) {
  1579. if (s->name)
  1580. tot += strlen(s->name) + 1;
  1581. if (s->pass)
  1582. tot += strlen(s->pass) + 1;
  1583. if (s->realname)
  1584. tot += strlen(s->realname) + 1;
  1585. tot += sizeof(struct server_list);
  1586. }
  1587. if (realservername)
  1588. tot += strlen(realservername) + 1;
  1589. tot += msgq_expmem(&mq) + msgq_expmem(&hq) + msgq_expmem(&modeq);
  1590. return tot;
  1591. }
  1592. static void server_report(int idx, int details)
  1593. {
  1594. char s1[64], s[128];
  1595. int servidx;
  1596. if (server_online) {
  1597. dprintf(idx, " Online as: %s%s%s (%s)\n", botname, botuserhost[0] ?
  1598. "!" : "", botuserhost[0] ? botuserhost : "", botrealname);
  1599. if (nick_juped)
  1600. dprintf(idx, " NICK IS JUPED: %s%s\n", origbotname,
  1601. keepnick ? " (trying)" : "");
  1602. daysdur(now, server_online, s1);
  1603. egg_snprintf(s, sizeof s, "(connected %s)", s1);
  1604. if (server_lag && !lastpingcheck) {
  1605. if (server_lag == -1)
  1606. egg_snprintf(s1, sizeof s1, " (bad pong replies)");
  1607. else
  1608. egg_snprintf(s1, sizeof s1, " (lag: %ds)", server_lag);
  1609. strcat(s, s1);
  1610. }
  1611. }
  1612. if ((trying_server || server_online) &&
  1613. ((servidx = findanyidx(serv)) != -1)) {
  1614. #ifdef TLS
  1615. dprintf(idx, " Server [%s]:%s%d %s\n", dcc[servidx].host,
  1616. dcc[servidx].ssl ? "+" : "", dcc[servidx].port, trying_server ?
  1617. "(trying)" : s);
  1618. #else
  1619. dprintf(idx, " Server [%s]:%d %s\n", dcc[servidx].host,
  1620. dcc[servidx].port, trying_server ? "(trying)" : s);
  1621. #endif
  1622. } else
  1623. dprintf(idx, " %s\n", IRC_NOSERVER);
  1624. if (modeq.tot)
  1625. dprintf(idx, " %s %d%% (%d msgs)\n", IRC_MODEQUEUE,
  1626. (int) ((float) (modeq.tot * 100.0) / (float) maxqmsg),
  1627. (int) modeq.tot);
  1628. if (mq.tot)
  1629. dprintf(idx, " %s %d%% (%d msgs)\n", IRC_SERVERQUEUE,
  1630. (int) ((float) (mq.tot * 100.0) / (float) maxqmsg), (int) mq.tot);
  1631. if (hq.tot)
  1632. dprintf(idx, " %s %d%% (%d msgs)\n", IRC_HELPQUEUE,
  1633. (int) ((float) (hq.tot * 100.0) / (float) maxqmsg), (int) hq.tot);
  1634. if

Large files files are truncated, but you can click here to view the full file