botcmd.c 32 KB

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