servmsg.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531
  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. return 1; //ignore the current msg
  335. }
  336. p = strchr(floodhost, '@');
  337. if (p) {
  338. p++;
  339. if (egg_strcasecmp(lastmsghost[which], p)) { /* New */
  340. strcpy(lastmsghost[which], p);
  341. lastmsgtime[which] = now;
  342. lastmsgs[which] = 0;
  343. return 0;
  344. }
  345. } else
  346. return 0; /* Uh... whatever. */
  347. if (lastmsgtime[which] < now - lapse) {
  348. /* Flood timer expired, reset it */
  349. lastmsgtime[which] = now;
  350. lastmsgs[which] = 0;
  351. return 0;
  352. }
  353. lastmsgs[which]++;
  354. if (lastmsgs[which] >= thr) { /* FLOOD */
  355. /* Reset counters */
  356. lastmsgs[which] = 0;
  357. lastmsgtime[which] = 0;
  358. lastmsghost[which][0] = 0;
  359. u = get_user_by_host(from);
  360. /* Private msg */
  361. simple_sprintf(h, "*!*@%s", p);
  362. putlog(LOG_MISC, "*", "Flood from @%s! Placing on ignore!", p);
  363. addignore(h, conf.bot->nick, (which == FLOOD_CTCP) ? "CTCP flood" : "MSG/NOTICE flood", now + (60 * ignore_time));
  364. return 1;
  365. }
  366. return 0;
  367. }
  368. /* Check for more than 8 control characters in a line.
  369. * This could indicate: beep flood CTCP avalanche.
  370. */
  371. bool detect_avalanche(char *msg)
  372. {
  373. int count = 0;
  374. for (unsigned char *p = (unsigned char *) msg; (*p) && (count < 8); p++)
  375. if ((*p == 7) || (*p == 1))
  376. count++;
  377. if (count >= 8)
  378. return 1;
  379. else
  380. return 0;
  381. }
  382. /* Got a private message.
  383. */
  384. static int gotmsg(char *from, char *msg)
  385. {
  386. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  387. (*msg == '@'))) /* Notice to a channel, not handled here */
  388. return 0;
  389. char *to = NULL, buf[UHOSTLEN] = "", *nick = NULL, ctcpbuf[512] = "", *uhost = buf,
  390. *ctcp = NULL, *p = NULL, *p1 = NULL, *code = NULL;
  391. struct userrec *u = NULL;
  392. int ctcp_count = 0;
  393. bool ignoring = match_ignore(from);
  394. to = newsplit(&msg);
  395. fixcolon(msg);
  396. /* Only check if flood-ctcp is active */
  397. strcpy(uhost, from);
  398. nick = splitnick(&uhost);
  399. if (flood_ctcp.count && detect_avalanche(msg)) {
  400. if (!ignoring) {
  401. putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
  402. p = strchr(uhost, '@');
  403. if (p != NULL)
  404. p++;
  405. else
  406. p = uhost;
  407. simple_sprintf(ctcpbuf, "*!*@%s", p);
  408. addignore(ctcpbuf, conf.bot->nick, "ctcp avalanche", now + (60 * ignore_time));
  409. ignoring++;
  410. }
  411. }
  412. /* Check for CTCP: */
  413. ctcp_reply[0] = 0;
  414. p = strchr(msg, 1);
  415. while ((p != NULL) && (*p)) {
  416. p++;
  417. p1 = p;
  418. while ((*p != 1) && (*p != 0))
  419. p++;
  420. if (*p == 1) {
  421. *p = 0;
  422. ctcp = strcpy(ctcpbuf, p1);
  423. strcpy(p1 - 1, p + 1);
  424. if (!ignoring)
  425. ignoring = detect_flood(nick, uhost, from, strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
  426. /* Respond to the first answer_ctcp */
  427. p = strchr(msg, 1);
  428. if (ctcp_count < answer_ctcp) {
  429. ctcp_count++;
  430. if (ctcp[0] != ' ') {
  431. code = newsplit(&ctcp);
  432. if ((to[0] == '$') || strchr(to, '.')) {
  433. if (!ignoring) {
  434. /* Don't interpret */
  435. putlog(LOG_PUBLIC, to, "CTCP %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  436. }
  437. } else {
  438. u = get_user_by_host(from);
  439. if (!ignoring || trigger_on_ignore) {
  440. if (check_bind_ctcp(nick, uhost, u, to, code, ctcp) == BIND_RET_LOG && !ignoring) {
  441. if (!strcmp(code, "DCC")) {
  442. /* If it gets this far unhandled, it means that
  443. * the user is totally unknown.
  444. */
  445. code = newsplit(&ctcp);
  446. if (!strcmp(code, "CHAT")) {
  447. if (!quiet_reject) {
  448. if (u)
  449. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, "I'm not accepting call at the moment.");
  450. else
  451. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_NOSTRANGERS);
  452. }
  453. if (!ischanhub())
  454. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  455. else
  456. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  457. } else {
  458. putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from);
  459. }
  460. } else if (!strcmp(code, "CHAT")) {
  461. if (!quiet_reject) {
  462. if (u)
  463. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, "I'm not accepting call at the moment.");
  464. else
  465. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, DCC_NOSTRANGERS);
  466. }
  467. if (!ischanhub())
  468. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  469. else
  470. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  471. }
  472. }
  473. if (!strcmp(code, "ACTION")) {
  474. putlog(LOG_MSGS, "*", "Action to %s: %s %s", to, nick, ctcp);
  475. } else {
  476. putlog(LOG_MSGS, "*", "CTCP %s: %s from %s (%s)", code, ctcp, nick, uhost);
  477. } /* I love a good close cascade ;) */
  478. }
  479. }
  480. }
  481. }
  482. }
  483. }
  484. /* Send out possible ctcp responses */
  485. if (ctcp_reply[0]) {
  486. if (ctcp_mode != 2) {
  487. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  488. } else {
  489. if (now - last_ctcp > flood_ctcp.time) {
  490. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  491. count_ctcp = 1;
  492. } else if (count_ctcp < flood_ctcp.count) {
  493. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  494. count_ctcp++;
  495. }
  496. last_ctcp = now;
  497. }
  498. }
  499. if (msg[0]) {
  500. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  501. /* Msg from oper */
  502. ignoring = detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  503. if (!ignoring) {
  504. /* Do not interpret as command */
  505. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  506. }
  507. } else {
  508. char *my_code = NULL;
  509. Auth *auth = Auth::Find(uhost);
  510. if (!auth)
  511. ignoring = detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  512. my_code = newsplit(&msg);
  513. rmspace(msg);
  514. /* is it a cmd? */
  515. if (my_code && my_code[0] && my_code[1] && auth && auth->Authed() && my_code[0] == auth_prefix[0]) {
  516. my_code++; //eliminate the prefix
  517. auth->atime = now;
  518. if (!check_bind_authc(my_code, auth, NULL, msg))
  519. putlog(LOG_MSGS, "*", "[%s] %c%s %s", from, auth_prefix[0], my_code, msg);
  520. } else if (!ignoring && (my_code[0] != auth_prefix[0] || !my_code[1] || !auth || !auth->Authed())) {
  521. struct userrec *my_u = get_user_by_host(from);
  522. bool doit = 1;
  523. if (!egg_strcasecmp(my_code, "op") || !egg_strcasecmp(my_code, "pass") || !egg_strcasecmp(my_code, "invite")
  524. || !egg_strcasecmp(my_code, "ident")
  525. || !egg_strcasecmp(my_code, msgop) || !egg_strcasecmp(my_code, msgpass)
  526. || !egg_strcasecmp(my_code, msginvite) || !egg_strcasecmp(my_code, msgident)) {
  527. const char *buf2 = NULL;
  528. doit = 0;
  529. if (!egg_strcasecmp(my_code, msgop))
  530. buf2 = "op";
  531. else if (!egg_strcasecmp(my_code, msgpass))
  532. buf2 = "pass";
  533. else if (!egg_strcasecmp(my_code, msginvite))
  534. buf2 = "invite";
  535. else if (!egg_strcasecmp(my_code, msgident))
  536. buf2 = "ident";
  537. if (buf2)
  538. check_bind_msg(buf2, nick, uhost, my_u, msg);
  539. else
  540. putlog(LOG_MSGS, "*", "(%s!%s) attempted to use invalid msg cmd '%s'", nick, uhost, my_code);
  541. }
  542. if (doit)
  543. check_bind_msg(my_code, nick, uhost, my_u, msg);
  544. }
  545. }
  546. }
  547. return 0;
  548. }
  549. /* Got a private notice.
  550. */
  551. static int gotnotice(char *from, char *msg)
  552. {
  553. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  554. (*msg == '@'))) /* Notice to a channel, not handled here */
  555. return 0;
  556. char *to = NULL, *nick = NULL, ctcpbuf[512] = "", *p = NULL, *p1 = NULL, buf[512] = "",
  557. *uhost = buf, *ctcp = NULL, *ctcpmsg = NULL, *ptr = NULL;
  558. struct userrec *u = NULL;
  559. bool ignoring = match_ignore(from);
  560. to = newsplit(&msg);
  561. fixcolon(msg);
  562. strcpy(uhost, from);
  563. nick = splitnick(&uhost);
  564. if (flood_ctcp.count && detect_avalanche(msg)) {
  565. /* Discard -- kick user if it was to the channel */
  566. if (!ignoring)
  567. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  568. return 0;
  569. }
  570. /* Check for CTCP: */
  571. ctcpmsg = ptr = strdup(msg);
  572. p = strchr(ctcpmsg, 1);
  573. while ((p != NULL) && (*p)) {
  574. p++;
  575. p1 = p;
  576. while ((*p != 1) && (*p != 0))
  577. p++;
  578. if (*p == 1) {
  579. *p = 0;
  580. ctcp = strcpy(ctcpbuf, p1);
  581. strcpy(p1 - 1, p + 1);
  582. if (!ignoring)
  583. ignoring = detect_flood(nick, uhost, from, FLOOD_CTCP);
  584. p = strchr(ctcpmsg, 1);
  585. if (ctcp[0] != ' ') {
  586. char *code = newsplit(&ctcp);
  587. if ((to[0] == '$') || strchr(to, '.')) {
  588. if (!ignoring)
  589. putlog(LOG_PUBLIC, "*",
  590. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  591. } else {
  592. u = get_user_by_host(from);
  593. if (!ignoring || trigger_on_ignore) {
  594. check_bind_ctcr(nick, uhost, u, to, code, ctcp);
  595. if (!ignoring)
  596. /* Who cares? */
  597. putlog(LOG_MSGS, "*", "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  598. }
  599. }
  600. }
  601. }
  602. }
  603. free(ptr);
  604. if (msg[0]) {
  605. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  606. ignoring = detect_flood(nick, uhost, from, FLOOD_NOTICE);
  607. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s", nick, uhost, to, msg);
  608. } else {
  609. /* Server notice? */
  610. if ((nick[0] == 0) || (uhost[0] == 0)) {
  611. if (!server_online &&
  612. (!strcmp(msg, "*** You are exempt from flood limits.") ||
  613. !strcmp(msg, "*** You are exempt from user limits. congrats.")))
  614. floodless = 1;
  615. /* Hidden `250' connection count message from server */
  616. if (strncmp(msg, "Highest connection count:", 25))
  617. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  618. } else {
  619. ignoring = detect_flood(nick, uhost, from, FLOOD_NOTICE);
  620. u = get_user_by_host(from);
  621. if (!ignoring)
  622. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  623. }
  624. }
  625. }
  626. return 0;
  627. }
  628. /* WALLOPS: oper's nuisance
  629. */
  630. static int gotwall(char *from, char *msg)
  631. {
  632. fixcolon(msg);
  633. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  634. return 0;
  635. }
  636. static void server_10secondly()
  637. {
  638. if (server_online && keepnick) {
  639. /* NOTE: now that botname can but upto NICKLEN bytes long,
  640. * check that it's not just a truncation of the full nick.
  641. */
  642. if (strncmp(botname, origbotname, strlen(botname))) {
  643. /* See if my nickname is in use and if if my nick is right. */
  644. dprintf(DP_SERVER, "ISON :%s %s\n", botname, origbotname);
  645. }
  646. }
  647. }
  648. /* Called once a minute... but if we're the only one on the
  649. * channel, we only wanna send out "lusers" once every 5 mins.
  650. */
  651. static void minutely_checks()
  652. {
  653. /* Only check if we have already successfully logged in. */
  654. if (server_online) {
  655. static int count = 4;
  656. bool ok = 0;
  657. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  658. if (channel_active(chan) && chan->channel.members == 1) {
  659. ok = 1;
  660. break;
  661. }
  662. }
  663. if (!ok)
  664. return;
  665. count++;
  666. if (count >= 5) {
  667. dprintf(DP_SERVER, "LUSERS\n");
  668. count = 0;
  669. }
  670. }
  671. }
  672. /* Pong from server.
  673. */
  674. static int gotpong(char *from, char *msg)
  675. {
  676. newsplit(&msg);
  677. fixcolon(msg); /* Scrap server name */
  678. server_lag = now - my_atoul(msg);
  679. if (server_lag > 99999) {
  680. /* IRCnet lagmeter support by drummer */
  681. server_lag = now - lastpingtime;
  682. }
  683. return 0;
  684. }
  685. /* This is a reply on ISON :<current> <orig> [<alt>]
  686. */
  687. static void got303(char *from, char *msg)
  688. {
  689. if (!keepnick || !strncmp(botname, origbotname, strlen(botname)))
  690. return;
  691. char *tmp = NULL;
  692. newsplit(&msg);
  693. fixcolon(msg);
  694. tmp = newsplit(&msg);
  695. if (tmp[0] && !rfc_casecmp(botname, tmp)) {
  696. bool ison_orig = 0;
  697. while ((tmp = newsplit(&msg))[0]) { /* no, it's NOT == */
  698. if (!rfc_casecmp(tmp, origbotname))
  699. ison_orig = 1;
  700. }
  701. if (!ison_orig) {
  702. if (!nick_juped)
  703. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  704. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  705. }
  706. }
  707. }
  708. /* 432 : Bad nickname
  709. */
  710. static int got432(char *from, char *msg)
  711. {
  712. char *erroneus = NULL;
  713. newsplit(&msg);
  714. erroneus = newsplit(&msg);
  715. if (server_online)
  716. putlog(LOG_MISC, "*", "NICK IS INVALID: %s (keeping '%s').", erroneus,
  717. botname);
  718. else {
  719. putlog(LOG_MISC, "*", "Server says my nickname is invalid.");
  720. if (!keepnick) {
  721. makepass(erroneus);
  722. erroneus[NICKMAX] = 0;
  723. dprintf(DP_MODE, "NICK %s\n", erroneus);
  724. }
  725. return 0;
  726. }
  727. return 0;
  728. }
  729. /* 433 : Nickname in use
  730. * Change nicks till we're acceptable or we give up
  731. */
  732. static int got433(char *from, char *msg)
  733. {
  734. /* We are online and have a nickname, we'll keep it */
  735. if (server_online) {
  736. char *tmp = NULL;
  737. newsplit(&msg);
  738. tmp = newsplit(&msg);
  739. putlog(LOG_MISC, "*", "NICK IN USE: %s (keeping '%s').", tmp, botname);
  740. nick_juped = 0;
  741. return 0;
  742. }
  743. gotfake433(from);
  744. return 0;
  745. }
  746. /* 437 : Channel/Nickname juped (IRCnet)
  747. */
  748. static int got437(char *from, char *msg)
  749. {
  750. char *s = NULL;
  751. struct chanset_t *chan = NULL;
  752. newsplit(&msg);
  753. s = newsplit(&msg);
  754. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  755. chan = findchan(s);
  756. if (chan) {
  757. if (chan->status & CHAN_ACTIVE) {
  758. putlog(LOG_MISC, "*", IRC_CANTCHANGENICK, s);
  759. } else {
  760. if (!channel_juped(chan)) {
  761. putlog(LOG_MISC, "*", "Channel %s is juped. :(", s);
  762. chan->status |= CHAN_JUPED;
  763. }
  764. }
  765. }
  766. } else if (server_online) {
  767. if (!nick_juped)
  768. putlog(LOG_MISC, "*", "NICK IS JUPED: %s (keeping '%s').", s, botname);
  769. if (!rfc_casecmp(s, origbotname))
  770. nick_juped = 1;
  771. } else {
  772. putlog(LOG_MISC, "*", "%s: %s", "Nickname has been juped", s);
  773. gotfake433(from);
  774. }
  775. return 0;
  776. }
  777. /* 438 : Nick change too fast
  778. */
  779. static int got438(char *from, char *msg)
  780. {
  781. newsplit(&msg);
  782. newsplit(&msg);
  783. fixcolon(msg);
  784. putlog(LOG_MISC, "*", "%s", msg);
  785. return 0;
  786. }
  787. static int got451(char *from, char *msg)
  788. {
  789. /* Usually if we get this then we really messed up somewhere
  790. * or this is a non-standard server, so we log it and kill the socket
  791. * hoping the next server will work :) -poptix
  792. */
  793. /* Um, this does occur on a lagged anti-spoof server connection if the
  794. * (minutely) sending of joins occurs before the bot does its ping reply.
  795. * Probably should do something about it some time - beldin
  796. */
  797. putlog(LOG_MISC, "*", "%s says I'm not registered, trying next one.", from);
  798. nuke_server("The server says we are not registered yet..");
  799. return 0;
  800. }
  801. /* Got error notice
  802. */
  803. static int goterror(char *from, char *msg)
  804. {
  805. if (msg[0] == ':')
  806. msg++;
  807. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  808. putlog(LOG_SERV, "*", "Disconnecting from server.");
  809. nuke_server("Bah, stupid error messages.");
  810. return 1;
  811. }
  812. /* Got nick change.
  813. */
  814. static int gotnick(char *from, char *msg)
  815. {
  816. char *nick = NULL, *buf = NULL, *buf_ptr = NULL;
  817. struct userrec *u = NULL;
  818. buf = buf_ptr = strdup(from);
  819. u = get_user_by_host(buf);
  820. nick = splitnick(&buf);
  821. fixcolon(msg);
  822. if (match_my_nick(nick)) {
  823. /* Regained nick! */
  824. strlcpy(botname, msg, NICKLEN);
  825. altnick_char = 0;
  826. if (!strcmp(msg, origbotname)) {
  827. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  828. nick_juped = 0;
  829. } else if (keepnick && strcmp(nick, msg)) {
  830. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  831. if (!rfc_casecmp(nick, origbotname)) {
  832. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  833. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  834. }
  835. } else
  836. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  837. } else if ((keepnick) && (rfc_casecmp(nick, msg))) {
  838. /* Only do the below if there was actual nick change, case doesn't count */
  839. if (!rfc_casecmp(nick, origbotname)) {
  840. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  841. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  842. }
  843. }
  844. free(buf_ptr);
  845. return 0;
  846. }
  847. static int gotmode(char *from, char *msg)
  848. {
  849. char *ch = NULL, *buf = NULL, *buf_ptr = NULL;
  850. buf_ptr = buf = strdup(msg);
  851. ch = newsplit(&buf);
  852. /* Usermode changes? */
  853. if (strchr(CHANMETA, ch[0]) == NULL) {
  854. if (match_my_nick(ch) && check_mode_r) {
  855. /* umode +r? - D0H dalnet uses it to mean something different */
  856. fixcolon(buf);
  857. if ((buf[0] == '+') && strchr(buf, 'r')) {
  858. putlog(LOG_MISC | LOG_JOIN, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
  859. nuke_server("i-lines suck");
  860. }
  861. }
  862. }
  863. free(buf_ptr);
  864. return 0;
  865. }
  866. static void disconnect_server(int idx, int dolost)
  867. {
  868. if ((serv != dcc[idx].sock) && serv >= 0)
  869. killsock(serv);
  870. if (dcc[idx].sock >= 0)
  871. killsock(dcc[idx].sock);
  872. dcc[idx].sock = -1;
  873. serv = -1;
  874. servidx = -1;
  875. server_online = 0;
  876. floodless = 0;
  877. botuserhost[0] = 0;
  878. botuserip[0] = 0;
  879. if (dolost) {
  880. Auth::DeleteAll();
  881. trying_server = 0;
  882. lostdcc(idx);
  883. }
  884. }
  885. static void eof_server(int idx)
  886. {
  887. putlog(LOG_SERV, "*", "Disconnected from %s", dcc[idx].host);
  888. disconnect_server(idx, DO_LOST);
  889. }
  890. static void display_server(int idx, char *buf)
  891. {
  892. simple_sprintf(buf, "%s (lag: %d)", trying_server ? "conn" : "serv", server_lag);
  893. }
  894. static void connect_server(void);
  895. static void kill_server(int idx, void *x)
  896. {
  897. disconnect_server(idx, NO_LOST); /* eof_server will lostdcc() it. */
  898. if (!segfaulted) // don't bother if we've segfaulted, too many memory calls in this loop
  899. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  900. clear_channel(chan, 1);
  901. /* A new server connection will be automatically initiated in
  902. about 2 seconds. */
  903. }
  904. static void timeout_server(int idx)
  905. {
  906. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  907. disconnect_server(idx, DO_LOST);
  908. }
  909. static void server_activity(int, char *, int);
  910. struct dcc_table SERVER_SOCKET =
  911. {
  912. "SERVER",
  913. 0,
  914. eof_server,
  915. server_activity,
  916. NULL,
  917. timeout_server,
  918. display_server,
  919. kill_server,
  920. NULL,
  921. NULL
  922. };
  923. static void server_activity(int idx, char *msg, int len)
  924. {
  925. char *from = NULL, *code = NULL;
  926. if (trying_server) {
  927. strcpy(dcc[idx].nick, "(server)");
  928. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  929. trying_server = 0;
  930. /*
  931. servidx = idx;
  932. serv = dcc[idx].sosck;
  933. */
  934. SERVER_SOCKET.timeout_val = 0;
  935. }
  936. waiting_for_awake = 0;
  937. if (msg[0] == ':') {
  938. msg++;
  939. from = newsplit(&msg);
  940. } else
  941. from = "";
  942. code = newsplit(&msg);
  943. if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
  944. if (!match_ignore(from))
  945. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  946. } else
  947. putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
  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. if ((u = host_conflicts(s))) {
  1017. if (u != conf.bot->u) {
  1018. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  1019. sdprintf("I am %s, they are: %s, (%X vs %X)", conf.bot->u->handle, u->handle, conf.bot->u, u);
  1020. } else
  1021. sdprintf("Already have hostmask '%s' added for myself", s);
  1022. return;
  1023. }
  1024. addhost_by_handle(conf.bot->nick, s);
  1025. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  1026. }
  1027. /* 311 $me nick username address * :realname */
  1028. static int got311(char *from, char *msg)
  1029. {
  1030. char *nick = NULL, *username = NULL, *address = NULL, uhost[UHOSTLEN + 1];
  1031. struct userrec *u = NULL;
  1032. newsplit(&msg);
  1033. nick = newsplit(&msg);
  1034. username = newsplit(&msg);
  1035. address = newsplit(&msg);
  1036. newsplit(&msg);
  1037. fixcolon(msg);
  1038. if (match_my_nick(nick)) {
  1039. simple_snprintf(botuserhost, sizeof botuserhost, "%s@%s", username, address);
  1040. check_hostmask();
  1041. }
  1042. irc_whois(nick, "$b%s$b [%s@%s]", nick, username, address);
  1043. simple_snprintf(uhost, sizeof uhost, "%s!%s@%s", nick, username, address);
  1044. if ((u = get_user_by_host(uhost)))
  1045. irc_whois(nick, " username : $u%s$u", u->handle);
  1046. irc_whois(nick, " ircname : %s", msg);
  1047. return 0;
  1048. }
  1049. /* 319 $me nick :channels */
  1050. static int got319(char *from, char *msg)
  1051. {
  1052. char *nick = NULL;
  1053. newsplit(&msg);
  1054. nick = newsplit(&msg);
  1055. fixcolon(msg);
  1056. irc_whois(nick, " channels : %s", msg);
  1057. return 0;
  1058. }
  1059. /* 312 $me nick server :text */
  1060. static int got312(char *from, char *msg)
  1061. {
  1062. char *nick = NULL, *server = NULL;
  1063. newsplit(&msg);
  1064. nick = newsplit(&msg);
  1065. server = newsplit(&msg);
  1066. fixcolon(msg);
  1067. irc_whois(nick, " server : %s [%s]", server, msg);
  1068. return 0;
  1069. }
  1070. /* 301 $me nick :away msg */
  1071. static int got301(char *from, char *msg)
  1072. {
  1073. char *nick = NULL;
  1074. newsplit(&msg);
  1075. nick = newsplit(&msg);
  1076. fixcolon(msg);
  1077. irc_whois(nick, " away : %s", msg);
  1078. return 0;
  1079. }
  1080. /* 313 $me nick :server text */
  1081. static int got313(char *from, char *msg)
  1082. {
  1083. char *nick = NULL;
  1084. newsplit(&msg);
  1085. nick = newsplit(&msg);
  1086. fixcolon(msg);
  1087. irc_whois(nick, " : $b%s$b", msg);
  1088. return 0;
  1089. }
  1090. /* 317 $me nick idle signon :idle-eng signon-eng */
  1091. static int got317(char *from, char *msg)
  1092. {
  1093. char *nick = NULL, date[50] = "";
  1094. time_t idle, signon;
  1095. int mydays, myhours, mymins, mysecs;
  1096. newsplit(&msg);
  1097. nick = newsplit(&msg);
  1098. idle = atol(newsplit(&msg));
  1099. signon = atol(newsplit(&msg));
  1100. fixcolon(msg);
  1101. egg_strftime(date, sizeof date, "%c %Z", gmtime(&signon));
  1102. mydays = idle / 86400;
  1103. idle = idle % 86400;
  1104. myhours = idle / 3600;
  1105. idle = idle % 3600;
  1106. mymins = idle / 60;
  1107. idle = idle % 60;
  1108. mysecs = idle;
  1109. irc_whois(nick, " idle : %d days %d hours %d mins %d secs [signon: %s]", mydays, myhours, mymins, mysecs, date);
  1110. return 0;
  1111. }
  1112. static int got369(char *from, char *msg)
  1113. {
  1114. return got318_369(from, msg, 1);
  1115. }
  1116. /* 318/319 $me nick :End of /? */
  1117. static int got318_369(char *from, char *msg, int whowas)
  1118. {
  1119. char *nick = NULL;
  1120. newsplit(&msg);
  1121. nick = newsplit(&msg);
  1122. fixcolon(msg);
  1123. irc_whois(nick, "%s", msg);
  1124. for (int idx = 0; idx < dcc_total; idx++) {
  1125. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick) &&
  1126. ((!whowas && !dcc[idx].whowas) || (whowas && dcc[idx].whowas))) {
  1127. dcc[idx].whois[0] = 0;
  1128. dcc[idx].whowas = 0;
  1129. }
  1130. }
  1131. return 0;
  1132. }
  1133. /* 401 $me nick :text */
  1134. static int got401(char *from, char *msg)
  1135. {
  1136. char *nick = NULL;
  1137. newsplit(&msg);
  1138. nick = newsplit(&msg);
  1139. fixcolon(msg);
  1140. irc_whois(nick, "%s", msg);
  1141. for (int idx = 0; idx < dcc_total; idx++)
  1142. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick))
  1143. dcc[idx].whowas = 1;
  1144. dprintf(DP_SERVER, "WHOWAS %s %s\n", nick, from);
  1145. return 0;
  1146. }
  1147. /* 406 $me nick :text */
  1148. static int got406(char *from, char *msg)
  1149. {
  1150. char *nick = NULL;
  1151. newsplit(&msg);
  1152. nick = newsplit(&msg);
  1153. fixcolon(msg);
  1154. irc_whois(nick, "%s", msg);
  1155. return 0;
  1156. }
  1157. /* 718 $me nick user@host :msg
  1158. * for receiving a msg while +g
  1159. */
  1160. static int got718(char *from, char *msg)
  1161. {
  1162. char *nick = NULL, *uhost = NULL;
  1163. newsplit(&msg);
  1164. nick = newsplit(&msg);
  1165. uhost = newsplit(&msg);
  1166. fixcolon(msg);
  1167. putlog(LOG_WALL, "*", "(+g) !%s!%s! %s", nick, uhost, msg);
  1168. return 0;
  1169. }
  1170. static cmd_t my_raw_binds[] =
  1171. {
  1172. {"PRIVMSG", "", (Function) gotmsg, NULL, LEAF},
  1173. {"NOTICE", "", (Function) gotnotice, NULL, LEAF},
  1174. {"MODE", "", (Function) gotmode, NULL, LEAF},
  1175. {"PING", "", (Function) gotping, NULL, LEAF},
  1176. {"PONG", "", (Function) gotpong, NULL, LEAF},
  1177. {"WALLOPS", "", (Function) gotwall, NULL, LEAF},
  1178. {"001", "", (Function) got001, NULL, LEAF},
  1179. {"005", "", (Function) got005, NULL, LEAF},
  1180. {"303", "", (Function) got303, NULL, LEAF},
  1181. {"432", "", (Function) got432, NULL, LEAF},
  1182. {"433", "", (Function) got433, NULL, LEAF},
  1183. {"437", "", (Function) got437, NULL, LEAF},
  1184. {"438", "", (Function) got438, NULL, LEAF},
  1185. {"451", "", (Function) got451, NULL, LEAF},
  1186. {"442", "", (Function) got442, NULL, LEAF},
  1187. {"NICK", "", (Function) gotnick, NULL, LEAF},
  1188. {"ERROR", "", (Function) goterror, NULL, LEAF},
  1189. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1190. {"ERROR:", "", (Function) goterror, NULL, LEAF},
  1191. {"KICK", "", (Function) gotkick, NULL, LEAF},
  1192. /* WHOIS RAWS */
  1193. {"311", "", (Function) got311, NULL, LEAF}, /* ident host * :realname */
  1194. {"314", "", (Function) got311, NULL, LEAF}, /* "" -WHOWAS */
  1195. {"319", "", (Function) got319, NULL, LEAF}, /* :#channels */
  1196. {"312", "", (Function) got312, NULL, LEAF}, /* server :gecos */
  1197. {"301", "", (Function) got301, NULL, LEAF}, /* :away msg */
  1198. {"313", "", (Function) got313, NULL, LEAF}, /* :ircop */
  1199. {"317", "", (Function) got317, NULL, LEAF}, /* idle, signon :idle-eng, signon-eng */
  1200. {"401", "", (Function) got401, NULL, LEAF},
  1201. {"406", "", (Function) got406, NULL, LEAF},
  1202. {"318", "", (Function) whoispenalty, NULL, LEAF}, /* :End of /WHOIS */
  1203. {"369", "", (Function) got369, NULL, LEAF}, /* :End of /WHOWAS */
  1204. {"718", "", (Function) got718, NULL, LEAF},
  1205. {NULL, NULL, NULL, NULL, 0}
  1206. };
  1207. static void server_dns_callback(int, void *, const char *, char **);
  1208. /* Hook up to a server
  1209. */
  1210. static void connect_server(void)
  1211. {
  1212. char pass[121] = "", botserver[UHOSTLEN] = "";
  1213. int newidx;
  1214. port_t botserverport = 0;
  1215. waiting_for_awake = 0;
  1216. /* trying_server = now; */
  1217. empty_msgq();
  1218. if (newserverport) { /* cmd_jump was used; connect specified server */
  1219. curserv = -1; /* Reset server list */
  1220. strcpy(botserver, newserver);
  1221. botserverport = newserverport;
  1222. strcpy(pass, newserverpass);
  1223. newserver[0] = newserverport = newserverpass[0] = 0;
  1224. }
  1225. if (!cycle_time) {
  1226. struct chanset_t *chan = NULL;
  1227. struct server_list *x = serverlist;
  1228. if (!x)
  1229. return;
  1230. trying_server = now;
  1231. newidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1232. if (newidx < 0) {
  1233. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1234. trying_server = 0;
  1235. return;
  1236. }
  1237. next_server(&curserv, botserver, &botserverport, pass);
  1238. putlog(LOG_SERV, "*", "Trying server %s:%d", botserver, botserverport);
  1239. dcc[newidx].port = botserverport;
  1240. strcpy(dcc[newidx].nick, "(server)");
  1241. strlcpy(dcc[newidx].host, botserver, UHOSTLEN);
  1242. botuserhost[0] = 0;
  1243. nick_juped = 0;
  1244. for (chan = chanset; chan; chan = chan->next)
  1245. chan->status &= ~CHAN_JUPED;
  1246. dcc[newidx].timeval = now;
  1247. dcc[newidx].sock = -1;
  1248. dcc[newidx].u.dns->cbuf = strdup(pass);
  1249. cycle_time = 15; /* wait 15 seconds before attempting next server connect */
  1250. /* I'm resolving... don't start another server connect request */
  1251. resolvserv = 1;
  1252. /* Resolve the hostname. */
  1253. int dns_id = egg_dns_lookup(botserver, 20, server_dns_callback, (void *) newidx);
  1254. if (dns_id >= 0)
  1255. dcc[newidx].dns_id = dns_id;
  1256. /* wait for async reply */
  1257. }
  1258. }
  1259. static void server_dns_callback(int id, void *client_data, const char *host, char **ips)
  1260. {
  1261. int idx = (int) client_data;
  1262. resolvserv = 0;
  1263. if (!valid_dns_id(idx, id))
  1264. return;
  1265. if (!ips) {
  1266. putlog(LOG_SERV, "*", "Failed connect to %s (DNS lookup failed)", host);
  1267. trying_server = 0;
  1268. lostdcc(idx);
  1269. return;
  1270. }
  1271. addr_t addr;
  1272. char *ip = NULL;
  1273. /* FIXME: this is a temporary hack to stop bots from connecting over ipv4 when they should be on ipv6
  1274. * eventually will handle this in open_telnet(ips);
  1275. */
  1276. if (conf.bot->net.v6) {
  1277. int i = 0;
  1278. for (i = 0; ips[i]; i++) {
  1279. if (is_dotted_ip(ips[i]) == AF_INET6) {
  1280. ip = ips[i];
  1281. break;
  1282. }
  1283. }
  1284. if (!ip) {
  1285. putlog(LOG_SERV, "*", "Failed connect to %s (Could not DNS as IPV6)", host);
  1286. trying_server = 0;
  1287. lostdcc(idx);
  1288. return;
  1289. }
  1290. } else
  1291. ip = ips[0];
  1292. get_addr(ip, &addr);
  1293. if (addr.family == AF_INET)
  1294. dcc[idx].addr = htonl(addr.u.addr.s_addr);
  1295. strcpy(serverpass, (char *) dcc[idx].u.dns->cbuf);
  1296. changeover_dcc(idx, &SERVER_SOCKET, 0);
  1297. identd_open();
  1298. serv = open_telnet(ip, dcc[idx].port, 0);
  1299. if (serv < 0) {
  1300. putlog(LOG_SERV, "*", "Failed connect to %s (%s)", dcc[idx].host, strerror(errno));
  1301. trying_server = 0;
  1302. lostdcc(idx);
  1303. } else {
  1304. int i = 1;
  1305. /* set these now so if we fail disconnect_server() can cleanup right. */
  1306. dcc[idx].sock = serv;
  1307. servidx = idx;
  1308. sdprintf("Connecting to '%s' (serv: %d, servidx: %d)", dcc[idx].host, serv, servidx);
  1309. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(int));
  1310. /* Queue standard login */
  1311. dcc[idx].timeval = now;
  1312. SERVER_SOCKET.timeout_val = &server_timeout;
  1313. /* Another server may have truncated it, so use the original */
  1314. strcpy(botname, origbotname);
  1315. /* Start alternate nicks from the beginning */
  1316. altnick_char = 0;
  1317. /* reset counter so first ctcp is dumped for tcms */
  1318. first_ctcp_check = 0;
  1319. if (serverpass[0])
  1320. dprintf(DP_MODE, "PASS %s\n", serverpass);
  1321. dprintf(DP_MODE, "NICK %s\n", botname);
  1322. dprintf(DP_MODE, "USER %s localhost %s :%s\n", botuser, dcc[idx].host, botrealname);
  1323. /* Wait for async result now */
  1324. }
  1325. }