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