servmsg.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345
  1. #ifdef LEAF
  2. /*
  3. * servmsg.c -- part of server.mod
  4. *
  5. */
  6. #ifdef S_NODELAY
  7. #include <netinet/tcp.h>
  8. #endif
  9. #define msgop CFG_MSGOP.ldata ? CFG_MSGOP.ldata : CFG_MSGOP.gdata ? CFG_MSGOP.gdata : ""
  10. #define msgpass CFG_MSGPASS.ldata ? CFG_MSGPASS.ldata : CFG_MSGPASS.gdata ? CFG_MSGPASS.gdata : ""
  11. #define msginvite CFG_MSGINVITE.ldata ? CFG_MSGINVITE.ldata : CFG_MSGINVITE.gdata ? CFG_MSGINVITE.gdata : ""
  12. #define msgident CFG_MSGIDENT.ldata ? CFG_MSGIDENT.ldata : CFG_MSGIDENT.gdata ? CFG_MSGIDENT.gdata : ""
  13. char cursrvname[120]="";
  14. static time_t last_ctcp = (time_t) 0L;
  15. static int count_ctcp = 0;
  16. static char altnick_char = 0;
  17. /* We try to change to a preferred unique nick here. We always first try the
  18. * specified alternate nick. If that failes, we repeatedly modify the nick
  19. * until it gets accepted.
  20. *
  21. * sent nick:
  22. * "<altnick><c>"
  23. * ^--- additional count character: 1-9^-_\\[]`a-z
  24. * ^--------- given, alternate nick
  25. *
  26. * The last added character is always saved in altnick_char. At the very first
  27. * attempt (were altnick_char is 0), we try the alternate nick without any
  28. * additions.
  29. *
  30. * fixed by guppy (1999/02/24) and Fabian (1999/11/26)
  31. */
  32. int rolls = 0;
  33. static int gotfake433(char *from)
  34. {
  35. int l = strlen(botname) - 1;
  36. int use_chr = 1;
  37. char *altchrs = STR("-_\\`^[]");
  38. /* First run? */
  39. if (altnick_char == 0) {
  40. altnick_char = altchrs[0];
  41. /* the nick is already as long as it can be. */
  42. if (l + 1 == nick_len) {
  43. /* make the last char the altnick_char */
  44. botname[l] = altnick_char;
  45. } else {
  46. /* tack it on to the end */
  47. botname[++l] = altnick_char;
  48. botname[l + 1] = 0;
  49. }
  50. } else {
  51. char *p = strchr(altchrs, altnick_char);
  52. p++;
  53. if (!*p) {
  54. /* fun BX style rolling, WEEEE */
  55. if (rolls < 10) {
  56. char tmp;
  57. if (rolls == 0)
  58. strcpy(botname, origbotname);
  59. tmp = botname[8];
  60. if (strchr(BADNICKCHARS, tmp))
  61. tmp = '_';
  62. altnick_char = 0;
  63. use_chr = 0;
  64. rolls++;
  65. botname[8] = botname[7];
  66. botname[7] = botname[6];
  67. botname[6] = botname[5];
  68. botname[5] = botname[4];
  69. botname[4] = botname[3];
  70. botname[3] = botname[2];
  71. botname[2] = botname[1];
  72. botname[1] = botname[0];
  73. botname[0] = tmp;
  74. } else {
  75. /* when we run out of 'altchrs', then make altnick_char a random alpha */
  76. altnick_char = 'a' + random() % 26;
  77. }
  78. } else {
  79. /* else, make altnick_char the 'oknick' */
  80. altnick_char = (*p);
  81. }
  82. if (use_chr)
  83. botname[l] = altnick_char;
  84. }
  85. putlog(LOG_SERV, "*", IRC_BOTNICKINUSE, botname);
  86. dprintf(DP_SERVER, "NICK %s\n", botname);
  87. return 0;
  88. }
  89. /* Check for tcl-bound msg command, return 1 if found
  90. *
  91. * msg: proc-name <nick> <user@host> <handle> <args...>
  92. */
  93. static int check_tcl_msg(char *cmd, char *nick, char *uhost,
  94. struct userrec *u, char *args)
  95. {
  96. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  97. char *hand = u ? u->handle : "*";
  98. int x;
  99. get_user_flagrec(u, &fr, NULL);
  100. Tcl_SetVar(interp, "_msg1", nick, 0);
  101. Tcl_SetVar(interp, "_msg2", uhost, 0);
  102. Tcl_SetVar(interp, "_msg3", hand, 0);
  103. Tcl_SetVar(interp, "_msg4", args, 0);
  104. x = check_tcl_bind(H_msg, cmd, &fr, " $_msg1 $_msg2 $_msg3 $_msg4",
  105. MATCH_EXACT | BIND_HAS_BUILTINS | BIND_USE_ATTR);
  106. if (x == BIND_EXEC_LOG)
  107. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, hand,
  108. cmd, args);
  109. return ((x == BIND_MATCHED) || (x == BIND_EXECUTED) || (x == BIND_EXEC_LOG));
  110. }
  111. #ifdef S_AUTH
  112. static int check_tcl_msgc(char *cmd, char *nick, char *uhost,
  113. struct userrec *u, char *args)
  114. {
  115. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  116. char *hand = u ? u->handle : "*";
  117. int x;
  118. get_user_flagrec(u, &fr, NULL);
  119. Tcl_SetVar(interp, "_msgc1", nick, 0);
  120. Tcl_SetVar(interp, "_msgc2", uhost, 0);
  121. Tcl_SetVar(interp, "_msgc3", hand, 0);
  122. Tcl_SetVar(interp, "_msgc4", args, 0);
  123. Tcl_SetVar(interp, "_msgc5", NULL, 0);
  124. x = check_tcl_bind(H_msgc, cmd, &fr, " $_msgc1 $_msgc2 $_msgc3 $_msgc4 $_msgc5",
  125. MATCH_EXACT | BIND_HAS_BUILTINS | BIND_USE_ATTR);
  126. if (x == BIND_EXEC_LOG)
  127. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s%s %s", nick, uhost, hand, cmdprefix,
  128. cmd, args);
  129. return ((x == BIND_MATCHED) || (x == BIND_EXECUTED) || (x == BIND_EXEC_LOG));
  130. }
  131. #endif /* S_AUTH */
  132. /* Return 1 if processed.
  133. */
  134. static int check_tcl_raw(char *from, char *code, char *msg)
  135. {
  136. int x;
  137. Tcl_SetVar(interp, "_raw1", from, 0);
  138. Tcl_SetVar(interp, "_raw2", code, 0);
  139. Tcl_SetVar(interp, "_raw3", msg, 0);
  140. x = check_tcl_bind(H_raw, code, 0, " $_raw1 $_raw2 $_raw3",
  141. MATCH_EXACT | BIND_STACKABLE | BIND_WANTRET);
  142. return (x == BIND_EXEC_LOG);
  143. }
  144. static int check_tcl_ctcpr(char *nick, char *uhost, struct userrec *u,
  145. char *dest, char *keyword, char *args,
  146. p_tcl_bind_list table)
  147. {
  148. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
  149. int x;
  150. get_user_flagrec(u, &fr, NULL);
  151. Tcl_SetVar(interp, "_ctcpr1", nick, 0);
  152. Tcl_SetVar(interp, "_ctcpr2", uhost, 0);
  153. Tcl_SetVar(interp, "_ctcpr3", u ? u->handle : "*", 0);
  154. Tcl_SetVar(interp, "_ctcpr4", dest, 0);
  155. Tcl_SetVar(interp, "_ctcpr5", keyword, 0);
  156. Tcl_SetVar(interp, "_ctcpr6", args, 0);
  157. x = check_tcl_bind(table, keyword, &fr,
  158. " $_ctcpr1 $_ctcpr2 $_ctcpr3 $_ctcpr4 $_ctcpr5 $_ctcpr6",
  159. MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE |
  160. ((table == H_ctcp) ? BIND_WANTRET : 0));
  161. return (x == BIND_EXEC_LOG) || (table == H_ctcr);
  162. }
  163. static int match_my_nick(char *nick)
  164. {
  165. if (!rfc_casecmp(nick, botname))
  166. return 1;
  167. return 0;
  168. }
  169. /* 001: welcome to IRC (use it to fix the server name)
  170. */
  171. static int got001(char *from, char *msg)
  172. {
  173. struct server_list *x;
  174. int i, servidx = findanyidx(serv);
  175. struct chanset_t *chan;
  176. /* Ok...param #1 of 001 = what server thinks my nick is */
  177. server_online = now;
  178. fixcolon(msg);
  179. strncpyz(botname, msg, NICKLEN);
  180. altnick_char = 0;
  181. strncpyz(cursrvname, from, sizeof(cursrvname));
  182. dprintf(DP_SERVER, "WHOIS %s\n", botname); /* get user@host */
  183. dprintf(DP_SERVER, "MODE %s +iws\n");
  184. x = serverlist;
  185. if (x == NULL)
  186. return 0; /* Uh, no server list */
  187. /* Only join if the IRC module is loaded. */
  188. if (module_find("irc", 0, 0))
  189. for (chan = chanset; chan; chan = chan->next) {
  190. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND);
  191. if (shouldjoin(chan))
  192. dprintf(DP_SERVER, "JOIN %s %s\n",
  193. (chan->name[0]) ? chan->name : chan->dname,
  194. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  195. }
  196. if (egg_strcasecmp(from, dcc[servidx].host)) {
  197. putlog(LOG_MISC, "*", "(%s claims to be %s; updating server list)",
  198. dcc[servidx].host, from);
  199. for (i = curserv; i > 0 && x != NULL; i--)
  200. x = x->next;
  201. if (x == NULL) {
  202. putlog(LOG_MISC, "*", "Invalid server list!");
  203. return 0;
  204. }
  205. if (x->realname)
  206. nfree(x->realname);
  207. if (strict_servernames == 1) {
  208. x->realname = NULL;
  209. if (x->name)
  210. nfree(x->name);
  211. x->name = nmalloc(strlen(from) + 1);
  212. strcpy(x->name, from);
  213. } else {
  214. x->realname = nmalloc(strlen(from) + 1);
  215. strcpy(x->realname, from);
  216. }
  217. }
  218. return 0;
  219. }
  220. /* Got 442: not on channel
  221. */
  222. static int got442(char *from, char *msg)
  223. {
  224. char *chname;
  225. struct chanset_t *chan;
  226. struct server_list *x;
  227. int i;
  228. for (x = serverlist, i = 0; x; x = x->next, i++)
  229. if (i == curserv) {
  230. if (egg_strcasecmp(from, x->realname ? x->realname : x->name))
  231. return 0;
  232. break;
  233. }
  234. newsplit(&msg);
  235. chname = newsplit(&msg);
  236. chan = findchan(chname);
  237. if (chan)
  238. if (shouldjoin(chan)) {
  239. module_entry *me = module_find("channels", 0, 0);
  240. putlog(LOG_MISC, chname, IRC_SERVNOTONCHAN, chname);
  241. if (me && me->funcs)
  242. (me->funcs[CHANNEL_CLEAR])(chan, 1);
  243. chan->status &= ~CHAN_ACTIVE;
  244. dprintf(DP_MODE, "JOIN %s %s\n", chan->name,
  245. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  246. }
  247. return 0;
  248. }
  249. /* Close the current server connection.
  250. */
  251. static void nuke_server(char *reason)
  252. {
  253. if (serv >= 0) {
  254. int servidx = findanyidx(serv);
  255. if (reason && (servidx > 0))
  256. dprintf(servidx, "QUIT :%s\n", reason);
  257. disconnect_server(servidx);
  258. lostdcc(servidx);
  259. }
  260. }
  261. static char ctcp_reply[1024] = "";
  262. static int lastmsgs[FLOOD_GLOBAL_MAX];
  263. static char lastmsghost[FLOOD_GLOBAL_MAX][81];
  264. static time_t lastmsgtime[FLOOD_GLOBAL_MAX];
  265. /* Do on NICK, PRIVMSG, NOTICE and JOIN.
  266. */
  267. static int detect_flood(char *floodnick, char *floodhost, char *from, int which)
  268. {
  269. char *p, ftype[10], h[1024];
  270. struct userrec *u;
  271. int thr = 0, lapse = 0, atr;
  272. u = get_user_by_host(from);
  273. atr = u ? u->flags : 0;
  274. if (atr & (USER_BOT))
  275. return 0;
  276. #ifdef S_AUTH
  277. if (findauth(floodhost) > -1)
  278. return 0;
  279. #endif /* S_AUTH */
  280. /* Determine how many are necessary to make a flood */
  281. switch (which) {
  282. case FLOOD_PRIVMSG:
  283. case FLOOD_NOTICE:
  284. thr = flud_thr;
  285. lapse = flud_time;
  286. strcpy(ftype, "msg");
  287. break;
  288. case FLOOD_CTCP:
  289. thr = flud_ctcp_thr;
  290. lapse = flud_ctcp_time;
  291. strcpy(ftype, "ctcp");
  292. break;
  293. }
  294. if ((thr == 0) || (lapse == 0))
  295. return 0; /* No flood protection */
  296. /* Okay, make sure i'm not flood-checking myself */
  297. if (match_my_nick(floodnick))
  298. return 0;
  299. if (!egg_strcasecmp(floodhost, botuserhost))
  300. return 0; /* My user@host (?) */
  301. p = strchr(floodhost, '@');
  302. if (p) {
  303. p++;
  304. if (egg_strcasecmp(lastmsghost[which], p)) { /* New */
  305. strcpy(lastmsghost[which], p);
  306. lastmsgtime[which] = now;
  307. lastmsgs[which] = 0;
  308. return 0;
  309. }
  310. } else
  311. return 0; /* Uh... whatever. */
  312. if (lastmsgtime[which] < now - lapse) {
  313. /* Flood timer expired, reset it */
  314. lastmsgtime[which] = now;
  315. lastmsgs[which] = 0;
  316. return 0;
  317. }
  318. lastmsgs[which]++;
  319. if (lastmsgs[which] >= thr) { /* FLOOD */
  320. /* Reset counters */
  321. lastmsgs[which] = 0;
  322. lastmsgtime[which] = 0;
  323. lastmsghost[which][0] = 0;
  324. u = get_user_by_host(from);
  325. /* Private msg */
  326. simple_sprintf(h, "*!*@%s", p);
  327. putlog(LOG_MISC, "*", IRC_FLOODIGNORE1, p);
  328. addignore(h, botnetnick, (which == FLOOD_CTCP) ? "CTCP flood" :
  329. "MSG/NOTICE flood", now + (60 * ignore_time));
  330. }
  331. return 0;
  332. }
  333. /* Check for more than 8 control characters in a line.
  334. * This could indicate: beep flood CTCP avalanche.
  335. */
  336. static int detect_avalanche(char *msg)
  337. {
  338. int count = 0;
  339. unsigned char *p;
  340. for (p = (unsigned char *) msg; (*p) && (count < 8); p++)
  341. if ((*p == 7) || (*p == 1))
  342. count++;
  343. if (count >= 8)
  344. return 1;
  345. else
  346. return 0;
  347. }
  348. /* Got a private message.
  349. */
  350. static int gotmsg(char *from, char *msg)
  351. {
  352. char *to, buf[UHOSTLEN], *nick, ctcpbuf[512], *uhost = buf, *ctcp;
  353. char *p, *p1, *code;
  354. struct userrec *u;
  355. int ctcp_count = 0;
  356. #ifdef S_AUTH
  357. int i = 0;
  358. #endif /* S_AUTH */
  359. int ignoring = 0;
  360. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  361. (*msg == '@'))) /* Notice to a channel, not handled here */
  362. return 0;
  363. ignoring = match_ignore(from);
  364. to = newsplit(&msg);
  365. fixcolon(msg);
  366. /* Only check if flood-ctcp is active */
  367. strcpy(uhost, from);
  368. nick = splitnick(&uhost);
  369. if (flud_ctcp_thr && detect_avalanche(msg)) {
  370. if (!ignoring) {
  371. putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
  372. p = strchr(uhost, '@');
  373. if (p != NULL)
  374. p++;
  375. else
  376. p = uhost;
  377. simple_sprintf(ctcpbuf, "*!*@%s", p);
  378. addignore(ctcpbuf, botnetnick, "ctcp avalanche",
  379. now + (60 * ignore_time));
  380. }
  381. }
  382. /* Check for CTCP: */
  383. ctcp_reply[0] = 0;
  384. p = strchr(msg, 1);
  385. while ((p != NULL) && (*p)) {
  386. p++;
  387. p1 = p;
  388. while ((*p != 1) && (*p != 0))
  389. p++;
  390. if (*p == 1) {
  391. *p = 0;
  392. ctcp = strcpy(ctcpbuf, p1);
  393. strcpy(p1 - 1, p + 1);
  394. if (!ignoring)
  395. detect_flood(nick, uhost, from,
  396. strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  397. /* Respond to the first answer_ctcp */
  398. p = strchr(msg, 1);
  399. if (ctcp_count < answer_ctcp) {
  400. ctcp_count++;
  401. if (ctcp[0] != ' ') {
  402. code = newsplit(&ctcp);
  403. if ((to[0] == '$') || strchr(to, '.')) {
  404. if (!ignoring)
  405. /* Don't interpret */
  406. putlog(LOG_PUBLIC, to, "CTCP %s: %s from %s (%s) to %s",
  407. code, ctcp, nick, uhost, to);
  408. } else {
  409. u = get_user_by_host(from);
  410. if (!ignoring || trigger_on_ignore) {
  411. if (!check_tcl_ctcp(nick, uhost, u, to, code, ctcp) &&
  412. !ignoring) {
  413. if ((lowercase_ctcp && !egg_strcasecmp(code, "DCC")) ||
  414. (!lowercase_ctcp && !strcmp(code, "DCC"))) {
  415. /* If it gets this far unhandled, it means that
  416. * the user is totally unknown.
  417. */
  418. code = newsplit(&ctcp);
  419. if (!strcmp(code, "CHAT")) {
  420. if (!quiet_reject) {
  421. if (u)
  422. dprintf(DP_HELP, "NOTICE %s :%s\n", nick,
  423. "I'm not accepting call at the moment.");
  424. else
  425. dprintf(DP_HELP, "NOTICE %s :%s\n",
  426. nick, DCC_NOSTRANGERS);
  427. }
  428. if (!ischanhub())
  429. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  430. else
  431. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  432. } else
  433. putlog(LOG_MISC, "*", "Refused DCC %s: %s",
  434. code, from);
  435. }
  436. }
  437. if (!strcmp(code, "ACTION")) {
  438. putlog(LOG_MSGS, "*", "Action to %s: %s %s",
  439. to, nick, ctcp);
  440. } else {
  441. putlog(LOG_MSGS, "*", "CTCP %s: %s from %s (%s)",
  442. code, ctcp, nick, uhost);
  443. } /* I love a good close cascade ;) */
  444. }
  445. }
  446. }
  447. }
  448. }
  449. }
  450. /* Send out possible ctcp responses */
  451. if (ctcp_reply[0]) {
  452. if (ctcp_mode != 2) {
  453. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  454. } else {
  455. if (now - last_ctcp > flud_ctcp_time) {
  456. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  457. count_ctcp = 1;
  458. } else if (count_ctcp < flud_ctcp_thr) {
  459. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  460. count_ctcp++;
  461. }
  462. last_ctcp = now;
  463. }
  464. }
  465. if (msg[0]) {
  466. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  467. /* Msg from oper */
  468. if (!ignoring) {
  469. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  470. /* Do not interpret as command */
  471. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  472. }
  473. } else {
  474. char *code;
  475. struct userrec *u;
  476. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  477. u = get_user_by_host(from);
  478. code = newsplit(&msg);
  479. rmspace(msg);
  480. #ifdef S_AUTH
  481. i = findauth(uhost);
  482. /* is it a cmd? */
  483. if (i > -1 && auth[i].authed && code[0] == cmdprefix[0] && code[1]) {
  484. code++;
  485. u = auth[i].user;
  486. if (check_tcl_msgc(code, nick, uhost, u, msg))
  487. auth[i].atime = now;
  488. else
  489. putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg);
  490. } else if ((code[0] != cmdprefix[0] || !code[1] || i == -1 || !(auth[i].authed))) {
  491. #endif /* S_AUTH */
  492. if (!ignoring) {
  493. int doit = 1, result = 0;
  494. #ifdef S_MSGCMDS
  495. if (!egg_strcasecmp(code, "op") || !egg_strcasecmp(code, "pass") || !egg_strcasecmp(code, "invite")
  496. || !egg_strcasecmp(code, "ident")
  497. || !egg_strcasecmp(code, msgop) || !egg_strcasecmp(code, msgpass)
  498. || !egg_strcasecmp(code, msginvite) || !egg_strcasecmp(code, msgident)) {
  499. /* || !strcmp(code, msgop) || !strcmp(code, msgpass) || !strcmp(code, msgop)) { */
  500. char buf[10];
  501. doit = 0;
  502. if (!egg_strcasecmp(code, msgop))
  503. sprintf(buf, "op");
  504. else if (!egg_strcasecmp(code, msgpass))
  505. sprintf(buf, "pass");
  506. else if (!egg_strcasecmp(code, msginvite))
  507. sprintf(buf, "invite");
  508. else if (!egg_strcasecmp(code, msgident))
  509. sprintf(buf, "ident");
  510. if (buf[0])
  511. result = check_tcl_msg(buf, nick, uhost, u, msg);
  512. }
  513. #endif /* S_MSGCMDS */
  514. if (doit)
  515. result = check_tcl_msg(code, nick, uhost, u, msg);
  516. if (!result)
  517. putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg);
  518. }
  519. #ifdef S_AUTH
  520. }
  521. #endif /* S_AUTH */
  522. }
  523. }
  524. return 0;
  525. }
  526. /* Got a private notice.
  527. */
  528. static int gotnotice(char *from, char *msg)
  529. {
  530. char *to, *nick, ctcpbuf[512], *p, *p1, buf[512], *uhost = buf, *ctcp;
  531. struct userrec *u;
  532. int ignoring;
  533. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  534. (*msg == '@'))) /* Notice to a channel, not handled here */
  535. return 0;
  536. ignoring = match_ignore(from);
  537. to = newsplit(&msg);
  538. fixcolon(msg);
  539. strcpy(uhost, from);
  540. nick = splitnick(&uhost);
  541. if (flud_ctcp_thr && detect_avalanche(msg)) {
  542. /* Discard -- kick user if it was to the channel */
  543. if (!ignoring)
  544. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  545. return 0;
  546. }
  547. /* Check for CTCP: */
  548. p = strchr(msg, 1);
  549. while ((p != NULL) && (*p)) {
  550. p++;
  551. p1 = p;
  552. while ((*p != 1) && (*p != 0))
  553. p++;
  554. if (*p == 1) {
  555. *p = 0;
  556. ctcp = strcpy(ctcpbuf, p1);
  557. strcpy(p1 - 1, p + 1);
  558. if (!ignoring)
  559. detect_flood(nick, uhost, from, FLOOD_CTCP);
  560. p = strchr(msg, 1);
  561. if (ctcp[0] != ' ') {
  562. char *code = newsplit(&ctcp);
  563. if ((to[0] == '$') || strchr(to, '.')) {
  564. if (!ignoring)
  565. putlog(LOG_PUBLIC, "*",
  566. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp,
  567. nick, uhost, to);
  568. } else {
  569. u = get_user_by_host(from);
  570. if (!ignoring || trigger_on_ignore) {
  571. check_tcl_ctcr(nick, uhost, u, to, code, ctcp);
  572. if (!ignoring)
  573. /* Who cares? */
  574. putlog(LOG_MSGS, "*",
  575. "CTCP reply %s: %s from %s (%s) to %s",
  576. code, ctcp, nick, uhost, to);
  577. }
  578. }
  579. }
  580. }
  581. }
  582. if (msg[0]) {
  583. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  584. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  585. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s",
  586. nick, uhost, to, msg);
  587. } else {
  588. /* Server notice? */
  589. if ((nick[0] == 0) || (uhost[0] == 0)) {
  590. /* Hidden `250' connection count message from server */
  591. if (strncmp(msg, "Highest connection count:", 25))
  592. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  593. } else {
  594. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  595. u = get_user_by_host(from);
  596. if (!ignoring)
  597. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  598. }
  599. }
  600. }
  601. return 0;
  602. }
  603. /* got 251: lusers
  604. * <server> 251 <to> :there are 2258 users and 805 invisible on 127 servers
  605. */
  606. static int got251(char *from, char *msg)
  607. {
  608. int i;
  609. char *servs;
  610. if (min_servs == 0)
  611. return 0; /* No minimum limit on servers */
  612. newsplit(&msg);
  613. fixcolon(msg); /* NOTE!!! If servlimit is not set or is 0 */
  614. for (i = 0; i < 8; i++)
  615. newsplit(&msg); /* lusers IS NOT SENT AT ALL!! */
  616. servs = newsplit(&msg);
  617. if (strncmp(msg, "servers", 7))
  618. return 0; /* Was invalid format */
  619. while (*servs && (*servs < 32))
  620. servs++; /* I've seen some lame nets put bolds &
  621. * stuff in here :/ */
  622. i = atoi(servs);
  623. if (i < min_servs) {
  624. putlog(LOG_SERV, "*", IRC_AUTOJUMP, min_servs, i);
  625. nuke_server(IRC_CHANGINGSERV);
  626. }
  627. return 0;
  628. }
  629. /* WALLOPS: oper's nuisance
  630. */
  631. static int gotwall(char *from, char *msg)
  632. {
  633. char *nick;
  634. fixcolon(msg);
  635. /* Following is not needed at all, but we'll keep it for compatibility sak$
  636. * so not to confuse possible scripts that are parsing log files.
  637. */
  638. if (strchr(from, '!')) {
  639. nick = splitnick(&from);
  640. putlog(LOG_WALL, "*", "!%s(%s)! %s", nick, from, msg);
  641. } else
  642. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  643. return 0;
  644. }
  645. static void server_10secondly()
  646. {
  647. if (server_online && keepnick) {
  648. /* NOTE: now that botname can but upto NICKLEN bytes long,
  649. * check that it's not just a truncation of the full nick.
  650. */
  651. if (strncmp(botname, origbotname, strlen(botname))) {
  652. /* See if my nickname is in use and if if my nick is right. */
  653. dprintf(DP_SERVER, "ISON :%s %s\n", botname, origbotname);
  654. }
  655. }
  656. }
  657. /* Called once a minute... but if we're the only one on the
  658. * channel, we only wanna send out "lusers" once every 5 mins.
  659. */
  660. static void minutely_checks()
  661. {
  662. /* Only check if we have already successfully logged in. */
  663. if (server_online && (min_servs != 0)) {
  664. static int count = 4;
  665. int ok = 0;
  666. struct chanset_t *chan;
  667. for (chan = chanset; chan; chan = chan->next) {
  668. if (channel_active(chan) && chan->channel.members == 1) {
  669. ok = 1;
  670. break;
  671. }
  672. }
  673. if (!ok)
  674. return;
  675. count++;
  676. if (count >= 5) {
  677. dprintf(DP_SERVER, "LUSERS\n");
  678. count = 0;
  679. }
  680. }
  681. }
  682. /* Pong from server.
  683. */
  684. static int gotpong(char *from, char *msg)
  685. {
  686. newsplit(&msg);
  687. fixcolon(msg); /* Scrap server name */
  688. server_lag = now - my_atoul(msg);
  689. if (server_lag > 99999) {
  690. /* IRCnet lagmeter support by drummer */
  691. server_lag = now - lastpingtime;
  692. }
  693. return 0;
  694. }
  695. /* This is a reply on ISON :<current> <orig> [<alt>]
  696. */
  697. static void got303(char *from, char *msg)
  698. {
  699. char *tmp;
  700. int ison_orig = 0;
  701. if (!keepnick ||
  702. !strncmp(botname, origbotname, strlen(botname))) {
  703. return;
  704. }
  705. newsplit(&msg);
  706. fixcolon(msg);
  707. tmp = newsplit(&msg);
  708. if (tmp[0] && !rfc_casecmp(botname, tmp)) {
  709. while ((tmp = newsplit(&msg))[0]) { /* no, it's NOT == */
  710. if (!rfc_casecmp(tmp, origbotname))
  711. ison_orig = 1;
  712. }
  713. if (!ison_orig) {
  714. if (!nick_juped)
  715. putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
  716. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  717. }
  718. }
  719. }
  720. /* 432 : Bad nickname
  721. */
  722. static int got432(char *from, char *msg)
  723. {
  724. char *erroneus;
  725. newsplit(&msg);
  726. erroneus = newsplit(&msg);
  727. if (server_online)
  728. putlog(LOG_MISC, "*", "NICK IS INVALID: %s (keeping '%s').", erroneus,
  729. botname);
  730. else {
  731. putlog(LOG_MISC, "*", IRC_BADBOTNICK);
  732. if (!keepnick) {
  733. makepass(erroneus);
  734. erroneus[NICKMAX] = 0;
  735. dprintf(DP_MODE, "NICK %s\n", erroneus);
  736. }
  737. return 0;
  738. }
  739. return 0;
  740. }
  741. /* 433 : Nickname in use
  742. * Change nicks till we're acceptable or we give up
  743. */
  744. static int got433(char *from, char *msg)
  745. {
  746. char *tmp;
  747. if (server_online) {
  748. /* We are online and have a nickname, we'll keep it */
  749. newsplit(&msg);
  750. tmp = newsplit(&msg);
  751. putlog(LOG_MISC, "*", "NICK IN USE: %s (keeping '%s').", tmp, botname);
  752. nick_juped = 0;
  753. return 0;
  754. }
  755. gotfake433(from);
  756. return 0;
  757. }
  758. /* 437 : Nickname juped (IRCnet)
  759. */
  760. static int got437(char *from, char *msg)
  761. {
  762. char *s;
  763. struct chanset_t *chan;
  764. newsplit(&msg);
  765. s = newsplit(&msg);
  766. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  767. chan = findchan(s);
  768. if (chan) {
  769. if (chan->status & CHAN_ACTIVE) {
  770. putlog(LOG_MISC, "*", IRC_CANTCHANGENICK, s);
  771. } else {
  772. if (!channel_juped(chan)) {
  773. putlog(LOG_MISC, "*", IRC_CHANNELJUPED, s);
  774. chan->status |= CHAN_JUPED;
  775. }
  776. }
  777. }
  778. } else if (server_online) {
  779. if (!nick_juped)
  780. putlog(LOG_MISC, "*", "NICK IS JUPED: %s (keeping '%s').", s, botname);
  781. if (!rfc_casecmp(s, origbotname))
  782. nick_juped = 1;
  783. } else {
  784. putlog(LOG_MISC, "*", "%s: %s", IRC_BOTNICKJUPED, s);
  785. gotfake433(from);
  786. }
  787. return 0;
  788. }
  789. /* 438 : Nick change too fast
  790. */
  791. static int got438(char *from, char *msg)
  792. {
  793. newsplit(&msg);
  794. newsplit(&msg);
  795. fixcolon(msg);
  796. putlog(LOG_MISC, "*", "%s", msg);
  797. return 0;
  798. }
  799. static int got451(char *from, char *msg)
  800. {
  801. /* Usually if we get this then we really messed up somewhere
  802. * or this is a non-standard server, so we log it and kill the socket
  803. * hoping the next server will work :) -poptix
  804. */
  805. /* Um, this does occur on a lagged anti-spoof server connection if the
  806. * (minutely) sending of joins occurs before the bot does its ping reply.
  807. * Probably should do something about it some time - beldin
  808. */
  809. putlog(LOG_MISC, "*", IRC_NOTREGISTERED1, from);
  810. nuke_server(IRC_NOTREGISTERED2);
  811. return 0;
  812. }
  813. /* Got error notice
  814. */
  815. static int goterror(char *from, char *msg)
  816. {
  817. /* FIXME: fixcolon doesn't do what we need here, this is a temp fix
  818. * fixcolon(msg);
  819. */
  820. if (msg[0] == ':')
  821. msg++;
  822. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  823. if (serverror_quit) {
  824. putlog(LOG_SERV, "*", "Disconnecting from server.");
  825. nuke_server("Bah, stupid error messages.");
  826. }
  827. return 1;
  828. }
  829. /* Got nick change.
  830. */
  831. static int gotnick(char *from, char *msg)
  832. {
  833. char *nick;
  834. struct userrec *u;
  835. u = get_user_by_host(from);
  836. nick = splitnick(&from);
  837. fixcolon(msg);
  838. check_queues(nick, msg);
  839. if (match_my_nick(nick)) {
  840. /* Regained nick! */
  841. strncpyz(botname, msg, NICKLEN);
  842. altnick_char = 0;
  843. if (!strcmp(msg, origbotname)) {
  844. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  845. nick_juped = 0;
  846. } else if (keepnick && strcmp(nick, msg)) {
  847. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  848. if (!rfc_casecmp(nick, origbotname)) {
  849. putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
  850. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  851. }
  852. } else
  853. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  854. } else if ((keepnick) && (rfc_casecmp(nick, msg))) {
  855. /* Only do the below if there was actual nick change, case doesn't count */
  856. if (!rfc_casecmp(nick, origbotname)) {
  857. putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
  858. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  859. }
  860. }
  861. return 0;
  862. }
  863. static int gotmode(char *from, char *msg)
  864. {
  865. char *ch;
  866. ch = newsplit(&msg);
  867. /* Usermode changes? */
  868. if (strchr(CHANMETA, ch[0]) == NULL) {
  869. if (match_my_nick(ch) && check_mode_r) {
  870. /* umode +r? - D0H dalnet uses it to mean something different */
  871. fixcolon(msg);
  872. if ((msg[0] == '+') && strchr(msg, 'r')) {
  873. int servidx = findanyidx(serv);
  874. putlog(LOG_MISC | LOG_JOIN, "*",
  875. "%s has me i-lined (jumping)", dcc[servidx].host);
  876. nuke_server("i-lines suck");
  877. }
  878. }
  879. }
  880. return 0;
  881. }
  882. static void disconnect_server(int idx)
  883. {
  884. server_online = 0;
  885. if (dcc[idx].sock >= 0)
  886. killsock(dcc[idx].sock);
  887. dcc[idx].sock = (-1);
  888. serv = (-1);
  889. botuserhost[0] = 0;
  890. }
  891. static void eof_server(int idx)
  892. {
  893. putlog(LOG_SERV, "*", "%s %s", IRC_DISCONNECTED, dcc[idx].host);
  894. #ifdef S_AUTH
  895. {
  896. int i = 0;
  897. if (ischanhub() && auth_total > 0) {
  898. putlog(LOG_DEBUG, "*", "Removing %d auth entries.", auth_total);
  899. for (i = 0; i < auth_total; i++)
  900. removeauth(i);
  901. }
  902. }
  903. #endif /* S_AUTH */
  904. disconnect_server(idx);
  905. lostdcc(idx);
  906. }
  907. static void display_server(int idx, char *buf)
  908. {
  909. sprintf(buf, "%s (lag: %d)", trying_server ? "conn" : "serv",
  910. server_lag);
  911. }
  912. static void connect_server(void);
  913. static void kill_server(int idx, void *x)
  914. {
  915. module_entry *me;
  916. disconnect_server(idx);
  917. if ((me = module_find("channels", 0, 0)) && me->funcs) {
  918. struct chanset_t *chan;
  919. for (chan = chanset; chan; chan = chan->next)
  920. (me->funcs[CHANNEL_CLEAR]) (chan, 1);
  921. }
  922. /* A new server connection will be automatically initiated in
  923. about 2 seconds. */
  924. }
  925. static void timeout_server(int idx)
  926. {
  927. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  928. disconnect_server(idx);
  929. lostdcc(idx);
  930. }
  931. static void server_activity(int idx, char *msg, int len);
  932. static struct dcc_table SERVER_SOCKET =
  933. {
  934. "SERVER",
  935. 0,
  936. eof_server,
  937. server_activity,
  938. NULL,
  939. timeout_server,
  940. display_server,
  941. NULL,
  942. kill_server,
  943. NULL
  944. };
  945. int isop(char *mode)
  946. {
  947. int state = 0,
  948. cnt = 0;
  949. char *p;
  950. p = mode;
  951. while ((*p) && (*p != ' ')) {
  952. if (*p == '-')
  953. state = 1;
  954. else if (*p == '+')
  955. state = 0;
  956. else if ((!state) && (*p == 'o'))
  957. cnt++;
  958. p++;
  959. }
  960. return (cnt >= 1);
  961. }
  962. int ismdop(char *mode)
  963. {
  964. int state = 0,
  965. cnt = 0;
  966. char *p;
  967. p = mode;
  968. while ((*p) && (*p != ' ')) {
  969. if (*p == '-')
  970. state = 1;
  971. else if (*p == '+')
  972. state = 0;
  973. else if ((state) && (*p == 'o'))
  974. cnt++;
  975. p++;
  976. }
  977. return (cnt >= 3);
  978. }
  979. static void server_activity(int idx, char *msg, int len)
  980. {
  981. char *from, *code;
  982. if (trying_server) {
  983. strcpy(dcc[idx].nick, "(server)");
  984. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  985. trying_server = 0;
  986. SERVER_SOCKET.timeout_val = 0;
  987. }
  988. waiting_for_awake = 0;
  989. from = "";
  990. if (msg[0] == ':') {
  991. msg++;
  992. from = newsplit(&msg);
  993. }
  994. code = newsplit(&msg);
  995. if (use_console_r) {
  996. if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
  997. if (!match_ignore(from))
  998. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  999. } else
  1000. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  1001. }
  1002. /* This has GOT to go into the raw binding table, * merely because this
  1003. * is less effecient.
  1004. */
  1005. check_tcl_raw(from, code, msg);
  1006. }
  1007. static int gotping(char *from, char *msg)
  1008. {
  1009. fixcolon(msg);
  1010. dprintf(DP_MODE, "PONG :%s\n", msg);
  1011. return 0;
  1012. }
  1013. static int gotkick(char *from, char *msg)
  1014. {
  1015. char *nick;
  1016. nick = from;
  1017. if (rfc_casecmp(nick, botname))
  1018. /* Not my kick, I don't need to bother about it. */
  1019. return 0;
  1020. if (use_penalties) {
  1021. last_time += 2;
  1022. if (debug_output)
  1023. putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
  1024. }
  1025. return 0;
  1026. }
  1027. /* Another sec penalty if bot did a whois on another server.
  1028. */
  1029. static int whoispenalty(char *from, char *msg)
  1030. {
  1031. struct server_list *x = serverlist;
  1032. int i, ii;
  1033. if (x && use_penalties) {
  1034. i = ii = 0;
  1035. for (; x; x = x->next) {
  1036. if (i == curserv) {
  1037. if ((strict_servernames == 1) || !x->realname) {
  1038. if (strcmp(x->name, from))
  1039. ii = 1;
  1040. } else {
  1041. if (strcmp(x->realname, from))
  1042. ii = 1;
  1043. }
  1044. }
  1045. i++;
  1046. }
  1047. if (ii) {
  1048. last_time += 1;
  1049. if (debug_output)
  1050. putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
  1051. }
  1052. }
  1053. return 0;
  1054. }
  1055. static int got311(char *from, char *msg)
  1056. {
  1057. char *n1, *n2, *u, *h;
  1058. n1 = newsplit(&msg);
  1059. n2 = newsplit(&msg);
  1060. u = newsplit(&msg);
  1061. h = newsplit(&msg);
  1062. if (!n1 || !n2 || !u || !h)
  1063. return 0;
  1064. if (match_my_nick(n2))
  1065. egg_snprintf(botuserhost, sizeof botuserhost, "%s@%s", u, h);
  1066. return 0;
  1067. }
  1068. static cmd_t my_raw_binds[] =
  1069. {
  1070. {"PRIVMSG", "", (Function) gotmsg, NULL},
  1071. {"NOTICE", "", (Function) gotnotice, NULL},
  1072. {"MODE", "", (Function) gotmode, NULL},
  1073. {"PING", "", (Function) gotping, NULL},
  1074. {"PONG", "", (Function) gotpong, NULL},
  1075. {"WALLOPS", "", (Function) gotwall, NULL},
  1076. {"001", "", (Function) got001, NULL},
  1077. {"251", "", (Function) got251, NULL},
  1078. {"303", "", (Function) got303, NULL},
  1079. {"432", "", (Function) got432, NULL},
  1080. {"433", "", (Function) got433, NULL},
  1081. {"437", "", (Function) got437, NULL},
  1082. {"438", "", (Function) got438, NULL},
  1083. {"451", "", (Function) got451, NULL},
  1084. {"442", "", (Function) got442, NULL},
  1085. {"NICK", "", (Function) gotnick, NULL},
  1086. {"ERROR", "", (Function) goterror, NULL},
  1087. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1088. {"ERROR:", "", (Function) goterror, NULL},
  1089. {"KICK", "", (Function) gotkick, NULL},
  1090. {"318", "", (Function) whoispenalty, NULL},
  1091. {"311", "", (Function) got311, NULL},
  1092. {NULL, NULL, NULL, NULL}
  1093. };
  1094. static void server_resolve_success(int);
  1095. static void server_resolve_failure(int);
  1096. /* Hook up to a server
  1097. */
  1098. static void connect_server(void)
  1099. {
  1100. char pass[121], botserver[UHOSTLEN];
  1101. static int oldserv = -1;
  1102. int servidx;
  1103. unsigned int botserverport = 0;
  1104. waiting_for_awake = 0;
  1105. trying_server = now;
  1106. empty_msgq();
  1107. /* Start up the counter (always reset it if "never-give-up" is on) */
  1108. if ((oldserv < 0) || (never_give_up))
  1109. oldserv = curserv;
  1110. if (newserverport) { /* Jump to specified server */
  1111. curserv = (-1); /* Reset server list */
  1112. strcpy(botserver, newserver);
  1113. botserverport = newserverport;
  1114. strcpy(pass, newserverpass);
  1115. newserver[0] = 0;
  1116. newserverport = 0;
  1117. newserverpass[0] = 0;
  1118. oldserv = (-1);
  1119. } else
  1120. pass[0] = 0;
  1121. if (!cycle_time) {
  1122. struct chanset_t *chan;
  1123. struct server_list *x = serverlist;
  1124. if (!x)
  1125. return;
  1126. servidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1127. if (servidx < 0) {
  1128. putlog(LOG_SERV, "*",
  1129. "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1130. return;
  1131. }
  1132. next_server(&curserv, botserver, &botserverport, pass);
  1133. putlog(LOG_SERV, "*", "%s %s:%d", IRC_SERVERTRY, botserver, botserverport);
  1134. dcc[servidx].port = botserverport;
  1135. strcpy(dcc[servidx].nick, "(server)");
  1136. strncpyz(dcc[servidx].host, botserver, UHOSTLEN);
  1137. botuserhost[0] = 0;
  1138. nick_juped = 0;
  1139. for (chan = chanset; chan; chan = chan->next)
  1140. chan->status &= ~CHAN_JUPED;
  1141. dcc[servidx].timeval = now;
  1142. dcc[servidx].sock = -1;
  1143. dcc[servidx].u.dns->host = get_data_ptr(strlen(dcc[servidx].host) + 1);
  1144. strcpy(dcc[servidx].u.dns->host, dcc[servidx].host);
  1145. dcc[servidx].u.dns->cbuf = get_data_ptr(strlen(pass) + 1);
  1146. strcpy(dcc[servidx].u.dns->cbuf, pass);
  1147. dcc[servidx].u.dns->dns_success = server_resolve_success;
  1148. dcc[servidx].u.dns->dns_failure = server_resolve_failure;
  1149. dcc[servidx].u.dns->dns_type = RES_IPBYHOST;
  1150. dcc[servidx].u.dns->type = &SERVER_SOCKET;
  1151. if (server_cycle_wait)
  1152. /* Back to 1st server & set wait time.
  1153. * Note: Put it here, just in case the server quits on us quickly
  1154. */
  1155. cycle_time = server_cycle_wait;
  1156. else
  1157. cycle_time = 0;
  1158. /* I'm resolving... don't start another server connect request */
  1159. resolvserv = 1;
  1160. /* Resolve the hostname. */
  1161. #ifdef USE_IPV6
  1162. server_resolve_success(servidx);
  1163. #else
  1164. dcc_dnsipbyhost(dcc[servidx].host);
  1165. #endif /* USE_IPV6 */
  1166. }
  1167. }
  1168. static void server_resolve_failure(int servidx)
  1169. {
  1170. serv = -1;
  1171. resolvserv = 0;
  1172. putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host,
  1173. IRC_DNSFAILED);
  1174. lostdcc(servidx);
  1175. }
  1176. static void server_resolve_success(int servidx)
  1177. {
  1178. int oldserv = dcc[servidx].u.dns->ibuf;
  1179. #ifdef S_NODELAY
  1180. int i = 0;
  1181. #endif
  1182. char s[121], pass[121];
  1183. resolvserv = 0;
  1184. dcc[servidx].addr = dcc[servidx].u.dns->ip;
  1185. strcpy(pass, dcc[servidx].u.dns->cbuf);
  1186. changeover_dcc(servidx, &SERVER_SOCKET, 0);
  1187. #ifdef USE_IPV6
  1188. serv = open_telnet(dcc[servidx].host, dcc[servidx].port);
  1189. #else
  1190. serv = open_telnet(iptostr(htonl(dcc[servidx].addr)), dcc[servidx].port);
  1191. #endif /* USE_IPV6 */
  1192. if (serv < 0) {
  1193. neterror(s);
  1194. putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host,
  1195. s);
  1196. lostdcc(servidx);
  1197. if (oldserv == curserv && !never_give_up)
  1198. fatal("NO SERVERS WILL ACCEPT MY CONNECTION.", 0);
  1199. } else {
  1200. dcc[servidx].sock = serv;
  1201. #ifdef HAVE_SSL
  1202. if (!ssl_link(dcc[servidx].sock, CONNECT_SSL)) {
  1203. dcc[servidx].ssl = 0;
  1204. putlog(LOG_SERV, "*", "SSL for '%s' failed", dcc[servidx].host);
  1205. } else {
  1206. putlog(LOG_SERV, "*", "SSL for '%s' successful", dcc[servidx].host);
  1207. dcc[servidx].ssl = 1;
  1208. }
  1209. #else
  1210. dcc[servidx].ssl = 0;
  1211. #endif /* HAVE_SSL */
  1212. #ifdef S_NODELAY
  1213. i = 1;
  1214. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(i));
  1215. #endif /* S_NODELAY */
  1216. /* Queue standard login */
  1217. dcc[servidx].timeval = now;
  1218. SERVER_SOCKET.timeout_val = &server_timeout;
  1219. /* Another server may have truncated it, so use the original */
  1220. strcpy(botname, origbotname);
  1221. /* Start alternate nicks from the beginning */
  1222. altnick_char = 0;
  1223. if (pass[0])
  1224. dprintf(DP_MODE, "PASS %s\n", pass);
  1225. dprintf(DP_MODE, "NICK %s\n", botname);
  1226. dprintf(DP_MODE, "USER %s . . :%s\n", botuser, botrealname);
  1227. /* Wait for async result now */
  1228. }
  1229. }
  1230. #endif /* LEAF */