| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697 |
- #ifdef LEAF
- /*
- * servmsg.c -- part of server.mod
- *
- */
- #ifdef S_NODELAY
- #include <netinet/tcp.h>
- #endif
- 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)
- */
- static int gotfake433(char *from)
- {
- int l = strlen(botname) - 1;
- char *oknicks = "-_\\`^[]{}";
- Context;
- /* First run? */
- if (altnick_char == 0) {
- Context;
- altnick_char = oknicks[0];
- Context;
- if (l + 1 == NICKMAX) {
- Context;
- botname[l] = altnick_char;
- } else {
- Context;
- botname[++l] = altnick_char;
- botname[l + 1] = 0;
- }
- } else {
- char *p = strchr(oknicks, altnick_char);
- Context;
- p++;
- Context;
- if (!*p) {
- Context;
- altnick_char = 'a' + random() % 26;
- } else {
- Context;
- altnick_char = (*p);
- }
- Context;
- botname[l] = altnick_char;
- }
- putlog(LOG_SERV, "*", IRC_BOTNICKINUSE, botname);
- Context;
- dprintf(DP_MODE, "NICK %s\n", botname);
- Context;
- return 0;
- }
- /* Check for tcl-bound msg command, return 1 if found
- *
- * msg: proc-name <nick> <user@host> <handle> <args...>
- */
- static int check_tcl_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};
- char *hand = u ? u->handle : "*";
- int x;
- get_user_flagrec(u, &fr, NULL);
- Tcl_SetVar(interp, "_msg1", nick, 0);
- Tcl_SetVar(interp, "_msg2", uhost, 0);
- Tcl_SetVar(interp, "_msg3", hand, 0);
- Tcl_SetVar(interp, "_msg4", args, 0);
- x = check_tcl_bind(H_msg, cmd, &fr, " $_msg1 $_msg2 $_msg3 $_msg4",
- MATCH_EXACT | BIND_HAS_BUILTINS | BIND_USE_ATTR);
- if (x == BIND_EXEC_LOG)
- putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s %s", nick, uhost, hand,
- cmd, args);
- return ((x == BIND_MATCHED) || (x == BIND_EXECUTED) || (x == BIND_EXEC_LOG));
- }
- static int check_tcl_msgc(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};
- char *hand = u ? u->handle : "*";
- int x;
- get_user_flagrec(u, &fr, NULL);
- Tcl_SetVar(interp, "_msgc1", nick, 0);
- Tcl_SetVar(interp, "_msgc2", uhost, 0);
- Tcl_SetVar(interp, "_msgc3", hand, 0);
- Tcl_SetVar(interp, "_msgc4", args, 0);
- Tcl_SetVar(interp, "_msgc5", NULL, 0);
- x = check_tcl_bind(H_msgc, cmd, &fr, " $_msgc1 $_msgc2 $_msgc3 $_msgc4 $_msgc5",
- MATCH_EXACT | BIND_HAS_BUILTINS | BIND_USE_ATTR);
- if (x == BIND_EXEC_LOG)
- putlog(LOG_CMDS, "*", "(%s!%s) !%s! %s%s %s", nick, uhost, hand, cmdprefix,
- cmd, args);
- return ((x == BIND_MATCHED) || (x == BIND_EXECUTED) || (x == BIND_EXEC_LOG));
- }
- static void check_tcl_notc(char *nick, char *uhost, struct userrec *u,
- char *dest, char *arg)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
- get_user_flagrec(u, &fr, NULL);
- Tcl_SetVar(interp, "_notc1", nick, 0);
- Tcl_SetVar(interp, "_notc2", uhost, 0);
- Tcl_SetVar(interp, "_notc3", u ? u->handle : "*", 0);
- Tcl_SetVar(interp, "_notc4", arg, 0);
- Tcl_SetVar(interp, "_notc5", dest, 0);
- check_tcl_bind(H_notc, arg, &fr, " $_notc1 $_notc2 $_notc3 $_notc4 $_notc5",
- MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE);
- }
- static void check_tcl_msgm(char *cmd, char *nick, char *uhost,
- struct userrec *u, char *arg)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
- char args[1024];
- if (arg[0])
- simple_sprintf(args, "%s %s", cmd, arg);
- else
- strcpy(args, cmd);
- get_user_flagrec(u, &fr, NULL);
- Tcl_SetVar(interp, "_msgm1", nick, 0);
- Tcl_SetVar(interp, "_msgm2", uhost, 0);
- Tcl_SetVar(interp, "_msgm3", u ? u->handle : "*", 0);
- Tcl_SetVar(interp, "_msgm4", args, 0);
- check_tcl_bind(H_msgm, args, &fr, " $_msgm1 $_msgm2 $_msgm3 $_msgm4",
- MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE);
- }
- /* Return 1 if processed.
- */
- static int check_tcl_raw(char *from, char *code, char *msg)
- {
- int x;
- Context;
- Tcl_SetVar(interp, "_raw1", from, 0);
- Tcl_SetVar(interp, "_raw2", code, 0);
- Tcl_SetVar(interp, "_raw3", msg, 0);
- x = check_tcl_bind(H_raw, code, 0, " $_raw1 $_raw2 $_raw3",
- MATCH_EXACT | BIND_STACKABLE | BIND_WANTRET);
- return (x == BIND_EXEC_LOG);
- }
- static int check_tcl_ctcpr(char *nick, char *uhost, struct userrec *u,
- char *dest, char *keyword, char *args,
- p_tcl_bind_list table)
- {
- struct flag_record fr = {FR_GLOBAL | FR_CHAN | FR_ANYWH, 0, 0, 0, 0, 0};
- int x;
- get_user_flagrec(u, &fr, NULL);
- Tcl_SetVar(interp, "_ctcpr1", nick, 0);
- Tcl_SetVar(interp, "_ctcpr2", uhost, 0);
- Tcl_SetVar(interp, "_ctcpr3", u ? u->handle : "*", 0);
- Tcl_SetVar(interp, "_ctcpr4", dest, 0);
- Tcl_SetVar(interp, "_ctcpr5", keyword, 0);
- Tcl_SetVar(interp, "_ctcpr6", args, 0);
- x = check_tcl_bind(table, keyword, &fr,
- " $_ctcpr1 $_ctcpr2 $_ctcpr3 $_ctcpr4 $_ctcpr5 $_ctcpr6",
- MATCH_MASK | BIND_USE_ATTR | BIND_STACKABLE |
- ((table == H_ctcp) ? BIND_WANTRET : 0));
- return (x == BIND_EXEC_LOG) || (table == H_ctcr);
- }
- static int check_tcl_wall(char *from, char *msg)
- {
- int x;
- Tcl_SetVar(interp, "_wall1", from, 0);
- Tcl_SetVar(interp, "_wall2", msg, 0);
- x = check_tcl_bind(H_wall, msg, 0, " $_wall1 $_wall2",
- MATCH_MASK | BIND_STACKABLE);
- if (x == BIND_EXEC_LOG) {
- putlog(LOG_WALL, "*", "!%s! %s", from, msg);
- return 1;
- } else
- return 0;
- }
- static int check_tcl_flud(char *nick, char *uhost, struct userrec *u,
- char *ftype, char *chname)
- {
- int x;
- Tcl_SetVar(interp, "_flud1", nick, 0);
- Tcl_SetVar(interp, "_flud2", uhost, 0);
- Tcl_SetVar(interp, "_flud3", u ? u->handle : "*", 0);
- Tcl_SetVar(interp, "_flud4", ftype, 0);
- Tcl_SetVar(interp, "_flud5", chname, 0);
- x = check_tcl_bind(H_flud, ftype, 0,
- " $_flud1 $_flud2 $_flud3 $_flud4 $_flud5",
- MATCH_MASK | BIND_STACKABLE | BIND_WANTRET);
- return (x == BIND_EXEC_LOG);
- }
- 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, servidx = findanyidx(serv);
- struct chanset_t *chan;
- /* Ok...param #1 of 001 = what server thinks my nick is */
- server_online = now;
- fixcolon(msg);
- strncpyz(botname, msg, NICKLEN);
- altnick_char = 0;
- strncpy0(cursrvname, from, sizeof(cursrvname));
- dprintf(DP_SERVER, "WHOIS %s\n", botname); /* get user@host */
- /* Call Tcl init-server */
- if (initserver[0])
- do_tcl("init-server", initserver);
- check_tcl_event("init-server");
- 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 (!channel_inactive(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)
- nfree(x->realname);
- if (strict_servernames == 1) {
- x->realname = NULL;
- if (x->name)
- nfree(x->name);
- x->name = nmalloc(strlen(from) + 1);
- strcpy(x->name, from);
- } else {
- x->realname = nmalloc(strlen(from) + 1);
- strcpy(x->realname, 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 (!channel_inactive(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) {
- int servidx = findanyidx(serv);
- if (reason && (servidx > 0))
- dprintf(servidx, "QUIT :%s\n", reason);
- disconnect_server(servidx);
- lostdcc(servidx);
- }
- }
- 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;
- if (isauthed(floodhost) > -1)
- return 0;
- /* 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);
- if (check_tcl_flud(floodnick, floodhost, u, ftype, "*"))
- return 0;
- /* 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, i;
- 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_tcl_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);
- }
- 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);
- Context;
- i = isauthed(uhost);
- /* is it a cmd? */
- Context;
- if (i > -1 && auth[i].authed && code[0] == cmdprefix[0] && code[1]) {
- code++;
- Context;
- u = auth[i].user;
- if (check_tcl_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))) {
- Context;
- if (!ignoring || trigger_on_ignore)
- check_tcl_msgm(code, nick, uhost, u, msg);
- if (!ignoring)
- if (!check_tcl_msg(code, nick, uhost, u, msg))
- putlog(LOG_MSGS, "*", "[%s] %s %s", from, code, msg);
- }
- }
- }
- 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_tcl_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 || trigger_on_ignore)
- check_tcl_notc(nick, uhost, u, botname, msg);
- if (!ignoring)
- putlog(LOG_MSGS, "*", "-%s (%s)- %s", nick, uhost, msg);
- }
- }
- }
- return 0;
- }
- /* got 251: lusers
- * <server> 251 <to> :there are 2258 users and 805 invisible on 127 servers
- */
- static int got251(char *from, char *msg)
- {
- int i;
- char *servs;
- if (min_servs == 0)
- return 0; /* No minimum limit on servers */
- newsplit(&msg);
- fixcolon(msg); /* NOTE!!! If servlimit is not set or is 0 */
- for (i = 0; i < 8; i++)
- newsplit(&msg); /* lusers IS NOT SENT AT ALL!! */
- servs = newsplit(&msg);
- if (strncmp(msg, "servers", 7))
- return 0; /* Was invalid format */
- while (*servs && (*servs < 32))
- servs++; /* I've seen some lame nets put bolds &
- * stuff in here :/ */
- i = atoi(servs);
- if (i < min_servs) {
- putlog(LOG_SERV, "*", IRC_AUTOJUMP, min_servs, i);
- nuke_server(IRC_CHANGINGSERV);
- }
- return 0;
- }
- /* WALLOPS: oper's nuisance
- */
- static int gotwall(char *from, char *msg)
- {
- char *nick;
- int r;
- fixcolon(msg);
- r = check_tcl_wall(from, msg);
- if (r == 0) {
- /* 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()
- {
- char *alt;
- if (!server_online)
- return;
- if (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. */
- alt = get_altbotnick();
- if (alt[0] && egg_strcasecmp (botname, alt))
- dprintf(DP_SERVER, "ISON :%s %s %s\n", botname, origbotname, alt);
- else
- 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()
- {
- static int count = 4;
- int ok = 0;
- struct chanset_t *chan;
- /* Only check if we have already successfully logged in. */
- if (!server_online)
- return;
- if (min_servs == 0)
- return;
- 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, *alt;
- int ison_orig = 0, ison_alt = 0;
- if (!keepnick ||
- !strncmp(botname, origbotname, strlen(botname))) {
- return;
- }
- newsplit(&msg);
- fixcolon(msg);
- alt = get_altbotnick();
- 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;
- else if (alt[0] && !rfc_casecmp(tmp, alt))
- ison_alt = 1;
- }
- if (!ison_orig) {
- if (!nick_juped)
- putlog(LOG_MISC, "*", IRC_GETORIGNICK, origbotname);
- dprintf(DP_SERVER, "NICK %s\n", origbotname);
- } else if (alt[0] && !ison_alt && rfc_casecmp(botname, alt)) {
- putlog(LOG_MISC, "*", IRC_GETALTNICK, alt);
- dprintf(DP_SERVER, "NICK %s\n", alt);
- }
- }
- }
- /* 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;
- Context;
- 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;
- }
- Context;
- gotfake433(from);
- Context;
- 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, *alt = get_altbotnick();
- 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 (alt[0] && !strcmp(msg, alt))
- putlog(LOG_SERV | LOG_MISC, "*", "Regained alternate nickname '%s'.",
- msg);
- 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 if (alt[0] && !rfc_casecmp(nick, alt)
- && egg_strcasecmp(botname, origbotname)) {
- putlog(LOG_MISC, "*", IRC_GETALTNICK, alt);
- dprintf(DP_SERVER, "NICK %s\n", alt);
- }
- } 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);
- } else if (alt[0] && !rfc_casecmp(nick, alt) &&
- egg_strcasecmp(botname, origbotname)) {
- putlog(LOG_MISC, "*", IRC_GETALTNICK, altnick);
- dprintf(DP_SERVER, "NICK %s\n", altnick);
- }
- }
- return 0;
- }
- static int gotmode(char *from, char *msg)
- {
- char *ch;
- Context;
- ch = newsplit(&msg);
- /* 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(msg);
- if ((msg[0] == '+') && strchr(msg, 'r')) {
- int servidx = findanyidx(serv);
- putlog(LOG_MISC | LOG_JOIN, "*",
- "%s has me i-lined (jumping)", dcc[servidx].host);
- nuke_server("i-lines suck");
- }
- }
- }
- return 0;
- }
- static void disconnect_server(int idx)
- {
- if (server_online > 0)
- check_tcl_event("disconnect-server");
- server_online = 0;
- if (dcc[idx].sock >= 0)
- killsock(dcc[idx].sock);
- dcc[idx].sock = (-1);
- serv = (-1);
- botuserhost[0] = 0;
- }
- static void eof_server(int idx)
- {
- int i = 0;
- putlog(LOG_SERV, "*", "%s %s", IRC_DISCONNECTED, dcc[idx].host);
- if (ischanhub() && auth_total > 0) {
- putlog(LOG_DEBUG, "*", "Removing %d auth entries.", auth_total);
- for (i = 0; i < auth_total; i++)
- removeauth(i);
- }
- disconnect_server(idx);
- lostdcc(idx);
- }
- 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(idx);
- 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(idx);
- lostdcc(idx);
- }
- 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,
- NULL,
- 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);
- }
- /*
- void got_rsn(char *botnick, char *code, char *par) {
- if (strcmp(origbotname, botname)) {
- randuse = 0;
- dprintf(DP_MODE, STR("NICK %s\n"), origbotname);
- }
- }
- void got_rn(char *botnick, char *code, char *par) {
- int l = (rand() % 4) + 6, i;
- char newnick[NICKLEN+1];
- for (i=0;i<l;i++)
- newnick[i]=(rand() % 2) * 32 + 65 + rand() % 26;
- newnick[l]=0;
- randuse = 1;
- //havealt = 1;
- dprintf(DP_MODE, STR("NICK %s\n"), newnick);
- }
- */
- static void server_activity(int idx, char *msg, int len)
- {
- char *from, *code, tmp[1024];
- char sign = '+';
- Context;
- 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;
- }
- Context;
- waiting_for_awake = 0;
- from = "";
- if (msg[0] == ':') {
- msg++;
- from = newsplit(&msg);
- }
- code = newsplit(&msg);
- /* check MODEs now, we're in a rush */
- Context;
- if (!strcmp(code, STR("MODE")) && (msg[0] == '#') && strchr(from, '!')) {
- /* It's MODE #chan by a user */
- char *modes[5] = { NULL, NULL, NULL, NULL, NULL };
- char *nfrom,
- *hfrom;
- int i;
- struct userrec *ufrom = NULL;
- struct chanset_t *chan = NULL;
- char work[1024],
- *wptr,
- *p;
- memberlist *m;
- int modecnt = 0,
- ops = 0,
- deops = 0,
- bans = 0,
- unbans = 0;
- /* Split up the mode: #chan modes param param param param */
- strncpy0(work, msg, sizeof(work));
- wptr = work;
- Context;
- p = newsplit(&wptr);
- chan = findchan(p);
- p = newsplit(&wptr);
- while (*p) {
- char *mp;
- if (*p == '+')
- sign = '+';
- else if (*p == '-')
- sign = '-';
- else if (strchr(STR("oblkvIe"), p[0])) {
- mp = newsplit(&wptr);
- if (strchr("ob", p[0])) {
- /* Just want o's and b's */
- modes[modecnt] = nmalloc(strlen(mp) + 4);
- sprintf(modes[modecnt], STR("%c%c %s"), sign, p[0], mp);
- modecnt++;
- if (p[0] == 'o') {
- if (sign == '+')
- ops++;
- else
- deops++;
- }
- if (p[0] == 'b') {
- if (sign == '+')
- bans++;
- else
- unbans++;
- }
- }
- } else if (strchr(STR("pstnmi"), p[0])) {
- } else {
- /* hrmm... what modechar did i forget? */
- putlog(LOG_ERRORS, "*", STR("Forgotten modechar: %c"), p[0]);
- }
- p++;
- }
- Context;
- ufrom = get_user_by_host(from);
-
- /* Split up from */
- strncpy0(work, from, sizeof(work));
- p = strchr(work, '!');
- *p++ = 0;
- nfrom = work;
- hfrom = p;
- /* Now we got modes[], chan, ufrom, nfrom, hfrom, and count of each relevant mode */
- // check for mdop
- if ((chan) && (deops >= 3)) {
- if ((!ufrom) || (!(ufrom->flags & USER_BOT))) {
- if (ROLE_KICK_MDOP) {
- m=ismember(chan, nfrom);
- if (!m || !chan_sentkick(m)) {
-
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, nfrom, kickprefix, kickreason(KICK_MASSDEOP));
- tputs(serv, tmp, strlen(tmp));
- if (m)
- m->flags |= SENTKICK;
- }
- }
- }
- }
- //check for mop
- if (chan && (ops >= 3)) {
- if (channel_nomop(chan)) {
- if ((!ufrom) || (!(ufrom->flags & USER_BOT))) {
- if (ROLE_KICK_MDOP) {
- m=ismember(chan, nfrom);
- if (!m || !chan_sentkick(m)) {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, nfrom, kickprefix, kickreason(KICK_MANUALOP));
- tputs(serv, tmp, strlen(tmp));
- if (m)
- m->flags |= SENTKICK;
- }
- }
- }
- }
- }
- if (chan && ops && (ufrom) && (ufrom->flags & USER_BOT)
- && (!channel_fastop(chan))
- && (!channel_take(chan))
- ) {
- int isbadop = 0;
- if ((modecnt != 2) || (strncmp(modes[0], "+o", 2))
- || (strncmp(modes[1], "-b", 2)))
- isbadop = 1;
- else {
- char enccookie[25],
- plaincookie[25],
- key[NICKLEN + 20],
- goodcookie[25];
- /* -b *!*@[...] */
- strncpy0(enccookie, (char *) &(modes[1][8]), sizeof(enccookie));
- p = enccookie + strlen(enccookie) - 1;
- // *p = 0;
- // while (p - enccookie < 24) {
- // *p++ = '.';
- // *p = 0;
- // }
- strcpy(key, nfrom);
- strcat(key, netpass);
- //putlog(LOG_DEBUG, "*", "Decrypting cookie: %s with key %s", enccookie, key);
- p = decrypt_string(key, enccookie);
- //putlog(LOG_DEBUG, "*", "Decrypted cookie: %s", p);
- Context;
- strncpy0(plaincookie, p, sizeof(plaincookie));
- nfree(p);
- /*
- last 6 digits of time
- last 5 chars of nick
- last 5 regular chars of chan
- */
- makeplaincookie(chan->dname, (char *) (modes[0] + 3), goodcookie);
- // putlog(LOG_DEBUG, "*", "cookie from %s: %s should be: %s", nfrom, plaincookie, goodcookie);
- if (strncmp((char *) &plaincookie[6], (char *) &goodcookie[6], 5))
- isbadop = 2;
- else if (strncmp((char *) &plaincookie[11], (char *) &goodcookie[11], 5))
- isbadop = 3;
- else {
- char tmp[20];
- long optime;
- int off;
- sprintf(tmp, STR("%010li"), (now + timesync));
- strncpy0((char *) &tmp[4], plaincookie, 7);
- optime = atol(tmp);
- off = (now + timesync - optime);
- if (abs(off) > op_time_slack) {
- // isbadop = 4;
- putlog(LOG_ERRORS, "*", "%s opped with bad ts (not punishing.): %li was off by %li", nfrom, optime, off);
- }
- }
- }
- if (isbadop) {
- char trg[NICKLEN + 1] = "";
- int n,
- i;
- memberlist *m;
- Context;
- switch (role) {
- case 0:
- break;
- case 1:
- /* Kick opper */
- m=ismember(chan, nfrom);
- if (!m || !chan_sentkick(m)) {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, nfrom, kickprefix, kickreason(KICK_BADOP));
- tputs(serv, tmp, strlen(tmp));
- if (m)
- m->flags |= SENTKICK;
- }
- sprintf(tmp, STR("%s MODE %s"), from, msg);
- deflag_user(ufrom, DEFLAG_BADCOOKIE, tmp, chan);
- break;
- default:
- n = role - 1;
- i = 0;
- while ((i < 5) && (n > 0)) {
- if (modes[i] && !strncmp(modes[i], "+o", 2))
- n--;
- if (n)
- i++;
- }
- if (!n) {
- strcpy(trg, (char *) &modes[i][3]);
- m = ismember(chan, trg);
- if (m) {
- if (!(m->flags & CHANOP)) {
- if (!chan_sentkick(m)) {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, trg, kickprefix, kickreason(KICK_BADOPPED));
- tputs(serv, tmp, strlen(tmp));
- m->flags |= SENTKICK;
- }
- }
- }
- }
- }
- if (isbadop == 1)
- putlog(LOG_WARN, "*", STR("Missing cookie: %s MODE %s"), from, msg);
- else if (isbadop == 2)
- putlog(LOG_WARN, "*", STR("Invalid cookie (bad nick): %s MODE %s"), from, msg);
- else if (isbadop == 3)
- putlog(LOG_WARN, "*", STR("Invalid cookie (bad chan): %s MODE %s"), from, msg);
- else if (isbadop == 4)
- putlog(LOG_WARN, "*", STR("Invalid cookie (bad time): %s MODE %s"), from, msg);
- } else
- putlog(LOG_DEBUG, "@", STR("Good op: %s"), msg);
- }
- if ((ops) && chan && !channel_manop(chan) && (ufrom)
- && !(ufrom->flags & USER_BOT)) {
- char trg[NICKLEN + 1] = "";
- int n,
- i;
- memberlist *m;
- switch (role) {
- case 0:
- break;
- case 1:
- /* Kick opper */
- m = ismember(chan, nfrom);
- if (!m || !chan_sentkick(m)) {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, nfrom, kickprefix, kickreason(KICK_MANUALOP));
- tputs(serv, tmp, strlen(tmp));
- if (m)
- m->flags |= SENTKICK;
- }
- sprintf(tmp, STR("%s MODE %s"), from, msg);
- deflag_user(ufrom, DEFLAG_MANUALOP, tmp, chan);
- break;
- default:
- n = role - 1;
- i = 0;
- while ((i < 5) && (n > 0)) {
- if (modes[i] && !strncmp(modes[i], "+o", 2))
- n--;
- if (n)
- i++;
- }
- if (!n) {
- strcpy(trg, (char *) &modes[i][3]);
- m = ismember(chan, trg);
- if (m) {
- if (!(m->flags & CHANOP) && (rfc_casecmp(botname, trg))) {
- if (!chan_sentkick(m)) {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, trg, kickprefix, kickreason(KICK_MANUALOPPED));
- tputs(serv, tmp, strlen(tmp));
- m->flags |= SENTKICK;
- }
- }
- } else {
- sprintf(tmp, STR("KICK %s %s :%s%s\n"), chan->name, trg, kickprefix, kickreason(KICK_MANUALOPPED));
- tputs(serv, tmp, strlen(tmp));
- }
- }
- }
- }
- for (i = 0; i < 5; i++)
- if (modes[i])
- nfree(modes[i]);
- }
- 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_tcl_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},
- {"251", "", (Function) got251, 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;
- int servidx;
- 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) {
- // putlog(LOG_SERV, "*", "No servers in server list");
- // cycle_time = 10;
- 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;
- }
- if (connectserver[0]) /* drummer */
- do_tcl("connect-server", connectserver);
- check_tcl_event("connect-server");
- 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 = get_data_ptr(strlen(dcc[servidx].host) + 1);
- strcpy(dcc[servidx].u.dns->host, dcc[servidx].host);
- dcc[servidx].u.dns->cbuf = get_data_ptr(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;
- 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);
- 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\n", pass);
- dprintf(DP_MODE, "NICK %s\n", botname);
- dprintf(DP_MODE, "USER %s . . :%s\n", botuser, botrealname);
- /* Wait for async result now */
- }
- }
- #endif
|