botnet.c 44 KB

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