| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706 |
- /*
- * botnet.c -- handles:
- * keeping track of which bot's connected where in the chain
- * dumping a list of bots or a bot tree to a user
- * channel name associations on the party line
- * rejecting a bot
- * linking, unlinking, and relaying to another bot
- * pinging the bots periodically and checking leaf status
- *
- */
- #include "common.h"
- #include "botnet.h"
- #include "color.h"
- #include "chanprog.h"
- #include "net.h"
- #include "users.h"
- #include "misc.h"
- #include "userrec.h"
- #include "main.h"
- #include "dccutil.h"
- #include "dcc.h"
- #include "botmsg.h"
- #include "tandem.h"
- #include "core_binds.h"
- #ifdef HUB
- # ifdef S_AUTOLOCK
- # include "src/mod/channels.mod/channels.h"
- # endif /* S_AUTOLOCK */
- #endif /* HUB */
- tand_t *tandbot = NULL; /* Keep track of tandem bots on the
- botnet */
- party_t *party = NULL; /* Keep track of people on the botnet */
- int tands = 0; /* Number of bots on the botnet */
- static int maxparty = 50; /* Maximum space for party line members
- currently */
- static int parties = 0; /* Number of people on the botnet */
- static int share_unlinks = 1; /* Allow remote unlinks of my
- sharebots? */
- void init_party()
- {
- party = (party_t *) calloc(1, maxparty * sizeof(party_t));
- }
- tand_t *findbot(char *who)
- {
- tand_t *ptr = NULL;
- for (ptr = tandbot; ptr; ptr = ptr->next)
- if (!egg_strcasecmp(ptr->bot, who))
- return ptr;
- return NULL;
- }
- /* Add a tandem bot to our chain list
- */
- void addbot(char *who, char *from, char *next, char flag, int vlocalhub, time_t vbuildts, char *vversion)
- {
- tand_t **ptr = &tandbot, *ptr2 = NULL;
- while (*ptr) {
- if (!egg_strcasecmp((*ptr)->bot, who))
- putlog(LOG_BOTS, "*", "!!! Duplicate botnet bot entry!!");
- ptr = &((*ptr)->next);
- }
- ptr2 = calloc(1, sizeof(tand_t));
- strncpy(ptr2->bot, who, HANDLEN);
- ptr2->bot[HANDLEN] = 0;
- ptr2->share = flag;
- ptr2->localhub = vlocalhub;
- ptr2->buildts = vbuildts;
- if (vversion && vversion[0])
- strncpyz(ptr2->version, vversion, 121);
- ptr2->next = *ptr;
- *ptr = ptr2;
- /* May be via itself */
- ptr2->via = findbot(from);
- if (!egg_strcasecmp(next, conf.bot->nick))
- ptr2->uplink = (tand_t *) 1;
- else
- ptr2->uplink = findbot(next);
- tands++;
- }
- #ifdef HUB
- #ifdef G_BACKUP
- void check_should_backup()
- {
- struct chanset_t *chan = NULL;
- for (chan = chanset; chan; chan = chan->next) {
- if (chan->channel.backup_time && (chan->channel.backup_time < now) && !channel_backup(chan)) {
- do_chanset(NULL, chan, STR("+backup"), DO_LOCAL | DO_NET);
- chan->channel.backup_time = 0;
- }
- }
- }
- #endif /* G_BACKUP */
- #endif /* HUB */
- void updatebot(int idx, char *who, char share, int vlocalhub, time_t vbuildts, char *vversion)
- {
- tand_t *ptr = findbot(who);
- if (ptr) {
- if (share)
- ptr->share = share;
- if (vlocalhub)
- ptr->localhub = vlocalhub;
- if (vbuildts)
- ptr->buildts = vbuildts;
- if (vversion && vversion[0])
- strncpyz(ptr->version, vversion, 121);
- botnet_send_update(idx, ptr);
- }
- }
- /* New botnet member
- */
- int addparty(char *bot, char *nick, int chan, char flag, int sock,
- char *from, int *idx)
- {
- int i;
- for (i = 0; i < parties; i++) {
- /* Just changing the channel of someone already on? */
- if (!egg_strcasecmp(party[i].bot, bot) &&
- (party[i].sock == sock)) {
- int oldchan = party[i].chan;
- party[i].chan = chan;
- party[i].timer = now;
- if (from[0]) {
- if (flag == ' ')
- flag = '-';
- party[i].flag = flag;
- if (party[i].from)
- free(party[i].from);
- party[i].from = strdup(from);
- }
- *idx = i;
- return oldchan;
- }
- }
- /* New member */
- if (parties == maxparty) {
- maxparty += 50;
- party = (party_t *) realloc((void *) party, maxparty * sizeof(party_t));
- }
- strncpy(party[parties].nick, nick, HANDLEN);
- party[parties].nick[HANDLEN] = 0;
- strncpy(party[parties].bot, bot, HANDLEN);
- party[parties].bot[HANDLEN] = 0;
- party[parties].chan = chan;
- party[parties].sock = sock;
- party[parties].status = 0;
- party[parties].away = 0;
- party[parties].timer = now; /* cope. */
- if (from[0]) {
- if (flag == ' ')
- flag = '-';
- party[parties].flag = flag;
- party[parties].from = strdup(from);
- } else {
- party[parties].flag = ' ';
- party[parties].from = strdup("(unknown)");
- }
- *idx = parties;
- parties++;
- return -1;
- }
- /* Alter status flags for remote party-line user.
- */
- void partystat(char *bot, int sock, int add, int rem)
- {
- int i;
- for (i = 0; i < parties; i++) {
- if ((!egg_strcasecmp(party[i].bot, bot)) &&
- (party[i].sock == sock)) {
- party[i].status |= add;
- party[i].status &= ~rem;
- }
- }
- }
- /* Other bot is sharing idle info.
- */
- void partysetidle(char *bot, int sock, int secs)
- {
- int i;
- for (i = 0; i < parties; i++) {
- if ((!egg_strcasecmp(party[i].bot, bot)) &&
- (party[i].sock == sock)) {
- party[i].timer = (now - (time_t) secs);
- }
- }
- }
- /* Return someone's chat channel.
- */
- int getparty(char *bot, int sock)
- {
- int i;
- for (i = 0; i < parties; i++) {
- if (!egg_strcasecmp(party[i].bot, bot) &&
- (party[i].sock == sock)) {
- return i;
- }
- }
- return -1;
- }
- /* Un-idle someone
- */
- int partyidle(char *bot, char *nick)
- {
- int i, ok = 0;
- for (i = 0; i < parties; i++) {
- if ((!egg_strcasecmp(party[i].bot, bot)) &&
- (!egg_strcasecmp(party[i].nick, nick))) {
- party[i].timer = now;
- ok = 1;
- }
- }
- return ok;
- }
- /* Change someone's nick
- */
- int partynick(char *bot, int sock, char *nick)
- {
- char work[HANDLEN + 1] = "";
- int i;
- for (i = 0; i < parties; i++) {
- if (!egg_strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
- strcpy(work, party[i].nick);
- strncpy(party[i].nick, nick, HANDLEN);
- party[i].nick[HANDLEN] = 0;
- strcpy(nick, work);
- return i;
- }
- }
- return -1;
- }
- /* Set away message
- */
- void partyaway(char *bot, int sock, char *msg)
- {
- int i;
- for (i = 0; i < parties; i++) {
- if ((!egg_strcasecmp(party[i].bot, bot)) &&
- (party[i].sock == sock)) {
- if (party[i].away)
- free(party[i].away);
- if (msg[0]) {
- party[i].away = strdup(msg);
- } else
- party[i].away = 0;
- }
- }
- }
- /* Remove a tandem bot from the chain list
- */
- void rembot(const char *who)
- {
- tand_t **ptr = &tandbot, *ptr2 = NULL;
- struct userrec *u = NULL;
- while (*ptr) {
- if (!egg_strcasecmp((*ptr)->bot, who))
- break;
- ptr = &((*ptr)->next);
- }
- if (!*ptr)
- /* May have just .unlink *'d */
- return;
- u = get_user_by_handle(userlist, (char *) who);
- if (u != NULL)
- touch_laston(u, "unlinked", now);
- ptr2 = *ptr;
- *ptr = ptr2->next;
- free(ptr2);
- tands--;
- dupwait_notify(who);
- }
- void remparty(char *bot, int sock)
- {
- int i;
- for (i = 0; i < parties; i++)
- if ((!egg_strcasecmp(party[i].bot, bot)) &&
- (party[i].sock == sock)) {
- parties--;
- if (party[i].from)
- free(party[i].from);
- if (party[i].away)
- free(party[i].away);
- if (i < parties) {
- strcpy(party[i].bot, party[parties].bot);
- strcpy(party[i].nick, party[parties].nick);
- party[i].chan = party[parties].chan;
- party[i].sock = party[parties].sock;
- party[i].flag = party[parties].flag;
- party[i].status = party[parties].status;
- party[i].timer = party[parties].timer;
- party[i].from = party[parties].from;
- party[i].away = party[parties].away;
- }
- }
- }
- /* Cancel every user that was on a certain bot
- */
- void rempartybot(char *bot)
- {
- int i;
- for (i = 0; i < parties; i++)
- if (!egg_strcasecmp(party[i].bot, bot)) {
- remparty(bot, party[i].sock);
- i--;
- }
- }
- /* Remove every bot linked 'via' bot <x>
- */
- void unvia(int idx, tand_t *who)
- {
- tand_t *bot = NULL, *bot2 = NULL;
- if (!who)
- return; /* Safety */
- rempartybot(who->bot);
- bot = tandbot;
- while (bot) {
- if (bot->uplink == who) {
- unvia(idx, bot);
- bot2 = bot->next;
- rembot(bot->bot);
- bot = bot2;
- } else
- bot = bot->next;
- }
- }
- void besthub(char *hub)
- {
- tand_t *ptr = tandbot;
- struct userrec *u = NULL, *besthubu = NULL;
- char bestlval[20] = "", lval[20] = "";
- hub[0] = 0;
- strcpy(bestlval, "z");
- while (ptr) {
- u = get_user_by_handle(userlist, ptr->bot);
- if (u) {
- link_pref_val(u, lval);
- if (strcmp(lval, bestlval) < 0) {
- strcpy(bestlval, lval);
- besthubu = u;
- }
- }
- ptr = ptr->next;
- }
- if (besthubu)
- strcpy(hub, besthubu->handle);
- return;
- }
- /* Return index into dcc list of the bot that connects us to bot <x>
- */
- int nextbot(char *who)
- {
- int j;
- tand_t *bot = findbot(who);
- if (!bot)
- return -1;
- for (j = 0; j < dcc_total; j++)
- if (bot->via && !egg_strcasecmp(bot->via->bot, dcc[j].nick) &&
- (dcc[j].type == &DCC_BOT))
- return j;
- return -1; /* We're not connected to 'via' */
- }
- /* Return name of the bot that is directly connected to bot X
- */
- char *lastbot(char *who)
- {
- tand_t *bot = findbot(who);
- if (!bot)
- return "*";
- else if (bot->uplink == (tand_t *) 1)
- return conf.bot->nick;
- else
- return bot->uplink->bot;
- }
- /* Modern version of 'whom' (use local data)
- */
- void answer_local_whom(int idx, int chan)
- {
- char format[81] = "", c = 0, idle[40] = "";
- int i, t, nicklen, botnicklen, total = 0;
- if (chan == (-1))
- dprintf(idx, "%s (+: %s, *: %s)\n", BOT_BOTNETUSERS, BOT_PARTYLINE,
- BOT_LOCALCHAN);
- else if (chan > 0) {
- dprintf(idx, "%s %s%d:\n", BOT_USERSONCHAN,
- (chan < GLOBAL_CHANS) ? "" : "*", chan % GLOBAL_CHANS);
- }
- /* Find longest nick and botnick */
- nicklen = botnicklen = 0;
- for (i = 0; i < dcc_total; i++)
- if (dcc[i].type == &DCC_CHAT) {
- if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
- t = strlen(dcc[i].nick); if(t > nicklen) nicklen = t;
- t = strlen(conf.bot->nick); if(t > botnicklen) botnicklen = t;
- }
- }
- for (i = 0; i < parties; i++) {
- if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
- t = strlen(party[i].nick); if(t > nicklen) nicklen = t;
- t = strlen(party[i].bot); if(t > botnicklen) botnicklen = t;
- }
- }
- if(nicklen < 9) nicklen = 9;
- if(botnicklen < 9) botnicklen = 9;
- #ifdef HUB
- egg_snprintf(format, sizeof format, "%%-%us %%-%us %%s\n",
- nicklen, botnicklen);
- dprintf(idx, format, " Nick", " Bot", " Host");
- dprintf(idx, format, "----------", "---------", "--------------------");
- egg_snprintf(format, sizeof format, "%%c%%-%us %%c %%-%us %%s%%s\n",
- nicklen, botnicklen);
- #else /* !HUB */
- egg_snprintf(format, sizeof format, "%%-%us\n", nicklen);
- dprintf(idx, format, " Nick");
- dprintf(idx, format, "----------");
- egg_snprintf(format, sizeof format, "%%c%%-%us %%c %%s\n", nicklen);
- #endif /* HUB */
- for (i = 0; i < dcc_total; i++)
- if (dcc[i].type == &DCC_CHAT) {
- if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
- c = geticon(i);
- if (c == '-')
- c = ' ';
- if (now - dcc[i].timeval > 300) {
- unsigned long mydays, hrs, mins;
- mydays = (now - dcc[i].timeval) / 86400;
- hrs = ((now - dcc[i].timeval) - (mydays * 86400)) / 3600;
- mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
- if (mydays > 0)
- sprintf(idle, " [idle %lud%luh]", mydays, hrs);
- else if (hrs > 0)
- sprintf(idle, " [idle %luh%lum]", hrs, mins);
- else
- sprintf(idle, " [idle %lum]", mins);
- } else
- idle[0] = 0;
- total++;
- dprintf(idx, format, c, dcc[i].nick,
- (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
- (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
- #ifdef HUB
- (chan == (-1)) ? '*' : ' ', conf.bot->nick, dcc[i].host, idle);
- #else /* !HUB */
- (chan == (-1)) ? '*' : ' ', idle);
- #endif /* HUB */
- if (dcc[i].u.chat->away != NULL)
- dprintf(idx, " AWAY: %s\n", dcc[i].u.chat->away);
- }
- }
- for (i = 0; i < parties; i++) {
- if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
- c = party[i].flag;
- if (c == '-')
- c = ' ';
- if (party[i].timer == 0L)
- strcpy(idle, " [idle?]");
- else if (now - party[i].timer > 300) {
- unsigned long mydays, hrs, mins;
- mydays = (now - party[i].timer) / 86400;
- hrs = ((now - party[i].timer) - (mydays * 86400)) / 3600;
- mins = ((now - party[i].timer) - (hrs * 3600)) / 60;
- if (mydays > 0)
- sprintf(idle, " [idle %lud%luh]", mydays, hrs);
- else if (hrs > 0)
- sprintf(idle, " [idle %luh%lum]", hrs, mins);
- else
- sprintf(idle, " [idle %lum]", mins);
- } else
- idle[0] = 0;
- total++;
- dprintf(idx, format, c, party[i].nick,
- (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ',
- #ifdef HUB
- party[i].bot, party[i].from, idle);
- #else /* !HUB */
- idle);
- #endif /* HUB */
- if (party[i].status & PLSTAT_AWAY)
- dprintf(idx, " %s: %s\n", MISC_AWAY,
- party[i].away ? party[i].away : "");
- }
- }
- dprintf(idx, "Total users: %d\n", total);
- }
- #ifdef HUB
- /* Show z a list of all bots connected
- */
- void tell_bots(int idx)
- {
- char format[81] = "";
- int upi = 1, downi = 0;
- size_t len;
- struct userrec *u = NULL;
- len = strlen(conf.bot->nick);
- //up = calloc(1, ulen + 1);
- //strcpy(up, conf.bot->nick);
- //down = calloc(1, 1);
- egg_snprintf(format, sizeof format, "%%s%%-11s%%s %%-5s %%-10s %%-11s %%s\n");
- dprintf(idx, format, "", "Bot", "", "OS", "Username", "Shell");
- dprintf(idx, format, "", "-----------", "", "----------", "-----------", "-----------------------");
-
- /* FIXME: NEED ME! */
- for (u = userlist; u; u = u->next) {
- if (u->bot && (egg_strcasecmp(u->handle, conf.bot->nick))) {
- size_t hlen = strlen(u->handle);
- int up = 0;
- if (findbot(u->handle)) {
- upi++;
- up = 1;
- } else
- downi++;
- dprintf(idx, format, up ? GREEN(idx) : RED(idx), u->handle, COLOR_END(idx),
- get_user(&USERENTRY_OS, u), get_user(&USERENTRY_USERNAME, u),
- get_user(&USERENTRY_NODENAME, u));
- // if (up) {
- }
- }
- /*
- if (!downi)
- dprintf(idx, "Bots up (%d) [%sall%s]: %s\n", upi, BOLD(idx), BOLD_END(idx), up);
- else
- dprintf(idx, "Bots up (%d): %s\n", upi, up);
-
- if (downi)
- dprintf(idx, "Bots down (%d): %s\n", downi, down);
- dprintf(idx, "Total: %d [%s%d%% up%s, %s%d%% down%s]\n", upi + downi, GREEN(idx), (100 * upi / (upi + downi)),
- COLOR_END(idx), RED(idx), (100 * downi / (upi + downi)), COLOR_END(idx));
- free(up);
- free(down);
- */
- }
- /* Show a simpleton bot tree
- */
- void tell_bottree(int idx)
- {
- char s[161] = "", work[1024] = "";
- tand_t *last[20] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
- tand_t *this = NULL, *bot = NULL, *bot2 = NULL;
- int lev = 0, more = 1, mark[20], ok, cnt, i = 0, imark, tothops = 0;
- if (tands == 0) {
- dprintf(idx, "%s\n", BOT_NOBOTSLINKED);
- return;
- }
- for (bot = tandbot; bot; bot = bot->next)
- if (!bot->uplink) {
- if (i) {
- s[i++] = ',';
- s[i++] = ' ';
- }
- strcpy(s + i, bot->bot);
- i += strlen(bot->bot);
- }
- if (s[0])
- dprintf(idx, "(%s %s)\n", BOT_NOTRACEINFO, s);
- dprintf(idx, "%s%s%s (%s %li)\n", localhub ? RED(idx) : "",
- conf.bot->nick,
- localhub ? COLOR_END(idx) : "",
- egg_version, buildts);
- this = (tand_t *) 1;
- work[0] = 0;
- while (more) {
- if (lev == 20) {
- dprintf(idx, "\n%s\n", BOT_COMPLEXTREE);
- return;
- }
- cnt = 0;
- tothops += lev;
- for (bot = tandbot; bot; bot = bot->next)
- if (bot->uplink == this)
- cnt++;
- if (cnt) {
- imark = 0;
- for (i = 0; i < lev; i++) {
- if (mark[i])
- strcpy(work + imark, " | ");
- else
- strcpy(work + imark, " ");
- imark += 5;
- }
- if (cnt > 1)
- strcpy(work + imark, " |-");
- else
- strcpy(work + imark, " `-");
- s[0] = 0;
- bot = tandbot;
- while (!s[0]) {
- if (bot->uplink == this) {
- if (bot->share)
- i = sprintf(s, "%c", bot->share);
- else
- i = sprintf(s, "-");
- i = sprintf(s + 1, "%s%s%s (%s %li)", bot->localhub ? RED(idx) : "",
- bot->bot,
- bot->localhub ? COLOR_END(idx) : "",
- bot->version, bot->buildts);
- } else
- bot = bot->next;
- }
- dprintf(idx, "%s%s\n", work, s);
- if (cnt > 1)
- mark[lev] = 1;
- else
- mark[lev] = 0;
- work[0] = 0;
- last[lev] = this;
- this = bot;
- lev++;
- more = 1;
- } else {
- while (cnt == 0) {
- /* No subtrees from here */
- if (lev == 0) {
- dprintf(idx, "(( tree error ))\n");
- return;
- }
- ok = 0;
- for (bot = tandbot; bot; bot = bot->next) {
- if (bot->uplink == last[lev - 1]) {
- if (this == bot)
- ok = 1;
- else if (ok) {
- cnt++;
- if (cnt == 1) {
- bot2 = bot;
- if (bot->share)
- i = sprintf(s, "%c", bot->share);
- else
- i = sprintf(s, "-");
- i = sprintf(s + 1, "%s%s%s (%s %li)", bot->localhub ? RED(idx) : "",
- bot->bot,
- bot->localhub ? COLOR_END(idx) : "",
- bot->version, bot->buildts);
- }
- }
- }
- }
- if (cnt) {
- imark = 0;
- for (i = 1; i < lev; i++) {
- if (mark[i - 1])
- strcpy(work + imark, " | ");
- else
- strcpy(work + imark, " ");
- imark += 5;
- }
- more = 1;
- if (cnt > 1)
- dprintf(idx, "%s |-%s\n", work, s);
- else
- dprintf(idx, "%s `-%s\n", work, s);
- this = bot2;
- work[0] = 0;
- if (cnt > 1)
- mark[lev - 1] = 1;
- else
- mark[lev - 1] = 0;
- } else {
- /* This was the last child */
- lev--;
- if (lev == 0) {
- more = 0;
- cnt = 999;
- } else {
- more = 1;
- this = last[lev];
- }
- }
- }
- }
- }
- /* Hop information: (9d) */
- dprintf(idx, "Average hops: %3.1f, total bots: %d\n", ((float) tothops) / ((float) tands), tands + 1);
- }
- #endif /* HUB */
- /* Dump list of links to a new bot
- */
- void dump_links(int z)
- {
- register int i;
- register size_t l;
- char x[1024] = "";
- #ifdef HUB
- tand_t *bot = NULL;
- for (bot = tandbot; bot; bot = bot->next) {
- char *p = NULL;
- if (bot->uplink == (tand_t *) 1)
- p = conf.bot->nick;
- else
- p = bot->uplink->bot;
- l = simple_sprintf(x, "n %s %s %cD0gc %d %d %s\n", bot->bot, p, bot->share, bot->localhub,
- bot->buildts, bot->version ? bot->version : "");
- tputs(dcc[z].sock, x, l);
- }
- #endif /* HUB */
- /* Dump party line members */
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type == &DCC_CHAT) {
- if ((dcc[i].u.chat->channel >= 0) && (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
- l = simple_sprintf(x, "j !%s %s %D %c%D %s\n", conf.bot->nick, dcc[i].nick, dcc[i].u.chat->channel, geticon(i), dcc[i].sock, dcc[i].host);
- tputs(dcc[z].sock, x, l);
- l = simple_sprintf(x, "i %s %D %D %s\n", conf.bot->nick, dcc[i].sock, now - dcc[i].timeval, dcc[i].u.chat->away ? dcc[i].u.chat->away : "");
- tputs(dcc[z].sock, x, l);
- }
- }
- }
- for (i = 0; i < parties; i++) {
- l = simple_sprintf(x, "j %s %s %D %c%D %s\n", party[i].bot, party[i].nick, party[i].chan, party[i].flag, party[i].sock, party[i].from);
- tputs(dcc[z].sock, x, l);
- if ((party[i].status & PLSTAT_AWAY) || (party[i].timer != 0)) {
- l = simple_sprintf(x, "i %s %D %D %s\n", party[i].bot, party[i].sock, now - party[i].timer, party[i].away ? party[i].away : "");
- tputs(dcc[z].sock, x, l);
- }
- }
- }
- int in_chain(char *who)
- {
- if (findbot(who))
- return 1;
- if (!egg_strcasecmp(who, conf.bot->nick))
- return 1;
- return 0;
- }
- int bots_in_subtree(tand_t *bot)
- {
- int nr = 1;
- tand_t *b = NULL;
- if (!bot)
- return 0;
- for (b = tandbot; b; b = b->next) {
- if (b->bot && (b->uplink == bot)) {
- nr += bots_in_subtree(b);
- }
- }
- return nr;
- }
- int users_in_subtree(tand_t *bot)
- {
- int i, nr;
- tand_t *b = NULL;
- nr = 0;
- if (!bot)
- return 0;
- for (i = 0; i < parties; i++)
- if (!egg_strcasecmp(party[i].bot, bot->bot))
- nr++;
- for (b = tandbot; b; b = b->next)
- if (b->bot && (b->uplink == bot))
- nr += users_in_subtree(b);
- return nr;
- }
- /* Break link with a tandembot
- */
- int botunlink(int idx, char *nick, char *reason)
- {
- register int i;
- int bots, users;
- tand_t *bot = NULL;
- if (nick[0] == '*')
- dprintf(idx, "%s\n", BOT_UNLINKALL);
- for (i = 0; i < dcc_total; i++) {
- if ((nick[0] == '*') || !egg_strcasecmp(dcc[i].nick, nick)) {
- if (dcc[i].type == &DCC_FORK_BOT) {
- if (idx >= 0)
- dprintf(idx, "%s: %s -> %s.\n", BOT_KILLLINKATTEMPT,
- dcc[i].nick, dcc[i].host);
- putlog(LOG_BOTS, "*", "%s: %s -> %s:%d",
- BOT_KILLLINKATTEMPT, dcc[i].nick,
- dcc[i].host, dcc[i].port);
- killsock(dcc[i].sock);
- lostdcc(i);
- if (nick[0] != '*')
- return 1;
- } else if (dcc[i].type == &DCC_BOT_NEW) {
- if (idx >= 0)
- dprintf(idx, "%s %s.\n", BOT_ENDLINKATTEMPT,
- dcc[i].nick);
- putlog(LOG_BOTS, "*", "%s %s @ %s:%d",
- "Stopped trying to link", dcc[i].nick,
- dcc[i].host, dcc[i].port);
- killsock(dcc[i].sock);
- lostdcc(i);
- if (nick[0] != '*')
- return 1;
- } else if (dcc[i].type == &DCC_BOT) {
- char s[1024] = "";
- if (idx >= 0)
- dprintf(idx, "%s %s.\n", BOT_BREAKLINK, dcc[i].nick);
- else if ((idx == -3) && (b_status(i) & STAT_SHARE) && !share_unlinks)
- return -1;
- bot = findbot(dcc[i].nick);
- bots = bots_in_subtree(bot);
- users = users_in_subtree(bot);
- if (reason && reason[0]) {
- simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
- BOT_UNLINKEDFROM, dcc[i].nick, reason, bots,
- (bots != 1) ? "s" : "", users, (users != 1) ?
- "s" : "");
- dprintf(i, "bye %s\n", reason);
- } else {
- simple_sprintf(s, "%s %s (lost %d bot%s and %d user%s)",
- BOT_UNLINKEDFROM, dcc[i].nick, bots, (bots != 1) ?
- "s" : "", users, (users != 1) ? "s" : "");
- dprintf(i, "bye No reason\n");
- }
- chatout("*** %s\n", s);
- botnet_send_unlinked(i, dcc[i].nick, s);
- killsock(dcc[i].sock);
- lostdcc(i);
- if (nick[0] != '*')
- return 1;
- }
- }
- }
- if (idx >= 0 && nick[0] != '*')
- dprintf(idx, "%s\n", BOT_NOTCONNECTED);
- if (nick[0] != '*') {
- bot = findbot(nick);
- if (bot) {
- /* The internal bot list is desynched from the dcc list
- sometimes. While we still search for the bug, provide
- an easy way to clear out those `ghost'-bots.
- Fabian (2000-08-02) */
- char *ghost = "BUG!!: Found bot `%s' in internal bot list, but it\n"
- " shouldn't have been there! Removing.\n"
- " This is a known bug we haven't fixed yet. If this\n"
- " bot is the newest eggdrop version available and you\n"
- " know a *reliable* way to reproduce the bug, please\n"
- " contact us - we need your help!\n";
- if (idx >= 0)
- dprintf(idx, ghost, nick);
- else
- putlog(LOG_MISC, "*", ghost, nick);
- rembot(bot->bot);
- return 1;
- }
- }
- if (nick[0] == '*') {
- dprintf(idx, "%s\n", BOT_WIPEBOTTABLE);
- while (tandbot)
- rembot(tandbot->bot);
- while (parties) {
- parties--;
- /* Assert? */
- }
- }
- return 0;
- }
- static void botlink_resolve_success(int);
- static void botlink_resolve_failure(int);
- /* Link to another bot
- */
- int botlink(char *linker, int idx, char *nick)
- {
- struct bot_addr *bi = NULL;
- struct userrec *u = NULL;
- register int i;
- u = get_user_by_handle(userlist, nick);
- if (!u || !u->bot) {
- if (idx >= 0)
- dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
- } else if (!egg_strcasecmp(nick, conf.bot->nick)) {
- if (idx >= 0)
- dprintf(idx, "%s\n", BOT_CANTLINKMYSELF);
- } else if (in_chain(nick) && (idx != -3)) {
- if (idx >= 0)
- dprintf(idx, "%s\n", BOT_ALREADYLINKED);
- } else {
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].user == u) &&
- ((dcc[i].type == &DCC_FORK_BOT) ||
- (dcc[i].type == &DCC_BOT_NEW))) {
- if (idx >= 0)
- dprintf(idx, "%s\n", BOT_ALREADYLINKING);
- return 0;
- }
- /* Address to connect to is in 'info' */
- bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
- if (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0)) {
- if (idx >= 0) {
- dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
- dprintf(idx, "%s .chaddr %s %s\n",
- MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
- }
- } else if (dcc_total == max_dcc) {
- if (idx >= 0)
- dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
- } else {
- correct_handle(nick);
- if (idx > -2)
- putlog(LOG_BOTS, "*", "%s %s at %s:%d ...", BOT_LINKING, nick,
- bi->address, bi->telnet_port);
- i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
- dcc[i].timeval = now;
- dcc[i].port = bi->telnet_port;
- dcc[i].user = u;
- strcpy(dcc[i].nick, nick);
- strcpy(dcc[i].host, bi->address);
- dcc[i].u.dns->ibuf = idx;
- dcc[i].u.dns->cptr = calloc(1, strlen(linker) + 1);
- strcpy(dcc[i].u.dns->cptr, linker);
- dcc[i].u.dns->host = calloc(1, strlen(dcc[i].host) + 1);
- strcpy(dcc[i].u.dns->host, dcc[i].host);
- dcc[i].u.dns->dns_success = botlink_resolve_success;
- dcc[i].u.dns->dns_failure = botlink_resolve_failure;
- dcc[i].u.dns->dns_type = RES_IPBYHOST;
- dcc[i].u.dns->type = &DCC_FORK_BOT;
- #ifdef USE_IPV6
- botlink_resolve_success(i);
- #else
- dcc_dnsipbyhost(bi->address);
- #endif /* USE_IPV6 */
- return 1;
- }
- }
- return 0;
- }
- static void botlink_resolve_failure(int i)
- {
- free(dcc[i].u.dns->cptr);
- lostdcc(i);
- }
- static void botlink_resolve_success(int i)
- {
- int idx = dcc[i].u.dns->ibuf;
- char *linker = dcc[i].u.dns->cptr;
- dcc[i].addr = dcc[i].u.dns->ip;
- changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
- dcc[i].timeval = now;
- strcpy(dcc[i].u.bot->linker, linker);
- strcpy(dcc[i].u.bot->version, "(primitive bot)");
- strcpy(dcc[i].u.bot->sysname, "*");
- dcc[i].u.bot->numver = idx;
- dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
- #ifdef USE_IPV6
- dcc[i].sock = getsock(SOCK_STRONGCONN, hostprotocol(dcc[i].host));
- #else
- dcc[i].sock = getsock(SOCK_STRONGCONN);
- #endif /* USE_IPV6 */
- free(linker);
- if (dcc[i].sock > 0)
- identd_open(); /* will be closed when an ident is replied. */
- if (dcc[i].sock < 0 ||
- #ifdef USE_IPV6
- open_telnet_raw(dcc[i].sock, dcc[i].host,
- #else
- open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
- #endif /* USE_IPV6 */
- dcc[i].port) < 0)
- failed_link(i);
- else { /* let's attempt to initiate SSL before ANYTHING else... */
- #ifdef HAVE_SSL
- /* if (!ssl_link(dcc[i].sock, CONNECT_SSL)) {
- debug2("back from ssl_link(%d, %d) for botlink (failed)", dcc[i].sock, CONNECT_SSL);
- dcc[i].ssl = 0;
- putlog(LOG_BOTS, "*", "SSL link for '%s' failed", dcc[i].nick);
- } else */
- dcc[i].ssl = 1;
- #else
- dcc[i].ssl = 0;
- #endif /* HAVE_SSL */
- }
- }
- static void failed_tandem_relay(int idx)
- {
- int uidx = (-1), i;
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_PRE_RELAY) &&
- (dcc[i].u.relay->sock == dcc[idx].sock))
- uidx = i;
- if (uidx < 0) {
- putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
- dcc[idx].sock, dcc[idx].u.relay->sock);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- if (dcc[idx].port >= dcc[idx].u.relay->port + 3) {
- struct chat_info *ci = dcc[uidx].u.relay->chat;
- dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
- dcc[uidx].status = dcc[uidx].u.relay->old_status;
- free(dcc[uidx].u.relay);
- dcc[uidx].u.chat = ci;
- dcc[uidx].type = &DCC_CHAT;
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- killsock(dcc[idx].sock);
- #ifdef USE_IPV6
- dcc[idx].sock = getsock(SOCK_STRONGCONN, hostprotocol(dcc[idx].host));
- #else
- dcc[idx].sock = getsock(SOCK_STRONGCONN);
- #endif /* USE_IPV6 */
- dcc[uidx].u.relay->sock = dcc[idx].sock;
- dcc[idx].port++;
- dcc[idx].timeval = now;
- if (dcc[idx].sock < 0 ||
- #ifdef USE_IPV6
- open_telnet_raw(dcc[idx].sock, dcc[idx].host,
- #else
- open_telnet_raw(dcc[idx].sock, dcc[idx].addr ?
- iptostr(htonl(dcc[idx].addr)) :
- dcc[idx].host,
- #endif /* USE_IPV6 */
- dcc[idx].port) < 0)
- failed_tandem_relay(idx);
- }
- static void tandem_relay_resolve_failure(int);
- static void tandem_relay_resolve_success(int);
- /* Relay to another tandembot
- */
- void tandem_relay(int idx, char *nick, register int i)
- {
- struct userrec *u = NULL;
- struct bot_addr *bi = NULL;
- struct chat_info *ci = NULL;
- u = get_user_by_handle(userlist, nick);
- if (!u || !u->bot) {
- dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
- return;
- }
- if (!egg_strcasecmp(nick, conf.bot->nick)) {
- dprintf(idx, "%s\n", BOT_CANTRELAYMYSELF);
- return;
- }
- /* Address to connect to is in 'info' */
- bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
- if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
- dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
- dprintf(idx, "%s .chaddr %s %s\n",
- MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
- return;
- }
- i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
- if (i < 0) {
- dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
- return;
- }
- #ifdef USE_IPV6
- dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, hostprotocol(bi->address));
- #else
- dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
- #endif /* USE_IPV6 */
- if (dcc[i].sock < 0) {
- lostdcc(i);
- dprintf(idx, "%s\n", MISC_NOFREESOCK);
- return;
- }
- dcc[i].port = bi->relay_port;
- dcc[i].addr = 0L;
- strcpy(dcc[i].nick, nick);
- dcc[i].user = u;
- strcpy(dcc[i].host, bi->address);
- #ifdef HUB
- dprintf(idx, "%s %s @ %s:%d ...\n", BOT_CONNECTINGTO, nick, bi->address, bi->relay_port);
- #endif /* HUB */
- dprintf(idx, "%s\n", BOT_BYEINFO1);
- dcc[idx].type = &DCC_PRE_RELAY;
- ci = dcc[idx].u.chat;
- dcc[idx].u.relay = calloc(1, sizeof(struct relay_info));
- dcc[idx].u.relay->chat = ci;
- dcc[idx].u.relay->old_status = dcc[idx].status;
- dcc[idx].u.relay->sock = dcc[i].sock;
- dcc[i].timeval = now;
- dcc[i].u.dns->ibuf = dcc[idx].sock;
- dcc[i].u.dns->host = calloc(1, strlen(bi->address) + 1);
- strcpy(dcc[i].u.dns->host, bi->address);
- dcc[i].u.dns->dns_success = tandem_relay_resolve_success;
- dcc[i].u.dns->dns_failure = tandem_relay_resolve_failure;
- dcc[i].u.dns->dns_type = RES_IPBYHOST;
- dcc[i].u.dns->type = &DCC_FORK_RELAY;
- #ifdef USE_IPV6
- tandem_relay_resolve_success(i);
- #else
- dcc_dnsipbyhost(bi->address);
- #endif /* USE_IPV6 */
- }
- static void tandem_relay_resolve_failure(int idx)
- {
- struct chat_info *ci = NULL;
- register int uidx = (-1), i;
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_PRE_RELAY) &&
- (dcc[i].u.relay->sock == dcc[idx].sock)) {
- uidx = i;
- break;
- }
- if (uidx < 0) {
- putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
- dcc[idx].sock, dcc[idx].u.relay->sock);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- ci = dcc[uidx].u.relay->chat;
- dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
- dcc[uidx].status = dcc[uidx].u.relay->old_status;
- free(dcc[uidx].u.relay);
- dcc[uidx].u.chat = ci;
- dcc[uidx].type = &DCC_CHAT;
- killsock(dcc[idx].sock);
- lostdcc(idx);
- }
- static void tandem_relay_resolve_success(int i)
- {
- int sock = dcc[i].u.dns->ibuf;
- dcc[i].addr = dcc[i].u.dns->ip;
- changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
- dcc[i].u.relay->chat = calloc(1, sizeof(struct chat_info));
- dcc[i].u.relay->sock = sock;
- dcc[i].u.relay->port = dcc[i].port;
- dcc[i].u.relay->chat->away = NULL;
- dcc[i].u.relay->chat->msgs_per_sec = 0;
- dcc[i].u.relay->chat->con_flags = 0;
- dcc[i].u.relay->chat->buffer = NULL;
- dcc[i].u.relay->chat->max_line = 0;
- dcc[i].u.relay->chat->line_count = 0;
- dcc[i].u.relay->chat->current_lines = 0;
- dcc[i].timeval = now;
- #ifdef USE_IPV6
- if (open_telnet_raw(dcc[i].sock, dcc[i].host,
- #else
- if (open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
- #endif /* USE_IPV6 */
- dcc[i].port) < 0)
- failed_tandem_relay(i);
- }
- /* Input from user before connect is ready
- */
- static void pre_relay(int idx, char *buf, register int i)
- {
- register int tidx = (-1);
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_FORK_RELAY) &&
- (dcc[i].u.relay->sock == dcc[idx].sock)) {
- tidx = i;
- break;
- }
- if (tidx < 0) {
- /* Now try to find it among the DNSWAIT sockets instead. */
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_DNSWAIT) &&
- (dcc[i].sock == dcc[idx].u.relay->sock)) {
- tidx = i;
- break;
- }
- }
- if (tidx < 0) {
- putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
- dcc[idx].sock, dcc[idx].u.relay->sock);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- if (!egg_strcasecmp(buf, "*bye*")) {
- /* Disconnect */
- struct chat_info *ci = dcc[idx].u.relay->chat;
- dprintf(idx, "%s %s.\n", BOT_ABORTRELAY1, dcc[tidx].nick);
- dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
- putlog(LOG_MISC, "*", "%s %s -> %s", BOT_ABORTRELAY3, dcc[idx].nick,
- dcc[tidx].nick);
- dcc[idx].status = dcc[idx].u.relay->old_status;
- free(dcc[idx].u.relay);
- dcc[idx].u.chat = ci;
- dcc[idx].type = &DCC_CHAT;
- killsock(dcc[tidx].sock);
- lostdcc(tidx);
- return;
- }
- }
- /* User disconnected before her relay had finished connecting
- */
- static void failed_pre_relay(int idx)
- {
- register int tidx = (-1), i;
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_FORK_RELAY) &&
- (dcc[i].u.relay->sock == dcc[idx].sock)) {
- tidx = i;
- break;
- }
- if (tidx < 0) {
- /* Now try to find it among the DNSWAIT sockets instead. */
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_DNSWAIT) &&
- (dcc[i].sock == dcc[idx].u.relay->sock)) {
- tidx = i;
- break;
- }
- }
- if (tidx < 0) {
- putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
- dcc[idx].sock, dcc[idx].u.relay->sock);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
- dcc[idx].host, dcc[idx].port);
- putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPINGRELAY, dcc[tidx].nick);
- if ((dcc[tidx].sock != STDOUT) || backgrd) {
- if (idx > tidx) {
- int t = tidx;
- tidx = idx;
- idx = t;
- }
- killsock(dcc[tidx].sock);
- lostdcc(tidx);
- } else {
- fatal("Lost my terminal?!", 0);
- }
- killsock(dcc[idx].sock);
- lostdcc(idx);
- }
- static void cont_tandem_relay(int idx, char *buf, register int i)
- {
- register int uidx = (-1);
- struct relay_info *ri = NULL;
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type == &DCC_PRE_RELAY) &&
- (dcc[i].u.relay->sock == dcc[idx].sock))
- uidx = i;
- if (uidx < 0) {
- putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
- dcc[i].sock, dcc[i].u.relay->sock);
- killsock(dcc[i].sock);
- lostdcc(i);
- return;
- }
- dcc[idx].type = &DCC_RELAY;
- dcc[idx].u.relay->sock = dcc[uidx].sock;
- dcc[uidx].u.relay->sock = dcc[idx].sock;
- dprintf(uidx, "%s %s ...\n", BOT_RELAYSUCCESS, dcc[idx].nick);
- dprintf(uidx, "%s\n\n", BOT_BYEINFO2);
- putlog(LOG_MISC, "*", "%s %s -> %s", BOT_RELAYLINK,
- dcc[uidx].nick, dcc[idx].nick);
- ri = dcc[uidx].u.relay; /* YEAH */
- dcc[uidx].type = &DCC_CHAT;
- dcc[uidx].u.chat = ri->chat;
- if (dcc[uidx].u.chat->channel >= 0) {
- chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s %s\n",
- dcc[uidx].nick, BOT_PARTYLEFT);
- if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
- botnet_send_part_idx(uidx, NULL);
- }
- check_bind_chof(dcc[uidx].nick, uidx);
- dcc[uidx].type = &DCC_RELAYING;
- dcc[uidx].u.relay = ri;
- }
- static void eof_dcc_relay(int idx)
- {
- register int j;
- struct chat_info *ci = NULL;
- for (j = 0; j < dcc_total; j++)
- if (dcc[j].sock == dcc[idx].u.relay->sock)
- break;
- if (j == dcc_total) {
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- dcc[j].status = dcc[j].u.relay->old_status;
- /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
- if (dcc[j].status & STAT_TELNET)
- dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
- putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_ENDRELAY1, dcc[j].nick,
- dcc[idx].nick);
- dprintf(j, "\n\n*** %s %s\n", BOT_ENDRELAY2, conf.bot->nick);
- ci = dcc[j].u.relay->chat;
- free(dcc[j].u.relay);
- dcc[j].u.chat = ci;
- dcc[j].type = &DCC_CHAT;
- if (dcc[j].u.chat->channel >= 0) {
- chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
- dcc[j].nick, BOT_PARTYREJOINED);
- if (dcc[j].u.chat->channel < GLOBAL_CHANS)
- botnet_send_join_idx(j, -1);
- }
- check_bind_chon(dcc[j].nick, j);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- }
- static void eof_dcc_relaying(int idx)
- {
- register int j, x = dcc[idx].u.relay->sock;
- putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
- dcc[idx].host, dcc[idx].port);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- for (j = 0; (dcc[j].sock != x) || (dcc[j].type == &DCC_FORK_RELAY); j++);
- putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPEDRELAY, dcc[j].nick);
- killsock(dcc[j].sock);
- lostdcc(j); /* Drop connection to the bot */
- }
- static void dcc_relay(int idx, char *buf, int j)
- {
- unsigned char *p = (unsigned char *) buf;
- int mark;
- for (j = 0; dcc[j].sock != dcc[idx].u.relay->sock ||
- dcc[j].type != &DCC_RELAYING; j++);
- /* If redirecting to a non-telnet user, swallow telnet codes and
- escape sequences. */
- if (!(dcc[j].status & STAT_TELNET)) {
- while (*p != 0) {
- while (*p && *p != 255 && *p != '\r')
- p++; /* Search for IAC, escape sequences and CR. */
- if (*p == 255) {
- mark = 2;
- if (!*(p + 1))
- mark = 1; /* Bogus */
- if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
- mark = 3;
- if (!*(p + 2))
- mark = 2; /* Bogus */
- }
- strcpy((char *) p, (char *) (p + mark));
- } else if (*p == '\r')
- strcpy((char *) p, (char *) (p + 1));
- }
- if (!buf[0])
- dprintf(-dcc[idx].u.relay->sock, " \n");
- else
- dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
- return;
- }
- /* Telnet user */
- if (!buf[0])
- dprintf(-dcc[idx].u.relay->sock, " \r\n");
- else
- dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
- }
- static void dcc_relaying(int idx, char *buf, int j)
- {
- struct chat_info *ci = NULL;
- if (egg_strcasecmp(buf, "*bye*")) {
- dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
- return;
- }
- for (j = 0; (dcc[j].sock != dcc[idx].u.relay->sock) ||
- (dcc[j].type != &DCC_RELAY); j++);
- dcc[idx].status = dcc[idx].u.relay->old_status;
- /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
- if (dcc[idx].status & STAT_TELNET)
- dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
- dprintf(idx, "\n(%s %s.)\n", BOT_BREAKRELAY, dcc[j].nick);
- dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
- putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_RELAYBROKEN,
- dcc[idx].nick, dcc[j].nick);
- if (dcc[idx].u.relay->chat->channel >= 0) {
- chanout_but(-1, dcc[idx].u.relay->chat->channel,
- "*** %s joined the party line.\n", dcc[idx].nick);
- if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
- botnet_send_join_idx(idx, -1);
- }
- ci = dcc[idx].u.relay->chat;
- free(dcc[idx].u.relay);
- dcc[idx].u.chat = ci;
- dcc[idx].type = &DCC_CHAT;
- check_bind_chon(dcc[idx].nick, idx);
- killsock(dcc[j].sock);
- lostdcc(j);
- }
- static void display_relay(int i, char *other)
- {
- sprintf(other, "rela -> sock %li", dcc[i].u.relay->sock);
- }
- static void display_relaying(int i, char *other)
- {
- sprintf(other, ">rly -> sock %li", dcc[i].u.relay->sock);
- }
- static void display_tandem_relay(int i, char *other)
- {
- strcpy(other, "other rela");
- }
- static void display_pre_relay(int i, char *other)
- {
- strcpy(other, "other >rly");
- }
- static void kill_relay(int idx, void *x)
- {
- register struct relay_info *p = (struct relay_info *) x;
- if (p->chat)
- DCC_CHAT.kill(idx, p->chat);
- free(p);
- }
- struct dcc_table DCC_RELAY =
- {
- "RELAY",
- 0, /* Flags */
- eof_dcc_relay,
- dcc_relay,
- NULL,
- NULL,
- display_relay,
- kill_relay,
- NULL,
- NULL
- };
- static void out_relay(int idx, char *buf, void *x)
- {
- register struct relay_info *p = (struct relay_info *) x;
- if (p && p->chat)
- DCC_CHAT.output(idx, buf, p->chat);
- else
- tputs(dcc[idx].sock, buf, strlen(buf));
- }
- struct dcc_table DCC_RELAYING =
- {
- "RELAYING",
- 0, /* Flags */
- eof_dcc_relaying,
- dcc_relaying,
- NULL,
- NULL,
- display_relaying,
- kill_relay,
- out_relay,
- NULL
- };
- struct dcc_table DCC_FORK_RELAY =
- {
- "FORK_RELAY",
- 0, /* Flags */
- failed_tandem_relay,
- cont_tandem_relay,
- &connect_timeout,
- failed_tandem_relay,
- display_tandem_relay,
- kill_relay,
- NULL,
- NULL
- };
- struct dcc_table DCC_PRE_RELAY =
- {
- "PRE_RELAY",
- 0, /* Flags */
- failed_pre_relay,
- pre_relay,
- NULL,
- NULL,
- display_pre_relay,
- kill_relay,
- NULL,
- NULL
- };
- /* Once a minute, send 'ping' to each bot -- no exceptions
- */
- void check_botnet_pings()
- {
- int i;
- int bots, users;
- tand_t *bot = NULL;
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type == &DCC_BOT)
- if (dcc[i].status & STAT_PINGED) {
- char s[1024] = "";
- putlog(LOG_BOTS, "*", "%s: %s", BOT_PINGTIMEOUT, dcc[i].nick);
- bot = findbot(dcc[i].nick);
- bots = bots_in_subtree(bot);
- users = users_in_subtree(bot);
- simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_PINGTIMEOUT,
- dcc[i].nick, bots, (bots != 1) ? "s" : "",
- users, (users != 1) ? "s" : "");
- chatout("*** %s\n", s);
- botnet_send_unlinked(i, dcc[i].nick, s);
- killsock(dcc[i].sock);
- lostdcc(i);
- }
- }
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type == &DCC_BOT) {
- botnet_send_ping(i);
- dcc[i].status |= STAT_PINGED;
- }
- }
- for (i = 0; i < dcc_total; i++) {
- if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_LEAF)) {
- tand_t *via = findbot(dcc[i].nick);
- for (bot = tandbot; bot; bot = bot->next) {
- if ((via == bot->via) && (bot != via)) {
- /* Not leaflike behavior */
- if (dcc[i].status & STAT_WARNED) {
- char s[1024] = "";
- putlog(LOG_BOTS, "*", "%s %s (%s).", BOT_DISCONNECTED,
- dcc[i].nick, BOT_BOTNOTLEAFLIKE);
- dprintf(i, "bye %s\n", BOT_BOTNOTLEAFLIKE);
- bot = findbot(dcc[i].nick);
- bots = bots_in_subtree(bot);
- users = users_in_subtree(bot);
- simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
- BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE,
- bots, (bots != 1) ? "s" : "", users, (users != 1) ?
- "s" : "");
- chatout("*** %s\n", s);
- botnet_send_unlinked(i, dcc[i].nick, s);
- killsock(dcc[i].sock);
- lostdcc(i);
- } else {
- putlog(LOG_MISC, "*", "I am lame, and am now rejecting %s", bot->bot);
- botnet_send_reject(i, conf.bot->nick, NULL, bot->bot,
- NULL, NULL);
- dcc[i].status |= STAT_WARNED;
- }
- } else
- dcc[i].status &= ~STAT_WARNED;
- }
- }
- }
- #ifdef HUB
- #ifdef S_AUTOLOCK
- check_should_lock();
- #endif /* S_AUTOLOCK */
- #ifdef G_BACKUP
- check_should_backup();
- #endif /* G_BACKUP */
- #endif /* HUB */
- }
- void zapfbot(int idx)
- {
- char s[1024] = "";
- int bots, users;
- tand_t *bot = NULL;
- bot = findbot(dcc[idx].nick);
- bots = bots_in_subtree(bot);
- users = users_in_subtree(bot);
- simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_BOTDROPPED,
- dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
- (users != 1) ? "s" : "");
- chatout("*** %s\n", s);
- botnet_send_unlinked(idx, dcc[idx].nick, s);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- }
- static int get_role(char *bot)
- {
- int rl, i;
- struct bot_addr *ba = NULL;
- int r[5] = { 0, 0, 0, 0, 0 };
- struct userrec *u = NULL, *u2 = NULL;
- if (!(u2 = get_user_by_handle(userlist, bot)))
- return 1;
- for (u = userlist; u; u = u->next) {
- if (u->bot) {
- if (strcmp(u->handle, bot)) {
- ba = get_user(&USERENTRY_BOTADDR, u);
- if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0) && (ba->roleid < 5))
- r[(ba->roleid - 1)]++;
- }
- }
- }
- rl = 0;
- for (i = 1; i <= 4; i++)
- if (r[i] < r[rl])
- rl = i;
- rl++;
- ba = get_user(&USERENTRY_BOTADDR, u2);
- if (ba)
- ba->roleid = rl;
- return rl;
- }
- void lower_bot_linked(int idx)
- {
- char tmp[6] = "";
- egg_snprintf(tmp, sizeof tmp, "rl %d", get_role(dcc[idx].nick));
- putbot(dcc[idx].nick, tmp);
- }
|