servmsg.c 40 KB

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