botnet.cc 50 KB

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