botcmd.cc 37 KB

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