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