servmsg.c 36 KB

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