servmsg.c 35 KB

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