servmsg.c 49 KB

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