server.c 28 KB

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