botnet.c 49 KB

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