botnet.c 43 KB

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