botnet.cc 50 KB

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