server.c 29 KB

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