botnet.cc 51 KB

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