| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- #ifdef LEAF
- /*
- * servmsg.c -- part of server.mod
- *
- */
- #ifdef S_NODELAY
- #include <netinet/tcp.h>
- #endif
- #define msgop CFG_MSGOP.ldata ? CFG_MSGOP.ldata : CFG_MSGOP.gdata ? CFG_MSGOP.gdata : ""
- #define msgpass CFG_MSGPASS.ldata ? CFG_MSGPASS.ldata : CFG_MSGPASS.gdata ? CFG_MSGPASS.gdata : ""
- #define msginvite CFG_MSGINVITE.ldata ? CFG_MSGINVITE.ldata : CFG_MSGINVITE.gdata ? CFG_MSGINVITE.gdata : ""
- #define msgident CFG_MSGIDENT.ldata ? CFG_MSGIDENT.ldata : CFG_MSGIDENT.gdata ? CFG_MSGIDENT.gdata : ""
- char cursrvname[120]="";
- static time_t last_ctcp = (time_t) 0L;
- static int count_ctcp = 0;
- static char altnick_char = 0;
- /* We try to change to a preferred unique nick here. We always first try the
- * specified alternate nick. If that failes, we repeatedly modify the nick
- * until it gets accepted.
- *
- * sent nick:
- * "<altnick><c>"
- * ^--- additional count character: 1-9^-_\\[]`a-z
- * ^--------- given, alternate nick
- *
- * The last added character is always saved in altnick_char. At the very first
- * attempt (were altnick_char is 0), we try the alternate nick without any
- * additions.
- *
- * fixed by guppy (1999/02/24) and Fabian (1999/11/26)
- */
- int rolls = 0;
- static int gotfake433(char *from)
- {
- int l = strlen(botname) - 1;
- int use_chr = 1;
- char *altchrs = STR("-_\\`^[]");
- /* First run? */
- if (altnick_char == 0) {
- altnick_char = altchrs[0];
- /* the nick is already as long as it can be. */
- if (l + 1 == nick_len) {
- /* make the last char the altnick_char */
- botname[l] = altnick_char;
- } else {
- /* tack it on to the end */
- botname[++l] = altnick_char;
- botname[l + 1] = 0;
- }
- } else {
- char *p = strchr(altchrs, altnick_char);
- p++;
- if (!*p) {
- /* fun BX style rolling, WEEEE */
- if (rolls < 10) {
- char tmp;
- if (rolls == 0)
- strcpy(botname, origbotname);
- tmp = botname[8];
- if (strchr(BADNICKCHARS, tmp))
- tmp = '_';
- altnick_char = 0;
- use_chr = 0;
-
- rolls++;
- botname[8] = botname[7];
- botname[7] = botname[6];
- botname[6] = botname[5];
- botname[5] = botname[4];
- botname[4] = botname[3];
- botname[3] = botname[2];
- botname[2] = botname[1];
- botname[1] = botname[0];
- botname[0] = tmp;
- } else {
- /* when we run out of 'altchrs', then make altnick_char a random alpha */
- altnick_char = 'a' + random() % 26;
- }
- } else {
- /* else, make altnick_char the 'oknick' */
- altnick_char = (*p);
- }
- if (use_chr)
- botname[l] = altnick_char;
- }
- putlog(LOG_SERV, "*", IRC_BOTNICKINUSE, botname);
- dprintf(DP_SERVER, "NICK %s\n", botname);
- return 0;
- }
- /* Check for tcl-bound msg command, return 1 if found
- *
- * msg: proc-name <nick> <user@host> <handle> <args...>
- */
- static int check_bind_msg(char *cmd, char *nick, char *uhost, struct userrec *u, char *args)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
- int x;
- get_user_flagrec(u, &fr, NULL);
- x = check_bind(BT_msg, cmd, &fr, nick, uhost, u, args);
- if (x & BIND_RET_LOG) putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, u ? u->handle : "*" , args);
- if (x) return(1);
- else return(0);
- }
- #ifdef S_AUTH
- static int check_bind_msgc(char *cmd, char *nick, char *from, struct userrec *u, char *args)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0};
- int x;
- x = check_bind(BT_msgc, cmd, &fr, nick, from, u, args, NULL);
- if (x & BIND_RET_LOG)
- putlog(LOG_CMDS, "*", " in check_bind_msgc(%s!%s) !%s! %s%s %s", nick, from, u ? u->handle : "*", cmdprefix, cmd, args);
- if (x & BIND_RET_BREAK) return(1);
- return(0);
- }
- #endif /* S_AUTH */
- /* Return 1 if processed.
- */
- static int check_bind_raw(char *from, char *code, char *msg)
- {
- // return check_bind(BT_raw, code, NULL, from, code, msg);
- return check_bind(BT_raw, code, NULL, from, msg);
- }
- static int check_bind_ctcpr(char *nick, char *uhost, struct userrec *u,
- char *dest, char *keyword, char *args,
- bind_table_t *table)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
- get_user_flagrec(u, &fr, NULL);
- return check_bind(table, keyword, &fr, nick, uhost, u, dest, keyword, args);
- }
- static int match_my_nick(char *nick)
- {
- if (!rfc_casecmp(nick, botname))
- return 1;
- return 0;
- }
- /* 001: welcome to IRC (use it to fix the server name)
- */
- static int got001(char *from, char *msg)
- {
- struct server_list *x;
- int i;
- struct chanset_t *chan;
- /* Ok...param #1 of 001 = what server thinks my nick is */
- server_online = now;
- checked_hostmask = 0;
- fixcolon(msg);
- strncpyz(botname, msg, NICKLEN);
- altnick_char = 0;
- strncpyz(cursrvname, from, sizeof(cursrvname));
- dprintf(DP_SERVER, "WHOIS %s\n", botname); /* get user@host */
- dprintf(DP_SERVER, "MODE %s +iws\n");
- x = serverlist;
- if (x == NULL)
- return 0; /* Uh, no server list */
- /* Only join if the IRC module is loaded. */
- if (module_find("irc", 0, 0))
- for (chan = chanset; chan; chan = chan->next) {
- chan->status &= ~(CHAN_ACTIVE | CHAN_PEND);
- if (shouldjoin(chan))
- dprintf(DP_SERVER, "JOIN %s %s\n",
- (chan->name[0]) ? chan->name : chan->dname,
- chan->channel.key[0] ? chan->channel.key : chan->key_prot);
- }
- if (egg_strcasecmp(from, dcc[servidx].host)) {
- putlog(LOG_MISC, "*", "(%s claims to be %s; updating server list)",
- dcc[servidx].host, from);
- for (i = curserv; i > 0 && x != NULL; i--)
- x = x->next;
- if (x == NULL) {
- putlog(LOG_MISC, "*", "Invalid server list!");
- return 0;
- }
- if (x->realname)
- free(x->realname);
- if (strict_servernames == 1) {
- x->realname = NULL;
- if (x->name)
- free(x->name);
- x->name = strdup(from);
- } else {
- x->realname = strdup(from);
- }
- }
- return 0;
- }
- /* Got 442: not on channel
- */
- static int got442(char *from, char *msg)
- {
- char *chname;
- struct chanset_t *chan;
- struct server_list *x;
- int i;
- for (x = serverlist, i = 0; x; x = x->next, i++)
- if (i == curserv) {
- if (egg_strcasecmp(from, x->realname ? x->realname : x->name))
- return 0;
- break;
- }
- newsplit(&msg);
- chname = newsplit(&msg);
- chan = findchan(chname);
- if (chan)
- if (shouldjoin(chan)) {
- module_entry *me = module_find("channels", 0, 0);
- putlog(LOG_MISC, chname, IRC_SERVNOTONCHAN, chname);
- if (me && me->funcs)
- (me->funcs[CHANNEL_CLEAR])(chan, 1);
- chan->status &= ~CHAN_ACTIVE;
- dprintf(DP_MODE, "JOIN %s %s\n", chan->name,
- chan->channel.key[0] ? chan->channel.key : chan->key_prot);
- }
- return 0;
- }
- /* Close the current server connection.
- */
- static void nuke_server(char *reason)
- {
- if (serv >= 0) {
- if (reason && (servidx > 0)) dprintf(servidx, "QUIT :%s\r\n", reason);
- disconnect_server();
- }
- }
- static char ctcp_reply[1024] = "";
- static int lastmsgs[FLOOD_GLOBAL_MAX];
- static char lastmsghost[FLOOD_GLOBAL_MAX][81];
- static time_t lastmsgtime[FLOOD_GLOBAL_MAX];
- /* Do on NICK, PRIVMSG, NOTICE and JOIN.
- */
- static int detect_flood(char *floodnick, char *floodhost, char *from, int which)
- {
- char *p, ftype[10], h[1024];
- struct userrec *u;
- int thr = 0, lapse = 0, atr;
- u = get_user_by_host(from);
- atr = u ? u->flags : 0;
- if (atr & (USER_BOT))
- return 0;
- #ifdef S_AUTH
- if (findauth(floodhost) > -1)
- return 0;
- #endif /* S_AUTH */
- /* Determine how many are necessary to make a flood */
- switch (which) {
- case FLOOD_PRIVMSG:
- case FLOOD_NOTICE:
- thr = flud_thr;
- lapse = flud_time;
- strcpy(ftype, "msg");
- break;
- case FLOOD_CTCP:
- thr = flud_ctcp_thr;
- lapse = flud_ctcp_time;
- strcpy(ftype, "ctcp");
- break;
- }
- if ((thr == 0) || (lapse == 0))
- return 0; /* No flood protection */
- /* Okay, make sure i'm not flood-checking myself */
- if (match_my_nick(floodnick))
- return 0;
- if (!egg_strcasecmp(floodhost, botuserhost))
- return 0; /* My user@host (?) */
- p = strchr(floodhost, '@');
- if (p) {
- p++;
- if (egg_strcasecmp(lastmsghost[which], p)) { /* New */
- strcpy(lastmsghost[which], p);
- lastmsgtime[which] = now;
- lastmsgs[which] = 0;
- return 0;
- }
- } else
- return 0; /* Uh... whatever. */
- if (lastmsgtime[which] < now - lapse) {
- /* Flood timer expired, reset it */
- lastmsgtime[which] = now;
- lastmsgs[which] = 0;
- return 0;
- }
- lastmsgs[which]++;
- if (lastmsgs[which] >= thr) { /* FLOOD */
- /* Reset counters */
- lastmsgs[which] = 0;
- lastmsgtime[which] = 0;
- lastmsghost[which][0] = 0;
- u = get_user_by_host(from);
- /* Private msg */
- simple_sprintf(h, "*!*@%s", p);
- putlog(LOG_MISC, "*", IRC_FLOODIGNORE1, p);
- addignore(h, botnetnick, (which == FLOOD_CTCP) ? "CTCP flood" :
- "MSG/NOTICE flood", now + (60 * ignore_time));
- }
- return 0;
- }
- /* Check for more than 8 control characters in a line.
- * This could indicate: beep flood CTCP avalanche.
- */
- static int detect_avalanche(char *msg)
- {
- int count = 0;
- unsigned char *p;
- for (p = (unsigned char *) msg; (*p) && (count < 8); p++)
- if ((*p == 7) || (*p == 1))
- count++;
- if (count >= 8)
- return 1;
- else
- return 0;
- }
- /* Got a private message.
- */
- static int gotmsg(char *from, char *msg)
- {
- char *to, buf[UHOSTLEN], *nick, ctcpbuf[512], *uhost = buf, *ctcp;
- char *p, *p1, *code;
- struct userrec *u;
- int ctcp_count = 0;
- #ifdef S_AUTH
- int i = 0;
- #endif /* S_AUTH */
- int ignoring = 0;
- if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
- (*msg == '@'))) /* Notice to a channel, not handled here */
- return 0;
- ignoring = match_ignore(from);
- to = newsplit(&msg);
- fixcolon(msg);
- /* Only check if flood-ctcp is active */
- strcpy(uhost, from);
- nick = splitnick(&uhost);
- if (flud_ctcp_thr && detect_avalanche(msg)) {
- if (!ignoring) {
- putlog(LOG_MODES, "*", "Avalanche from %s - ignoring", from);
- p = strchr(uhost, '@');
- if (p != NULL)
- p++;
- else
- p = uhost;
- simple_sprintf(ctcpbuf, "*!*@%s", p);
- addignore(ctcpbuf, botnetnick, "ctcp avalanche",
- now + (60 * ignore_time));
- }
- }
- /* Check for CTCP: */
- ctcp_reply[0] = 0;
- p = strchr(msg, 1);
- while ((p != NULL) && (*p)) {
- p++;
- p1 = p;
- while ((*p != 1) && (*p != 0))
- p++;
- if (*p == 1) {
- *p = 0;
- ctcp = strcpy(ctcpbuf, p1);
- strcpy(p1 - 1, p + 1);
- if (!ignoring)
- detect_flood(nick, uhost, from,
- strncmp(ctcp, "ACTION ", 7) ? FLOOD_CTCP : FLOOD_PRIVMSG);
- /* Respond to the first answer_ctcp */
- p = strchr(msg, 1);
- if (ctcp_count < answer_ctcp) {
- ctcp_count++;
- if (ctcp[0] != ' ') {
- code = newsplit(&ctcp);
- if ((to[0] == '$') || strchr(to, '.')) {
- if (!ignoring)
- /* Don't interpret */
- putlog(LOG_PUBLIC, to, "CTCP %s: %s from %s (%s) to %s",
- code, ctcp, nick, uhost, to);
- } else {
- u = get_user_by_host(from);
- if (!ignoring || trigger_on_ignore) {
- if (!check_bind_ctcp(nick, uhost, u, to, code, ctcp) &&
- !ignoring) {
- if ((lowercase_ctcp && !egg_strcasecmp(code, "DCC")) ||
- (!lowercase_ctcp && !strcmp(code, "DCC"))) {
- /* If it gets this far unhandled, it means that
- * the user is totally unknown.
- */
- code = newsplit(&ctcp);
- if (!strcmp(code, "CHAT")) {
- if (!quiet_reject) {
- if (u)
- dprintf(DP_HELP, "NOTICE %s :%s\n", nick,
- "I'm not accepting call at the moment.");
- else
- dprintf(DP_HELP, "NOTICE %s :%s\n",
- nick, DCC_NOSTRANGERS);
- }
- if (!ischanhub())
- putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSEDNC, from);
- else
- putlog(LOG_MISC, "*", "%s: %s", DCC_REFUSED, from);
- } else
- putlog(LOG_MISC, "*", "Refused DCC %s: %s",
- code, from);
- }
- }
- if (!strcmp(code, "ACTION")) {
- putlog(LOG_MSGS, "*", "Action to %s: %s %s",
- to, nick, ctcp);
- } else {
- putlog(LOG_MSGS, "*", "CTCP %s: %s from %s (%s)",
- code, ctcp, nick, uhost);
- } /* I love a good close cascade ;) */
- }
- }
- }
- }
- }
- }
- /* Send out possible ctcp responses */
- if (ctcp_reply[0]) {
- if (ctcp_mode != 2) {
- dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
- } else {
- if (now - last_ctcp > flud_ctcp_time) {
- dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
- count_ctcp = 1;
- } else if (count_ctcp < flud_ctcp_thr) {
- dprintf(DP_HELP, "NOTICE %s :%s\n", nick, ctcp_reply);
- count_ctcp++;
- }
- last_ctcp = now;
- }
- }
- if (msg[0]) {
- if ((to[0] == '$') || (strchr(to, '.') != NULL)) {
- /* Msg from oper */
- if (!ignoring) {
- detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
- /* Do not interpret as command */
- putlog(LOG_MSGS | LOG_SERV, "*", "[%s!%s to %s] %s",nick, uhost, to, msg);
- }
- } else {
- char *code;
- struct userrec *u;
- detect_flood(nick, uhost, from, FLOOD_PRIVMSG);
- u = get_user_by_host(from);
- code = newsplit(&msg);
- rmspace(msg);
- #ifdef S_AUTH
- i = findauth(uhost);
- /* is it a cmd? */
- if (i > -1 && auth[i].authed && code[0] == cmdprefix[0] && code[1]) {
- code++;
- u = auth[i].user;
- if (check_bind_msgc(code, nick, uhost, u, msg))
- auth[i].atime = now;
- else
- putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg);
- } else if ((code[0] != cmdprefix[0] || !code[1] || i == -1 || !(auth[i].authed))) {
- #endif /* S_AUTH */
- if (!ignoring) {
- int doit = 1, result = 0;
- #ifdef S_MSGCMDS
- if (!egg_strcasecmp(code, "op") || !egg_strcasecmp(code, "pass") || !egg_strcasecmp(code, "invite")
- || !egg_strcasecmp(code, "ident")
- || !egg_strcasecmp(code, msgop) || !egg_strcasecmp(code, msgpass)
- || !egg_strcasecmp(code, msginvite) || !egg_strcasecmp(code, msgident)) {
- /* || !strcmp(code, msgop) || !strcmp(code, msgpass) || !strcmp(code, msgop)) { */
- char buf[10];
- doit = 0;
- buf[0] = 0;
- if (!egg_strcasecmp(code, msgop))
- sprintf(buf, "op");
- else if (!egg_strcasecmp(code, msgpass))
- sprintf(buf, "pass");
- else if (!egg_strcasecmp(code, msginvite))
- sprintf(buf, "invite");
- else if (!egg_strcasecmp(code, msgident))
- sprintf(buf, "ident");
- if (buf[0])
- result = check_bind_msg(buf, nick, uhost, u, msg);
- }
- #endif /* S_MSGCMDS */
- if (doit)
- result = check_bind_msg(code, nick, uhost, u, msg);
-
- if (!result)
- putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg);
- }
- #ifdef S_AUTH
- }
- #endif /* S_AUTH */
- }
- }
- return 0;
- }
- /* Got a private notice.
- */
- static int gotnotice(char *from, char *msg)
- {
- char *to, *nick, ctcpbuf[512], *p, *p1, buf[512], *uhost = buf, *ctcp;
- struct userrec *u;
- int ignoring;
- if (msg[0] && ((strchr(CHANMETA, *msg) != NULL) ||
- (*msg == '@'))) /* Notice to a channel, not handled here */
- return 0;
- ignoring = match_ignore(from);
- to = newsplit(&msg);
- fixcolon(msg);
- strcpy(uhost, from);
- nick = splitnick(&uhost);
- if (flud_ctcp_thr && detect_avalanche(msg)) {
- /* Discard -- kick user if it was to the channel */
- if (!ignoring)
- putlog(LOG_MODES, "*", "Avalanche from %s", from);
- return 0;
- }
- /* Check for CTCP: */
- p = strchr(msg, 1);
- while ((p != NULL) && (*p)) {
- p++;
- p1 = p;
- while ((*p != 1) && (*p != 0))
- p++;
- if (*p == 1) {
- *p = 0;
- ctcp = strcpy(ctcpbuf, p1);
- strcpy(p1 - 1, p + 1);
- if (!ignoring)
- detect_flood(nick, uhost, from, FLOOD_CTCP);
- p = strchr(msg, 1);
- if (ctcp[0] != ' ') {
- char *code = newsplit(&ctcp);
- if ((to[0] == '$') || strchr(to, '.')) {
- if (!ignoring)
- putlog(LOG_PUBLIC, "*",
- "CTCP reply %s: %s from %s (%s) to %s", code, ctcp,
- nick, uhost, to);
- } else {
- u = get_user_by_host(from);
- if (!ignoring || trigger_on_ignore) {
- check_bind_ctcr(nick, uhost, u, to, code, ctcp);
- if (!ignoring)
- /* Who cares? */
- putlog(LOG_MSGS, "*",
- "CTCP reply %s: %s from %s (%s) to %s",
- code, ctcp, nick, uhost, to);
- }
- }
- }
- }
- }
- if (msg[0]) {
- if (((to[0] == '$') || strchr(to, '.')) && !ignoring) {
- detect_flood(nick, uhost, from, FLOOD_NOTICE);
- putlog(LOG_MSGS | LOG_SERV, "*", "-%s (%s) to %s- %s",
- nick, uhost, to, msg);
- } else {
- /* Server notice? */
- if ((nick[0] == 0) || (uhost[0] == 0)) {
- /* Hidden `250' connection count message from server */
- if (strncmp(msg, "Highest connection count:", 25))
- putlog(LOG_SERV, "*", "-NOTICE- %s", msg);
- } else {
- detect_flood(nick, uhost, from, FLOOD_NOTICE);
- u = get_user_by_host(from);
- if (!ignoring)
- putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
- }
- }
- }
- return 0;
- }
- /* WALLOPS: oper's nuisance
- */
- static int gotwall(char *from, char *msg)
- {
- char *nick;
- fixcolon(msg);
- /* Following is not needed at all, but we'll keep it for compatibility sak$
- * so not to confuse possible scripts that are parsing log files.
- */
- if (strchr(from, '!')) {
- nick = splitnick(&from);
- putlog(LOG_WALL, "*", "!%s(%s)! %s", nick, from, msg);
- } else
- putlog(LOG_WALL, "*", "!%s! %s", from, msg);
- return 0;
- }
- static void server_10secondly()
- {
- if (server_online && keepnick) {
- /* NOTE: now that botname can but upto NICKLEN bytes long,
- * check that it's not just a truncation of the full nick.
- */
- if (strncmp(botname, origbotname, strlen(botname))) {
- /* See if my nickname is in use and if if my nick is right. */
- dprintf(DP_SERVER, "ISON :%s %s\n", botname, origbotname);
- }
- }
- }
- /* Called once a minute... but if we're the only one on the
- * channel, we only wanna send out "lusers" once every 5 mins.
- */
- static void minutely_checks()
- {
- /* Only check if we have already successfully logged in. */
- if (server_online) {
- static int count = 4;
- int ok = 0;
- struct chanset_t *chan;
- for (chan = chanset; chan; chan = chan->next) {
- if (channel_active(chan) && chan->channel.members == 1) {
- ok = 1;
- break;
- }
- }
- if (!ok)
- return;
- count++;
- if (count >= 5) {
- dprintf(DP_SERVER, "LUSERS\n");
- count = 0;
- }
- }
- }
- /* Pong from server.
- */
- static int gotpong(char *from, char *msg)
- {
- newsplit(&msg);
- fixcolon(msg); /* Scrap server name */
- server_lag = now - my_atoul(msg);
- if (server_lag > 99999) {
- /* IRCnet lagmeter support by drummer */
- server_lag = now - lastpingtime;
- }
- return 0;
- }
- /* This is a reply on ISON :<current> <orig> [<alt>]
- */
- static void got303(char *from, char *msg)
- {
- char *tmp;
- int ison_orig = 0;
- if (!keepnick ||
- !strncmp(botname, origbotname, strlen(botname))) {
- return;
- }
- newsplit(&msg);
- fixcolon(msg);
- tmp = newsplit(&msg);
- if (tmp[0] && !rfc_casecmp(botname, tmp)) {
- while ((tmp = newsplit(&msg))[0]) { /* no, it's NOT == */
- if (!rfc_casecmp(tmp, origbotname))
- ison_orig = 1;
- }
- if (!ison_orig) {
- if (!nick_juped)
- putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
- dprintf(DP_SERVER, "NICK %s\n", origbotname);
- }
- }
- }
- /* 432 : Bad nickname
- */
- static int got432(char *from, char *msg)
- {
- char *erroneus;
- newsplit(&msg);
- erroneus = newsplit(&msg);
- if (server_online)
- putlog(LOG_MISC, "*", "NICK IS INVALID: %s (keeping '%s').", erroneus,
- botname);
- else {
- putlog(LOG_MISC, "*", IRC_BADBOTNICK);
- if (!keepnick) {
- makepass(erroneus);
- erroneus[NICKMAX] = 0;
- dprintf(DP_MODE, "NICK %s\n", erroneus);
- }
- return 0;
- }
- return 0;
- }
- /* 433 : Nickname in use
- * Change nicks till we're acceptable or we give up
- */
- static int got433(char *from, char *msg)
- {
- char *tmp;
- if (server_online) {
- /* We are online and have a nickname, we'll keep it */
- newsplit(&msg);
- tmp = newsplit(&msg);
- putlog(LOG_MISC, "*", "NICK IN USE: %s (keeping '%s').", tmp, botname);
- nick_juped = 0;
- return 0;
- }
- gotfake433(from);
- return 0;
- }
- /* 437 : Nickname juped (IRCnet)
- */
- static int got437(char *from, char *msg)
- {
- char *s;
- struct chanset_t *chan;
- newsplit(&msg);
- s = newsplit(&msg);
- if (s[0] && (strchr(CHANMETA, s[0]) != NULL)) {
- chan = findchan(s);
- if (chan) {
- if (chan->status & CHAN_ACTIVE) {
- putlog(LOG_MISC, "*", IRC_CANTCHANGENICK, s);
- } else {
- if (!channel_juped(chan)) {
- putlog(LOG_MISC, "*", IRC_CHANNELJUPED, s);
- chan->status |= CHAN_JUPED;
- }
- }
- }
- } else if (server_online) {
- if (!nick_juped)
- putlog(LOG_MISC, "*", "NICK IS JUPED: %s (keeping '%s').", s, botname);
- if (!rfc_casecmp(s, origbotname))
- nick_juped = 1;
- } else {
- putlog(LOG_MISC, "*", "%s: %s", IRC_BOTNICKJUPED, s);
- gotfake433(from);
- }
- return 0;
- }
- /* 438 : Nick change too fast
- */
- static int got438(char *from, char *msg)
- {
- newsplit(&msg);
- newsplit(&msg);
- fixcolon(msg);
- putlog(LOG_MISC, "*", "%s", msg);
- return 0;
- }
- static int got451(char *from, char *msg)
- {
- /* Usually if we get this then we really messed up somewhere
- * or this is a non-standard server, so we log it and kill the socket
- * hoping the next server will work :) -poptix
- */
- /* Um, this does occur on a lagged anti-spoof server connection if the
- * (minutely) sending of joins occurs before the bot does its ping reply.
- * Probably should do something about it some time - beldin
- */
- putlog(LOG_MISC, "*", IRC_NOTREGISTERED1, from);
- nuke_server(IRC_NOTREGISTERED2);
- return 0;
- }
- /* Got error notice
- */
- static int goterror(char *from, char *msg)
- {
- /* FIXME: fixcolon doesn't do what we need here, this is a temp fix
- * fixcolon(msg);
- */
- if (msg[0] == ':')
- msg++;
- putlog(LOG_SERV, "*", "-ERROR from server- %s", msg);
- if (serverror_quit) {
- putlog(LOG_SERV, "*", "Disconnecting from server.");
- nuke_server("Bah, stupid error messages.");
- }
- return 1;
- }
- /* Got nick change.
- */
- static int gotnick(char *from, char *msg)
- {
- char *nick;
- struct userrec *u;
- u = get_user_by_host(from);
- nick = splitnick(&from);
- fixcolon(msg);
- check_queues(nick, msg);
- if (match_my_nick(nick)) {
- /* Regained nick! */
- strncpyz(botname, msg, NICKLEN);
- altnick_char = 0;
- if (!strcmp(msg, origbotname)) {
- putlog(LOG_SERV | LOG_MISC, "*", "Regained nickname '%s'.", msg);
- nick_juped = 0;
- } else if (keepnick && strcmp(nick, msg)) {
- putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
- if (!rfc_casecmp(nick, origbotname)) {
- putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
- dprintf(DP_SERVER, "NICK %s\n", origbotname);
- }
- } else
- putlog(LOG_SERV | LOG_MISC, "*", "Nickname changed to '%s'???", msg);
- } else if ((keepnick) && (rfc_casecmp(nick, msg))) {
- /* Only do the below if there was actual nick change, case doesn't count */
- if (!rfc_casecmp(nick, origbotname)) {
- putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
- dprintf(DP_SERVER, "NICK %s\n", origbotname);
- }
- }
- return 0;
- }
- static int gotmode(char *from, char *msg)
- {
- char *ch, *buf = strdup(msg), *buf_ptr;
- buf_ptr = buf;
- ch = newsplit(&buf);
- /* Usermode changes? */
- if (strchr(CHANMETA, ch[0]) == NULL) {
- if (match_my_nick(ch) && check_mode_r) {
- /* umode +r? - D0H dalnet uses it to mean something different */
- fixcolon(buf);
- if ((buf[0] == '+') && strchr(buf, 'r')) {
- putlog(LOG_MISC | LOG_JOIN, "*", "%s has me i-lined (jumping)", dcc[servidx].host);
- nuke_server("i-lines suck");
- }
- }
- }
- free(buf_ptr);
- return 0;
- }
- static void disconnect_server()
- {
- int idx;
- if (server_online > 0) check_bind_event("disconnect-server");
- server_online = 0;
- if (servidx != -1 && dcc[servidx].sock >= 0) {
- killsock(dcc[servidx].sock);
- dcc[servidx].sock = (-1);
- }
- serv = -1;
- botuserhost[0] = 0;
- idx = servidx;
- servidx = -1;
- lostdcc(idx);
- }
- static void eof_server(int idx)
- {
- putlog(LOG_SERV, "*", "%s %s", IRC_DISCONNECTED, dcc[idx].host);
- #ifdef S_AUTH
- {
- int i = 0;
- if (ischanhub() && auth_total > 0) {
- putlog(LOG_DEBUG, "*", "Removing %d auth entries.", auth_total);
- for (i = 0; i < auth_total; i++)
- removeauth(i);
- }
- }
- #endif /* S_AUTH */
- disconnect_server();
- }
- static void display_server(int idx, char *buf)
- {
- sprintf(buf, "%s (lag: %d)", trying_server ? "conn" : "serv",
- server_lag);
- }
- static void connect_server(void);
- static void kill_server(int idx, void *x)
- {
- module_entry *me;
- disconnect_server();
- if ((me = module_find("channels", 0, 0)) && me->funcs) {
- struct chanset_t *chan;
- for (chan = chanset; chan; chan = chan->next)
- (me->funcs[CHANNEL_CLEAR]) (chan, 1);
- }
- /* A new server connection will be automatically initiated in
- about 2 seconds. */
- }
- static void timeout_server(int idx)
- {
- putlog(LOG_SERV, "*", "Timeout: connect to %s", dcc[idx].host);
- disconnect_server();
- }
- static void server_activity(int idx, char *msg, int len);
- static struct dcc_table SERVER_SOCKET =
- {
- "SERVER",
- 0,
- eof_server,
- server_activity,
- NULL,
- timeout_server,
- display_server,
- kill_server,
- NULL
- };
- int isop(char *mode)
- {
- int state = 0,
- cnt = 0;
- char *p;
- p = mode;
- while ((*p) && (*p != ' ')) {
- if (*p == '-')
- state = 1;
- else if (*p == '+')
- state = 0;
- else if ((!state) && (*p == 'o'))
- cnt++;
- p++;
- }
- return (cnt >= 1);
- }
- int ismdop(char *mode)
- {
- int state = 0,
- cnt = 0;
- char *p;
- p = mode;
- while ((*p) && (*p != ' ')) {
- if (*p == '-')
- state = 1;
- else if (*p == '+')
- state = 0;
- else if ((state) && (*p == 'o'))
- cnt++;
- p++;
- }
- return (cnt >= 3);
- }
- static void server_activity(int idx, char *msg, int len)
- {
- char *from, *code;
- if (trying_server) {
- strcpy(dcc[idx].nick, "(server)");
- putlog(LOG_SERV, "*", "Connected to %s", dcc[idx].host);
- trying_server = 0;
- SERVER_SOCKET.timeout_val = 0;
- }
- waiting_for_awake = 0;
- from = "";
- if (msg[0] == ':') {
- msg++;
- from = newsplit(&msg);
- }
- code = newsplit(&msg);
- if (use_console_r) {
- if (!strcmp(code, "PRIVMSG") || !strcmp(code, "NOTICE")) {
- if (!match_ignore(from))
- putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
- } else
- putlog(LOG_RAW, "@", "[@] %s %s %s", from, code, msg);
- }
- /* This has GOT to go into the raw binding table, * merely because this
- * is less effecient.
- */
- check_bind_raw(from, code, msg);
- }
- static int gotping(char *from, char *msg)
- {
- fixcolon(msg);
- dprintf(DP_MODE, "PONG :%s\n", msg);
- return 0;
- }
- static int gotkick(char *from, char *msg)
- {
- char *nick;
- nick = from;
- if (rfc_casecmp(nick, botname))
- /* Not my kick, I don't need to bother about it. */
- return 0;
- if (use_penalties) {
- last_time += 2;
- if (debug_output)
- putlog(LOG_SRVOUT, "*", "adding 2secs penalty (successful kick)");
- }
- return 0;
- }
- /* Another sec penalty if bot did a whois on another server.
- */
- static int whoispenalty(char *from, char *msg)
- {
- struct server_list *x = serverlist;
- int i, ii;
- if (x && use_penalties) {
- i = ii = 0;
- for (; x; x = x->next) {
- if (i == curserv) {
- if ((strict_servernames == 1) || !x->realname) {
- if (strcmp(x->name, from))
- ii = 1;
- } else {
- if (strcmp(x->realname, from))
- ii = 1;
- }
- }
- i++;
- }
- if (ii) {
- last_time += 1;
- if (debug_output)
- putlog(LOG_SRVOUT, "*", "adding 1sec penalty (remote whois)");
- }
- }
- return 0;
- }
- static int got311(char *from, char *msg)
- {
- char *n1, *n2, *u, *h;
-
- n1 = newsplit(&msg);
- n2 = newsplit(&msg);
- u = newsplit(&msg);
- h = newsplit(&msg);
-
- if (!n1 || !n2 || !u || !h)
- return 0;
-
- if (match_my_nick(n2))
- egg_snprintf(botuserhost, sizeof botuserhost, "%s@%s", u, h);
-
- return 0;
- }
- static cmd_t my_raw_binds[] =
- {
- {"PRIVMSG", "", (Function) gotmsg, NULL},
- {"NOTICE", "", (Function) gotnotice, NULL},
- {"MODE", "", (Function) gotmode, NULL},
- {"PING", "", (Function) gotping, NULL},
- {"PONG", "", (Function) gotpong, NULL},
- {"WALLOPS", "", (Function) gotwall, NULL},
- {"001", "", (Function) got001, NULL},
- {"303", "", (Function) got303, NULL},
- {"432", "", (Function) got432, NULL},
- {"433", "", (Function) got433, NULL},
- {"437", "", (Function) got437, NULL},
- {"438", "", (Function) got438, NULL},
- {"451", "", (Function) got451, NULL},
- {"442", "", (Function) got442, NULL},
- {"NICK", "", (Function) gotnick, NULL},
- {"ERROR", "", (Function) goterror, NULL},
- /* ircu2.10.10 has a bug when a client is throttled ERROR is sent wrong */
- {"ERROR:", "", (Function) goterror, NULL},
- {"KICK", "", (Function) gotkick, NULL},
- {"318", "", (Function) whoispenalty, NULL},
- {"311", "", (Function) got311, NULL},
- {NULL, NULL, NULL, NULL}
- };
- static void server_resolve_success(int);
- static void server_resolve_failure(int);
- /* Hook up to a server
- */
- static void connect_server(void)
- {
- char pass[121], botserver[UHOSTLEN];
- static int oldserv = -1;
- unsigned int botserverport = 0;
- waiting_for_awake = 0;
- trying_server = now;
- empty_msgq();
- /* Start up the counter (always reset it if "never-give-up" is on) */
- if ((oldserv < 0) || (never_give_up))
- oldserv = curserv;
- if (newserverport) { /* Jump to specified server */
- curserv = (-1); /* Reset server list */
- strcpy(botserver, newserver);
- botserverport = newserverport;
- strcpy(pass, newserverpass);
- newserver[0] = 0;
- newserverport = 0;
- newserverpass[0] = 0;
- oldserv = (-1);
- } else
- pass[0] = 0;
- if (!cycle_time) {
- struct chanset_t *chan;
- struct server_list *x = serverlist;
- if (!x)
- return;
-
- servidx = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
- if (servidx < 0) {
- putlog(LOG_SERV, "*",
- "NO MORE DCC CONNECTIONS -- Can't create server connection.");
- return;
- }
- next_server(&curserv, botserver, &botserverport, pass);
- putlog(LOG_SERV, "*", "%s %s:%d", IRC_SERVERTRY, botserver, botserverport);
- dcc[servidx].port = botserverport;
- strcpy(dcc[servidx].nick, "(server)");
- strncpyz(dcc[servidx].host, botserver, UHOSTLEN);
- botuserhost[0] = 0;
- nick_juped = 0;
- for (chan = chanset; chan; chan = chan->next)
- chan->status &= ~CHAN_JUPED;
- dcc[servidx].timeval = now;
- dcc[servidx].sock = -1;
- dcc[servidx].u.dns->host = calloc(1, strlen(dcc[servidx].host) + 1);
- strcpy(dcc[servidx].u.dns->host, dcc[servidx].host);
- dcc[servidx].u.dns->cbuf = calloc(1, strlen(pass) + 1);
- strcpy(dcc[servidx].u.dns->cbuf, pass);
- dcc[servidx].u.dns->dns_success = server_resolve_success;
- dcc[servidx].u.dns->dns_failure = server_resolve_failure;
- dcc[servidx].u.dns->dns_type = RES_IPBYHOST;
- dcc[servidx].u.dns->type = &SERVER_SOCKET;
- if (server_cycle_wait)
- /* Back to 1st server & set wait time.
- * Note: Put it here, just in case the server quits on us quickly
- */
- cycle_time = server_cycle_wait;
- else
- cycle_time = 0;
- /* I'm resolving... don't start another server connect request */
- resolvserv = 1;
- /* Resolve the hostname. */
- #ifdef USE_IPV6
- server_resolve_success(servidx);
- #else
- dcc_dnsipbyhost(dcc[servidx].host);
- #endif /* USE_IPV6 */
- }
- }
- static void server_resolve_failure(int servidx)
- {
- serv = -1;
- servidx = -1;
- resolvserv = 0;
- putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host,
- IRC_DNSFAILED);
- lostdcc(servidx);
- }
- static void server_resolve_success(int servidx)
- {
- int oldserv = dcc[servidx].u.dns->ibuf;
- #ifdef S_NODELAY
- int i = 0;
- #endif
- char s[121], pass[121];
- resolvserv = 0;
- dcc[servidx].addr = dcc[servidx].u.dns->ip;
- strcpy(pass, dcc[servidx].u.dns->cbuf);
- changeover_dcc(servidx, &SERVER_SOCKET, 0);
- #ifdef USE_IPV6
- serv = open_telnet(dcc[servidx].host, dcc[servidx].port);
- #else
- serv = open_telnet(iptostr(htonl(dcc[servidx].addr)), dcc[servidx].port);
- #endif /* USE_IPV6 */
- if (serv < 0) {
- neterror(s);
- putlog(LOG_SERV, "*", "%s %s (%s)", IRC_FAILEDCONNECT, dcc[servidx].host,
- s);
- lostdcc(servidx);
- servidx = -1;
- if (oldserv == curserv && !never_give_up)
- fatal("NO SERVERS WILL ACCEPT MY CONNECTION.", 0);
- } else {
- dcc[servidx].sock = serv;
- #ifdef HAVE_SSL
- if (!ssl_link(dcc[servidx].sock, CONNECT_SSL)) {
- dcc[servidx].ssl = 0;
- putlog(LOG_SERV, "*", "SSL for '%s' failed", dcc[servidx].host);
- } else {
- putlog(LOG_SERV, "*", "SSL for '%s' successful", dcc[servidx].host);
- dcc[servidx].ssl = 1;
- }
- #else
- dcc[servidx].ssl = 0;
- #endif /* HAVE_SSL */
- #ifdef S_NODELAY
- i = 1;
- setsockopt(serv, 6, TCP_NODELAY, &i, sizeof(i));
- #endif /* S_NODELAY */
- /* Queue standard login */
- dcc[servidx].timeval = now;
- SERVER_SOCKET.timeout_val = &server_timeout;
- /* Another server may have truncated it, so use the original */
- strcpy(botname, origbotname);
- /* Start alternate nicks from the beginning */
- altnick_char = 0;
- if (pass[0]) dprintf(DP_MODE, "PASS %s\r\n", pass);
- dprintf(DP_MODE, "NICK %s\r\n", botname);
- dprintf(DP_MODE, "USER %s localhost %s :%s\r\n", botuser, dcc[servidx].host, botrealname);
- /* Wait for async result now */
- }
- }
- #endif /* LEAF */
|