1
0

botcmd.cc 37 KB

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