server.c 29 KB

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