| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- /*
- * botcmd.c -- handles:
- * commands that comes across the botnet
- * userfile transfer and update commands from sharebots
- *
- */
- #include "common.h"
- #include "botcmd.h"
- #include "main.h"
- #include "src/mod/share.mod/share.h"
- #include "src/mod/update.mod/update.h"
- #include "net.h"
- #include "tclhash.h"
- #include "misc.h"
- #include "dcc.h"
- #include "userrec.h"
- #include "cfg.h"
- #include "dccutil.h"
- #include "cmds.h"
- #include "chanprog.h"
- #include "botmsg.h"
- #include "botnet.h"
- #include "tandem.h"
- #include "users.h"
- #include "chan.h"
- #include "core_binds.h"
- static char TBUF[1024] = ""; /* Static buffer for goofy bot stuff */
- static char base64to[256] =
- {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
- 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28,
- 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
- 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- };
- int base64_to_int(char *buf)
- {
- int i = 0;
- while (*buf) {
- i = i << 6;
- i += base64to[(int) *buf];
- buf++;
- }
- return i;
- }
- static void fake_alert(int idx, char *item, char *extra)
- {
- static unsigned long lastfake; /* The last time fake_alert was used */
- if (now - lastfake > 10) {
- /* Don't fake_alert more than once every 10secs */
- dprintf(idx, "ct %s NOTICE: %s (%s != %s).\n", conf.bot->nick, NET_FAKEREJECT, item, extra);
- putlog(LOG_BOTS, "*", "%s %s (%s != %s).", dcc[idx].nick, NET_FAKEREJECT, item, extra);
- lastfake = now;
- }
- }
- /* chan <from> <chan> <text>
- */
- static void bot_chan2(int idx, char *msg)
- {
- char *from = NULL, *p = NULL;
- int i, chan;
- from = newsplit(&msg);
- p = newsplit(&msg);
- chan = base64_to_int(p);
- /* Strip annoying control chars */
- for (p = from; *p;) {
- if ((*p < 32) || (*p == 127))
- /* FIXME: overlap strcpy(p, p + 1); */
- sprintf(p, "%s", p + 1);
- else
- p++;
- }
- p = strchr(from, '@');
- if (p) {
- sprintf(TBUF, "<%s> %s", from, msg);
- *p = 0;
- if (!partyidle(p + 1, from)) {
- *p = '@';
- fake_alert(idx, "user", from);
- return;
- }
- *p = '@';
- p++;
- } else {
- sprintf(TBUF, "*** (%s) %s", from, msg);
- p = from;
- }
- i = nextbot(p);
- if (i != idx) {
- fake_alert(idx, "direction", p);
- } else {
- chanout_but(-1, chan, "%s\n", TBUF);
- /* Send to new version bots */
- if (i >= 0)
- botnet_send_chan(idx, from, NULL, chan, msg);
- /* if (strchr(from, '@') != NULL)
- check_bind_chat(from, chan, msg);
- else
- check_bind_bcst(from, chan, msg);
- */
- }
- }
- #ifdef S_DCCPASS
- void bot_cmdpass(int idx, char *par)
- {
- char *p = NULL;
- p = strchr(par, ' ');
- if (p) {
- *p++ = 0;
- botnet_send_cmdpass(idx, par, p);
- p--;
- *p = ' ';
- } else {
- botnet_send_cmdpass(idx, par, "");
- }
- set_cmd_pass(par, 0);
- }
- #endif /* S_DCCPASS */
- void bot_config(int idx, char *par)
- {
- got_config_share(idx, par, 0);
- }
- void bot_configbroad(int idx, char *par)
- {
- got_config_share(idx, par, 1);
- }
- void bot_remotecmd(int idx, char *par) {
- char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
-
- if (par[0])
- tbot = newsplit(&par);
- if (par[0])
- fbot = newsplit(&par);
- if (par[0])
- fhnd = newsplit(&par);
- if (par[0])
- fidx = newsplit(&par);
- if (!strcmp(tbot, conf.bot->nick)) {
- gotremotecmd(tbot, fbot, fhnd, fidx, par);
- } else if (!strcmp(tbot, "*")) {
- botnet_send_cmd_broad(idx, fbot, fhnd, atoi(fidx), par);
- gotremotecmd(tbot, fbot, fhnd, fidx, par);
- } else {
- if (nextbot(tbot) != idx)
- botnet_send_cmd(fbot, tbot, fhnd, atoi(fidx), par);
- }
- }
- void bot_remotereply(int idx, char *par) {
- char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
- if (par[0])
- tbot = newsplit(&par);
- if (par[0])
- fbot = newsplit(&par);
- if (par[0])
- fhnd = newsplit(&par);
- if (par[0])
- fidx = newsplit(&par);
- if (!strcmp(tbot, conf.bot->nick)) {
- gotremotereply(fbot, fhnd, fidx, par);
- } else {
- if (nextbot(tbot)!= idx)
- botnet_send_cmdreply(fbot, tbot, fhnd, fidx, par);
- }
- }
- /* chat <from> <notice> -- only from bots
- */
- static void bot_chat(int idx, char *par)
- {
- char *from = NULL;
- int i;
- from = newsplit(&par);
- if (strchr(from, '@') != NULL) {
- fake_alert(idx, "bot", from);
- return;
- }
- /* Make sure the bot is valid */
- i = nextbot(from);
- if (i != idx) {
- fake_alert(idx, "direction", from);
- return;
- }
- chatout("*** (%s) %s\n", from, par);
- botnet_send_chat(idx, from, par);
- }
- /* actchan <from> <chan> <text>
- */
- static void bot_actchan(int idx, char *par)
- {
- char *from = NULL, *p = NULL;
- int i, chan;
- from = newsplit(&par);
- p = strchr(from, '@');
- if (p == NULL) {
- /* How can a bot do an action? */
- fake_alert(idx, "user@bot", from);
- return;
- }
- *p = 0;
- if (!partyidle(p + 1, from)) {
- *p = '@';
- fake_alert(idx, "user", from);
- return;
- }
- *p = '@';
- p++;
- i = nextbot(p);
- if (i != idx) {
- fake_alert(idx, "direction", p);
- return;
- }
- p = newsplit(&par);
- chan = base64_to_int(p);
- for (p = from; *p;) {
- if ((*p < 32) || (*p == 127))
- sprintf(p, "%s", p + 1);
- /* FIXME: overlap strcpy(p, p + 1); */
- else
- p++;
- }
- chanout_but(-1, chan, "* %s %s\n", from, par);
- botnet_send_act(idx, from, NULL, chan, par);
- }
- /* priv <from> <to> <message>
- */
- static void bot_priv(int idx, char *par)
- {
- char *from = NULL, *p = NULL, *to = TBUF, *tobot = NULL;
- int i;
- from = newsplit(&par);
- tobot = newsplit(&par);
- splitc(to, tobot, '@');
- p = strchr(from, '@');
- if (p != NULL)
- p++;
- else
- p = from;
- i = nextbot(p);
- if (i != idx) {
- fake_alert(idx, "direction", p);
- return;
- }
- if (!to[0])
- return; /* Silently ignore notes to '@bot' this
- * is legacy code */
- if (!egg_strcasecmp(tobot, conf.bot->nick)) { /* For me! */
- if (p == from)
- add_note(to, from, par, -2, 0);
- else {
- i = add_note(to, from, par, -1, 0);
- if (from[0] != '@')
- switch (i) {
- case NOTE_ERROR:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s.", BOT_NOSUCHUSER, to);
- break;
- case NOTE_STORED:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s", BOT_NOTESTORED2);
- break;
- case NOTE_FULL:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s", BOT_NOTEBOXFULL);
- break;
- case NOTE_AWAY:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s", to, BOT_NOTEISAWAY);
- break;
- case NOTE_FWD:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s", "Not online; note forwarded to:", to);
- break;
- case NOTE_TCL:
- break; /* Do nothing */
- case NOTE_OK:
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s.", BOT_NOTESENTTO, to);
- break;
- }
- }
- } else { /* Pass it on */
- i = nextbot(tobot);
- if (i >= 0)
- botnet_send_priv(i, from, to, tobot, "%s", par);
- }
- }
- static void bot_bye(int idx, char *par)
- {
- char s[1024] = "";
- int users, bots;
- bots = bots_in_subtree(findbot(dcc[idx].nick));
- users = users_in_subtree(findbot(dcc[idx].nick));
- simple_sprintf(s, "%s %s. %s (lost %d bot%s and %d user%s)",
- BOT_DISCONNECTED, dcc[idx].nick, par[0] ?
- par : "No reason", bots, (bots != 1) ?
- "s" : "", users, (users != 1) ? "s" : "");
- putlog(LOG_BOTS, "*", "%s", s);
- chatout("*** %s\n", s);
- botnet_send_unlinked(idx, dcc[idx].nick, s);
- dprintf(idx, "*bye\n");
- killsock(dcc[idx].sock);
- lostdcc(idx);
- }
- static void remote_tell_who(int idx, char *nick, int chan)
- {
- int i = 10, k, l, ok = 0;
- char s[1024] = "", *realnick = NULL;
- struct chanset_t *c = NULL;
- realnick = strchr(nick, ':');
- if (realnick)
- realnick++;
- else
- realnick = nick;
- putlog(LOG_BOTS, "*", "#%s# who", realnick);
- strcpy(s, "Channels: ");
- for (c = chanset; c; c = c->next)
- if (!channel_secret(c) && shouldjoin(c)) {
- l = strlen(c->dname);
- if (i + l < 1021) {
- if (i > 10) {
- sprintf(s, "%s, %s", s, c->dname);
- } else {
- strcpy(s, c->dname);
- i += (l + 2);
- }
- }
- }
- if (i > 10) {
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", s, ver);
- } else {
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", BOT_NOCHANNELS, ver);
- }
- if (admin[0])
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "Admin: %s", admin);
- if (chan == 0) {
- botnet_send_priv(idx, conf.bot->nick, nick, NULL,
- "%s (* = %s, + = %s, @ = %s)",
- BOT_PARTYMEMBS, MISC_OWNER, MISC_MASTER, MISC_OP);
- } else {
- botnet_send_priv(idx, conf.bot->nick, nick, NULL,
- "%s %s%d: (* = %s, + = %s, @ = %s)\n",
- BOT_PEOPLEONCHAN,
- (chan < GLOBAL_CHANS) ? "" : "*",
- chan % GLOBAL_CHANS,
- MISC_OWNER, MISC_MASTER, MISC_OP);
- }
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type->flags & DCT_REMOTEWHO)
- if (dcc[i].u.chat->channel == chan) {
- k = sprintf(s, " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
- dcc[i].nick, dcc[i].host);
- 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(s + k, " (%s %lud%luh)", MISC_IDLE, mydays, hrs);
- else if (hrs > 0)
- sprintf(s + k, " (%s %luh%lum)", MISC_IDLE, hrs, mins);
- else
- sprintf(s + k, " (%s %lum)", MISC_IDLE, mins);
- }
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
- if (dcc[i].u.chat->away != NULL)
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, " %s: %s", MISC_AWAY, dcc[i].u.chat->away);
- }
- }
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type == &DCC_BOT) {
- if (!ok) {
- ok = 1;
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", BOT_BOTSCONNECTED);
- }
- sprintf(s, " %s%c%-15s %s",
- dcc[i].status & STAT_CALLED ? "<-" : "->",
- dcc[i].status & STAT_SHARE ? '+' : ' ',
- dcc[i].nick, dcc[i].u.bot->version);
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
- }
- }
- ok = 0;
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].type->flags & DCT_REMOTEWHO) {
- if (dcc[i].u.chat->channel != chan) {
- if (!ok) {
- ok = 1;
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", BOT_OTHERPEOPLE);
- }
- l = sprintf(s, " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)), dcc[i].nick, dcc[i].host);
- if (now - dcc[i].timeval > 300) {
- k = (now - dcc[i].timeval) / 60;
- if (k < 60)
- sprintf(s + l, " (%s %dm)", MISC_IDLE, k);
- else
- sprintf(s + l, " (%s %dh%dm)", MISC_IDLE, k / 60, k % 60);
- }
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
- if (dcc[i].u.chat->away != NULL)
- botnet_send_priv(idx, conf.bot->nick, nick, NULL, " %s: %s", MISC_AWAY, dcc[i].u.chat->away);
- }
- }
- }
- }
- static void bot_shellinfo(int idx, char *par)
- {
- char *username = NULL, *sysname = NULL, *nodename = NULL;
-
- username = newsplit(&par);
- sysname = newsplit(&par);
- nodename = newsplit(&par);
- set_user(&USERENTRY_USERNAME, dcc[idx].user, username);
- set_user(&USERENTRY_OS, dcc[idx].user, sysname);
- dcc[idx].u.bot->sysname[0] = 0;
- strcpy(dcc[idx].u.bot->sysname, sysname);
- set_user(&USERENTRY_NODENAME, dcc[idx].user, nodename);
- }
- /* FIXME: remove after 1.1.8 */
- static void bot_sysname(int idx, char *par)
- {
- dcc[idx].u.bot->sysname[0] = 0;
- strcpy(dcc[idx].u.bot->sysname, par);
- }
- /* who <from@bot> <tobot> <chan#>
- */
- static void bot_who(int idx, char *par)
- {
- char *from = NULL, *to = NULL, *p = NULL;
- int i, chan;
- from = newsplit(&par);
- p = strchr(from, '@');
- if (!p) {
- sprintf(TBUF, "%s@%s", from, dcc[idx].nick);
- from = TBUF;
- }
- to = newsplit(&par);
- if (!egg_strcasecmp(to, conf.bot->nick))
- to[0] = 0; /* (for me) */
- chan = base64_to_int(par);
- if (to[0]) { /* Pass it on */
- i = nextbot(to);
- if (i >= 0)
- botnet_send_who(i, from, to, chan);
- } else {
- remote_tell_who(idx, from, chan);
- }
- }
- static void bot_endlink(int idx, char *par)
- {
- dcc[idx].status &= ~STAT_LINKING;
- }
- static void bot_ping(int idx, char *par)
- {
- botnet_send_pong(idx);
- }
- static void bot_pong(int idx, char *par)
- {
- dcc[idx].status &= ~STAT_PINGED;
- if (dcc[idx].pingtime > (now - 120))
- dcc[idx].pingtime -= now;
- else
- dcc[idx].pingtime = 120;
- }
- /* link <from@bot> <who> <to-whom>
- */
- static void bot_link(int idx, char *par)
- {
- char *from = NULL, *bot = NULL, *rfrom = NULL;
- int i;
- from = newsplit(&par);
- bot = newsplit(&par);
- if (!egg_strcasecmp(bot, conf.bot->nick)) {
- if ((rfrom = strchr(from, ':')))
- rfrom++;
- else
- rfrom = from;
- putlog(LOG_CMDS, "*", "#%s# link %s", rfrom, par);
- if (botlink(from, -1, par))
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s ...", BOT_LINKATTEMPT, par);
- else
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s.", BOT_CANTLINKTHERE);
- } else {
- i = nextbot(bot);
- if (i >= 0)
- botnet_send_link(i, from, bot, par);
- }
- }
- /* unlink <from@bot> <linking-bot> <undesired-bot> <reason>
- */
- static void bot_unlink(int idx, char *par)
- {
- char *from = NULL, *bot = NULL, *rfrom = NULL, *p = NULL, *undes = NULL;
- int i;
- from = newsplit(&par);
- bot = newsplit(&par);
- undes = newsplit(&par);
- if (!egg_strcasecmp(bot, conf.bot->nick)) {
- if ((rfrom = strchr(from, ':')))
- rfrom++;
- else
- rfrom = from;
- putlog(LOG_CMDS, "*", "#%s# unlink %s (%s)", rfrom, undes, par[0] ? par : "No reason");
- i = botunlink(-3, undes, par[0] ? par : NULL);
- if (i == 1) {
- p = strchr(from, '@');
- if (p) {
- /* idx will change after unlink -- get new idx
- *
- * TODO: This has changed with the new lostdcc() behaviour. Check
- * if we can optimise the situation.
- */
- i = nextbot(p + 1);
- if (i >= 0)
- botnet_send_priv(i, conf.bot->nick, from, NULL, "Unlinked from %s.", undes);
- }
- } else if (i == 0) {
- botnet_send_unlinked(-1, undes, "");
- p = strchr(from, '@');
- if (p) {
- /* Ditto above, about idx */
- i = nextbot(p + 1);
- if (i >= 0)
- botnet_send_priv(i, conf.bot->nick, from, NULL, "%s %s.", BOT_CANTUNLINK, undes);
- }
- } else {
- p = strchr(from, '@');
- if (p) {
- i = nextbot(p + 1);
- if (i >= 0)
- botnet_send_priv(i, conf.bot->nick, from, NULL, "Can't remotely unlink sharebots.");
- }
- }
- } else {
- i = nextbot(bot);
- if (i >= 0)
- botnet_send_unlink(i, from, bot, undes, par);
- }
- }
- /* Bot next share?
- */
- static void bot_update(int idx, char *par)
- {
- char *bot = NULL, x, *vversion = NULL;
- int vlocalhub = 0;
- time_t vbuildts = 0L;
- bot = newsplit(&par);
- x = par[0];
- if (x)
- par++;
- newsplit(&par); /* vnum */
- if (par[0])
- vlocalhub = atoi(newsplit(&par));
- if (par[0])
- vbuildts = atol(newsplit(&par));
- if (par[0])
- vversion = newsplit(&par);
- if (in_chain(bot))
- updatebot(idx, bot, x, vlocalhub, vbuildts, vversion);
- }
- /* Newbot next share?
- */
- static void bot_nlinked(int idx, char *par)
- {
- char *newbot = NULL, *next = NULL, *p = NULL, s[1024] = "", x = 0, *vversion = NULL;
- int bogus = 0, i, vlocalhub = 0;
- time_t vbuildts = 0L;
- newbot = newsplit(&par);
- next = newsplit(&par);
- s[0] = 0;
- if (!next[0]) {
- putlog(LOG_BOTS, "*", "Invalid eggnet protocol from %s (zapfing)", dcc[idx].nick);
- simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick, MISC_INVALIDBOT);
- dprintf(idx, "error invalid eggnet protocol for 'nlinked'\n");
- } else if ((in_chain(newbot)) || (!egg_strcasecmp(newbot, conf.bot->nick))) {
- /* Loop! */
- putlog(LOG_BOTS, "*", "%s %s (mutual: %s)", BOT_LOOPDETECT, dcc[idx].nick, newbot);
- simple_sprintf(s, "%s %s: disconnecting %s", MISC_LOOP, newbot, dcc[idx].nick);
- dprintf(idx, "error Loop (%s)\n", newbot);
- }
- if (!s[0]) {
- for (p = newbot; *p; p++)
- if ((*p < 32) || (*p == 127) || ((p - newbot) >= HANDLEN))
- bogus = 1;
- i = nextbot(next);
- if (i != idx)
- bogus = 1;
- }
- if (bogus) {
- putlog(LOG_BOTS, "*", "%s %s! (%s -> %s)", BOT_BOGUSLINK, dcc[idx].nick, next, newbot);
- simple_sprintf(s, "%s: %s %s", BOT_BOGUSLINK, dcc[idx].nick, MISC_DISCONNECTED);
- dprintf(idx, "error %s (%s -> %s)\n", BOT_BOGUSLINK, next, newbot);
- }
- if (s[0]) {
- chatout("*** %s\n", s);
- botnet_send_unlinked(idx, dcc[idx].nick, s);
- dprintf(idx, "bye %s\n", BOT_ILLEGALLINK);
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- x = par[0];
- if (x)
- par++;
- else
- x = '-';
- if (par[0])
- newsplit(&par); /* vnum */
- if (par[0])
- vlocalhub = atoi(newsplit(&par));
- if (par[0])
- vbuildts = atol(newsplit(&par));
- if (par[0])
- vversion = newsplit(&par);
- botnet_send_nlinked(idx, newbot, next, x, vlocalhub, vbuildts, vversion);
-
- if (x == '!') {
- #ifdef HUB
- chatout("*** (%s) %s %s.\n", next, NET_LINKEDTO, newbot);
- #else
- chatout("*** %s linked to botnet.\n", newbot);
- #endif
- x = '-';
- }
- addbot(newbot, dcc[idx].nick, next, x, vlocalhub, vbuildts, vversion ? vversion : "");
- }
- static void bot_unlinked(int idx, char *par)
- {
- int i;
- char *bot = NULL;
- bot = newsplit(&par);
- i = nextbot(bot);
- if ((i >= 0) && (i != idx)) /* Bot is NOT downstream along idx, so
- * BOGUS! */
- fake_alert(idx, "direction", bot);
- else if (i >= 0) { /* Valid bot downstream of idx */
- if (par[0])
- /* #ifdef HUB */
- chatout("*** (%s) %s\n", lastbot(bot), par);
- /*
- #else
- chatout("*** %s unlinked from botnet.\n", par);
- #endif
- */
- botnet_send_unlinked(idx, bot, par);
- unvia(idx, findbot(bot));
- rembot(bot);
- }
- /* Otherwise it's not even a valid bot, so just ignore! */
- }
- /* trace <from@bot> <dest> <chain:chain..>
- */
- static void bot_trace(int idx, char *par)
- {
- char *from = NULL, *dest = NULL;
- int i;
- from = newsplit(&par);
- dest = newsplit(&par);
- simple_sprintf(TBUF, "%s:%s", par, conf.bot->nick);
- botnet_send_traced(idx, from, TBUF);
- if (egg_strcasecmp(dest, conf.bot->nick) && ((i = nextbot(dest)) > 0))
- botnet_send_trace(i, from, dest, par);
- }
- /* traced <to@bot> <chain:chain..>
- */
- static void bot_traced(int idx, char *par)
- {
- char *to = NULL, *p = NULL;
- int i, sock;
- to = newsplit(&par);
- p = strchr(to, '@');
- if (p == NULL)
- p = to;
- else {
- *p = 0;
- p++;
- }
- if (!egg_strcasecmp(p, conf.bot->nick)) {
- time_t t = 0;
- char *p2 = par, *ss = TBUF;
- splitc(ss, to, ':');
- if (ss[0])
- sock = atoi(ss);
- else
- sock = (-1);
- if (par[0] == ':') {
- t = atoi(par + 1);
- p2 = strchr(par + 1, ':');
- if (p2)
- p2++;
- else
- p2 = par + 1;
- }
- for (i = 0; i < dcc_total; i++)
- if ((dcc[i].type->flags & DCT_CHAT) &&
- (!egg_strcasecmp(dcc[i].nick, to)) &&
- ((sock == (-1)) || (sock == dcc[i].sock))) {
- if (t) {
- int j=0;
- {
- register char *c=p;
- for (; *c != '\0'; c++) if (*c == ':') j++;
- }
- dprintf(i, "%s -> %s (%lu secs, %d hop%s)\n", BOT_TRACERESULT, p2,
- now - t, j, (j != 1) ? "s" : "");
- } else
- dprintf(i, "%s -> %s\n", BOT_TRACERESULT, p);
- }
- } else {
- i = nextbot(p);
- if (p != to)
- *--p = '@';
- if (i >= 0)
- botnet_send_traced(i, to, par);
- }
- }
- static void bot_timesync(int idx, char *par)
- {
- putlog(LOG_DEBUG, "@", "Got timesync from %s: %s (%li - %li)", dcc[idx].nick, par, atol(par), now);
- timesync = atol(par) - now;
- #ifdef HUB
- send_timesync(-1);
- #endif /* HUB */
- }
- /* reject <from> <bot>
- */
- static void bot_reject(int idx, char *par)
- {
- char *from = NULL, *who = NULL, *destbot = NULL, *frombot = NULL;
- struct userrec *u = NULL;
- int i;
- from = newsplit(&par);
- frombot = strchr(from, '@');
- if (frombot)
- frombot++;
- else
- frombot = from;
- i = nextbot(frombot);
- if (i != idx) {
- fake_alert(idx, "direction", frombot);
- return;
- }
- who = newsplit(&par);
- if (!(destbot = strchr(who, '@'))) {
- /* Rejecting a bot */
- i = nextbot(who);
- if (i < 0) {
- botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s (%s)",
- BOT_CANTUNLINK, who, BOT_DOESNTEXIST);
- } else if (!egg_strcasecmp(dcc[i].nick, who)) {
- char s[1024] = "";
- /* I'm the connection to the rejected bot */
- putlog(LOG_BOTS, "*", "%s %s %s", from, MISC_REJECTED, dcc[i].nick);
- dprintf(i, "bye %s\n", par[0] ? par : MISC_REJECTED);
- simple_sprintf(s, "%s %s (%s: %s)",
- MISC_DISCONNECTED, dcc[i].nick, from,
- par[0] ? par : MISC_REJECTED);
- chatout("*** %s\n", s);
- botnet_send_unlinked(i, dcc[i].nick, s);
- killsock(dcc[i].sock);
- lostdcc(i);
- } else {
- if (i >= 0)
- botnet_send_reject(i, from, NULL, who, NULL, par);
- }
- } else { /* Rejecting user */
- *destbot++ = 0;
- if (!egg_strcasecmp(destbot, conf.bot->nick)) {
- /* Kick someone here! */
- int ok = 0;
- for (i = 0; (i < dcc_total) && (!ok); i++)
- if ((!egg_strcasecmp(who, dcc[i].nick)) && (dcc[i].type->flags & DCT_CHAT)) {
- u = get_user_by_handle(userlist, dcc[i].nick);
- if (u && ((u->flags & USER_OWNER) && !(dcc[idx].user->flags & USER_ADMIN))) {
- add_note(from, conf.bot->nick, BOT_NOOWNERBOOT, -1, 0);
- return;
- }
- do_boot(i, from, par);
- ok = 1;
- putlog(LOG_CMDS, "*", "#%s# boot %s (%s)", from, who, par[0] ? par : "No reason");
- }
- } else {
- i = nextbot(destbot);
- *--destbot = '@';
- if (i >= 0)
- botnet_send_reject(i, from, NULL, who, NULL, par);
- }
- }
- }
- static void bot_thisbot(int idx, char *par)
- {
- if (egg_strcasecmp(par, dcc[idx].nick)) {
- char s[1024] = "";
- putlog(LOG_BOTS, "*", NET_WRONGBOT, dcc[idx].nick, par);
- dprintf(idx, "bye %s\n", MISC_IMPOSTER);
- simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick,
- MISC_IMPOSTER);
- chatout("*** %s\n", s);
- botnet_send_unlinked(idx, dcc[idx].nick, s);
- unvia(idx, findbot(dcc[idx].nick));
- killsock(dcc[idx].sock);
- lostdcc(idx);
- return;
- }
- /* Set capitalization the way they want it */
- noshare = 1;
- change_handle(dcc[idx].user, par);
- noshare = 0;
- strcpy(dcc[idx].nick, par);
- }
- static void bot_hublog(char *botnick, char *code, char *msg)
- {
- #ifdef HUB
- char *par = NULL, *parp;
- par = parp = strdup(msg);
- if (egg_isdigit(par[0])) {
- int type = atoi(newsplit(&par));
- putlog(type, "@", "(%s) %s", botnick, par);
- } else {
- putlog(LOG_ERRORS, "*", "Malformed HL line from %s: %s %s", botnick, code, par);
- }
- free(parp);
- #endif /* HUB */
- }
- /* Used to send a direct msg from Tcl on one bot to Tcl on another
- * zapf <frombot> <tobot> <code [param]>
- */
- static void bot_zapf(int idx, char *par)
- {
- char *from = NULL, *to = NULL;
- int i;
- from = newsplit(&par);
- to = newsplit(&par);
- i = nextbot(from);
- if (i != idx) {
- fake_alert(idx, "direction", from);
- return;
- }
- if (!egg_strcasecmp(to, conf.bot->nick)) {
- /* For me! */
- char *opcode;
- opcode = newsplit(&par);
- check_bind_bot(from, opcode, par);
- return;
- } else {
- i = nextbot(to);
- if (i >= 0)
- botnet_send_zapf(i, from, to, par);
- }
- }
- /* Used to send a global msg from Tcl on one bot to every other bot
- * zapf-broad <frombot> <code [param]>
- */
- static void bot_zapfbroad(int idx, char *par)
- {
- char *from = NULL, *opcode = NULL;
- int i;
- from = newsplit(&par);
- opcode = newsplit(&par);
- i = nextbot(from);
- if (i != idx) {
- fake_alert(idx, "direction", from);
- return;
- }
- check_bind_bot(from, opcode, par);
- botnet_send_zapf_broad(idx, from, opcode, par);
- }
- static void bot_error(int idx, char *par)
- {
- putlog(LOG_MISC | LOG_BOTS, "*", "%s: %s", dcc[idx].nick, par);
- }
- /* nc <bot> <sock> <newnick>
- */
- static void bot_nickchange(int idx, char *par)
- {
- char *bot = NULL, *ssock = NULL, *newnick = NULL;
- int sock, i;
- bot = newsplit(&par);
- i = nextbot(bot);
- if (i != idx) {
- fake_alert(idx, "direction", bot);
- return;
- }
- ssock = newsplit(&par);
- sock = base64_to_int(ssock);
- newnick = newsplit(&par);
- i = partynick(bot, sock, newnick);
- if (i < 0) {
- fake_alert(idx, "sock#", ssock);
- return;
- }
- chanout_but(-1, party[i].chan, "*** (%s) Nick change: %s -> %s\n",
- bot, newnick, party[i].nick);
- botnet_send_nkch_part(idx, i, newnick);
- }
- /* join <bot> <nick> <chan> <flag><sock> <from>
- */
- static void bot_join(int idx, char *par)
- {
- char *bot = NULL, *nick = NULL, *x = NULL, *y = NULL;
- struct userrec *u = NULL;
- int i, sock, chan, i2, linking = 0;
- bot = newsplit(&par);
- if (bot[0] == '!') {
- linking = 1;
- bot++;
- }
- if (b_status(idx) & STAT_LINKING) {
- linking = 1;
- }
- nick = newsplit(&par);
- x = newsplit(&par);
- chan = base64_to_int(x);
- y = newsplit(&par);
- if ((chan < 0) || !y[0])
- return; /* Woops! pre 1.2.1's send .chat off'ers
- * too!! */
- if (!y[0]) {
- y[0] = '-';
- sock = 0;
- } else {
- sock = base64_to_int(y + 1);
- }
- i = nextbot(bot);
- if (i != idx) { /* Ok, garbage from a 1.0g (who uses that
- * now?) OR raistlin being evil :) */
- fake_alert(idx, "direction", bot);
- return;
- }
- u = get_user_by_handle(userlist, nick);
- if (u) {
- sprintf(TBUF, "@%s", bot);
- touch_laston(u, TBUF, now);
- }
- i = addparty(bot, nick, chan, y[0], sock, par, &i2);
- botnet_send_join_party(idx, linking, i2, i);
- if (i != chan) {
- if (i >= 0) {
- chanout_but(-1, i, "*** (%s) %s %s %s.\n", bot, nick, NET_LEFTTHE, i ? "channel" : "party line");
- }
- if (!linking)
- chanout_but(-1, chan, "*** (%s) %s %s %s.\n", bot, nick, NET_JOINEDTHE, chan ? "channel" : "party line");
- }
- }
- /* part <bot> <nick> <sock> [etc..]
- */
- static void bot_part(int idx, char *par)
- {
- char *bot = NULL, *nick = NULL, *etc = NULL;
- struct userrec *u = NULL;
- int sock, partyidx;
- int silent = 0;
- bot = newsplit(&par);
- if (bot[0] == '!') {
- silent = 1;
- bot++;
- }
- nick = newsplit(&par);
- etc = newsplit(&par);
- sock = base64_to_int(etc);
- u = get_user_by_handle(userlist, nick);
- if (u) {
- sprintf(TBUF, "@%s", bot);
- touch_laston(u, TBUF, now);
- }
- if ((partyidx = getparty(bot, sock)) != -1) {
- if (!silent) {
- register int chan = party[partyidx].chan;
- if (par[0])
- chanout_but(-1, chan, "*** (%s) %s %s %s (%s).\n", bot, nick,
- NET_LEFTTHE,
- chan ? "channel" : "party line", par);
- else
- chanout_but(-1, chan, "*** (%s) %s %s %s.\n", bot, nick,
- NET_LEFTTHE,
- chan ? "channel" : "party line");
- }
- botnet_send_part_party(idx, partyidx, par, silent);
- remparty(bot, sock);
- }
- }
- /* away <bot> <sock> <message>
- * null message = unaway
- */
- static void bot_away(int idx, char *par)
- {
- char *bot = NULL, *etc = NULL;
- int sock, partyidx, linking = 0;
- bot = newsplit(&par);
- if (bot[0] == '!') {
- linking = 1;
- bot++;
- }
- if (b_status(idx) & STAT_LINKING) {
- linking = 1;
- }
- etc = newsplit(&par);
- sock = base64_to_int(etc);
- check_bind_away(bot, idx, par);
- if (par[0]) {
- partystat(bot, sock, PLSTAT_AWAY, 0);
- partyaway(bot, sock, par);
- } else {
- partystat(bot, sock, 0, PLSTAT_AWAY);
- }
- partyidx = getparty(bot, sock);
- if (!linking) {
- if (par[0])
- chanout_but(-1, party[partyidx].chan,
- "*** (%s) %s %s: %s.\n", bot,
- party[partyidx].nick, NET_AWAY, par);
- else
- chanout_but(-1, party[partyidx].chan,
- "*** (%s) %s %s.\n", bot,
- party[partyidx].nick, NET_UNAWAY);
- }
- botnet_send_away(idx, bot, sock, par, linking);
- }
- /* (a courtesy info to help during connect bursts)
- * idle <bot> <sock> <#secs> [away msg]
- */
- static void bot_idle(int idx, char *par)
- {
- char *bot = NULL, *work = NULL;
- int sock, idle;
- bot = newsplit(&par);
- work = newsplit(&par);
- sock = base64_to_int(work);
- work = newsplit(&par);
- idle = base64_to_int(work);
- partysetidle(bot, sock, idle);
- if (par[0]) {
- partystat(bot, sock, PLSTAT_AWAY, 0);
- partyaway(bot, sock, par);
- }
- botnet_send_idle(idx, bot, sock, idle, par);
- }
- void bot_share(int idx, char *par)
- {
- sharein(idx, par);
- }
- void bot_shareupdate(int idx, char *par)
- {
- updatein(idx, par);
- }
- /* v <frombot> <tobot> <idx:nick>
- */
- static void bot_versions(int sock, char *par)
- {
- char *frombot = newsplit(&par), *tobot = NULL, *from = NULL;
- if (nextbot(frombot) != sock)
- fake_alert(sock, "versions-direction", frombot);
- else if (egg_strcasecmp(tobot = newsplit(&par), conf.bot->nick)) {
- if ((sock = nextbot(tobot)) >= 0)
- dprintf(sock, "v %s %s %s\n", frombot, tobot, par);
- } else {
- from = newsplit(&par);
- botnet_send_priv(sock, conf.bot->nick, from, frombot, "Modules loaded:\n");
- /* wtf?
- for (me = module_list; me; me = me->next)
- botnet_send_priv(sock, conf.bot->nick, from, frombot, " Module: %s\n", me->name);
- */
- botnet_send_priv(sock, conf.bot->nick, from, frombot, "End of module list.\n");
- }
- }
- /* BOT COMMANDS
- *
- * function call should be:
- * int bot_whatever(idx,"parameters");
- *
- * SORT these, dcc_bot uses a shortcut which requires them sorted
- *
- * yup, those are tokens there to allow a more efficient botnet as
- * time goes on (death to slowly upgrading llama's)
- */
- botcmd_t C_bot[] =
- {
- {"a", (Function) bot_actchan},
- {"aw", (Function) bot_away},
- {"bye", (Function) bot_bye},
- {"c", (Function) bot_chan2},
- {"cg", (Function) bot_config},
- {"cgb", (Function) bot_configbroad},
- #ifdef S_DCCPASS
- {"cp", (Function) bot_cmdpass},
- #endif
- {"ct", (Function) bot_chat},
- {"e", (Function) bot_error},
- {"el", (Function) bot_endlink},
- {"i", (Function) bot_idle},
- {"j", (Function) bot_join},
- {"l", (Function) bot_link},
- {"n", (Function) bot_nlinked},
- {"nc", (Function) bot_nickchange},
- {"p", (Function) bot_priv},
- {"pi", (Function) bot_ping},
- {"po", (Function) bot_pong},
- {"pt", (Function) bot_part},
- {"r", (Function) bot_reject},
- {"rc", (Function) bot_remotecmd},
- {"rr", (Function) bot_remotereply},
- {"s", (Function) bot_share},
- {"sb", (Function) bot_shareupdate},
- {"si", (Function) bot_shellinfo},
- {"t", (Function) bot_trace},
- {"tb", (Function) bot_thisbot},
- {"td", (Function) bot_traced},
- {"ts", (Function) bot_timesync},
- {"u", (Function) bot_update},
- {"ul", (Function) bot_unlink},
- {"un", (Function) bot_unlinked},
- {"v", (Function) bot_versions},
- {"vs", (Function) bot_sysname},
- {"w", (Function) bot_who},
- {"z", (Function) bot_zapf},
- {"zb", (Function) bot_zapfbroad},
- {NULL, NULL}
- };
- void send_remote_simul(int idx, char *bot, char *cmd, char *par)
- {
- char msg[SGRAB - 110] = "";
- egg_snprintf(msg, sizeof msg, "r-s %d %s %d %s %lu %s %s", idx, dcc[idx].nick, dcc[idx].u.chat->con_flags,
- dcc[idx].u.chat->con_chan, dcc[idx].status, cmd, par);
- putbot(bot, msg);
- }
- /* idx nick conmask cmd par */
- static void bot_rsim(char *botnick, char *code, char *msg)
- {
- int ridx = -1, idx = -1, i = 0, rconmask;
- unsigned long status = 0;
- char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX] = "", *par = NULL, *parp = NULL;
- par = parp = strdup(msg);
- ridx = atoi(newsplit(&par));
- nick = newsplit(&par);
- rconmask = atoi(newsplit(&par));
- rconchan = newsplit(&par);
- if (egg_isdigit(par[0]))
- status = (unsigned long) atoi(newsplit(&par));
- cmd = newsplit(&par);
- if (ridx < 0 || !nick || !cmd) {
- free(parp);
- return;
- }
- for (i = 0; i < dcc_total; i++) {
- if (dcc[i].simul == ridx) {
- putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
- dcc[i].simultime = now;
- idx = i;
- break;
- }
- }
- if (idx < 0) {
- idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
- putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
- dcc[idx].sock = -1;
- dcc[idx].timeval = now;
- dcc[idx].simultime = now;
- dcc[idx].simul = ridx;
- dcc[idx].status = status;
- strcpy(dcc[idx].simulbot, botnick);
- dcc[idx].u.chat->con_flags = rconmask;
- strcpy(dcc[idx].u.chat->con_chan, rconchan);
- dcc[idx].u.chat->strip_flags = STRIP_ALL;
- strcpy(dcc[idx].nick, nick);
- egg_snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
- strcpy(dcc[idx].host, buf);
- dcc[idx].addr = 0L;
- dcc[idx].user = get_user_by_handle(userlist, nick);
- }
- rmspace(par);
- check_bind_dcc(cmd, idx, par);
- free(parp);
- }
- void bounce_simul(int idx, char *buf)
- {
- char rmsg[SGRAB - 110] = "";
- if (!buf || !buf[0] || !dcc[idx].simulbot || !dcc[idx].simulbot[0] || idx < 0)
- return;
- egg_snprintf(rmsg, sizeof rmsg, "r-sr %d %s", dcc[idx].simul, buf); /* remote-simul[r]eturn idx buf */
- putbot(dcc[idx].simulbot, rmsg);
- }
- #ifdef HUB
- static void bot_rsimr(char *botnick, char *code, char *msg)
- {
- if (msg[0]) {
- char *par = strdup(msg), *parp = par, *prefix = NULL;
- int idx = atoi(newsplit(&par));
- size_t size = strlen(botnick) + 4;
- prefix = calloc(1, size);
- egg_snprintf(prefix, size, "[%s] ", botnick);
- dumplots(idx, prefix, par);
- free(prefix);
- free(parp);
- }
- }
- #endif /* HUB */
- static cmd_t my_bot[] =
- {
- {"hl", "", (Function) bot_hublog, NULL},
- #ifdef HUB /* This will only allow hubs to read the return text */
- {"r-sr", "", (Function) bot_rsimr, NULL},
- #endif /* HUB */
- {"r-s", "", (Function) bot_rsim, NULL},
- {NULL, NULL, NULL, NULL}
- };
- void init_botcmd()
- {
- add_builtins("bot", my_bot);
- }
|