botnet.c 43 KB

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