botnet.cc 49 KB

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