botnet.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. /*
  2. * botnet.c -- handles:
  3. * keeping track of which bot's connected where in the chain
  4. * dumping a list of bots or a bot tree to a user
  5. * channel name associations on the party line
  6. * rejecting a bot
  7. * linking, unlinking, and relaying to another bot
  8. * pinging the bots periodically and checking leaf status
  9. *
  10. */
  11. #include "main.h"
  12. #include "tandem.h"
  13. extern int dcc_total, backgrd, connect_timeout, max_dcc,
  14. egg_numver, cfg_count;
  15. extern struct userrec *userlist;
  16. extern struct dcc_t *dcc;
  17. extern time_t now;
  18. extern Tcl_Interp *interp;
  19. extern struct cfg_entry ** cfg;
  20. tand_t *tandbot; /* Keep track of tandem bots on the
  21. botnet */
  22. party_t *party; /* Keep track of people on the botnet */
  23. static int maxparty = 200; /* Maximum space for party line members
  24. currently */
  25. int tands = 0; /* Number of bots on the botnet */
  26. int parties = 0; /* Number of people on the botnet */
  27. char botnetnick[HANDLEN + 1] = ""; /* Botnet nickname */
  28. int share_unlinks = 1; /* Allow remote unlinks of my
  29. sharebots? */
  30. int expmem_botnet()
  31. {
  32. int size = 0, i;
  33. tand_t *bot;
  34. for (bot = tandbot; bot; bot = bot->next)
  35. size += sizeof(tand_t);
  36. size += (maxparty * sizeof(party_t));
  37. for (i = 0; i < parties; i++) {
  38. if (party[i].away)
  39. size += strlen(party[i].away) + 1;
  40. if (party[i].from)
  41. size += strlen(party[i].from) + 1;
  42. }
  43. return size;
  44. }
  45. void init_bots()
  46. {
  47. tandbot = NULL;
  48. /* Grab space for 50 bots for now -- expand later as needed */
  49. maxparty = 50;
  50. party = (party_t *) nmalloc(maxparty * sizeof(party_t));
  51. }
  52. tand_t *findbot(char *who)
  53. {
  54. tand_t *ptr;
  55. for (ptr = tandbot; ptr; ptr = ptr->next)
  56. if (!egg_strcasecmp(ptr->bot, who))
  57. return ptr;
  58. return NULL;
  59. }
  60. /* Add a tandem bot to our chain list
  61. */
  62. void addbot(char *who, char *from, char *next, char flag, int vernum)
  63. {
  64. tand_t **ptr = &tandbot, *ptr2;
  65. while (*ptr) {
  66. if (!egg_strcasecmp((*ptr)->bot, who))
  67. putlog(LOG_BOTS, "*", "!!! Duplicate botnet bot entry!!");
  68. ptr = &((*ptr)->next);
  69. }
  70. ptr2 = nmalloc(sizeof(tand_t));
  71. strncpy(ptr2->bot, who, HANDLEN);
  72. ptr2->bot[HANDLEN] = 0;
  73. ptr2->share = flag;
  74. ptr2->ver = vernum;
  75. ptr2->next = *ptr;
  76. *ptr = ptr2;
  77. /* May be via itself */
  78. ptr2->via = findbot(from);
  79. if (!egg_strcasecmp(next, botnetnick))
  80. ptr2->uplink = (tand_t *) 1;
  81. else
  82. ptr2->uplink = findbot(next);
  83. tands++;
  84. }
  85. void updatebot(int idx, char *who, char share, int vernum)
  86. {
  87. tand_t *ptr = findbot(who);
  88. if (ptr) {
  89. if (share)
  90. ptr->share = share;
  91. if (vernum)
  92. ptr->ver = vernum;
  93. botnet_send_update(idx, ptr);
  94. }
  95. }
  96. /* For backward 1.0 compatibility:
  97. * grab the (first) sock# for a user on another bot
  98. */
  99. int partysock(char *bot, char *nick)
  100. {
  101. int i;
  102. for (i = 0; i < parties; i++) {
  103. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  104. (!egg_strcasecmp(party[i].nick, nick)))
  105. return party[i].sock;
  106. }
  107. return 0;
  108. }
  109. /* New botnet member
  110. */
  111. int addparty(char *bot, char *nick, int chan, char flag, int sock,
  112. char *from, int *idx)
  113. {
  114. int i;
  115. for (i = 0; i < parties; i++) {
  116. /* Just changing the channel of someone already on? */
  117. if (!egg_strcasecmp(party[i].bot, bot) &&
  118. (party[i].sock == sock)) {
  119. int oldchan = party[i].chan;
  120. party[i].chan = chan;
  121. party[i].timer = now;
  122. if (from[0]) {
  123. if (flag == ' ')
  124. flag = '-';
  125. party[i].flag = flag;
  126. if (party[i].from)
  127. nfree(party[i].from);
  128. party[i].from = nmalloc(strlen(from) + 1);
  129. strcpy(party[i].from, from);
  130. }
  131. *idx = i;
  132. return oldchan;
  133. }
  134. }
  135. /* New member */
  136. if (parties == maxparty) {
  137. maxparty += 50;
  138. party = (party_t *) nrealloc((void *) party, maxparty * sizeof(party_t));
  139. }
  140. strncpy(party[parties].nick, nick, HANDLEN);
  141. party[parties].nick[HANDLEN] = 0;
  142. strncpy(party[parties].bot, bot, HANDLEN);
  143. party[parties].bot[HANDLEN] = 0;
  144. party[parties].chan = chan;
  145. party[parties].sock = sock;
  146. party[parties].status = 0;
  147. party[parties].away = 0;
  148. party[parties].timer = now; /* cope. */
  149. if (from[0]) {
  150. if (flag == ' ')
  151. flag = '-';
  152. party[parties].flag = flag;
  153. party[parties].from = nmalloc(strlen(from) + 1);
  154. strcpy(party[parties].from, from);
  155. } else {
  156. party[parties].flag = ' ';
  157. party[parties].from = nmalloc(10);
  158. strcpy(party[parties].from, "(unknown)");
  159. }
  160. *idx = parties;
  161. parties++;
  162. return -1;
  163. }
  164. /* Alter status flags for remote party-line user.
  165. */
  166. void partystat(char *bot, int sock, int add, int rem)
  167. {
  168. int i;
  169. for (i = 0; i < parties; i++) {
  170. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  171. (party[i].sock == sock)) {
  172. party[i].status |= add;
  173. party[i].status &= ~rem;
  174. }
  175. }
  176. }
  177. /* Other bot is sharing idle info.
  178. */
  179. void partysetidle(char *bot, int sock, int secs)
  180. {
  181. int i;
  182. for (i = 0; i < parties; i++) {
  183. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  184. (party[i].sock == sock)) {
  185. party[i].timer = (now - (time_t) secs);
  186. }
  187. }
  188. }
  189. /* Return someone's chat channel.
  190. */
  191. int getparty(char *bot, int sock)
  192. {
  193. int i;
  194. for (i = 0; i < parties; i++) {
  195. if (!egg_strcasecmp(party[i].bot, bot) &&
  196. (party[i].sock == sock)) {
  197. return i;
  198. }
  199. }
  200. return -1;
  201. }
  202. /* Un-idle someone
  203. */
  204. int partyidle(char *bot, char *nick)
  205. {
  206. int i, ok = 0;
  207. for (i = 0; i < parties; i++) {
  208. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  209. (!egg_strcasecmp(party[i].nick, nick))) {
  210. party[i].timer = now;
  211. ok = 1;
  212. }
  213. }
  214. return ok;
  215. }
  216. /* Change someone's nick
  217. */
  218. int partynick(char *bot, int sock, char *nick)
  219. {
  220. char work[HANDLEN + 1];
  221. int i;
  222. for (i = 0; i < parties; i++) {
  223. if (!egg_strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
  224. strcpy(work, party[i].nick);
  225. strncpy(party[i].nick, nick, HANDLEN);
  226. party[i].nick[HANDLEN] = 0;
  227. strcpy(nick, work);
  228. return i;
  229. }
  230. }
  231. return -1;
  232. }
  233. /* Set away message
  234. */
  235. void partyaway(char *bot, int sock, char *msg)
  236. {
  237. int i;
  238. for (i = 0; i < parties; i++) {
  239. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  240. (party[i].sock == sock)) {
  241. if (party[i].away)
  242. nfree(party[i].away);
  243. if (msg[0]) {
  244. party[i].away = nmalloc(strlen(msg) + 1);
  245. strcpy(party[i].away, msg);
  246. } else
  247. party[i].away = 0;
  248. }
  249. }
  250. }
  251. /* Remove a tandem bot from the chain list
  252. */
  253. void rembot(char *who)
  254. {
  255. tand_t **ptr = &tandbot, *ptr2;
  256. struct userrec *u;
  257. while (*ptr) {
  258. if (!egg_strcasecmp((*ptr)->bot, who))
  259. break;
  260. ptr = &((*ptr)->next);
  261. }
  262. if (!*ptr)
  263. /* May have just .unlink *'d */
  264. return;
  265. check_tcl_disc(who);
  266. u = get_user_by_handle(userlist, who);
  267. if (u != NULL)
  268. touch_laston(u, "unlinked", now);
  269. ptr2 = *ptr;
  270. *ptr = ptr2->next;
  271. nfree(ptr2);
  272. tands--;
  273. dupwait_notify(who);
  274. }
  275. void remparty(char *bot, int sock)
  276. {
  277. int i;
  278. for (i = 0; i < parties; i++)
  279. if ((!egg_strcasecmp(party[i].bot, bot)) &&
  280. (party[i].sock == sock)) {
  281. parties--;
  282. if (party[i].from)
  283. nfree(party[i].from);
  284. if (party[i].away)
  285. nfree(party[i].away);
  286. if (i < parties) {
  287. strcpy(party[i].bot, party[parties].bot);
  288. strcpy(party[i].nick, party[parties].nick);
  289. party[i].chan = party[parties].chan;
  290. party[i].sock = party[parties].sock;
  291. party[i].flag = party[parties].flag;
  292. party[i].status = party[parties].status;
  293. party[i].timer = party[parties].timer;
  294. party[i].from = party[parties].from;
  295. party[i].away = party[parties].away;
  296. }
  297. }
  298. }
  299. /* Cancel every user that was on a certain bot
  300. */
  301. void rempartybot(char *bot)
  302. {
  303. int i;
  304. for (i = 0; i < parties; i++)
  305. if (!egg_strcasecmp(party[i].bot, bot)) {
  306. if (party[i].chan >= 0)
  307. check_tcl_chpt(bot, party[i].nick, party[i].sock, party[i].chan);
  308. remparty(bot, party[i].sock);
  309. i--;
  310. }
  311. }
  312. /* Remove every bot linked 'via' bot <x>
  313. */
  314. void unvia(int idx, tand_t * who)
  315. {
  316. tand_t *bot, *bot2;
  317. if (!who)
  318. return; /* Safety */
  319. rempartybot(who->bot);
  320. bot = tandbot;
  321. while (bot) {
  322. if (bot->uplink == who) {
  323. unvia(idx, bot);
  324. bot2 = bot->next;
  325. rembot(bot->bot);
  326. bot = bot2;
  327. } else
  328. bot = bot->next;
  329. }
  330. }
  331. void besthub(char *hub)
  332. {
  333. tand_t *ptr = tandbot;
  334. struct userrec *u,
  335. *besthub = NULL;
  336. char bestlval[20],
  337. lval[20];
  338. hub[0] = 0;
  339. strcpy(bestlval, "z");
  340. while (ptr) {
  341. u = get_user_by_handle(userlist, ptr->bot);
  342. if (u) {
  343. link_pref_val(u, lval);
  344. if (strcmp(lval, bestlval) < 0) {
  345. strcpy(bestlval, lval);
  346. besthub = u;
  347. }
  348. }
  349. ptr = ptr->next;
  350. }
  351. if (besthub)
  352. strcpy(hub, besthub->handle);
  353. return;
  354. }
  355. /* Return index into dcc list of the bot that connects us to bot <x>
  356. */
  357. int nextbot(char *who)
  358. {
  359. int j;
  360. tand_t *bot = findbot(who);
  361. if (!bot)
  362. return -1;
  363. for (j = 0; j < dcc_total; j++)
  364. if (bot->via && !egg_strcasecmp(bot->via->bot, dcc[j].nick) &&
  365. (dcc[j].type == &DCC_BOT))
  366. return j;
  367. return -1; /* We're not connected to 'via' */
  368. }
  369. /* Return name of the bot that is directly connected to bot X
  370. */
  371. char *lastbot(char *who)
  372. {
  373. tand_t *bot = findbot(who);
  374. if (!bot)
  375. return "*";
  376. else if (bot->uplink == (tand_t *) 1)
  377. return botnetnick;
  378. else
  379. return bot->uplink->bot;
  380. }
  381. /* Modern version of 'whom' (use local data)
  382. */
  383. void answer_local_whom(int idx, int chan)
  384. {
  385. char format[81];
  386. char c, idle[40];
  387. int i, t, nicklen, botnicklen, total=0;
  388. if (chan == (-1))
  389. dprintf(idx, "%s (+: %s, *: %s)\n", BOT_BOTNETUSERS, BOT_PARTYLINE,
  390. BOT_LOCALCHAN);
  391. else if (chan > 0) {
  392. simple_sprintf(idle, "assoc %d", chan);
  393. if ((Tcl_Eval(interp, idle) != TCL_OK) || !interp->result[0])
  394. dprintf(idx, "%s %s%d:\n", BOT_USERSONCHAN,
  395. (chan < GLOBAL_CHANS) ? "" : "*", chan % GLOBAL_CHANS);
  396. else
  397. dprintf(idx, "%s '%s%s' (%s%d):\n", BOT_USERSONCHAN,
  398. (chan < GLOBAL_CHANS) ? "" : "*", interp->result,
  399. (chan < GLOBAL_CHANS) ? "" : "*", chan % GLOBAL_CHANS);
  400. }
  401. /* Find longest nick and botnick */
  402. nicklen = botnicklen = 0;
  403. for (i = 0; i < dcc_total; i++)
  404. if (dcc[i].type == &DCC_CHAT) {
  405. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  406. t = strlen(dcc[i].nick); if(t > nicklen) nicklen = t;
  407. t = strlen(botnetnick); if(t > botnicklen) botnicklen = t;
  408. }
  409. }
  410. for (i = 0; i < parties; i++) {
  411. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  412. t = strlen(party[i].nick); if(t > nicklen) nicklen = t;
  413. t = strlen(party[i].bot); if(t > botnicklen) botnicklen = t;
  414. }
  415. }
  416. if(nicklen < 9) nicklen = 9;
  417. if(botnicklen < 9) botnicklen = 9;
  418. #ifdef HUB
  419. egg_snprintf(format, sizeof format, "%%-%us %%-%us %%s\n",
  420. nicklen, botnicklen);
  421. dprintf(idx, format, " Nick", " Bot", " Host");
  422. dprintf(idx, format, "----------", "---------", "--------------------");
  423. egg_snprintf(format, sizeof format, "%%c%%-%us %%c %%-%us %%s%%s\n",
  424. nicklen, botnicklen);
  425. #else
  426. egg_snprintf(format, sizeof format, "%%-%us\n", nicklen);
  427. dprintf(idx, format, " Nick");
  428. dprintf(idx, format, "----------");
  429. egg_snprintf(format, sizeof format, "%%c%%-%us %%c %%s\n", nicklen);
  430. #endif
  431. for (i = 0; i < dcc_total; i++)
  432. if (dcc[i].type == &DCC_CHAT) {
  433. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  434. c = geticon(i);
  435. if (c == '-')
  436. c = ' ';
  437. if (now - dcc[i].timeval > 300) {
  438. unsigned long days, hrs, mins;
  439. days = (now - dcc[i].timeval) / 86400;
  440. hrs = ((now - dcc[i].timeval) - (days * 86400)) / 3600;
  441. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  442. if (days > 0)
  443. sprintf(idle, " [idle %lud%luh]", days, hrs);
  444. else if (hrs > 0)
  445. sprintf(idle, " [idle %luh%lum]", hrs, mins);
  446. else
  447. sprintf(idle, " [idle %lum]", mins);
  448. } else
  449. idle[0] = 0;
  450. total++;
  451. Context;
  452. dprintf(idx, format, c, dcc[i].nick,
  453. (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
  454. (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
  455. #ifdef HUB
  456. (chan == (-1)) ? '*' : ' ', botnetnick, dcc[i].host, idle);
  457. #else
  458. (chan == (-1)) ? '*' : ' ', idle);
  459. #endif
  460. Context;
  461. if (dcc[i].u.chat->away != NULL)
  462. dprintf(idx, " AWAY: %s\n", dcc[i].u.chat->away);
  463. }
  464. }
  465. for (i = 0; i < parties; i++) {
  466. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  467. c = party[i].flag;
  468. if (c == '-')
  469. c = ' ';
  470. if (party[i].timer == 0L)
  471. strcpy(idle, " [idle?]");
  472. else if (now - party[i].timer > 300) {
  473. unsigned long days, hrs, mins;
  474. days = (now - party[i].timer) / 86400;
  475. hrs = ((now - party[i].timer) - (days * 86400)) / 3600;
  476. mins = ((now - party[i].timer) - (hrs * 3600)) / 60;
  477. if (days > 0)
  478. sprintf(idle, " [idle %lud%luh]", days, hrs);
  479. else if (hrs > 0)
  480. sprintf(idle, " [idle %luh%lum]", hrs, mins);
  481. else
  482. sprintf(idle, " [idle %lum]", mins);
  483. } else
  484. idle[0] = 0;
  485. total++;
  486. dprintf(idx, format, c, party[i].nick,
  487. (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ',
  488. #ifdef HUB
  489. party[i].bot, party[i].from, idle);
  490. #else
  491. idle);
  492. #endif
  493. if (party[i].status & PLSTAT_AWAY)
  494. dprintf(idx, " %s: %s\n", MISC_AWAY,
  495. party[i].away ? party[i].away : "");
  496. }
  497. }
  498. dprintf(idx, "Total users: %d\n", total);
  499. }
  500. /* Show z a list of all bots connected
  501. */
  502. void tell_bots(int idx)
  503. {
  504. char s[512];
  505. int i;
  506. tand_t *bot;
  507. if (!tands) {
  508. dprintf(idx, STR("No bots linked\n"));
  509. return;
  510. }
  511. strcpy(s, botnetnick);
  512. i = strlen(botnetnick);
  513. for (bot = tandbot; bot; bot = bot->next) {
  514. if (i > (500 - HANDLEN)) {
  515. dprintf(idx, STR("Bots: %s\n"), s);
  516. s[0] = 0;
  517. i = 0;
  518. }
  519. if (i) {
  520. s[i++] = ',';
  521. s[i++] = ' ';
  522. }
  523. strcpy(s + i, bot->bot);
  524. i += strlen(bot->bot);
  525. }
  526. if (s[0])
  527. dprintf(idx, STR("Bots: %s\n"), s);
  528. dprintf(idx, STR("(Total: %d)\n"), tands + 1);
  529. }
  530. /* Show a simpleton bot tree
  531. */
  532. void tell_bottree(int idx, int showver)
  533. {
  534. char s[161];
  535. tand_t *last[20], *this, *bot, *bot2 = NULL;
  536. int lev = 0, more = 1, mark[20], ok, cnt, i, imark;
  537. char work[1024];
  538. int tothops = 0;
  539. if (tands == 0) {
  540. dprintf(idx, "%s\n", BOT_NOBOTSLINKED);
  541. return;
  542. }
  543. s[0] = 0;
  544. i = 0;
  545. for (bot = tandbot; bot; bot = bot->next)
  546. if (!bot->uplink) {
  547. if (i) {
  548. s[i++] = ',';
  549. s[i++] = ' ';
  550. }
  551. strcpy(s + i, bot->bot);
  552. i += strlen(bot->bot);
  553. }
  554. if (s[0])
  555. dprintf(idx, "(%s %s)\n", BOT_NOTRACEINFO, s);
  556. if (showver)
  557. dprintf(idx, "%s (%d.%d.%d.%d)\n", botnetnick,
  558. egg_numver / 1000000,
  559. egg_numver % 1000000 / 10000,
  560. egg_numver % 10000 / 100,
  561. egg_numver % 100);
  562. else
  563. dprintf(idx, "%s\n", botnetnick);
  564. this = (tand_t *) 1;
  565. work[0] = 0;
  566. while (more) {
  567. if (lev == 20) {
  568. dprintf(idx, "\n%s\n", BOT_COMPLEXTREE);
  569. return;
  570. }
  571. cnt = 0;
  572. tothops += lev;
  573. for (bot = tandbot; bot; bot = bot->next)
  574. if (bot->uplink == this)
  575. cnt++;
  576. if (cnt) {
  577. imark = 0;
  578. for (i = 0; i < lev; i++) {
  579. if (mark[i])
  580. strcpy(work + imark, " | ");
  581. else
  582. strcpy(work + imark, " ");
  583. imark += 5;
  584. }
  585. if (cnt > 1)
  586. strcpy(work + imark, " |-");
  587. else
  588. strcpy(work + imark, " `-");
  589. s[0] = 0;
  590. bot = tandbot;
  591. while (!s[0]) {
  592. if (bot->uplink == this) {
  593. if (bot->ver) {
  594. i = sprintf(s, "%c%s", bot->share, bot->bot);
  595. if (showver)
  596. sprintf(s + i, " (%d.%d.%d.%d)",
  597. bot->ver / 1000000,
  598. bot->ver % 1000000 / 10000,
  599. bot->ver % 10000 / 100,
  600. bot->ver % 100);
  601. } else {
  602. sprintf(s, "-%s", bot->bot);
  603. }
  604. } else
  605. bot = bot->next;
  606. }
  607. dprintf(idx, "%s%s\n", work, s);
  608. if (cnt > 1)
  609. mark[lev] = 1;
  610. else
  611. mark[lev] = 0;
  612. work[0] = 0;
  613. last[lev] = this;
  614. this = bot;
  615. lev++;
  616. more = 1;
  617. } else {
  618. while (cnt == 0) {
  619. /* No subtrees from here */
  620. if (lev == 0) {
  621. dprintf(idx, "(( tree error ))\n");
  622. return;
  623. }
  624. ok = 0;
  625. for (bot = tandbot; bot; bot = bot->next) {
  626. if (bot->uplink == last[lev - 1]) {
  627. if (this == bot)
  628. ok = 1;
  629. else if (ok) {
  630. cnt++;
  631. if (cnt == 1) {
  632. bot2 = bot;
  633. if (bot->ver) {
  634. i = sprintf(s, "%c%s", bot->share, bot->bot);
  635. if (showver)
  636. sprintf(s + i, " (%d.%d.%d.%d)",
  637. bot->ver / 1000000,
  638. bot->ver % 1000000 / 10000,
  639. bot->ver % 10000 / 100,
  640. bot->ver % 100);
  641. } else {
  642. sprintf(s, "-%s", bot->bot);
  643. }
  644. }
  645. }
  646. }
  647. }
  648. if (cnt) {
  649. imark = 0;
  650. for (i = 1; i < lev; i++) {
  651. if (mark[i - 1])
  652. strcpy(work + imark, " | ");
  653. else
  654. strcpy(work + imark, " ");
  655. imark += 5;
  656. }
  657. more = 1;
  658. if (cnt > 1)
  659. dprintf(idx, "%s |-%s\n", work, s);
  660. else
  661. dprintf(idx, "%s `-%s\n", work, s);
  662. this = bot2;
  663. work[0] = 0;
  664. if (cnt > 1)
  665. mark[lev - 1] = 1;
  666. else
  667. mark[lev - 1] = 0;
  668. } else {
  669. /* This was the last child */
  670. lev--;
  671. if (lev == 0) {
  672. more = 0;
  673. cnt = 999;
  674. } else {
  675. more = 1;
  676. this = last[lev];
  677. }
  678. }
  679. }
  680. }
  681. }
  682. /* Hop information: (9d) */
  683. dprintf(idx, "Average hops: %3.1f, total bots: %d\n",
  684. ((float) tothops) / ((float) tands), tands + 1);
  685. }
  686. /* Dump list of links to a new bot
  687. */
  688. void dump_links(int z)
  689. {
  690. register int i, l;
  691. char x[1024];
  692. tand_t *bot;
  693. for (bot = tandbot; bot; bot = bot->next) {
  694. char *p;
  695. if (bot->uplink == (tand_t *) 1)
  696. p = botnetnick;
  697. else
  698. p = bot->uplink->bot;
  699. l = simple_sprintf(x, "n %s %s %c%D\n", bot->bot, p,
  700. bot->share, bot->ver);
  701. tputs(dcc[z].sock, x, l);
  702. }
  703. if (!(bot_flags(dcc[z].user) & BOT_ISOLATE)) {
  704. /* Dump party line members */
  705. for (i = 0; i < dcc_total; i++) {
  706. if (dcc[i].type == &DCC_CHAT) {
  707. if ((dcc[i].u.chat->channel >= 0) &&
  708. (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
  709. l = simple_sprintf(x, "j !%s %s %D %c%D %s\n",
  710. botnetnick, dcc[i].nick,
  711. dcc[i].u.chat->channel, geticon(i),
  712. dcc[i].sock, dcc[i].host);
  713. tputs(dcc[z].sock, x, l);
  714. l = simple_sprintf(x, "i %s %D %D %s\n", botnetnick,
  715. dcc[i].sock, now - dcc[i].timeval,
  716. dcc[i].u.chat->away ? dcc[i].u.chat->away : "");
  717. tputs(dcc[z].sock, x, l);
  718. }
  719. }
  720. }
  721. for (i = 0; i < parties; i++) {
  722. l = simple_sprintf(x, "j %s %s %D %c%D %s\n",
  723. party[i].bot, party[i].nick,
  724. party[i].chan, party[i].flag,
  725. party[i].sock, party[i].from);
  726. tputs(dcc[z].sock, x, l);
  727. if ((party[i].status & PLSTAT_AWAY) || (party[i].timer != 0)) {
  728. l = simple_sprintf(x, "i %s %D %D %s\n", party[i].bot,
  729. party[i].sock, now - party[i].timer,
  730. party[i].away ? party[i].away : "");
  731. tputs(dcc[z].sock, x, l);
  732. }
  733. }
  734. }
  735. }
  736. int in_chain(char *who)
  737. {
  738. if (findbot(who))
  739. return 1;
  740. if (!egg_strcasecmp(who, botnetnick))
  741. return 1;
  742. return 0;
  743. }
  744. int bots_in_subtree(tand_t *bot)
  745. {
  746. int nr = 1;
  747. tand_t *b;
  748. if (!bot)
  749. return 0;
  750. for (b = tandbot; b; b = b->next) {
  751. if (b->bot && (b->uplink == bot)) {
  752. nr += bots_in_subtree(b);
  753. }
  754. }
  755. return nr;
  756. }
  757. int users_in_subtree(tand_t *bot)
  758. {
  759. int i, nr;
  760. tand_t *b;
  761. nr = 0;
  762. if (!bot)
  763. return 0;
  764. for (i = 0; i < parties; i++)
  765. if (!egg_strcasecmp(party[i].bot, bot->bot))
  766. nr++;
  767. for (b = tandbot; b; b = b->next)
  768. if (b->bot && (b->uplink == bot))
  769. nr += users_in_subtree(b);
  770. return nr;
  771. }
  772. /* Break link with a tandembot
  773. */
  774. int botunlink(int idx, char *nick, char *reason)
  775. {
  776. char s[20];
  777. register int i;
  778. int bots, users;
  779. tand_t *bot;
  780. if (nick[0] == '*')
  781. dprintf(idx, "%s\n", BOT_UNLINKALL);
  782. for (i = 0; i < dcc_total; i++) {
  783. if ((nick[0] == '*') || !egg_strcasecmp(dcc[i].nick, nick)) {
  784. if (dcc[i].type == &DCC_FORK_BOT) {
  785. if (idx >= 0)
  786. dprintf(idx, "%s: %s -> %s.\n", BOT_KILLLINKATTEMPT,
  787. dcc[i].nick, dcc[i].host);
  788. putlog(LOG_BOTS, "*", "%s: %s -> %s:%d",
  789. BOT_KILLLINKATTEMPT, dcc[i].nick,
  790. dcc[i].host, dcc[i].port);
  791. killsock(dcc[i].sock);
  792. lostdcc(i);
  793. if (nick[0] != '*')
  794. return 1;
  795. } else if (dcc[i].type == &DCC_BOT_NEW) {
  796. if (idx >= 0)
  797. dprintf(idx, "%s %s.\n", BOT_ENDLINKATTEMPT,
  798. dcc[i].nick);
  799. putlog(LOG_BOTS, "*", "%s %s @ %s:%d",
  800. "Stopped trying to link", dcc[i].nick,
  801. dcc[i].host, dcc[i].port);
  802. killsock(dcc[i].sock);
  803. lostdcc(i);
  804. if (nick[0] != '*')
  805. return 1;
  806. } else if (dcc[i].type == &DCC_BOT) {
  807. char s[1024];
  808. if (idx >= 0)
  809. dprintf(idx, "%s %s.\n", BOT_BREAKLINK, dcc[i].nick);
  810. else if ((idx == -3) && (b_status(i) & STAT_SHARE) && !share_unlinks)
  811. return -1;
  812. bot = findbot(dcc[i].nick);
  813. bots = bots_in_subtree(bot);
  814. users = users_in_subtree(bot);
  815. if (reason && reason[0]) {
  816. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  817. BOT_UNLINKEDFROM, dcc[i].nick, reason, bots,
  818. (bots != 1) ? "s" : "", users, (users != 1) ?
  819. "s" : "");
  820. dprintf(i, "bye %s\n", reason);
  821. } else {
  822. simple_sprintf(s, "%s %s (lost %d bot%s and %d user%s)",
  823. BOT_UNLINKEDFROM, dcc[i].nick, bots, (bots != 1) ?
  824. "s" : "", users, (users != 1) ? "s" : "");
  825. dprintf(i, "bye No reason\n");
  826. }
  827. chatout("*** %s\n", s);
  828. botnet_send_unlinked(i, dcc[i].nick, s);
  829. killsock(dcc[i].sock);
  830. lostdcc(i);
  831. if (nick[0] != '*')
  832. return 1;
  833. }
  834. }
  835. }
  836. if (idx >= 0 && nick[0] != '*')
  837. dprintf(idx, "%s\n", BOT_NOTCONNECTED);
  838. if (nick[0] != '*') {
  839. bot = findbot(nick);
  840. if (bot) {
  841. /* The internal bot list is desynched from the dcc list
  842. sometimes. While we still search for the bug, provide
  843. an easy way to clear out those `ghost'-bots.
  844. Fabian (2000-08-02) */
  845. char *ghost = "BUG!!: Found bot `%s' in internal bot list, but it\n"
  846. " shouldn't have been there! Removing.\n"
  847. " This is a known bug we haven't fixed yet. If this\n"
  848. " bot is the newest eggdrop version available and you\n"
  849. " know a *reliable* way to reproduce the bug, please\n"
  850. " contact us - we need your help!\n";
  851. if (idx >= 0)
  852. dprintf(idx, ghost, nick);
  853. else
  854. putlog(LOG_MISC, "*", ghost, nick);
  855. rembot(bot->bot);
  856. return 1;
  857. }
  858. }
  859. if (nick[0] == '*') {
  860. dprintf(idx, "%s\n", BOT_WIPEBOTTABLE);
  861. while (tandbot)
  862. rembot(tandbot->bot);
  863. while (parties) {
  864. parties--;
  865. /* Assert? */
  866. if (party[i].chan >= 0)
  867. check_tcl_chpt(party[i].bot, party[i].nick, party[i].sock,
  868. party[i].chan);
  869. }
  870. strcpy(s, "killassoc &");
  871. Tcl_Eval(interp, s);
  872. }
  873. return 0;
  874. }
  875. static void botlink_resolve_success(int);
  876. static void botlink_resolve_failure(int);
  877. /* Link to another bot
  878. */
  879. int botlink(char *linker, int idx, char *nick)
  880. {
  881. struct bot_addr *bi;
  882. struct userrec *u;
  883. register int i;
  884. u = get_user_by_handle(userlist, nick);
  885. if (!u || !(u->flags & USER_BOT)) {
  886. if (idx >= 0)
  887. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  888. } else if (!egg_strcasecmp(nick, botnetnick)) {
  889. if (idx >= 0)
  890. dprintf(idx, "%s\n", BOT_CANTLINKMYSELF);
  891. } else if (in_chain(nick) && (idx != -3)) {
  892. if (idx >= 0)
  893. dprintf(idx, "%s\n", BOT_ALREADYLINKED);
  894. } else {
  895. for (i = 0; i < dcc_total; i++)
  896. if ((dcc[i].user == u) &&
  897. ((dcc[i].type == &DCC_FORK_BOT) ||
  898. (dcc[i].type == &DCC_BOT_NEW))) {
  899. if (idx >= 0)
  900. dprintf(idx, "%s\n", BOT_ALREADYLINKING);
  901. return 0;
  902. }
  903. /* Address to connect to is in 'info' */
  904. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  905. if (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0)) {
  906. if (idx >= 0) {
  907. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  908. dprintf(idx, "%s .chaddr %s %s\n",
  909. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  910. }
  911. } else if (dcc_total == max_dcc) {
  912. if (idx >= 0)
  913. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  914. } else {
  915. correct_handle(nick);
  916. if (idx > -2)
  917. putlog(LOG_BOTS, "*", "%s %s at %s:%d ...", BOT_LINKING, nick,
  918. bi->address, bi->telnet_port);
  919. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  920. dcc[i].timeval = now;
  921. dcc[i].port = bi->telnet_port;
  922. dcc[i].user = u;
  923. strcpy(dcc[i].nick, nick);
  924. strcpy(dcc[i].host, bi->address);
  925. dcc[i].u.dns->ibuf = idx;
  926. dcc[i].u.dns->cptr = get_data_ptr(strlen(linker) + 1);
  927. strcpy(dcc[i].u.dns->cptr, linker);
  928. dcc[i].u.dns->host = get_data_ptr(strlen(dcc[i].host) + 1);
  929. strcpy(dcc[i].u.dns->host, dcc[i].host);
  930. dcc[i].u.dns->dns_success = botlink_resolve_success;
  931. dcc[i].u.dns->dns_failure = botlink_resolve_failure;
  932. dcc[i].u.dns->dns_type = RES_IPBYHOST;
  933. dcc[i].u.dns->type = &DCC_FORK_BOT;
  934. dcc_dnsipbyhost(bi->address);
  935. return 1;
  936. }
  937. }
  938. return 0;
  939. }
  940. static void botlink_resolve_failure(int i)
  941. {
  942. char s[81];
  943. // putlog(LOG_BOTS, "*", DCC_LINKFAIL, dcc[i].nick);
  944. strcpy(s, dcc[i].nick);
  945. nfree(dcc[i].u.dns->cptr);
  946. lostdcc(i);
  947. autolink_cycle(s); /* Check for more auto-connections */
  948. }
  949. static void botlink_resolve_success(int i)
  950. {
  951. int idx = dcc[i].u.dns->ibuf;
  952. char *linker = dcc[i].u.dns->cptr;
  953. dcc[i].addr = dcc[i].u.dns->ip;
  954. changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
  955. dcc[i].timeval = now;
  956. strcpy(dcc[i].u.bot->linker, linker);
  957. strcpy(dcc[i].u.bot->version, "(primitive bot)");
  958. strcpy(dcc[i].u.bot->sysname, "*");
  959. dcc[i].u.bot->numver = idx;
  960. dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
  961. #ifdef USE_IPV6
  962. dcc[i].sock = getsock(SOCK_STRONGCONN, getprotocol(dcc[i].host));
  963. #else
  964. dcc[i].sock = getsock(SOCK_STRONGCONN);
  965. #endif /* USE_IPV6 */
  966. nfree(linker);
  967. if (dcc[i].sock < 0 ||
  968. open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  969. dcc[i].port) < 0)
  970. failed_link(i);
  971. }
  972. static void failed_tandem_relay(int idx)
  973. {
  974. int uidx = (-1), i;
  975. for (i = 0; i < dcc_total; i++)
  976. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  977. (dcc[i].u.relay->sock == dcc[idx].sock))
  978. uidx = i;
  979. if (uidx < 0) {
  980. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  981. dcc[idx].sock, dcc[idx].u.relay->sock);
  982. killsock(dcc[idx].sock);
  983. lostdcc(idx);
  984. return;
  985. }
  986. if (dcc[idx].port >= dcc[idx].u.relay->port + 3) {
  987. struct chat_info *ci = dcc[uidx].u.relay->chat;
  988. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  989. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  990. nfree(dcc[uidx].u.relay);
  991. dcc[uidx].u.chat = ci;
  992. dcc[uidx].type = &DCC_CHAT;
  993. killsock(dcc[idx].sock);
  994. lostdcc(idx);
  995. return;
  996. }
  997. killsock(dcc[idx].sock);
  998. #ifdef USE_IPV6
  999. dcc[idx].sock = getsock(SOCK_STRONGCONN, getprotocol(dcc[idx].host));
  1000. #else
  1001. dcc[idx].sock = getsock(SOCK_STRONGCONN);
  1002. #endif /* USE_IPV6 */
  1003. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1004. dcc[idx].port++;
  1005. dcc[idx].timeval = now;
  1006. if (dcc[idx].sock < 0 ||
  1007. open_telnet_raw(dcc[idx].sock, dcc[idx].addr ?
  1008. iptostr(htonl(dcc[idx].addr)) :
  1009. dcc[idx].host, dcc[idx].port) < 0)
  1010. failed_tandem_relay(idx);
  1011. }
  1012. static void tandem_relay_resolve_failure(int);
  1013. static void tandem_relay_resolve_success(int);
  1014. /* Relay to another tandembot
  1015. */
  1016. void tandem_relay(int idx, char *nick, register int i)
  1017. {
  1018. struct userrec *u;
  1019. struct bot_addr *bi;
  1020. struct chat_info *ci;
  1021. u = get_user_by_handle(userlist, nick);
  1022. if (!u || !(u->flags & USER_BOT)) {
  1023. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  1024. return;
  1025. }
  1026. if (!egg_strcasecmp(nick, botnetnick)) {
  1027. dprintf(idx, "%s\n", BOT_CANTRELAYMYSELF);
  1028. return;
  1029. }
  1030. /* Address to connect to is in 'info' */
  1031. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1032. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1033. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  1034. dprintf(idx, "%s .chaddr %s %s\n",
  1035. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  1036. return;
  1037. }
  1038. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1039. if (i < 0) {
  1040. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  1041. return;
  1042. }
  1043. #ifdef USE_IPV6
  1044. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, getprotocol(bi->address));
  1045. #else
  1046. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
  1047. #endif /* USE_IPV6 */
  1048. if (dcc[i].sock < 0) {
  1049. lostdcc(i);
  1050. dprintf(idx, "%s\n", MISC_NOFREESOCK);
  1051. return;
  1052. }
  1053. dcc[i].port = bi->relay_port;
  1054. dcc[i].addr = 0L;
  1055. strcpy(dcc[i].nick, nick);
  1056. dcc[i].user = u;
  1057. strcpy(dcc[i].host, bi->address);
  1058. #ifdef HUB
  1059. dprintf(idx, "%s %s @ %s:%d ...\n", BOT_CONNECTINGTO, nick,
  1060. bi->address, bi->relay_port);
  1061. #endif
  1062. dprintf(idx, "%s\n", BOT_BYEINFO1);
  1063. dcc[idx].type = &DCC_PRE_RELAY;
  1064. ci = dcc[idx].u.chat;
  1065. dcc[idx].u.relay = get_data_ptr(sizeof(struct relay_info));
  1066. dcc[idx].u.relay->chat = ci;
  1067. dcc[idx].u.relay->old_status = dcc[idx].status;
  1068. dcc[idx].u.relay->sock = dcc[i].sock;
  1069. dcc[i].timeval = now;
  1070. dcc[i].u.dns->ibuf = dcc[idx].sock;
  1071. dcc[i].u.dns->host = get_data_ptr(strlen(bi->address) + 1);
  1072. strcpy(dcc[i].u.dns->host, bi->address);
  1073. dcc[i].u.dns->dns_success = tandem_relay_resolve_success;
  1074. dcc[i].u.dns->dns_failure = tandem_relay_resolve_failure;
  1075. dcc[i].u.dns->dns_type = RES_IPBYHOST;
  1076. dcc[i].u.dns->type = &DCC_FORK_RELAY;
  1077. dcc_dnsipbyhost(bi->address);
  1078. }
  1079. static void tandem_relay_resolve_failure(int idx)
  1080. {
  1081. struct chat_info *ci;
  1082. register int uidx = (-1), i;
  1083. for (i = 0; i < dcc_total; i++)
  1084. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1085. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1086. uidx = i;
  1087. break;
  1088. }
  1089. if (uidx < 0) {
  1090. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1091. dcc[idx].sock, dcc[idx].u.relay->sock);
  1092. killsock(dcc[idx].sock);
  1093. lostdcc(idx);
  1094. return;
  1095. }
  1096. ci = dcc[uidx].u.relay->chat;
  1097. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  1098. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  1099. nfree(dcc[uidx].u.relay);
  1100. dcc[uidx].u.chat = ci;
  1101. dcc[uidx].type = &DCC_CHAT;
  1102. killsock(dcc[idx].sock);
  1103. lostdcc(idx);
  1104. }
  1105. static void tandem_relay_resolve_success(int i)
  1106. {
  1107. int sock = dcc[i].u.dns->ibuf;
  1108. dcc[i].addr = dcc[i].u.dns->ip;
  1109. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1110. dcc[i].u.relay->chat = get_data_ptr(sizeof(struct chat_info));
  1111. dcc[i].u.relay->sock = sock;
  1112. dcc[i].u.relay->port = dcc[i].port;
  1113. dcc[i].u.relay->chat->away = NULL;
  1114. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1115. dcc[i].u.relay->chat->con_flags = 0;
  1116. dcc[i].u.relay->chat->buffer = NULL;
  1117. dcc[i].u.relay->chat->max_line = 0;
  1118. dcc[i].u.relay->chat->line_count = 0;
  1119. dcc[i].u.relay->chat->current_lines = 0;
  1120. dcc[i].timeval = now;
  1121. if (open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  1122. dcc[i].port) < 0)
  1123. failed_tandem_relay(i);
  1124. }
  1125. /* Input from user before connect is ready
  1126. */
  1127. static void pre_relay(int idx, char *buf, register int i)
  1128. {
  1129. register int tidx = (-1);
  1130. for (i = 0; i < dcc_total; i++)
  1131. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1132. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1133. tidx = i;
  1134. break;
  1135. }
  1136. if (tidx < 0) {
  1137. /* Now try to find it among the DNSWAIT sockets instead. */
  1138. for (i = 0; i < dcc_total; i++)
  1139. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1140. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1141. tidx = i;
  1142. break;
  1143. }
  1144. }
  1145. if (tidx < 0) {
  1146. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1147. dcc[idx].sock, dcc[idx].u.relay->sock);
  1148. killsock(dcc[idx].sock);
  1149. lostdcc(idx);
  1150. return;
  1151. }
  1152. if (!egg_strcasecmp(buf, "*bye*")) {
  1153. /* Disconnect */
  1154. struct chat_info *ci = dcc[idx].u.relay->chat;
  1155. dprintf(idx, "%s %s.\n", BOT_ABORTRELAY1, dcc[tidx].nick);
  1156. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, botnetnick);
  1157. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_ABORTRELAY3, dcc[idx].nick,
  1158. dcc[tidx].nick);
  1159. dcc[idx].status = dcc[idx].u.relay->old_status;
  1160. nfree(dcc[idx].u.relay);
  1161. dcc[idx].u.chat = ci;
  1162. dcc[idx].type = &DCC_CHAT;
  1163. killsock(dcc[tidx].sock);
  1164. lostdcc(tidx);
  1165. return;
  1166. }
  1167. }
  1168. /* User disconnected before her relay had finished connecting
  1169. */
  1170. static void failed_pre_relay(int idx)
  1171. {
  1172. register int tidx = (-1), i;
  1173. for (i = 0; i < dcc_total; i++)
  1174. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1175. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1176. tidx = i;
  1177. break;
  1178. }
  1179. if (tidx < 0) {
  1180. /* Now try to find it among the DNSWAIT sockets instead. */
  1181. for (i = 0; i < dcc_total; i++)
  1182. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1183. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1184. tidx = i;
  1185. break;
  1186. }
  1187. }
  1188. if (tidx < 0) {
  1189. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1190. dcc[idx].sock, dcc[idx].u.relay->sock);
  1191. killsock(dcc[idx].sock);
  1192. lostdcc(idx);
  1193. return;
  1194. }
  1195. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1196. dcc[idx].host, dcc[idx].port);
  1197. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPINGRELAY, dcc[tidx].nick);
  1198. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1199. if (idx > tidx) {
  1200. int t = tidx;
  1201. tidx = idx;
  1202. idx = t;
  1203. }
  1204. killsock(dcc[tidx].sock);
  1205. lostdcc(tidx);
  1206. } else {
  1207. fatal("Lost my terminal?!", 0);
  1208. }
  1209. killsock(dcc[idx].sock);
  1210. lostdcc(idx);
  1211. }
  1212. static void cont_tandem_relay(int idx, char *buf, register int i)
  1213. {
  1214. register int uidx = (-1);
  1215. struct relay_info *ri;
  1216. for (i = 0; i < dcc_total; i++)
  1217. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1218. (dcc[i].u.relay->sock == dcc[idx].sock))
  1219. uidx = i;
  1220. if (uidx < 0) {
  1221. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1222. dcc[i].sock, dcc[i].u.relay->sock);
  1223. killsock(dcc[i].sock);
  1224. lostdcc(i);
  1225. return;
  1226. }
  1227. dcc[idx].type = &DCC_RELAY;
  1228. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1229. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1230. dprintf(uidx, "%s %s ...\n", BOT_RELAYSUCCESS, dcc[idx].nick);
  1231. dprintf(uidx, "%s\n\n", BOT_BYEINFO2);
  1232. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_RELAYLINK,
  1233. dcc[uidx].nick, dcc[idx].nick);
  1234. ri = dcc[uidx].u.relay; /* YEAH */
  1235. dcc[uidx].type = &DCC_CHAT;
  1236. dcc[uidx].u.chat = ri->chat;
  1237. if (dcc[uidx].u.chat->channel >= 0) {
  1238. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s %s\n",
  1239. dcc[uidx].nick, BOT_PARTYLEFT);
  1240. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1241. botnet_send_part_idx(uidx, NULL);
  1242. check_tcl_chpt(botnetnick, dcc[uidx].nick, dcc[uidx].sock,
  1243. dcc[uidx].u.chat->channel);
  1244. }
  1245. check_tcl_chof(dcc[uidx].nick, dcc[uidx].sock);
  1246. dcc[uidx].type = &DCC_RELAYING;
  1247. dcc[uidx].u.relay = ri;
  1248. }
  1249. static void eof_dcc_relay(int idx)
  1250. {
  1251. register int j;
  1252. struct chat_info *ci;
  1253. for (j = 0; j < dcc_total; j++)
  1254. if (dcc[j].sock == dcc[idx].u.relay->sock)
  1255. break;
  1256. if (j == dcc_total) {
  1257. killsock(dcc[idx].sock);
  1258. lostdcc(idx);
  1259. return;
  1260. }
  1261. dcc[j].status = dcc[j].u.relay->old_status;
  1262. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1263. if (dcc[j].status & STAT_TELNET)
  1264. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1265. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_ENDRELAY1, dcc[j].nick,
  1266. dcc[idx].nick);
  1267. dprintf(j, "\n\n*** %s %s\n", BOT_ENDRELAY2, botnetnick);
  1268. ci = dcc[j].u.relay->chat;
  1269. nfree(dcc[j].u.relay);
  1270. dcc[j].u.chat = ci;
  1271. dcc[j].type = &DCC_CHAT;
  1272. if (dcc[j].u.chat->channel >= 0) {
  1273. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1274. dcc[j].nick, BOT_PARTYREJOINED);
  1275. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1276. botnet_send_join_idx(j, -1);
  1277. }
  1278. check_tcl_chon(dcc[j].nick, dcc[j].sock);
  1279. check_tcl_chjn(botnetnick, dcc[j].nick, dcc[j].u.chat->channel,
  1280. geticon(j), dcc[j].sock, dcc[j].host);
  1281. killsock(dcc[idx].sock);
  1282. lostdcc(idx);
  1283. }
  1284. static void eof_dcc_relaying(int idx)
  1285. {
  1286. register int j, x = dcc[idx].u.relay->sock;
  1287. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1288. dcc[idx].host, dcc[idx].port);
  1289. killsock(dcc[idx].sock);
  1290. lostdcc(idx);
  1291. for (j = 0; (dcc[j].sock != x) || (dcc[j].type == &DCC_FORK_RELAY); j++);
  1292. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPEDRELAY, dcc[j].nick);
  1293. killsock(dcc[j].sock);
  1294. lostdcc(j); /* Drop connection to the bot */
  1295. }
  1296. static void dcc_relay(int idx, char *buf, int j)
  1297. {
  1298. unsigned char *p = (unsigned char *) buf;
  1299. int mark;
  1300. for (j = 0; dcc[j].sock != dcc[idx].u.relay->sock ||
  1301. dcc[j].type != &DCC_RELAYING; j++);
  1302. /* If redirecting to a non-telnet user, swallow telnet codes and
  1303. escape sequences. */
  1304. if (!(dcc[j].status & STAT_TELNET)) {
  1305. while (*p != 0) {
  1306. // while (*p != 255 && (*p != '\033' || *(p + 1) != '[') && *p != '\r' && *p)
  1307. while (*p != 255 && *p != '\r' && *p)
  1308. p++; /* Search for IAC, escape sequences and CR. */
  1309. if (*p == 255) {
  1310. mark = 2;
  1311. if (!*(p + 1))
  1312. mark = 1; /* Bogus */
  1313. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1314. mark = 3;
  1315. if (!*(p + 2))
  1316. mark = 2; /* Bogus */
  1317. }
  1318. strcpy((char *) p, (char *) (p + mark));
  1319. /*
  1320. } else if (*p == '\033') {
  1321. unsigned char *e;
  1322. // Search for the end of the escape sequence.
  1323. for (e = p + 2; *e != 'm' && *e; e++)
  1324. ;
  1325. strcpy((char *) p, (char *) (e + 1));
  1326. */
  1327. } else if (*p == '\r')
  1328. strcpy((char *) p, (char *) (p + 1));
  1329. }
  1330. if (!buf[0])
  1331. dprintf(-dcc[idx].u.relay->sock, " \n");
  1332. else
  1333. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1334. return;
  1335. }
  1336. /* Telnet user */
  1337. if (!buf[0])
  1338. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1339. else
  1340. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1341. }
  1342. static void dcc_relaying(int idx, char *buf, int j)
  1343. {
  1344. struct chat_info *ci;
  1345. if (egg_strcasecmp(buf, "*bye*")) {
  1346. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1347. return;
  1348. }
  1349. for (j = 0; (dcc[j].sock != dcc[idx].u.relay->sock) ||
  1350. (dcc[j].type != &DCC_RELAY); j++);
  1351. dcc[idx].status = dcc[idx].u.relay->old_status;
  1352. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1353. if (dcc[idx].status & STAT_TELNET)
  1354. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1355. dprintf(idx, "\n(%s %s.)\n", BOT_BREAKRELAY, dcc[j].nick);
  1356. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, botnetnick);
  1357. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_RELAYBROKEN,
  1358. dcc[idx].nick, dcc[j].nick);
  1359. if (dcc[idx].u.relay->chat->channel >= 0) {
  1360. chanout_but(-1, dcc[idx].u.relay->chat->channel,
  1361. "*** %s joined the party line.\n", dcc[idx].nick);
  1362. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1363. botnet_send_join_idx(idx, -1);
  1364. }
  1365. ci = dcc[idx].u.relay->chat;
  1366. nfree(dcc[idx].u.relay);
  1367. dcc[idx].u.chat = ci;
  1368. dcc[idx].type = &DCC_CHAT;
  1369. check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
  1370. if (dcc[idx].u.chat->channel >= 0)
  1371. check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  1372. geticon(idx), dcc[idx].sock, dcc[idx].host);
  1373. killsock(dcc[j].sock);
  1374. lostdcc(j);
  1375. }
  1376. static void display_relay(int i, char *other)
  1377. {
  1378. sprintf(other, "rela -> sock %d", dcc[i].u.relay->sock);
  1379. }
  1380. static void display_relaying(int i, char *other)
  1381. {
  1382. sprintf(other, ">rly -> sock %d", dcc[i].u.relay->sock);
  1383. }
  1384. static void display_tandem_relay(int i, char *other)
  1385. {
  1386. strcpy(other, "other rela");
  1387. }
  1388. static void display_pre_relay(int i, char *other)
  1389. {
  1390. strcpy(other, "other >rly");
  1391. }
  1392. static int expmem_relay(void *x)
  1393. {
  1394. register struct relay_info *p = (struct relay_info *) x;
  1395. int tot = sizeof(struct relay_info);
  1396. if (p->chat)
  1397. tot += DCC_CHAT.expmem(p->chat);
  1398. return tot;
  1399. }
  1400. static void kill_relay(int idx, void *x)
  1401. {
  1402. register struct relay_info *p = (struct relay_info *) x;
  1403. if (p->chat)
  1404. DCC_CHAT.kill(idx, p->chat);
  1405. nfree(p);
  1406. }
  1407. struct dcc_table DCC_RELAY =
  1408. {
  1409. "RELAY",
  1410. 0, /* Flags */
  1411. eof_dcc_relay,
  1412. dcc_relay,
  1413. NULL,
  1414. NULL,
  1415. display_relay,
  1416. expmem_relay,
  1417. kill_relay,
  1418. NULL
  1419. };
  1420. static void out_relay(int idx, char *buf, void *x)
  1421. {
  1422. register struct relay_info *p = (struct relay_info *) x;
  1423. if (p && p->chat)
  1424. DCC_CHAT.output(idx, buf, p->chat);
  1425. else
  1426. tputs(dcc[idx].sock, buf, strlen(buf));
  1427. }
  1428. struct dcc_table DCC_RELAYING =
  1429. {
  1430. "RELAYING",
  1431. 0, /* Flags */
  1432. eof_dcc_relaying,
  1433. dcc_relaying,
  1434. NULL,
  1435. NULL,
  1436. display_relaying,
  1437. expmem_relay,
  1438. kill_relay,
  1439. out_relay
  1440. };
  1441. struct dcc_table DCC_FORK_RELAY =
  1442. {
  1443. "FORK_RELAY",
  1444. 0, /* Flags */
  1445. failed_tandem_relay,
  1446. cont_tandem_relay,
  1447. &connect_timeout,
  1448. failed_tandem_relay,
  1449. display_tandem_relay,
  1450. expmem_relay,
  1451. kill_relay,
  1452. NULL
  1453. };
  1454. struct dcc_table DCC_PRE_RELAY =
  1455. {
  1456. "PRE_RELAY",
  1457. 0, /* Flags */
  1458. failed_pre_relay,
  1459. pre_relay,
  1460. NULL,
  1461. NULL,
  1462. display_pre_relay,
  1463. expmem_relay,
  1464. kill_relay,
  1465. NULL
  1466. };
  1467. /* Once a minute, send 'ping' to each bot -- no exceptions
  1468. */
  1469. void check_botnet_pings()
  1470. {
  1471. int i;
  1472. int bots, users;
  1473. tand_t *bot;
  1474. for (i = 0; i < dcc_total; i++)
  1475. if (dcc[i].type == &DCC_BOT)
  1476. if (dcc[i].status & STAT_PINGED) {
  1477. char s[1024];
  1478. putlog(LOG_BOTS, "*", "%s: %s", BOT_PINGTIMEOUT, dcc[i].nick);
  1479. bot = findbot(dcc[i].nick);
  1480. bots = bots_in_subtree(bot);
  1481. users = users_in_subtree(bot);
  1482. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_PINGTIMEOUT,
  1483. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1484. users, (users != 1) ? "s" : "");
  1485. chatout("*** %s\n", s);
  1486. botnet_send_unlinked(i, dcc[i].nick, s);
  1487. killsock(dcc[i].sock);
  1488. lostdcc(i);
  1489. }
  1490. for (i = 0; i < dcc_total; i++)
  1491. if (dcc[i].type == &DCC_BOT) {
  1492. botnet_send_ping(i);
  1493. dcc[i].status |= STAT_PINGED;
  1494. }
  1495. for (i = 0; i < dcc_total; i++)
  1496. if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_LEAF)) {
  1497. tand_t *bot, *via = findbot(dcc[i].nick);
  1498. for (bot = tandbot; bot; bot = bot->next) {
  1499. if ((via == bot->via) && (bot != via)) {
  1500. /* Not leaflike behavior */
  1501. if (dcc[i].status & STAT_WARNED) {
  1502. char s[1024];
  1503. putlog(LOG_BOTS, "*", "%s %s (%s).", BOT_DISCONNECTED,
  1504. dcc[i].nick, BOT_BOTNOTLEAFLIKE);
  1505. dprintf(i, "bye %s\n", BOT_BOTNOTLEAFLIKE);
  1506. bot = findbot(dcc[i].nick);
  1507. bots = bots_in_subtree(bot);
  1508. users = users_in_subtree(bot);
  1509. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  1510. BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE,
  1511. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1512. "s" : "");
  1513. chatout("*** %s\n", s);
  1514. botnet_send_unlinked(i, dcc[i].nick, s);
  1515. killsock(dcc[i].sock);
  1516. lostdcc(i);
  1517. } else {
  1518. //fix
  1519. putlog(LOG_MISC, "*", "I am lame, and am now rejecting %s", bot->bot);
  1520. botnet_send_reject(i, botnetnick, NULL, bot->bot,
  1521. NULL, NULL);
  1522. dcc[i].status |= STAT_WARNED;
  1523. }
  1524. } else
  1525. dcc[i].status &= ~STAT_WARNED;
  1526. }
  1527. }
  1528. }
  1529. void zapfbot(int idx)
  1530. {
  1531. char s[1024];
  1532. int bots, users;
  1533. tand_t *bot;
  1534. bot = findbot(dcc[idx].nick);
  1535. bots = bots_in_subtree(bot);
  1536. users = users_in_subtree(bot);
  1537. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_BOTDROPPED,
  1538. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1539. (users != 1) ? "s" : "");
  1540. chatout("*** %s\n", s);
  1541. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1542. killsock(dcc[idx].sock);
  1543. lostdcc(idx);
  1544. }
  1545. void restart_chons()
  1546. {
  1547. int i;
  1548. /* Dump party line members */
  1549. for (i = 0; i < dcc_total; i++) {
  1550. if (dcc[i].type == &DCC_CHAT) {
  1551. check_tcl_chon(dcc[i].nick, dcc[i].sock);
  1552. check_tcl_chjn(botnetnick, dcc[i].nick, dcc[i].u.chat->channel,
  1553. geticon(i), dcc[i].sock, dcc[i].host);
  1554. }
  1555. }
  1556. for (i = 0; i < parties; i++) {
  1557. check_tcl_chjn(party[i].bot, party[i].nick, party[i].chan,
  1558. party[i].flag, party[i].sock, party[i].from);
  1559. }
  1560. }
  1561. static int get_role(char *bot)
  1562. {
  1563. int rl,
  1564. i;
  1565. struct bot_addr *ba;
  1566. int r[5] = { 0, 0, 0, 0, 0 };
  1567. struct userrec *u,
  1568. *u2;
  1569. u2 = get_user_by_handle(userlist, bot);
  1570. if (!u2)
  1571. return 1;
  1572. for (u = userlist; u; u = u->next) {
  1573. if (u->flags & USER_BOT) {
  1574. if (strcmp(u->handle, bot)) {
  1575. ba = get_user(&USERENTRY_BOTADDR, u);
  1576. if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0)
  1577. && (ba->roleid < 5))
  1578. r[(ba->roleid - 1)]++;
  1579. }
  1580. }
  1581. }
  1582. rl = 0;
  1583. for (i = 1; i <= 4; i++)
  1584. if (r[i] < r[rl])
  1585. rl = i;
  1586. rl++;
  1587. ba = get_user(&USERENTRY_BOTADDR, u2);
  1588. if (ba)
  1589. ba->roleid = rl;
  1590. return rl;
  1591. }
  1592. void lower_bot_linked(int idx)
  1593. {
  1594. char tmp[5];
  1595. // int i;
  1596. // botnet_send_logsettings(idx);
  1597. // for (i=0;i<cfg_count;i++)
  1598. // botnet_send_cfg(idx, cfg[i]);
  1599. // send_channel_sync(dcc[idx].nick, NULL);
  1600. sprintf(tmp, STR("rl %d"), get_role(dcc[idx].nick));
  1601. botnet_send_zapf(nextbot(dcc[idx].nick), botnetnick, dcc[idx].nick, tmp);
  1602. }
  1603. void higher_bot_linked(int idx)
  1604. {
  1605. }