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