botnet.cc 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2014 Bryan Drewery
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. /*
  21. * botnet.c -- handles:
  22. * keeping track of which bot's connected where in the chain
  23. * dumping a list of bots or a bot tree to a user
  24. * channel name associations on the party line
  25. * rejecting a bot
  26. * linking, unlinking, and relaying to another bot
  27. * pinging the bots periodically and checking leaf status
  28. *
  29. */
  30. #include <sys/types.h>
  31. #include <sys/socket.h>
  32. #include <netinet/in.h>
  33. #include <arpa/inet.h>
  34. #include "common.h"
  35. #include "botnet.h"
  36. #include "color.h"
  37. #include "chanprog.h"
  38. #include "net.h"
  39. #include "socket.h"
  40. #include "adns.h"
  41. #include "match.h"
  42. #include "users.h"
  43. #include "misc.h"
  44. #include "userrec.h"
  45. #include "main.h"
  46. #include "dccutil.h"
  47. #include "dcc.h"
  48. #include "botmsg.h"
  49. #include "tandem.h"
  50. #include "core_binds.h"
  51. #include <bdlib/src/String.h>
  52. #include <bdlib/src/Array.h>
  53. #include <algorithm>
  54. #include <unordered_map>
  55. tand_t *tandbot = NULL; /* Keep track of tandem bots on the
  56. botnet */
  57. party_t *party = NULL; /* Keep track of people on the botnet */
  58. int tands = 0; /* Number of bots on the botnet */
  59. static int maxparty = 50; /* Maximum space for party line members
  60. currently */
  61. static int parties = 0; /* Number of people on the botnet */
  62. static int share_unlinks = 1; /* Allow remote unlinks of my
  63. sharebots? */
  64. void init_party()
  65. {
  66. party = (party_t *) calloc(1, maxparty * sizeof(party_t));
  67. }
  68. tand_t *findbot(const char *who)
  69. {
  70. for (tand_t* ptr = tandbot; ptr; ptr = ptr->next)
  71. if (!strcasecmp(ptr->bot, who))
  72. return ptr;
  73. return NULL;
  74. }
  75. extern void counter_clear(const char* botnick);
  76. /* Add a tandem bot to our chain list
  77. */
  78. void addbot(char *who, char *from, char *next, char flag, int vlocalhub, time_t vbuildts, char *vcommit, char *vversion, int fflags)
  79. {
  80. tand_t **ptr = &tandbot, *ptr2 = NULL;
  81. while (*ptr) {
  82. if (!strcasecmp((*ptr)->bot, who))
  83. putlog(LOG_BOTS, "*", "!!! Duplicate botnet bot entry!!");
  84. ptr = &((*ptr)->next);
  85. }
  86. ptr2 = (tand_t *) calloc(1, sizeof(tand_t));
  87. strlcpy(ptr2->bot, who, HANDLEN + 1);
  88. ptr2->bot[HANDLEN] = 0;
  89. ptr2->share = flag;
  90. ptr2->localhub = vlocalhub;
  91. ptr2->buildts = vbuildts;
  92. strlcpy(ptr2->commit, vcommit, sizeof(ptr2->commit));
  93. if (vversion && vversion[0])
  94. strlcpy(ptr2->version, vversion, 121);
  95. ptr2->next = *ptr;
  96. *ptr = ptr2;
  97. /* May be via itself */
  98. ptr2->via = findbot(from);
  99. /* Look up the bot in internal users */
  100. ptr2->hub = is_hub(who);
  101. /* Cache user record */
  102. ptr2->u = userlist ? get_user_by_handle(userlist, who) : NULL;
  103. ptr2->fflags = fflags;
  104. if (fflags != -1) {
  105. char buf[15];
  106. simple_snprintf(buf, sizeof(buf), "%d", ptr2->fflags);
  107. set_user(&USERENTRY_FFLAGS, ptr2->u ? ptr2->u : get_user_by_handle(userlist, who), buf);
  108. }
  109. if (!strcasecmp(next, conf.bot->nick))
  110. ptr2->uplink = (tand_t *) 1;
  111. else
  112. ptr2->uplink = findbot(next);
  113. tands++;
  114. counter_clear(who);
  115. }
  116. #ifdef G_BACKUP
  117. void check_should_backup()
  118. {
  119. struct chanset_t *chan = NULL;
  120. for (chan = chanset; chan; chan = chan->next) {
  121. if (chan->channel.backup_time && (chan->channel.backup_time < now) && !channel_backup(chan)) {
  122. do_chanset(NULL, chan, STR("+backup"), DO_LOCAL | DO_NET);
  123. chan->channel.backup_time = 0;
  124. }
  125. }
  126. }
  127. #endif /* G_BACKUP */
  128. void updatebot(int idx, char *who, char share, int vlocalhub, time_t vbuildts, char *vcommit, char *vversion, int fflags)
  129. {
  130. tand_t *ptr = findbot(who);
  131. if (ptr) {
  132. if (share)
  133. ptr->share = share;
  134. if (vlocalhub)
  135. ptr->localhub = vlocalhub;
  136. if (vbuildts)
  137. ptr->buildts = vbuildts;
  138. if (vcommit)
  139. strlcpy(ptr->commit, vcommit, sizeof(ptr->commit));
  140. if (vversion && vversion[0])
  141. strlcpy(ptr->version, vversion, 121);
  142. /* -1 = unknown (do not modify) */
  143. if (fflags != -1) {
  144. char buf[15];
  145. ptr->fflags = fflags;
  146. simple_snprintf(buf, sizeof(buf), "%d", ptr->fflags);
  147. set_user(&USERENTRY_FFLAGS, ptr->u ? ptr->u : get_user_by_handle(userlist, who), buf);
  148. }
  149. /* Assign flags here */
  150. botnet_send_update(idx, ptr);
  151. }
  152. }
  153. /* New botnet member
  154. */
  155. int addparty(char *bot, char *nick, int chan, char flag, int sock, char *from, int *idx)
  156. {
  157. int i = 0;
  158. for (i = 0; i < parties; i++) {
  159. /* Just changing the channel of someone already on? */
  160. if (!strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
  161. int oldchan = party[i].chan;
  162. party[i].chan = chan;
  163. party[i].timer = now;
  164. if (from[0]) {
  165. if (flag == ' ')
  166. flag = '-';
  167. party[i].flag = flag;
  168. if (party[i].from)
  169. free(party[i].from);
  170. party[i].from = strdup(from);
  171. }
  172. *idx = i;
  173. return oldchan;
  174. }
  175. }
  176. /* New member */
  177. if (parties == maxparty) {
  178. maxparty += 50;
  179. party = (party_t *) realloc((void *) party, maxparty * sizeof(party_t));
  180. }
  181. strlcpy(party[parties].nick, nick, sizeof(party[parties].nick));
  182. strlcpy(party[parties].bot, bot, sizeof(party[parties].bot));
  183. party[parties].chan = chan;
  184. party[parties].sock = sock;
  185. party[parties].status = 0;
  186. party[parties].away = 0;
  187. party[parties].timer = now; /* cope. */
  188. if (from[0]) {
  189. if (flag == ' ')
  190. flag = '-';
  191. party[parties].flag = flag;
  192. party[parties].from = strdup(from);
  193. } else {
  194. party[parties].flag = ' ';
  195. party[parties].from = strdup("(unknown)");
  196. }
  197. *idx = parties;
  198. parties++;
  199. return -1;
  200. }
  201. /* Alter status flags for remote party-line user.
  202. */
  203. void partystat(char *bot, int sock, int add, int rem)
  204. {
  205. for (int i = 0; i < parties; i++) {
  206. if ((!strcasecmp(party[i].bot, bot)) &&
  207. (party[i].sock == sock)) {
  208. party[i].status |= add;
  209. party[i].status &= ~rem;
  210. }
  211. }
  212. }
  213. /* Other bot is sharing idle info.
  214. */
  215. void partysetidle(char *bot, int sock, int secs)
  216. {
  217. for (int i = 0; i < parties; i++) {
  218. if ((!strcasecmp(party[i].bot, bot)) &&
  219. (party[i].sock == sock)) {
  220. party[i].timer = (now - (time_t) secs);
  221. }
  222. }
  223. }
  224. /* Return someone's chat channel.
  225. */
  226. int getparty(const char *bot, int sock)
  227. {
  228. for (int i = 0; i < parties; i++) {
  229. if (!strcasecmp(party[i].bot, bot) &&
  230. (party[i].sock == sock)) {
  231. return i;
  232. }
  233. }
  234. return -1;
  235. }
  236. /* Un-idle someone
  237. */
  238. bool partyidle(char *bot, char *nick)
  239. {
  240. bool ok = 0;
  241. for (int i = 0; i < parties; i++) {
  242. if ((!strcasecmp(party[i].bot, bot)) && (!strcasecmp(party[i].nick, nick))) {
  243. party[i].timer = now;
  244. ok = 1;
  245. }
  246. }
  247. return ok;
  248. }
  249. /* Change someone's nick
  250. */
  251. int partynick(char *bot, int sock, char *nick)
  252. {
  253. char work[HANDLEN + 1] = "";
  254. for (int i = 0; i < parties; i++) {
  255. if (!strcasecmp(party[i].bot, bot) && (party[i].sock == sock)) {
  256. strlcpy(work, party[i].nick, sizeof(work));
  257. strlcpy(party[i].nick, nick, sizeof(party[i].nick));
  258. strlcpy(nick, work, HANDLEN + 1);
  259. return i;
  260. }
  261. }
  262. return -1;
  263. }
  264. /* Set away message
  265. */
  266. void partyaway(char *bot, int sock, char *msg)
  267. {
  268. for (int i = 0; i < parties; i++) {
  269. if ((!strcasecmp(party[i].bot, bot)) &&
  270. (party[i].sock == sock)) {
  271. if (party[i].away)
  272. free(party[i].away);
  273. if (msg[0]) {
  274. party[i].away = strdup(msg);
  275. } else
  276. party[i].away = 0;
  277. }
  278. }
  279. }
  280. /* Remove a tandem bot from the chain list
  281. */
  282. void rembot(const char *whoin)
  283. {
  284. tand_t **ptr = &tandbot, *ptr2 = NULL;
  285. char *who = strdup(whoin);
  286. while (*ptr) {
  287. if (!strcasecmp((*ptr)->bot, who))
  288. break;
  289. ptr = &((*ptr)->next);
  290. }
  291. if (!*ptr) {
  292. /* May have just .unlink *'d */
  293. free(who);
  294. return;
  295. }
  296. struct userrec *u = get_user_by_handle(userlist, (char *) who);
  297. if (u) {
  298. noshare++;
  299. touch_laston(u, "unlinked", now);
  300. noshare--;
  301. }
  302. ptr2 = *ptr;
  303. *ptr = ptr2->next;
  304. free(ptr2);
  305. tands--;
  306. dupwait_notify(who);
  307. free(who);
  308. }
  309. void remparty(char *bot, int sock)
  310. {
  311. for (int i = 0; i < parties; i++)
  312. if ((!strcasecmp(party[i].bot, bot)) &&
  313. (party[i].sock == sock)) {
  314. parties--;
  315. if (party[i].from)
  316. free(party[i].from);
  317. if (party[i].away)
  318. free(party[i].away);
  319. if (i < parties) {
  320. strlcpy(party[i].bot, party[parties].bot, sizeof(party[i].bot));
  321. strlcpy(party[i].nick, party[parties].nick, sizeof(party[i].nick));
  322. party[i].chan = party[parties].chan;
  323. party[i].sock = party[parties].sock;
  324. party[i].flag = party[parties].flag;
  325. party[i].status = party[parties].status;
  326. party[i].timer = party[parties].timer;
  327. party[i].from = party[parties].from;
  328. party[i].away = party[parties].away;
  329. }
  330. }
  331. }
  332. /* Cancel every user that was on a certain bot
  333. */
  334. void rempartybot(char *bot)
  335. {
  336. for (int i = 0; i < parties; i++)
  337. if (!strcasecmp(party[i].bot, bot)) {
  338. remparty(bot, party[i].sock);
  339. i--;
  340. }
  341. }
  342. /* Remove every bot linked 'via' bot <x>
  343. */
  344. void unvia(int idx, tand_t *who)
  345. {
  346. if (!who)
  347. return; /* Safety */
  348. tand_t *bot = NULL, *bot2 = NULL;
  349. rempartybot(who->bot);
  350. bot = tandbot;
  351. while (bot) {
  352. if (bot->uplink == who) {
  353. unvia(idx, bot);
  354. bot2 = bot->next;
  355. rembot(bot->bot);
  356. bot = bot2;
  357. } else
  358. bot = bot->next;
  359. }
  360. }
  361. /* Return index into dcc list of the bot that connects us to bot <x>
  362. */
  363. int nextbot(const char *who)
  364. {
  365. tand_t *bot = findbot(who);
  366. if (!bot)
  367. return -1;
  368. for (int j = 0; j < dcc_total; j++) {
  369. if (dcc[j].type && bot->via && (dcc[j].type == &DCC_BOT) && !strcasecmp(bot->via->bot, dcc[j].nick))
  370. return j;
  371. }
  372. return -1; /* We're not connected to 'via' */
  373. }
  374. /* Return name of the bot that is directly connected to bot X
  375. */
  376. char *lastbot(const char *who)
  377. {
  378. tand_t *bot = findbot(who);
  379. if (!bot)
  380. return "*";
  381. else if (bot->uplink == (tand_t *) 1)
  382. return conf.bot->nick;
  383. else
  384. return bot->uplink->bot;
  385. }
  386. /* Modern version of 'whom' (use local data)
  387. */
  388. void answer_local_whom(int idx, int chan)
  389. {
  390. char format[81] = "", c = 0, idle[40] = "";
  391. int i, t, nicklen, botnicklen, total = 0;
  392. if (chan == (-1))
  393. dprintf(idx, "%s (+: %s, *: %s)\n", "Users across the botnet", "Party line", "Local channel");
  394. else if (chan > 0)
  395. dprintf(idx, "%s %s%d:\n", "Users on channel", (chan < GLOBAL_CHANS) ? "" : "*", chan % GLOBAL_CHANS);
  396. /* Find longest nick and botnick */
  397. nicklen = botnicklen = 0;
  398. for (i = 0; i < dcc_total; i++) {
  399. if (dcc[i].type && dcc[i].simul == -1 && dcc[i].type == &DCC_CHAT) {
  400. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  401. t = strlen(dcc[i].nick); if(t > nicklen) nicklen = t;
  402. t = strlen(conf.bot->nick); if(t > botnicklen) botnicklen = t;
  403. }
  404. }
  405. }
  406. for (i = 0; i < parties; i++) {
  407. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  408. t = strlen(party[i].nick); if(t > nicklen) nicklen = t;
  409. t = strlen(party[i].bot); if(t > botnicklen) botnicklen = t;
  410. }
  411. }
  412. if(nicklen < 9) nicklen = 9;
  413. if(botnicklen < 9) botnicklen = 9;
  414. if (conf.bot->hub) {
  415. simple_snprintf(format, sizeof format, "%%-%us %%-%us %%s\n", nicklen, botnicklen);
  416. dprintf(idx, format, " Nick", " Bot", " Host");
  417. dprintf(idx, format, "----------", "---------", "--------------------");
  418. simple_snprintf(format, sizeof format, "%%c%%-%us %%c %%-%us %%s%%s\n", nicklen, botnicklen);
  419. } else {
  420. simple_snprintf(format, sizeof format, "%%-%us\n", nicklen);
  421. dprintf(idx, format, " Nick");
  422. dprintf(idx, format, "----------");
  423. simple_snprintf(format, sizeof format, "%%c%%-%us %%c %%s\n", nicklen);
  424. }
  425. for (i = 0; i < dcc_total; i++) {
  426. if (dcc[i].type && dcc[i].simul == -1 && dcc[i].type == &DCC_CHAT) {
  427. if ((chan == (-1)) || ((chan >= 0) && (dcc[i].u.chat->channel == chan))) {
  428. c = geticon(i);
  429. if (c == '-')
  430. c = ' ';
  431. if (now - dcc[i].timeval > 300) {
  432. unsigned long mydays, hrs, mins;
  433. mydays = (now - dcc[i].timeval) / 86400;
  434. hrs = ((now - dcc[i].timeval) - (mydays * 86400)) / 3600;
  435. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  436. if (mydays > 0)
  437. simple_snprintf(idle, sizeof(idle), " [idle %lud%luh]", mydays, hrs);
  438. else if (hrs > 0)
  439. simple_snprintf(idle, sizeof(idle), " [idle %luh%lum]", hrs, mins);
  440. else
  441. simple_snprintf(idle, sizeof(idle), " [idle %lum]", mins);
  442. } else
  443. idle[0] = 0;
  444. total++;
  445. if (conf.bot->hub)
  446. dprintf(idx, format, c, dcc[i].nick,
  447. (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
  448. (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
  449. (chan == (-1)) ? '*' : ' ', conf.bot->nick, dcc[i].host, idle);
  450. else
  451. dprintf(idx, format, c, dcc[i].nick,
  452. (dcc[i].u.chat->channel == 0) && (chan == (-1)) ? '+' :
  453. (dcc[i].u.chat->channel > GLOBAL_CHANS) &&
  454. (chan == (-1)) ? '*' : ' ', idle);
  455. if (dcc[i].u.chat->away != NULL)
  456. dprintf(idx, " AWAY: %s\n", dcc[i].u.chat->away);
  457. if (dcc[i].u.chat->su_nick && (dcc[idx].user->flags & USER_ADMIN))
  458. dprintf(idx, " SU FROM: %s\n", dcc[i].u.chat->su_nick);
  459. }
  460. }
  461. }
  462. for (i = 0; i < parties; i++) {
  463. if ((chan == (-1)) || ((chan >= 0) && (party[i].chan == chan))) {
  464. c = party[i].flag;
  465. if (c == '-')
  466. c = ' ';
  467. if (party[i].timer == 0L)
  468. strlcpy(idle, " [idle?]", sizeof(idle));
  469. else if (now - party[i].timer > 300) {
  470. unsigned long mydays, hrs, mins;
  471. mydays = (now - party[i].timer) / 86400;
  472. hrs = ((now - party[i].timer) - (mydays * 86400)) / 3600;
  473. mins = ((now - party[i].timer) - (hrs * 3600)) / 60;
  474. if (mydays > 0)
  475. simple_snprintf(idle, sizeof(idle), " [idle %lud%luh]", mydays, hrs);
  476. else if (hrs > 0)
  477. simple_snprintf(idle, sizeof(idle), " [idle %luh%lum]", hrs, mins);
  478. else
  479. simple_snprintf(idle, sizeof(idle), " [idle %lum]", mins);
  480. } else
  481. idle[0] = 0;
  482. total++;
  483. if (conf.bot->hub)
  484. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ',
  485. party[i].bot, party[i].from, idle);
  486. else
  487. dprintf(idx, format, c, party[i].nick, (party[i].chan == 0) && (chan == (-1)) ? '+' : ' ', idle);
  488. if (party[i].status & PLSTAT_AWAY)
  489. dprintf(idx, " AWAY: %s\n", party[i].away ? party[i].away : "");
  490. }
  491. }
  492. dprintf(idx, "Total users: %d\n", total);
  493. }
  494. bool sortNodes(const bd::String nodeA, const bd::String nodeB) {
  495. const bd::String unknown("(unknown)");
  496. if (nodeA == unknown) {
  497. return true;
  498. } else if (nodeB == unknown) {
  499. return false;
  500. }
  501. // Reverse the domains
  502. const bd::Array<bd::String> partsA(nodeA.split("."));
  503. const bd::Array<bd::String> partsB(nodeB.split("."));
  504. bd::Array<bd::String> reversedPartsA, reversedPartsB;
  505. bd::String reversedNodeA, reversedNodeB;
  506. if (partsA.length()) {
  507. for (size_t i = partsA.length() - 1; i > 0; --i) {
  508. reversedPartsA << partsA[i - 1];
  509. }
  510. }
  511. if (partsB.length()) {
  512. for (size_t i = partsB.length() - 1; i > 0; --i) {
  513. reversedPartsB << partsB[i - 1];
  514. }
  515. }
  516. reversedNodeA = reversedPartsA.join(".");
  517. reversedNodeB = reversedPartsB.join(".");
  518. return reversedNodeA < reversedNodeB;
  519. }
  520. /* Show z a list of all bots connected
  521. */
  522. void
  523. tell_bots(int idx, int up, const char *nodename)
  524. {
  525. size_t total = 0, maxNodeNameLength = 0;;
  526. bd::Array<bd::String> nodes;
  527. std::unordered_map<bd::String, bd::Array<bd::String> > nodeBots;
  528. bd::Array<bd::String> bots;
  529. bd::String group;
  530. if (nodename && nodename[0] == '%') {
  531. group = nodename + 1;
  532. }
  533. // Gather a list of nodes and bots per node, as well as per domain
  534. for (struct userrec* u = userlist; u; u = u->next) {
  535. if (u->bot) {
  536. // If looking for groups, exclude hubs
  537. if (group.length() && bot_hublevel(u) != 999) {
  538. continue;
  539. }
  540. ++total;
  541. bd::String botnick(u->handle);
  542. const bd::Array<bd::String> botgroups((bd::String(var_get_bot_data(u, "groups", true))).split(","));
  543. // Include this bot?
  544. const bool group_match = group.length() && botgroups.find(group) != botgroups.npos;
  545. const char *userNode = (const char*) get_user(&USERENTRY_NODENAME, u);
  546. const bd::String node(userNode ? userNode : "(unknown)");
  547. const bool node_match = ((nodename && node.length() && wild_match(nodename, node.c_str())) || !nodename);
  548. const bool bot_found = u == conf.bot->u || findbot(u->handle);
  549. const bool up_down_match = (nodename || (!nodename && ((up && bot_found) || (!up && !bot_found))));
  550. if (group_match || (group.length() == 0 && node_match && up_down_match)) {
  551. if (nodes.find(node) == nodes.npos) {
  552. nodes << node;
  553. if (node.length() > maxNodeNameLength) {
  554. maxNodeNameLength = node.length();
  555. }
  556. }
  557. if ((group.length() || nodename) && !bot_found) {
  558. botnick = '*' + botnick;
  559. }
  560. nodeBots[node] << botnick;
  561. bots << botnick;
  562. }
  563. }
  564. }
  565. if (group.length() == 0 && !nodename) {
  566. dumplots(idx, nodename ? "Matching: " : (up ? "Up: " : "Down: "), static_cast<bd::String>(bots.join(" ")).c_str());
  567. } else {
  568. // Sort by nodes
  569. std::sort(nodes.begin(), nodes.end(), sortNodes);
  570. for (size_t i = 0; i < nodes.length(); ++i) {
  571. const bd::String node(nodes[i]);
  572. const bd::Array<bd::String> botsInNode(nodeBots[node]);
  573. dumplots(idx, bd::String::printf("%*s: ", int(maxNodeNameLength), node.c_str()).c_str(), static_cast<bd::String>(botsInNode.join(" ")).c_str());
  574. }
  575. }
  576. if (nodename || group.length()) {
  577. dprintf(idx, "(Total Matching: %zu/%zu)\n", bots.length(), total);
  578. } else {
  579. dprintf(idx, "(Total %s: %zu/%zu)\n", up ? "up" : "down", bots.length(), total);
  580. }
  581. }
  582. /* Show a simpleton bot tree
  583. */
  584. void tell_bottree(int idx)
  585. {
  586. if (tands == 0) {
  587. dprintf(idx, "%s\n", "No bots linked.");
  588. return;
  589. }
  590. char *color_str = NULL;
  591. tand_t *last[20] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  592. NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
  593. tand_t *thisbot = NULL, *bot = NULL, *bot2 = NULL;
  594. int lev = 0, more = 1, mark[20], ok, cnt, i = 0, tothops = 0;
  595. struct userrec *botu = NULL;
  596. bd::String s, work;
  597. for (bot = tandbot; bot; bot = bot->next)
  598. if (!bot->uplink) {
  599. if (i)
  600. s += ", ";
  601. s += bot->bot;
  602. if (!i)
  603. i = 1;
  604. }
  605. if (bot_hublevel(conf.bot->u) < 999)
  606. color_str = (char *) YELLOW(idx);
  607. else if (conf.bot->localhub)
  608. color_str = (char *) RED(idx);
  609. else
  610. color_str = (char *) NULL;
  611. if (s.length())
  612. dprintf(idx, "(%s %s)\n", "No trace info for:", s.c_str());
  613. dprintf(idx, "%s%s%s (%s)\n", color_str ? color_str : "",
  614. conf.bot->nick,
  615. color_str ? COLOR_END(idx) : "",
  616. egg_version);
  617. thisbot = (tand_t *) 1;
  618. while (more) {
  619. if (lev == 20) {
  620. dprintf(idx, "\n%s\n", "Tree too complex!");
  621. return;
  622. }
  623. cnt = 0;
  624. tothops += lev;
  625. for (bot = tandbot; bot; bot = bot->next)
  626. if (bot->uplink == thisbot)
  627. cnt++;
  628. if (cnt) {
  629. for (i = 0; i < lev; i++) {
  630. if (mark[i])
  631. work += " | ";
  632. else
  633. work += " ";
  634. }
  635. if (cnt > 1)
  636. work += " |-";
  637. else
  638. work += " `-";
  639. s.clear();
  640. bot = tandbot;
  641. while (bot && !s) {
  642. if (bot->uplink == thisbot) {
  643. botu = get_user_by_handle(userlist, bot->bot);
  644. if (botu && bot_hublevel(botu) < 999)
  645. color_str = (char *) YELLOW(idx);
  646. else if (botu && bot->localhub)
  647. color_str = (char *) RED(idx);
  648. else
  649. color_str = (char *) NULL;
  650. s = bd::String::printf("%c%s%s%s (%s)", bot->share ? bot->share : '-', color_str ? color_str : "",
  651. bot->bot,
  652. color_str ? COLOR_END(idx) : "",
  653. bot->version);
  654. } else
  655. bot = bot->next;
  656. }
  657. dprintf(idx, "%s%s\n", work.c_str(), s.c_str());
  658. if (cnt > 1)
  659. mark[lev] = 1;
  660. else
  661. mark[lev] = 0;
  662. work.clear();
  663. last[lev] = thisbot;
  664. thisbot = bot;
  665. lev++;
  666. more = 1;
  667. } else {
  668. while (cnt == 0) {
  669. /* No subtrees from here */
  670. if (lev == 0) {
  671. dprintf(idx, "(( tree error ))\n");
  672. return;
  673. }
  674. ok = 0;
  675. for (bot = tandbot; bot; bot = bot->next) {
  676. if (bot->uplink == last[lev - 1]) {
  677. if (thisbot == bot)
  678. ok = 1;
  679. else if (ok) {
  680. cnt++;
  681. if (cnt == 1) {
  682. botu = get_user_by_handle(userlist, bot->bot);
  683. if (botu && bot_hublevel(botu) < 999)
  684. color_str = (char *) YELLOW(idx);
  685. else if (botu && bot->localhub)
  686. color_str = (char *) RED(idx);
  687. else
  688. color_str = (char *) NULL;
  689. bot2 = bot;
  690. s = bd::String::printf("%c%s%s%s (%s)", bot->share ? bot->share : '-', color_str ? color_str : "",
  691. bot->bot,
  692. color_str ? COLOR_END(idx) : "",
  693. bot->version);
  694. }
  695. }
  696. }
  697. }
  698. if (cnt) {
  699. for (i = 1; i < lev; i++) {
  700. if (mark[i - 1])
  701. work += " | ";
  702. else
  703. work += " ";
  704. }
  705. more = 1;
  706. if (cnt > 1)
  707. dprintf(idx, "%s |-%s\n", work.c_str(), s.c_str());
  708. else
  709. dprintf(idx, "%s `-%s\n", work.c_str(), s.c_str());
  710. thisbot = bot2;
  711. work.clear();
  712. if (cnt > 1)
  713. mark[lev - 1] = 1;
  714. else
  715. mark[lev - 1] = 0;
  716. } else {
  717. /* This was the last child */
  718. lev--;
  719. if (lev == 0) {
  720. more = 0;
  721. cnt = 999;
  722. } else {
  723. more = 1;
  724. thisbot = last[lev];
  725. }
  726. }
  727. }
  728. }
  729. }
  730. /* Hop information: (9d) */
  731. dprintf(idx, "Average hops: %3.1f, total bots: %d\n", ((float) tothops) / ((float) tands), tands + 1);
  732. }
  733. /* Dump list of links to a new bot
  734. */
  735. void dump_links(int z)
  736. {
  737. int i;
  738. size_t l;
  739. char x[1024] = "";
  740. if (conf.bot->hub || conf.bot->localhub) {
  741. tand_t *bot = NULL;
  742. char *p = NULL;
  743. for (bot = tandbot; bot; bot = bot->next) {
  744. if (bot->uplink == (tand_t *) 1)
  745. p = conf.bot->nick;
  746. else
  747. p = bot->uplink->bot;
  748. l = simple_snprintf(x, sizeof(x), "n %s %s %cD0gc %d %d %s %s\n", bot->bot, p, bot->share, bot->localhub,
  749. (int) bot->buildts, bot->commit, bot->version);
  750. tputs(dcc[z].sock, x, l);
  751. }
  752. }
  753. /* Dump party line members */
  754. for (i = 0; i < dcc_total; i++) {
  755. if (dcc[i].type && dcc[i].type == &DCC_CHAT && dcc[i].simul == -1) {
  756. if ((dcc[i].u.chat->channel >= 0) && (dcc[i].u.chat->channel < GLOBAL_CHANS)) {
  757. l = simple_snprintf2(x, sizeof(x), "j !%s %s %D %c%D %s\n", conf.bot->nick, dcc[i].nick,
  758. dcc[i].u.chat->channel, geticon(i), dcc[i].sock, dcc[i].host);
  759. tputs(dcc[z].sock, x, l);
  760. l = simple_snprintf2(x, sizeof(x), "i %s %D %D %s\n", conf.bot->nick,
  761. dcc[i].sock, now - dcc[i].timeval,
  762. dcc[i].u.chat->away ? dcc[i].u.chat->away : "");
  763. tputs(dcc[z].sock, x, l);
  764. }
  765. }
  766. }
  767. for (i = 0; i < parties; i++) {
  768. l = simple_snprintf2(x, sizeof(x), "j %s %s %D %c%D %s\n", party[i].bot, party[i].nick,
  769. party[i].chan, party[i].flag, party[i].sock, party[i].from);
  770. tputs(dcc[z].sock, x, l);
  771. if ((party[i].status & PLSTAT_AWAY) || (party[i].timer != 0)) {
  772. 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 : "");
  773. tputs(dcc[z].sock, x, l);
  774. }
  775. }
  776. }
  777. int in_chain(const char *who)
  778. {
  779. if (!strcasecmp(who, conf.bot->nick))
  780. return 1;
  781. if (findbot(who))
  782. return 1;
  783. return 0;
  784. }
  785. int bots_in_subtree(const tand_t *bot)
  786. {
  787. if (!bot)
  788. return 0;
  789. int nr = 1;
  790. tand_t *b = NULL;
  791. for (b = tandbot; b; b = b->next) {
  792. if (b->uplink == bot) {
  793. nr += bots_in_subtree(b);
  794. }
  795. }
  796. return nr;
  797. }
  798. int users_in_subtree(const tand_t *bot)
  799. {
  800. if (!bot)
  801. return 0;
  802. int i, nr = 0;
  803. tand_t *b = NULL;
  804. for (i = 0; i < parties; i++)
  805. if (!strcasecmp(party[i].bot, bot->bot))
  806. nr++;
  807. for (b = tandbot; b; b = b->next)
  808. if (b->uplink == bot)
  809. nr += users_in_subtree(b);
  810. return nr;
  811. }
  812. /* Break link with a tandembot
  813. */
  814. int botunlink(int idx, const char *nick, const char *reason)
  815. {
  816. int i;
  817. int bots, users;
  818. tand_t *bot = NULL;
  819. if (nick[0] == '*')
  820. dprintf(idx, "%s\n", "Unlinking all bots...");
  821. for (i = 0; i < dcc_total; i++) {
  822. if (dcc[i].type && ((nick[0] == '*') || !strcasecmp(dcc[i].nick, nick))) {
  823. if (dcc[i].type == &DCC_FORK_BOT) {
  824. if (idx >= 0)
  825. dprintf(idx, "%s: %s -> %s.\n", "Killed link attempt to",
  826. dcc[i].nick, dcc[i].host);
  827. putlog(LOG_BOTS, "*", "%s: %s -> %s:%d",
  828. "Killed link attempt to", dcc[i].nick,
  829. dcc[i].host, dcc[i].port);
  830. killsock(dcc[i].sock);
  831. lostdcc(i);
  832. if (nick[0] != '*')
  833. return 1;
  834. } else if (dcc[i].type == &DCC_BOT_NEW) {
  835. if (idx >= 0)
  836. dprintf(idx, "%s %s.\n", "No longer trying to link:",
  837. dcc[i].nick);
  838. putlog(LOG_BOTS, "*", "%s %s @ %s:%d",
  839. "Stopped trying to link", dcc[i].nick,
  840. dcc[i].host, dcc[i].port);
  841. killsock(dcc[i].sock);
  842. lostdcc(i);
  843. if (nick[0] != '*')
  844. return 1;
  845. } else if (dcc[i].type == &DCC_BOT) {
  846. char s[1024] = "";
  847. if (idx >= 0)
  848. dprintf(idx, "%s %s.\n", "Breaking link with", dcc[i].nick);
  849. else if ((idx == -3) && (b_status(i) & STAT_SHARE) && !share_unlinks)
  850. return -1;
  851. bot = findbot(dcc[i].nick);
  852. bots = bots_in_subtree(bot);
  853. users = users_in_subtree(bot);
  854. if (reason && reason[0]) {
  855. simple_snprintf(s, sizeof(s), "%s %s (%s) (lost %d bot%s and %d user%s)",
  856. "Unlinked from:", dcc[i].nick, reason, bots,
  857. (bots != 1) ? "s" : "", users, (users != 1) ?
  858. "s" : "");
  859. dprintf(i, "bye %s\n", reason);
  860. } else {
  861. simple_snprintf(s, sizeof(s), "%s %s (lost %d bot%s and %d user%s)",
  862. "Unlinked from:", dcc[i].nick, bots, (bots != 1) ?
  863. "s" : "", users, (users != 1) ? "s" : "");
  864. dprintf(i, "bye No reason\n");
  865. }
  866. chatout("*** %s\n", s);
  867. botnet_send_unlinked(i, dcc[i].nick, s);
  868. killsock(dcc[i].sock);
  869. lostdcc(i);
  870. if (nick[0] != '*')
  871. return 1;
  872. }
  873. }
  874. }
  875. if (idx >= 0 && nick[0] != '*')
  876. dprintf(idx, "%s\n", "Not connected to that bot.");
  877. if (nick[0] != '*') {
  878. bot = findbot(nick);
  879. if (bot) {
  880. /* The internal bot list is desynched from the dcc list
  881. sometimes. While we still search for the bug, provide
  882. an easy way to clear out those `ghost'-bots.
  883. Fabian (2000-08-02) */
  884. char *ghost = "BUG!!: Found bot `%s' in internal bot list, but it\n"
  885. " shouldn't have been there! Removing.\n"
  886. " This is a known bug we haven't fixed yet. If this\n"
  887. " bot is the newest eggdrop version available and you\n"
  888. " know a *reliable* way to reproduce the bug, please\n"
  889. " contact us - we need your help!\n";
  890. if (idx >= 0)
  891. dprintf(idx, ghost, nick);
  892. else
  893. putlog(LOG_MISC, "*", ghost, nick);
  894. rembot(bot->bot);
  895. return 1;
  896. }
  897. }
  898. if (nick[0] == '*') {
  899. dprintf(idx, "%s\n", "Smooshing bot tables and assocs...");
  900. while (tandbot)
  901. rembot(tandbot->bot);
  902. while (parties) {
  903. parties--;
  904. }
  905. }
  906. return 0;
  907. }
  908. static void botlink_dns_callback(int, void *, const char *, bd::Array<bd::String>);
  909. static void botlink_real(int);
  910. /* Link to another bot
  911. */
  912. int botlink(char *linker, int idx, char *nick)
  913. {
  914. struct userrec *u = get_user_by_handle(userlist, nick);
  915. if (!u || !u->bot) {
  916. if (idx >= 0)
  917. dprintf(idx, "%s %s\n", nick, "is not a known bot.");
  918. } else if (!strcasecmp(nick, conf.bot->nick)) {
  919. if (idx >= 0)
  920. dprintf(idx, "%s\n", "Link to myself? Oh boy, Freud would have a field day.");
  921. } else if (in_chain(nick) && (idx != -3)) {
  922. if (idx >= 0)
  923. dprintf(idx, "That bot is already connected up.\n");
  924. } else {
  925. int i;
  926. for (i = 0; i < dcc_total; i++) {
  927. if (dcc[i].type && (dcc[i].user == u) &&
  928. ((dcc[i].type == &DCC_FORK_BOT) ||
  929. (dcc[i].type == &DCC_BOT_NEW))) {
  930. if (idx >= 0)
  931. dprintf(idx, "Already linking to that bot\n");
  932. return 0;
  933. }
  934. }
  935. bool unix_domain = 0;
  936. if (!conf.bot->hub && !conf.bot->localhub && !strcmp(nick, conf.localhub))
  937. unix_domain = 1;
  938. /* Address to connect to is in 'info' */
  939. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  940. if (!unix_domain && (!bi || !strlen(bi->address) || !bi->telnet_port || (bi->telnet_port <= 0))) {
  941. if (idx >= 0) {
  942. dprintf(idx, "Invalid telnet address:port stored for '%s'.\n", nick);
  943. dprintf(idx, "Use: %schaddr %s <address>:<port#>[/<relay-port#>]\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "", nick);
  944. }
  945. } else if (dcc_total == max_dcc) {
  946. if (idx >= 0)
  947. dprintf(idx, "%s\n", "Sorry, too many DCC connections.");
  948. } else {
  949. correct_handle(nick);
  950. char *address = NULL;
  951. in_port_t port = 0;
  952. if (unix_domain) {
  953. address = conf.localhub_socket;
  954. } else if (bi) {
  955. address = bi->address;
  956. port = bi->telnet_port;
  957. }
  958. if (idx > -2) {
  959. if (port)
  960. putlog(LOG_BOTS, "*", "Linking to %s at %s:%d ...", nick, address, port);
  961. else
  962. putlog(LOG_BOTS, "*", "Linking to %s at %s ...", nick, address);
  963. }
  964. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  965. dcc[i].timeval = now;
  966. dcc[i].port = port;
  967. dcc[i].user = u;
  968. strlcpy(dcc[i].nick, nick, sizeof(dcc[i].nick));
  969. strlcpy(dcc[i].host, address, sizeof(dcc[i].host));
  970. dcc[i].u.dns->cptr = strdup(linker);
  971. dcc[i].u.dns->ibuf = idx;
  972. dcc[i].bot = 1;
  973. if (unix_domain) {
  974. botlink_real(i);
  975. } else {
  976. int dns_id = egg_dns_lookup(address, 20, botlink_dns_callback, (void *) (long)i);
  977. /* dns_id
  978. * -1 means it was cached and the callback already called
  979. * -2 means it's already being looked up.. try again later .. */
  980. if (dns_id >= 0)
  981. dcc[i].dns_id = dns_id;
  982. else if (dns_id == -2) {
  983. lostdcc(i);
  984. return 0;
  985. }
  986. }
  987. return 1;
  988. /* wait for async reply */
  989. }
  990. }
  991. return 0;
  992. }
  993. static void botlink_dns_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips)
  994. {
  995. long data = (long) client_data;
  996. int i = (int) data;
  997. Context;
  998. if (!valid_dns_id(i, id))
  999. return;
  1000. // if (valid_idx(i)) {
  1001. // idx = dcc[i].u.dns->ibuf;
  1002. // }
  1003. bd::String ip_from_dns;
  1004. if (ips.size()) {
  1005. #ifdef USE_IPV6
  1006. /* If IPv6 is available use it, otherwise fall back on IPv4 */
  1007. if (conf.bot->net.v6)
  1008. ip_from_dns = dns_find_ip(ips, AF_INET6);
  1009. if (!ip_from_dns)
  1010. #endif /* USE_IPV6 */
  1011. {
  1012. /* Use IPv4 if no ipv6 is available */
  1013. ip_from_dns = dns_find_ip(ips, AF_INET);
  1014. }
  1015. }
  1016. if (!ips.size() || !ip_from_dns) {
  1017. putlog(LOG_BOTS, "*", "Could not link to %s (DNS error).\n", dcc[i].nick);
  1018. lostdcc(i);
  1019. return;
  1020. }
  1021. dcc[i].addr = inet_addr(ip_from_dns.c_str());
  1022. strlcpy(dcc[i].host, ip_from_dns.c_str(), sizeof(dcc[i].host));
  1023. botlink_real(i);
  1024. }
  1025. static void botlink_real(int i)
  1026. {
  1027. int idx = dcc[i].u.dns->ibuf;
  1028. char *linker = strdup(dcc[i].u.dns->cptr);
  1029. free(dcc[i].u.dns->cptr);
  1030. dcc[i].u.dns->cptr = NULL;
  1031. changeover_dcc(i, &DCC_FORK_BOT, sizeof(struct bot_info));
  1032. dcc[i].timeval = now;
  1033. struct bot_info dummy;
  1034. strlcpy(dcc[i].u.bot->version, "(primitive bot)", sizeof(dummy.version));
  1035. strlcpy(dcc[i].u.bot->sysname, "*", 2);
  1036. strlcpy(dcc[i].u.bot->linker, linker, sizeof(dummy.linker));
  1037. dcc[i].u.bot->numver = idx;
  1038. free(linker);
  1039. dcc[i].u.bot->port = dcc[i].port; /* Remember where i started */
  1040. #ifdef USE_IPV6
  1041. int af_type;
  1042. if (dcc[i].port)
  1043. af_type = is_dotted_ip(dcc[i].host);
  1044. else
  1045. af_type = AF_UNIX;
  1046. dcc[i].sock = getsock(SOCK_STRONGCONN, af_type);
  1047. #else
  1048. dcc[i].sock = getsock(SOCK_STRONGCONN);
  1049. #endif /* USE_IPV6 */
  1050. int open_telnet_return = 0;
  1051. if (dcc[i].sock < 0 || (open_telnet_return = open_telnet_raw(dcc[i].sock, dcc[i].host, dcc[i].port, 0, 1)) < 0) {
  1052. if (open_telnet_return == -1)
  1053. dcc[i].sock = -1;
  1054. failed_link(i);
  1055. }
  1056. /* wait for async reply */
  1057. }
  1058. static void failed_tandem_relay(int idx)
  1059. {
  1060. int uidx = (-1), i;
  1061. for (i = 0; i < dcc_total; i++) {
  1062. if (dcc[i].type && (dcc[i].type == &DCC_PRE_RELAY) &&
  1063. (dcc[i].u.relay->sock == dcc[idx].sock))
  1064. uidx = i;
  1065. }
  1066. if (uidx < 0) {
  1067. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1068. if (dcc[idx].sock != -1) {
  1069. killsock(dcc[idx].sock);
  1070. dcc[idx].sock = -1;
  1071. }
  1072. lostdcc(idx);
  1073. return;
  1074. }
  1075. struct chat_info *ci = dcc[uidx].u.relay->chat;
  1076. dprintf(uidx, "Could not relay to %s.\n", dcc[idx].nick);
  1077. dcc[uidx].status = dcc[uidx].u.relay->old_status;
  1078. free(dcc[uidx].u.relay);
  1079. dcc[uidx].u.chat = ci;
  1080. dcc[uidx].type = &DCC_CHAT;
  1081. if (dcc[idx].sock != -1) {
  1082. killsock(dcc[idx].sock);
  1083. dcc[idx].sock = -1;
  1084. }
  1085. lostdcc(idx);
  1086. return;
  1087. }
  1088. static void tandem_relay_dns_callback(int, void *, const char *, bd::Array<bd::String>);
  1089. /* Relay to another tandembot
  1090. */
  1091. void tandem_relay(int idx, char *nick, int i)
  1092. {
  1093. struct userrec *u = get_user_by_handle(userlist, nick);
  1094. if (!u || !u->bot) {
  1095. dprintf(idx, "%s %s\n", nick, "is not a known bot.");
  1096. return;
  1097. }
  1098. if (!strcasecmp(nick, conf.bot->nick)) {
  1099. dprintf(idx, "%s\n", "Relay to myself? What on EARTH would be the point?!");
  1100. return;
  1101. }
  1102. /* Address to connect to is in 'info' */
  1103. struct bot_addr *bi = (struct bot_addr *) get_user(&USERENTRY_BOTADDR, u);
  1104. if (!bi || !strlen(bi->address) || !bi->relay_port || (bi->relay_port <= 0)) {
  1105. dprintf(idx, "Invalid telnet address:port stored for '%s'.\n", nick);
  1106. dprintf(idx, "Use: %schaddr %s <address>:<port#>[/<relay-port#>]\n", (dcc[idx].u.chat->channel >= 0) ? settings.dcc_prefix : "", nick);
  1107. return;
  1108. }
  1109. i = new_dcc(&DCC_DNSWAIT, sizeof(struct dns_info));
  1110. if (i < 0) {
  1111. dprintf(idx, "%s\n", "Sorry, too many DCC connections.");
  1112. return;
  1113. }
  1114. dcc[i].port = bi->relay_port;
  1115. dcc[i].addr = 0L;
  1116. strlcpy(dcc[i].nick, nick, sizeof(dcc[i].nick));
  1117. dcc[i].user = u;
  1118. strlcpy(dcc[i].host, bi->address, sizeof(dcc[i].host));
  1119. if (conf.bot->hub)
  1120. dprintf(idx, "%s %s @ %s:%d ...\n", "Establishing encrypted connection to", nick, bi->address, bi->relay_port);
  1121. dprintf(idx, "(Type *BYE* on a line by itself to abort.)\n");
  1122. dcc[idx].type = &DCC_PRE_RELAY;
  1123. struct chat_info *ci = dcc[idx].u.chat;
  1124. dcc[idx].u.relay = (struct relay_info *) calloc(1, sizeof(struct relay_info));
  1125. dcc[idx].u.relay->chat = ci;
  1126. dcc[idx].u.relay->old_status = dcc[idx].status;
  1127. dcc[idx].u.relay->idx = i;
  1128. dcc[idx].u.relay->sock = -1;
  1129. dcc[i].timeval = now;
  1130. dcc[i].u.dns->ibuf = idx;
  1131. int dns_id = egg_dns_lookup(bi->address, 20, tandem_relay_dns_callback, (void *) (long) i);
  1132. if (dns_id >= 0)
  1133. dcc[i].dns_id = dns_id;
  1134. return;
  1135. /* wait for async reply */
  1136. }
  1137. static void tandem_relay_dns_callback(int id, void *client_data, const char *host, bd::Array<bd::String> ips)
  1138. {
  1139. //64bit hacks
  1140. long data = (long) client_data;
  1141. int i = (int) data, idx = -1;
  1142. Context;
  1143. if (!valid_dns_id(i, id))
  1144. return;
  1145. if (valid_idx(i))
  1146. idx = dcc[i].u.dns->ibuf;
  1147. if (idx < 0) {
  1148. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", i, idx);
  1149. lostdcc(i);
  1150. return;
  1151. }
  1152. bd::String ip_from_dns;
  1153. if (ips.size()) {
  1154. #ifdef USE_IPV6
  1155. /* If IPv6 is available use it, otherwise fall back on IPv4 */
  1156. if (conf.bot->net.v6)
  1157. ip_from_dns = dns_find_ip(ips, AF_INET6);
  1158. if (!ip_from_dns)
  1159. #endif /* USE_IPV6 */
  1160. {
  1161. /* Use IPv4 if no ipv6 is available */
  1162. ip_from_dns = dns_find_ip(ips, AF_INET);
  1163. }
  1164. }
  1165. if (!ips.size() || !ip_from_dns) {
  1166. struct chat_info *ci = dcc[idx].u.relay->chat;
  1167. dprintf(idx, "Could not relay to %s (DNS error).\n", dcc[i].nick);
  1168. dcc[idx].status = dcc[idx].u.relay->old_status;
  1169. free(dcc[idx].u.relay);
  1170. dcc[idx].u.chat = ci;
  1171. dcc[idx].type = &DCC_CHAT;
  1172. lostdcc(i);
  1173. return;
  1174. }
  1175. #ifdef USE_IPV6
  1176. int af = is_dotted_ip(ip_from_dns.c_str());
  1177. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL, af);
  1178. #else
  1179. dcc[i].sock = getsock(SOCK_STRONGCONN | SOCK_VIRTUAL);
  1180. #endif /* USE_IPV6 */
  1181. if (dcc[i].sock < 0) {
  1182. lostdcc(i);
  1183. dprintf(idx, "No free sockets available.\n");
  1184. return;
  1185. }
  1186. dcc[idx].u.relay->sock = dcc[i].sock;
  1187. changeover_dcc(i, &DCC_FORK_RELAY, sizeof(struct relay_info));
  1188. dcc[i].addr = inet_addr(ip_from_dns.c_str());
  1189. dcc[i].u.relay->chat = (struct chat_info *) calloc(1, sizeof(struct chat_info));
  1190. dcc[i].u.relay->sock = dcc[idx].sock;
  1191. dcc[i].u.relay->port = dcc[i].port;
  1192. #ifdef USE_IPV6
  1193. dcc[i].u.relay->af = af;
  1194. #endif
  1195. dcc[i].u.relay->chat->away = NULL;
  1196. dcc[i].u.relay->chat->msgs_per_sec = 0;
  1197. dcc[i].u.relay->chat->con_flags = 0;
  1198. dcc[i].u.relay->chat->buffer = NULL;
  1199. dcc[i].u.relay->chat->max_line = 0;
  1200. dcc[i].u.relay->chat->line_count = 0;
  1201. dcc[i].u.relay->chat->current_lines = 0;
  1202. dcc[i].timeval = now;
  1203. if (open_telnet_raw(dcc[i].sock, ip_from_dns.c_str(), dcc[i].port, 0) < 0) {
  1204. dcc[i].sock = -1;
  1205. failed_tandem_relay(i);
  1206. }
  1207. }
  1208. /* Input from user before connect is ready
  1209. */
  1210. static void pre_relay(int idx, char *buf, int len)
  1211. {
  1212. int tidx = (-1), i;
  1213. for (i = 0; i < dcc_total; i++) {
  1214. if (dcc[i].type && dcc[i].type == &DCC_FORK_RELAY && (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1215. tidx = i;
  1216. break;
  1217. }
  1218. }
  1219. if (tidx < 0) {
  1220. /* Now try to find it among the DNSWAIT sockets instead. */
  1221. for (i = 0; i < dcc_total; i++) {
  1222. if (dcc[i].type && (dcc[i].type == &DCC_DNSWAIT && (dcc[idx].u.relay->idx == i))) {
  1223. tidx = i;
  1224. break;
  1225. }
  1226. }
  1227. }
  1228. if (tidx < 0) {
  1229. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1230. killsock(dcc[idx].sock);
  1231. lostdcc(idx);
  1232. return;
  1233. }
  1234. if (!strcasecmp(buf, "*bye*")) {
  1235. /* Disconnect */
  1236. struct chat_info *ci = dcc[idx].u.relay->chat;
  1237. dprintf(idx, "Aborting relay attempt to %s.\n", dcc[tidx].nick);
  1238. dprintf(idx, "You are now back on %s.\n\n", conf.bot->nick);
  1239. putlog(LOG_MISC, "*", "Relay aborted: %s -> %s", dcc[idx].nick, dcc[tidx].nick);
  1240. dcc[idx].status = dcc[idx].u.relay->old_status;
  1241. free(dcc[idx].u.relay);
  1242. dcc[idx].u.chat = ci;
  1243. dcc[idx].type = &DCC_CHAT;
  1244. if (dcc[tidx].sock != -1)
  1245. killsock(dcc[tidx].sock);
  1246. lostdcc(tidx);
  1247. return;
  1248. }
  1249. }
  1250. /* User disconnected before her relay had finished connecting
  1251. */
  1252. static void failed_pre_relay(int idx)
  1253. {
  1254. int tidx = (-1), i;
  1255. for (i = 0; i < dcc_total; i++) {
  1256. if (dcc[i].type && (dcc[i].type == &DCC_FORK_RELAY) &&
  1257. (dcc[i].u.relay->sock == dcc[idx].sock)) {
  1258. tidx = i;
  1259. break;
  1260. }
  1261. }
  1262. if (tidx < 0) {
  1263. /* Now try to find it among the DNSWAIT sockets instead. */
  1264. for (i = 0; i < dcc_total; i++) {
  1265. if (dcc[i].type && (dcc[i].type == &DCC_DNSWAIT && (dcc[idx].u.relay->idx == i))) {
  1266. tidx = i;
  1267. break;
  1268. }
  1269. }
  1270. }
  1271. if (tidx < 0) {
  1272. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1273. killsock(dcc[idx].sock);
  1274. lostdcc(idx);
  1275. return;
  1276. }
  1277. putlog(LOG_MISC, "*", "%s [%s]%s/%d", "Lost dcc connection to", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  1278. putlog(LOG_MISC, "*", "(%s %s)", "Dropping relay attempt to", dcc[tidx].nick);
  1279. if ((dcc[tidx].sock != STDOUT) || backgrd) {
  1280. if (idx > tidx) {
  1281. int t = tidx;
  1282. tidx = idx;
  1283. idx = t;
  1284. }
  1285. if (dcc[tidx].sock != -1)
  1286. killsock(dcc[tidx].sock);
  1287. lostdcc(tidx);
  1288. } else {
  1289. fatal("Lost my terminal?!", 0);
  1290. }
  1291. if (dcc[idx].sock != -1)
  1292. killsock(dcc[idx].sock);
  1293. lostdcc(idx);
  1294. }
  1295. static void cont_tandem_relay(int idx, char *buf, int len)
  1296. {
  1297. int uidx = (-1), i;
  1298. struct relay_info *ri = NULL;
  1299. for (i = 0; i < dcc_total; i++) {
  1300. if (dcc[i].type && (dcc[i].type == &DCC_PRE_RELAY) && (dcc[i].u.relay->sock == dcc[idx].sock))
  1301. uidx = i;
  1302. }
  1303. if (uidx < 0) {
  1304. putlog(LOG_MISC, "*", "Can't find user for relay! %d -> %d", dcc[idx].sock, dcc[idx].u.relay->sock);
  1305. killsock(dcc[idx].sock);
  1306. lostdcc(idx);
  1307. return;
  1308. }
  1309. dcc[idx].type = &DCC_RELAY;
  1310. dcc[idx].u.relay->sock = dcc[uidx].sock;
  1311. dcc[uidx].u.relay->sock = dcc[idx].sock;
  1312. dprintf(uidx, "%s %s ...\n", "Success!\n\nNOW CONNECTED TO RELAY BOT", dcc[idx].nick);
  1313. dprintf(uidx, "(You can type *BYE* to prematurely close the connection.)\n\n");
  1314. putlog(LOG_MISC, "*", "%s %s -> %s", "Relay link:",
  1315. dcc[uidx].nick, dcc[idx].nick);
  1316. ri = dcc[uidx].u.relay;
  1317. dcc[uidx].type = &DCC_CHAT;
  1318. dcc[uidx].u.chat = ri->chat;
  1319. if (dcc[uidx].u.chat->channel >= 0) {
  1320. chanout_but(-1, dcc[uidx].u.chat->channel, "*** %s left the party line.\n", dcc[uidx].nick);
  1321. if (dcc[uidx].u.chat->channel < GLOBAL_CHANS)
  1322. botnet_send_part_idx(uidx, NULL);
  1323. }
  1324. check_bind_chof(dcc[uidx].nick, uidx);
  1325. dcc[uidx].type = &DCC_RELAYING;
  1326. dcc[uidx].u.relay = ri;
  1327. dprintf(idx, "+%s\n", dcc[uidx].nick);
  1328. }
  1329. static void eof_dcc_relay(int idx)
  1330. {
  1331. int j;
  1332. for (j = 0; j < dcc_total; j++)
  1333. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock)
  1334. break;
  1335. if (j == dcc_total || !dcc[j].type) {
  1336. killsock(dcc[idx].sock);
  1337. lostdcc(idx);
  1338. return;
  1339. }
  1340. dcc[j].status = dcc[j].u.relay->old_status;
  1341. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1342. if (dcc[j].status & STAT_TELNET)
  1343. dprintf(j, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1344. putlog(LOG_MISC, "*", "%s: %s -> %s", "Ended relay link", dcc[j].nick,
  1345. dcc[idx].nick);
  1346. dprintf(j, "\n\n*** %s %s\n", "RELAY CONNECTION DROPPED.\nYou are now back on", conf.bot->nick);
  1347. struct chat_info *ci = dcc[j].u.relay->chat;
  1348. free(dcc[j].u.relay);
  1349. dcc[j].u.chat = ci;
  1350. dcc[j].type = &DCC_CHAT;
  1351. if (dcc[j].u.chat->channel >= 0) {
  1352. chanout_but(-1, dcc[j].u.chat->channel, "*** %s %s.\n",
  1353. dcc[j].nick, "rejoined the party line.");
  1354. if (dcc[j].u.chat->channel < GLOBAL_CHANS)
  1355. botnet_send_join_idx(j);
  1356. }
  1357. check_bind_chon(dcc[j].nick, j);
  1358. killsock(dcc[idx].sock);
  1359. lostdcc(idx);
  1360. }
  1361. static void eof_dcc_relaying(int idx)
  1362. {
  1363. int j, x = dcc[idx].u.relay->sock;
  1364. putlog(LOG_MISC, "*", "%s [%s]%s/%d", "Lost dcc connection to", dcc[idx].nick, dcc[idx].host, dcc[idx].port);
  1365. killsock(dcc[idx].sock);
  1366. lostdcc(idx);
  1367. for (j = 0; j < dcc_total; j++)
  1368. if (dcc[j].type && dcc[j].sock == x && dcc[j].type != &DCC_FORK_RELAY)
  1369. break;
  1370. putlog(LOG_MISC, "*", "(%s %s)", "Dropping relay link to", dcc[j].nick);
  1371. killsock(dcc[j].sock);
  1372. lostdcc(j); /* Drop connection to the bot */
  1373. }
  1374. static void dcc_relay(int idx, char *buf, int j)
  1375. {
  1376. unsigned char *p = (unsigned char *) buf;
  1377. int mark;
  1378. for (j = 0; j < dcc_total; j++)
  1379. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAYING)
  1380. break;
  1381. if (!strncasecmp(buf, STR("neg!"), 4)) { /* something to parse in enclink.c */
  1382. newsplit(&buf);
  1383. link_parse(idx, buf);
  1384. return;
  1385. } else if (!strncasecmp(buf, STR("neg?"), 4)) { /* we're connecting to THEM */
  1386. newsplit(&buf);
  1387. link_challenge_to(idx, buf);
  1388. return;
  1389. }
  1390. /* If redirecting to a non-telnet user, swallow telnet codes and
  1391. escape sequences. */
  1392. if (!(dcc[j].status & STAT_TELNET)) {
  1393. while (*p != 0) {
  1394. while (*p && *p != 255 && *p != '\r')
  1395. p++; /* Search for IAC, escape sequences and CR. */
  1396. if (*p == 255) {
  1397. mark = 2;
  1398. if (!*(p + 1))
  1399. mark = 1; /* Bogus */
  1400. if ((*(p + 1) >= 251) || (*(p + 1) <= 254)) {
  1401. mark = 3;
  1402. if (!*(p + 2))
  1403. mark = 2; /* Bogus */
  1404. }
  1405. memmove((char *) p, (char *) (p + mark), strlen((char *) (p + mark)) + 1);
  1406. } else if (*p == '\r')
  1407. memmove((char *) p, (char *) (p + 1), strlen((char *) p));
  1408. }
  1409. if (!buf[0])
  1410. dprintf(-dcc[idx].u.relay->sock, " \n");
  1411. else
  1412. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1413. return;
  1414. }
  1415. /* Telnet user */
  1416. if (!buf[0])
  1417. dprintf(-dcc[idx].u.relay->sock, " \r\n");
  1418. else
  1419. dprintf(-dcc[idx].u.relay->sock, "%s\r\n", buf);
  1420. }
  1421. static void dcc_relaying(int idx, char *buf, int j)
  1422. {
  1423. if (strcasecmp(buf, "*bye*")) {
  1424. dprintf(-dcc[idx].u.relay->sock, "%s\n", buf);
  1425. return;
  1426. }
  1427. /* The user want's to abort, so return them to partyline */
  1428. for (j = 0; j < dcc_total; j++)
  1429. if (dcc[j].type && dcc[j].sock == dcc[idx].u.relay->sock && dcc[j].type == &DCC_RELAY)
  1430. break;
  1431. dcc[idx].status = dcc[idx].u.relay->old_status;
  1432. /* In case echo was off, turn it back on (send IAC WON'T ECHO): */
  1433. if (dcc[idx].status & STAT_TELNET)
  1434. dprintf(idx, TLN_IAC_C TLN_WONT_C TLN_ECHO_C "\n");
  1435. dprintf(idx, "\n(%s %s.)\n", "Breaking connection to", dcc[j].nick);
  1436. dprintf(idx, "You are now back on %s.\n\n", conf.bot->nick);
  1437. putlog(LOG_MISC, "*", "%s: %s -> %s", "Relay broken", dcc[idx].nick, dcc[j].nick);
  1438. if (dcc[idx].u.relay->chat->channel >= 0) {
  1439. chanout_but(-1, dcc[idx].u.relay->chat->channel, "*** %s joined the party line.\n", dcc[idx].nick);
  1440. if (dcc[idx].u.relay->chat->channel < GLOBAL_CHANS)
  1441. botnet_send_join_idx(idx);
  1442. }
  1443. struct chat_info *ci = dcc[idx].u.relay->chat;
  1444. free(dcc[idx].u.relay);
  1445. dcc[idx].u.chat = ci;
  1446. dcc[idx].type = &DCC_CHAT;
  1447. check_bind_chon(dcc[idx].nick, idx);
  1448. if (dcc[j].sock != -1)
  1449. killsock(dcc[j].sock);
  1450. lostdcc(j);
  1451. }
  1452. static void display_relay(int i, char *other, size_t bufsiz)
  1453. {
  1454. simple_snprintf(other, bufsiz, "rela -> sock %d", dcc[i].u.relay->sock);
  1455. }
  1456. static void display_relaying(int i, char *other, size_t bufsiz)
  1457. {
  1458. simple_snprintf(other, bufsiz, ">rly -> sock %d", dcc[i].u.relay->sock);
  1459. }
  1460. static void display_tandem_relay(int i, char *other, size_t bufsiz)
  1461. {
  1462. strlcpy(other, "other rela", bufsiz);
  1463. }
  1464. static void display_pre_relay(int i, char *other, size_t bufsiz)
  1465. {
  1466. strlcpy(other, "other >rly", bufsiz);
  1467. }
  1468. static void kill_relay(int idx, void *x)
  1469. {
  1470. struct relay_info *p = (struct relay_info *) x;
  1471. if (p->chat)
  1472. DCC_CHAT.kill(idx, p->chat);
  1473. free(p);
  1474. }
  1475. struct dcc_table DCC_RELAY =
  1476. {
  1477. "RELAY",
  1478. 0, /* Flags */
  1479. eof_dcc_relay,
  1480. dcc_relay,
  1481. NULL,
  1482. NULL,
  1483. display_relay,
  1484. kill_relay,
  1485. NULL,
  1486. NULL
  1487. };
  1488. static void out_relay(int idx, char *buf, void *x)
  1489. {
  1490. struct relay_info *p = (struct relay_info *) x;
  1491. if (p && p->chat)
  1492. DCC_CHAT.output(idx, buf, p->chat);
  1493. else
  1494. tputs(dcc[idx].sock, buf, strlen(buf));
  1495. }
  1496. struct dcc_table DCC_RELAYING =
  1497. {
  1498. "RELAYING",
  1499. 0, /* Flags */
  1500. eof_dcc_relaying,
  1501. dcc_relaying,
  1502. NULL,
  1503. NULL,
  1504. display_relaying,
  1505. kill_relay,
  1506. out_relay,
  1507. NULL
  1508. };
  1509. struct dcc_table DCC_FORK_RELAY =
  1510. {
  1511. "FORK_RELAY",
  1512. 0, /* Flags */
  1513. failed_tandem_relay,
  1514. cont_tandem_relay,
  1515. &connect_timeout,
  1516. failed_tandem_relay,
  1517. display_tandem_relay,
  1518. kill_relay,
  1519. NULL,
  1520. NULL
  1521. };
  1522. struct dcc_table DCC_PRE_RELAY =
  1523. {
  1524. "PRE_RELAY",
  1525. 0, /* Flags */
  1526. failed_pre_relay,
  1527. pre_relay,
  1528. NULL,
  1529. NULL,
  1530. display_pre_relay,
  1531. kill_relay,
  1532. NULL,
  1533. NULL
  1534. };
  1535. /* Once a minute, send 'ping' to each bot -- no exceptions
  1536. */
  1537. void check_botnet_pings()
  1538. {
  1539. int i, bots, users, top_index = 0;
  1540. tand_t *bot = NULL;
  1541. for (i = 0; i < dcc_total; i++) {
  1542. if (dcc[i].type) {
  1543. top_index = i;
  1544. if (dcc[i].type == &DCC_BOT) {
  1545. // Hubs only allow localhubs to link, which CAN link bots now, so this isn't so cut and dry now
  1546. #ifdef no
  1547. if (dcc[i].status & STAT_LEAF) {
  1548. tand_t *via = findbot(dcc[i].nick);
  1549. // Check if this leaf has any linked bots
  1550. for (bot = tandbot; bot; bot = bot->next) {
  1551. if ((via == bot->via) && (bot != via)) {
  1552. /* Not leaflike behavior */
  1553. if (dcc[i].status & STAT_WARNED) {
  1554. char s[1024] = "";
  1555. putlog(LOG_BOTS, "*", "%s %s (%s).", "Disconnected from:", dcc[i].nick, "unleaflike behavior");
  1556. dprintf(i, "bye %s\n", "unleaflike behavior");
  1557. bot = findbot(dcc[i].nick);
  1558. bots = bots_in_subtree(bot);
  1559. users = users_in_subtree(bot);
  1560. simple_snprintf(s, sizeof(s), "%s %s (%s) (lost %d bot%s and %d user%s)",
  1561. "Disconnected from:", dcc[i].nick, "unleaflike behavior",
  1562. bots, (bots != 1) ? "s" : "", users, (users != 1) ?
  1563. "s" : "");
  1564. chatout("*** %s\n", s);
  1565. botnet_send_unlinked(i, dcc[i].nick, s);
  1566. killsock(dcc[i].sock);
  1567. lostdcc(i);
  1568. } else {
  1569. botnet_send_reject(i, conf.bot->nick, NULL, bot->bot, NULL, NULL);
  1570. dcc[i].status |= STAT_WARNED;
  1571. }
  1572. } else
  1573. dcc[i].status &= ~STAT_WARNED;
  1574. }
  1575. }
  1576. #endif
  1577. if (dcc[i].status & STAT_PINGED) {
  1578. char s[1024] = "";
  1579. putlog(LOG_BOTS, "*", "%s: %s", "Ping timeout", dcc[i].nick);
  1580. bot = findbot(dcc[i].nick);
  1581. bots = bots_in_subtree(bot);
  1582. users = users_in_subtree(bot);
  1583. simple_snprintf(s, sizeof(s), "%s: %s (lost %d bot%s and %d user%s)", "Ping timeout",
  1584. dcc[i].nick, bots, (bots != 1) ? "s" : "",
  1585. users, (users != 1) ? "s" : "");
  1586. chatout("*** %s\n", s);
  1587. botnet_send_unlinked(i, dcc[i].nick, s);
  1588. killsock(dcc[i].sock);
  1589. lostdcc(i);
  1590. } else {
  1591. botnet_send_ping(i);
  1592. dcc[i].status |= STAT_PINGED;
  1593. }
  1594. }
  1595. }
  1596. }
  1597. if (top_index != (dcc_total - 1))
  1598. dcc_total = top_index + 1;
  1599. }
  1600. void zapfbot(int idx)
  1601. {
  1602. char s[1024] = "";
  1603. tand_t *bot = findbot(dcc[idx].nick);
  1604. int bots = bots_in_subtree(bot), users = users_in_subtree(bot);
  1605. simple_snprintf(s, sizeof(s), "%s: %s (lost %d bot%s and %d user%s)", "Dropped bot",
  1606. dcc[idx].nick, bots, (bots != 1) ? "s" : "", users,
  1607. (users != 1) ? "s" : "");
  1608. chatout("*** %s\n", s);
  1609. botnet_send_unlinked(idx, dcc[idx].nick, s);
  1610. killsock(dcc[idx].sock);
  1611. lostdcc(idx);
  1612. }
  1613. /* vim: set sts=2 sw=2 ts=8 et: */