botnet.c 43 KB

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