botnet.c 45 KB

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