botnet.c 45 KB

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