servmsg.c 38 KB

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