botnet.c 45 KB

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