servmsg.c 38 KB

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