botnet.cc 49 KB

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