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 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. }
  429. }
  430. }
  431. for (i = 0; i < parties; i++) {
  432. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  433. c = party[i].flag;
  434. if (c == '-')
  435. c = ' ';
  436. if (party[i].timer == 0L)
  437. strcpy(idle, " [idle?]");
  438. else if (now - party[i].timer > 300) {
  439. unsigned long mydays, hrs, mins;
  440. mydays = (now - party[i].timer) / 86400;
  441. hrs = ((now - party[i].timer) - (mydays * 86400)) / 3600;
  442. mins = ((now - party[i].timer) - (hrs * 3600)) / 60;
  443. if (mydays > 0)
  444. sprintf(idle, " [idle %lud%luh]", mydays, hrs);
  445. else if (hrs > 0)
  446. sprintf(idle, " [idle %luh%lum]", hrs, mins);
  447. else
  448. sprintf(idle, " [idle %lum]", mins);
  449. } else
  450. idle[0] = 0;
  451. total++;
  452. if (conf.bot->hub)
  453. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ',
  454. party[i].bot, party[i].from, idle);
  455. else
  456. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ', idle);
  457. if (party[i].status & PLSTAT_AWAY)
  458. dprintf(idx, " %s: %s\n", MISC_AWAY,
  459. party[i].away ? party[i].away : "");
  460. }
  461. }
  462. dprintf(idx, "Total users: %d\n", total);
  463. }
  464. /* Show z a list of all bots connected
  465. */
  466. void
  467. tell_bots(int idx, int up, const char *nodename)
  468. {
  469. struct userrec *u = NULL;
  470. int cnt = 0, tot = 0, total = 1, mtot = 0;
  471. char work[151] = "", *node = NULL;
  472. if (up) {
  473. if (nodename)
  474. node = (char *) get_user(&USERENTRY_NODENAME, conf.bot->u);
  475. if (!nodename || wild_match(nodename, node)) {
  476. strcat(work, conf.bot->nick);
  477. strcat(work, " ");
  478. cnt++;
  479. tot++;
  480. if (nodename)
  481. mtot++;
  482. }
  483. }
  484. for (u = userlist; u; u = u->next) {
  485. if (u->bot) {
  486. if (egg_strcasecmp(u->handle, conf.bot->nick)) {
  487. bool found = 0;
  488. if (findbot(u->handle))
  489. found = 1;
  490. total++;
  491. if (nodename || (!nodename && ((!up && !found) || (up && found)))) {
  492. if (nodename)
  493. node = (char *) get_user(&USERENTRY_NODENAME, u);
  494. if (!nodename || wild_match(nodename, node)) {
  495. if (nodename && !found)
  496. strcat(work, "*");
  497. strcat(work, u->handle);
  498. cnt++;
  499. if (nodename)
  500. mtot++;
  501. if (!nodename || (nodename && found))
  502. tot++;
  503. if (cnt == 11) {
  504. dprintf(idx, "%s bots: %s\n", nodename ? "Matching" : up ? "Up" : "Down", work);
  505. work[0] = 0;
  506. cnt = 0;
  507. } else {
  508. strcat(work, " ");
  509. }
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if (work[0])
  516. dprintf(idx, "%s bot%s: %s\n", nodename ? "Matching" : up ? "Up" : "Down", cnt > 1 ? "s" : "", work);
  517. if (nodename)
  518. dprintf(idx, "(Total Matching: %d/%d)\n", mtot, total);
  519. dprintf(idx, "(Total %s: %d/%d)\n", nodename ? "up" : up ? "up" : "down", tot, nodename ? mtot : total);
  520. }
  521. /* Show a simpleton bot tree
  522. */
  523. void tell_bottree(int idx)
  524. {
  525. if (tands == 0) {
  526. dprintf(idx, "%s\n", BOT_NOBOTSLINKED);
  527. return;
  528. }
  529. char s[161] = "", work[1024] = "", *color_str = NULL;
  530. tand_t *last[20] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  531. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  532. tand_t *thisbot = NULL, *bot = NULL, *bot2 = NULL;
  533. int lev = 0, more = 1, mark[20], ok, cnt, i = 0, imark, tothops = 0;
  534. struct userrec *botu = NULL;
  535. for (bot = tandbot; bot; bot = bot->next)
  536. if (!bot->uplink) {
  537. if (i) {
  538. s[i++] = ',';
  539. s[i++] = ' ';
  540. }
  541. strcpy(s + i, bot->bot);
  542. i += strlen(bot->bot);
  543. }
  544. if (bot_hublevel(conf.bot->u) < 999)
  545. color_str = (char *) YELLOW(idx);
  546. else if (conf.bot->localhub)
  547. color_str = (char *) RED(idx);
  548. else
  549. color_str = (char *) NULL;
  550. if (s[0])
  551. dprintf(idx, "(%s %s)\n", BOT_NOTRACEINFO, s);
  552. dprintf(idx, "%s%s%s (%s %li)\n", color_str ? color_str : "",
  553. conf.bot->nick,
  554. color_str ? COLOR_END(idx) : "",
  555. egg_version, buildts);
  556. thisbot = (tand_t *) 1;
  557. work[0] = 0;
  558. while (more) {
  559. if (lev == 20) {
  560. dprintf(idx, "\n%s\n", BOT_COMPLEXTREE);
  561. return;
  562. }
  563. cnt = 0;
  564. tothops += lev;
  565. for (bot = tandbot; bot; bot = bot->next)
  566. if (bot->uplink == thisbot)
  567. cnt++;
  568. if (cnt) {
  569. imark = 0;
  570. for (i = 0; i < lev; i++) {
  571. if (mark[i])
  572. strcpy(work + imark, " | ");
  573. else
  574. strcpy(work + imark, " ");
  575. imark += 5;
  576. }
  577. if (cnt > 1)
  578. strcpy(work + imark, " |-");
  579. else
  580. strcpy(work + imark, " `-");
  581. s[0] = 0;
  582. bot = tandbot;
  583. while (!s[0]) {
  584. if (bot->uplink == thisbot) {
  585. botu = get_user_by_handle(userlist, bot->bot);
  586. if (botu && bot_hublevel(botu) < 999)
  587. color_str = (char *) YELLOW(idx);
  588. else if (botu && bot->localhub)
  589. color_str = (char *) RED(idx);
  590. else
  591. color_str = (char *) NULL;
  592. if (bot->share)
  593. i = simple_sprintf(s, "%c", bot->share);
  594. else
  595. i = simple_sprintf(s, "-");
  596. i = sprintf(s + 1, "%s%s%s (%s %li)", color_str ? color_str : "",
  597. bot->bot,
  598. color_str ? COLOR_END(idx) : "",
  599. bot->version, bot->buildts);
  600. } else
  601. bot = bot->next;
  602. }
  603. dprintf(idx, "%s%s\n", work, s);
  604. if (cnt > 1)
  605. mark[lev] = 1;
  606. else
  607. mark[lev] = 0;
  608. work[0] = 0;
  609. last[lev] = thisbot;
  610. thisbot = bot;
  611. lev++;
  612. more = 1;
  613. } else {
  614. while (cnt == 0) {
  615. /* No subtrees from here */
  616. if (lev == 0) {
  617. dprintf(idx, "(( tree error ))\n");
  618. return;
  619. }
  620. ok = 0;
  621. for (bot = tandbot; bot; bot = bot->next) {
  622. if (bot->uplink == last[lev - 1]) {
  623. if (thisbot == bot)
  624. ok = 1;
  625. else if (ok) {
  626. cnt++;
  627. if (cnt == 1) {
  628. botu = get_user_by_handle(userlist, bot->bot);
  629. if (botu && bot_hublevel(botu) < 999)
  630. color_str = (char *) YELLOW(idx);
  631. else if (botu && bot->localhub)
  632. color_str = (char *) RED(idx);
  633. else
  634. color_str = (char *) NULL;
  635. bot2 = bot;
  636. if (bot->share)
  637. i = simple_sprintf(s, "%c", bot->share);
  638. else
  639. i = simple_sprintf(s, "-");
  640. i = sprintf(s + 1, "%s%s%s (%s %li)", color_str ? color_str : "",
  641. bot->bot,
  642. color_str ? COLOR_END(idx) : "",
  643. bot->version, bot->buildts);
  644. }
  645. }
  646. }
  647. }
  648. if (cnt) {
  649. imark = 0;
  650. for (i = 1; i < lev; i++) {
  651. if (mark[i - 1])
  652. strcpy(work + imark, " | ");
  653. else
  654. strcpy(work + imark, " ");
  655. imark += 5;
  656. }
  657. more = 1;
  658. if (cnt > 1)
  659. dprintf(idx, "%s |-%s\n", work, s);
  660. else
  661. dprintf(idx, "%s `-%s\n", work, s);
  662. thisbot = bot2;
  663. work[0] = 0;
  664. if (cnt > 1)
  665. mark[lev - 1] = 1;
  666. else
  667. mark[lev - 1] = 0;
  668. } else {
  669. /* This was the last child */
  670. lev--;
  671. if (lev == 0) {
  672. more = 0;
  673. cnt = 999;
  674. } else {
  675. more = 1;
  676. thisbot = last[lev];
  677. }
  678. }
  679. }
  680. }
  681. }
  682. /* Hop information: (9d) */
  683. dprintf(idx, "Average hops: %3.1f, total bots: %d\n", ((float) tothops) / ((float) tands), tands + 1);
  684. }
  685. /* Dump list of links to a new bot
  686. */
  687. void dump_links(int z)
  688. {
  689. register int i;
  690. register size_t l;
  691. char x[1024] = "";
  692. if (conf.bot->hub) {
  693. tand_t *bot = NULL;
  694. char *p = NULL;
  695. for (bot = tandbot; bot; bot = bot->next) {
  696. if (bot->uplink == (tand_t *) 1)
  697. p = conf.bot->nick;
  698. else
  699. p = bot->uplink->bot;
  700. l = simple_sprintf(x, "n %s %s %cD0gc %d %d %s\n", bot->bot, p, bot->share, bot->localhub,
  701. (int) bot->buildts, bot->version ? bot->version : "");
  702. tputs(dcc[z].sock, x, l);
  703. }
  704. }
  705. /* Dump party line members */
  706. for (i = 0; i < dcc_total; i++) {
  707. if (dcc[i].type && dcc[i].type == &DCC_CHAT && !dcc[i].simul) {
  708. if ((dcc[i].u.chat->channel >= 0) && (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
  709. l = simple_sprintf2(x, "j !%s %s %D %c%D %s\n", conf.bot->nick, dcc[i].nick,
  710. dcc[i].u.chat->channel, geticon(i), dcc[i].sock, dcc[i].host);
  711. tputs(dcc[z].sock, x, l);
  712. l = simple_sprintf2(x, "i %s %D %D %s\n", conf.bot->nick,
  713. dcc[i].sock, now - dcc[i].timeval,
  714. dcc[i].u.chat->away ? dcc[i].u.chat->away : "");
  715. tputs(dcc[z].sock, x, l);
  716. }
  717. }
  718. }
  719. for (i = 0; i < parties; i++) {
  720. l = simple_sprintf2(x, "j %s %s %D %c%D %s\n", party[i].bot, party[i].nick,
  721. 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_sprintf2(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. }
  857. }
  858. return 0;
  859. }
  860. static void botlink_dns_callback(int, void *, const char *, char **);
  861. /* Link to another bot
  862. */
  863. int botlink(char *linker, int idx, char *nick)
  864. {
  865. struct userrec *u = get_user_by_handle(userlist, nick);
  866. if (!u || !u->bot) {
  867. if (idx >= 0)
  868. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  869. } else if (!egg_strcasecmp(nick, conf.bot->nick)) {
  870. if (idx >= 0)
  871. dprintf(idx, "%s\n", BOT_CANTLINKMYSELF);
  872. } else if (in_chain(nick) && (idx != -3)) {
  873. if (idx >= 0)
  874. dprintf(idx, "%s\n", BOT_ALREADYLINKED);
  875. } else {
  876. register int i;
  877. for (i = 0; i < dcc_total; i++) {
  878. if (dcc[i].type && (dcc[i].user == u) &&
  879. ((dcc[i].type == &DCC_FORK_BOT) ||
  880. (dcc[i].type == &DCC_BOT_NEW))) {
  881. if (idx >= 0)
  882. dprintf(idx, "%s\n", BOT_ALREADYLINKING);
  883. return 0;
  884. }
  885. }
  886. /* Address to connect to is in 'info' */
  887. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  888. if (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0)) {
  889. if (idx >= 0) {
  890. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  891. dprintf(idx, "%s .chaddr %s %s\n",
  892. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  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, "*", "%s %s at %s:%d ...", BOT_LINKING, 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, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  976. dcc[idx].sock, dcc[idx].u.relay->sock);
  977. killsock(dcc[idx].sock);
  978. lostdcc(idx);
  979. return;
  980. }
  981. if (dcc[idx].port >= dcc[idx].u.relay->port + 3) {
  982. struct chat_info *ci = dcc[uidx].u.relay->chat;
  983. dprintf(uidx, "%s %s.\n", BOT_CANTLINKTO, dcc[idx].nick);
  984. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  985. free(dcc[uidx].u.relay);
  986. dcc[uidx].u.chat = ci;
  987. dcc[uidx].type = &DCC_CHAT;
  988. killsock(dcc[idx].sock);
  989. lostdcc(idx);
  990. return;
  991. }
  992. killsock(dcc[idx].sock);
  993. #ifdef USE_IPV6
  994. dcc[idx].sock = getsock(SOCK_STRONGCONN, hostprotocol(dcc[idx].host));
  995. #else
  996. dcc[idx].sock = getsock(SOCK_STRONGCONN);
  997. #endif /* USE_IPV6 */
  998. dcc[uidx].u.relay->sock = dcc[idx].sock;
  999. dcc[idx].port++;
  1000. dcc[idx].timeval = now;
  1001. if (dcc[idx].sock < 0 ||
  1002. #ifdef USE_IPV6
  1003. open_telnet_raw(dcc[idx].sock, dcc[idx].host,
  1004. #else
  1005. open_telnet_raw(dcc[idx].sock, dcc[idx].addr ?
  1006. iptostr(htonl(dcc[idx].addr)) :
  1007. dcc[idx].host,
  1008. #endif /* USE_IPV6 */
  1009. dcc[idx].port, 0) < 0)
  1010. failed_tandem_relay(idx);
  1011. }
  1012. static void tandem_relay_dns_callback(int, void *, const char *, char **);
  1013. /* Relay to another tandembot
  1014. */
  1015. void tandem_relay(int idx, char *nick, register int i)
  1016. {
  1017. struct userrec *u = get_user_by_handle(userlist, nick);
  1018. if (!u || !u->bot) {
  1019. dprintf(idx, "%s %s\n", nick, BOT_BOTUNKNOWN);
  1020. return;
  1021. }
  1022. if (!egg_strcasecmp(nick, conf.bot->nick)) {
  1023. dprintf(idx, "%s\n", BOT_CANTRELAYMYSELF);
  1024. return;
  1025. }
  1026. /* Address to connect to is in 'info' */
  1027. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1028. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1029. dprintf(idx, "%s '%s'.\n", BOT_NOTELNETADDY, nick);
  1030. dprintf(idx, "%s .chaddr %s %s\n",
  1031. MISC_USEFORMAT, nick, MISC_CHADDRFORMAT);
  1032. return;
  1033. }
  1034. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1035. if (i < 0) {
  1036. dprintf(idx, "%s\n", DCC_TOOMANYDCCS1);
  1037. return;
  1038. }
  1039. dcc[i].port = bi->relay_port;
  1040. dcc[i].addr = 0L;
  1041. strcpy(dcc[i].nick, nick);
  1042. dcc[i].user = u;
  1043. strcpy(dcc[i].host, bi->address);
  1044. if (conf.bot->hub)
  1045. dprintf(idx, "%s %s @ %s:%d ...\n", BOT_CONNECTINGTO, nick, bi->address, bi->relay_port);
  1046. dprintf(idx, "%s\n", BOT_BYEINFO1);
  1047. dcc[idx].type = &DCC_PRE_RELAY;
  1048. struct chat_info *ci = dcc[idx].u.chat;
  1049. dcc[idx].u.relay = (struct relay_info *) my_calloc(1, sizeof(struct relay_info));
  1050. dcc[idx].u.relay->chat = ci;
  1051. dcc[idx].u.relay->old_status = dcc[idx].status;
  1052. dcc[i].timeval = now;
  1053. dcc[i].u.dns->ibuf = idx;
  1054. int dns_id = egg_dns_lookup(bi->address, 20, tandem_relay_dns_callback, (void *) i);
  1055. if (dns_id >= 0)
  1056. dcc[i].dns_id = dns_id;
  1057. return;
  1058. /* wait for async reply */
  1059. }
  1060. static void tandem_relay_dns_callback(int id, void *client_data, const char *host, char **ips)
  1061. {
  1062. int i = (int) client_data, idx = -1;
  1063. if (!valid_dns_id(i, id))
  1064. return;
  1065. if (valid_idx(i))
  1066. idx = dcc[i].u.dns->ibuf;
  1067. if (idx < 0) {
  1068. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER, i, idx);
  1069. lostdcc(i);
  1070. return;
  1071. }
  1072. if (!ips) {
  1073. struct chat_info *ci = dcc[idx].u.relay->chat;
  1074. dprintf(idx, "%s %s.\n", BOT_CANTLINKTO, dcc[i].nick);
  1075. dcc[idx].status = dcc[idx].u.relay->old_status;
  1076. free(dcc[idx].u.relay);
  1077. dcc[idx].u.chat = ci;
  1078. dcc[idx].type = &DCC_CHAT;
  1079. lostdcc(i);
  1080. return;
  1081. }
  1082. #ifdef USE_IPV6
  1083. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, is_dotted_ip(ips[0]));
  1084. #else
  1085. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
  1086. #endif /* USE_IPV6 */
  1087. if (dcc[i].sock < 0) {
  1088. lostdcc(i);
  1089. dprintf(idx, "%s\n", MISC_NOFREESOCK);
  1090. return;
  1091. }
  1092. dcc[idx].u.relay->sock = dcc[i].sock;
  1093. int sock = dcc[idx].u.dns->ibuf;
  1094. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1095. dcc[i].addr = inet_addr(ips[0]);
  1096. dcc[i].u.relay->chat = (struct chat_info *) my_calloc(1, sizeof(struct chat_info));
  1097. dcc[i].u.relay->sock = sock;
  1098. dcc[i].u.relay->port = dcc[i].port;
  1099. dcc[i].u.relay->chat->away = NULL;
  1100. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1101. dcc[i].u.relay->chat->con_flags = 0;
  1102. dcc[i].u.relay->chat->buffer = NULL;
  1103. dcc[i].u.relay->chat->max_line = 0;
  1104. dcc[i].u.relay->chat->line_count = 0;
  1105. dcc[i].u.relay->chat->current_lines = 0;
  1106. dcc[i].timeval = now;
  1107. if (open_telnet_raw(dcc[i].sock, ips[0], dcc[i].port, 0) < 0)
  1108. failed_tandem_relay(i);
  1109. }
  1110. /* Input from user before connect is ready
  1111. */
  1112. static void pre_relay(int idx, char *buf, register int len)
  1113. {
  1114. register int tidx = (-1), i;
  1115. for (i = 0; i < dcc_total; i++) {
  1116. if (dcc[i].type && dcc[i].type == &DCC_FORK_RELAY && !dcc[i].addr && (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1117. tidx = i;
  1118. break;
  1119. }
  1120. }
  1121. if (tidx < 0) {
  1122. /* Now try to find it among the DNSWAIT sockets instead. */
  1123. for (i = 0; i < dcc_total; i++) {
  1124. if (dcc[i].type && (dcc[i].type != &DCC_DNSWAIT || (dcc[i].sock == dcc[idx].u.relay->sock))) {
  1125. tidx = i;
  1126. break;
  1127. }
  1128. }
  1129. }
  1130. if (tidx < 0) {
  1131. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1132. dcc[idx].sock, dcc[idx].u.relay->sock);
  1133. killsock(dcc[idx].sock);
  1134. lostdcc(idx);
  1135. return;
  1136. }
  1137. if (!egg_strcasecmp(buf, "*bye*")) {
  1138. /* Disconnect */
  1139. struct chat_info *ci = dcc[idx].u.relay->chat;
  1140. dprintf(idx, "%s %s.\n", BOT_ABORTRELAY1, dcc[tidx].nick);
  1141. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
  1142. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_ABORTRELAY3, dcc[idx].nick, dcc[tidx].nick);
  1143. dcc[idx].status = dcc[idx].u.relay->old_status;
  1144. free(dcc[idx].u.relay);
  1145. dcc[idx].u.chat = ci;
  1146. dcc[idx].type = &DCC_CHAT;
  1147. killsock(dcc[tidx].sock);
  1148. lostdcc(tidx);
  1149. return;
  1150. }
  1151. }
  1152. /* User disconnected before her relay had finished connecting
  1153. */
  1154. static void failed_pre_relay(int idx)
  1155. {
  1156. register int tidx = (-1), i;
  1157. for (i = 0; i < dcc_total; i++) {
  1158. if (dcc[i].type && (dcc[i].type == &DCC_FORK_RELAY) &&
  1159. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1160. tidx = i;
  1161. break;
  1162. }
  1163. }
  1164. if (tidx < 0) {
  1165. /* Now try to find it among the DNSWAIT sockets instead. */
  1166. for (i = 0; i < dcc_total; i++) {
  1167. if (dcc[i].type && (dcc[i].type != &DCC_DNSWAIT || (dcc[i].sock == dcc[idx].u.relay->sock))) {
  1168. tidx = i;
  1169. break;
  1170. }
  1171. }
  1172. }
  1173. if (tidx < 0) {
  1174. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1175. dcc[idx].sock, dcc[idx].u.relay->sock);
  1176. killsock(dcc[idx].sock);
  1177. lostdcc(idx);
  1178. return;
  1179. }
  1180. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1181. dcc[idx].host, dcc[idx].port);
  1182. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPINGRELAY, dcc[tidx].nick);
  1183. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1184. if (idx > tidx) {
  1185. int t = tidx;
  1186. tidx = idx;
  1187. idx = t;
  1188. }
  1189. killsock(dcc[tidx].sock);
  1190. lostdcc(tidx);
  1191. } else {
  1192. fatal("Lost my terminal?!", 0);
  1193. }
  1194. killsock(dcc[idx].sock);
  1195. lostdcc(idx);
  1196. }
  1197. static void cont_tandem_relay(int idx, char *buf, register int len)
  1198. {
  1199. register int uidx = (-1), i;
  1200. struct relay_info *ri = NULL;
  1201. for (i = 0; i < dcc_total; i++) {
  1202. if (dcc[i].type && (dcc[i].type == &DCC_PRE_RELAY) && (dcc[i].u.relay->sock == dcc[idx].sock))
  1203. uidx = i;
  1204. }
  1205. if (uidx < 0) {
  1206. putlog(LOG_MISC, "*", "%s %d -> %d", BOT_CANTFINDRELAYUSER,
  1207. dcc[idx].sock, dcc[idx].u.relay->sock);
  1208. killsock(dcc[idx].sock);
  1209. lostdcc(idx);
  1210. return;
  1211. }
  1212. dcc[idx].type = &DCC_RELAY;
  1213. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1214. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1215. dprintf(uidx, "%s %s ...\n", BOT_RELAYSUCCESS, dcc[idx].nick);
  1216. dprintf(uidx, "%s\n\n", BOT_BYEINFO2);
  1217. putlog(LOG_MISC, "*", "%s %s -> %s", BOT_RELAYLINK,
  1218. dcc[uidx].nick, dcc[idx].nick);
  1219. ri = dcc[uidx].u.relay;
  1220. dcc[uidx].type = &DCC_CHAT;
  1221. dcc[uidx].u.chat = ri->chat;
  1222. if (dcc[uidx].u.chat->channel >= 0) {
  1223. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s %s\n",
  1224. dcc[uidx].nick, BOT_PARTYLEFT);
  1225. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1226. botnet_send_part_idx(uidx, NULL);
  1227. }
  1228. check_bind_chof(dcc[uidx].nick, uidx);
  1229. dcc[uidx].type = &DCC_RELAYING;
  1230. dcc[uidx].u.relay = ri;
  1231. }
  1232. static void eof_dcc_relay(int idx)
  1233. {
  1234. register int j;
  1235. for (j = 0; j < dcc_total; j++)
  1236. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock)
  1237. break;
  1238. if (j == dcc_total || !dcc[j].type) {
  1239. killsock(dcc[idx].sock);
  1240. lostdcc(idx);
  1241. return;
  1242. }
  1243. dcc[j].status = dcc[j].u.relay->old_status;
  1244. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1245. if (dcc[j].status & STAT_TELNET)
  1246. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1247. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_ENDRELAY1, dcc[j].nick,
  1248. dcc[idx].nick);
  1249. dprintf(j, "\n\n*** %s %s\n", BOT_ENDRELAY2, conf.bot->nick);
  1250. struct chat_info *ci = dcc[j].u.relay->chat;
  1251. free(dcc[j].u.relay);
  1252. dcc[j].u.chat = ci;
  1253. dcc[j].type = &DCC_CHAT;
  1254. if (dcc[j].u.chat->channel >= 0) {
  1255. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1256. dcc[j].nick, BOT_PARTYREJOINED);
  1257. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1258. botnet_send_join_idx(j);
  1259. }
  1260. check_bind_chon(dcc[j].nick, j);
  1261. killsock(dcc[idx].sock);
  1262. lostdcc(idx);
  1263. }
  1264. static void eof_dcc_relaying(int idx)
  1265. {
  1266. register int j, x = dcc[idx].u.relay->sock;
  1267. putlog(LOG_MISC, "*", "%s [%s]%s/%d", BOT_LOSTDCCUSER, dcc[idx].nick,
  1268. dcc[idx].host, dcc[idx].port);
  1269. killsock(dcc[idx].sock);
  1270. lostdcc(idx);
  1271. for (j = 0; j < dcc_total; j++)
  1272. if (dcc[j].type && dcc[j].sock == x && dcc[j].type != &DCC_FORK_RELAY)
  1273. break;
  1274. putlog(LOG_MISC, "*", "(%s %s)", BOT_DROPPEDRELAY, dcc[j].nick);
  1275. killsock(dcc[j].sock);
  1276. lostdcc(j); /* Drop connection to the bot */
  1277. }
  1278. static void dcc_relay(int idx, char *buf, int j)
  1279. {
  1280. unsigned char *p = (unsigned char *) buf;
  1281. int mark;
  1282. for (j = 0; j < dcc_total; j++)
  1283. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAYING)
  1284. break;
  1285. /* If redirecting to a non-telnet user, swallow telnet codes and
  1286. escape sequences. */
  1287. if (!(dcc[j].status & STAT_TELNET)) {
  1288. while (*p != 0) {
  1289. while (*p && *p != 255 && *p != '\r')
  1290. p++; /* Search for IAC, escape sequences and CR. */
  1291. if (*p == 255) {
  1292. mark = 2;
  1293. if (!*(p + 1))
  1294. mark = 1; /* Bogus */
  1295. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1296. mark = 3;
  1297. if (!*(p + 2))
  1298. mark = 2; /* Bogus */
  1299. }
  1300. strcpy((char *) p, (char *) (p + mark));
  1301. } else if (*p == '\r')
  1302. strcpy((char *) p, (char *) (p + 1));
  1303. }
  1304. if (!buf[0])
  1305. dprintf(-dcc[idx].u.relay->sock, " \n");
  1306. else
  1307. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1308. return;
  1309. }
  1310. /* Telnet user */
  1311. if (!buf[0])
  1312. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1313. else
  1314. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1315. }
  1316. static void dcc_relaying(int idx, char *buf, int j)
  1317. {
  1318. if (egg_strcasecmp(buf, "*bye*")) {
  1319. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1320. return;
  1321. }
  1322. /* The user want's to abort, so return them to partyline */
  1323. for (j = 0; j < dcc_total; j++)
  1324. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAY)
  1325. break;
  1326. dcc[idx].status = dcc[idx].u.relay->old_status;
  1327. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1328. if (dcc[idx].status & STAT_TELNET)
  1329. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1330. dprintf(idx, "\n(%s %s.)\n", BOT_BREAKRELAY, dcc[j].nick);
  1331. dprintf(idx, "%s %s.\n\n", BOT_ABORTRELAY2, conf.bot->nick);
  1332. putlog(LOG_MISC, "*", "%s: %s -> %s", BOT_RELAYBROKEN, dcc[idx].nick, dcc[j].nick);
  1333. if (dcc[idx].u.relay->chat->channel >= 0) {
  1334. chanout_but(-1, dcc[idx].u.relay->chat->channel, "*** %s joined the party line.\n", dcc[idx].nick);
  1335. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1336. botnet_send_join_idx(idx);
  1337. }
  1338. struct chat_info *ci = dcc[idx].u.relay->chat;
  1339. free(dcc[idx].u.relay);
  1340. dcc[idx].u.chat = ci;
  1341. dcc[idx].type = &DCC_CHAT;
  1342. check_bind_chon(dcc[idx].nick, idx);
  1343. killsock(dcc[j].sock);
  1344. lostdcc(j);
  1345. }
  1346. static void display_relay(int i, char *other)
  1347. {
  1348. simple_sprintf(other, "rela -> sock %d", dcc[i].u.relay->sock);
  1349. }
  1350. static void display_relaying(int i, char *other)
  1351. {
  1352. simple_sprintf(other, ">rly -> sock %d", dcc[i].u.relay->sock);
  1353. }
  1354. static void display_tandem_relay(int i, char *other)
  1355. {
  1356. strcpy(other, "other rela");
  1357. }
  1358. static void display_pre_relay(int i, char *other)
  1359. {
  1360. strcpy(other, "other >rly");
  1361. }
  1362. static void kill_relay(int idx, void *x)
  1363. {
  1364. register struct relay_info *p = (struct relay_info *) x;
  1365. if (p->chat)
  1366. DCC_CHAT.kill(idx, p->chat);
  1367. free(p);
  1368. }
  1369. struct dcc_table DCC_RELAY =
  1370. {
  1371. "RELAY",
  1372. 0, /* Flags */
  1373. eof_dcc_relay,
  1374. dcc_relay,
  1375. NULL,
  1376. NULL,
  1377. display_relay,
  1378. kill_relay,
  1379. NULL,
  1380. NULL
  1381. };
  1382. static void out_relay(int idx, char *buf, void *x)
  1383. {
  1384. register struct relay_info *p = (struct relay_info *) x;
  1385. if (p && p->chat)
  1386. DCC_CHAT.output(idx, buf, p->chat);
  1387. else
  1388. tputs(dcc[idx].sock, buf, strlen(buf));
  1389. }
  1390. struct dcc_table DCC_RELAYING =
  1391. {
  1392. "RELAYING",
  1393. 0, /* Flags */
  1394. eof_dcc_relaying,
  1395. dcc_relaying,
  1396. NULL,
  1397. NULL,
  1398. display_relaying,
  1399. kill_relay,
  1400. out_relay,
  1401. NULL
  1402. };
  1403. struct dcc_table DCC_FORK_RELAY =
  1404. {
  1405. "FORK_RELAY",
  1406. 0, /* Flags */
  1407. failed_tandem_relay,
  1408. cont_tandem_relay,
  1409. &connect_timeout,
  1410. failed_tandem_relay,
  1411. display_tandem_relay,
  1412. kill_relay,
  1413. NULL,
  1414. NULL
  1415. };
  1416. struct dcc_table DCC_PRE_RELAY =
  1417. {
  1418. "PRE_RELAY",
  1419. 0, /* Flags */
  1420. failed_pre_relay,
  1421. pre_relay,
  1422. NULL,
  1423. NULL,
  1424. display_pre_relay,
  1425. kill_relay,
  1426. NULL,
  1427. NULL
  1428. };
  1429. /* Once a minute, send 'ping' to each bot -- no exceptions
  1430. */
  1431. void check_botnet_pings()
  1432. {
  1433. int i, bots, users, top_index = 0;
  1434. tand_t *bot = NULL;
  1435. for (i = 0; i < dcc_total; i++) {
  1436. if (dcc[i].type) {
  1437. top_index = i;
  1438. if (dcc[i].type == &DCC_BOT) {
  1439. if (dcc[i].status & STAT_LEAF) {
  1440. tand_t *via = findbot(dcc[i].nick);
  1441. for (bot = tandbot; bot; bot = bot->next) {
  1442. if ((via == bot->via) && (bot != via)) {
  1443. /* Not leaflike behavior */
  1444. if (dcc[i].status & STAT_WARNED) {
  1445. char s[1024] = "";
  1446. putlog(LOG_BOTS, "*", "%s %s (%s).", BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE);
  1447. dprintf(i, "bye %s\n", BOT_BOTNOTLEAFLIKE);
  1448. bot = findbot(dcc[i].nick);
  1449. bots = bots_in_subtree(bot);
  1450. users = users_in_subtree(bot);
  1451. simple_sprintf(s, "%s %s (%s) (lost %d bot%s and %d user%s)",
  1452. BOT_DISCONNECTED, dcc[i].nick, BOT_BOTNOTLEAFLIKE,
  1453. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1454. "s" : "");
  1455. chatout("*** %s\n", s);
  1456. botnet_send_unlinked(i, dcc[i].nick, s);
  1457. killsock(dcc[i].sock);
  1458. lostdcc(i);
  1459. } else {
  1460. botnet_send_reject(i, conf.bot->nick, NULL, bot->bot, NULL, NULL);
  1461. dcc[i].status |= STAT_WARNED;
  1462. }
  1463. } else
  1464. dcc[i].status &= ~STAT_WARNED;
  1465. }
  1466. }
  1467. if (dcc[i].status & STAT_PINGED) {
  1468. char s[1024] = "";
  1469. putlog(LOG_BOTS, "*", "%s: %s", BOT_PINGTIMEOUT, dcc[i].nick);
  1470. bot = findbot(dcc[i].nick);
  1471. bots = bots_in_subtree(bot);
  1472. users = users_in_subtree(bot);
  1473. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_PINGTIMEOUT,
  1474. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1475. users, (users != 1) ? "s" : "");
  1476. chatout("*** %s\n", s);
  1477. botnet_send_unlinked(i, dcc[i].nick, s);
  1478. killsock(dcc[i].sock);
  1479. lostdcc(i);
  1480. } else {
  1481. botnet_send_ping(i);
  1482. dcc[i].status |= STAT_PINGED;
  1483. }
  1484. }
  1485. }
  1486. }
  1487. if (top_index != (dcc_total - 1))
  1488. trim_dcclist(top_index);
  1489. }
  1490. void zapfbot(int idx)
  1491. {
  1492. char s[1024] = "";
  1493. tand_t *bot = findbot(dcc[idx].nick);
  1494. int bots = bots_in_subtree(bot), users = users_in_subtree(bot);
  1495. simple_sprintf(s, "%s: %s (lost %d bot%s and %d user%s)", BOT_BOTDROPPED,
  1496. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1497. (users != 1) ? "s" : "");
  1498. chatout("*** %s\n", s);
  1499. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1500. killsock(dcc[idx].sock);
  1501. lostdcc(idx);
  1502. }
  1503. static int get_role(char *bot)
  1504. {
  1505. struct userrec *u2 = NULL;
  1506. if (!(u2 = get_user_by_handle(userlist, bot)))
  1507. return 1;
  1508. if (bot_hublevel(u2) != 999)
  1509. return 0;
  1510. int rl, i;
  1511. struct bot_addr *ba = NULL;
  1512. int r[5] = { 0, 0, 0, 0, 0 };
  1513. struct userrec *u = NULL;
  1514. for (u = userlist; u; u = u->next) {
  1515. if (u->bot && bot_hublevel(u) == 999) {
  1516. if (strcmp(u->handle, bot)) {
  1517. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1518. if ((nextbot(u->handle) >= 0) && (ba) && (ba->roleid > 0) && (ba->roleid < 5))
  1519. r[(ba->roleid - 1)]++;
  1520. }
  1521. }
  1522. }
  1523. rl = 0;
  1524. for (i = 1; i <= 4; i++)
  1525. if (r[i] < r[rl])
  1526. rl = i;
  1527. rl++;
  1528. ba = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u2);
  1529. if (ba)
  1530. ba->roleid = rl;
  1531. return rl;
  1532. }
  1533. void lower_bot_linked(int idx)
  1534. {
  1535. char tmp[6] = "";
  1536. simple_sprintf(tmp, "rl %d", get_role(dcc[idx].nick));
  1537. putbot(dcc[idx].nick, tmp);
  1538. }