botnet.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706
  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 tell_bots(int idx)
  485. {
  486. char format[81] = "";
  487. int upi = 1, downi = 0;
  488. size_t len;
  489. struct userrec *u = NULL;
  490. len = strlen(conf.bot->nick);
  491. //up = calloc(1, ulen + 1);
  492. //strcpy(up, conf.bot->nick);
  493. //down = calloc(1, 1);
  494. egg_snprintf(format, sizeof format, "%%s%%-11s%%s %%-5s %%-10s %%-11s %%s\n");
  495. dprintf(idx, format, "", "Bot", "", "OS", "Username", "Shell");
  496. dprintf(idx, format, "", "-----------", "", "----------", "-----------", "-----------------------");
  497. /* FIXME: NEED ME! */
  498. for (u = userlist; u; u = u->next) {
  499. if (u->bot && (egg_strcasecmp(u->handle, conf.bot->nick))) {
  500. size_t hlen = strlen(u->handle);
  501. int up = 0;
  502. if (findbot(u->handle)) {
  503. upi++;
  504. up = 1;
  505. } else
  506. downi++;
  507. dprintf(idx, format, up ? GREEN(idx) : RED(idx), u->handle, COLOR_END(idx),
  508. get_user(&USERENTRY_OS, u), get_user(&USERENTRY_USERNAME, u),
  509. get_user(&USERENTRY_NODENAME, u));
  510. // if (up) {
  511. }
  512. }
  513. /*
  514. if (!downi)
  515. dprintf(idx, "Bots up (%d) [%sall%s]: %s\n", upi, BOLD(idx), BOLD_END(idx), up);
  516. else
  517. dprintf(idx, "Bots up (%d): %s\n", upi, up);
  518. if (downi)
  519. dprintf(idx, "Bots down (%d): %s\n", downi, down);
  520. dprintf(idx, "Total: %d [%s%d%% up%s, %s%d%% down%s]\n", upi + downi, GREEN(idx), (100 * upi / (upi + downi)),
  521. COLOR_END(idx), RED(idx), (100 * downi / (upi + downi)), COLOR_END(idx));
  522. free(up);
  523. free(down);
  524. */
  525. }
  526. /* Show a simpleton bot tree
  527. */
  528. void tell_bottree(int idx)
  529. {
  530. char s[161] = "", work[1024] = "";
  531. tand_t *last[20] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  532. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  533. tand_t *this = NULL, *bot = NULL, *bot2 = NULL;
  534. int lev = 0, more = 1, mark[20], ok, cnt, i = 0, imark, tothops = 0;
  535. if (tands == 0) {
  536. dprintf(idx, "%s\n", BOT_NOBOTSLINKED);
  537. return;
  538. }
  539. for (bot = tandbot; bot; bot = bot->next)
  540. if (!bot->uplink) {
  541. if (i) {
  542. s[i++] = ',';
  543. s[i++] = ' ';
  544. }
  545. strcpy(s + i, bot->bot);
  546. i += strlen(bot->bot);
  547. }
  548. if (s[0])
  549. dprintf(idx, "(%s %s)\n", BOT_NOTRACEINFO, s);
  550. dprintf(idx, "%s%s%s (%s %li)\n", localhub ? RED(idx) : "",
  551. conf.bot->nick,
  552. localhub ? COLOR_END(idx) : "",
  553. egg_version, buildts);
  554. this = (tand_t *) 1;
  555. work[0] = 0;
  556. while (more) {
  557. if (lev == 20) {
  558. dprintf(idx, "\n%s\n", BOT_COMPLEXTREE);
  559. return;
  560. }
  561. cnt = 0;
  562. tothops += lev;
  563. for (bot = tandbot; bot; bot = bot->next)
  564. if (bot->uplink == this)
  565. cnt++;
  566. if (cnt) {
  567. imark = 0;
  568. for (i = 0; i < lev; i++) {
  569. if (mark[i])
  570. strcpy(work + imark, " | ");
  571. else
  572. strcpy(work + imark, " ");
  573. imark += 5;
  574. }
  575. if (cnt > 1)
  576. strcpy(work + imark, " |-");
  577. else
  578. strcpy(work + imark, " `-");
  579. s[0] = 0;
  580. bot = tandbot;
  581. while (!s[0]) {
  582. if (bot->uplink == this) {
  583. if (bot->share)
  584. i = sprintf(s, "%c", bot->share);
  585. else
  586. i = sprintf(s, "-");
  587. i = sprintf(s + 1, "%s%s%s (%s %li)", bot->localhub ? RED(idx) : "",
  588. bot->bot,
  589. bot->localhub ? COLOR_END(idx) : "",
  590. bot->version, bot->buildts);
  591. } else
  592. bot = bot->next;
  593. }
  594. dprintf(idx, "%s%s\n", work, s);
  595. if (cnt > 1)
  596. mark[lev] = 1;
  597. else
  598. mark[lev] = 0;
  599. work[0] = 0;
  600. last[lev] = this;
  601. this = bot;
  602. lev++;
  603. more = 1;
  604. } else {
  605. while (cnt == 0) {
  606. /* No subtrees from here */
  607. if (lev == 0) {
  608. dprintf(idx, "(( tree error ))\n");
  609. return;
  610. }
  611. ok = 0;
  612. for (bot = tandbot; bot; bot = bot->next) {
  613. if (bot->uplink == last[lev - 1]) {
  614. if (this == bot)
  615. ok = 1;
  616. else if (ok) {
  617. cnt++;
  618. if (cnt == 1) {
  619. bot2 = bot;
  620. if (bot->share)
  621. i = sprintf(s, "%c", bot->share);
  622. else
  623. i = sprintf(s, "-");
  624. i = sprintf(s + 1, "%s%s%s (%s %li)", bot->localhub ? RED(idx) : "",
  625. bot->bot,
  626. bot->localhub ? COLOR_END(idx) : "",
  627. bot->version, bot->buildts);
  628. }
  629. }
  630. }
  631. }
  632. if (cnt) {
  633. imark = 0;
  634. for (i = 1; i < lev; i++) {
  635. if (mark[i - 1])
  636. strcpy(work + imark, " | ");
  637. else
  638. strcpy(work + imark, " ");
  639. imark += 5;
  640. }
  641. more = 1;
  642. if (cnt > 1)
  643. dprintf(idx, "%s |-%s\n", work, s);
  644. else
  645. dprintf(idx, "%s `-%s\n", work, s);
  646. this = bot2;
  647. work[0] = 0;
  648. if (cnt > 1)
  649. mark[lev - 1] = 1;
  650. else
  651. mark[lev - 1] = 0;
  652. } else {
  653. /* This was the last child */
  654. lev--;
  655. if (lev == 0) {
  656. more = 0;
  657. cnt = 999;
  658. } else {
  659. more = 1;
  660. this = last[lev];
  661. }
  662. }
  663. }
  664. }
  665. }
  666. /* Hop information: (9d) */
  667. dprintf(idx, "Average hops: %3.1f, total bots: %d\n", ((float) tothops) / ((float) tands), tands + 1);
  668. }
  669. #endif /* HUB */
  670. /* Dump list of links to a new bot
  671. */
  672. void dump_links(int z)
  673. {
  674. register int i;
  675. register size_t l;
  676. char x[1024] = "";
  677. #ifdef HUB
  678. tand_t *bot = NULL;
  679. for (bot = tandbot; bot; bot = bot->next) {
  680. char *p = NULL;
  681. if (bot->uplink == (tand_t *) 1)
  682. p = conf.bot->nick;
  683. else
  684. p = bot->uplink->bot;
  685. l = simple_sprintf(x, "n %s %s %cD0gc %d %d %s\n", bot->bot, p, bot->share, bot->localhub,
  686. bot->buildts, bot->version ? bot->version : "");
  687. tputs(dcc[z].sock, x, l);
  688. }
  689. #endif /* HUB */
  690. /* Dump party line members */
  691. for (i = 0; i < dcc_total; i++) {
  692. if (dcc[i].type == &DCC_CHAT) {
  693. if ((dcc[i].u.chat->channel >= 0) && (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
  694. 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);
  695. tputs(dcc[z].sock, x, l);
  696. 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 : "");
  697. tputs(dcc[z].sock, x, l);
  698. }
  699. }
  700. }
  701. for (i = 0; i < parties; i++) {
  702. 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);
  703. tputs(dcc[z].sock, x, l);
  704. if ((party[i].status & PLSTAT_AWAY) || (party[i].timer != 0)) {
  705. 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 : "");
  706. tputs(dcc[z].sock, x, l);
  707. }
  708. }
  709. }
  710. int in_chain(char *who)
  711. {
  712. if (findbot(who))
  713. return 1;
  714. if (!egg_strcasecmp(who, conf.bot->nick))
  715. return 1;
  716. return 0;
  717. }
  718. int bots_in_subtree(tand_t *bot)
  719. {
  720. int nr = 1;
  721. tand_t *b = NULL;
  722. if (!bot)
  723. return 0;
  724. for (b = tandbot; b; b = b->next) {
  725. if (b->bot && (b->uplink == bot)) {
  726. nr += bots_in_subtree(b);
  727. }
  728. }
  729. return nr;
  730. }
  731. int users_in_subtree(tand_t *bot)
  732. {
  733. int i, nr;
  734. tand_t *b = NULL;
  735. nr = 0;
  736. if (!bot)
  737. return 0;
  738. for (i = 0; i < parties; i++)
  739. if (!egg_strcasecmp(party[i].bot, bot->bot))
  740. nr++;
  741. for (b = tandbot; b; b = b->next)
  742. if (b->bot && (b->uplink == bot))
  743. nr += users_in_subtree(b);
  744. return nr;
  745. }
  746. /* Break link with a tandembot
  747. */
  748. int botunlink(int idx, char *nick, char *reason)
  749. {
  750. register int i;
  751. int bots, users;
  752. tand_t *bot = NULL;
  753. if (nick[0] == '*')
  754. dprintf(idx, "%s\n", BOT_UNLINKALL);
  755. for (i = 0; i < dcc_total; i++) {
  756. if ((nick[0] == '*') || !egg_strcasecmp(dcc[i].nick, nick)) {
  757. if (dcc[i].type == &DCC_FORK_BOT) {
  758. if (idx >= 0)
  759. dprintf(idx, "%s: %s -> %s.\n", BOT_KILLLINKATTEMPT,
  760. dcc[i].nick, dcc[i].host);
  761. putlog(LOG_BOTS, "*", "%s: %s -> %s:%d",
  762. BOT_KILLLINKATTEMPT, dcc[i].nick,
  763. dcc[i].host, dcc[i].port);
  764. killsock(dcc[i].sock);
  765. lostdcc(i);
  766. if (nick[0] != '*')
  767. return 1;
  768. } else if (dcc[i].type == &DCC_BOT_NEW) {
  769. if (idx >= 0)
  770. dprintf(idx, "%s %s.\n", BOT_ENDLINKATTEMPT,
  771. dcc[i].nick);
  772. putlog(LOG_BOTS, "*", "%s %s @ %s:%d",
  773. "Stopped trying to link", dcc[i].nick,
  774. dcc[i].host, dcc[i].port);
  775. killsock(dcc[i].sock);
  776. lostdcc(i);
  777. if (nick[0] != '*')
  778. return 1;
  779. } else if (dcc[i].type == &DCC_BOT) {
  780. char s[1024] = "";
  781. if (idx >= 0)
  782. dprintf(idx, "%s %s.\n", BOT_BREAKLINK, dcc[i].nick);
  783. else if ((idx == -3) && (b_status(i) & STAT_SHARE) && !share_unlinks)
  784. return -1;
  785. bot = findbot(dcc[i].nick);
  786. bots = bots_in_subtree(bot);
  787. users = users_in_subtree(bot);
  788. if (reason && reason[0]) {
  789. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  790. BOT_UNLINKEDFROM, dcc[i].nick, reason, bots,
  791. (bots != 1) ? "s" : "", users, (users != 1) ?
  792. "s" : "");
  793. dprintf(i, "bye %s\n", reason);
  794. } else {
  795. simple_sprintf(s, "%s %s (lost %d bot%s and %d user%s)",
  796. BOT_UNLINKEDFROM, dcc[i].nick, bots, (bots != 1) ?
  797. "s" : "", users, (users != 1) ? "s" : "");
  798. dprintf(i, "bye No reason\n");
  799. }
  800. chatout("*** %s\n", s);
  801. botnet_send_unlinked(i, dcc[i].nick, s);
  802. killsock(dcc[i].sock);
  803. lostdcc(i);
  804. if (nick[0] != '*')
  805. return 1;
  806. }
  807. }
  808. }
  809. if (idx >= 0 && nick[0] != '*')
  810. dprintf(idx, "%s\n", BOT_NOTCONNECTED);
  811. if (nick[0] != '*') {
  812. bot = findbot(nick);
  813. if (bot) {
  814. /* The internal bot list is desynched from the dcc list
  815. sometimes. While we still search for the bug, provide
  816. an easy way to clear out those `ghost'-bots.
  817. Fabian (2000-08-02) */
  818. char *ghost = "BUG!!: Found bot `%s' in internal bot list, but it\n"
  819. " shouldn't have been there! Removing.\n"
  820. " This is a known bug we haven't fixed yet. If this\n"
  821. " bot is the newest eggdrop version available and you\n"
  822. " know a *reliable* way to reproduce the bug, please\n"
  823. " contact us - we need your help!\n";
  824. if (idx >= 0)
  825. dprintf(idx, ghost, nick);
  826. else
  827. putlog(LOG_MISC, "*", ghost, nick);
  828. rembot(bot->bot);
  829. return 1;
  830. }
  831. }
  832. if (nick[0] == '*') {
  833. dprintf(idx, "%s\n", BOT_WIPEBOTTABLE);
  834. while (tandbot)
  835. rembot(tandbot->bot);
  836. while (parties) {
  837. parties--;
  838. /* Assert? */
  839. }
  840. }
  841. return 0;
  842. }
  843. static void botlink_resolve_success(int);
  844. static void botlink_resolve_failure(int);
  845. /* Link to another bot
  846. */
  847. int botlink(char *linker, int idx, char *nick)
  848. {
  849. struct bot_addr *bi = NULL;
  850. struct userrec *u = NULL;
  851. register int i;
  852. u = get_user_by_handle(userlist, nick);
  853. if (!u || !u->bot) {
  854. if (idx >= 0)
  855. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  856. } else if (!egg_strcasecmp(nick, conf.bot->nick)) {
  857. if (idx >= 0)
  858. dprintf(idx, "%s\n", BOT_CANTLINKMYSELF);
  859. } else if (in_chain(nick) && (idx != -3)) {
  860. if (idx >= 0)
  861. dprintf(idx, "%s\n", BOT_ALREADYLINKED);
  862. } else {
  863. for (i = 0; i < dcc_total; i++)
  864. if ((dcc[i].user == u) &&
  865. ((dcc[i].type == &DCC_FORK_BOT) ||
  866. (dcc[i].type == &DCC_BOT_NEW))) {
  867. if (idx >= 0)
  868. dprintf(idx, "%s\n", BOT_ALREADYLINKING);
  869. return 0;
  870. }
  871. /* Address to connect to is in 'info' */
  872. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  873. if (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0)) {
  874. if (idx >= 0) {
  875. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  876. dprintf(idx, "%s .chaddr %s %s\n",
  877. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  878. }
  879. } else if (dcc_total == max_dcc) {
  880. if (idx >= 0)
  881. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  882. } else {
  883. correct_handle(nick);
  884. if (idx > -2)
  885. putlog(LOG_BOTS, "*", "%s %s at %s:%d ...", BOT_LINKING, nick,
  886. bi->address, bi->telnet_port);
  887. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  888. dcc[i].timeval = now;
  889. dcc[i].port = bi->telnet_port;
  890. dcc[i].user = u;
  891. strcpy(dcc[i].nick, nick);
  892. strcpy(dcc[i].host, bi->address);
  893. dcc[i].u.dns->ibuf = idx;
  894. dcc[i].u.dns->cptr = calloc(1, strlen(linker) + 1);
  895. strcpy(dcc[i].u.dns->cptr, linker);
  896. dcc[i].u.dns->host = calloc(1, strlen(dcc[i].host) + 1);
  897. strcpy(dcc[i].u.dns->host, dcc[i].host);
  898. dcc[i].u.dns->dns_success = botlink_resolve_success;
  899. dcc[i].u.dns->dns_failure = botlink_resolve_failure;
  900. dcc[i].u.dns->dns_type = RES_IPBYHOST;
  901. dcc[i].u.dns->type = &DCC_FORK_BOT;
  902. #ifdef USE_IPV6
  903. botlink_resolve_success(i);
  904. #else
  905. dcc_dnsipbyhost(bi->address);
  906. #endif /* USE_IPV6 */
  907. return 1;
  908. }
  909. }
  910. return 0;
  911. }
  912. static void botlink_resolve_failure(int i)
  913. {
  914. free(dcc[i].u.dns->cptr);
  915. lostdcc(i);
  916. }
  917. static void botlink_resolve_success(int i)
  918. {
  919. int idx = dcc[i].u.dns->ibuf;
  920. char *linker = dcc[i].u.dns->cptr;
  921. dcc[i].addr = dcc[i].u.dns->ip;
  922. changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
  923. dcc[i].timeval = now;
  924. strcpy(dcc[i].u.bot->linker, linker);
  925. strcpy(dcc[i].u.bot->version, "(primitive bot)");
  926. strcpy(dcc[i].u.bot->sysname, "*");
  927. dcc[i].u.bot->numver = idx;
  928. dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
  929. #ifdef USE_IPV6
  930. dcc[i].sock = getsock(SOCK_STRONGCONN, hostprotocol(dcc[i].host));
  931. #else
  932. dcc[i].sock = getsock(SOCK_STRONGCONN);
  933. #endif /* USE_IPV6 */
  934. free(linker);
  935. if (dcc[i].sock > 0)
  936. identd_open(); /* will be closed when an ident is replied. */
  937. if (dcc[i].sock < 0 ||
  938. #ifdef USE_IPV6
  939. open_telnet_raw(dcc[i].sock, dcc[i].host,
  940. #else
  941. open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  942. #endif /* USE_IPV6 */
  943. dcc[i].port) < 0)
  944. failed_link(i);
  945. else { /* let's attempt to initiate SSL before ANYTHING else... */
  946. #ifdef HAVE_SSL
  947. /* if (!ssl_link(dcc[i].sock, CONNECT_SSL)) {
  948. debug2("back from ssl_link(%d, %d) for botlink (failed)", dcc[i].sock, CONNECT_SSL);
  949. dcc[i].ssl = 0;
  950. putlog(LOG_BOTS, "*", "SSL link for '%s' failed", dcc[i].nick);
  951. } else */
  952. dcc[i].ssl = 1;
  953. #else
  954. dcc[i].ssl = 0;
  955. #endif /* HAVE_SSL */
  956. }
  957. }
  958. static void failed_tandem_relay(int idx)
  959. {
  960. int uidx = (-1), i;
  961. for (i = 0; i < dcc_total; i++)
  962. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  963. (dcc[i].u.relay->sock == dcc[idx].sock))
  964. uidx = i;
  965. if (uidx < 0) {
  966. putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
  967. dcc[idx].sock, dcc[idx].u.relay->sock);
  968. killsock(dcc[idx].sock);
  969. lostdcc(idx);
  970. return;
  971. }
  972. if (dcc[idx].port >= dcc[idx].u.relay->port + 3) {
  973. struct chat_info *ci = dcc[uidx].u.relay->chat;
  974. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  975. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  976. free(dcc[uidx].u.relay);
  977. dcc[uidx].u.chat = ci;
  978. dcc[uidx].type = &DCC_CHAT;
  979. killsock(dcc[idx].sock);
  980. lostdcc(idx);
  981. return;
  982. }
  983. killsock(dcc[idx].sock);
  984. #ifdef USE_IPV6
  985. dcc[idx].sock = getsock(SOCK_STRONGCONN, hostprotocol(dcc[idx].host));
  986. #else
  987. dcc[idx].sock = getsock(SOCK_STRONGCONN);
  988. #endif /* USE_IPV6 */
  989. dcc[uidx].u.relay->sock = dcc[idx].sock;
  990. dcc[idx].port++;
  991. dcc[idx].timeval = now;
  992. if (dcc[idx].sock < 0 ||
  993. #ifdef USE_IPV6
  994. open_telnet_raw(dcc[idx].sock, dcc[idx].host,
  995. #else
  996. open_telnet_raw(dcc[idx].sock, dcc[idx].addr ?
  997. iptostr(htonl(dcc[idx].addr)) :
  998. dcc[idx].host,
  999. #endif /* USE_IPV6 */
  1000. dcc[idx].port) < 0)
  1001. failed_tandem_relay(idx);
  1002. }
  1003. static void tandem_relay_resolve_failure(int);
  1004. static void tandem_relay_resolve_success(int);
  1005. /* Relay to another tandembot
  1006. */
  1007. void tandem_relay(int idx, char *nick, register int i)
  1008. {
  1009. struct userrec *u = NULL;
  1010. struct bot_addr *bi = NULL;
  1011. struct chat_info *ci = NULL;
  1012. u = get_user_by_handle(userlist, nick);
  1013. if (!u || !u->bot) {
  1014. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  1015. return;
  1016. }
  1017. if (!egg_strcasecmp(nick, conf.bot->nick)) {
  1018. dprintf(idx, "%s\n", BOT_CANTRELAYMYSELF);
  1019. return;
  1020. }
  1021. /* Address to connect to is in 'info' */
  1022. bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1023. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1024. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  1025. dprintf(idx, "%s .chaddr %s %s\n",
  1026. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  1027. return;
  1028. }
  1029. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1030. if (i < 0) {
  1031. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  1032. return;
  1033. }
  1034. #ifdef USE_IPV6
  1035. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, hostprotocol(bi->address));
  1036. #else
  1037. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
  1038. #endif /* USE_IPV6 */
  1039. if (dcc[i].sock < 0) {
  1040. lostdcc(i);
  1041. dprintf(idx, "%s\n", MISC_NOFREESOCK);
  1042. return;
  1043. }
  1044. dcc[i].port = bi->relay_port;
  1045. dcc[i].addr = 0L;
  1046. strcpy(dcc[i].nick, nick);
  1047. dcc[i].user = u;
  1048. strcpy(dcc[i].host, bi->address);
  1049. #ifdef HUB
  1050. dprintf(idx, "%s %s @ %s:%d ...\n", BOT_CONNECTINGTO, nick, bi->address, bi->relay_port);
  1051. #endif /* HUB */
  1052. dprintf(idx, "%s\n", BOT_BYEINFO1);
  1053. dcc[idx].type = &DCC_PRE_RELAY;
  1054. ci = dcc[idx].u.chat;
  1055. dcc[idx].u.relay = calloc(1, sizeof(struct relay_info));
  1056. dcc[idx].u.relay->chat = ci;
  1057. dcc[idx].u.relay->old_status = dcc[idx].status;
  1058. dcc[idx].u.relay->sock = dcc[i].sock;
  1059. dcc[i].timeval = now;
  1060. dcc[i].u.dns->ibuf = dcc[idx].sock;
  1061. dcc[i].u.dns->host = calloc(1, strlen(bi->address) + 1);
  1062. strcpy(dcc[i].u.dns->host, bi->address);
  1063. dcc[i].u.dns->dns_success = tandem_relay_resolve_success;
  1064. dcc[i].u.dns->dns_failure = tandem_relay_resolve_failure;
  1065. dcc[i].u.dns->dns_type = RES_IPBYHOST;
  1066. dcc[i].u.dns->type = &DCC_FORK_RELAY;
  1067. #ifdef USE_IPV6
  1068. tandem_relay_resolve_success(i);
  1069. #else
  1070. dcc_dnsipbyhost(bi->address);
  1071. #endif /* USE_IPV6 */
  1072. }
  1073. static void tandem_relay_resolve_failure(int idx)
  1074. {
  1075. struct chat_info *ci = NULL;
  1076. register int uidx = (-1), i;
  1077. for (i = 0; i < dcc_total; i++)
  1078. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1079. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1080. uidx = i;
  1081. break;
  1082. }
  1083. if (uidx < 0) {
  1084. putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
  1085. dcc[idx].sock, dcc[idx].u.relay->sock);
  1086. killsock(dcc[idx].sock);
  1087. lostdcc(idx);
  1088. return;
  1089. }
  1090. ci = dcc[uidx].u.relay->chat;
  1091. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  1092. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  1093. free(dcc[uidx].u.relay);
  1094. dcc[uidx].u.chat = ci;
  1095. dcc[uidx].type = &DCC_CHAT;
  1096. killsock(dcc[idx].sock);
  1097. lostdcc(idx);
  1098. }
  1099. static void tandem_relay_resolve_success(int i)
  1100. {
  1101. int sock = dcc[i].u.dns->ibuf;
  1102. dcc[i].addr = dcc[i].u.dns->ip;
  1103. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1104. dcc[i].u.relay->chat = calloc(1, sizeof(struct chat_info));
  1105. dcc[i].u.relay->sock = sock;
  1106. dcc[i].u.relay->port = dcc[i].port;
  1107. dcc[i].u.relay->chat->away = NULL;
  1108. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1109. dcc[i].u.relay->chat->con_flags = 0;
  1110. dcc[i].u.relay->chat->buffer = NULL;
  1111. dcc[i].u.relay->chat->max_line = 0;
  1112. dcc[i].u.relay->chat->line_count = 0;
  1113. dcc[i].u.relay->chat->current_lines = 0;
  1114. dcc[i].timeval = now;
  1115. #ifdef USE_IPV6
  1116. if (open_telnet_raw(dcc[i].sock, dcc[i].host,
  1117. #else
  1118. if (open_telnet_raw(dcc[i].sock, iptostr(htonl(dcc[i].addr)),
  1119. #endif /* USE_IPV6 */
  1120. dcc[i].port) < 0)
  1121. failed_tandem_relay(i);
  1122. }
  1123. /* Input from user before connect is ready
  1124. */
  1125. static void pre_relay(int idx, char *buf, register int i)
  1126. {
  1127. register int tidx = (-1);
  1128. for (i = 0; i < dcc_total; i++)
  1129. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1130. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1131. tidx = i;
  1132. break;
  1133. }
  1134. if (tidx < 0) {
  1135. /* Now try to find it among the DNSWAIT sockets instead. */
  1136. for (i = 0; i < dcc_total; i++)
  1137. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1138. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1139. tidx = i;
  1140. break;
  1141. }
  1142. }
  1143. if (tidx < 0) {
  1144. putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
  1145. dcc[idx].sock, dcc[idx].u.relay->sock);
  1146. killsock(dcc[idx].sock);
  1147. lostdcc(idx);
  1148. return;
  1149. }
  1150. if (!egg_strcasecmp(buf, "*bye*")) {
  1151. /* Disconnect */
  1152. struct chat_info *ci = dcc[idx].u.relay->chat;
  1153. dprintf(idx, "%s %s.\n", BOT_ABORTRELAY1, dcc[tidx].nick);
  1154. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
  1155. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_ABORTRELAY3, dcc[idx].nick,
  1156. dcc[tidx].nick);
  1157. dcc[idx].status = dcc[idx].u.relay->old_status;
  1158. free(dcc[idx].u.relay);
  1159. dcc[idx].u.chat = ci;
  1160. dcc[idx].type = &DCC_CHAT;
  1161. killsock(dcc[tidx].sock);
  1162. lostdcc(tidx);
  1163. return;
  1164. }
  1165. }
  1166. /* User disconnected before her relay had finished connecting
  1167. */
  1168. static void failed_pre_relay(int idx)
  1169. {
  1170. register int tidx = (-1), i;
  1171. for (i = 0; i < dcc_total; i++)
  1172. if ((dcc[i].type == &DCC_FORK_RELAY) &&
  1173. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1174. tidx = i;
  1175. break;
  1176. }
  1177. if (tidx < 0) {
  1178. /* Now try to find it among the DNSWAIT sockets instead. */
  1179. for (i = 0; i < dcc_total; i++)
  1180. if ((dcc[i].type == &DCC_DNSWAIT) &&
  1181. (dcc[i].sock == dcc[idx].u.relay->sock)) {
  1182. tidx = i;
  1183. break;
  1184. }
  1185. }
  1186. if (tidx < 0) {
  1187. putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
  1188. dcc[idx].sock, dcc[idx].u.relay->sock);
  1189. killsock(dcc[idx].sock);
  1190. lostdcc(idx);
  1191. return;
  1192. }
  1193. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1194. dcc[idx].host, dcc[idx].port);
  1195. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPINGRELAY, dcc[tidx].nick);
  1196. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1197. if (idx > tidx) {
  1198. int t = tidx;
  1199. tidx = idx;
  1200. idx = t;
  1201. }
  1202. killsock(dcc[tidx].sock);
  1203. lostdcc(tidx);
  1204. } else {
  1205. fatal("Lost my terminal?!", 0);
  1206. }
  1207. killsock(dcc[idx].sock);
  1208. lostdcc(idx);
  1209. }
  1210. static void cont_tandem_relay(int idx, char *buf, register int i)
  1211. {
  1212. register int uidx = (-1);
  1213. struct relay_info *ri = NULL;
  1214. for (i = 0; i < dcc_total; i++)
  1215. if ((dcc[i].type == &DCC_PRE_RELAY) &&
  1216. (dcc[i].u.relay->sock == dcc[idx].sock))
  1217. uidx = i;
  1218. if (uidx < 0) {
  1219. putlog(LOG_MISC, "*", "%s %li -> %li", BOT_CANTFINDRELAYUSER,
  1220. dcc[i].sock, dcc[i].u.relay->sock);
  1221. killsock(dcc[i].sock);
  1222. lostdcc(i);
  1223. return;
  1224. }
  1225. dcc[idx].type = &DCC_RELAY;
  1226. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1227. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1228. dprintf(uidx, "%s %s ...\n", BOT_RELAYSUCCESS, dcc[idx].nick);
  1229. dprintf(uidx, "%s\n\n", BOT_BYEINFO2);
  1230. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_RELAYLINK,
  1231. dcc[uidx].nick, dcc[idx].nick);
  1232. ri = dcc[uidx].u.relay; /* YEAH */
  1233. dcc[uidx].type = &DCC_CHAT;
  1234. dcc[uidx].u.chat = ri->chat;
  1235. if (dcc[uidx].u.chat->channel >= 0) {
  1236. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s %s\n",
  1237. dcc[uidx].nick, BOT_PARTYLEFT);
  1238. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1239. botnet_send_part_idx(uidx, NULL);
  1240. }
  1241. check_bind_chof(dcc[uidx].nick, uidx);
  1242. dcc[uidx].type = &DCC_RELAYING;
  1243. dcc[uidx].u.relay = ri;
  1244. }
  1245. static void eof_dcc_relay(int idx)
  1246. {
  1247. register int j;
  1248. struct chat_info *ci = NULL;
  1249. for (j = 0; j < dcc_total; j++)
  1250. if (dcc[j].sock == dcc[idx].u.relay->sock)
  1251. break;
  1252. if (j == dcc_total) {
  1253. killsock(dcc[idx].sock);
  1254. lostdcc(idx);
  1255. return;
  1256. }
  1257. dcc[j].status = dcc[j].u.relay->old_status;
  1258. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1259. if (dcc[j].status & STAT_TELNET)
  1260. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1261. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_ENDRELAY1, dcc[j].nick,
  1262. dcc[idx].nick);
  1263. dprintf(j, "\n\n*** %s %s\n", BOT_ENDRELAY2, conf.bot->nick);
  1264. ci = dcc[j].u.relay->chat;
  1265. free(dcc[j].u.relay);
  1266. dcc[j].u.chat = ci;
  1267. dcc[j].type = &DCC_CHAT;
  1268. if (dcc[j].u.chat->channel >= 0) {
  1269. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1270. dcc[j].nick, BOT_PARTYREJOINED);
  1271. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1272. botnet_send_join_idx(j, -1);
  1273. }
  1274. check_bind_chon(dcc[j].nick, j);
  1275. killsock(dcc[idx].sock);
  1276. lostdcc(idx);
  1277. }
  1278. static void eof_dcc_relaying(int idx)
  1279. {
  1280. register int j, x = dcc[idx].u.relay->sock;
  1281. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1282. dcc[idx].host, dcc[idx].port);
  1283. killsock(dcc[idx].sock);
  1284. lostdcc(idx);
  1285. for (j = 0; (dcc[j].sock != x) || (dcc[j].type == &DCC_FORK_RELAY); j++);
  1286. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPEDRELAY, dcc[j].nick);
  1287. killsock(dcc[j].sock);
  1288. lostdcc(j); /* Drop connection to the bot */
  1289. }
  1290. static void dcc_relay(int idx, char *buf, int j)
  1291. {
  1292. unsigned char *p = (unsigned char *) buf;
  1293. int mark;
  1294. for (j = 0; dcc[j].sock != dcc[idx].u.relay->sock ||
  1295. dcc[j].type != &DCC_RELAYING; j++);
  1296. /* If redirecting to a non-telnet user, swallow telnet codes and
  1297. escape sequences. */
  1298. if (!(dcc[j].status & STAT_TELNET)) {
  1299. while (*p != 0) {
  1300. while (*p && *p != 255 && *p != '\r')
  1301. p++; /* Search for IAC, escape sequences and CR. */
  1302. if (*p == 255) {
  1303. mark = 2;
  1304. if (!*(p + 1))
  1305. mark = 1; /* Bogus */
  1306. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1307. mark = 3;
  1308. if (!*(p + 2))
  1309. mark = 2; /* Bogus */
  1310. }
  1311. strcpy((char *) p, (char *) (p + mark));
  1312. } else if (*p == '\r')
  1313. strcpy((char *) p, (char *) (p + 1));
  1314. }
  1315. if (!buf[0])
  1316. dprintf(-dcc[idx].u.relay->sock, " \n");
  1317. else
  1318. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1319. return;
  1320. }
  1321. /* Telnet user */
  1322. if (!buf[0])
  1323. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1324. else
  1325. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1326. }
  1327. static void dcc_relaying(int idx, char *buf, int j)
  1328. {
  1329. struct chat_info *ci = NULL;
  1330. if (egg_strcasecmp(buf, "*bye*")) {
  1331. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1332. return;
  1333. }
  1334. for (j = 0; (dcc[j].sock != dcc[idx].u.relay->sock) ||
  1335. (dcc[j].type != &DCC_RELAY); j++);
  1336. dcc[idx].status = dcc[idx].u.relay->old_status;
  1337. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1338. if (dcc[idx].status & STAT_TELNET)
  1339. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1340. dprintf(idx, "\n(%s %s.)\n", BOT_BREAKRELAY, dcc[j].nick);
  1341. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
  1342. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_RELAYBROKEN,
  1343. dcc[idx].nick, dcc[j].nick);
  1344. if (dcc[idx].u.relay->chat->channel >= 0) {
  1345. chanout_but(-1, dcc[idx].u.relay->chat->channel,
  1346. "*** %s joined the party line.\n", dcc[idx].nick);
  1347. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1348. botnet_send_join_idx(idx, -1);
  1349. }
  1350. ci = dcc[idx].u.relay->chat;
  1351. free(dcc[idx].u.relay);
  1352. dcc[idx].u.chat = ci;
  1353. dcc[idx].type = &DCC_CHAT;
  1354. check_bind_chon(dcc[idx].nick, idx);
  1355. killsock(dcc[j].sock);
  1356. lostdcc(j);
  1357. }
  1358. static void display_relay(int i, char *other)
  1359. {
  1360. sprintf(other, "rela -> sock %li", dcc[i].u.relay->sock);
  1361. }
  1362. static void display_relaying(int i, char *other)
  1363. {
  1364. sprintf(other, ">rly -> sock %li", dcc[i].u.relay->sock);
  1365. }
  1366. static void display_tandem_relay(int i, char *other)
  1367. {
  1368. strcpy(other, "other rela");
  1369. }
  1370. static void display_pre_relay(int i, char *other)
  1371. {
  1372. strcpy(other, "other >rly");
  1373. }
  1374. static void kill_relay(int idx, void *x)
  1375. {
  1376. register struct relay_info *p = (struct relay_info *) x;
  1377. if (p->chat)
  1378. DCC_CHAT.kill(idx, p->chat);
  1379. free(p);
  1380. }
  1381. struct dcc_table DCC_RELAY =
  1382. {
  1383. "RELAY",
  1384. 0, /* Flags */
  1385. eof_dcc_relay,
  1386. dcc_relay,
  1387. NULL,
  1388. NULL,
  1389. display_relay,
  1390. kill_relay,
  1391. NULL,
  1392. NULL
  1393. };
  1394. static void out_relay(int idx, char *buf, void *x)
  1395. {
  1396. register struct relay_info *p = (struct relay_info *) x;
  1397. if (p && p->chat)
  1398. DCC_CHAT.output(idx, buf, p->chat);
  1399. else
  1400. tputs(dcc[idx].sock, buf, strlen(buf));
  1401. }
  1402. struct dcc_table DCC_RELAYING =
  1403. {
  1404. "RELAYING",
  1405. 0, /* Flags */
  1406. eof_dcc_relaying,
  1407. dcc_relaying,
  1408. NULL,
  1409. NULL,
  1410. display_relaying,
  1411. kill_relay,
  1412. out_relay,
  1413. NULL
  1414. };
  1415. struct dcc_table DCC_FORK_RELAY =
  1416. {
  1417. "FORK_RELAY",
  1418. 0, /* Flags */
  1419. failed_tandem_relay,
  1420. cont_tandem_relay,
  1421. &connect_timeout,
  1422. failed_tandem_relay,
  1423. display_tandem_relay,
  1424. kill_relay,
  1425. NULL,
  1426. NULL
  1427. };
  1428. struct dcc_table DCC_PRE_RELAY =
  1429. {
  1430. "PRE_RELAY",
  1431. 0, /* Flags */
  1432. failed_pre_relay,
  1433. pre_relay,
  1434. NULL,
  1435. NULL,
  1436. display_pre_relay,
  1437. kill_relay,
  1438. NULL,
  1439. NULL
  1440. };
  1441. /* Once a minute, send 'ping' to each bot -- no exceptions
  1442. */
  1443. void check_botnet_pings()
  1444. {
  1445. int i;
  1446. int bots, users;
  1447. tand_t *bot = NULL;
  1448. for (i = 0; i < dcc_total; i++) {
  1449. if (dcc[i].type == &DCC_BOT)
  1450. if (dcc[i].status & STAT_PINGED) {
  1451. char s[1024] = "";
  1452. putlog(LOG_BOTS, "*", "%s: %s", BOT_PINGTIMEOUT, dcc[i].nick);
  1453. bot = findbot(dcc[i].nick);
  1454. bots = bots_in_subtree(bot);
  1455. users = users_in_subtree(bot);
  1456. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_PINGTIMEOUT,
  1457. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1458. users, (users != 1) ? "s" : "");
  1459. chatout("*** %s\n", s);
  1460. botnet_send_unlinked(i, dcc[i].nick, s);
  1461. killsock(dcc[i].sock);
  1462. lostdcc(i);
  1463. }
  1464. }
  1465. for (i = 0; i < dcc_total; i++) {
  1466. if (dcc[i].type == &DCC_BOT) {
  1467. botnet_send_ping(i);
  1468. dcc[i].status |= STAT_PINGED;
  1469. }
  1470. }
  1471. for (i = 0; i < dcc_total; i++) {
  1472. if ((dcc[i].type == &DCC_BOT) && (dcc[i].status & STAT_LEAF)) {
  1473. tand_t *via = findbot(dcc[i].nick);
  1474. for (bot = tandbot; bot; bot = bot->next) {
  1475. if ((via == bot->via) && (bot != via)) {
  1476. /* Not leaflike behavior */
  1477. if (dcc[i].status & STAT_WARNED) {
  1478. char s[1024] = "";
  1479. putlog(LOG_BOTS, "*", "%s %s (%s).", BOT_DISCONNECTED,
  1480. dcc[i].nick, BOT_BOTNOTLEAFLIKE);
  1481. dprintf(i, "bye %s\n", BOT_BOTNOTLEAFLIKE);
  1482. bot = findbot(dcc[i].nick);
  1483. bots = bots_in_subtree(bot);
  1484. users = users_in_subtree(bot);
  1485. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  1486. BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE,
  1487. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1488. "s" : "");
  1489. chatout("*** %s\n", s);
  1490. botnet_send_unlinked(i, dcc[i].nick, s);
  1491. killsock(dcc[i].sock);
  1492. lostdcc(i);
  1493. } else {
  1494. putlog(LOG_MISC, "*", "I am lame, and am now rejecting %s", bot->bot);
  1495. botnet_send_reject(i, conf.bot->nick, NULL, bot->bot,
  1496. NULL, NULL);
  1497. dcc[i].status |= STAT_WARNED;
  1498. }
  1499. } else
  1500. dcc[i].status &= ~STAT_WARNED;
  1501. }
  1502. }
  1503. }
  1504. #ifdef HUB
  1505. #ifdef S_AUTOLOCK
  1506. check_should_lock();
  1507. #endif /* S_AUTOLOCK */
  1508. #ifdef G_BACKUP
  1509. check_should_backup();
  1510. #endif /* G_BACKUP */
  1511. #endif /* HUB */
  1512. }
  1513. void zapfbot(int idx)
  1514. {
  1515. char s[1024] = "";
  1516. int bots, users;
  1517. tand_t *bot = NULL;
  1518. bot = findbot(dcc[idx].nick);
  1519. bots = bots_in_subtree(bot);
  1520. users = users_in_subtree(bot);
  1521. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_BOTDROPPED,
  1522. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1523. (users != 1) ? "s" : "");
  1524. chatout("*** %s\n", s);
  1525. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1526. killsock(dcc[idx].sock);
  1527. lostdcc(idx);
  1528. }
  1529. static int get_role(char *bot)
  1530. {
  1531. int rl, i;
  1532. struct bot_addr *ba = NULL;
  1533. int r[5] = { 0, 0, 0, 0, 0 };
  1534. struct userrec *u = NULL, *u2 = NULL;
  1535. if (!(u2 = get_user_by_handle(userlist, bot)))
  1536. return 1;
  1537. for (u = userlist; u; u = u->next) {
  1538. if (u->bot) {
  1539. if (strcmp(u->handle, bot)) {
  1540. ba = get_user(&USERENTRY_BOTADDR, u);
  1541. if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0) && (ba->roleid < 5))
  1542. r[(ba->roleid - 1)]++;
  1543. }
  1544. }
  1545. }
  1546. rl = 0;
  1547. for (i = 1; i <= 4; i++)
  1548. if (r[i] < r[rl])
  1549. rl = i;
  1550. rl++;
  1551. ba = get_user(&USERENTRY_BOTADDR, u2);
  1552. if (ba)
  1553. ba->roleid = rl;
  1554. return rl;
  1555. }
  1556. void lower_bot_linked(int idx)
  1557. {
  1558. char tmp[6] = "";
  1559. egg_snprintf(tmp, sizeof tmp, "rl %d", get_role(dcc[idx].nick));
  1560. putbot(dcc[idx].nick, tmp);
  1561. }