server.c 34 KB

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