botnet.c 45 KB

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