botnet.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739
  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. dcc[i].sock = getsock(SOCK_STRONGCONN,getprotocol(dcc[i].host));
  962. nfree(linker);
  963. if (dcc[i].sock < 0 ||
  964. open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  965. dcc[i].port) < 0)
  966. failed_link(i);
  967. }
  968. static void failed_tandem_relay(int idx)
  969. {
  970. int uidx = (-1), i;
  971. for (i = 0; i < dcc_total; i++)
  972. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  973. (dcc[i].u.relay->sock == dcc[idx].sock))
  974. uidx = i;
  975. if (uidx < 0) {
  976. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  977. dcc[idx].sock, dcc[idx].u.relay->sock);
  978. killsock(dcc[idx].sock);
  979. lostdcc(idx);
  980. return;
  981. }
  982. if (dcc[idx].port >= dcc[idx].u.relay->port + 3) {
  983. struct chat_info *ci = dcc[uidx].u.relay->chat;
  984. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  985. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  986. nfree(dcc[uidx].u.relay);
  987. dcc[uidx].u.chat = ci;
  988. dcc[uidx].type = &DCC_CHAT;
  989. killsock(dcc[idx].sock);
  990. lostdcc(idx);
  991. return;
  992. }
  993. killsock(dcc[idx].sock);
  994. dcc[idx].sock = getsock(SOCK_STRONGCONN,getprotocol(dcc[idx].host));
  995. dcc[uidx].u.relay->sock = dcc[idx].sock;
  996. dcc[idx].port++;
  997. dcc[idx].timeval = now;
  998. if (dcc[idx].sock < 0 ||
  999. open_telnet_raw(dcc[idx].sock, dcc[idx].addr ?
  1000. iptostr(htonl(dcc[idx].addr)) :
  1001. dcc[idx].host, dcc[idx].port) < 0)
  1002. failed_tandem_relay(idx);
  1003. }
  1004. static void tandem_relay_resolve_failure(int);
  1005. static void tandem_relay_resolve_success(int);
  1006. /* Relay to another tandembot
  1007. */
  1008. void tandem_relay(int idx, char *nick, register int i)
  1009. {
  1010. struct userrec *u;
  1011. struct bot_addr *bi;
  1012. struct chat_info *ci;
  1013. u = get_user_by_handle(userlist, nick);
  1014. if (!u || !(u->flags & USER_BOT)) {
  1015. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  1016. return;
  1017. }
  1018. if (!egg_strcasecmp(nick, botnetnick)) {
  1019. dprintf(idx, "%s\n", BOT_CANTRELAYMYSELF);
  1020. return;
  1021. }
  1022. /* Address to connect to is in 'info' */
  1023. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1024. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1025. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  1026. dprintf(idx, "%s .chaddr %s %s\n",
  1027. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  1028. return;
  1029. }
  1030. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1031. if (i < 0) {
  1032. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  1033. return;
  1034. }
  1035. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL,getprotocol(bi->address));
  1036. if (dcc[i].sock < 0) {
  1037. lostdcc(i);
  1038. dprintf(idx, "%s\n", MISC_NOFREESOCK);
  1039. return;
  1040. }
  1041. dcc[i].port = bi->relay_port;
  1042. dcc[i].addr = 0L;
  1043. strcpy(dcc[i].nick, nick);
  1044. dcc[i].user = u;
  1045. strcpy(dcc[i].host, bi->address);
  1046. #ifdef HUB
  1047. dprintf(idx, "%s %s @ %s:%d ...\n", BOT_CONNECTINGTO, nick,
  1048. bi->address, bi->relay_port);
  1049. #endif
  1050. dprintf(idx, "%s\n", BOT_BYEINFO1);
  1051. dcc[idx].type = &DCC_PRE_RELAY;
  1052. ci = dcc[idx].u.chat;
  1053. dcc[idx].u.relay = get_data_ptr(sizeof(struct relay_info));
  1054. dcc[idx].u.relay->chat = ci;
  1055. dcc[idx].u.relay->old_status = dcc[idx].status;
  1056. dcc[idx].u.relay->sock = dcc[i].sock;
  1057. dcc[i].timeval = now;
  1058. dcc[i].u.dns->ibuf = dcc[idx].sock;
  1059. dcc[i].u.dns->host = get_data_ptr(strlen(bi->address) + 1);
  1060. strcpy(dcc[i].u.dns->host, bi->address);
  1061. dcc[i].u.dns->dns_success = tandem_relay_resolve_success;
  1062. dcc[i].u.dns->dns_failure = tandem_relay_resolve_failure;
  1063. dcc[i].u.dns->dns_type = RES_IPBYHOST;
  1064. dcc[i].u.dns->type = &DCC_FORK_RELAY;
  1065. dcc_dnsipbyhost(bi->address);
  1066. }
  1067. static void tandem_relay_resolve_failure(int idx)
  1068. {
  1069. struct chat_info *ci;
  1070. register int uidx = (-1), i;
  1071. for (i = 0; i < dcc_total; i++)
  1072. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1073. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1074. uidx = i;
  1075. break;
  1076. }
  1077. if (uidx < 0) {
  1078. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1079. dcc[idx].sock, dcc[idx].u.relay->sock);
  1080. killsock(dcc[idx].sock);
  1081. lostdcc(idx);
  1082. return;
  1083. }
  1084. ci = dcc[uidx].u.relay->chat;
  1085. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  1086. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  1087. nfree(dcc[uidx].u.relay);
  1088. dcc[uidx].u.chat = ci;
  1089. dcc[uidx].type = &DCC_CHAT;
  1090. killsock(dcc[idx].sock);
  1091. lostdcc(idx);
  1092. }
  1093. static void tandem_relay_resolve_success(int i)
  1094. {
  1095. int sock = dcc[i].u.dns->ibuf;
  1096. dcc[i].addr = dcc[i].u.dns->ip;
  1097. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1098. dcc[i].u.relay->chat = get_data_ptr(sizeof(struct chat_info));
  1099. dcc[i].u.relay->sock = sock;
  1100. dcc[i].u.relay->port = dcc[i].port;
  1101. dcc[i].u.relay->chat->away = NULL;
  1102. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1103. dcc[i].u.relay->chat->con_flags = 0;
  1104. dcc[i].u.relay->chat->buffer = NULL;
  1105. dcc[i].u.relay->chat->max_line = 0;
  1106. dcc[i].u.relay->chat->line_count = 0;
  1107. dcc[i].u.relay->chat->current_lines = 0;
  1108. dcc[i].timeval = now;
  1109. if (open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  1110. dcc[i].port) < 0)
  1111. failed_tandem_relay(i);
  1112. }
  1113. /* Input from user before connect is ready
  1114. */
  1115. static void pre_relay(int idx, char *buf, register int i)
  1116. {
  1117. register int tidx = (-1);
  1118. for (i = 0; i < dcc_total; i++)
  1119. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1120. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1121. tidx = i;
  1122. break;
  1123. }
  1124. if (tidx < 0) {
  1125. /* Now try to find it among the DNSWAIT sockets instead. */
  1126. for (i = 0; i < dcc_total; i++)
  1127. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1128. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1129. tidx = i;
  1130. break;
  1131. }
  1132. }
  1133. if (tidx < 0) {
  1134. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1135. dcc[idx].sock, dcc[idx].u.relay->sock);
  1136. killsock(dcc[idx].sock);
  1137. lostdcc(idx);
  1138. return;
  1139. }
  1140. if (!egg_strcasecmp(buf, "*bye*")) {
  1141. /* Disconnect */
  1142. struct chat_info *ci = dcc[idx].u.relay->chat;
  1143. dprintf(idx, "%s %s.\n", BOT_ABORTRELAY1, dcc[tidx].nick);
  1144. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, botnetnick);
  1145. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_ABORTRELAY3, dcc[idx].nick,
  1146. dcc[tidx].nick);
  1147. dcc[idx].status = dcc[idx].u.relay->old_status;
  1148. nfree(dcc[idx].u.relay);
  1149. dcc[idx].u.chat = ci;
  1150. dcc[idx].type = &DCC_CHAT;
  1151. killsock(dcc[tidx].sock);
  1152. lostdcc(tidx);
  1153. return;
  1154. }
  1155. }
  1156. /* User disconnected before her relay had finished connecting
  1157. */
  1158. static void failed_pre_relay(int idx)
  1159. {
  1160. register int tidx = (-1), i;
  1161. for (i = 0; i < dcc_total; i++)
  1162. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1163. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1164. tidx = i;
  1165. break;
  1166. }
  1167. if (tidx < 0) {
  1168. /* Now try to find it among the DNSWAIT sockets instead. */
  1169. for (i = 0; i < dcc_total; i++)
  1170. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1171. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1172. tidx = i;
  1173. break;
  1174. }
  1175. }
  1176. if (tidx < 0) {
  1177. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1178. dcc[idx].sock, dcc[idx].u.relay->sock);
  1179. killsock(dcc[idx].sock);
  1180. lostdcc(idx);
  1181. return;
  1182. }
  1183. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1184. dcc[idx].host, dcc[idx].port);
  1185. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPINGRELAY, dcc[tidx].nick);
  1186. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1187. if (idx > tidx) {
  1188. int t = tidx;
  1189. tidx = idx;
  1190. idx = t;
  1191. }
  1192. killsock(dcc[tidx].sock);
  1193. lostdcc(tidx);
  1194. } else {
  1195. fatal("Lost my terminal?!", 0);
  1196. }
  1197. killsock(dcc[idx].sock);
  1198. lostdcc(idx);
  1199. }
  1200. static void cont_tandem_relay(int idx, char *buf, register int i)
  1201. {
  1202. register int uidx = (-1);
  1203. struct relay_info *ri;
  1204. for (i = 0; i < dcc_total; i++)
  1205. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1206. (dcc[i].u.relay->sock == dcc[idx].sock))
  1207. uidx = i;
  1208. if (uidx < 0) {
  1209. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1210. dcc[i].sock, dcc[i].u.relay->sock);
  1211. killsock(dcc[i].sock);
  1212. lostdcc(i);
  1213. return;
  1214. }
  1215. dcc[idx].type = &DCC_RELAY;
  1216. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1217. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1218. dprintf(uidx, "%s %s ...\n", BOT_RELAYSUCCESS, dcc[idx].nick);
  1219. dprintf(uidx, "%s\n\n", BOT_BYEINFO2);
  1220. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_RELAYLINK,
  1221. dcc[uidx].nick, dcc[idx].nick);
  1222. ri = dcc[uidx].u.relay; /* YEAH */
  1223. dcc[uidx].type = &DCC_CHAT;
  1224. dcc[uidx].u.chat = ri->chat;
  1225. if (dcc[uidx].u.chat->channel >= 0) {
  1226. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s %s\n",
  1227. dcc[uidx].nick, BOT_PARTYLEFT);
  1228. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1229. botnet_send_part_idx(uidx, NULL);
  1230. check_tcl_chpt(botnetnick, dcc[uidx].nick, dcc[uidx].sock,
  1231. dcc[uidx].u.chat->channel);
  1232. }
  1233. check_tcl_chof(dcc[uidx].nick, dcc[uidx].sock);
  1234. dcc[uidx].type = &DCC_RELAYING;
  1235. dcc[uidx].u.relay = ri;
  1236. }
  1237. static void eof_dcc_relay(int idx)
  1238. {
  1239. register int j;
  1240. struct chat_info *ci;
  1241. for (j = 0; j < dcc_total; j++)
  1242. if (dcc[j].sock == dcc[idx].u.relay->sock)
  1243. break;
  1244. if (j == dcc_total) {
  1245. killsock(dcc[idx].sock);
  1246. lostdcc(idx);
  1247. return;
  1248. }
  1249. dcc[j].status = dcc[j].u.relay->old_status;
  1250. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1251. if (dcc[j].status & STAT_TELNET)
  1252. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1253. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_ENDRELAY1, dcc[j].nick,
  1254. dcc[idx].nick);
  1255. dprintf(j, "\n\n*** %s %s\n", BOT_ENDRELAY2, botnetnick);
  1256. ci = dcc[j].u.relay->chat;
  1257. nfree(dcc[j].u.relay);
  1258. dcc[j].u.chat = ci;
  1259. dcc[j].type = &DCC_CHAT;
  1260. if (dcc[j].u.chat->channel >= 0) {
  1261. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1262. dcc[j].nick, BOT_PARTYREJOINED);
  1263. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1264. botnet_send_join_idx(j, -1);
  1265. }
  1266. check_tcl_chon(dcc[j].nick, dcc[j].sock);
  1267. check_tcl_chjn(botnetnick, dcc[j].nick, dcc[j].u.chat->channel,
  1268. geticon(j), dcc[j].sock, dcc[j].host);
  1269. killsock(dcc[idx].sock);
  1270. lostdcc(idx);
  1271. }
  1272. static void eof_dcc_relaying(int idx)
  1273. {
  1274. register int j, x = dcc[idx].u.relay->sock;
  1275. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1276. dcc[idx].host, dcc[idx].port);
  1277. killsock(dcc[idx].sock);
  1278. lostdcc(idx);
  1279. for (j = 0; (dcc[j].sock != x) || (dcc[j].type == &DCC_FORK_RELAY); j++);
  1280. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPEDRELAY, dcc[j].nick);
  1281. killsock(dcc[j].sock);
  1282. lostdcc(j); /* Drop connection to the bot */
  1283. }
  1284. static void dcc_relay(int idx, char *buf, int j)
  1285. {
  1286. unsigned char *p = (unsigned char *) buf;
  1287. int mark;
  1288. for (j = 0; dcc[j].sock != dcc[idx].u.relay->sock ||
  1289. dcc[j].type != &DCC_RELAYING; j++);
  1290. /* If redirecting to a non-telnet user, swallow telnet codes and
  1291. escape sequences. */
  1292. if (!(dcc[j].status & STAT_TELNET)) {
  1293. while (*p != 0) {
  1294. // while (*p != 255 && (*p != '\033' || *(p + 1) != '[') && *p != '\r' && *p)
  1295. while (*p != 255 && *p != '\r' && *p)
  1296. p++; /* Search for IAC, escape sequences and CR. */
  1297. if (*p == 255) {
  1298. mark = 2;
  1299. if (!*(p + 1))
  1300. mark = 1; /* Bogus */
  1301. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1302. mark = 3;
  1303. if (!*(p + 2))
  1304. mark = 2; /* Bogus */
  1305. }
  1306. strcpy((char *) p, (char *) (p + mark));
  1307. /*
  1308. } else if (*p == '\033') {
  1309. unsigned char *e;
  1310. // Search for the end of the escape sequence.
  1311. for (e = p + 2; *e != 'm' && *e; e++)
  1312. ;
  1313. strcpy((char *) p, (char *) (e + 1));
  1314. */
  1315. } else if (*p == '\r')
  1316. strcpy((char *) p, (char *) (p + 1));
  1317. }
  1318. if (!buf[0])
  1319. dprintf(-dcc[idx].u.relay->sock, " \n");
  1320. else
  1321. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1322. return;
  1323. }
  1324. /* Telnet user */
  1325. if (!buf[0])
  1326. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1327. else
  1328. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1329. }
  1330. static void dcc_relaying(int idx, char *buf, int j)
  1331. {
  1332. struct chat_info *ci;
  1333. if (egg_strcasecmp(buf, "*bye*")) {
  1334. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1335. return;
  1336. }
  1337. for (j = 0; (dcc[j].sock != dcc[idx].u.relay->sock) ||
  1338. (dcc[j].type != &DCC_RELAY); j++);
  1339. dcc[idx].status = dcc[idx].u.relay->old_status;
  1340. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1341. if (dcc[idx].status & STAT_TELNET)
  1342. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1343. dprintf(idx, "\n(%s %s.)\n", BOT_BREAKRELAY, dcc[j].nick);
  1344. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, botnetnick);
  1345. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_RELAYBROKEN,
  1346. dcc[idx].nick, dcc[j].nick);
  1347. if (dcc[idx].u.relay->chat->channel >= 0) {
  1348. chanout_but(-1, dcc[idx].u.relay->chat->channel,
  1349. "*** %s joined the party line.\n", dcc[idx].nick);
  1350. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1351. botnet_send_join_idx(idx, -1);
  1352. }
  1353. ci = dcc[idx].u.relay->chat;
  1354. nfree(dcc[idx].u.relay);
  1355. dcc[idx].u.chat = ci;
  1356. dcc[idx].type = &DCC_CHAT;
  1357. check_tcl_chon(dcc[idx].nick, dcc[idx].sock);
  1358. if (dcc[idx].u.chat->channel >= 0)
  1359. check_tcl_chjn(botnetnick, dcc[idx].nick, dcc[idx].u.chat->channel,
  1360. geticon(idx), dcc[idx].sock, dcc[idx].host);
  1361. killsock(dcc[j].sock);
  1362. lostdcc(j);
  1363. }
  1364. static void display_relay(int i, char *other)
  1365. {
  1366. sprintf(other, "rela -> sock %d", dcc[i].u.relay->sock);
  1367. }
  1368. static void display_relaying(int i, char *other)
  1369. {
  1370. sprintf(other, ">rly -> sock %d", dcc[i].u.relay->sock);
  1371. }
  1372. static void display_tandem_relay(int i, char *other)
  1373. {
  1374. strcpy(other, "other rela");
  1375. }
  1376. static void display_pre_relay(int i, char *other)
  1377. {
  1378. strcpy(other, "other >rly");
  1379. }
  1380. static int expmem_relay(void *x)
  1381. {
  1382. register struct relay_info *p = (struct relay_info *) x;
  1383. int tot = sizeof(struct relay_info);
  1384. if (p->chat)
  1385. tot += DCC_CHAT.expmem(p->chat);
  1386. return tot;
  1387. }
  1388. static void kill_relay(int idx, void *x)
  1389. {
  1390. register struct relay_info *p = (struct relay_info *) x;
  1391. if (p->chat)
  1392. DCC_CHAT.kill(idx, p->chat);
  1393. nfree(p);
  1394. }
  1395. struct dcc_table DCC_RELAY =
  1396. {
  1397. "RELAY",
  1398. 0, /* Flags */
  1399. eof_dcc_relay,
  1400. dcc_relay,
  1401. NULL,
  1402. NULL,
  1403. display_relay,
  1404. expmem_relay,
  1405. kill_relay,
  1406. NULL
  1407. };
  1408. static void out_relay(int idx, char *buf, void *x)
  1409. {
  1410. register struct relay_info *p = (struct relay_info *) x;
  1411. if (p && p->chat)
  1412. DCC_CHAT.output(idx, buf, p->chat);
  1413. else
  1414. tputs(dcc[idx].sock, buf, strlen(buf));
  1415. }
  1416. struct dcc_table DCC_RELAYING =
  1417. {
  1418. "RELAYING",
  1419. 0, /* Flags */
  1420. eof_dcc_relaying,
  1421. dcc_relaying,
  1422. NULL,
  1423. NULL,
  1424. display_relaying,
  1425. expmem_relay,
  1426. kill_relay,
  1427. out_relay
  1428. };
  1429. struct dcc_table DCC_FORK_RELAY =
  1430. {
  1431. "FORK_RELAY",
  1432. 0, /* Flags */
  1433. failed_tandem_relay,
  1434. cont_tandem_relay,
  1435. &connect_timeout,
  1436. failed_tandem_relay,
  1437. display_tandem_relay,
  1438. expmem_relay,
  1439. kill_relay,
  1440. NULL
  1441. };
  1442. struct dcc_table DCC_PRE_RELAY =
  1443. {
  1444. "PRE_RELAY",
  1445. 0, /* Flags */
  1446. failed_pre_relay,
  1447. pre_relay,
  1448. NULL,
  1449. NULL,
  1450. display_pre_relay,
  1451. expmem_relay,
  1452. kill_relay,
  1453. NULL
  1454. };
  1455. /* Once a minute, send 'ping' to each bot -- no exceptions
  1456. */
  1457. void check_botnet_pings()
  1458. {
  1459. int i;
  1460. int bots, users;
  1461. tand_t *bot;
  1462. for (i = 0; i < dcc_total; i++)
  1463. if (dcc[i].type == &DCC_BOT)
  1464. if (dcc[i].status & STAT_PINGED) {
  1465. char s[1024];
  1466. putlog(LOG_BOTS, "*", "%s: %s", BOT_PINGTIMEOUT, dcc[i].nick);
  1467. bot = findbot(dcc[i].nick);
  1468. bots = bots_in_subtree(bot);
  1469. users = users_in_subtree(bot);
  1470. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_PINGTIMEOUT,
  1471. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1472. users, (users != 1) ? "s" : "");
  1473. chatout("*** %s\n", s);
  1474. botnet_send_unlinked(i, dcc[i].nick, s);
  1475. killsock(dcc[i].sock);
  1476. lostdcc(i);
  1477. }
  1478. for (i = 0; i < dcc_total; i++)
  1479. if (dcc[i].type == &DCC_BOT) {
  1480. botnet_send_ping(i);
  1481. dcc[i].status |= STAT_PINGED;
  1482. }
  1483. for (i = 0; i < dcc_total; i++)
  1484. if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_LEAF)) {
  1485. tand_t *bot, *via = findbot(dcc[i].nick);
  1486. for (bot = tandbot; bot; bot = bot->next) {
  1487. if ((via == bot->via) && (bot != via)) {
  1488. /* Not leaflike behavior */
  1489. if (dcc[i].status & STAT_WARNED) {
  1490. char s[1024];
  1491. putlog(LOG_BOTS, "*", "%s %s (%s).", BOT_DISCONNECTED,
  1492. dcc[i].nick, BOT_BOTNOTLEAFLIKE);
  1493. dprintf(i, "bye %s\n", BOT_BOTNOTLEAFLIKE);
  1494. bot = findbot(dcc[i].nick);
  1495. bots = bots_in_subtree(bot);
  1496. users = users_in_subtree(bot);
  1497. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  1498. BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE,
  1499. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1500. "s" : "");
  1501. chatout("*** %s\n", s);
  1502. botnet_send_unlinked(i, dcc[i].nick, s);
  1503. killsock(dcc[i].sock);
  1504. lostdcc(i);
  1505. } else {
  1506. //fix
  1507. putlog(LOG_MISC, "*", "I am lame, and am now rejecting %s", bot->bot);
  1508. botnet_send_reject(i, botnetnick, NULL, bot->bot,
  1509. NULL, NULL);
  1510. dcc[i].status |= STAT_WARNED;
  1511. }
  1512. } else
  1513. dcc[i].status &= ~STAT_WARNED;
  1514. }
  1515. }
  1516. }
  1517. void zapfbot(int idx)
  1518. {
  1519. char s[1024];
  1520. int bots, users;
  1521. tand_t *bot;
  1522. bot = findbot(dcc[idx].nick);
  1523. bots = bots_in_subtree(bot);
  1524. users = users_in_subtree(bot);
  1525. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_BOTDROPPED,
  1526. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1527. (users != 1) ? "s" : "");
  1528. chatout("*** %s\n", s);
  1529. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1530. killsock(dcc[idx].sock);
  1531. lostdcc(idx);
  1532. }
  1533. void restart_chons()
  1534. {
  1535. int i;
  1536. /* Dump party line members */
  1537. for (i = 0; i < dcc_total; i++) {
  1538. if (dcc[i].type == &DCC_CHAT) {
  1539. check_tcl_chon(dcc[i].nick, dcc[i].sock);
  1540. check_tcl_chjn(botnetnick, dcc[i].nick, dcc[i].u.chat->channel,
  1541. geticon(i), dcc[i].sock, dcc[i].host);
  1542. }
  1543. }
  1544. for (i = 0; i < parties; i++) {
  1545. check_tcl_chjn(party[i].bot, party[i].nick, party[i].chan,
  1546. party[i].flag, party[i].sock, party[i].from);
  1547. }
  1548. }
  1549. static int get_role(char *bot)
  1550. {
  1551. int rl,
  1552. i;
  1553. struct bot_addr *ba;
  1554. int r[5] = { 0, 0, 0, 0, 0 };
  1555. struct userrec *u,
  1556. *u2;
  1557. u2 = get_user_by_handle(userlist, bot);
  1558. if (!u2)
  1559. return 1;
  1560. for (u = userlist; u; u = u->next) {
  1561. if (u->flags & USER_BOT) {
  1562. if (strcmp(u->handle, bot)) {
  1563. ba = get_user(&USERENTRY_BOTADDR, u);
  1564. if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0)
  1565. && (ba->roleid < 5))
  1566. r[(ba->roleid - 1)]++;
  1567. }
  1568. }
  1569. }
  1570. rl = 0;
  1571. for (i = 1; i <= 4; i++)
  1572. if (r[i] < r[rl])
  1573. rl = i;
  1574. rl++;
  1575. ba = get_user(&USERENTRY_BOTADDR, u2);
  1576. if (ba)
  1577. ba->roleid = rl;
  1578. return rl;
  1579. }
  1580. void lower_bot_linked(int idx)
  1581. {
  1582. char tmp[5];
  1583. // int i;
  1584. // botnet_send_logsettings(idx);
  1585. // for (i=0;i<cfg_count;i++)
  1586. // botnet_send_cfg(idx, cfg[i]);
  1587. // send_channel_sync(dcc[idx].nick, NULL);
  1588. sprintf(tmp, STR("rl %d"), get_role(dcc[idx].nick));
  1589. botnet_send_zapf(nextbot(dcc[idx].nick), botnetnick, dcc[idx].nick, tmp);
  1590. }
  1591. void higher_bot_linked(int idx)
  1592. {
  1593. }