server.c 30 KB

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