servmsg.c 34 KB

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