botnet.c 50 KB

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