botnet.c 45 KB

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