server.c 30 KB

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