server.c 27 KB

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