server.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138
  1. #ifdef LEAF
  2. /*
  3. * server.c -- part of server.mod
  4. * basic irc server support
  5. *
  6. */
  7. #include "src/common.h"
  8. #include "src/cfg.h"
  9. #include "src/botmsg.h"
  10. #include "src/rfc1459.h"
  11. #include "src/settings.h"
  12. #include "src/tclhash.h"
  13. #include "src/users.h"
  14. #include "src/userrec.h"
  15. #include "src/main.h"
  16. #include "src/response.h"
  17. #include "src/misc.h"
  18. #include "src/chanprog.h"
  19. #include "src/net.h"
  20. #include "src/auth.h"
  21. #include "src/adns.h"
  22. #include "src/socket.h"
  23. #include "src/egg_timer.h"
  24. #include "src/mod/channels.mod/channels.h"
  25. #include "src/mod/irc.mod/irc.h"
  26. #include "server.h"
  27. #include <stdarg.h>
  28. bool strict_servernames;
  29. bool checked_hostmask; /* Used in request_op()/check_hostmask() cleared on connect */
  30. int ctcp_mode;
  31. int serv = -1; /* sock # of server currently */
  32. int servidx = -1; /* idx of server */
  33. char newserver[121] = ""; /* new server? */
  34. port_t newserverport; /* new server port? */
  35. char newserverpass[121] = ""; /* new server password? */
  36. static char serverpass[121] = "";
  37. static time_t trying_server; /* trying to connect to a server right now? */
  38. static int curserv; /* current position in server list: */
  39. int flud_thr; /* msg flood threshold */
  40. time_t flud_time; /* msg flood time */
  41. int flud_ctcp_thr; /* ctcp flood threshold */
  42. time_t flud_ctcp_time; /* ctcp flood time */
  43. char botuserhost[UHOSTLEN] = ""; /* bot's user@host (refreshed whenever the bot joins a channel) */
  44. /* may not be correct user@host BUT it's how the server sees it */
  45. static bool keepnick = 1; /* keep trying to regain my intended
  46. nickname? */
  47. static bool nick_juped = 0; /* True if origbotname is juped(RPL437) (dw) */
  48. bool quiet_reject; /* Quietly reject dcc chat or sends from
  49. users without access? */
  50. static time_t waiting_for_awake; /* set when i unidle myself, cleared when
  51. i get the response */
  52. time_t server_online; /* server connection time */
  53. char botrealname[121] = ""; /* realname of bot */
  54. static time_t server_timeout; /* server timeout for connecting */
  55. struct server_list *serverlist = NULL; /* old-style queue, still used by
  56. server list */
  57. time_t cycle_time; /* cycle time till next server connect */
  58. port_t default_port; /* default IRC port */
  59. static char oldnick[NICKLEN] = ""; /* previous nickname *before* rehash */
  60. bool trigger_on_ignore; /* trigger bindings if user is ignored ? */
  61. int answer_ctcp; /* answer how many stacked ctcp's ? */
  62. static bool check_mode_r; /* check for IRCNET +r modes */
  63. static int net_type;
  64. static bool resolvserv; /* in the process of resolving a server host */
  65. static bool double_mode; /* allow a msgs to be twice in a queue? */
  66. static bool double_server;
  67. static bool double_help;
  68. static bool double_warned;
  69. static time_t lastpingtime; /* IRCNet LAGmeter support -- drummer */
  70. static char stackablecmds[511] = "";
  71. static char stackable2cmds[511] = "";
  72. static time_t last_time;
  73. static bool use_penalties;
  74. static int use_fastdeq;
  75. size_t nick_len = 9; /* Maximal nick length allowed on the network. */
  76. static void empty_msgq(void);
  77. static void next_server(int *, char *, port_t *, char *);
  78. static void disconnect_server(int, int);
  79. static int calc_penalty(char *);
  80. static bool fast_deq(int);
  81. static char *splitnicks(char **);
  82. static void msgq_clear(struct msgq_head *qh);
  83. static int stack_limit;
  84. /* New bind tables. */
  85. static bind_table_t *BT_raw = NULL, *BT_msg = NULL;
  86. bind_table_t *BT_ctcr = NULL, *BT_ctcp = NULL, *BT_msgc = NULL;
  87. #include "servmsg.c"
  88. #define MAXPENALTY 10
  89. /* Number of seconds to wait between transmitting queued lines to the server
  90. * lower this value at your own risk. ircd is known to start flood control
  91. * at 512 bytes/2 seconds.
  92. */
  93. #define msgrate 2
  94. /* Maximum messages to store in each queue. */
  95. static int maxqmsg;
  96. static struct msgq_head mq, hq, modeq;
  97. static int burst;
  98. #include "cmdsserv.c"
  99. /*
  100. * Bot server queues
  101. */
  102. /* Called periodically to shove out another queued item.
  103. *
  104. * 'mode' queue gets priority now.
  105. *
  106. * Most servers will allow 'busts' of upto 5 msgs, so let's put something
  107. * in to support flushing modeq a little faster if possible.
  108. * Will send upto 4 msgs from modeq, and then send 1 msg every time
  109. * it will *not* send anything from hq until the 'burst' value drops
  110. * down to 0 again (allowing a sudden mq flood to sneak through).
  111. */
  112. static void deq_msg()
  113. {
  114. bool ok = 0;
  115. /* now < last_time tested 'cause clock adjustments could mess it up */
  116. if ((now - last_time) >= msgrate || now < (last_time - 90)) {
  117. last_time = now;
  118. if (burst > 0)
  119. burst--;
  120. ok = 1;
  121. }
  122. if (serv < 0)
  123. return;
  124. struct msgq *q = NULL;
  125. /* Send upto 4 msgs to server if the *critical queue* has anything in it */
  126. if (modeq.head) {
  127. while (modeq.head && (burst < 4) && ((last_time - now) < MAXPENALTY)) {
  128. if (!modeq.head)
  129. break;
  130. if (fast_deq(DP_MODE)) {
  131. burst++;
  132. continue;
  133. }
  134. tputs(serv, modeq.head->msg, modeq.head->len);
  135. if (debug_output) {
  136. modeq.head->msg[strlen(modeq.head->msg) - 1] = 0; /* delete the "\n" */
  137. putlog(LOG_SRVOUT, "@", "[m->] %s", modeq.head->msg);
  138. }
  139. modeq.tot--;
  140. last_time += calc_penalty(modeq.head->msg);
  141. q = modeq.head->next;
  142. free(modeq.head->msg);
  143. free(modeq.head);
  144. modeq.head = q;
  145. burst++;
  146. }
  147. if (!modeq.head)
  148. modeq.last = 0;
  149. return;
  150. }
  151. /* Send something from the normal msg q even if we're slightly bursting */
  152. if (burst > 1)
  153. return;
  154. if (mq.head) {
  155. burst++;
  156. if (fast_deq(DP_SERVER))
  157. return;
  158. tputs(serv, mq.head->msg, mq.head->len);
  159. if (debug_output) {
  160. mq.head->msg[strlen(mq.head->msg) - 1] = 0; /* delete the "\n" */
  161. putlog(LOG_SRVOUT, "@", "[s->] %s", mq.head->msg);
  162. }
  163. mq.tot--;
  164. last_time += calc_penalty(mq.head->msg);
  165. q = mq.head->next;
  166. free(mq.head->msg);
  167. free(mq.head);
  168. mq.head = q;
  169. if (!mq.head)
  170. mq.last = NULL;
  171. return;
  172. }
  173. /* Never send anything from the help queue unless everything else is
  174. * finished.
  175. */
  176. if (!hq.head || burst || !ok)
  177. return;
  178. if (fast_deq(DP_HELP))
  179. return;
  180. tputs(serv, hq.head->msg, hq.head->len);
  181. if (debug_output) {
  182. hq.head->msg[strlen(hq.head->msg) - 1] = 0; /* delete the "\n" */
  183. putlog(LOG_SRVOUT, "@", "[h->] %s", hq.head->msg);
  184. }
  185. hq.tot--;
  186. last_time += calc_penalty(hq.head->msg);
  187. q = hq.head->next;
  188. free(hq.head->msg);
  189. free(hq.head);
  190. hq.head = q;
  191. if (!hq.head)
  192. hq.last = NULL;
  193. }
  194. static int calc_penalty(char * msg)
  195. {
  196. if (!use_penalties && net_type != NETT_UNDERNET && net_type != NETT_HYBRID_EFNET)
  197. return 0;
  198. char *cmd = NULL, *par1 = NULL, *par2 = NULL, *par3 = NULL;
  199. register int penalty, i, ii;
  200. if (msg[strlen(msg) - 1] == '\n')
  201. msg[strlen(msg) - 1] = '\0';
  202. cmd = newsplit(&msg);
  203. if (msg)
  204. i = strlen(msg);
  205. else
  206. i = strlen(cmd);
  207. last_time -= 2; /* undo eggdrop standard flood prot */
  208. if (net_type == NETT_UNDERNET || net_type == NETT_HYBRID_EFNET) {
  209. last_time += (2 + i / 120);
  210. return 0;
  211. }
  212. penalty = (1 + i / 100);
  213. if (!egg_strcasecmp(cmd, "KICK")) {
  214. par1 = newsplit(&msg); /* channel */
  215. par2 = newsplit(&msg); /* victim(s) */
  216. par3 = splitnicks(&par2);
  217. penalty++;
  218. while (strlen(par3) > 0) {
  219. par3 = splitnicks(&par2);
  220. penalty++;
  221. }
  222. ii = penalty;
  223. par3 = splitnicks(&par1);
  224. while (strlen(par1) > 0) {
  225. par3 = splitnicks(&par1);
  226. penalty += ii;
  227. }
  228. } else if (!egg_strcasecmp(cmd, "MODE")) {
  229. i = 0;
  230. par1 = newsplit(&msg); /* channel */
  231. par2 = newsplit(&msg); /* mode(s) */
  232. if (!strlen(par2))
  233. i++;
  234. while (strlen(par2) > 0) {
  235. if (strchr("ntimps", par2[0]))
  236. i += 3;
  237. else if (!strchr("+-", par2[0]))
  238. i += 1;
  239. par2++;
  240. }
  241. while (strlen(msg) > 0) {
  242. newsplit(&msg);
  243. i += 2;
  244. }
  245. ii = 0;
  246. while (strlen(par1) > 0) {
  247. splitnicks(&par1);
  248. ii++;
  249. }
  250. penalty += (ii * i);
  251. } else if (!egg_strcasecmp(cmd, "TOPIC")) {
  252. penalty++;
  253. par1 = newsplit(&msg); /* channel */
  254. par2 = newsplit(&msg); /* topic */
  255. if (strlen(par2) > 0) { /* topic manipulation => 2 penalty points */
  256. penalty += 2;
  257. par3 = splitnicks(&par1);
  258. while (strlen(par1) > 0) {
  259. par3 = splitnicks(&par1);
  260. penalty += 2;
  261. }
  262. }
  263. } else if (!egg_strcasecmp(cmd, "PRIVMSG") ||
  264. !egg_strcasecmp(cmd, "NOTICE")) {
  265. par1 = newsplit(&msg); /* channel(s)/nick(s) */
  266. /* Add one sec penalty for each recipient */
  267. while (strlen(par1) > 0) {
  268. splitnicks(&par1);
  269. penalty++;
  270. }
  271. } else if (!egg_strcasecmp(cmd, "WHO")) {
  272. par1 = newsplit(&msg); /* masks */
  273. par2 = par1;
  274. while (strlen(par1) > 0) {
  275. par2 = splitnicks(&par1);
  276. if (strlen(par2) > 4) /* long WHO-masks receive less penalty */
  277. penalty += 3;
  278. else
  279. penalty += 5;
  280. }
  281. } else if (!egg_strcasecmp(cmd, "AWAY")) {
  282. if (strlen(msg) > 0)
  283. penalty += 2;
  284. else
  285. penalty += 1;
  286. } else if (!egg_strcasecmp(cmd, "INVITE")) {
  287. /* Successful invite receives 2 or 3 penalty points. Let's go
  288. * with the maximum.
  289. */
  290. penalty += 3;
  291. } else if (!egg_strcasecmp(cmd, "JOIN")) {
  292. penalty += 2;
  293. } else if (!egg_strcasecmp(cmd, "PART")) {
  294. penalty += 4;
  295. } else if (!egg_strcasecmp(cmd, "VERSION")) {
  296. penalty += 2;
  297. } else if (!egg_strcasecmp(cmd, "TIME")) {
  298. penalty += 2;
  299. } else if (!egg_strcasecmp(cmd, "TRACE")) {
  300. penalty += 2;
  301. } else if (!egg_strcasecmp(cmd, "NICK")) {
  302. penalty += 3;
  303. } else if (!egg_strcasecmp(cmd, "ISON")) {
  304. penalty += 1;
  305. } else if (!egg_strcasecmp(cmd, "WHOIS")) {
  306. penalty += 2;
  307. } else if (!egg_strcasecmp(cmd, "DNS")) {
  308. penalty += 2;
  309. } else
  310. penalty++; /* just add standard-penalty */
  311. /* Shouldn't happen, but you never know... */
  312. if (penalty > 99)
  313. penalty = 99;
  314. if (penalty < 2) {
  315. putlog(LOG_SRVOUT, "*", "Penalty < 2sec, that's impossible!");
  316. penalty = 2;
  317. }
  318. if (debug_output && penalty != 0)
  319. putlog(LOG_SRVOUT, "*", "Adding penalty: %i", penalty);
  320. return penalty;
  321. }
  322. char *splitnicks(char **rest)
  323. {
  324. if (!rest)
  325. return *rest = "";
  326. register char *o = *rest, *r = NULL;
  327. while (*o == ' ')
  328. o++;
  329. r = o;
  330. while (*o && *o != ',')
  331. o++;
  332. if (*o)
  333. *o++ = 0;
  334. *rest = o;
  335. return r;
  336. }
  337. static bool fast_deq(int which)
  338. {
  339. if (!use_fastdeq)
  340. return 0;
  341. struct msgq_head *h = NULL;
  342. struct msgq *m = NULL, *nm = NULL;
  343. char msgstr[511] = "", nextmsgstr[511] = "", tosend[511] = "", victims[511] = "", stackable[511] = "",
  344. *msg = NULL, *nextmsg = NULL, *cmd = NULL, *nextcmd = NULL, *to = NULL, *nextto = NULL, *stckbl = NULL;
  345. int cmd_count = 0, stack_method = 1;
  346. size_t len;
  347. bool found = 0, doit = 0;
  348. switch (which) {
  349. case DP_MODE:
  350. h = &modeq;
  351. break;
  352. case DP_SERVER:
  353. h = &mq;
  354. break;
  355. case DP_HELP:
  356. h = &hq;
  357. break;
  358. default:
  359. return 0;
  360. }
  361. m = h->head;
  362. strncpyz(msgstr, m->msg, sizeof msgstr);
  363. msg = msgstr;
  364. cmd = newsplit(&msg);
  365. if (use_fastdeq > 1) {
  366. strncpyz(stackable, stackablecmds, sizeof stackable);
  367. stckbl = stackable;
  368. while (strlen(stckbl) > 0)
  369. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  370. found = 1;
  371. break;
  372. }
  373. /* If use_fastdeq is 2, only commands in the list should be stacked. */
  374. if (use_fastdeq == 2 && !found)
  375. return 0;
  376. /* If use_fastdeq is 3, only commands that are _not_ in the list
  377. * should be stacked.
  378. */
  379. if (use_fastdeq == 3 && found)
  380. return 0;
  381. /* we check for the stacking method (default=1) */
  382. strncpyz(stackable, stackable2cmds, sizeof stackable);
  383. stckbl = stackable;
  384. while (strlen(stckbl) > 0)
  385. if (!egg_strcasecmp(newsplit(&stckbl), cmd)) {
  386. stack_method = 2;
  387. break;
  388. }
  389. }
  390. to = newsplit(&msg);
  391. len = strlen(to);
  392. if (to[len - 1] == '\n')
  393. to[len -1] = 0;
  394. simple_sprintf(victims, "%s", to);
  395. while (m) {
  396. nm = m->next;
  397. if (!nm)
  398. break;
  399. strncpyz(nextmsgstr, nm->msg, sizeof nextmsgstr);
  400. nextmsg = nextmsgstr;
  401. nextcmd = newsplit(&nextmsg);
  402. nextto = newsplit(&nextmsg);
  403. len = strlen(nextto);
  404. if (nextto[len - 1] == '\n')
  405. nextto[len - 1] = 0;
  406. if ( strcmp(to, nextto) /* we don't stack to the same recipients */
  407. && !strcmp(cmd, nextcmd) && !strcmp(msg, nextmsg)
  408. && ((strlen(cmd) + strlen(victims) + strlen(nextto)
  409. + strlen(msg) + 2) < 510)
  410. && (!stack_limit || cmd_count < stack_limit - 1)) {
  411. cmd_count++;
  412. if (stack_method == 1)
  413. simple_sprintf(victims, "%s,%s", victims, nextto);
  414. else
  415. simple_sprintf(victims, "%s %s", victims, nextto);
  416. doit = 1;
  417. m->next = nm->next;
  418. if (!nm->next)
  419. h->last = m;
  420. free(nm->msg);
  421. free(nm);
  422. h->tot--;
  423. } else
  424. m = m->next;
  425. }
  426. if (doit) {
  427. simple_sprintf(tosend, "%s %s %s", cmd, victims, msg);
  428. len = strlen(tosend);
  429. tosend[len - 1] = '\n';
  430. tputs(serv, tosend, len);
  431. m = h->head->next;
  432. free(h->head->msg);
  433. free(h->head);
  434. h->head = m;
  435. if (!h->head)
  436. h->last = 0;
  437. h->tot--;
  438. if (debug_output) {
  439. tosend[len - 1] = 0;
  440. switch (which) {
  441. case DP_MODE:
  442. putlog(LOG_SRVOUT, "*", "[m=>] %s", tosend);
  443. break;
  444. case DP_SERVER:
  445. putlog(LOG_SRVOUT, "*", "[s=>] %s", tosend);
  446. break;
  447. case DP_HELP:
  448. putlog(LOG_SRVOUT, "*", "[h=>] %s", tosend);
  449. break;
  450. }
  451. }
  452. last_time += calc_penalty(tosend);
  453. return 1;
  454. }
  455. return 0;
  456. }
  457. /* Clean out the msg queues (like when changing servers).
  458. */
  459. static void empty_msgq()
  460. {
  461. msgq_clear(&modeq);
  462. msgq_clear(&mq);
  463. msgq_clear(&hq);
  464. burst = 0;
  465. }
  466. /* Use when sending msgs... will spread them out so there's no flooding.
  467. */
  468. void queue_server(int which, char *buf, int len)
  469. {
  470. /* Don't even BOTHER if there's no server online. */
  471. if (serv < 0)
  472. return;
  473. struct msgq_head *h = NULL, tempq;
  474. struct msgq *q = NULL, *tq = NULL, *tqq = NULL;
  475. int doublemsg = 0, qnext = 0;
  476. /* No queue for PING and PONG - drummer */
  477. if (!egg_strncasecmp(buf, "PING", 4) || !egg_strncasecmp(buf, "PONG", 4)) {
  478. if (buf[1] == 'I' || buf[1] == 'i')
  479. lastpingtime = now; /* lagmeter */
  480. tputs(serv, buf, len);
  481. if (debug_output) {
  482. if (buf[len - 1] == '\n')
  483. buf[len - 1] = 0;
  484. putlog(LOG_SRVOUT, "@", "[m->] %s", buf);
  485. }
  486. return;
  487. }
  488. switch (which) {
  489. case DP_MODE_NEXT:
  490. qnext = 1;
  491. /* Fallthrough */
  492. case DP_MODE:
  493. h = &modeq;
  494. tempq = modeq;
  495. if (double_mode)
  496. doublemsg = 1;
  497. break;
  498. case DP_SERVER_NEXT:
  499. qnext = 1;
  500. /* Fallthrough */
  501. case DP_SERVER:
  502. h = &mq;
  503. tempq = mq;
  504. if (double_server)
  505. doublemsg = 1;
  506. break;
  507. case DP_HELP_NEXT:
  508. qnext = 1;
  509. /* Fallthrough */
  510. case DP_HELP:
  511. h = &hq;
  512. tempq = hq;
  513. if (double_help)
  514. doublemsg = 1;
  515. break;
  516. default:
  517. putlog(LOG_MISC, "*", "!!! queuing unknown type to server!!");
  518. return;
  519. }
  520. if (h->tot < maxqmsg) {
  521. /* Don't queue msg if it's already queued? */
  522. if (!doublemsg)
  523. for (tq = tempq.head; tq; tq = tqq) {
  524. tqq = tq->next;
  525. if (!egg_strcasecmp(tq->msg, buf)) {
  526. if (!double_warned) {
  527. if (buf[len - 1] == '\n')
  528. buf[len - 1] = 0;
  529. debug1("msg already queued. skipping: %s", buf);
  530. double_warned = 1;
  531. }
  532. return;
  533. }
  534. }
  535. q = (struct msgq *) calloc(1, sizeof(struct msgq));
  536. if (qnext)
  537. q->next = h->head;
  538. else
  539. q->next = NULL;
  540. if (h->head) {
  541. if (!qnext)
  542. h->last->next = q;
  543. } else
  544. h->head = q;
  545. if (qnext)
  546. h->head = q;
  547. h->last = q;
  548. q->len = len;
  549. q->msg = (char *) calloc(1, len + 1);
  550. strncpyz(q->msg, buf, len + 1);
  551. h->tot++;
  552. h->warned = 0;
  553. double_warned = 0;
  554. } else {
  555. if (!h->warned) {
  556. switch (which) {
  557. case DP_MODE_NEXT:
  558. /* Fallthrough */
  559. case DP_MODE:
  560. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM MODE QUEUE");
  561. break;
  562. case DP_SERVER_NEXT:
  563. /* Fallthrough */
  564. case DP_SERVER:
  565. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM SERVER QUEUE");
  566. break;
  567. case DP_HELP_NEXT:
  568. /* Fallthrough */
  569. case DP_HELP:
  570. putlog(LOG_MISC, "*", "!!! OVER MAXIMUM HELP QUEUE");
  571. break;
  572. }
  573. }
  574. h->warned = 1;
  575. }
  576. if (debug_output && !h->warned) {
  577. if (buf[len - 1] == '\n')
  578. buf[len - 1] = 0;
  579. switch (which) {
  580. case DP_MODE:
  581. putlog(LOG_SRVOUT, "@", "[!m] %s", buf);
  582. break;
  583. case DP_SERVER:
  584. putlog(LOG_SRVOUT, "@", "[!s] %s", buf);
  585. break;
  586. case DP_HELP:
  587. putlog(LOG_SRVOUT, "@", "[!h] %s", buf);
  588. break;
  589. case DP_MODE_NEXT:
  590. putlog(LOG_SRVOUT, "@", "[!!m] %s", buf);
  591. break;
  592. case DP_SERVER_NEXT:
  593. putlog(LOG_SRVOUT, "@", "[!!s] %s", buf);
  594. break;
  595. case DP_HELP_NEXT:
  596. putlog(LOG_SRVOUT, "@", "[!!h] %s", buf);
  597. break;
  598. }
  599. }
  600. if (which == DP_MODE || which == DP_MODE_NEXT)
  601. deq_msg(); /* DP_MODE needs to be sent ASAP, flush if
  602. possible. */
  603. }
  604. /* Add a new server to the server_list.
  605. */
  606. void add_server(char *ss)
  607. {
  608. struct server_list *x = NULL, *z = NULL;
  609. #ifdef USE_IPV6
  610. char *r = NULL;
  611. #endif /* USE_IPV6 */
  612. char *p = NULL, *q = NULL;
  613. for (z = serverlist; z && z->next; z = z->next);
  614. while (ss) {
  615. p = strchr(ss, ',');
  616. if (p)
  617. *p++ = 0;
  618. x = (struct server_list *) calloc(1, sizeof(struct server_list));
  619. x->next = 0;
  620. x->realname = 0;
  621. x->port = 0;
  622. if (z)
  623. z->next = x;
  624. else
  625. serverlist = x;
  626. z = x;
  627. q = strchr(ss, ':');
  628. if (!q) {
  629. x->port = default_port;
  630. x->pass = 0;
  631. x->name = strdup(ss);
  632. } else {
  633. #ifdef USE_IPV6
  634. if (ss[0] == '[') {
  635. *ss++;
  636. q = strchr(ss, ']');
  637. *q++ = 0; /* intentional */
  638. r = strchr(q, ':');
  639. if (!r)
  640. x->port = default_port;
  641. }
  642. #endif /* USE_IPV6 */
  643. *q++ = 0;
  644. x->name = (char *) calloc(1, q - ss);
  645. strcpy(x->name, ss);
  646. ss = q;
  647. q = strchr(ss, ':');
  648. if (!q) {
  649. x->pass = 0;
  650. } else {
  651. *q++ = 0;
  652. x->pass = strdup(q);
  653. }
  654. #ifdef USE_IPV6
  655. if (!x->port) {
  656. x->port = atoi(ss);
  657. }
  658. #else
  659. x->port = atoi(ss);
  660. #endif /* USE_IPV6 */
  661. }
  662. ss = p;
  663. }
  664. }
  665. /* Clear out the given server_list.
  666. */
  667. void clearq(struct server_list *xx)
  668. {
  669. struct server_list *x = NULL;
  670. while (xx) {
  671. x = xx->next;
  672. if (xx->name)
  673. free(xx->name);
  674. if (xx->pass)
  675. free(xx->pass);
  676. if (xx->realname)
  677. free(xx->realname);
  678. free(xx);
  679. xx = x;
  680. }
  681. }
  682. /* Set botserver to the next available server.
  683. *
  684. * -> if (*ptr == -1) then jump to that particular server
  685. */
  686. static void next_server(int *ptr, char *servname, port_t *port, char *pass)
  687. {
  688. struct server_list *x = serverlist;
  689. if (x == NULL)
  690. return;
  691. int i = 0;
  692. /* -1 --> Go to specified server */
  693. if (*ptr == (-1)) {
  694. for (; x; x = x->next) {
  695. if (x->port == *port) {
  696. if (!egg_strcasecmp(x->name, servname)) {
  697. *ptr = i;
  698. return;
  699. } else if (x->realname && !egg_strcasecmp(x->realname, servname)) {
  700. *ptr = i;
  701. strncpyz(servname, x->realname, sizeof servname);
  702. return;
  703. }
  704. }
  705. i++;
  706. }
  707. /* Gotta add it: */
  708. x = (struct server_list *) calloc(1, sizeof(struct server_list));
  709. x->next = 0;
  710. x->realname = 0;
  711. x->name = strdup(servname);
  712. x->port = *port ? *port : default_port;
  713. if (pass && pass[0]) {
  714. x->pass = strdup(pass);
  715. } else
  716. x->pass = NULL;
  717. list_append((struct list_type **) (&serverlist), (struct list_type *) x);
  718. *ptr = i;
  719. return;
  720. }
  721. /* Find where i am and boogie */
  722. i = (*ptr);
  723. while (i > 0 && x != NULL) {
  724. x = x->next;
  725. i--;
  726. }
  727. if (x != NULL) {
  728. x = x->next;
  729. (*ptr)++;
  730. } /* Go to next server */
  731. if (x == NULL) {
  732. x = serverlist;
  733. *ptr = 0;
  734. } /* Start over at the beginning */
  735. strcpy(servname, x->name);
  736. *port = x->port ? x->port : default_port;
  737. if (x->pass)
  738. strcpy(pass, x->pass);
  739. else
  740. pass[0] = 0;
  741. }
  742. static void do_nettype(void)
  743. {
  744. switch (net_type) {
  745. case NETT_EFNET:
  746. check_mode_r = 0;
  747. break;
  748. case NETT_IRCNET:
  749. check_mode_r = 1;
  750. use_fastdeq = 3;
  751. simple_sprintf(stackablecmds, "INVITE AWAY VERSION NICK");
  752. break;
  753. case NETT_UNDERNET:
  754. check_mode_r = 0;
  755. use_fastdeq = 2;
  756. simple_sprintf(stackablecmds, "PRIVMSG NOTICE TOPIC PART WHOIS USERHOST USERIP ISON");
  757. simple_sprintf(stackable2cmds, "USERHOST USERIP ISON");
  758. break;
  759. case NETT_DALNET:
  760. check_mode_r = 0;
  761. use_fastdeq = 2;
  762. simple_sprintf(stackablecmds, "PRIVMSG NOTICE PART WHOIS WHOWAS USERHOST ISON WATCH DCCALLOW");
  763. simple_sprintf(stackable2cmds, "USERHOST ISON WATCH");
  764. break;
  765. case NETT_HYBRID_EFNET:
  766. check_mode_r = 0;
  767. break;
  768. }
  769. }
  770. /*
  771. * CTCP DCC CHAT functions
  772. */
  773. static int sanitycheck_dcc(char *nick, char *from, char *ipaddy, char *port)
  774. {
  775. /* According to the latest RFC, the clients SHOULD be able to handle
  776. * DNS names that are up to 255 characters long. This is not broken.
  777. */
  778. char badaddress[16];
  779. in_addr_t ip = my_atoul(ipaddy);
  780. int prt = atoi(port);
  781. if (prt < 1) {
  782. putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible port of %u!",
  783. nick, from, prt);
  784. return 0;
  785. }
  786. sprintf(badaddress, "%u.%u.%u.%u", (ip >> 24) & 0xff, (ip >> 16) & 0xff,
  787. (ip >> 8) & 0xff, ip & 0xff);
  788. if (ip < (1 << 24)) {
  789. putlog(LOG_MISC, "*", "ALERT: (%s!%s) specified an impossible IP of %s!",
  790. nick, from, badaddress);
  791. return 0;
  792. }
  793. return 1;
  794. }
  795. static void dcc_chat_hostresolved(int);
  796. /* This only handles CHAT requests, otherwise it's handled in filesys.
  797. */
  798. static int ctcp_DCC_CHAT(char *nick, char *from, struct userrec *u, char *object, char *keyword, char *text)
  799. {
  800. if (!ischanhub())
  801. return BIND_RET_LOG;
  802. char *action = NULL, *param = NULL, *ip = NULL, *prt = NULL;
  803. action = newsplit(&text);
  804. param = newsplit(&text);
  805. ip = newsplit(&text);
  806. prt = newsplit(&text);
  807. if (egg_strcasecmp(action, "CHAT") || egg_strcasecmp(object, botname) || !u)
  808. return BIND_RET_LOG;
  809. int i;
  810. bool ok = 1;
  811. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  812. get_user_flagrec(u, &fr, 0);
  813. if (ischanhub() && !glob_chuba(fr))
  814. ok = 0;
  815. if (dcc_total == max_dcc) {
  816. if (!quiet_reject)
  817. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_TOOMANYDCCS1);
  818. putlog(LOG_MISC, "*", DCC_TOOMANYDCCS2, "CHAT", param, nick, from);
  819. } else if (!ok) {
  820. if (!quiet_reject)
  821. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED2);
  822. putlog(LOG_MISC, "*", "%s: %s!%s", ischanhub() ? DCC_REFUSED : DCC_REFUSEDNC, nick, from);
  823. } else if (u_pass_match(u, "-")) {
  824. if (!quiet_reject)
  825. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_REFUSED3);
  826. putlog(LOG_MISC, "*", "%s: %s!%s", DCC_REFUSED4, nick, from);
  827. } else if (atoi(prt) < 1024 || atoi(prt) > 65535) {
  828. /* Invalid port */
  829. if (!quiet_reject)
  830. dprintf(DP_HELP, "NOTICE %s :%s (invalid port)\n", nick, DCC_CONNECTFAILED1);
  831. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED3, nick, from);
  832. } else {
  833. if (!sanitycheck_dcc(nick, from, ip, prt))
  834. return 1;
  835. i = new_dcc(&DCC_CHAT_PASS, sizeof(struct chat_info));
  836. if (i < 0) {
  837. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", nick, ip);
  838. return BIND_RET_BREAK;
  839. }
  840. dcc[i].addr = my_atoul(ip);
  841. dcc[i].port = atoi(prt);
  842. dcc[i].sock = -1;
  843. strcpy(dcc[i].nick, u->handle);
  844. strcpy(dcc[i].host, from);
  845. dcc[i].timeval = now;
  846. dcc[i].user = u;
  847. dcc_chat_hostresolved(i);
  848. // egg_dns_reverse(dcc[i].addr, 20, dcc_chat_dns_callback, (void *) i);
  849. }
  850. return BIND_RET_BREAK;
  851. }
  852. //static void tandem_relay_dns_callback(void *client_data, const char *host, char **ips)
  853. static void dcc_chat_hostresolved(int i)
  854. {
  855. char buf[512] = "", ip[512] = "";
  856. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  857. egg_snprintf(buf, sizeof buf, "%d", dcc[i].port);
  858. egg_snprintf(ip, sizeof ip, "%lu", iptolong(htonl(dcc[i].addr)));
  859. #ifdef USE_IPV6
  860. dcc[i].sock = getsock(0, AF_INET);
  861. #else
  862. dcc[i].sock = getsock(0);
  863. #endif /* USE_IPV6 */
  864. if (dcc[i].sock < 0 || open_telnet_dcc(dcc[i].sock, ip, buf) < 0) {
  865. strcpy(buf, strerror(errno));
  866. if (!quiet_reject)
  867. dprintf(DP_HELP, "NOTICE %s :%s (%s)\n", dcc[i].nick, DCC_CONNECTFAILED1, buf);
  868. putlog(LOG_MISC, "*", "%s: CHAT (%s!%s)", DCC_CONNECTFAILED2, dcc[i].nick, dcc[i].host);
  869. putlog(LOG_MISC, "*", " (%s)", buf);
  870. killsock(dcc[i].sock);
  871. lostdcc(i);
  872. } else {
  873. bool ok = 1;
  874. #ifdef HAVE_SSL
  875. ssl_link(dcc[i].sock, CONNECT_SSL);
  876. #endif /* HAVE_SSL */
  877. dcc[i].status = STAT_ECHO;
  878. get_user_flagrec(dcc[i].user, &fr, 0);
  879. if (ischanhub() && !glob_chuba(fr))
  880. ok = 0;
  881. if (ok)
  882. dcc[i].status |= STAT_PARTY;
  883. strcpy(dcc[i].u.chat->con_chan, (chanset) ? chanset->dname : "*");
  884. dcc[i].timeval = now;
  885. /* Ok, we're satisfied with them now: attempt the connect */
  886. putlog(LOG_MISC, "*", "DCC connection: CHAT (%s!%s)", dcc[i].nick, dcc[i].host);
  887. dprintf(i, "%s\n", response(RES_USERNAME));
  888. }
  889. return;
  890. }
  891. /*
  892. * Server timer functions
  893. */
  894. static void server_secondly()
  895. {
  896. if (cycle_time)
  897. cycle_time--;
  898. deq_msg();
  899. if (!resolvserv && serv < 0 && !trying_server)
  900. connect_server();
  901. }
  902. static void server_check_lag()
  903. {
  904. if (server_online && !waiting_for_awake && !trying_server) {
  905. dprintf(DP_MODE, "PING :%li\n", now);
  906. waiting_for_awake = 1;
  907. }
  908. }
  909. static void server_5minutely()
  910. {
  911. if (server_online && waiting_for_awake && ((now - lastpingtime) >= 300)) {
  912. /* Uh oh! Never got pong from last time, five minutes ago!
  913. * Server is probably stoned.
  914. */
  915. disconnect_server(servidx, DO_LOST);
  916. putlog(LOG_SERV, "*", IRC_SERVERSTONED);
  917. }
  918. }
  919. void server_die()
  920. {
  921. cycle_time = 100;
  922. if (server_online) {
  923. dprintf(-serv, "QUIT :%s\n", quit_msg[0] ? quit_msg : "");
  924. sleep(2); /* Give the server time to understand */
  925. }
  926. nuke_server(NULL);
  927. }
  928. /* A report on the module status.
  929. */
  930. void server_report(int idx, int details)
  931. {
  932. char s1[64] = "", s[128] = "";
  933. if (server_online) {
  934. dprintf(idx, " Online as: %s%s%s (%s)\n", botname,
  935. botuserhost[0] ? "!" : "", botuserhost[0] ? botuserhost : "",
  936. botrealname);
  937. if (nick_juped)
  938. dprintf(idx, " NICK IS JUPED: %s %s\n", origbotname,
  939. keepnick ? "(trying)" : "");
  940. nick_juped = 0; /* WHY?? -- drummer */
  941. daysdur(now, server_online, s1);
  942. egg_snprintf(s, sizeof s, "(connected %s)", s1);
  943. if (server_lag && !waiting_for_awake) {
  944. if (server_lag == (-1))
  945. egg_snprintf(s1, sizeof s1, " (bad pong replies)");
  946. else
  947. egg_snprintf(s1, sizeof s1, " (lag: %ds)", server_lag);
  948. strcat(s, s1);
  949. }
  950. }
  951. if ((trying_server || server_online) && (servidx != (-1))) {
  952. dprintf(idx, " Server %s:%d %s\n", dcc[servidx].host, dcc[servidx].port,
  953. trying_server ? "(trying)" : s);
  954. } else
  955. dprintf(idx, " %s\n", IRC_NOSERVER);
  956. if (modeq.tot)
  957. dprintf(idx, " %s %d%%, %d msgs\n", IRC_MODEQUEUE,
  958. (int) ((float) (modeq.tot * 100.0) / (float) maxqmsg),
  959. (int) modeq.tot);
  960. if (mq.tot)
  961. dprintf(idx, " %s %d%%, %d msgs\n", IRC_SERVERQUEUE,
  962. (int) ((float) (mq.tot * 100.0) / (float) maxqmsg), (int) mq.tot);
  963. if (hq.tot)
  964. dprintf(idx, " %s %d%%, %d msgs\n", IRC_HELPQUEUE,
  965. (int) ((float) (hq.tot * 100.0) / (float) maxqmsg), (int) hq.tot);
  966. if (details) {
  967. dprintf(idx, " Flood is: %d msg/%lus, %d ctcp/%lus\n",
  968. flud_thr, flud_time, flud_ctcp_thr, flud_ctcp_time);
  969. }
  970. }
  971. static void msgq_clear(struct msgq_head *qh)
  972. {
  973. register struct msgq *qq = NULL;
  974. for (register struct msgq *q = qh->head; q; q = qq) {
  975. qq = q->next;
  976. free(q->msg);
  977. free(q);
  978. }
  979. qh->head = qh->last = NULL;
  980. qh->tot = qh->warned = 0;
  981. }
  982. static cmd_t my_ctcps[] =
  983. {
  984. {"DCC", "", (Function) ctcp_DCC_CHAT, "server:DCC"},
  985. {NULL, NULL, NULL, NULL}
  986. };
  987. void server_init()
  988. {
  989. /*
  990. * Init of all the variables *must* be done in _start rather than
  991. * globally.
  992. */
  993. botname[0] = 0;
  994. trying_server = 0;
  995. server_lag = 0;
  996. curserv = 0;
  997. flud_thr = 5;
  998. flud_time = 60;
  999. flud_ctcp_thr = 3;
  1000. flud_ctcp_time = 60;
  1001. quiet_reject = 1;
  1002. waiting_for_awake = 0;
  1003. server_online = 0;
  1004. strcpy(botrealname, "A deranged product of evil coders");
  1005. server_timeout = 15;
  1006. strict_servernames = 1;
  1007. serverlist = NULL;
  1008. cycle_time = 0;
  1009. oldnick[0] = 0;
  1010. trigger_on_ignore = 0;
  1011. answer_ctcp = 1;
  1012. check_mode_r = 0;
  1013. maxqmsg = 300;
  1014. burst = 0;
  1015. net_type = NETT_EFNET;
  1016. double_mode = 0;
  1017. double_server = 0;
  1018. double_help = 0;
  1019. use_penalties = 0;
  1020. use_fastdeq = 0;
  1021. stackablecmds[0] = 0;
  1022. strcpy(stackable2cmds, "USERHOST ISON");
  1023. resolvserv = 0;
  1024. lastpingtime = 0;
  1025. last_time = 0;
  1026. stack_limit = 4;
  1027. BT_msgc = bind_table_add("msgc", 5, "ssUss", MATCH_FLAGS, 0);
  1028. BT_msg = bind_table_add("msg", 4, "ssUs", MATCH_FLAGS, 0);
  1029. BT_raw = bind_table_add("raw", 2, "ss", 0, BIND_STACKABLE);
  1030. BT_ctcr = bind_table_add("ctcr", 6, "ssUsss", 0, BIND_STACKABLE);
  1031. BT_ctcp = bind_table_add("ctcp", 6, "ssUsss", 0, BIND_STACKABLE);
  1032. add_builtins("raw", my_raw_binds);
  1033. add_builtins("dcc", C_dcc_serv);
  1034. add_builtins("ctcp", my_ctcps);
  1035. timer_create_secs(1, "server_secondly", (Function) server_secondly);
  1036. timer_create_secs(10, "server_10secondly", (Function) server_10secondly);
  1037. timer_create_secs(30, "server_check_lag", (Function) server_check_lag);
  1038. timer_create_secs(300, "server_5minutely", (Function) server_5minutely);
  1039. timer_create_secs(60, "minutely_checks", (Function) minutely_checks);
  1040. mq.head = hq.head = modeq.head = NULL;
  1041. mq.last = hq.last = modeq.last = NULL;
  1042. mq.tot = hq.tot = modeq.tot = 0;
  1043. mq.warned = hq.warned = modeq.warned = 0;
  1044. double_warned = 0;
  1045. newserver[0] = 0;
  1046. newserverport = 0;
  1047. curserv = 999;
  1048. checked_hostmask = 0;
  1049. do_nettype();
  1050. }
  1051. #endif /* LEAF */