servmsg.c 40 KB

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