botnet.c 45 KB

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