server.c 30 KB

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