servmsg.c 49 KB

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