botnet.cc 50 KB

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