server.c 33 KB

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