botnet.c 45 KB

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