servmsg.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * servmsg.c -- part of server.mod
  22. *
  23. */
  24. #include <netinet/tcp.h>
  25. char cursrvname[120] = "";
  26. char curnetwork[120] = "";
  27. static time_t last_ctcp = (time_t) 0L;
  28. static int count_ctcp = 0;
  29. char altnick_char = 0;
  30. unsigned int rolls = 0;
  31. #define ROLL_RIGHT
  32. #undef ROLL_LEFT
  33. static void rotate_nick(char *nick, char *orignick)
  34. {
  35. size_t len = strlen(nick);
  36. int use_chr = 1;
  37. #ifdef DEBUG
  38. sdprintf("rotate_nick(%s, %s) rolls: %d altnick_char: %c", nick, orignick, rolls, altnick_char);
  39. #endif
  40. /* First run? */
  41. if (altnick_char == 0 && !rolls && altchars[0]) {
  42. altnick_char = altchars[0];
  43. /* the nick is already as long as it can be. */
  44. if (len == (unsigned) nick_len) {
  45. /* make the last char the current altnick_char */
  46. nick[len - 1] = altnick_char;
  47. } else {
  48. /* tack it on to the end */
  49. nick[len] = altnick_char;
  50. nick[len + 1] = 0;
  51. }
  52. } else {
  53. char *p = NULL;
  54. if ((p = strchr(altchars, altnick_char)))
  55. p++;
  56. /* if we haven't been rolling, use the ALTCHARS
  57. */
  58. if (!rolls && p && *p) {
  59. altnick_char = *p;
  60. /* if we've already rolled, just keep rolling until we've rolled completely
  61. * after that, just generate random chars
  62. */
  63. } else if (rolls < len) {
  64. /* fun BX style rolling, WEEEE */
  65. char tmp = 0;
  66. size_t i = 0;
  67. altnick_char = 0;
  68. use_chr = 0;
  69. if (rolls == 0) {
  70. strlcpy(nick, orignick, sizeof(botname));
  71. len = strlen(nick);
  72. }
  73. #ifdef ROLL_RIGHT
  74. tmp = nick[len - 1];
  75. #endif /* ROLL_RIGHT */
  76. #ifdef ROLL_LEFT
  77. tmp = nick[0];
  78. #endif /* ROLL_LEFT */
  79. if (strchr(BADHANDCHARS, tmp))
  80. tmp = '_';
  81. rolls++;
  82. #ifdef ROLL_RIGHT
  83. for (i = (len - 1); i > 0; i--)
  84. nick[i] = nick[i - 1];
  85. nick[0] = tmp;
  86. #endif /* ROLL_RIGHT */
  87. #ifdef ROLL_LEFT
  88. for (i = 0; i < (len - 1); i++)
  89. nick[i] = nick[i + 1];
  90. nick[len - 1] = tmp;
  91. #endif /* ROLL_LEFT */
  92. nick[len] = 0;
  93. } else {
  94. /* we've tried ALTCHARS, and rolled the nick, just use random chars now */
  95. altnick_char = 'a' + randint(26);
  96. }
  97. if (use_chr && altnick_char) {
  98. nick[len - 1] = altnick_char;
  99. nick[len] = 0;
  100. }
  101. }
  102. }
  103. //This is only called on failed nick on connect
  104. static int gotfake433(char *nick)
  105. {
  106. //Failed to get jupenick on connect, try normal nick
  107. if (altnick_char == 0 && jupenick[0] && !rfc_casecmp(botname, jupenick)) {
  108. strlcpy(botname, origbotname, NICKLEN);
  109. } else //Rotate on failed normal nick
  110. rotate_nick(botname, origbotname);
  111. putlog(LOG_SERV, "*", "NICK IN USE: '%s' Trying '%s'", nick, botname);
  112. dprintf(DP_SERVER, "NICK %s\n", botname);
  113. return 0;
  114. }
  115. /* Check for tcl-bound msg command, return 1 if found
  116. *
  117. * msg: proc-name <nick> <user@host> <handle> <args...>
  118. */
  119. static void check_bind_msg(const char *cmd, char *nick, char *uhost, struct userrec *u, char *args)
  120. {
  121. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  122. int x;
  123. get_user_flagrec(u, &fr, NULL);
  124. x = check_bind(BT_msg, cmd, &fr, nick, uhost, u, args);
  125. if (x & BIND_RET_LOG)
  126. putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, u ? u->handle : "*" , cmd, args);
  127. else if (x == 0)
  128. putlog(LOG_MSGS, "*", "[%s!%s] %s %s", nick, uhost, cmd, args);
  129. }
  130. /* Return 1 if processed.
  131. */
  132. static int check_bind_raw(char *from, char *code, char *msg)
  133. {
  134. char *p1 = NULL, *p2 = NULL, *myfrom = NULL, *mymsg = NULL;
  135. int ret = 0;
  136. myfrom = p1 = strdup(from);
  137. mymsg = p2 = strdup(msg);
  138. ret = check_bind(BT_raw, code, NULL, myfrom, mymsg);
  139. free(p1);
  140. free(p2);
  141. return ret;
  142. }
  143. int check_bind_ctcpr(char *nick, char *uhost, struct userrec *u,
  144. char *dest, char *keyword, char *args,
  145. bind_table_t *table)
  146. {
  147. struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0 };
  148. get_user_flagrec(u, &fr, NULL);
  149. return check_bind(table, keyword, &fr, nick, uhost, u, dest, keyword, args);
  150. }
  151. bool match_my_nick(char *nick)
  152. {
  153. return (!rfc_casecmp(nick, botname));
  154. }
  155. void rehash_monitor_list() {
  156. if (!use_monitor) return;
  157. dprintf(DP_SERVER, "MONITOR C\n");
  158. if (jupenick[0])
  159. dprintf(DP_SERVER, "MONITOR + %s,%s\n", jupenick, origbotname);
  160. else
  161. dprintf(DP_SERVER, "MONITOR + %s", origbotname);
  162. }
  163. void rehash_server(const char *servname, const char *nick)
  164. {
  165. server_online = now;
  166. altnick_char = 0;
  167. strlcpy(cursrvname, servname, sizeof(cursrvname));
  168. if (servidx >= 0)
  169. curservport = dcc[servidx].port;
  170. if (nick && nick[0]) {
  171. strlcpy(botname, nick, NICKLEN);
  172. dprintf(DP_SERVER, "WHOIS %s\n", botname); /* get user@host */
  173. dprintf(DP_SERVER, "USERHOST %s\n", botname); /* get user@ip */
  174. dprintf(DP_SERVER, "MODE %s %s\n", botname, var_get_str_by_name("usermode"));
  175. rehash_monitor_list();
  176. }
  177. }
  178. void
  179. join_chans()
  180. {
  181. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  182. if (shouldjoin(chan)) {
  183. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING);
  184. dprintf(DP_SERVER, "JOIN %s %s\n", (chan->name[0]) ? chan->name : chan->dname,
  185. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  186. chan->status |= CHAN_JOINING;
  187. }
  188. }
  189. }
  190. /* 001: welcome to IRC (use it to fix the server name)
  191. */
  192. static int got001(char *from, char *msg)
  193. {
  194. fixcolon(msg);
  195. rehash_server(from, msg);
  196. /* Ok...param #1 of 001 = what server thinks my nick is */
  197. join_chans();
  198. #ifdef no
  199. if (strcasecmp(from, dcc[servidx].host)) {
  200. struct server_list *x = serverlist;
  201. if (x == NULL)
  202. return 0; /* Uh, no server list */
  203. putlog(LOG_MISC, "*", "(%s claims to be %s; updating server list)", dcc[servidx].host, from);
  204. for (int i = curserv; i > 0 && x != NULL; i--)
  205. x = x->next;
  206. if (x == NULL) {
  207. putlog(LOG_MISC, "*", "Invalid server list!");
  208. return 0;
  209. }
  210. }
  211. #endif
  212. return 0;
  213. }
  214. /* <server> 004 <to> <servername> <version> <user modes> <channel modes> */
  215. static int
  216. got004(char *from, char *msg)
  217. {
  218. char *tmp = NULL;
  219. newsplit(&msg); /* nick */
  220. newsplit(&msg); /* server */
  221. //Cache the results for later parsing if needed (after a restart)
  222. //Sending 'VERSION' does not work on all servers, plus this speeds
  223. //up a restart by removing the need to wait for the information
  224. if (!replaying_cache)
  225. dprintf(DP_CACHE, ":%s 004 . . %s", from, msg);
  226. tmp = newsplit(&msg);
  227. /* cookies won't work on ircu or Unreal or snircd */
  228. if (strstr(tmp, "u2.") || strstr(tmp, "Unreeal") || strstr(tmp, "snircd")) {
  229. putlog(LOG_DEBUG, "*", "Disabling cookies as they are not supported on %s", cursrvname);
  230. cookies_disabled = true;
  231. }
  232. return 0;
  233. }
  234. /* <server> 005 <to> <option> <option> <... option> :are supported by this server */
  235. static int
  236. got005(char *from, char *msg)
  237. {
  238. char *tmp = NULL, *p, *p2;
  239. newsplit(&msg); /* nick */
  240. //Cache the results for later parsing if needed (after a restart)
  241. //Sending 'VERSION' does not work on all servers, plus this speeds
  242. //up a restart by removing the need to wait for the information
  243. if (!replaying_cache)
  244. dprintf(DP_CACHE, ":%s 005 . %s", from, msg);
  245. while ((tmp = newsplit(&msg))[0]) {
  246. p = NULL;
  247. if ((p = strchr(tmp, '=')))
  248. *p++ = 0;
  249. if (!strcasecmp(tmp, ":are"))
  250. break;
  251. else if (!strcasecmp(tmp, "MODES")) {
  252. modesperline = atoi(p);
  253. if (modesperline > MODES_PER_LINE_MAX)
  254. modesperline = MODES_PER_LINE_MAX;
  255. } else if (!strcasecmp(tmp, "NICKLEN"))
  256. nick_len = atoi(p);
  257. else if (!strcasecmp(tmp, "MONITOR")) {
  258. bool need_to_rehash_monitor = 0;
  259. if (!use_monitor && !replaying_cache) // Don't rehash if restarting. No need.
  260. need_to_rehash_monitor = 1;
  261. use_monitor = 1;
  262. if (need_to_rehash_monitor)
  263. rehash_monitor_list();
  264. }
  265. else if (!strcasecmp(tmp, "NETWORK"))
  266. strlcpy(curnetwork, p, 120);
  267. else if (!strcasecmp(tmp, "PENALTY"))
  268. use_penalties = 1;
  269. else if (!strcasecmp(tmp, "WHOX"))
  270. use_354 = 1;
  271. else if (!strcasecmp(tmp, "EXCEPTS"))
  272. use_exempts = 1;
  273. else if (!strcasecmp(tmp, "INVEX"))
  274. use_invites = 1;
  275. else if (!strcasecmp(tmp, "MAXBANS")) {
  276. max_bans = atoi(p);
  277. max_modes = max_bans;
  278. max_exempts = max_bans;
  279. max_invites = max_bans;
  280. }
  281. else if (!strcasecmp(tmp, "MAXLIST")) {
  282. p2 = NULL;
  283. if ((p2 = strchr(p, ':'))) {
  284. *p2++ = 0;
  285. max_modes = atoi(p2);
  286. if (strchr(p, 'e'))
  287. max_exempts = max_modes;
  288. if (strchr(p, 'b'))
  289. max_bans = max_modes;
  290. if (strchr(p, 'I'))
  291. max_invites = max_modes;
  292. }
  293. }
  294. else if (!strcasecmp(tmp, "CASEMAPPING")) {
  295. /* we are default set to rfc1459, so only switch if NOT rfc1459 */
  296. if (strcasecmp(p, "rfc1459")) {
  297. rfc_casecmp = strcasecmp;
  298. rfc_toupper = toupper;
  299. }
  300. }
  301. }
  302. return 0;
  303. }
  304. /* Got 442: not on channel
  305. */
  306. static int got442(char *from, char *msg)
  307. {
  308. char *chname = NULL;
  309. struct chanset_t *chan = NULL;
  310. int i;
  311. struct server_list *x = NULL;
  312. for (x = serverlist, i = 0; x; x = x->next, i++)
  313. if (i == curserv) {
  314. if (strcasecmp(from, x->name))
  315. return 0;
  316. break;
  317. }
  318. newsplit(&msg);
  319. chname = newsplit(&msg);
  320. chan = findchan(chname);
  321. if (chan)
  322. if (shouldjoin(chan) && !channel_joining(chan)) {
  323. putlog(LOG_MISC, chname, "Server says I'm not on channel: %s", chname);
  324. clear_channel(chan, 1);
  325. chan->status &= ~CHAN_ACTIVE;
  326. chan->status |= CHAN_JOINING;
  327. dprintf(DP_MODE, "JOIN %s %s\n", chan->name,
  328. chan->channel.key[0] ? chan->channel.key : chan->key_prot);
  329. }
  330. return 0;
  331. }
  332. /* Close the current server connection.
  333. */
  334. void nuke_server(const char *reason)
  335. {
  336. if (serv >= 0 && servidx >= 0) {
  337. if (reason)
  338. dprintf(-serv, "QUIT :%s\n", reason);
  339. sleep(1);
  340. disconnect_server(servidx, DO_LOST);
  341. }
  342. }
  343. char ctcp_reply[1024] = "";
  344. static int lastmsgs[FLOOD_GLOBAL_MAX];
  345. static char lastmsghost[FLOOD_GLOBAL_MAX][128];
  346. static time_t lastmsgtime[FLOOD_GLOBAL_MAX];
  347. static int dronemsgs;
  348. static time_t dronemsgtime;
  349. static bool set_pls_g;
  350. static interval_t flood_g_time = 60;
  351. rate_t flood_g = { 6, 2 };
  352. void unset_g(int data)
  353. {
  354. dprintf(DP_MODE, "MODE %s :-g\n", botname);
  355. set_pls_g = 0;
  356. }
  357. /* Do on NICK, PRIVMSG, NOTICE and JOIN.
  358. */
  359. static bool detect_flood(char *floodnick, char *floodhost, char *from, int which)
  360. {
  361. struct userrec *u = get_user_by_host(from);
  362. int atr = u ? u->flags : 0;
  363. if ((u && u->bot) || (atr & USER_NOFLOOD))
  364. return 0;
  365. char *p = NULL, ftype[10] = "", h[1024] = "";
  366. int thr = 0;
  367. time_t lapse = 0;
  368. /* Determine how many are necessary to make a flood */
  369. switch (which) {
  370. case FLOOD_PRIVMSG:
  371. case FLOOD_NOTICE:
  372. thr = flood_msg.count;
  373. lapse = flood_msg.time;
  374. strlcpy(ftype, "msg", 4);
  375. break;
  376. case FLOOD_CTCP:
  377. thr = flood_ctcp.count;
  378. lapse = flood_ctcp.time;
  379. strlcpy(ftype, "ctcp", 5);
  380. break;
  381. }
  382. if ((thr == 0) || (lapse == 0))
  383. return 0; /* No flood protection */
  384. /* Okay, make sure i'm not flood-checking myself */
  385. if (match_my_nick(floodnick))
  386. return 0;
  387. if (!strcasecmp(floodhost, botuserhost))
  388. return 0; /* My user@host (?) */
  389. //FIXME: hack for +g
  390. if (dronemsgtime < now - flood_g.time) { //expired, reset counter
  391. dronemsgs = 0;
  392. // dronemsgtime = now;
  393. }
  394. dronemsgs++;
  395. dronemsgtime = now;
  396. if (!set_pls_g && dronemsgs >= flood_g.count) { //flood from dronenet, let's attempt to set +g
  397. egg_timeval_t howlong;
  398. set_pls_g = 1;
  399. dronemsgs = 0;
  400. dronemsgtime = 0;
  401. dprintf(DP_DUMP, "MODE %s :+g\n", botname);
  402. howlong.sec = flood_g_time;
  403. howlong.usec = 0;
  404. timer_create(&howlong, "Unset umode +g", (Function) unset_g);
  405. putlog(LOG_MISC, "*", "Drone flood detected! Setting +g for %d seconds.", flood_g_time);
  406. return 1; //ignore the current msg
  407. }
  408. p = strchr(floodhost, '@');
  409. if (p) {
  410. p++;
  411. if (strcasecmp(lastmsghost[which], p)) { /* New */
  412. strlcpy(lastmsghost[which], p, 128);
  413. lastmsgtime[which] = now;
  414. lastmsgs[which] = 0;
  415. return 0;
  416. }
  417. } else
  418. return 0; /* Uh... whatever. */
  419. if (lastmsgtime[which] < now - lapse) {
  420. /* Flood timer expired, reset it */
  421. lastmsgtime[which] = now;
  422. lastmsgs[which] = 0;
  423. return 0;
  424. }
  425. lastmsgs[which]++;
  426. if (lastmsgs[which] >= thr) { /* FLOOD */
  427. /* Reset counters */
  428. lastmsgs[which] = 0;
  429. lastmsgtime[which] = 0;
  430. lastmsghost[which][0] = 0;
  431. u = get_user_by_host(from);
  432. /* Private msg */
  433. simple_snprintf(h, sizeof(h), "*!*@%s", p);
  434. putlog(LOG_MISC, "*", "Flood from @%s! Placing on ignore!", p);
  435. addignore(h, conf.bot->nick, (which == FLOOD_CTCP) ? "CTCP flood" : "MSG/NOTICE flood", now + (60 * ignore_time));
  436. return 1;
  437. }
  438. return 0;
  439. }
  440. /* Check for more than 8 control characters in a line.
  441. * This could indicate: beep flood CTCP avalanche.
  442. */
  443. bool detect_avalanche(char *msg)
  444. {
  445. int count = 0;
  446. for (unsigned char *p = (unsigned char *) msg; (*p) && (count < 8); p++)
  447. if ((*p == 7) || (*p == 1))
  448. count++;
  449. if (count >= 8)
  450. return 1;
  451. else
  452. return 0;
  453. }
  454. /* Got a private message.
  455. */
  456. static int gotmsg(char *from, char *msg)
  457. {
  458. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  459. (*msg == '@'))) /* Notice to a channel, not handled here */
  460. return 0;
  461. char *to = NULL, buf[UHOSTLEN] = "", *nick = NULL, ctcpbuf[512] = "", *uhost = buf,
  462. *ctcp = NULL, *p = NULL, *p1 = NULL, *code = NULL;
  463. struct userrec *u = NULL;
  464. int ctcp_count = 0;
  465. bool ignoring = match_ignore(from);
  466. to = newsplit(&msg);
  467. fixcolon(msg);
  468. /* Only check if flood-ctcp is active */
  469. strlcpy(uhost, from, UHOSTLEN);
  470. nick = splitnick(&uhost);
  471. if (flood_ctcp.count && detect_avalanche(msg)) {
  472. if (!ignoring) {
  473. putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
  474. p = strchr(uhost, '@');
  475. if (p != NULL)
  476. p++;
  477. else
  478. p = uhost;
  479. simple_snprintf(ctcpbuf, sizeof(ctcpbuf), "*!*@%s", p);
  480. addignore(ctcpbuf, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  481. ignoring = 1;
  482. }
  483. }
  484. /* Check for CTCP: */
  485. ctcp_reply[0] = 0;
  486. p = strchr(msg, 1);
  487. while ((p != NULL) && (*p)) {
  488. p++;
  489. p1 = p;
  490. while ((*p != 1) && (*p != 0))
  491. p++;
  492. if (*p == 1) {
  493. *p = 0;
  494. strlcpy(ctcpbuf, p1, sizeof(ctcpbuf));
  495. ctcp = ctcpbuf;
  496. strcpy(p1 - 1, p + 1);
  497. if (!ignoring)
  498. detect_flood(nick, uhost, from, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  499. /* Respond to the first answer_ctcp */
  500. p = strchr(msg, 1);
  501. if (ctcp_count < answer_ctcp) {
  502. ctcp_count++;
  503. if (ctcp[0] != ' ') {
  504. code = newsplit(&ctcp);
  505. if ((to[0] == '$') || strchr(to, '.')) {
  506. if (!ignoring) {
  507. /* Don't interpret */
  508. putlog(LOG_PUBLIC, to, "CTCP %s from %s (%s) to %s: %s", code, nick, uhost, to, ctcp);
  509. }
  510. } else {
  511. u = get_user_by_host(from);
  512. if (!ignoring || trigger_on_ignore) {
  513. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG && !ignoring) {
  514. if (!strcmp(code, "DCC")) {
  515. /* If it gets this far unhandled, it means that
  516. * the user is totally unknown.
  517. */
  518. code = newsplit(&ctcp);
  519. if (!strcmp(code, "CHAT")) {
  520. if (!ischanhub())
  521. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (I'm not a chathub (+c))", from);
  522. else
  523. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (no access)", from);
  524. } else {
  525. putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from);
  526. }
  527. } else if (!strcmp(code, "CHAT")) {
  528. if (!ischanhub())
  529. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (I'm not a chathub (+c))", from);
  530. else
  531. putlog(LOG_MISC, "*", "%s: %s", "Refused DCC chat (no access)", from);
  532. }
  533. }
  534. if (!strcmp(code, "ACTION")) {
  535. putlog(LOG_MSGS, "*", "* %s (%s): %s", nick, uhost, ctcp);
  536. } else {
  537. putlog(LOG_MSGS, "*", "CTCP %s: from %s (%s): %s", code, nick, uhost, ctcp);
  538. } /* I love a good close cascade ;) */
  539. }
  540. }
  541. }
  542. }
  543. }
  544. }
  545. /* Send out possible ctcp responses */
  546. if (ctcp_reply[0]) {
  547. if (ctcp_mode != 2) {
  548. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  549. } else {
  550. if (now - last_ctcp > flood_ctcp.time) {
  551. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  552. count_ctcp = 1;
  553. } else if (count_ctcp < flood_ctcp.count) {
  554. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  555. count_ctcp++;
  556. }
  557. last_ctcp = now;
  558. }
  559. }
  560. if (msg[0]) {
  561. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  562. /* Msg from oper */
  563. if (!ignoring) {
  564. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  565. /* Do not interpret as command */
  566. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  567. }
  568. } else {
  569. char *my_code = NULL;
  570. Auth *auth = NULL;
  571. if (auth_prefix[0])
  572. auth = Auth::Find(uhost);
  573. if (!auth && !ignoring)
  574. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  575. my_code = newsplit(&msg);
  576. rmspace(msg);
  577. /* is it a cmd? */
  578. if (auth_prefix[0] && my_code && my_code[0] && my_code[1] && auth && auth->Authed() && my_code[0] == auth_prefix[0]) {
  579. my_code++; //eliminate the prefix
  580. auth->atime = now;
  581. if (!check_bind_authc(my_code, auth, NULL, msg))
  582. putlog(LOG_MSGS, "*", "[%s] %c%s %s", from, auth_prefix[0], my_code, msg);
  583. } else if (!ignoring && (my_code[0] != auth_prefix[0] || !my_code[1] || !auth || !auth->Authed())) {
  584. struct userrec *my_u = get_user_by_host(from);
  585. bool doit = 1;
  586. if (!strcasecmp(my_code, "op") || !strcasecmp(my_code, "pass") || !strcasecmp(my_code, "invite")
  587. || !strcasecmp(my_code, "ident")
  588. || !strcasecmp(my_code, msgop) || !strcasecmp(my_code, msgpass)
  589. || !strcasecmp(my_code, msginvite) || !strcasecmp(my_code, msgident)) {
  590. const char *buf2 = NULL;
  591. doit = 0;
  592. if (!strcasecmp(my_code, msgop))
  593. buf2 = "op";
  594. else if (!strcasecmp(my_code, msgpass))
  595. buf2 = "pass";
  596. else if (!strcasecmp(my_code, msginvite))
  597. buf2 = "invite";
  598. else if (!strcasecmp(my_code, msgident))
  599. buf2 = "ident";
  600. if (buf2)
  601. check_bind_msg(buf2, nick, uhost, my_u, msg);
  602. else
  603. putlog(LOG_MSGS, "*", "(%s!%s) attempted to use invalid msg cmd '%s'", nick, uhost, my_code);
  604. }
  605. if (doit)
  606. check_bind_msg(my_code, nick, uhost, my_u, msg);
  607. }
  608. }
  609. }
  610. return 0;
  611. }
  612. /* Got a private notice.
  613. */
  614. static int gotnotice(char *from, char *msg)
  615. {
  616. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  617. (*msg == '@'))) /* Notice to a channel, not handled here */
  618. return 0;
  619. char *to = NULL, *nick = NULL, ctcpbuf[512] = "", *p = NULL, *p1 = NULL, buf[512] = "",
  620. *uhost = buf, *ctcp = NULL, *ctcpmsg = NULL, *ptr = NULL;
  621. struct userrec *u = NULL;
  622. bool ignoring = match_ignore(from);
  623. to = newsplit(&msg);
  624. fixcolon(msg);
  625. strlcpy(uhost, from, UHOSTLEN);
  626. nick = splitnick(&uhost);
  627. if (flood_ctcp.count && detect_avalanche(msg)) {
  628. /* Discard -- kick user if it was to the channel */
  629. if (!ignoring)
  630. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  631. return 0;
  632. }
  633. /* Check for CTCP: */
  634. ctcpmsg = ptr = strdup(msg);
  635. p = strchr(ctcpmsg, 1);
  636. while ((p != NULL) && (*p)) {
  637. p++;
  638. p1 = p;
  639. while ((*p != 1) && (*p != 0))
  640. p++;
  641. if (*p == 1) {
  642. *p = 0;
  643. strlcpy(ctcpbuf, p1, sizeof(ctcpbuf));
  644. ctcp = ctcpbuf;
  645. strcpy(p1 - 1, p + 1);
  646. if (!ignoring)
  647. detect_flood(nick, uhost, from, FLOOD_CTCP);
  648. p = strchr(ctcpmsg, 1);
  649. if (ctcp[0] != ' ') {
  650. char *code = newsplit(&ctcp);
  651. if ((to[0] == '$') || strchr(to, '.')) {
  652. if (!ignoring)
  653. putlog(LOG_PUBLIC, "*",
  654. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  655. } else {
  656. u = get_user_by_host(from);
  657. if (!ignoring || trigger_on_ignore) {
  658. check_bind_ctcr(nick, uhost, u, to, code, ctcp);
  659. if (!ignoring)
  660. /* Who cares? */
  661. putlog(LOG_MSGS, "*", "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  662. }
  663. }
  664. }
  665. }
  666. }
  667. free(ptr);
  668. if (msg[0]) {
  669. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  670. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  671. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s", nick, uhost, to, msg);
  672. } else {
  673. /* Server notice? */
  674. if ((nick[0] == 0) || (uhost[0] == 0)) {
  675. if (!server_online &&
  676. strstr(msg, "*** You are exempt from flood"))
  677. floodless = 1;
  678. /* Hidden `250' connection count message from server */
  679. if (strncmp(msg, "Highest connection count:", 25))
  680. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  681. } else if (!ignoring) {
  682. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  683. u = get_user_by_host(from);
  684. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  685. }
  686. }
  687. }
  688. return 0;
  689. }
  690. /* WALLOPS: oper's nuisance
  691. */
  692. static int gotwall(char *from, char *msg)
  693. {
  694. fixcolon(msg);
  695. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  696. return 0;
  697. }
  698. void server_send_ison()
  699. {
  700. if (server_online && keepnick && !use_monitor) {
  701. // Only check if we're not on jupenick, or there is no jupenick and we're not on the preferred nick
  702. bool have_jupenick = jupenick[0] ? match_my_nick(jupenick) : 0;
  703. if (!have_jupenick) {
  704. /* See if my nickname is in use and if if my nick is right. */
  705. if (jupenick[0] && !have_jupenick)
  706. dprintf(DP_SERVER, "ISON %s %s\n", origbotname, jupenick);
  707. else if (!match_my_nick(origbotname))
  708. dprintf(DP_SERVER, "ISON %s\n", origbotname);
  709. }
  710. }
  711. }
  712. /* Called once a minute... but if we're the only one on the
  713. * channel, we only wanna send out "lusers" once every 5 mins.
  714. */
  715. static void minutely_checks()
  716. {
  717. /* Only check if we have already successfully logged in. */
  718. if (server_online) {
  719. static int count = 4;
  720. bool ok = 0;
  721. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  722. if (channel_active(chan) && chan->channel.members == 1) {
  723. ok = 1;
  724. break;
  725. }
  726. }
  727. if (!ok)
  728. return;
  729. count++;
  730. if (count >= 5) {
  731. dprintf(DP_SERVER, "LUSERS\n");
  732. count = 0;
  733. }
  734. }
  735. }
  736. /* Pong from server.
  737. */
  738. static int gotpong(char *from, char *msg)
  739. {
  740. newsplit(&msg);
  741. fixcolon(msg); /* Scrap server name */
  742. server_lag = now - my_atoul(msg);
  743. if (server_lag > 99999) {
  744. /* IRCnet lagmeter support by drummer */
  745. server_lag = now - lastpingtime;
  746. }
  747. return 0;
  748. }
  749. static int nick_which(const char* nick, bool& is_jupe, bool& is_orig) {
  750. if (jupenick[0] && !rfc_casecmp(nick, jupenick)) {
  751. is_jupe = 1;
  752. // If some stupid reason they have the same jupenick/nick, make sure to mark it as on
  753. if (!rfc_casecmp(nick, origbotname))
  754. is_orig = 1;
  755. return 1;
  756. } else if (!rfc_casecmp(nick, origbotname)) {
  757. is_orig = 1;
  758. }
  759. return 0;
  760. }
  761. static void nick_available(bool is_jupe, bool is_orig) {
  762. if (jupenick[0] && is_jupe && !match_my_nick((jupenick))) {
  763. tried_jupenick = 1;
  764. dprintf(DP_SERVER, "NICK %s\n", jupenick);
  765. if (!jnick_juped)
  766. putlog(LOG_MISC, "*", "Switching back to jupenick '%s'", jupenick);
  767. // Don't switch to the nick if already on jupenick
  768. } else if (is_orig && !match_my_nick(origbotname) && (!jupenick[0] || !match_my_nick(jupenick))) {
  769. altnick_char = rolls = 0;
  770. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  771. if (!nick_juped)
  772. putlog(LOG_MISC, "*", "Switching back to nick '%s'", origbotname);
  773. }
  774. }
  775. void nicks_available(char* buf, char delim, bool buf_contains_available) {
  776. if (!buf[0] || !keepnick) return;
  777. bool is_jupe = 0, is_orig = 0;
  778. char *nick = NULL;
  779. if (delim) {
  780. while ((nick = newsplit(&buf, delim))[0]) {
  781. if (nick_which(nick, is_jupe, is_orig))
  782. break;
  783. }
  784. } else {
  785. nick = buf;
  786. nick_which(nick, is_jupe, is_orig);
  787. }
  788. if (buf_contains_available == 0) {
  789. is_jupe = !is_jupe;
  790. is_orig = !is_orig;
  791. }
  792. nick_available(is_jupe, is_orig);
  793. }
  794. /* This is a reply to MONITOR: Offline
  795. * RPL_MONOFFLINE
  796. * :<server> 731 <nick> :nick[,nick1]*
  797. */
  798. static void got731(char* from, char* msg)
  799. {
  800. char *tmp = newsplit(&msg);
  801. fixcolon(msg);
  802. //msg now contains the nick(s) available
  803. if (tmp[0] && (!strcmp(tmp, "*") || match_my_nick(tmp)))
  804. nicks_available(msg, ',');
  805. }
  806. /* This is a reply on ISON :<orig> [<jupenick>]
  807. */
  808. static void got303(char *from, char *msg)
  809. {
  810. char *tmp = newsplit(&msg);
  811. fixcolon(msg);
  812. if (tmp[0] && match_my_nick(tmp))
  813. nicks_available(msg, ' ', 0);
  814. }
  815. /* 432 : Bad nickname (RESV)
  816. */
  817. static int got432(char *from, char *msg)
  818. {
  819. char *erroneus = NULL;
  820. newsplit(&msg);
  821. erroneus = newsplit(&msg);
  822. bool is_jnick = 0, was_juped = 0;
  823. if (jupenick[0] && !strcmp(erroneus, jupenick)) {
  824. was_juped = jnick_juped;
  825. is_jnick = jnick_juped = 1;
  826. } else {
  827. was_juped = nick_juped;
  828. nick_juped = 1;
  829. }
  830. if (server_online) {
  831. if (!was_juped)
  832. putlog(LOG_MISC, "*", "%sNICK IS INVALID: '%s' (keeping '%s').", is_jnick ? "JUPE" : "", erroneus, botname);
  833. } else {
  834. putlog(LOG_MISC, "*", "Server says my %snick '%s' is invalid.", is_jnick ? "jupe" : "", botname);
  835. if (jupenick[0] && !strcmp(botname, jupenick))
  836. strlcpy(botname, origbotname, NICKLEN);
  837. else
  838. rotate_nick(botname, origbotname);
  839. dprintf(DP_MODE, "NICK %s\n", botname);
  840. return 0;
  841. }
  842. return 0;
  843. }
  844. /* 433 : Nickname in use
  845. * Change nicks till we're acceptable or we give up
  846. */
  847. static char rnick[NICKLEN] = "";
  848. static int got433(char *from, char *msg)
  849. {
  850. char *tmp = newsplit(&msg);
  851. /* We are online and have a nickname, we'll keep it */
  852. //Also make sure we're not juping the jupenick if we shouldn't be.
  853. //Prefer to be on the 'nick'(origbotname) at all times
  854. if (server_online) {
  855. tmp = newsplit(&msg);
  856. if (tried_jupenick)
  857. jnick_juped = 0;
  858. else if (!rfc_casecmp(tmp, origbotname))
  859. nick_juped = 0;
  860. sdprintf("433: botname: %s tmp: %s rolls: %d altnick_char: %c tried_jupenick: %d", botname, tmp, rolls, altnick_char, tried_jupenick);
  861. // Tried jupenick, but already on origbotname (or a rolled nick), stay on it.
  862. if (tried_jupenick && (match_my_nick(origbotname) || rolls || altnick_char != 0)) {
  863. putlog(LOG_MISC, "*", "%sNICK IN USE: '%s' (keeping '%s').", tried_jupenick ? "JUPE" : "", tmp, botname);
  864. } else { //Else need to find a new nick
  865. // Failed to get jupenick, not on origbotname now, try for origbotname and rotate from there
  866. if (tried_jupenick) {
  867. strlcpy(rnick, origbotname, NICKLEN);
  868. tried_jupenick = 0;
  869. } else {
  870. // Was a failed attempt at a rotated nick, keep rotating on origbotname
  871. strlcpy(rnick, tmp, NICKLEN);
  872. rotate_nick(rnick, origbotname);
  873. }
  874. // Make sure not trying to set the nick we're already on
  875. if (!match_my_nick(rnick)) {
  876. putlog(LOG_MISC, "*", "NICK IN USE: '%s' Trying '%s'", tmp, rnick);
  877. dprintf(DP_SERVER, "NICK %s\n", rnick);
  878. } else
  879. putlog(LOG_MISC, "*", "NICK IN USE: '%s' (keeping '%s')", tmp, botname);
  880. }
  881. } else {
  882. //Hack for different responses:
  883. //[@] efnet.demon.co.uk 433 * lolwut :Nickname is already in use.
  884. //[@] irc.efnet.no 433 lolwut :Nickname is already in use.
  885. if (tmp[0] == '*')
  886. tmp = newsplit(&msg);
  887. gotfake433(tmp);
  888. }
  889. return 0;
  890. }
  891. /* 437 : Channel/Nickname juped (IRCnet) (Also temp jupe during splits on efnet)
  892. * :ca.us.irc.xzibition.com 437 test1 bryan :Nick/channel is temporarily unavailable
  893. */
  894. static int got437(char *from, char *msg)
  895. {
  896. char *s = NULL;
  897. struct chanset_t *chan = NULL;
  898. newsplit(&msg);
  899. s = newsplit(&msg);
  900. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  901. chan = findchan(s);
  902. if (chan) {
  903. chan->status &= ~(CHAN_JOINING);
  904. if (chan->status & CHAN_ACTIVE) {
  905. putlog(LOG_MISC, "*", "Can't change nickname on %s. Is my nickname banned?", s);
  906. } else {
  907. if (!channel_juped(chan)) {
  908. putlog(LOG_MISC, "*", "Channel %s is juped. :(", s);
  909. chan->status |= CHAN_JUPED;
  910. }
  911. }
  912. }
  913. } else if (server_online) {
  914. if (!rfc_casecmp(s, origbotname)) {
  915. if (!nick_juped)
  916. putlog(LOG_MISC, "*", "NICK IS TEMPORARILY UNAVAILABLE: '%s' (keeping '%s').", s, botname);
  917. nick_juped = 1;
  918. } else if (jupenick[0] && !rfc_casecmp(s, jupenick)) {
  919. if (!jnick_juped)
  920. putlog(LOG_MISC, "*", "JUPENICK IS TEMPORARILY UNAVAILABLE: '%s' (keeping '%s').", s, botname);
  921. jnick_juped = 1;
  922. }
  923. } else {
  924. putlog(LOG_MISC, "*", "%s: %s", "Nickname has been juped", s);
  925. gotfake433(s);
  926. }
  927. return 0;
  928. }
  929. /* 438 : Nick change too fast
  930. */
  931. static int got438(char *from, char *msg)
  932. {
  933. newsplit(&msg);
  934. newsplit(&msg);
  935. fixcolon(msg);
  936. putlog(LOG_MISC, "*", "%s", msg);
  937. return 0;
  938. }
  939. static int got451(char *from, char *msg)
  940. {
  941. /* Usually if we get this then we really messed up somewhere
  942. * or this is a non-standard server, so we log it and kill the socket
  943. * hoping the next server will work :) -poptix
  944. */
  945. /* Um, this does occur on a lagged anti-spoof server connection if the
  946. * (minutely) sending of joins occurs before the bot does its ping reply.
  947. * Probably should do something about it some time - beldin
  948. */
  949. putlog(LOG_MISC, "*", "%s says I'm not registered, trying next one.", from);
  950. nuke_server("The server says we are not registered yet..");
  951. return 0;
  952. }
  953. /* Got error notice
  954. */
  955. static int goterror(char *from, char *msg)
  956. {
  957. if (msg[0] == ':')
  958. msg++;
  959. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  960. putlog(LOG_SERV, "*", "Disconnecting from server.");
  961. nuke_server("Bah, stupid error messages.");
  962. return 1;
  963. }
  964. /* Got nick change.
  965. */
  966. static int gotnick(char *from, char *msg)
  967. {
  968. char *nick = NULL, *buf = NULL, *buf_ptr = NULL;
  969. struct userrec *u = NULL;
  970. //Done to prevent gotnick in irc.mod getting a mangled from
  971. buf = buf_ptr = strdup(from);
  972. //Cache user
  973. u = get_user_by_host(buf);
  974. nick = splitnick(&buf);
  975. fixcolon(msg);
  976. if (match_my_nick(nick)) {
  977. /* Regained nick! */
  978. strlcpy(botname, msg, NICKLEN);
  979. tried_jupenick = 0;
  980. if (jupenick[0] && !rfc_casecmp(msg, jupenick)) {
  981. altnick_char = rolls = 0;
  982. putlog(LOG_SERV | LOG_MISC, "*", "Regained jupenick '%s'.", msg);
  983. jnick_juped = 0;
  984. } else if (!strcmp(msg, origbotname)) {
  985. altnick_char = rolls = 0;
  986. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  987. nick_juped = 0;
  988. } else if (keepnick && strcmp(nick, msg)) {
  989. // Was this an attempt at a nick thru rotation?
  990. if (!strcmp(rnick, msg)) {
  991. putlog(LOG_MISC, "*", "Nickname changed to '%s'", msg);
  992. } else {
  993. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  994. nicks_available(nick);
  995. }
  996. } else
  997. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  998. } else if ((keepnick) && (rfc_casecmp(nick, msg))) { //Ignore case changes
  999. nicks_available(nick);
  1000. }
  1001. free(buf_ptr);
  1002. return 0;
  1003. }
  1004. static int gotmode(char *from, char *msg)
  1005. {
  1006. char *ch = NULL, *buf = NULL, *buf_ptr = NULL;
  1007. buf_ptr = buf = strdup(msg);
  1008. ch = newsplit(&buf);
  1009. /* Usermode changes? */
  1010. if (strchr(CHANMETA, ch[0]) == NULL) {
  1011. if (match_my_nick(ch) && check_mode_r) {
  1012. /* umode +r? - D0H dalnet uses it to mean something different */
  1013. fixcolon(buf);
  1014. if ((buf[0] == '+') && strchr(buf, 'r')) {
  1015. putlog(LOG_MISC | LOG_JOIN, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
  1016. nuke_server("i-lines suck");
  1017. }
  1018. }
  1019. }
  1020. free(buf_ptr);
  1021. return 0;
  1022. }
  1023. static void disconnect_server(int idx, int dolost)
  1024. {
  1025. if ((serv != dcc[idx].sock) && serv >= 0)
  1026. killsock(serv);
  1027. if (dcc[idx].sock >= 0)
  1028. killsock(dcc[idx].sock);
  1029. dcc[idx].sock = -1;
  1030. serv = -1;
  1031. servidx = -1;
  1032. server_online = 0;
  1033. use_monitor = 0;
  1034. cookies_disabled = false;
  1035. floodless = 0;
  1036. botuserhost[0] = 0;
  1037. botuserip[0] = 0;
  1038. if (dolost) {
  1039. Auth::DeleteAll();
  1040. trying_server = 0;
  1041. lostdcc(idx);
  1042. }
  1043. /* Invalidate the cmd_swhois cache callback data */
  1044. for (int i = 0; i < dcc_total; i++) {
  1045. if (dcc[i].type && dcc[i].whois[0]) {
  1046. dcc[i].whois[0] = 0;
  1047. dcc[i].whowas = 0;
  1048. }
  1049. }
  1050. }
  1051. static void eof_server(int idx)
  1052. {
  1053. putlog(LOG_SERV, "*", "Disconnected from %s", dcc[idx].host);
  1054. disconnect_server(idx, DO_LOST);
  1055. }
  1056. static void display_server(int idx, char *buf, size_t bufsiz)
  1057. {
  1058. simple_snprintf(buf, bufsiz, "%s (lag: %d)", trying_server ? "conn" : "serv", server_lag);
  1059. }
  1060. static void connect_server(void);
  1061. static void kill_server(int idx, void *x)
  1062. {
  1063. disconnect_server(idx, NO_LOST); /* eof_server will lostdcc() it. */
  1064. if (!segfaulted) // don't bother if we've segfaulted, too many memory calls in this loop
  1065. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  1066. clear_channel(chan, 1);
  1067. /* A new server connection will be automatically initiated in
  1068. about 2 seconds. */
  1069. }
  1070. static void timeout_server(int idx)
  1071. {
  1072. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  1073. disconnect_server(idx, DO_LOST);
  1074. }
  1075. static void server_activity(int, char *, int);
  1076. struct dcc_table SERVER_SOCKET =
  1077. {
  1078. "SERVER",
  1079. 0,
  1080. eof_server,
  1081. server_activity,
  1082. NULL,
  1083. timeout_server,
  1084. display_server,
  1085. kill_server,
  1086. NULL,
  1087. NULL
  1088. };
  1089. static void server_activity(int idx, char *msg, int len)
  1090. {
  1091. char *from = NULL, *code = NULL;
  1092. if (trying_server) {
  1093. strlcpy(dcc[idx].nick, "(server)", NICKLEN);
  1094. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  1095. trying_server = 0;
  1096. /*
  1097. servidx = idx;
  1098. serv = dcc[idx].sosck;
  1099. */
  1100. SERVER_SOCKET.timeout_val = 0;
  1101. }
  1102. waiting_for_awake = 0;
  1103. if (msg[0] == ':') {
  1104. msg++;
  1105. from = newsplit(&msg);
  1106. } else
  1107. from = "";
  1108. code = newsplit(&msg);
  1109. if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
  1110. if (!match_ignore(from))
  1111. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  1112. } else
  1113. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  1114. /* This has GOT to go into the raw binding table, * merely because this
  1115. * is less effecient.
  1116. */
  1117. check_bind_raw(from, code, msg);
  1118. }
  1119. static int gotping(char *from, char *msg)
  1120. {
  1121. fixcolon(msg);
  1122. dprintf(DP_MODE, "PONG :%s\n", msg);
  1123. return 0;
  1124. }
  1125. static int gotkick(char *from, char *msg)
  1126. {
  1127. if (!match_my_nick(from))
  1128. /* Not my kick, I don't need to bother about it. */
  1129. return 0;
  1130. if (use_penalties) {
  1131. last_time += 2;
  1132. if (debug_output)
  1133. putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
  1134. }
  1135. return 0;
  1136. }
  1137. /* Another sec penalty if bot did a whois on another server.
  1138. */
  1139. static int got318_369(char *, char *, int);
  1140. static int whoispenalty(char *from, char *msg)
  1141. {
  1142. struct server_list *x = serverlist;
  1143. if (x && use_penalties) {
  1144. int i = 0, ii = 0;
  1145. for (; x; x = x->next) {
  1146. if (i == curserv) {
  1147. if (strcmp(x->name, from))
  1148. ii = 1;
  1149. }
  1150. i++;
  1151. }
  1152. if (ii) {
  1153. last_time += 1;
  1154. if (debug_output)
  1155. putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
  1156. }
  1157. }
  1158. got318_369(from, msg, 0);
  1159. return 0;
  1160. }
  1161. static void irc_whois(char *, const char *, ...) __attribute__((format(printf, 2, 3)));
  1162. static void
  1163. irc_whois(char *nick, const char *format, ...)
  1164. {
  1165. char va_out[2001] = "";
  1166. va_list va;
  1167. va_start(va, format);
  1168. egg_vsnprintf(va_out, sizeof(va_out) - 1, format, va);
  1169. va_end(va);
  1170. for (int idx = 0; idx < dcc_total; idx++) {
  1171. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1172. dprintf(idx, "%s\n", va_out);
  1173. break;
  1174. }
  1175. }
  1176. }
  1177. void check_hostmask()
  1178. {
  1179. if (!server_online || !botuserhost[0])
  1180. return;
  1181. char s[UHOSTLEN + 2] = "";
  1182. simple_snprintf(s, sizeof(s), "*!%s", botuserhost); /* just add actual user@ident, regardless of ~ */
  1183. /* dont add the host if it conflicts with another in the userlist */
  1184. struct userrec *u = NULL;
  1185. if ((u = host_conflicts(s))) {
  1186. if (u != conf.bot->u) {
  1187. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  1188. } else
  1189. sdprintf("Already have hostmask '%s' added for myself", s);
  1190. return;
  1191. }
  1192. addhost_by_handle(conf.bot->nick, s);
  1193. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  1194. }
  1195. /* 311 $me nick username address * :realname */
  1196. static int got311(char *from, char *msg)
  1197. {
  1198. char *nick = NULL, *username = NULL, *address = NULL, uhost[UHOSTLEN + 1];
  1199. struct userrec *u = NULL;
  1200. newsplit(&msg);
  1201. nick = newsplit(&msg);
  1202. username = newsplit(&msg);
  1203. address = newsplit(&msg);
  1204. newsplit(&msg);
  1205. fixcolon(msg);
  1206. if (match_my_nick(nick)) {
  1207. simple_snprintf(botuserhost, sizeof botuserhost, "%s@%s", username, address);
  1208. check_hostmask();
  1209. }
  1210. irc_whois(nick, "$b%s$b [%s@%s]", nick, username, address);
  1211. // FIXME: This should incorporate userip and member/client lookups - and should ACT on discovering a user.
  1212. // FIXME: This should also use whois_actually to cache userip
  1213. simple_snprintf(uhost, sizeof uhost, "%s!%s@%s", nick, username, address);
  1214. if ((u = get_user_by_host(uhost))) {
  1215. int idx = 0;
  1216. for (idx = 0; idx < dcc_total; idx++) {
  1217. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1218. if (whois_access(dcc[idx].user, u)) {
  1219. irc_whois(nick, " username : $u%s$u", u->handle);
  1220. }
  1221. break;
  1222. }
  1223. }
  1224. }
  1225. irc_whois(nick, " ircname : %s", msg);
  1226. return 0;
  1227. }
  1228. static char *
  1229. hide_chans(const char *nick, struct userrec *u, char *_channels, bool publicOnly)
  1230. {
  1231. char *channels = strdup(_channels), *channels_p = channels;
  1232. char *chname = NULL;
  1233. size_t len = strlen(channels) + 100 + 1;
  1234. struct chanset_t *chan = NULL;
  1235. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  1236. char *chans = (char *) my_calloc(1, len), *p = NULL;
  1237. while ((chname = newsplit(&channels))[0]) {
  1238. /* skip any modes in front of #chan */
  1239. if (!(p = strchr(chname, '#')))
  1240. if (!(p = strchr(chname, '&')))
  1241. if (!(p = strchr(chname, '!')))
  1242. continue;
  1243. chan = findchan_by_dname(p);
  1244. if (chan && !publicOnly)
  1245. get_user_flagrec(u, &fr, chan->dname, chan);
  1246. if (!chan ||
  1247. (!publicOnly && (
  1248. getnick(u->handle, chan) ||
  1249. !(channel_hidden(chan)) ||
  1250. chk_op(fr, chan)
  1251. )) ||
  1252. (publicOnly && !(channel_hidden(chan)))
  1253. ) {
  1254. if (chans[0])
  1255. strlcat(chans, " ", len);
  1256. strlcat(chans, chname, len);
  1257. }
  1258. }
  1259. free(channels_p);
  1260. return chans;
  1261. }
  1262. /* 319 $me nick :channels */
  1263. static int got319(char *from, char *msg)
  1264. {
  1265. char *nick = NULL;
  1266. newsplit(&msg);
  1267. nick = newsplit(&msg);
  1268. fixcolon(msg);
  1269. for (int idx = 0; idx < dcc_total; idx++) {
  1270. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1271. char *chans = NULL;
  1272. /* Show every channel the user has access to view */
  1273. if ((chans = hide_chans(nick, dcc[idx].user, msg, 0))) {
  1274. irc_whois(nick, " channels : %s", chans);
  1275. free(chans);
  1276. }
  1277. chans = NULL;
  1278. /* Show all of the 'public' channels */
  1279. if ((chans = hide_chans(nick, dcc[idx].user, msg, 1))) {
  1280. irc_whois(nick, " public : %s", chans);
  1281. free(chans);
  1282. }
  1283. break;
  1284. }
  1285. }
  1286. return 0;
  1287. }
  1288. /* 312 $me nick server :text */
  1289. static int got312(char *from, char *msg)
  1290. {
  1291. char *nick = NULL, *server = NULL;
  1292. newsplit(&msg);
  1293. nick = newsplit(&msg);
  1294. server = newsplit(&msg);
  1295. fixcolon(msg);
  1296. irc_whois(nick, " server : %s [%s]", server, msg);
  1297. return 0;
  1298. }
  1299. /* 301 $me nick :away msg */
  1300. static int got301(char *from, char *msg)
  1301. {
  1302. char *nick = NULL;
  1303. newsplit(&msg);
  1304. nick = newsplit(&msg);
  1305. fixcolon(msg);
  1306. irc_whois(nick, " away : %s", msg);
  1307. return 0;
  1308. }
  1309. /* 313 $me nick :server text */
  1310. static int got313(char *from, char *msg)
  1311. {
  1312. char *nick = NULL;
  1313. newsplit(&msg);
  1314. nick = newsplit(&msg);
  1315. fixcolon(msg);
  1316. irc_whois(nick, " : $b%s$b", msg);
  1317. return 0;
  1318. }
  1319. /* 317 $me nick idle signon :idle-eng signon-eng */
  1320. static int got317(char *from, char *msg)
  1321. {
  1322. char *nick = NULL, date[50] = "";
  1323. time_t idle, signon;
  1324. int mydays, myhours, mymins, mysecs;
  1325. newsplit(&msg);
  1326. nick = newsplit(&msg);
  1327. idle = atol(newsplit(&msg));
  1328. signon = atol(newsplit(&msg));
  1329. fixcolon(msg);
  1330. strftime(date, sizeof date, "%c %Z", gmtime(&signon));
  1331. mydays = idle / 86400;
  1332. idle = idle % 86400;
  1333. myhours = idle / 3600;
  1334. idle = idle % 3600;
  1335. mymins = idle / 60;
  1336. idle = idle % 60;
  1337. mysecs = idle;
  1338. irc_whois(nick, " idle : %d days %d hours %d mins %d secs [signon: %s]", mydays, myhours, mymins, mysecs, date);
  1339. return 0;
  1340. }
  1341. static int got369(char *from, char *msg)
  1342. {
  1343. return got318_369(from, msg, 1);
  1344. }
  1345. /* 318/319 $me nick :End of /? */
  1346. static int got318_369(char *from, char *msg, int whowas)
  1347. {
  1348. char *nick = NULL;
  1349. newsplit(&msg);
  1350. nick = newsplit(&msg);
  1351. fixcolon(msg);
  1352. irc_whois(nick, "%s", msg);
  1353. for (int idx = 0; idx < dcc_total; idx++) {
  1354. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick) &&
  1355. ((!whowas && !dcc[idx].whowas) || (whowas && dcc[idx].whowas))) {
  1356. dcc[idx].whois[0] = 0;
  1357. dcc[idx].whowas = 0;
  1358. }
  1359. }
  1360. return 0;
  1361. }
  1362. /* 401 $me nick :text */
  1363. static int got401(char *from, char *msg)
  1364. {
  1365. char *nick = NULL;
  1366. newsplit(&msg);
  1367. nick = newsplit(&msg);
  1368. fixcolon(msg);
  1369. irc_whois(nick, "%s", msg);
  1370. for (int idx = 0; idx < dcc_total; idx++)
  1371. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick))
  1372. dcc[idx].whowas = 1;
  1373. dprintf(DP_SERVER, "WHOWAS %s %s\n", nick, from);
  1374. return 0;
  1375. }
  1376. /* 406 $me nick :text */
  1377. static int got406(char *from, char *msg)
  1378. {
  1379. char *nick = NULL;
  1380. newsplit(&msg);
  1381. nick = newsplit(&msg);
  1382. fixcolon(msg);
  1383. irc_whois(nick, "%s", msg);
  1384. return 0;
  1385. }
  1386. /* 465 info_ :You are banned from this server- Temporary K-line 1 min. - Testing kline notices (2008/4/3 09.51) */
  1387. static int got465(char *from, char *msg)
  1388. {
  1389. newsplit(&msg); /* nick */
  1390. fixcolon(msg);
  1391. putlog(LOG_SERV, "*", "I am klined: %s", msg);
  1392. putlog(LOG_SERV, "*", "Disconnecting from server.");
  1393. nuke_server("I am klined!");
  1394. return 1;
  1395. }
  1396. /* 718 $me nick user@host :msg
  1397. * for receiving a msg while +g
  1398. */
  1399. static int got718(char *from, char *msg)
  1400. {
  1401. char *nick = NULL, *uhost = NULL;
  1402. newsplit(&msg);
  1403. nick = newsplit(&msg);
  1404. uhost = newsplit(&msg);
  1405. fixcolon(msg);
  1406. putlog(LOG_WALL, "*", "(+g) !%s!%s! %s", nick, uhost, msg);
  1407. return 0;
  1408. }
  1409. static cmd_t my_raw_binds[] =
  1410. {
  1411. {"PRIVMSG", "", (Function) gotmsg, NULL, LEAF},
  1412. {"NOTICE", "", (Function) gotnotice, NULL, LEAF},
  1413. {"MODE", "", (Function) gotmode, NULL, LEAF},
  1414. {"PING", "", (Function) gotping, NULL, LEAF},
  1415. {"PONG", "", (Function) gotpong, NULL, LEAF},
  1416. {"WALLOPS", "", (Function) gotwall, NULL, LEAF},
  1417. {"001", "", (Function) got001, NULL, LEAF},
  1418. {"004", "", (Function) got004, NULL, LEAF},
  1419. {"005", "", (Function) got005, NULL, LEAF},
  1420. {"303", "", (Function) got303, NULL, LEAF},
  1421. {"432", "", (Function) got432, NULL, LEAF},
  1422. {"433", "", (Function) got433, NULL, LEAF},
  1423. {"437", "", (Function) got437, NULL, LEAF},
  1424. {"438", "", (Function) got438, NULL, LEAF},
  1425. {"451", "", (Function) got451, NULL, LEAF},
  1426. {"442", "", (Function) got442, NULL, LEAF},
  1427. {"NICK", "", (Function) gotnick, NULL, LEAF},
  1428. {"ERROR", "", (Function) goterror, NULL, LEAF},
  1429. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1430. {"ERROR:", "", (Function) goterror, NULL, LEAF},
  1431. {"KICK", "", (Function) gotkick, NULL, LEAF},
  1432. /* WHOIS RAWS */
  1433. {"311", "", (Function) got311, NULL, LEAF}, /* ident host * :realname */
  1434. {"314", "", (Function) got311, NULL, LEAF}, /* "" -WHOWAS */
  1435. {"319", "", (Function) got319, NULL, LEAF}, /* :#channels */
  1436. {"312", "", (Function) got312, NULL, LEAF}, /* server :gecos */
  1437. {"301", "", (Function) got301, NULL, LEAF}, /* :away msg */
  1438. {"313", "", (Function) got313, NULL, LEAF}, /* :ircop */
  1439. {"317", "", (Function) got317, NULL, LEAF}, /* idle, signon :idle-eng, signon-eng */
  1440. {"401", "", (Function) got401, NULL, LEAF},
  1441. {"406", "", (Function) got406, NULL, LEAF},
  1442. {"465", "", (Function) got465, NULL, LEAF}, /* RPL_YOUREBANNEDCREEP */
  1443. {"318", "", (Function) whoispenalty, NULL, LEAF}, /* :End of /WHOIS */
  1444. {"369", "", (Function) got369, NULL, LEAF}, /* :End of /WHOWAS */
  1445. {"718", "", (Function) got718, NULL, LEAF},
  1446. {"731", "", (Function) got731, NULL, LEAF}, /* RPL_MONOFFLINE */
  1447. {NULL, NULL, NULL, NULL, 0}
  1448. };
  1449. static void server_dns_callback(int, void *, const char *, bd::Array<bd::String>);
  1450. /* Hook up to a server
  1451. */
  1452. static void connect_server(void)
  1453. {
  1454. char pass[121] = "", botserver[UHOSTLEN] = "";
  1455. int newidx;
  1456. port_t botserverport = 0;
  1457. waiting_for_awake = 0;
  1458. /* trying_server = now; */
  1459. empty_msgq();
  1460. if (newserverport) { /* cmd_jump was used; connect specified server */
  1461. curserv = -1; /* Reset server list */
  1462. strlcpy(botserver, newserver, sizeof(botserver));
  1463. botserverport = newserverport;
  1464. strlcpy(pass, newserverpass, sizeof(pass));
  1465. newserver[0] = newserverport = newserverpass[0] = 0;
  1466. }
  1467. if (!cycle_time) {
  1468. struct chanset_t *chan = NULL;
  1469. struct server_list *x = serverlist;
  1470. if (!x)
  1471. return;
  1472. trying_server = now;
  1473. newidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1474. if (newidx < 0) {
  1475. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1476. trying_server = 0;
  1477. return;
  1478. }
  1479. next_server(&curserv, botserver, &botserverport, pass);
  1480. putlog(LOG_SERV, "*", "Trying server %s:%d", botserver, botserverport);
  1481. dcc[newidx].port = botserverport;
  1482. strlcpy(dcc[newidx].nick, "(server)", NICKLEN);
  1483. strlcpy(dcc[newidx].host, botserver, UHOSTLEN);
  1484. botuserhost[0] = 0;
  1485. nick_juped = 0;
  1486. jnick_juped = 0;
  1487. tried_jupenick = 0;
  1488. rolls = 0;
  1489. altnick_char = 0;
  1490. use_monitor = 0;
  1491. for (chan = chanset; chan; chan = chan->next)
  1492. chan->status &= ~CHAN_JUPED;
  1493. dcc[newidx].timeval = now;
  1494. dcc[newidx].sock = -1;
  1495. dcc[newidx].u.dns->cbuf = strdup(pass);
  1496. cycle_time = 15; /* wait 15 seconds before attempting next server connect */
  1497. /* I'm resolving... don't start another server connect request */
  1498. resolvserv = 1;
  1499. /* Resolve the hostname. */
  1500. int dns_id = egg_dns_lookup(botserver, 20, server_dns_callback, (void *) (long) newidx);
  1501. if (dns_id >= 0)
  1502. dcc[newidx].dns_id = dns_id;
  1503. /* wait for async reply */
  1504. }
  1505. }
  1506. static void server_dns_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips)
  1507. {
  1508. //64bit hacks
  1509. long data = (long) client_data;
  1510. int idx = (int) data;
  1511. resolvserv = 0;
  1512. if (!valid_dns_id(idx, id))
  1513. return;
  1514. if (!ips.size()) {
  1515. putlog(LOG_SERV, "*", "Failed connect to %s (DNS lookup failed)", host);
  1516. trying_server = 0;
  1517. lostdcc(idx);
  1518. return;
  1519. }
  1520. my_addr_t addr;
  1521. char *ip = NULL;
  1522. /* FIXME: this is a temporary hack to stop bots from connecting over ipv4 when they should be on ipv6
  1523. * eventually will handle this in open_telnet(ips);
  1524. */
  1525. #ifdef USE_IPV6
  1526. if (conf.bot->net.v6) {
  1527. size_t i = 0;
  1528. for (i = 0; i < ips.size(); ++i) {
  1529. if (is_dotted_ip(bd::String(ips[i]).c_str()) == AF_INET6) {
  1530. ip = strdup(bd::String(ips[i]).c_str());
  1531. break;
  1532. }
  1533. }
  1534. if (!ip) {
  1535. putlog(LOG_SERV, "*", "Failed connect to %s (Could not DNS as IPV6)", host);
  1536. trying_server = 0;
  1537. lostdcc(idx);
  1538. return;
  1539. }
  1540. } else
  1541. #endif /* USE_IPV6 */
  1542. ip = strdup(bd::String(ips[0]).c_str());
  1543. get_addr(ip, &addr);
  1544. if (addr.family == AF_INET)
  1545. dcc[idx].addr = htonl(addr.u.addr.s_addr);
  1546. strlcpy(serverpass, (char *) dcc[idx].u.dns->cbuf, sizeof(serverpass));
  1547. changeover_dcc(idx, &SERVER_SOCKET, 0);
  1548. // identd_open(idx);
  1549. //No proxy, use identd, 2 = spoof ident
  1550. serv = open_telnet(ip, dcc[idx].port, 0, 2);
  1551. if (serv < 0) {
  1552. putlog(LOG_SERV, "*", "Failed connect to %s (%s)", dcc[idx].host, strerror(errno));
  1553. trying_server = 0;
  1554. lostdcc(idx);
  1555. } else {
  1556. int i = 1;
  1557. /* set these now so if we fail disconnect_server() can cleanup right. */
  1558. dcc[idx].sock = serv;
  1559. servidx = idx;
  1560. sdprintf("Connecting to '%s' (serv: %d, servidx: %d)", dcc[idx].host, serv, servidx);
  1561. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(int));
  1562. /* Queue standard login */
  1563. dcc[idx].timeval = now;
  1564. SERVER_SOCKET.timeout_val = &server_timeout;
  1565. /* Another server may have truncated it, so use the original */
  1566. if (jupenick[0])
  1567. strlcpy(botname, jupenick, sizeof(botname));
  1568. else
  1569. strlcpy(botname, origbotname, sizeof(botname));
  1570. /* Start alternate nicks from the beginning */
  1571. altnick_char = 0;
  1572. /* reset counter so first ctcp is dumped for tcms */
  1573. first_ctcp_check = 0;
  1574. if (serverpass[0])
  1575. dprintf(DP_MODE, "PASS %s\n", serverpass);
  1576. dprintf(DP_MODE, "NICK %s\n", botname);
  1577. dprintf(DP_MODE, "USER %s localhost %s :%s\n", botuser, dcc[idx].host, replace_vars(botrealname));
  1578. /* Wait for async result now */
  1579. }
  1580. free(ip);
  1581. }