server.c 29 KB

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