botnet.c 44 KB

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