botcmd.cc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  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, fflags = -1;
  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 (par[0])
  576. fflags = atoi(newsplit(&par));
  577. if (in_chain(bot))
  578. updatebot(idx, bot, x, vlocalhub, vbuildts, vcommit, vversion, fflags);
  579. }
  580. /* Newbot next share?
  581. */
  582. static void bot_nlinked(int idx, char *par)
  583. {
  584. char *newbot = NULL, *next = NULL, *p = NULL, s[1024] = "", x = 0, *vversion = NULL, *vcommit = NULL;
  585. int i, vlocalhub = 0, fflags = -1;
  586. time_t vbuildts = 0L;
  587. bool bogus = 0;
  588. newbot = newsplit(&par);
  589. next = newsplit(&par);
  590. s[0] = 0;
  591. if (!next[0]) {
  592. putlog(LOG_BOTS, "*", "Invalid eggnet protocol from %s (zapfing)", dcc[idx].nick);
  593. simple_snprintf(s, sizeof(s), "Disconnected %s (invalid bot)", dcc[idx].nick);
  594. dprintf(idx, "error invalid eggnet protocol for 'nlinked'\n");
  595. } else if ((in_chain(newbot)) || (!strcasecmp(newbot, conf.bot->nick))) {
  596. /* Loop! */
  597. putlog(LOG_BOTS, "*", "Loop detected %s (mutual: %s)", dcc[idx].nick, newbot);
  598. simple_snprintf(s, sizeof(s), "Detected loop: two bots exist named %s: disconnecting %s", newbot, dcc[idx].nick);
  599. dprintf(idx, "error Loop (%s)\n", newbot);
  600. }
  601. if (!s[0]) {
  602. for (p = newbot; *p; p++)
  603. if ((*p < 32) || (*p == 127))
  604. bogus = 1;
  605. i = nextbot(next);
  606. if (i != idx)
  607. bogus = 1;
  608. if (bogus) {
  609. putlog(LOG_BOTS, "*", "Bogus link notice from %s! (%s -> %s)", dcc[idx].nick, next, newbot);
  610. simple_snprintf(s, sizeof(s), "Bogus link notice from: %s Disconnected", dcc[idx].nick);
  611. dprintf(idx, "error Bogus link notice from (%s -> %s)\n", next, newbot);
  612. }
  613. }
  614. if (s[0]) {
  615. chatout("*** %s\n", s);
  616. botnet_send_unlinked(idx, dcc[idx].nick, s);
  617. dprintf(idx, "bye %s\n", s);
  618. killsock(dcc[idx].sock);
  619. lostdcc(idx);
  620. return;
  621. }
  622. x = par[0];
  623. if (x)
  624. par++;
  625. else
  626. x = '-';
  627. if (par[0])
  628. newsplit(&par); /* vnum */
  629. if (par[0])
  630. vlocalhub = atoi(newsplit(&par));
  631. if (par[0])
  632. vbuildts = atol(newsplit(&par));
  633. if (par[0])
  634. vcommit = newsplit(&par);
  635. if (par[0])
  636. vversion = newsplit(&par);
  637. if (par[0])
  638. fflags = atoi(newsplit(&par));
  639. botnet_send_nlinked(idx, newbot, next, x, vlocalhub, vbuildts, vcommit, vversion, fflags);
  640. if (x == '!') {
  641. if (conf.bot->hub)
  642. chatout("*** (%s) %s %s.\n", next, "Linked to", newbot);
  643. else
  644. chatout("*** %s linked to botnet.\n", newbot);
  645. x = '-';
  646. }
  647. addbot(newbot, dcc[idx].nick, next, x, vlocalhub, vbuildts, vcommit, vversion ? vversion : (char *) "", fflags);
  648. }
  649. static void bot_unlinked(int idx, char *par)
  650. {
  651. int i;
  652. char *bot = NULL;
  653. bot = newsplit(&par);
  654. i = nextbot(bot);
  655. if ((i >= 0) && (i != idx)) /* Bot is NOT downstream along idx, so
  656. * BOGUS! */
  657. fake_alert(idx, "direction", bot, "unlinked");
  658. else if (i >= 0) { /* Valid bot downstream of idx */
  659. if (par[0])
  660. /* #ifdef HUB */
  661. chatout("*** (%s) %s\n", lastbot(bot), par);
  662. /*
  663. #else
  664. chatout("*** %s unlinked from botnet.\n", par);
  665. #endif
  666. */
  667. botnet_send_unlinked(idx, bot, par);
  668. unvia(idx, findbot(bot));
  669. rembot(bot);
  670. }
  671. /* Otherwise it's not even a valid bot, so just ignore! */
  672. }
  673. /* trace <from@bot> <dest> <chain:chain..>
  674. */
  675. static void bot_trace(int idx, char *par)
  676. {
  677. char *from = NULL, *dest = NULL;
  678. int i;
  679. from = newsplit(&par);
  680. dest = newsplit(&par);
  681. simple_snprintf(TBUF, sizeof(TBUF), "%s:%s", par, conf.bot->nick);
  682. botnet_send_traced(idx, from, TBUF);
  683. if (strcasecmp(dest, conf.bot->nick) && ((i = nextbot(dest)) > 0))
  684. botnet_send_trace(i, from, dest, par);
  685. }
  686. /* traced <to@bot> <chain:chain..>
  687. */
  688. static void bot_traced(int idx, char *par)
  689. {
  690. char *to = NULL, *p = NULL;
  691. int i, sock;
  692. to = newsplit(&par);
  693. p = strchr(to, '@');
  694. if (p == NULL)
  695. p = to;
  696. else {
  697. *p = 0;
  698. p++;
  699. }
  700. if (!strcasecmp(p, conf.bot->nick)) {
  701. time_t t = 0;
  702. char *p2 = par, *ss = TBUF;
  703. splitc(ss, to, ':');
  704. if (ss[0])
  705. sock = atoi(ss);
  706. else
  707. sock = (-1);
  708. if (par[0] == ':') {
  709. t = atoi(par + 1);
  710. p2 = strchr(par + 1, ':');
  711. if (p2)
  712. p2++;
  713. else
  714. p2 = par + 1;
  715. }
  716. for (i = 0; i < dcc_total; i++) {
  717. if (dcc[i].type && (dcc[i].type->flags & DCT_CHAT) && (!strcasecmp(dcc[i].nick, to)) &&
  718. ((sock == (-1)) || (sock == dcc[i].sock))) {
  719. if (t) {
  720. int j=0;
  721. char *c = p2;
  722. for (; *c != '\0'; c++) if (*c == ':') j++;
  723. time_t tm;
  724. egg_timeval_t tv;
  725. timer_get_now(&tv);
  726. tm = ((tv.sec % 10000) * 100 + (tv.usec * 100) / (1000000)) - t;
  727. dprintf(i, "%s -> %s (%d.%d secs, %d hop%s)\n", "Trace result", p2,
  728. (int)(tm / 100), (int)(tm % 100), j, (j != 1) ? "s" : "");
  729. } else
  730. dprintf(i, "%s -> %s\n", "Trace result", p);
  731. }
  732. }
  733. } else {
  734. i = nextbot(p);
  735. if (p != to)
  736. *--p = '@';
  737. if (i >= 0)
  738. botnet_send_traced(i, to, par);
  739. }
  740. }
  741. static void bot_timesync(int idx, char *par)
  742. {
  743. // putlog(LOG_DEBUG, "@", "Got timesync from %s: %s (%li - %li)", dcc[idx].nick, par, atol(par), now);
  744. timesync = atol(par) - now;
  745. if (conf.bot->hub || conf.bot->localhub)
  746. send_timesync(-1);
  747. }
  748. /* reject <from> <bot>
  749. */
  750. static void bot_reject(int idx, char *par)
  751. {
  752. char *from = NULL, *who = NULL, *destbot = NULL, *frombot = NULL;
  753. struct userrec *u = NULL;
  754. int i;
  755. from = newsplit(&par);
  756. frombot = strchr(from, '@');
  757. if (frombot)
  758. frombot++;
  759. else
  760. frombot = from;
  761. i = nextbot(frombot);
  762. if (i != idx) {
  763. fake_alert(idx, "direction", frombot, "reject");
  764. return;
  765. }
  766. who = newsplit(&par);
  767. if (!(destbot = strchr(who, '@'))) {
  768. /* Rejecting a bot */
  769. i = nextbot(who);
  770. if (i < 0) {
  771. botnet_send_priv(idx, conf.bot->nick, from, NULL, "Can't unlink %s (doesn't exist)", who);
  772. } else if (!strcasecmp(dcc[i].nick, who)) {
  773. char s[1024];
  774. /* I'm the connection to the rejected bot */
  775. putlog(LOG_BOTS, "*", "%s rejected %s", from, dcc[i].nick);
  776. dprintf(i, "bye %s\n", par[0] ? par : "rejected");
  777. simple_sprintf(s, "Disconnected %s (%s: %s)", dcc[i].nick, from, par[0] ? par : "rejected");
  778. chatout("*** %s\n", s);
  779. botnet_send_unlinked(i, dcc[i].nick, s);
  780. killsock(dcc[i].sock);
  781. lostdcc(i);
  782. } else {
  783. if (i >= 0)
  784. botnet_send_reject(i, from, NULL, who, NULL, par);
  785. }
  786. } else { /* Rejecting user */
  787. *destbot++ = 0;
  788. if (!strcasecmp(destbot, conf.bot->nick)) {
  789. /* Kick someone here! */
  790. for (i = 0; i < dcc_total; i++) {
  791. if (dcc[i].type && dcc[i].simul == -1 && (dcc[i].type->flags & DCT_CHAT) && !strcasecmp(who, dcc[i].nick)) {
  792. u = get_user_by_handle(userlist, from);
  793. if (u) {
  794. if (!whois_access(u, dcc[idx].user)) {
  795. add_note(from, conf.bot->nick, "Sorry, you cannot boot them.", -1, 0);
  796. return;
  797. }
  798. do_boot(i, from, par);
  799. putlog(LOG_CMDS, "*", "#%s# boot %s (%s)", from, who, par[0] ? par : "No reason");
  800. }
  801. }
  802. }
  803. } else {
  804. i = nextbot(destbot);
  805. *--destbot = '@';
  806. if (i >= 0)
  807. botnet_send_reject(i, from, NULL, who, NULL, par);
  808. }
  809. }
  810. }
  811. static void bot_thisbot(int idx, char *par)
  812. {
  813. if (strcasecmp(par, dcc[idx].nick)) {
  814. char s[1024] = "";
  815. putlog(LOG_BOTS, "*", "Wrong bot--wanted %s, got %s", dcc[idx].nick, par);
  816. dprintf(idx, "bye imposter\n");
  817. simple_snprintf(s, sizeof(s), "Disconnected %s (imposter)", dcc[idx].nick);
  818. chatout("*** %s\n", s);
  819. botnet_send_unlinked(idx, dcc[idx].nick, s);
  820. unvia(idx, findbot(dcc[idx].nick));
  821. killsock(dcc[idx].sock);
  822. lostdcc(idx);
  823. return;
  824. }
  825. /* Set capitalization the way they want it */
  826. noshare = 1;
  827. change_handle(dcc[idx].user, par);
  828. noshare = 0;
  829. strlcpy(dcc[idx].nick, par, sizeof(dcc[idx].nick));
  830. }
  831. /* Used to send a direct msg from Tcl on one bot to Tcl on another
  832. * zapf <frombot> <tobot> <code [param]>
  833. */
  834. static void bot_zapf(int idx, char *par)
  835. {
  836. char *from = NULL, *to = NULL;
  837. int i;
  838. from = newsplit(&par);
  839. to = newsplit(&par);
  840. i = nextbot(from);
  841. if (i != idx) {
  842. fake_alert(idx, "direction", from, "zapf");
  843. return;
  844. }
  845. if (!strcasecmp(to, conf.bot->nick)) {
  846. /* For me! */
  847. char *opcode;
  848. opcode = newsplit(&par);
  849. check_bind_bot(from, opcode, par);
  850. return;
  851. } else {
  852. i = nextbot(to);
  853. if (i >= 0)
  854. botnet_send_zapf(i, from, to, par);
  855. }
  856. }
  857. /* Used to send a global msg from Tcl on one bot to every other bot
  858. * zapf-broad <frombot> <code [param]>
  859. */
  860. static void bot_zapfbroad(int idx, char *par)
  861. {
  862. char *from = NULL, *opcode = NULL;
  863. int i;
  864. from = newsplit(&par);
  865. opcode = newsplit(&par);
  866. i = nextbot(from);
  867. if (i != idx) {
  868. fake_alert(idx, "direction", from, "zapfb");
  869. return;
  870. }
  871. check_bind_bot(from, opcode, par);
  872. botnet_send_zapf_broad(idx, from, opcode, par);
  873. }
  874. static void bot_error(int idx, char *par)
  875. {
  876. putlog(LOG_MISC | LOG_BOTS, "*", "%s: %s", dcc[idx].nick, par);
  877. }
  878. /* nc <bot> <sock> <newnick>
  879. */
  880. static void bot_nickchange(int idx, char *par)
  881. {
  882. char *bot = NULL, *ssock = NULL, *newnick = NULL;
  883. int sock, i;
  884. bot = newsplit(&par);
  885. i = nextbot(bot);
  886. if (i != idx) {
  887. fake_alert(idx, "direction", bot, "nickchange_i");
  888. return;
  889. }
  890. ssock = newsplit(&par);
  891. sock = base64_to_int(ssock);
  892. newnick = newsplit(&par);
  893. i = partynick(bot, sock, newnick);
  894. if (i < 0) {
  895. fake_alert(idx, "sock#", ssock, "nickchange_ii");
  896. return;
  897. }
  898. chanout_but(-1, party[i].chan, "*** (%s) Nick change: %s -> %s\n",
  899. conf.bot->hub ? bot : "[botnet]", newnick, party[i].nick);
  900. botnet_send_nkch_part(idx, i, newnick);
  901. }
  902. /* join <bot> <nick> <chan> <flag><sock> <from>
  903. */
  904. static void bot_join(int idx, char *par)
  905. {
  906. char *bot = NULL, *nick = NULL, *x = NULL, *y = NULL;
  907. struct userrec *u = NULL;
  908. int i, sock, chan, i2, linking = 0;
  909. bot = newsplit(&par);
  910. if (bot[0] == '!') {
  911. linking = 1;
  912. bot++;
  913. }
  914. if (b_status(idx) & STAT_LINKING) {
  915. linking = 1;
  916. }
  917. nick = newsplit(&par);
  918. x = newsplit(&par);
  919. chan = base64_to_int(x);
  920. y = newsplit(&par);
  921. if ((chan < 0) || !y[0])
  922. return; /* Woops! pre 1.2.1's send .chat off'ers
  923. * too!! */
  924. if (!y[0]) {
  925. y[0] = '-';
  926. sock = 0;
  927. } else {
  928. sock = base64_to_int(y + 1);
  929. }
  930. i = nextbot(bot);
  931. if (i != idx) { /* Ok, garbage from a 1.0g (who uses that
  932. * now?) OR raistlin being evil :) */
  933. fake_alert(idx, "direction", bot, "join");
  934. return;
  935. }
  936. u = get_user_by_handle(userlist, nick);
  937. if (u) {
  938. simple_snprintf(TBUF, sizeof(TBUF), "@%s", bot);
  939. touch_laston(u, TBUF, now);
  940. }
  941. i = addparty(bot, nick, chan, y[0], sock, par, &i2);
  942. botnet_send_join_party(idx, linking, i2);
  943. if (i != chan) {
  944. if (i >= 0) {
  945. chanout_but(-1, i, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick, "has left the", i ? "channel" : "party line");
  946. }
  947. if (!linking)
  948. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick, "has joined the", chan ? "channel" : "party line");
  949. }
  950. }
  951. /* part <bot> <nick> <sock> [etc..]
  952. */
  953. static void bot_part(int idx, char *par)
  954. {
  955. char *bot = NULL, *nick = NULL, *etc = NULL;
  956. struct userrec *u = NULL;
  957. int sock, partyidx;
  958. int silent = 0;
  959. bot = newsplit(&par);
  960. if (bot[0] == '!') {
  961. silent = 1;
  962. bot++;
  963. }
  964. nick = newsplit(&par);
  965. etc = newsplit(&par);
  966. sock = base64_to_int(etc);
  967. u = get_user_by_handle(userlist, nick);
  968. if (u) {
  969. simple_snprintf(TBUF, sizeof(TBUF), "@%s", bot);
  970. touch_laston(u, TBUF, now);
  971. }
  972. if ((partyidx = getparty(bot, sock)) != -1) {
  973. if (!silent) {
  974. int chan = party[partyidx].chan;
  975. if (par[0])
  976. chanout_but(-1, chan, "*** (%s) %s %s %s (%s).\n", conf.bot->hub ? bot : "[botnet]", nick,
  977. "has left the",
  978. chan ? "channel" : "party line", par);
  979. else
  980. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", conf.bot->hub ? bot : "[botnet]", nick,
  981. "has left the",
  982. chan ? "channel" : "party line");
  983. }
  984. botnet_send_part_party(idx, partyidx, par, silent);
  985. remparty(bot, sock);
  986. }
  987. /* check if we have a remote idx for them */
  988. int i = 0;
  989. for (i = 0; i < dcc_total; i++) {
  990. /* This will potentially close all simul-idxs with matching nick, even though they may be connected multiple times
  991. but, it won't matter as a new will just be made as needed. */
  992. if (dcc[i].type && dcc[i].simul >= 0 && !strcasecmp(dcc[i].nick, nick)) {
  993. dcc[i].simul = -1;
  994. lostdcc(i);
  995. }
  996. }
  997. }
  998. /* away <bot> <sock> <message>
  999. * null message = unaway
  1000. */
  1001. static void bot_away(int idx, char *par)
  1002. {
  1003. char *bot = NULL, *etc = NULL;
  1004. int sock, partyidx, linking = 0;
  1005. bot = newsplit(&par);
  1006. if (bot[0] == '!') {
  1007. linking = 1;
  1008. bot++;
  1009. }
  1010. if (b_status(idx) & STAT_LINKING) {
  1011. linking = 1;
  1012. }
  1013. etc = newsplit(&par);
  1014. sock = base64_to_int(etc);
  1015. if (par[0]) {
  1016. partystat(bot, sock, PLSTAT_AWAY, 0);
  1017. partyaway(bot, sock, par);
  1018. } else {
  1019. partystat(bot, sock, 0, PLSTAT_AWAY);
  1020. }
  1021. partyidx = getparty(bot, sock);
  1022. if (!linking) {
  1023. if (par[0])
  1024. chanout_but(-1, party[partyidx].chan,
  1025. "*** (%s) %s %s: %s.\n", conf.bot->hub ? bot : "[botnet]",
  1026. party[partyidx].nick, "is now away", par);
  1027. else
  1028. chanout_but(-1, party[partyidx].chan,
  1029. "*** (%s) %s %s.\n", conf.bot->hub ? bot : "[botnet]",
  1030. party[partyidx].nick, "is no longer away");
  1031. }
  1032. botnet_send_away(idx, bot, sock, par, linking);
  1033. }
  1034. /* (a courtesy info to help during connect bursts)
  1035. * idle <bot> <sock> <#secs> [away msg]
  1036. */
  1037. static void bot_idle(int idx, char *par)
  1038. {
  1039. char *bot = NULL, *work = NULL;
  1040. int sock, idle;
  1041. bot = newsplit(&par);
  1042. work = newsplit(&par);
  1043. sock = base64_to_int(work);
  1044. work = newsplit(&par);
  1045. idle = base64_to_int(work);
  1046. partysetidle(bot, sock, idle);
  1047. if (par[0]) {
  1048. partystat(bot, sock, PLSTAT_AWAY, 0);
  1049. partyaway(bot, sock, par);
  1050. }
  1051. botnet_send_idle(idx, bot, sock, idle, par);
  1052. }
  1053. void bot_share(int idx, char *par)
  1054. {
  1055. sharein(idx, par);
  1056. }
  1057. static void bot_shareupdate(int idx, char *par)
  1058. {
  1059. updatein(idx, par);
  1060. }
  1061. /* v <frombot> <tobot> <idx:nick>
  1062. */
  1063. static void bot_versions(int sock, char *par)
  1064. {
  1065. char *frombot = newsplit(&par), *tobot = NULL, *from = NULL;
  1066. if (nextbot(frombot) != sock)
  1067. fake_alert(sock, "direction", frombot, "versions");
  1068. else if (strcasecmp(tobot = newsplit(&par), conf.bot->nick)) {
  1069. if ((sock = nextbot(tobot)) >= 0)
  1070. dprintf(sock, "v %s %s %s\n", frombot, tobot, par);
  1071. } else {
  1072. from = newsplit(&par);
  1073. botnet_send_priv(sock, conf.bot->nick, from, frombot, "Modules loaded:\n");
  1074. /* wtf?
  1075. for (me = module_list; me; me = me->next)
  1076. botnet_send_priv(sock, conf.bot->nick, from, frombot, " Module: %s\n", me->name);
  1077. */
  1078. botnet_send_priv(sock, conf.bot->nick, from, frombot, "End of module list.\n");
  1079. }
  1080. }
  1081. /* BOT COMMANDS
  1082. *
  1083. * function call should be:
  1084. * int bot_whatever(idx,"parameters");
  1085. *
  1086. * SORT these, dcc_bot uses a shortcut which requires them sorted
  1087. *
  1088. * yup, those are tokens there to allow a more efficient botnet as
  1089. * time goes on (death to slowly upgrading llama's)
  1090. */
  1091. botcmd_t C_bot[] =
  1092. {
  1093. {"a", bot_actchan, 0},
  1094. {"aw", bot_away, 0},
  1095. {"bye", bot_bye, 0},
  1096. {"c", bot_chan2, 0},
  1097. {"cp", bot_cmdpass, 0},
  1098. {"ct", bot_chat, 0},
  1099. {"e", bot_error, 0},
  1100. {"el", bot_endlink, 0},
  1101. {"i", bot_idle, 0},
  1102. {"j", bot_join, 0},
  1103. {"l", bot_link, 0},
  1104. {"lo", bot_log, 0},
  1105. {"n", bot_nlinked, 0},
  1106. {"nc", bot_nickchange, 0},
  1107. {"p", bot_priv, 0},
  1108. {"pi", bot_ping, 0},
  1109. {"po", bot_pong, 0},
  1110. {"pt", bot_part, 0},
  1111. {"r", bot_reject, 0},
  1112. {"rc", bot_remotecmd, 0},
  1113. {"rr", bot_remotereply, 0},
  1114. {"s", bot_share, 0},
  1115. {"sb", bot_shareupdate, 0},
  1116. {"si", bot_shellinfo, 0},
  1117. {"t", bot_trace, 0},
  1118. {"tb", bot_thisbot, 0},
  1119. {"td", bot_traced, 0},
  1120. {"ts", bot_timesync, 0},
  1121. {"u", bot_update, 0},
  1122. {"ul", bot_unlink, 0},
  1123. {"un", bot_unlinked, 0},
  1124. {"v", bot_versions, 0},
  1125. {"va", bot_set, 0},
  1126. {"vab", bot_setbroad, 0},
  1127. {"w", bot_who, 0},
  1128. {"z", bot_zapf, 0},
  1129. {"zb", bot_zapfbroad, 0},
  1130. {NULL, NULL, 0}
  1131. };
  1132. static int comp_botcmd_t(const void *m1, const void *m2) {
  1133. const botcmd_t *mi1 = (const botcmd_t *) m1;
  1134. const botcmd_t *mi2 = (const botcmd_t *) m2;
  1135. return strcasecmp(mi1->name, mi2->name);
  1136. }
  1137. const botcmd_t *search_botcmd_t(const botcmd_t *table, const char* keyString, size_t elements) {
  1138. botcmd_t key;
  1139. key.name = keyString;
  1140. return (const botcmd_t*) bsearch(&key, table, elements, sizeof(botcmd_t), comp_botcmd_t);
  1141. }
  1142. void parse_botcmd(int idx, const char* code, const char* msg) {
  1143. const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_bot, code, lengthof(C_bot) - 1);
  1144. if (cmd) {
  1145. /* Found a match */
  1146. if (have_cmd(NULL, cmd->type))
  1147. (cmd->func) (idx, (char*)msg);
  1148. }
  1149. }
  1150. void send_remote_simul(int idx, char *bot, char *cmd, char *par)
  1151. {
  1152. char msg[SGRAB - 110] = "";
  1153. simple_snprintf(msg, sizeof msg, "r-s %d %s %d %s %lu %s %s", idx, dcc[idx].nick, dcc[idx].u.chat->con_flags,
  1154. dcc[idx].u.chat->con_chan, dcc[idx].status, cmd, par);
  1155. putbot(bot, msg);
  1156. }
  1157. /* idx nick conmask cmd par */
  1158. static void bot_rsim(char *botnick, char *code, char *msg)
  1159. {
  1160. int ridx = -1, idx = -1, i = 0, rconmask;
  1161. unsigned long status = 0;
  1162. char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX] = "", *par = NULL, *parp = NULL;
  1163. struct userrec *u = get_user_by_handle(userlist, botnick);
  1164. if (bot_hublevel(u) == 999) {
  1165. putlog(LOG_WARN, "*", "BOTCMD received from a leaf. HIJACK.");
  1166. return;
  1167. }
  1168. par = parp = strdup(msg);
  1169. ridx = atoi(newsplit(&par));
  1170. nick = newsplit(&par);
  1171. rconmask = atoi(newsplit(&par));
  1172. rconchan = newsplit(&par);
  1173. if (egg_isdigit(par[0]))
  1174. status = (unsigned long) atoi(newsplit(&par));
  1175. cmd = newsplit(&par);
  1176. if (ridx < 0 || !nick || !cmd) {
  1177. free(parp);
  1178. return;
  1179. }
  1180. for (i = 0; i < dcc_total; i++) {
  1181. /* See if we can find a simul-idx for the same ridx/nick */
  1182. if (dcc[i].type && dcc[i].simul == ridx && !strcasecmp(dcc[i].nick, nick)) {
  1183. putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
  1184. dcc[i].simultime = now;
  1185. idx = i;
  1186. break;
  1187. }
  1188. }
  1189. if (idx < 0) {
  1190. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1191. putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
  1192. dcc[idx].sock = -1;
  1193. dcc[idx].timeval = now;
  1194. dcc[idx].simultime = now;
  1195. dcc[idx].simul = ridx;
  1196. dcc[idx].status = status;
  1197. strlcpy(dcc[idx].simulbot, botnick, sizeof(dcc[idx].simulbot));
  1198. dcc[idx].u.chat->con_flags = rconmask;
  1199. struct chat_info dummy;
  1200. strlcpy(dcc[idx].u.chat->con_chan, rconchan, sizeof(dummy.con_chan));
  1201. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  1202. strlcpy(dcc[idx].nick, nick, sizeof(dcc[idx].nick));
  1203. simple_snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
  1204. strlcpy(dcc[idx].host, buf, sizeof(dcc[idx].host));
  1205. dcc[idx].addr = 0L;
  1206. dcc[idx].user = get_user_by_handle(userlist, nick);
  1207. }
  1208. rmspace(par);
  1209. check_bind_dcc(cmd, idx, par);
  1210. free(parp);
  1211. }
  1212. void bounce_simul(int idx, char *buf)
  1213. {
  1214. char rmsg[SGRAB - 110] = "";
  1215. if (!buf || !buf[0] || !dcc[idx].simulbot[0] || idx < 0)
  1216. return;
  1217. /* Truncate out the newline that was put in from the dprintf() */
  1218. char *p = strchr(buf, '\n');
  1219. if (p)
  1220. *p = 0;
  1221. simple_snprintf(rmsg, sizeof rmsg, "r-sr %d %s %s", dcc[idx].simul, dcc[idx].user->handle, buf); /* remote-simul[r]eturn idx buf */
  1222. putbot(dcc[idx].simulbot, rmsg);
  1223. }
  1224. static void bot_rsimr(char *botnick, char *code, char *msg)
  1225. {
  1226. if (msg[0]) {
  1227. char * par = strdup(msg), *parp = par;
  1228. int idx = atoi(newsplit(&par, ' ', 0));
  1229. char *nick = newsplit(&par, ' ', 0);
  1230. if (dcc[idx].type && (dcc[idx].type == &DCC_CHAT) && dcc[idx].user && !strcmp(dcc[idx].user->handle, nick)) {
  1231. dprintf(idx, "[%s] %s\n", botnick, par);
  1232. }
  1233. free(parp);
  1234. }
  1235. }
  1236. static void bot_rd(char* botnick, char* code, char* msg)
  1237. {
  1238. size_t len = atoi(newsplit(&msg));
  1239. if (msg[0]) {
  1240. int idx = atoi(newsplit(&msg));
  1241. if (msg[0])
  1242. dprintf_real(idx, msg, len, len);
  1243. }
  1244. }
  1245. static void bot_suicide(char* botnick, char* code, char* msg)
  1246. {
  1247. conf_bot *bot = NULL;
  1248. bool valid_source = 0;
  1249. for (bot = conf.bots; bot && bot->nick; bot = bot->next) {
  1250. if (!strcmp(botnick, bot->nick)) {
  1251. valid_source = 1;
  1252. break;
  1253. }
  1254. }
  1255. if (!valid_source) {
  1256. putlog(LOG_WARN, "*", STR("AN INVALID BOT (%s) JUST SENT ME A SUICIDE REQUEST!"), botnick);
  1257. return;
  1258. }
  1259. suicide(msg);
  1260. }
  1261. static cmd_t my_bot[] =
  1262. {
  1263. {"rd", "", (Function) bot_rd, NULL, 0},
  1264. {"r-sr", "", (Function) bot_rsimr, NULL, HUB},
  1265. {"r-s", "", (Function) bot_rsim, NULL, 0},
  1266. {"suicide", "", (Function) bot_suicide, NULL, 0},
  1267. {NULL, NULL, NULL, NULL, 0}
  1268. };
  1269. void init_botcmd()
  1270. {
  1271. add_builtins("bot", my_bot);
  1272. }
  1273. /* vim: set sts=2 sw=2 ts=8 et: */