servmsg.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606
  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 %s\n", botname, var_get_str_by_name("usermode"));
  137. }
  138. }
  139. void
  140. join_chans()
  141. {
  142. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  143. if (shouldjoin(chan)) {
  144. chan->status &= ~(CHAN_ACTIVE | CHAN_PEND | CHAN_JOINING);
  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(-serv, "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. 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 from %s (%s) to %s: %s", code, nick, uhost, to, ctcp);
  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 (!ischanhub())
  448. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  449. else
  450. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  451. } else {
  452. putlog(LOG_MISC, "*", "Refused DCC %s: %s", code, from);
  453. }
  454. } else if (!strcmp(code, "CHAT")) {
  455. if (!ischanhub())
  456. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
  457. else
  458. putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
  459. }
  460. }
  461. if (!strcmp(code, "ACTION")) {
  462. putlog(LOG_MSGS, "*", "* %s (%s): %s", nick, uhost, ctcp);
  463. } else {
  464. putlog(LOG_MSGS, "*", "CTCP %s: from %s (%s): %s", code, nick, uhost, ctcp);
  465. } /* I love a good close cascade ;) */
  466. }
  467. }
  468. }
  469. }
  470. }
  471. }
  472. /* Send out possible ctcp responses */
  473. if (ctcp_reply[0]) {
  474. if (ctcp_mode != 2) {
  475. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  476. } else {
  477. if (now - last_ctcp > flood_ctcp.time) {
  478. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  479. count_ctcp = 1;
  480. } else if (count_ctcp < flood_ctcp.count) {
  481. dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
  482. count_ctcp++;
  483. }
  484. last_ctcp = now;
  485. }
  486. }
  487. if (msg[0]) {
  488. if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
  489. /* Msg from oper */
  490. if (!ignoring) {
  491. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  492. /* Do not interpret as command */
  493. putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
  494. }
  495. } else {
  496. char *my_code = NULL;
  497. Auth *auth = NULL;
  498. if (auth_prefix[0])
  499. auth = Auth::Find(uhost);
  500. if (!auth)
  501. detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
  502. my_code = newsplit(&msg);
  503. rmspace(msg);
  504. /* is it a cmd? */
  505. if (auth_prefix[0] && my_code && my_code[0] && my_code[1] && auth && auth->Authed() && my_code[0] == auth_prefix[0]) {
  506. my_code++; //eliminate the prefix
  507. auth->atime = now;
  508. if (!check_bind_authc(my_code, auth, NULL, msg))
  509. putlog(LOG_MSGS, "*", "[%s] %c%s %s", from, auth_prefix[0], my_code, msg);
  510. } else if (!ignoring && (my_code[0] != auth_prefix[0] || !my_code[1] || !auth || !auth->Authed())) {
  511. struct userrec *my_u = get_user_by_host(from);
  512. bool doit = 1;
  513. if (!egg_strcasecmp(my_code, "op") || !egg_strcasecmp(my_code, "pass") || !egg_strcasecmp(my_code, "invite")
  514. || !egg_strcasecmp(my_code, "ident")
  515. || !egg_strcasecmp(my_code, msgop) || !egg_strcasecmp(my_code, msgpass)
  516. || !egg_strcasecmp(my_code, msginvite) || !egg_strcasecmp(my_code, msgident)) {
  517. const char *buf2 = NULL;
  518. doit = 0;
  519. if (!egg_strcasecmp(my_code, msgop))
  520. buf2 = "op";
  521. else if (!egg_strcasecmp(my_code, msgpass))
  522. buf2 = "pass";
  523. else if (!egg_strcasecmp(my_code, msginvite))
  524. buf2 = "invite";
  525. else if (!egg_strcasecmp(my_code, msgident))
  526. buf2 = "ident";
  527. if (buf2)
  528. check_bind_msg(buf2, nick, uhost, my_u, msg);
  529. else
  530. putlog(LOG_MSGS, "*", "(%s!%s) attempted to use invalid msg cmd '%s'", nick, uhost, my_code);
  531. }
  532. if (doit)
  533. check_bind_msg(my_code, nick, uhost, my_u, msg);
  534. }
  535. }
  536. }
  537. return 0;
  538. }
  539. /* Got a private notice.
  540. */
  541. static int gotnotice(char *from, char *msg)
  542. {
  543. if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
  544. (*msg == '@'))) /* Notice to a channel, not handled here */
  545. return 0;
  546. char *to = NULL, *nick = NULL, ctcpbuf[512] = "", *p = NULL, *p1 = NULL, buf[512] = "",
  547. *uhost = buf, *ctcp = NULL, *ctcpmsg = NULL, *ptr = NULL;
  548. struct userrec *u = NULL;
  549. bool ignoring = match_ignore(from);
  550. to = newsplit(&msg);
  551. fixcolon(msg);
  552. strcpy(uhost, from);
  553. nick = splitnick(&uhost);
  554. if (flood_ctcp.count && detect_avalanche(msg)) {
  555. /* Discard -- kick user if it was to the channel */
  556. if (!ignoring)
  557. putlog(LOG_MODES, "*", "Avalanche from %s", from);
  558. return 0;
  559. }
  560. /* Check for CTCP: */
  561. ctcpmsg = ptr = strdup(msg);
  562. p = strchr(ctcpmsg, 1);
  563. while ((p != NULL) && (*p)) {
  564. p++;
  565. p1 = p;
  566. while ((*p != 1) && (*p != 0))
  567. p++;
  568. if (*p == 1) {
  569. *p = 0;
  570. ctcp = strcpy(ctcpbuf, p1);
  571. strcpy(p1 - 1, p + 1);
  572. if (!ignoring)
  573. detect_flood(nick, uhost, from, FLOOD_CTCP);
  574. p = strchr(ctcpmsg, 1);
  575. if (ctcp[0] != ' ') {
  576. char *code = newsplit(&ctcp);
  577. if ((to[0] == '$') || strchr(to, '.')) {
  578. if (!ignoring)
  579. putlog(LOG_PUBLIC, "*",
  580. "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  581. } else {
  582. u = get_user_by_host(from);
  583. if (!ignoring || trigger_on_ignore) {
  584. check_bind_ctcr(nick, uhost, u, to, code, ctcp);
  585. if (!ignoring)
  586. /* Who cares? */
  587. putlog(LOG_MSGS, "*", "CTCP reply %s: %s from %s (%s) to %s", code, ctcp, nick, uhost, to);
  588. }
  589. }
  590. }
  591. }
  592. }
  593. free(ptr);
  594. if (msg[0]) {
  595. if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
  596. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  597. putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s", nick, uhost, to, msg);
  598. } else {
  599. /* Server notice? */
  600. if ((nick[0] == 0) || (uhost[0] == 0)) {
  601. if (!server_online &&
  602. !strcmp(msg, "*** You are exempt from flood limits."))
  603. floodless = 1;
  604. /* Hidden `250' connection count message from server */
  605. if (strncmp(msg, "Highest connection count:", 25))
  606. putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
  607. } else {
  608. detect_flood(nick, uhost, from, FLOOD_NOTICE);
  609. u = get_user_by_host(from);
  610. if (!ignoring)
  611. putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
  612. }
  613. }
  614. }
  615. return 0;
  616. }
  617. /* WALLOPS: oper's nuisance
  618. */
  619. static int gotwall(char *from, char *msg)
  620. {
  621. fixcolon(msg);
  622. putlog(LOG_WALL, "*", "!%s! %s", from, msg);
  623. return 0;
  624. }
  625. void server_send_ison()
  626. {
  627. if (server_online && keepnick) {
  628. /* NOTE: now that botname can but upto NICKLEN bytes long,
  629. * check that it's not just a truncation of the full nick.
  630. */
  631. if (strncmp(botname, origbotname, strlen(botname))) {
  632. /* See if my nickname is in use and if if my nick is right. */
  633. dprintf(DP_SERVER, "ISON :%s %s\n", botname, origbotname);
  634. }
  635. }
  636. }
  637. /* Called once a minute... but if we're the only one on the
  638. * channel, we only wanna send out "lusers" once every 5 mins.
  639. */
  640. static void minutely_checks()
  641. {
  642. /* Only check if we have already successfully logged in. */
  643. if (server_online) {
  644. static int count = 4;
  645. bool ok = 0;
  646. for (register struct chanset_t *chan = chanset; chan; chan = chan->next) {
  647. if (channel_active(chan) && chan->channel.members == 1) {
  648. ok = 1;
  649. break;
  650. }
  651. }
  652. if (!ok)
  653. return;
  654. count++;
  655. if (count >= 5) {
  656. dprintf(DP_SERVER, "LUSERS\n");
  657. count = 0;
  658. }
  659. }
  660. }
  661. /* Pong from server.
  662. */
  663. static int gotpong(char *from, char *msg)
  664. {
  665. newsplit(&msg);
  666. fixcolon(msg); /* Scrap server name */
  667. server_lag = now - my_atoul(msg);
  668. if (server_lag > 99999) {
  669. /* IRCnet lagmeter support by drummer */
  670. server_lag = now - lastpingtime;
  671. }
  672. return 0;
  673. }
  674. /* This is a reply on ISON :<current> <orig> [<alt>]
  675. */
  676. static void got303(char *from, char *msg)
  677. {
  678. if (!keepnick || !strncmp(botname, origbotname, strlen(botname)))
  679. return;
  680. char *tmp = NULL;
  681. newsplit(&msg);
  682. fixcolon(msg);
  683. tmp = newsplit(&msg);
  684. if (tmp[0] && !match_my_nick(tmp)) {
  685. bool ison_orig = 0;
  686. while ((tmp = newsplit(&msg))[0]) {
  687. if (!rfc_casecmp(tmp, origbotname)) {
  688. ison_orig = 1;
  689. break; //Take out for alt checks
  690. }
  691. }
  692. if (!ison_orig) {
  693. if (!nick_juped)
  694. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  695. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  696. }
  697. }
  698. }
  699. /* 432 : Bad nickname
  700. */
  701. static int got432(char *from, char *msg)
  702. {
  703. char *erroneus = NULL;
  704. newsplit(&msg);
  705. erroneus = newsplit(&msg);
  706. if (server_online)
  707. putlog(LOG_MISC, "*", "NICK IS INVALID: %s (keeping '%s').", erroneus,
  708. botname);
  709. else {
  710. putlog(LOG_MISC, "*", "Server says my nickname is invalid.");
  711. if (!keepnick) {
  712. makepass(erroneus);
  713. erroneus[NICKMAX] = 0;
  714. dprintf(DP_MODE, "NICK %s\n", erroneus);
  715. }
  716. return 0;
  717. }
  718. return 0;
  719. }
  720. /* 433 : Nickname in use
  721. * Change nicks till we're acceptable or we give up
  722. */
  723. static int got433(char *from, char *msg)
  724. {
  725. /* We are online and have a nickname, we'll keep it */
  726. if (server_online) {
  727. char *tmp = NULL;
  728. newsplit(&msg);
  729. tmp = newsplit(&msg);
  730. putlog(LOG_MISC, "*", "NICK IN USE: %s (keeping '%s').", tmp, botname);
  731. nick_juped = 0;
  732. return 0;
  733. }
  734. gotfake433(from);
  735. return 0;
  736. }
  737. /* 437 : Channel/Nickname juped (IRCnet)
  738. */
  739. static int got437(char *from, char *msg)
  740. {
  741. char *s = NULL;
  742. struct chanset_t *chan = NULL;
  743. newsplit(&msg);
  744. s = newsplit(&msg);
  745. if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
  746. chan = findchan(s);
  747. if (chan) {
  748. chan->status &= ~(CHAN_JOINING);
  749. if (chan->status & CHAN_ACTIVE) {
  750. putlog(LOG_MISC, "*", IRC_CANTCHANGENICK, s);
  751. } else {
  752. if (!channel_juped(chan)) {
  753. putlog(LOG_MISC, "*", "Channel %s is juped. :(", s);
  754. chan->status |= CHAN_JUPED;
  755. }
  756. }
  757. }
  758. } else if (server_online) {
  759. if (!nick_juped)
  760. putlog(LOG_MISC, "*", "NICK IS JUPED: %s (keeping '%s').", s, botname);
  761. if (!rfc_casecmp(s, origbotname))
  762. nick_juped = 1;
  763. } else {
  764. putlog(LOG_MISC, "*", "%s: %s", "Nickname has been juped", s);
  765. gotfake433(from);
  766. }
  767. return 0;
  768. }
  769. /* 438 : Nick change too fast
  770. */
  771. static int got438(char *from, char *msg)
  772. {
  773. newsplit(&msg);
  774. newsplit(&msg);
  775. fixcolon(msg);
  776. putlog(LOG_MISC, "*", "%s", msg);
  777. return 0;
  778. }
  779. static int got451(char *from, char *msg)
  780. {
  781. /* Usually if we get this then we really messed up somewhere
  782. * or this is a non-standard server, so we log it and kill the socket
  783. * hoping the next server will work :) -poptix
  784. */
  785. /* Um, this does occur on a lagged anti-spoof server connection if the
  786. * (minutely) sending of joins occurs before the bot does its ping reply.
  787. * Probably should do something about it some time - beldin
  788. */
  789. putlog(LOG_MISC, "*", "%s says I'm not registered, trying next one.", from);
  790. nuke_server("The server says we are not registered yet..");
  791. return 0;
  792. }
  793. /* Got error notice
  794. */
  795. static int goterror(char *from, char *msg)
  796. {
  797. if (msg[0] == ':')
  798. msg++;
  799. putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
  800. putlog(LOG_SERV, "*", "Disconnecting from server.");
  801. nuke_server("Bah, stupid error messages.");
  802. return 1;
  803. }
  804. /* Got nick change.
  805. */
  806. static int gotnick(char *from, char *msg)
  807. {
  808. char *nick = NULL, *buf = NULL, *buf_ptr = NULL;
  809. struct userrec *u = NULL;
  810. buf = buf_ptr = strdup(from);
  811. u = get_user_by_host(buf);
  812. nick = splitnick(&buf);
  813. fixcolon(msg);
  814. if (match_my_nick(nick)) {
  815. /* Regained nick! */
  816. strlcpy(botname, msg, NICKLEN);
  817. altnick_char = 0;
  818. if (!strcmp(msg, origbotname)) {
  819. putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
  820. nick_juped = 0;
  821. } else if (keepnick && strcmp(nick, msg)) {
  822. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  823. if (!rfc_casecmp(nick, origbotname)) {
  824. putlog(LOG_MISC, "*", "Switching back to nick %s", origbotname);
  825. dprintf(DP_SERVER, "NICK %s\n", origbotname);
  826. }
  827. } else
  828. putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
  829. } else if ((keepnick) && (rfc_casecmp(nick, msg))) {
  830. /* Only do the below if there was actual nick change, case doesn't count */
  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. }
  836. free(buf_ptr);
  837. return 0;
  838. }
  839. static int gotmode(char *from, char *msg)
  840. {
  841. char *ch = NULL, *buf = NULL, *buf_ptr = NULL;
  842. buf_ptr = buf = strdup(msg);
  843. ch = newsplit(&buf);
  844. /* Usermode changes? */
  845. if (strchr(CHANMETA, ch[0]) == NULL) {
  846. if (match_my_nick(ch) && check_mode_r) {
  847. /* umode +r? - D0H dalnet uses it to mean something different */
  848. fixcolon(buf);
  849. if ((buf[0] == '+') && strchr(buf, 'r')) {
  850. putlog(LOG_MISC | LOG_JOIN, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
  851. nuke_server("i-lines suck");
  852. }
  853. }
  854. }
  855. free(buf_ptr);
  856. return 0;
  857. }
  858. static void disconnect_server(int idx, int dolost)
  859. {
  860. if ((serv != dcc[idx].sock) && serv >= 0)
  861. killsock(serv);
  862. if (dcc[idx].sock >= 0)
  863. killsock(dcc[idx].sock);
  864. dcc[idx].sock = -1;
  865. serv = -1;
  866. servidx = -1;
  867. server_online = 0;
  868. floodless = 0;
  869. botuserhost[0] = 0;
  870. botuserip[0] = 0;
  871. if (dolost) {
  872. Auth::DeleteAll();
  873. trying_server = 0;
  874. lostdcc(idx);
  875. }
  876. /* Invalidate the cmd_swhois cache callback data */
  877. for (int idx = 0; idx < dcc_total; idx++) {
  878. if (dcc[idx].type && dcc[idx].whois[0]) {
  879. dcc[idx].whois[0] = 0;
  880. dcc[idx].whowas = 0;
  881. }
  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. if (!segfaulted) // don't bother if we've segfaulted, too many memory calls in this loop
  898. for (struct chanset_t *chan = chanset; chan; chan = chan->next)
  899. clear_channel(chan, 1);
  900. /* A new server connection will be automatically initiated in
  901. about 2 seconds. */
  902. }
  903. static void timeout_server(int idx)
  904. {
  905. putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
  906. disconnect_server(idx, DO_LOST);
  907. }
  908. static void server_activity(int, char *, int);
  909. struct dcc_table SERVER_SOCKET =
  910. {
  911. "SERVER",
  912. 0,
  913. eof_server,
  914. server_activity,
  915. NULL,
  916. timeout_server,
  917. display_server,
  918. kill_server,
  919. NULL,
  920. NULL
  921. };
  922. static void server_activity(int idx, char *msg, int len)
  923. {
  924. char *from = NULL, *code = NULL;
  925. if (trying_server) {
  926. strcpy(dcc[idx].nick, "(server)");
  927. putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
  928. trying_server = 0;
  929. /*
  930. servidx = idx;
  931. serv = dcc[idx].sosck;
  932. */
  933. SERVER_SOCKET.timeout_val = 0;
  934. }
  935. waiting_for_awake = 0;
  936. if (msg[0] == ':') {
  937. msg++;
  938. from = newsplit(&msg);
  939. } else
  940. from = "";
  941. code = newsplit(&msg);
  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. /* This has GOT to go into the raw binding table, * merely because this
  948. * is less effecient.
  949. */
  950. check_bind_raw(from, code, msg);
  951. }
  952. static int gotping(char *from, char *msg)
  953. {
  954. fixcolon(msg);
  955. dprintf(DP_MODE, "PONG :%s\n", msg);
  956. return 0;
  957. }
  958. static int gotkick(char *from, char *msg)
  959. {
  960. if (!match_my_nick(from))
  961. /* Not my kick, I don't need to bother about it. */
  962. return 0;
  963. if (use_penalties) {
  964. last_time += 2;
  965. if (debug_output)
  966. putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
  967. }
  968. return 0;
  969. }
  970. /* Another sec penalty if bot did a whois on another server.
  971. */
  972. static int got318_369(char *, char *, int);
  973. static int whoispenalty(char *from, char *msg)
  974. {
  975. struct server_list *x = serverlist;
  976. if (x && use_penalties) {
  977. int i = 0, ii = 0;
  978. for (; x; x = x->next) {
  979. if (i == curserv) {
  980. if (strcmp(x->name, from))
  981. ii = 1;
  982. }
  983. i++;
  984. }
  985. if (ii) {
  986. last_time += 1;
  987. if (debug_output)
  988. putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
  989. }
  990. }
  991. got318_369(from, msg, 0);
  992. return 0;
  993. }
  994. static void irc_whois(char *, const char *, ...) __attribute__((format(printf, 2, 3)));
  995. static void
  996. irc_whois(char *nick, const char *format, ...)
  997. {
  998. char va_out[2001] = "";
  999. va_list va;
  1000. va_start(va, format);
  1001. egg_vsnprintf(va_out, sizeof(va_out) - 1, format, va);
  1002. va_end(va);
  1003. for (int idx = 0; idx < dcc_total; idx++) {
  1004. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1005. dprintf(idx, "%s\n", va_out);
  1006. break;
  1007. }
  1008. }
  1009. }
  1010. void check_hostmask()
  1011. {
  1012. if (!server_online || !botuserhost[0])
  1013. return;
  1014. char s[UHOSTLEN + 2] = "";
  1015. simple_sprintf(s, "*!%s", botuserhost); /* just add actual user@ident, regardless of ~ */
  1016. /* dont add the host if it conflicts with another in the userlist */
  1017. struct userrec *u = NULL;
  1018. if ((u = host_conflicts(s))) {
  1019. if (u != conf.bot->u) {
  1020. putlog(LOG_WARN, "*", "My automatic hostmask '%s' would conflict with user: '%s'. (Not adding)", s, u->handle);
  1021. sdprintf("I am %s, they are: %s, (%X vs %X)", conf.bot->u->handle, u->handle, conf.bot->u, u);
  1022. } else
  1023. sdprintf("Already have hostmask '%s' added for myself", s);
  1024. return;
  1025. }
  1026. addhost_by_handle(conf.bot->nick, s);
  1027. putlog(LOG_GETIN, "*", "Updated my hostmask: %s", s);
  1028. }
  1029. /* 311 $me nick username address * :realname */
  1030. static int got311(char *from, char *msg)
  1031. {
  1032. char *nick = NULL, *username = NULL, *address = NULL, uhost[UHOSTLEN + 1];
  1033. struct userrec *u = NULL;
  1034. newsplit(&msg);
  1035. nick = newsplit(&msg);
  1036. username = newsplit(&msg);
  1037. address = newsplit(&msg);
  1038. newsplit(&msg);
  1039. fixcolon(msg);
  1040. if (match_my_nick(nick)) {
  1041. simple_snprintf(botuserhost, sizeof botuserhost, "%s@%s", username, address);
  1042. check_hostmask();
  1043. }
  1044. irc_whois(nick, "$b%s$b [%s@%s]", nick, username, address);
  1045. simple_snprintf(uhost, sizeof uhost, "%s!%s@%s", nick, username, address);
  1046. if ((u = get_user_by_host(uhost))) {
  1047. int idx = 0;
  1048. for (idx = 0; idx < dcc_total; idx++) {
  1049. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1050. if (whois_access(dcc[idx].user, u)) {
  1051. irc_whois(nick, " username : $u%s$u", u->handle);
  1052. }
  1053. break;
  1054. }
  1055. }
  1056. }
  1057. irc_whois(nick, " ircname : %s", msg);
  1058. return 0;
  1059. }
  1060. static char *
  1061. hide_chans(const char *nick, struct userrec *u, char *_channels, bool publicOnly)
  1062. {
  1063. char *channels = strdup(_channels), *channels_p = channels;
  1064. char *chans = NULL, *chname = NULL, s[5] = "";
  1065. size_t len = strlen(channels) + 100 + 1;
  1066. struct chanset_t *chan = NULL;
  1067. struct flag_record fr = { FR_CHAN | FR_GLOBAL, 0, 0, 0 };
  1068. chans = (char *) my_calloc(1, len);
  1069. while ((chname = newsplit(&channels))[0]) {
  1070. /* save and skip any modes in front of #chan */
  1071. s[0] = 0;
  1072. while (chname[0] && chname[0] != '#') {
  1073. simple_snprintf(s, sizeof(s), "%s%c", s[0] ? s : "", chname[0]);
  1074. chname++;
  1075. }
  1076. chan = findchan_by_dname(chname);
  1077. if (chan && !publicOnly)
  1078. get_user_flagrec(u, &fr, chan->dname);
  1079. if (!chan ||
  1080. (!publicOnly && (
  1081. getnick(u->handle, chan) ||
  1082. !(chan->channel.mode & (CHANPRIV|CHANSEC)) ||
  1083. chk_op(fr, chan)
  1084. )) ||
  1085. (publicOnly && !(chan->channel.mode & (CHANPRIV|CHANSEC)))
  1086. ) {
  1087. if (chans[0])
  1088. simple_snprintf(chans, len, "%s %s%s", chans, s, chname);
  1089. else
  1090. simple_snprintf(chans, len, "%s%s", s, chname);
  1091. }
  1092. }
  1093. free(channels_p);
  1094. return chans;
  1095. }
  1096. /* 319 $me nick :channels */
  1097. static int got319(char *from, char *msg)
  1098. {
  1099. char *nick = NULL;
  1100. newsplit(&msg);
  1101. nick = newsplit(&msg);
  1102. fixcolon(msg);
  1103. for (int idx = 0; idx < dcc_total; idx++) {
  1104. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(nick, dcc[idx].whois)) {
  1105. char *chans = NULL;
  1106. /* Show every channel the user has access to view */
  1107. if ((chans = hide_chans(nick, dcc[idx].user, msg, 0))) {
  1108. irc_whois(nick, " channels : %s", chans);
  1109. free(chans);
  1110. }
  1111. chans = NULL;
  1112. /* Show all of the 'public' channels */
  1113. if ((chans = hide_chans(nick, dcc[idx].user, msg, 1))) {
  1114. irc_whois(nick, " public : %s", chans);
  1115. free(chans);
  1116. }
  1117. break;
  1118. }
  1119. }
  1120. return 0;
  1121. }
  1122. /* 312 $me nick server :text */
  1123. static int got312(char *from, char *msg)
  1124. {
  1125. char *nick = NULL, *server = NULL;
  1126. newsplit(&msg);
  1127. nick = newsplit(&msg);
  1128. server = newsplit(&msg);
  1129. fixcolon(msg);
  1130. irc_whois(nick, " server : %s [%s]", server, msg);
  1131. return 0;
  1132. }
  1133. /* 301 $me nick :away msg */
  1134. static int got301(char *from, char *msg)
  1135. {
  1136. char *nick = NULL;
  1137. newsplit(&msg);
  1138. nick = newsplit(&msg);
  1139. fixcolon(msg);
  1140. irc_whois(nick, " away : %s", msg);
  1141. return 0;
  1142. }
  1143. /* 313 $me nick :server text */
  1144. static int got313(char *from, char *msg)
  1145. {
  1146. char *nick = NULL;
  1147. newsplit(&msg);
  1148. nick = newsplit(&msg);
  1149. fixcolon(msg);
  1150. irc_whois(nick, " : $b%s$b", msg);
  1151. return 0;
  1152. }
  1153. /* 317 $me nick idle signon :idle-eng signon-eng */
  1154. static int got317(char *from, char *msg)
  1155. {
  1156. char *nick = NULL, date[50] = "";
  1157. time_t idle, signon;
  1158. int mydays, myhours, mymins, mysecs;
  1159. newsplit(&msg);
  1160. nick = newsplit(&msg);
  1161. idle = atol(newsplit(&msg));
  1162. signon = atol(newsplit(&msg));
  1163. fixcolon(msg);
  1164. egg_strftime(date, sizeof date, "%c %Z", gmtime(&signon));
  1165. mydays = idle / 86400;
  1166. idle = idle % 86400;
  1167. myhours = idle / 3600;
  1168. idle = idle % 3600;
  1169. mymins = idle / 60;
  1170. idle = idle % 60;
  1171. mysecs = idle;
  1172. irc_whois(nick, " idle : %d days %d hours %d mins %d secs [signon: %s]", mydays, myhours, mymins, mysecs, date);
  1173. return 0;
  1174. }
  1175. static int got369(char *from, char *msg)
  1176. {
  1177. return got318_369(from, msg, 1);
  1178. }
  1179. /* 318/319 $me nick :End of /? */
  1180. static int got318_369(char *from, char *msg, int whowas)
  1181. {
  1182. char *nick = NULL;
  1183. newsplit(&msg);
  1184. nick = newsplit(&msg);
  1185. fixcolon(msg);
  1186. irc_whois(nick, "%s", msg);
  1187. for (int idx = 0; idx < dcc_total; idx++) {
  1188. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick) &&
  1189. ((!whowas && !dcc[idx].whowas) || (whowas && dcc[idx].whowas))) {
  1190. dcc[idx].whois[0] = 0;
  1191. dcc[idx].whowas = 0;
  1192. }
  1193. }
  1194. return 0;
  1195. }
  1196. /* 401 $me nick :text */
  1197. static int got401(char *from, char *msg)
  1198. {
  1199. char *nick = NULL;
  1200. newsplit(&msg);
  1201. nick = newsplit(&msg);
  1202. fixcolon(msg);
  1203. irc_whois(nick, "%s", msg);
  1204. for (int idx = 0; idx < dcc_total; idx++)
  1205. if (dcc[idx].type && dcc[idx].whois[0] && !rfc_casecmp(dcc[idx].whois, nick))
  1206. dcc[idx].whowas = 1;
  1207. dprintf(DP_SERVER, "WHOWAS %s %s\n", nick, from);
  1208. return 0;
  1209. }
  1210. /* 406 $me nick :text */
  1211. static int got406(char *from, char *msg)
  1212. {
  1213. char *nick = NULL;
  1214. newsplit(&msg);
  1215. nick = newsplit(&msg);
  1216. fixcolon(msg);
  1217. irc_whois(nick, "%s", msg);
  1218. return 0;
  1219. }
  1220. /* 718 $me nick user@host :msg
  1221. * for receiving a msg while +g
  1222. */
  1223. static int got718(char *from, char *msg)
  1224. {
  1225. char *nick = NULL, *uhost = NULL;
  1226. newsplit(&msg);
  1227. nick = newsplit(&msg);
  1228. uhost = newsplit(&msg);
  1229. fixcolon(msg);
  1230. putlog(LOG_WALL, "*", "(+g) !%s!%s! %s", nick, uhost, msg);
  1231. return 0;
  1232. }
  1233. static cmd_t my_raw_binds[] =
  1234. {
  1235. {"PRIVMSG", "", (Function) gotmsg, NULL, LEAF},
  1236. {"NOTICE", "", (Function) gotnotice, NULL, LEAF},
  1237. {"MODE", "", (Function) gotmode, NULL, LEAF},
  1238. {"PING", "", (Function) gotping, NULL, LEAF},
  1239. {"PONG", "", (Function) gotpong, NULL, LEAF},
  1240. {"WALLOPS", "", (Function) gotwall, NULL, LEAF},
  1241. {"001", "", (Function) got001, NULL, LEAF},
  1242. {"005", "", (Function) got005, NULL, LEAF},
  1243. {"303", "", (Function) got303, NULL, LEAF},
  1244. {"432", "", (Function) got432, NULL, LEAF},
  1245. {"433", "", (Function) got433, NULL, LEAF},
  1246. {"437", "", (Function) got437, NULL, LEAF},
  1247. {"438", "", (Function) got438, NULL, LEAF},
  1248. {"451", "", (Function) got451, NULL, LEAF},
  1249. {"442", "", (Function) got442, NULL, LEAF},
  1250. {"NICK", "", (Function) gotnick, NULL, LEAF},
  1251. {"ERROR", "", (Function) goterror, NULL, LEAF},
  1252. /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
  1253. {"ERROR:", "", (Function) goterror, NULL, LEAF},
  1254. {"KICK", "", (Function) gotkick, NULL, LEAF},
  1255. /* WHOIS RAWS */
  1256. {"311", "", (Function) got311, NULL, LEAF}, /* ident host * :realname */
  1257. {"314", "", (Function) got311, NULL, LEAF}, /* "" -WHOWAS */
  1258. {"319", "", (Function) got319, NULL, LEAF}, /* :#channels */
  1259. {"312", "", (Function) got312, NULL, LEAF}, /* server :gecos */
  1260. {"301", "", (Function) got301, NULL, LEAF}, /* :away msg */
  1261. {"313", "", (Function) got313, NULL, LEAF}, /* :ircop */
  1262. {"317", "", (Function) got317, NULL, LEAF}, /* idle, signon :idle-eng, signon-eng */
  1263. {"401", "", (Function) got401, NULL, LEAF},
  1264. {"406", "", (Function) got406, NULL, LEAF},
  1265. {"318", "", (Function) whoispenalty, NULL, LEAF}, /* :End of /WHOIS */
  1266. {"369", "", (Function) got369, NULL, LEAF}, /* :End of /WHOWAS */
  1267. {"718", "", (Function) got718, NULL, LEAF},
  1268. {NULL, NULL, NULL, NULL, 0}
  1269. };
  1270. static void server_dns_callback(int, void *, const char *, char **);
  1271. /* Hook up to a server
  1272. */
  1273. static void connect_server(void)
  1274. {
  1275. char pass[121] = "", botserver[UHOSTLEN] = "";
  1276. int newidx;
  1277. port_t botserverport = 0;
  1278. waiting_for_awake = 0;
  1279. /* trying_server = now; */
  1280. empty_msgq();
  1281. if (newserverport) { /* cmd_jump was used; connect specified server */
  1282. curserv = -1; /* Reset server list */
  1283. strcpy(botserver, newserver);
  1284. botserverport = newserverport;
  1285. strcpy(pass, newserverpass);
  1286. newserver[0] = newserverport = newserverpass[0] = 0;
  1287. }
  1288. if (!cycle_time) {
  1289. struct chanset_t *chan = NULL;
  1290. struct server_list *x = serverlist;
  1291. if (!x)
  1292. return;
  1293. trying_server = now;
  1294. newidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1295. if (newidx < 0) {
  1296. putlog(LOG_SERV, "*", "NO MORE DCC CONNECTIONS -- Can't create server connection.");
  1297. trying_server = 0;
  1298. return;
  1299. }
  1300. next_server(&curserv, botserver, &botserverport, pass);
  1301. putlog(LOG_SERV, "*", "Trying server %s:%d", botserver, botserverport);
  1302. dcc[newidx].port = botserverport;
  1303. strcpy(dcc[newidx].nick, "(server)");
  1304. strlcpy(dcc[newidx].host, botserver, UHOSTLEN);
  1305. botuserhost[0] = 0;
  1306. nick_juped = 0;
  1307. for (chan = chanset; chan; chan = chan->next)
  1308. chan->status &= ~CHAN_JUPED;
  1309. dcc[newidx].timeval = now;
  1310. dcc[newidx].sock = -1;
  1311. dcc[newidx].u.dns->cbuf = strdup(pass);
  1312. cycle_time = 15; /* wait 15 seconds before attempting next server connect */
  1313. /* I'm resolving... don't start another server connect request */
  1314. resolvserv = 1;
  1315. /* Resolve the hostname. */
  1316. int dns_id = egg_dns_lookup(botserver, 20, server_dns_callback, (void *) newidx);
  1317. if (dns_id >= 0)
  1318. dcc[newidx].dns_id = dns_id;
  1319. /* wait for async reply */
  1320. }
  1321. }
  1322. static void server_dns_callback(int id, void *client_data, const char *host, char **ips)
  1323. {
  1324. int idx = (int) client_data;
  1325. resolvserv = 0;
  1326. if (!valid_dns_id(idx, id))
  1327. return;
  1328. if (!ips) {
  1329. putlog(LOG_SERV, "*", "Failed connect to %s (DNS lookup failed)", host);
  1330. trying_server = 0;
  1331. lostdcc(idx);
  1332. return;
  1333. }
  1334. my_addr_t addr;
  1335. char *ip = NULL;
  1336. /* FIXME: this is a temporary hack to stop bots from connecting over ipv4 when they should be on ipv6
  1337. * eventually will handle this in open_telnet(ips);
  1338. */
  1339. #ifdef USE_IPV6
  1340. if (conf.bot->net.v6) {
  1341. int i = 0;
  1342. for (i = 0; ips[i]; i++) {
  1343. if (is_dotted_ip(ips[i]) == AF_INET6) {
  1344. ip = ips[i];
  1345. break;
  1346. }
  1347. }
  1348. if (!ip) {
  1349. putlog(LOG_SERV, "*", "Failed connect to %s (Could not DNS as IPV6)", host);
  1350. trying_server = 0;
  1351. lostdcc(idx);
  1352. return;
  1353. }
  1354. } else
  1355. #endif /* USE_IPV6 */
  1356. ip = ips[0];
  1357. get_addr(ip, &addr);
  1358. if (addr.family == AF_INET)
  1359. dcc[idx].addr = htonl(addr.u.addr.s_addr);
  1360. strcpy(serverpass, (char *) dcc[idx].u.dns->cbuf);
  1361. changeover_dcc(idx, &SERVER_SOCKET, 0);
  1362. identd_open();
  1363. serv = open_telnet(ip, dcc[idx].port, 0);
  1364. if (serv < 0) {
  1365. putlog(LOG_SERV, "*", "Failed connect to %s (%s)", dcc[idx].host, strerror(errno));
  1366. trying_server = 0;
  1367. lostdcc(idx);
  1368. } else {
  1369. int i = 1;
  1370. /* set these now so if we fail disconnect_server() can cleanup right. */
  1371. dcc[idx].sock = serv;
  1372. servidx = idx;
  1373. sdprintf("Connecting to '%s' (serv: %d, servidx: %d)", dcc[idx].host, serv, servidx);
  1374. setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(int));
  1375. /* Queue standard login */
  1376. dcc[idx].timeval = now;
  1377. SERVER_SOCKET.timeout_val = &server_timeout;
  1378. /* Another server may have truncated it, so use the original */
  1379. strcpy(botname, origbotname);
  1380. /* Start alternate nicks from the beginning */
  1381. altnick_char = 0;
  1382. /* reset counter so first ctcp is dumped for tcms */
  1383. first_ctcp_check = 0;
  1384. if (serverpass[0])
  1385. dprintf(DP_MODE, "PASS %s\n", serverpass);
  1386. dprintf(DP_MODE, "NICK %s\n", botname);
  1387. dprintf(DP_MODE, "USER %s localhost %s :%s\n", botuser, dcc[idx].host, replace(botrealname, "$n", botname));
  1388. /* Wait for async result now */
  1389. }
  1390. }