botnet.c 44 KB

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