botcmd.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2008 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. * botcmd.c -- handles:
  22. * commands that comes across the botnet
  23. * userfile transfer and update commands from sharebots
  24. *
  25. */
  26. #include "common.h"
  27. #include "botcmd.h"
  28. #include "main.h"
  29. #include "base64.h"
  30. #include "src/mod/share.mod/share.h"
  31. #include "src/mod/update.mod/update.h"
  32. #include "net.h"
  33. #include "binds.h"
  34. #include "misc.h"
  35. #include "dcc.h"
  36. #include "userrec.h"
  37. #include "dccutil.h"
  38. #include "cmds.h"
  39. #include "chanprog.h"
  40. #include "botmsg.h"
  41. #include "botnet.h"
  42. #include "tandem.h"
  43. #include "users.h"
  44. #include "chan.h"
  45. #include "core_binds.h"
  46. #include "egg_timer.h"
  47. static char TBUF[1024] = ""; /* Static buffer for goofy bot stuff */
  48. static void fake_alert(int idx, char *item, char *extra, char *what)
  49. {
  50. static time_t lastfake; /* The last time fake_alert was used */
  51. if ((now - lastfake) > 10) {
  52. /* Don't fake_alert more than once every 10secs */
  53. dprintf(idx, "ct %s NOTICE: %s (%s != %s). (%s)\n", conf.bot->nick, "Fake message rejected", item, extra, what);
  54. putlog(LOG_BOTS, "*", "%s %s (%s != %s). (%s)", dcc[idx].nick, "Fake message rejected", item, extra, what);
  55. lastfake = now;
  56. }
  57. }
  58. /* chan <from> <chan> <text>
  59. */
  60. static void bot_chan2(int idx, char *msg)
  61. {
  62. char *from = NULL, *p = NULL;
  63. int i, chan;
  64. from = newsplit(&msg);
  65. p = newsplit(&msg);
  66. chan = base64_to_int(p);
  67. /* Strip annoying control chars */
  68. for (p = from; *p;) {
  69. if ((*p < 32) || (*p == 127))
  70. memmove(p, p + 1, strlen(p));
  71. else
  72. p++;
  73. }
  74. p = strchr(from, '@');
  75. if (p) {
  76. if (!conf.bot->hub) { /* Need to strip out the hub nick */
  77. char *q = NULL, newfrom[HANDLEN + 9 + 1]; /* HANDLEN@[botnet] */
  78. strlcpy(newfrom, from, sizeof(newfrom));
  79. q = strchr(newfrom, '@');
  80. *q = 0;
  81. simple_snprintf(TBUF, sizeof(TBUF), "<%s@[botnet]> %s", newfrom, msg);
  82. } else
  83. simple_snprintf(TBUF, sizeof(TBUF), "<%s> %s", from, msg);
  84. *p = 0;
  85. if (!partyidle(p + 1, from)) {
  86. *p = '@';
  87. fake_alert(idx, "user", from, "chan2_i");
  88. return;
  89. }
  90. *p = '@';
  91. p++;
  92. } else {
  93. simple_snprintf(TBUF, sizeof(TBUF), "*** (%s) %s", from, msg);
  94. p = from;
  95. }
  96. i = nextbot(p);
  97. if (i != idx) {
  98. fake_alert(idx, "direction", p, "chan2_ii");
  99. } else {
  100. chanout_but(-1, chan, "%s\n", TBUF);
  101. /* Send to new version bots */
  102. if (i >= 0)
  103. botnet_send_chan(idx, from, NULL, chan, msg);
  104. }
  105. }
  106. void bot_cmdpass(int idx, char *par)
  107. {
  108. char *p = strchr(par, ' ');
  109. if (p) {
  110. *p++ = 0;
  111. botnet_send_cmdpass(idx, par, p);
  112. p--;
  113. *p = ' ';
  114. } else {
  115. botnet_send_cmdpass(idx, par, "");
  116. }
  117. set_cmd_pass(par, 0);
  118. }
  119. void bot_set(int idx, char *par)
  120. {
  121. var_userfile_share_line(par, idx, 0);
  122. }
  123. void bot_setbroad(int idx, char *par)
  124. {
  125. var_userfile_share_line(par, idx, 1);
  126. }
  127. void bot_remotecmd(int idx, char *par) {
  128. char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
  129. if (par[0])
  130. tbot = newsplit(&par);
  131. if (par[0])
  132. fbot = newsplit(&par);
  133. if (par[0])
  134. fhnd = newsplit(&par);
  135. if (par[0])
  136. fidx = newsplit(&par);
  137. /* Make sure the bot is valid */
  138. int i = nextbot(fbot);
  139. if (i != idx) {
  140. fake_alert(idx, "direction", fbot, "rc");
  141. return;
  142. }
  143. if (!strcasecmp(tbot, conf.bot->nick)) {
  144. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  145. } else if (!strcmp(tbot, "*")) {
  146. botnet_send_cmd_broad(idx, fbot, fhnd, atoi(fidx), par);
  147. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  148. } else {
  149. if (nextbot(tbot) != idx)
  150. botnet_send_cmd(fbot, tbot, fhnd, atoi(fidx), par);
  151. }
  152. }
  153. void bot_remotereply(int idx, char *par) {
  154. char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
  155. if (par[0])
  156. tbot = newsplit(&par);
  157. if (par[0])
  158. fbot = newsplit(&par);
  159. if (par[0])
  160. fhnd = newsplit(&par);
  161. if (par[0])
  162. fidx = newsplit(&par);
  163. /* Make sure the bot is valid */
  164. int i = nextbot(fbot);
  165. if (i != idx) {
  166. fake_alert(idx, "direction", fbot, "rr");
  167. return;
  168. }
  169. if (!strcasecmp(tbot, conf.bot->nick)) {
  170. gotremotereply(fbot, fhnd, fidx, par);
  171. } else {
  172. if (nextbot(tbot)!= idx)
  173. botnet_send_cmdreply(fbot, tbot, fhnd, fidx, par);
  174. }
  175. }
  176. /* chat <from> <notice> -- only from bots
  177. */
  178. /* this is only sent to hub bots */
  179. static void bot_chat(int idx, char *par)
  180. {
  181. char *from = NULL;
  182. int i;
  183. from = newsplit(&par);
  184. if (strchr(from, '@') != NULL) {
  185. fake_alert(idx, "bot", from, "chat_i");
  186. return;
  187. }
  188. /* Make sure the bot is valid */
  189. i = nextbot(from);
  190. if (i != idx) {
  191. fake_alert(idx, "direction", from, "chat_ii");
  192. return;
  193. }
  194. chatout("*** (%s) %s\n", from, par);
  195. botnet_send_chat(idx, from, par);
  196. }
  197. /* actchan <from> <chan> <text>
  198. */
  199. static void bot_actchan(int idx, char *par)
  200. {
  201. char *from = NULL, *p = NULL;
  202. int i, chan;
  203. from = newsplit(&par);
  204. p = strchr(from, '@');
  205. if (p == NULL) {
  206. /* How can a bot do an action? */
  207. fake_alert(idx, "user@bot", from, "actchan_i");
  208. return;
  209. }
  210. *p = 0;
  211. if (!partyidle(p + 1, from)) {
  212. *p = '@';
  213. fake_alert(idx, "user", from, "actchan_ii");
  214. return;
  215. }
  216. *p = '@';
  217. p++;
  218. i = nextbot(p);
  219. if (i != idx) {
  220. fake_alert(idx, "direction", p, "actchan_iii");
  221. return;
  222. }
  223. p = newsplit(&par);
  224. chan = base64_to_int(p);
  225. for (p = from; *p;) {
  226. if ((*p < 32) || (*p == 127))
  227. memmove(p, p + 1, strlen(p));
  228. else
  229. p++;
  230. }
  231. if (!conf.bot->hub) { /* Need to strip out the hub nick */
  232. char *q = NULL, newfrom[HANDLEN + 9 + 1]; /* HANDLEN@[botnet] */
  233. strlcpy(newfrom, from, sizeof(newfrom));
  234. q = strchr(newfrom, '@');
  235. *q = 0;
  236. chanout_but(-1, chan, "* %s@[botnet] %s\n", newfrom, par);
  237. } else
  238. chanout_but(-1, chan, "* %s %s\n", from, par);
  239. botnet_send_act(idx, from, NULL, chan, par);
  240. }
  241. /* priv <from> <to> <message>
  242. */
  243. static void bot_priv(int idx, char *par)
  244. {
  245. char *from = NULL, *p = NULL, *to = TBUF, *tobot = NULL;
  246. int i;
  247. from = newsplit(&par);
  248. tobot = newsplit(&par);
  249. splitc(to, tobot, '@');
  250. p = strchr(from, '@');
  251. if (p != NULL)
  252. p++;
  253. else
  254. p = from;
  255. i = nextbot(p);
  256. if (i != idx) {
  257. fake_alert(idx, "direction", p, "priv_i");
  258. return;
  259. }
  260. if (!to[0])
  261. return; /* Silently ignore notes to '@bot' this
  262. * is legacy code */
  263. if (!strcasecmp(tobot, conf.bot->nick)) { /* For me! */
  264. if (p == from)
  265. add_note(to, from, par, -2, 0);
  266. else {
  267. i = add_note(to, from, par, -1, 0);
  268. if (from[0] != '@')
  269. switch (i) {
  270. case NOTE_ERROR:
  271. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s.", "No such user", to);
  272. break;
  273. }
  274. }
  275. } else { /* Pass it on */
  276. i = nextbot(tobot);
  277. if (i >= 0)
  278. botnet_send_priv(i, from, to, tobot, "%s", par);
  279. }
  280. }
  281. static void bot_bye(int idx, char *par)
  282. {
  283. char s[1024] = "";
  284. int users, bots;
  285. bots = bots_in_subtree(findbot(dcc[idx].nick));
  286. users = users_in_subtree(findbot(dcc[idx].nick));
  287. simple_snprintf(s, sizeof(s), "%s %s. %s (lost %d bot%s and %d user%s)",
  288. "Disconnected from:", conf.bot->hub ? dcc[idx].nick : "botnet", par[0] ?
  289. par : "No reason", bots, (bots != 1) ?
  290. "s" : "", users, (users != 1) ? "s" : "");
  291. putlog(LOG_BOTS, "*", "%s", s);
  292. chatout("*** %s\n", s);
  293. botnet_send_unlinked(idx, dcc[idx].nick, s);
  294. dprintf(idx, "*bye\n");
  295. killsock(dcc[idx].sock);
  296. lostdcc(idx);
  297. }
  298. static void remote_tell_who(int idx, char *nick, int chan)
  299. {
  300. int i = 10, k, l, ok = 0;
  301. char s[1024] = "", *realnick = NULL;
  302. struct chanset_t *c = NULL;
  303. realnick = strchr(nick, ':');
  304. if (realnick)
  305. realnick++;
  306. else
  307. realnick = nick;
  308. putlog(LOG_BOTS, "*", "#%s# who", realnick);
  309. strlcpy(s, "Channels: ", sizeof(s));
  310. for (c = chanset; c; c = c->next)
  311. if (!channel_secret(c) && shouldjoin(c)) {
  312. l = strlen(c->dname);
  313. if (i + l < 1021) {
  314. if (i > 10) {
  315. strlcat(s, ", ", sizeof(s));
  316. strlcat(s, c->dname, sizeof(s));
  317. } else {
  318. strlcpy(s, c->dname, sizeof(s));
  319. i += (l + 2);
  320. }
  321. }
  322. }
  323. if (i > 10) {
  324. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", s, ver);
  325. } else {
  326. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", "no channels", ver);
  327. }
  328. if (admin[0])
  329. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "Admin: %s", admin);
  330. if (chan == 0) {
  331. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  332. "Party line members: (^ = admin, * = owner, + = master, @ = op)");
  333. } else {
  334. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  335. "People on channel %s%d: (^ = admin, * = owner, + = master, @ = op)\n",
  336. (chan < GLOBAL_CHANS) ? "" : "*",
  337. chan % GLOBAL_CHANS);
  338. }
  339. for (i = 0; i < dcc_total; i++) {
  340. if (dcc[i].type && dcc[i].type->flags & DCT_REMOTEWHO) {
  341. if (dcc[i].u.chat->channel == chan) {
  342. k = simple_snprintf(s, sizeof(s), " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
  343. dcc[i].nick, dcc[i].host);
  344. if (now - dcc[i].timeval > 300) {
  345. unsigned long mydays, hrs, mins;
  346. mydays = (now - dcc[i].timeval) / 86400;
  347. hrs = ((now - dcc[i].timeval) - (mydays * 86400)) / 3600;
  348. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  349. if (mydays > 0)
  350. simple_snprintf(s + k, sizeof(s) - k, " (idle %lud%luh)", mydays, hrs);
  351. else if (hrs > 0)
  352. simple_snprintf(s + k, sizeof(s) - k, " (idle %luh%lum)", hrs, mins);
  353. else
  354. simple_snprintf(s + k, sizeof(s) - k, " (idle %lum)", mins);
  355. }
  356. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  357. if (dcc[i].u.chat->away != NULL)
  358. botnet_send_priv(idx, conf.bot->nick, nick, NULL, " AWAY: %s", dcc[i].u.chat->away);
  359. }
  360. }
  361. }
  362. for (i = 0; i < dcc_total; i++) {
  363. if (dcc[i].type && dcc[i].type == &DCC_BOT) {
  364. if (!ok) {
  365. ok = 1;
  366. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", "Bots connected");
  367. }
  368. simple_snprintf(s, sizeof(s), " %s%c%-15s %s",
  369. dcc[i].status & STAT_CALLED ? "<-" : "->",
  370. dcc[i].status & STAT_SHARE ? '+' : ' ',
  371. dcc[i].nick, dcc[i].u.bot->version);
  372. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  373. }
  374. }
  375. ok = 0;
  376. for (i = 0; i < dcc_total; i++) {
  377. if (dcc[i].type && dcc[i].type->flags & DCT_REMOTEWHO) {
  378. if (dcc[i].u.chat->channel != chan) {
  379. if (!ok) {
  380. ok = 1;
  381. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:", "Other people on the bot");
  382. }
  383. l = simple_snprintf(s, sizeof(s), " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)), dcc[i].nick, dcc[i].host);
  384. if (now - dcc[i].timeval > 300) {
  385. k = (now - dcc[i].timeval) / 60;
  386. if (k < 60)
  387. simple_snprintf(s + l, sizeof(s) - l, " (idle %dm)", k);
  388. else
  389. simple_snprintf(s + l, sizeof(s) - l, " (idle %dh%dm)", k / 60, k % 60);
  390. }
  391. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  392. if (dcc[i].u.chat->away != NULL)
  393. botnet_send_priv(idx, conf.bot->nick, nick, NULL, " AWAY: %s", dcc[i].u.chat->away);
  394. }
  395. }
  396. }
  397. }
  398. static void bot_shellinfo(int idx, char *par)
  399. {
  400. char *username = NULL, *sysname = NULL, *nodename = NULL, *arch = NULL, *botversion = NULL;
  401. username = newsplit(&par);
  402. sysname = newsplit(&par);
  403. nodename = newsplit(&par);
  404. arch = newsplit(&par);
  405. botversion = newsplit(&par);
  406. set_user(&USERENTRY_USERNAME, dcc[idx].user, username);
  407. set_user(&USERENTRY_OS, dcc[idx].user, sysname);
  408. dcc[idx].u.bot->sysname[0] = 0;
  409. struct bot_info dummy;
  410. strlcpy(dcc[idx].u.bot->sysname, sysname, sizeof(dummy.sysname));
  411. set_user(&USERENTRY_NODENAME, dcc[idx].user, nodename);
  412. set_user(&USERENTRY_ARCH, dcc[idx].user, arch);
  413. set_user(&USERENTRY_OSVER, dcc[idx].user, botversion);
  414. }
  415. /* who <from@bot> <tobot> <chan#>
  416. */
  417. static void bot_who(int idx, char *par)
  418. {
  419. char *from = NULL, *to = NULL, *p = NULL;
  420. int i, chan;
  421. from = newsplit(&par);
  422. p = strchr(from, '@');
  423. if (!p) {
  424. simple_snprintf(TBUF, sizeof(TBUF), "%s@%s", from, dcc[idx].nick);
  425. from = TBUF;
  426. }
  427. to = newsplit(&par);
  428. if (!strcasecmp(to, conf.bot->nick))
  429. to[0] = 0; /* (for me) */
  430. chan = base64_to_int(par);
  431. if (to[0]) { /* Pass it on */
  432. i = nextbot(to);
  433. if (i >= 0)
  434. botnet_send_who(i, from, to, chan);
  435. } else {
  436. remote_tell_who(idx, from, chan);
  437. }
  438. }
  439. static void bot_endlink(int idx, char *par)
  440. {
  441. dcc[idx].status &= ~STAT_LINKING;
  442. }
  443. static void bot_ping(int idx, char *par)
  444. {
  445. botnet_send_pong(idx);
  446. }
  447. static void bot_pong(int idx, char *par)
  448. {
  449. dcc[idx].status &= ~STAT_PINGED;
  450. if (dcc[idx].pingtime > (now - 120))
  451. dcc[idx].pingtime -= now;
  452. else
  453. dcc[idx].pingtime = 120;
  454. }
  455. /* link <from@bot> <who> <to-whom>
  456. */
  457. static void bot_link(int idx, char *par)
  458. {
  459. char *from = NULL, *bot = NULL, *rfrom = NULL;
  460. int i;
  461. from = newsplit(&par);
  462. bot = newsplit(&par);
  463. if (!strcasecmp(bot, conf.bot->nick)) {
  464. if ((rfrom = strchr(from, ':')))
  465. rfrom++;
  466. else
  467. rfrom = from;
  468. putlog(LOG_CMDS, "*", "#%s# link %s", rfrom, par);
  469. if (botlink(from, -1, par))
  470. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s ...", "Attempting to link", par);
  471. else
  472. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s.", "Can't link there");
  473. } else {
  474. i = nextbot(bot);
  475. if (i >= 0)
  476. botnet_send_link(i, from, bot, par);
  477. }
  478. }
  479. /*
  480. bot_log
  481. - forwards to all hubs linked directly if a hub (excluding the one who sent it)
  482. - leaf bots display and do not pass along.
  483. */
  484. static void bot_log(int idx, char *par)
  485. {
  486. char *from = newsplit(&par);
  487. int i = nextbot(from);
  488. if (i != idx) {
  489. fake_alert(idx, "direction", from, "log");
  490. return;
  491. }
  492. if (egg_isdigit(par[0])) {
  493. int type = atoi(newsplit(&par));
  494. if (conf.bot->hub)
  495. botnet_send_log(idx, from, type, par);
  496. putlog(type, "@", "(%s) %s", from, par);
  497. } else {
  498. putlog(LOG_ERRORS, "*", "Malformed HL line from %s: %s", from, par);
  499. }
  500. }
  501. /* unlink <from@bot> <linking-bot> <undesired-bot> <reason>
  502. */
  503. static void bot_unlink(int idx, char *par)
  504. {
  505. char *from = NULL, *bot = NULL, *rfrom = NULL, *p = NULL, *undes = NULL;
  506. int i;
  507. from = newsplit(&par);
  508. bot = newsplit(&par);
  509. undes = newsplit(&par);
  510. if (!strcasecmp(bot, conf.bot->nick)) {
  511. if ((rfrom = strchr(from, ':')))
  512. rfrom++;
  513. else
  514. rfrom = from;
  515. putlog(LOG_CMDS, "*", "#%s# unlink %s (%s)", rfrom, undes, par[0] ? par : "No reason");
  516. i = botunlink(-3, undes, par[0] ? par : NULL);
  517. if (i == 1) {
  518. p = strchr(from, '@');
  519. if (p) {
  520. /* idx will change after unlink -- get new idx
  521. *
  522. * TODO: This has changed with the new lostdcc() behaviour. Check
  523. * if we can optimise the situation.
  524. */
  525. i = nextbot(p + 1);
  526. if (i >= 0)
  527. botnet_send_priv(i, conf.bot->nick, from, NULL, "Unlinked from %s.", undes);
  528. }
  529. } else if (i == 0) {
  530. botnet_send_unlinked(-1, undes, "");
  531. p = strchr(from, '@');
  532. if (p) {
  533. /* Ditto above, about idx */
  534. i = nextbot(p + 1);
  535. if (i >= 0)
  536. botnet_send_priv(i, conf.bot->nick, from, NULL, "%s %s.", "Can't unlink", undes);
  537. }
  538. } else {
  539. p = strchr(from, '@');
  540. if (p) {
  541. i = nextbot(p + 1);
  542. if (i >= 0)
  543. botnet_send_priv(i, conf.bot->nick, from, NULL, "Can't remotely unlink sharebots.");
  544. }
  545. }
  546. } else {
  547. i = nextbot(bot);
  548. if (i >= 0)
  549. botnet_send_unlink(i, from, bot, undes, par);
  550. }
  551. }
  552. /* Bot next share?
  553. */
  554. static void bot_update(int idx, char *par)
  555. {
  556. char *bot = NULL, x, *vversion = NULL, *vcommit = NULL;
  557. int vlocalhub = 0;
  558. time_t vbuildts = 0L;
  559. bot = newsplit(&par);
  560. x = par[0];
  561. if (x)
  562. par++;
  563. newsplit(&par); /* vnum */
  564. if (par[0])
  565. vlocalhub = atoi(newsplit(&par));
  566. if (par[0])
  567. vbuildts = atol(newsplit(&par));
  568. if (par[0])
  569. vcommit = newsplit(&par);
  570. if (par[0])
  571. vversion = newsplit(&par);
  572. if (in_chain(bot))
  573. updatebot(idx, bot, x, vlocalhub, vbuildts, vcommit, vversion);
  574. }
  575. /* Newbot next share?
  576. */
  577. static void bot_nlinked(int idx, char *par)
  578. {
  579. char *newbot = NULL, *next = NULL, *p = NULL, s[1024] = "", x = 0, *vversion = NULL, *vcommit = NULL;
  580. int i, vlocalhub = 0;
  581. time_t vbuildts = 0L;
  582. bool bogus = 0;
  583. newbot = newsplit(&par);
  584. next = newsplit(&par);
  585. s[0] = 0;
  586. if (!next[0]) {
  587. putlog(LOG_BOTS, "*", "Invalid eggnet protocol from %s (zapfing)", dcc[idx].nick);
  588. simple_snprintf(s, sizeof(s), "Disconnected %s (invalid bot)", dcc[idx].nick);
  589. dprintf(idx, "error invalid eggnet protocol for 'nlinked'\n");
  590. } else if ((in_chain(newbot)) || (!strcasecmp(newbot, conf.bot->nick))) {
  591. /* Loop! */
  592. putlog(LOG_BOTS, "*", "Loop detected %s (mutual: %s)", dcc[idx].nick, newbot);
  593. simple_snprintf(s, sizeof(s), "Detected loop: two bots exist named %s: disconnecting %s", newbot, dcc[idx].nick);
  594. dprintf(idx, "error Loop (%s)\n", newbot);
  595. }
  596. if (!s[0]) {
  597. for (p = newbot; *p; p++)
  598. if ((*p < 32) || (*p == 127))
  599. bogus = 1;
  600. i = nextbot(next);
  601. if (i != idx)
  602. bogus = 1;
  603. if (bogus) {
  604. putlog(LOG_BOTS, "*", "Bogus link notice from %s! (%s -> %s)", dcc[idx].nick, next, newbot);
  605. simple_snprintf(s, sizeof(s), "Bogus link notice from: %s Disconnected", dcc[idx].nick);
  606. dprintf(idx, "error Bogus link notice from (%s -> %s)\n", next, newbot);
  607. }
  608. }
  609. if (s[0]) {
  610. chatout("*** %s\n", s);
  611. botnet_send_unlinked(idx, dcc[idx].nick, s);
  612. dprintf(idx, "bye %s\n", s);
  613. killsock(dcc[idx].sock);
  614. lostdcc(idx);
  615. return;
  616. }
  617. x = par[0];
  618. if (x)
  619. par++;
  620. else
  621. x = '-';
  622. if (par[0])
  623. newsplit(&par); /* vnum */
  624. if (par[0])
  625. vlocalhub = atoi(newsplit(&par));
  626. if (par[0])
  627. vbuildts = atol(newsplit(&par));
  628. if (par[0])
  629. vcommit = newsplit(&par);
  630. if (par[0])
  631. vversion = newsplit(&par);
  632. botnet_send_nlinked(idx, newbot, next, x, vlocalhub, vbuildts, vcommit, vversion);
  633. if (x == '!') {
  634. if (conf.bot->hub)
  635. chatout("*** (%s) %s %s.\n", next, "Linked to", newbot);
  636. else
  637. chatout("*** %s linked to botnet.\n", newbot);
  638. x = '-';
  639. }
  640. addbot(newbot, dcc[idx].nick, next, x, vlocalhub, vbuildts, vcommit, vversion ? vversion : (char *) "");
  641. }
  642. static void bot_unlinked(int idx, char *par)
  643. {
  644. int i;
  645. char *bot = NULL;
  646. bot = newsplit(&par);
  647. i = nextbot(bot);
  648. if ((i >= 0) && (i != idx)) /* Bot is NOT downstream along idx, so
  649. * BOGUS! */
  650. fake_alert(idx, "direction", bot, "unlinked");
  651. else if (i >= 0) { /* Valid bot downstream of idx */
  652. if (par[0])
  653. /* #ifdef HUB */
  654. chatout("*** (%s) %s\n", lastbot(bot), par);
  655. /*
  656. #else
  657. chatout("*** %s unlinked from botnet.\n", par);
  658. #endif
  659. */
  660. botnet_send_unlinked(idx, bot, par);
  661. unvia(idx, findbot(bot));
  662. rembot(bot);
  663. }
  664. /* Otherwise it's not even a valid bot, so just ignore! */
  665. }
  666. /* trace <from@bot> <dest> <chain:chain..>
  667. */
  668. static void bot_trace(int idx, char *par)
  669. {
  670. char *from = NULL, *dest = NULL;
  671. int i;
  672. from = newsplit(&par);
  673. dest = newsplit(&par);
  674. simple_snprintf(TBUF, sizeof(TBUF), "%s:%s", par, conf.bot->nick);
  675. botnet_send_traced(idx, from, TBUF);
  676. if (strcasecmp(dest, conf.bot->nick) && ((i = nextbot(dest)) > 0))
  677. botnet_send_trace(i, from, dest, par);
  678. }
  679. /* traced <to@bot> <chain:chain..>
  680. */
  681. static void bot_traced(int idx, char *par)
  682. {
  683. char *to = NULL, *p = NULL;
  684. int i, sock;
  685. to = newsplit(&par);
  686. p = strchr(to, '@');
  687. if (p == NULL)
  688. p = to;
  689. else {
  690. *p = 0;
  691. p++;
  692. }
  693. if (!strcasecmp(p, conf.bot->nick)) {
  694. time_t t = 0;
  695. char *p2 = par, *ss = TBUF;
  696. splitc(ss, to, ':');
  697. if (ss[0])
  698. sock = atoi(ss);
  699. else
  700. sock = (-1);
  701. if (par[0] == ':') {
  702. t = atoi(par + 1);
  703. p2 = strchr(par + 1, ':');
  704. if (p2)
  705. p2++;
  706. else
  707. p2 = par + 1;
  708. }
  709. for (i = 0; i < dcc_total; i++) {
  710. if (dcc[i].type && (dcc[i].type->flags & DCT_CHAT) && (!strcasecmp(dcc[i].nick, to)) &&
  711. ((sock == (-1)) || (sock == dcc[i].sock))) {
  712. if (t) {
  713. int j=0;
  714. register char *c = p2;
  715. for (; *c != '\0'; c++) if (*c == ':') j++;
  716. time_t tm;
  717. egg_timeval_t tv;
  718. timer_get_now(&tv);
  719. tm = ((tv.sec % 10000) * 100 + (tv.usec * 100) / (1000000)) - t;
  720. dprintf(i, "%s -> %s (%d.%d secs, %d hop%s)\n", "Trace result", p2,
  721. (int)(tm / 100), (int)(tm % 100), j, (j != 1) ? "s" : "");
  722. } else
  723. dprintf(i, "%s -> %s\n", "Trace result", p);
  724. }
  725. }
  726. } else {
  727. i = nextbot(p);
  728. if (p != to)
  729. *--p = '@';
  730. if (i >= 0)
  731. botnet_send_traced(i, to, par);
  732. }
  733. }
  734. static void bot_timesync(int idx, char *par)
  735. {
  736. // putlog(LOG_DEBUG, "@", "Got timesync from %s: %s (%li - %li)", dcc[idx].nick, par, atol(par), now);
  737. timesync = atol(par) - now;
  738. if (conf.bot->hub)
  739. send_timesync(-1);
  740. }
  741. /* reject <from> <bot>
  742. */
  743. static void bot_reject(int idx, char *par)
  744. {
  745. char *from = NULL, *who = NULL, *destbot = NULL, *frombot = NULL;
  746. struct userrec *u = NULL;
  747. int i;
  748. from = newsplit(&par);
  749. frombot = strchr(from, '@');
  750. if (frombot)
  751. frombot++;
  752. else
  753. frombot = from;
  754. i = nextbot(frombot);
  755. if (i != idx) {
  756. fake_alert(idx, "direction", frombot, "reject");
  757. return;
  758. }
  759. who = newsplit(&par);
  760. destbot = strchr(who, '@');
  761. *destbot++ = 0;
  762. if (!strcasecmp(destbot, conf.bot->nick)) {
  763. /* Kick someone here! */
  764. int ok = 0;
  765. for (i = 0; i < dcc_total; i++) {
  766. if (dcc[i].type && dcc[i].simul == -1 && !strcasecmp(who, dcc[i].nick) && (dcc[i].type->flags & DCT_CHAT)) {
  767. u = get_user_by_handle(userlist, from);
  768. if (u) {
  769. if (!whois_access(u, dcc[idx].user)) {
  770. add_note(from, conf.bot->nick, "Sorry, you cannot boot them.", -1, 0);
  771. return;
  772. }
  773. do_boot(i, from, par);
  774. putlog(LOG_CMDS, "*", "#%s# boot %s (%s)", from, who, par[0] ? par : "No reason");
  775. ok = 1;
  776. }
  777. }
  778. }
  779. } else {
  780. i = nextbot(destbot);
  781. *--destbot = '@';
  782. if (i >= 0)
  783. botnet_send_reject(i, from, NULL, who, NULL, par);
  784. }
  785. }
  786. static void bot_thisbot(int idx, char *par)
  787. {
  788. if (strcasecmp(par, dcc[idx].nick)) {
  789. char s[1024] = "";
  790. putlog(LOG_BOTS, "*", "Wrong bot--wanted %s, got %s", dcc[idx].nick, par);
  791. dprintf(idx, "bye imposter\n");
  792. simple_snprintf(s, sizeof(s), "Disconnected %s (imposter)", dcc[idx].nick);
  793. chatout("*** %s\n", s);
  794. botnet_send_unlinked(idx, dcc[idx].nick, s);
  795. unvia(idx, findbot(dcc[idx].nick));
  796. killsock(dcc[idx].sock);
  797. lostdcc(idx);
  798. return;
  799. }
  800. /* Set capitalization the way they want it */
  801. noshare = 1;
  802. change_handle(dcc[idx].user, par);
  803. noshare = 0;
  804. strlcpy(dcc[idx].nick, par, NICKLEN);
  805. }
  806. /* Used to send a direct msg from Tcl on one bot to Tcl on another
  807. * zapf <frombot> <tobot> <code [param]>
  808. */
  809. static void bot_zapf(int idx, char *par)
  810. {
  811. char *from = NULL, *to = NULL;
  812. int i;
  813. from = newsplit(&par);
  814. to = newsplit(&par);
  815. i = nextbot(from);
  816. if (i != idx) {
  817. fake_alert(idx, "direction", from, "zapf");
  818. return;
  819. }
  820. if (!strcasecmp(to, conf.bot->nick)) {
  821. /* For me! */
  822. char *opcode;
  823. opcode = newsplit(&par);
  824. check_bind_bot(from, opcode, par);
  825. return;
  826. } else {
  827. i = nextbot(to);
  828. if (i >= 0)
  829. botnet_send_zapf(i, from, to, par);
  830. }
  831. }
  832. /* Used to send a global msg from Tcl on one bot to every other bot
  833. * zapf-broad <frombot> <code [param]>
  834. */
  835. static void bot_zapfbroad(int idx, char *par)
  836. {
  837. char *from = NULL, *opcode = NULL;
  838. int i;
  839. from = newsplit(&par);
  840. opcode = newsplit(&par);
  841. i = nextbot(from);
  842. if (i != idx) {
  843. fake_alert(idx, "direction", from, "zapfb");
  844. return;
  845. }
  846. check_bind_bot(from, opcode, par);
  847. botnet_send_zapf_broad(idx, from, opcode, par);
  848. }
  849. static void bot_error(int idx, char *par)
  850. {
  851. putlog(LOG_MISC | LOG_BOTS, "*", "%s: %s", dcc[idx].nick, par);
  852. }
  853. /* nc <bot> <sock> <newnick>
  854. */
  855. static void bot_nickchange(int idx, char *par)
  856. {
  857. char *bot = NULL, *ssock = NULL, *newnick = NULL;
  858. int sock, i;
  859. bot = newsplit(&par);
  860. i = nextbot(bot);
  861. if (i != idx) {
  862. fake_alert(idx, "direction", bot, "nickchange_i");
  863. return;
  864. }
  865. ssock = newsplit(&par);
  866. sock = base64_to_int(ssock);
  867. newnick = newsplit(&par);
  868. i = partynick(bot, sock, newnick);
  869. if (i < 0) {
  870. fake_alert(idx, "sock#", ssock, "nickchange_ii");
  871. return;
  872. }
  873. chanout_but(-1, party[i].chan, "*** (%s) Nick change: %s -> %s\n",
  874. conf.bot->hub ? bot : "[botnet]", newnick, party[i].nick);
  875. botnet_send_nkch_part(idx, i, newnick);
  876. }
  877. /* join <bot> <nick> <chan> <flag><sock> <from>
  878. */
  879. static void bot_join(int idx, char *par)
  880. {
  881. char *bot = NULL, *nick = NULL, *x = NULL, *y = NULL;
  882. struct userrec *u = NULL;
  883. int i, sock, chan, i2, linking = 0;
  884. bot = newsplit(&par);
  885. if (bot[0] == '!') {
  886. linking = 1;
  887. bot++;
  888. }
  889. if (b_status(idx) & STAT_LINKING) {
  890. linking = 1;
  891. }
  892. nick = newsplit(&par);
  893. x = newsplit(&par);
  894. chan = base64_to_int(x);
  895. y = newsplit(&par);
  896. if ((chan < 0) || !y[0])
  897. return; /* Woops! pre 1.2.1's send .chat off'ers
  898. * too!! */
  899. if (!y[0]) {
  900. y[0] = '-';
  901. sock = 0;
  902. } else {
  903. sock = base64_to_int(y + 1);
  904. }
  905. i = nextbot(bot);
  906. if (i != idx) { /* Ok, garbage from a 1.0g (who uses that
  907. * now?) OR raistlin being evil :) */
  908. fake_alert(idx, "direction", bot, "join");
  909. return;
  910. }
  911. u = get_user_by_handle(userlist, nick);
  912. if (u) {
  913. simple_snprintf(TBUF, sizeof(TBUF), "@%s", bot);
  914. touch_laston(u, TBUF, now);
  915. }
  916. i = addparty(bot, nick, chan, y[0], sock, par, &i2);
  917. botnet_send_join_party(idx, linking, i2);
  918. if (i != chan) {
  919. if (i >= 0) {
  920. chanout_but(-1, i, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick, "has left the", i ? "channel" : "party line");
  921. }
  922. if (!linking)
  923. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick, "has joined the", chan ? "channel" : "party line");
  924. }
  925. }
  926. /* part <bot> <nick> <sock> [etc..]
  927. */
  928. static void bot_part(int idx, char *par)
  929. {
  930. char *bot = NULL, *nick = NULL, *etc = NULL;
  931. struct userrec *u = NULL;
  932. int sock, partyidx;
  933. int silent = 0;
  934. bot = newsplit(&par);
  935. if (bot[0] == '!') {
  936. silent = 1;
  937. bot++;
  938. }
  939. nick = newsplit(&par);
  940. etc = newsplit(&par);
  941. sock = base64_to_int(etc);
  942. u = get_user_by_handle(userlist, nick);
  943. if (u) {
  944. simple_snprintf(TBUF, sizeof(TBUF), "@%s", bot);
  945. touch_laston(u, TBUF, now);
  946. }
  947. if ((partyidx = getparty(bot, sock)) != -1) {
  948. if (!silent) {
  949. register int chan = party[partyidx].chan;
  950. if (par[0])
  951. chanout_but(-1, chan, "*** (%s) %s %s %s (%s).\n", conf.bot->hub ? bot : "[botnet]", nick,
  952. "has left the",
  953. chan ? "channel" : "party line", par);
  954. else
  955. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick,
  956. "has left the",
  957. chan ? "channel" : "party line");
  958. }
  959. botnet_send_part_party(idx, partyidx, par, silent);
  960. remparty(bot, sock);
  961. }
  962. /* check if we have a remote idx for them */
  963. int i = 0;
  964. for (i = 0; i < dcc_total; i++) {
  965. /* This will potentially close all simul-idxs with matching nick, even though they may be connected multiple times
  966. but, it won't matter as a new will just be made as needed. */
  967. if (dcc[i].type && dcc[i].simul >= 0 && !strcasecmp(dcc[i].nick, nick)) {
  968. dcc[i].simul = -1;
  969. lostdcc(i);
  970. }
  971. }
  972. }
  973. /* away <bot> <sock> <message>
  974. * null message = unaway
  975. */
  976. static void bot_away(int idx, char *par)
  977. {
  978. char *bot = NULL, *etc = NULL;
  979. int sock, partyidx, linking = 0;
  980. bot = newsplit(&par);
  981. if (bot[0] == '!') {
  982. linking = 1;
  983. bot++;
  984. }
  985. if (b_status(idx) & STAT_LINKING) {
  986. linking = 1;
  987. }
  988. etc = newsplit(&par);
  989. sock = base64_to_int(etc);
  990. if (par[0]) {
  991. partystat(bot, sock, PLSTAT_AWAY, 0);
  992. partyaway(bot, sock, par);
  993. } else {
  994. partystat(bot, sock, 0, PLSTAT_AWAY);
  995. }
  996. partyidx = getparty(bot, sock);
  997. if (!linking) {
  998. if (par[0])
  999. chanout_but(-1, party[partyidx].chan,
  1000. "*** (%s) %s %s: %s.\n", conf.bot->hub ? bot : "[botnet]",
  1001. party[partyidx].nick, "is now away", par);
  1002. else
  1003. chanout_but(-1, party[partyidx].chan,
  1004. "*** (%s) %s %s.\n", conf.bot->hub ? bot : "[botnet]",
  1005. party[partyidx].nick, "is no longer away");
  1006. }
  1007. botnet_send_away(idx, bot, sock, par, linking);
  1008. }
  1009. /* (a courtesy info to help during connect bursts)
  1010. * idle <bot> <sock> <#secs> [away msg]
  1011. */
  1012. static void bot_idle(int idx, char *par)
  1013. {
  1014. char *bot = NULL, *work = NULL;
  1015. int sock, idle;
  1016. bot = newsplit(&par);
  1017. work = newsplit(&par);
  1018. sock = base64_to_int(work);
  1019. work = newsplit(&par);
  1020. idle = base64_to_int(work);
  1021. partysetidle(bot, sock, idle);
  1022. if (par[0]) {
  1023. partystat(bot, sock, PLSTAT_AWAY, 0);
  1024. partyaway(bot, sock, par);
  1025. }
  1026. botnet_send_idle(idx, bot, sock, idle, par);
  1027. }
  1028. void bot_share(int idx, char *par)
  1029. {
  1030. sharein(idx, par);
  1031. }
  1032. void bot_shareupdate(int idx, char *par)
  1033. {
  1034. updatein(idx, par);
  1035. }
  1036. /* v <frombot> <tobot> <idx:nick>
  1037. */
  1038. static void bot_versions(int sock, char *par)
  1039. {
  1040. char *frombot = newsplit(&par), *tobot = NULL, *from = NULL;
  1041. if (nextbot(frombot) != sock)
  1042. fake_alert(sock, "direction", frombot, "versions");
  1043. else if (strcasecmp(tobot = newsplit(&par), conf.bot->nick)) {
  1044. if ((sock = nextbot(tobot)) >= 0)
  1045. dprintf(sock, "v %s %s %s\n", frombot, tobot, par);
  1046. } else {
  1047. from = newsplit(&par);
  1048. botnet_send_priv(sock, conf.bot->nick, from, frombot, "Modules loaded:\n");
  1049. /* wtf?
  1050. for (me = module_list; me; me = me->next)
  1051. botnet_send_priv(sock, conf.bot->nick, from, frombot, " Module: %s\n", me->name);
  1052. */
  1053. botnet_send_priv(sock, conf.bot->nick, from, frombot, "End of module list.\n");
  1054. }
  1055. }
  1056. /* BOT COMMANDS
  1057. *
  1058. * function call should be:
  1059. * int bot_whatever(idx,"parameters");
  1060. *
  1061. * SORT these, dcc_bot uses a shortcut which requires them sorted
  1062. *
  1063. * yup, those are tokens there to allow a more efficient botnet as
  1064. * time goes on (death to slowly upgrading llama's)
  1065. */
  1066. botcmd_t C_bot[] =
  1067. {
  1068. {"a", bot_actchan, 0},
  1069. {"aw", bot_away, 0},
  1070. {"bye", bot_bye, 0},
  1071. {"c", bot_chan2, 0},
  1072. {"cp", bot_cmdpass, 0},
  1073. {"ct", bot_chat, 0},
  1074. {"e", bot_error, 0},
  1075. {"el", bot_endlink, 0},
  1076. {"i", bot_idle, 0},
  1077. {"j", bot_join, 0},
  1078. {"l", bot_link, 0},
  1079. {"lo", bot_log, 0},
  1080. {"n", bot_nlinked, 0},
  1081. {"nc", bot_nickchange, 0},
  1082. {"p", bot_priv, 0},
  1083. {"pi", bot_ping, 0},
  1084. {"po", bot_pong, 0},
  1085. {"pt", bot_part, 0},
  1086. {"r", bot_reject, 0},
  1087. {"rc", bot_remotecmd, 0},
  1088. {"rr", bot_remotereply, 0},
  1089. {"s", bot_share, 0},
  1090. {"sb", bot_shareupdate, 0},
  1091. {"si", bot_shellinfo, 0},
  1092. {"t", bot_trace, 0},
  1093. {"tb", bot_thisbot, 0},
  1094. {"td", bot_traced, 0},
  1095. {"ts", bot_timesync, 0},
  1096. {"u", bot_update, 0},
  1097. {"ul", bot_unlink, 0},
  1098. {"un", bot_unlinked, 0},
  1099. {"v", bot_versions, 0},
  1100. {"va", bot_set, 0},
  1101. {"vab", bot_setbroad, 0},
  1102. {"w", bot_who, 0},
  1103. {"z", bot_zapf, 0},
  1104. {"zb", bot_zapfbroad, 0},
  1105. {NULL, NULL, 0}
  1106. };
  1107. static int comp_botcmd_t(const void *m1, const void *m2) {
  1108. const botcmd_t *mi1 = (const botcmd_t *) m1;
  1109. const botcmd_t *mi2 = (const botcmd_t *) m2;
  1110. return strcasecmp(mi1->name, mi2->name);
  1111. }
  1112. const botcmd_t *search_botcmd_t(const botcmd_t *table, const char* keyString, size_t elements) {
  1113. botcmd_t key;
  1114. key.name = keyString;
  1115. return (const botcmd_t*) bsearch(&key, table, elements, sizeof(botcmd_t), comp_botcmd_t);
  1116. }
  1117. void parse_botcmd(int idx, const char* code, const char* msg) {
  1118. const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_bot, code, (sizeof(C_bot)/sizeof(botcmd_t)) - 1);
  1119. if (cmd) {
  1120. /* Found a match */
  1121. if (have_cmd(NULL, cmd->type))
  1122. (cmd->func) (idx, (char*)msg);
  1123. }
  1124. }
  1125. void send_remote_simul(int idx, char *bot, char *cmd, char *par)
  1126. {
  1127. char msg[SGRAB - 110] = "";
  1128. simple_snprintf(msg, sizeof msg, "r-s %d %s %d %s %lu %s %s", idx, dcc[idx].nick, dcc[idx].u.chat->con_flags,
  1129. dcc[idx].u.chat->con_chan, dcc[idx].status, cmd, par);
  1130. putbot(bot, msg);
  1131. }
  1132. /* idx nick conmask cmd par */
  1133. static void bot_rsim(char *botnick, char *code, char *msg)
  1134. {
  1135. int ridx = -1, idx = -1, i = 0, rconmask;
  1136. unsigned long status = 0;
  1137. char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX] = "", *par = NULL, *parp = NULL;
  1138. struct userrec *u = get_user_by_handle(userlist, botnick);
  1139. if (bot_hublevel(u) == 999) {
  1140. putlog(LOG_WARN, "*", "BOTCMD received from a leaf. HIJACK.");
  1141. return;
  1142. }
  1143. par = parp = strdup(msg);
  1144. ridx = atoi(newsplit(&par));
  1145. nick = newsplit(&par);
  1146. rconmask = atoi(newsplit(&par));
  1147. rconchan = newsplit(&par);
  1148. if (egg_isdigit(par[0]))
  1149. status = (unsigned long) atoi(newsplit(&par));
  1150. cmd = newsplit(&par);
  1151. if (ridx < 0 || !nick || !cmd) {
  1152. free(parp);
  1153. return;
  1154. }
  1155. for (i = 0; i < dcc_total; i++) {
  1156. /* See if we can find a simul-idx for the same ridx/nick */
  1157. if (dcc[i].type && dcc[i].simul == ridx && !strcasecmp(dcc[i].nick, nick)) {
  1158. putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
  1159. dcc[i].simultime = now;
  1160. idx = i;
  1161. break;
  1162. }
  1163. }
  1164. if (idx < 0) {
  1165. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1166. putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
  1167. dcc[idx].sock = -1;
  1168. dcc[idx].timeval = now;
  1169. dcc[idx].simultime = now;
  1170. dcc[idx].simul = ridx;
  1171. dcc[idx].status = status;
  1172. strlcpy(dcc[idx].simulbot, botnick, NICKLEN);
  1173. dcc[idx].u.chat->con_flags = rconmask;
  1174. struct chat_info dummy;
  1175. strlcpy(dcc[idx].u.chat->con_chan, rconchan, sizeof(dummy.con_chan));
  1176. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  1177. strlcpy(dcc[idx].nick, nick, NICKLEN);
  1178. simple_snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
  1179. strlcpy(dcc[idx].host, buf, UHOSTLEN);
  1180. dcc[idx].addr = 0L;
  1181. dcc[idx].user = get_user_by_handle(userlist, nick);
  1182. }
  1183. rmspace(par);
  1184. check_bind_dcc(cmd, idx, par);
  1185. free(parp);
  1186. }
  1187. void bounce_simul(int idx, char *buf)
  1188. {
  1189. char rmsg[SGRAB - 110] = "";
  1190. if (!buf || !buf[0] || !dcc[idx].simulbot || !dcc[idx].simulbot[0] || idx < 0)
  1191. return;
  1192. /* Truncate out the newline that was put in from the dprintf() */
  1193. char *p = strchr(buf, '\n');
  1194. if (p)
  1195. *p = 0;
  1196. simple_snprintf(rmsg, sizeof rmsg, "r-sr %d %s %s", dcc[idx].simul, dcc[idx].user->handle, buf); /* remote-simul[r]eturn idx buf */
  1197. putbot(dcc[idx].simulbot, rmsg);
  1198. }
  1199. static void bot_rsimr(char *botnick, char *code, char *msg)
  1200. {
  1201. if (msg[0]) {
  1202. char * par = strdup(msg), *parp = par, *prefix = NULL;
  1203. int idx = atoi(newsplit(&par));
  1204. char *nick = newsplit(&par);
  1205. size_t size = strlen(botnick) + 4;
  1206. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT) && dcc[idx].user && !strcmp(dcc[idx].user->handle, nick)) {
  1207. prefix = (char *) my_calloc(1, size);
  1208. simple_snprintf(prefix, size, "[%s] ", botnick);
  1209. dumplots(idx, prefix, par);
  1210. free(prefix);
  1211. }
  1212. free(parp);
  1213. }
  1214. }
  1215. static cmd_t my_bot[] =
  1216. {
  1217. {"r-sr", "", (Function) bot_rsimr, NULL, HUB},
  1218. {"r-s", "", (Function) bot_rsim, NULL, 0},
  1219. {NULL, NULL, NULL, NULL, 0}
  1220. };
  1221. void init_botcmd()
  1222. {
  1223. add_builtins("bot", my_bot);
  1224. }