botnet.cc 50 KB

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