servmsg.c 38 KB

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