botcmd.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555
  1. /*
  2. * botcmd.c -- handles:
  3. * commands that comes across the botnet
  4. * userfile transfer and update commands from sharebots
  5. *
  6. */
  7. #include "main.h"
  8. #include "tandem.h"
  9. #include "users.h"
  10. #include "chan.h"
  11. #include "modules.h"
  12. extern char botnetnick[], ver[], admin[], network[], motdfile[];
  13. extern int dcc_total, remote_boots, noshare, timesync, conmask;
  14. extern struct dcc_t *dcc;
  15. extern struct chanset_t *chanset;
  16. extern struct userrec *userlist;
  17. extern Tcl_Interp *interp;
  18. extern time_t now, online_since;
  19. extern party_t *party;
  20. extern module_entry *module_list;
  21. static char TBUF[1024]; /* Static buffer for goofy bot stuff */
  22. static char base64to[256] =
  23. {
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  28. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28,
  29. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  30. 49, 50, 51, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  33. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  34. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  35. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  36. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  37. };
  38. int base64_to_int(char *buf)
  39. {
  40. int i = 0;
  41. while (*buf) {
  42. i = i << 6;
  43. i += base64to[(int) *buf];
  44. buf++;
  45. }
  46. return i;
  47. }
  48. /* Used for 1.0 compatibility: if a join message arrives with no sock#,
  49. * i'll just grab the next "fakesock" # (incrementing to assure uniqueness)
  50. */
  51. static int fakesock = 2300;
  52. static void fake_alert(int idx, char *item, char *extra)
  53. {
  54. static unsigned long lastfake; /* The last time fake_alert was used */
  55. if (now - lastfake > 10) {
  56. /* Don't fake_alert more than once every 10secs */
  57. dprintf(idx, "ct %s NOTICE: %s (%s != %s).\n",
  58. botnetnick, NET_FAKEREJECT, item, extra);
  59. putlog(LOG_BOTS, "*", "%s %s (%s != %s).", dcc[idx].nick, NET_FAKEREJECT,
  60. 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, *p;
  69. int i, chan;
  70. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  71. return;
  72. from = newsplit(&msg);
  73. p = newsplit(&msg);
  74. chan = base64_to_int(p);
  75. /* Strip annoying control chars */
  76. for (p = from; *p;) {
  77. if ((*p < 32) || (*p == 127))
  78. strcpy(p, p + 1);
  79. else
  80. p++;
  81. }
  82. p = strchr(from, '@');
  83. if (p) {
  84. sprintf(TBUF, "<%s> %s", from, msg);
  85. *p = 0;
  86. if (!partyidle(p + 1, from)) {
  87. *p = '@';
  88. fake_alert(idx, "user", from);
  89. return;
  90. }
  91. *p = '@';
  92. p++;
  93. } else {
  94. sprintf(TBUF, "*** (%s) %s", from, msg);
  95. p = from;
  96. }
  97. i = nextbot(p);
  98. if (i != idx) {
  99. fake_alert(idx, "direction", p);
  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. if (strchr(from, '@') != NULL)
  106. check_tcl_chat(from, chan, msg);
  107. else
  108. check_tcl_bcst(from, chan, msg);
  109. }
  110. }
  111. #ifdef S_DCCPASS
  112. void bot_cmdpass(int idx, char *par)
  113. {
  114. char *p;
  115. p = strchr(par, ' ');
  116. if (p) {
  117. *p++ = 0;
  118. botnet_send_cmdpass(idx, par, p);
  119. p--;
  120. *p = ' ';
  121. } else {
  122. botnet_send_cmdpass(idx, par, "");
  123. }
  124. set_cmd_pass(par, 0);
  125. }
  126. #endif
  127. void bot_config(int idx, char *par)
  128. {
  129. got_config_share(idx, par);
  130. }
  131. void bot_remotecmd(int idx, char *par) {
  132. char *tbot, *fbot, *fhnd, *fidx;
  133. tbot=newsplit(&par);
  134. fbot=newsplit(&par);
  135. fhnd=newsplit(&par);
  136. fidx=newsplit(&par);
  137. if (!strcmp(tbot, botnetnick)) {
  138. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  139. } else if (!strcmp(tbot, "*")) {
  140. botnet_send_cmd_broad(idx, fbot, fhnd, atoi(fidx), par);
  141. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  142. } else {
  143. if (nextbot(tbot)!=idx)
  144. botnet_send_cmd(fbot, tbot, fhnd, atoi(fidx), par);
  145. }
  146. }
  147. void bot_remotereply(int idx, char *par) {
  148. char *tbot, *fbot, *fhnd, *fidx;
  149. tbot=newsplit(&par);
  150. fbot=newsplit(&par);
  151. fhnd=newsplit(&par);
  152. fidx=newsplit(&par);
  153. if (!strcmp(tbot, botnetnick)) {
  154. gotremotereply(fbot, fhnd, fidx, par);
  155. } else {
  156. if (nextbot(tbot)!=idx)
  157. botnet_send_cmdreply(fbot, tbot, fhnd, fidx, par);
  158. }
  159. }
  160. /* chat <from> <notice> -- only from bots
  161. */
  162. static void bot_chat(int idx, char *par)
  163. {
  164. char *from;
  165. int i;
  166. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  167. return;
  168. from = newsplit(&par);
  169. if (strchr(from, '@') != NULL) {
  170. fake_alert(idx, "bot", from);
  171. return;
  172. }
  173. /* Make sure the bot is valid */
  174. i = nextbot(from);
  175. if (i != idx) {
  176. fake_alert(idx, "direction", from);
  177. return;
  178. }
  179. chatout("*** (%s) %s\n", from, par);
  180. botnet_send_chat(idx, from, par);
  181. }
  182. /* actchan <from> <chan> <text>
  183. */
  184. static void bot_actchan(int idx, char *par)
  185. {
  186. char *from, *p;
  187. int i, chan;
  188. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  189. return;
  190. from = newsplit(&par);
  191. p = strchr(from, '@');
  192. if (p == NULL) {
  193. /* How can a bot do an action? */
  194. fake_alert(idx, "user@bot", from);
  195. return;
  196. }
  197. *p = 0;
  198. if (!partyidle(p + 1, from)) {
  199. *p = '@';
  200. fake_alert(idx, "user", from);
  201. return;
  202. }
  203. *p = '@';
  204. p++;
  205. i = nextbot(p);
  206. if (i != idx) {
  207. fake_alert(idx, "direction", p);
  208. return;
  209. }
  210. p = newsplit(&par);
  211. chan = base64_to_int(p);
  212. for (p = from; *p;) {
  213. if ((*p < 32) || (*p == 127))
  214. strcpy(p, p + 1);
  215. else
  216. p++;
  217. }
  218. chanout_but(-1, chan, "* %s %s\n", from, par);
  219. botnet_send_act(idx, from, NULL, chan, par);
  220. check_tcl_act(from, chan, par);
  221. }
  222. /* priv <from> <to> <message>
  223. */
  224. static void bot_priv(int idx, char *par)
  225. {
  226. char *from, *p, *to = TBUF, *tobot;
  227. int i;
  228. from = newsplit(&par);
  229. tobot = newsplit(&par);
  230. splitc(to, tobot, '@');
  231. p = strchr(from, '@');
  232. if (p != NULL)
  233. p++;
  234. else
  235. p = from;
  236. i = nextbot(p);
  237. if (i != idx) {
  238. fake_alert(idx, "direction", p);
  239. return;
  240. }
  241. if (!to[0])
  242. return; /* Silently ignore notes to '@bot' this
  243. * is legacy code */
  244. if (!egg_strcasecmp(tobot, botnetnick)) { /* For me! */
  245. if (p == from)
  246. add_note(to, from, par, -2, 0);
  247. else {
  248. i = add_note(to, from, par, -1, 0);
  249. if (from[0] != '@')
  250. switch (i) {
  251. case NOTE_ERROR:
  252. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s.", BOT_NOSUCHUSER, to);
  253. break;
  254. case NOTE_STORED:
  255. botnet_send_priv(idx, botnetnick, from, NULL, "%s", BOT_NOTESTORED2);
  256. break;
  257. case NOTE_FULL:
  258. botnet_send_priv(idx, botnetnick, from, NULL, "%s", BOT_NOTEBOXFULL);
  259. break;
  260. case NOTE_AWAY:
  261. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s", to, BOT_NOTEISAWAY);
  262. break;
  263. case NOTE_FWD:
  264. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s", "Not online; note forwarded to:", to);
  265. break;
  266. case NOTE_REJECT:
  267. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s", to, "rejected your note.");
  268. break;
  269. case NOTE_TCL:
  270. break; /* Do nothing */
  271. case NOTE_OK:
  272. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s.", BOT_NOTESENTTO, 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_sprintf(s, "%s %s. %s (lost %d bot%s and %d user%s)",
  289. BOT_DISCONNECTED, dcc[idx].nick, par[0] ?
  290. par : "No reason", bots, (bots != 1) ?
  291. "s" : "", users, (users != 1) ? "s" : "");
  292. putlog(LOG_BOTS, "*", "%s", s);
  293. chatout("*** %s\n", s);
  294. botnet_send_unlinked(idx, dcc[idx].nick, s);
  295. dprintf(idx, "*bye\n");
  296. killsock(dcc[idx].sock);
  297. lostdcc(idx);
  298. }
  299. static void remote_tell_who(int idx, char *nick, int chan)
  300. {
  301. int i = 10, k, l, ok = 0;
  302. char s[1024], *realnick;
  303. struct chanset_t *c;
  304. realnick = strchr(nick, ':');
  305. if (realnick)
  306. realnick++;
  307. else
  308. realnick = nick;
  309. putlog(LOG_BOTS, "*", "#%s# who", realnick);
  310. strcpy(s, "Channels: ");
  311. for (c = chanset; c; c = c->next)
  312. if (!channel_secret(c) && !channel_inactive(c)) {
  313. l = strlen(c->name);
  314. if (i + l < 1021) {
  315. if (i > 10)
  316. sprintf(s, "%s, %s", s, c->name);
  317. else {
  318. strcpy(s, c->name);
  319. i += (l + 2);
  320. }
  321. }
  322. }
  323. if (i > 10) {
  324. botnet_send_priv(idx, botnetnick, nick, NULL, "%s (%s)", s, ver);
  325. } else
  326. botnet_send_priv(idx, botnetnick, nick, NULL, "%s (%s)", BOT_NOCHANNELS,
  327. ver);
  328. if (admin[0])
  329. botnet_send_priv(idx, botnetnick, nick, NULL, "Admin: %s", admin);
  330. if (chan == 0)
  331. botnet_send_priv(idx, botnetnick, nick, NULL,
  332. "%s (* = %s, + = %s, @ = %s)",
  333. BOT_PARTYMEMBS, MISC_OWNER, MISC_MASTER, MISC_OP);
  334. else {
  335. simple_sprintf(s, "assoc %d", chan);
  336. if ((Tcl_Eval(interp, s) != TCL_OK) || !interp->result[0])
  337. botnet_send_priv(idx, botnetnick, 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. else
  344. botnet_send_priv(idx, botnetnick, nick, NULL,
  345. "%s '%s' (%s%d): (* = %s, + = %s, @ = %s)\n",
  346. BOT_PEOPLEONCHAN, interp->result,
  347. (chan < GLOBAL_CHANS) ? "" : "*",
  348. chan % GLOBAL_CHANS,
  349. MISC_OWNER, MISC_MASTER, MISC_OP);
  350. }
  351. for (i = 0; i < dcc_total; i++)
  352. if (dcc[i].type->flags & DCT_REMOTEWHO)
  353. if (dcc[i].u.chat->channel == chan) {
  354. k = sprintf(s, " %c%-15s %s",
  355. (geticon(i) == '-' ? ' ' : geticon(i)),
  356. dcc[i].nick, dcc[i].host);
  357. if (now - dcc[i].timeval > 300) {
  358. unsigned long days, hrs, mins;
  359. days = (now - dcc[i].timeval) / 86400;
  360. hrs = ((now - dcc[i].timeval) - (days * 86400)) / 3600;
  361. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  362. if (days > 0)
  363. sprintf(s + k, " (%s %lud%luh)",
  364. MISC_IDLE, days, hrs);
  365. else if (hrs > 0)
  366. sprintf(s + k, " (%s %luh%lum)",
  367. MISC_IDLE, hrs, mins);
  368. else
  369. sprintf(s + k, " (%s %lum)",
  370. MISC_IDLE, mins);
  371. }
  372. botnet_send_priv(idx, botnetnick, nick, NULL, "%s", s);
  373. if (dcc[i].u.chat->away != NULL)
  374. botnet_send_priv(idx, botnetnick, nick, NULL, " %s: %s",
  375. MISC_AWAY, dcc[i].u.chat->away);
  376. }
  377. for (i = 0; i < dcc_total; i++)
  378. if (dcc[i].type == &DCC_BOT) {
  379. if (!ok) {
  380. ok = 1;
  381. botnet_send_priv(idx, botnetnick, nick, NULL,
  382. "%s:", BOT_BOTSCONNECTED);
  383. }
  384. sprintf(s, " %s%c%-15s %s",
  385. dcc[i].status & STAT_CALLED ? "<-" : "->",
  386. dcc[i].status & STAT_SHARE ? '+' : ' ',
  387. dcc[i].nick, dcc[i].u.bot->version);
  388. botnet_send_priv(idx, botnetnick, nick, NULL, "%s", s);
  389. }
  390. ok = 0;
  391. for (i = 0; i < dcc_total; i++)
  392. if (dcc[i].type->flags & DCT_REMOTEWHO)
  393. if (dcc[i].u.chat->channel != chan) {
  394. if (!ok) {
  395. ok = 1;
  396. botnet_send_priv(idx, botnetnick, nick, NULL, "%s:",
  397. BOT_OTHERPEOPLE);
  398. }
  399. l = sprintf(s, " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
  400. dcc[i].nick, dcc[i].host);
  401. if (now - dcc[i].timeval > 300) {
  402. k = (now - dcc[i].timeval) / 60;
  403. if (k < 60)
  404. sprintf(s + l, " (%s %dm)", MISC_IDLE, k);
  405. else
  406. sprintf(s + l, " (%s %dh%dm)", MISC_IDLE, k / 60, k % 60);
  407. }
  408. botnet_send_priv(idx, botnetnick, nick, NULL, "%s", s);
  409. if (dcc[i].u.chat->away != NULL)
  410. botnet_send_priv(idx, botnetnick, nick, NULL,
  411. " %s: %s", MISC_AWAY,
  412. dcc[i].u.chat->away);
  413. }
  414. }
  415. static void bot_sysname(int idx, char *par)
  416. {
  417. //simply copy it all to the string pointer.
  418. putlog(LOG_DEBUG, "@", "%s is using %s", dcc[idx].nick, par);
  419. dcc[idx].u.bot->sysname[0] = 0;
  420. strcpy(dcc[idx].u.bot->sysname, par);
  421. }
  422. /* who <from@bot> <tobot> <chan#>
  423. */
  424. static void bot_who(int idx, char *par)
  425. {
  426. char *from, *to, *p;
  427. int i, chan;
  428. from = newsplit(&par);
  429. p = strchr(from, '@');
  430. if (!p) {
  431. sprintf(TBUF, "%s@%s", from, dcc[idx].nick);
  432. from = TBUF;
  433. }
  434. to = newsplit(&par);
  435. if (!egg_strcasecmp(to, botnetnick))
  436. to[0] = 0; /* (for me) */
  437. chan = base64_to_int(par);
  438. if (to[0]) { /* Pass it on */
  439. i = nextbot(to);
  440. if (i >= 0)
  441. botnet_send_who(i, from, to, chan);
  442. } else {
  443. remote_tell_who(idx, from, chan);
  444. }
  445. }
  446. static void bot_endlink(int idx, char *par)
  447. {
  448. dcc[idx].status &= ~STAT_LINKING;
  449. }
  450. /* info? <from@bot> -> send priv
  451. */
  452. static void bot_infoq(int idx, char *par)
  453. {
  454. char s[200], s2[32], *realnick;
  455. struct chanset_t *chan;
  456. time_t now2;
  457. int hr, min;
  458. /* Strip the idx from user@bot */
  459. realnick = strchr(par, ':');
  460. if (realnick)
  461. realnick++;
  462. else
  463. realnick = par;
  464. putlog(LOG_BOTS, "@", "#%s# botinfo", realnick);
  465. now2 = now - online_since;
  466. s2[0] = 0;
  467. if (now2 > 86400) {
  468. int days = now2 / 86400;
  469. /* Days */
  470. sprintf(s2, "%d day", days);
  471. if (days >= 2)
  472. strcat(s2, "s");
  473. strcat(s2, ", ");
  474. now2 -= days * 86400;
  475. }
  476. hr = (time_t) ((int) now2 / 3600);
  477. now2 -= (hr * 3600);
  478. min = (time_t) ((int) now2 / 60);
  479. sprintf(&s2[strlen(s2)], "%02d:%02d", (int) hr, (int) min);
  480. if (module_find("server", 0, 0)) {
  481. s[0] = 0;
  482. for (chan = chanset; chan; chan = chan->next) {
  483. if (!channel_secret(chan)) {
  484. if ((strlen(s) + strlen(chan->dname) + strlen(network)
  485. + strlen(botnetnick) + strlen(ver) + 1) >= 200) {
  486. strcat(s,"++ ");
  487. break; /* Yegads..! */
  488. }
  489. strcat(s, chan->dname);
  490. strcat(s, ", ");
  491. }
  492. }
  493. if (s[0]) {
  494. s[strlen(s) - 2] = 0;
  495. botnet_send_priv(idx, botnetnick, par, NULL,
  496. "%s <%s> (%s) [UP %s]", ver, network, s, s2);
  497. } else
  498. botnet_send_priv(idx, botnetnick, par, NULL,
  499. "%s <%s> (%s) [UP %s]", ver, network, BOT_NOCHANNELS,
  500. s2);
  501. } else
  502. botnet_send_priv(idx, botnetnick, par, NULL,
  503. "%s <NO_IRC> [UP %s]", ver, s2);
  504. botnet_send_infoq(idx, par);
  505. }
  506. static void bot_ping(int idx, char *par)
  507. {
  508. botnet_send_pong(idx);
  509. }
  510. static void bot_pong(int idx, char *par)
  511. {
  512. dcc[idx].status &= ~STAT_PINGED;
  513. if (dcc[idx].pingtime > (now - 120))
  514. dcc[idx].pingtime -= now;
  515. else
  516. dcc[idx].pingtime = 120;
  517. }
  518. /* link <from@bot> <who> <to-whom>
  519. */
  520. static void bot_link(int idx, char *par)
  521. {
  522. char *from, *bot, *rfrom;
  523. int i;
  524. from = newsplit(&par);
  525. bot = newsplit(&par);
  526. if (!egg_strcasecmp(bot, botnetnick)) {
  527. if ((rfrom = strchr(from, ':')))
  528. rfrom++;
  529. else
  530. rfrom = from;
  531. putlog(LOG_CMDS, "*", "#%s# link %s", rfrom, par);
  532. if (botlink(from, -1, par))
  533. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s ...",
  534. BOT_LINKATTEMPT, par);
  535. else
  536. botnet_send_priv(idx, botnetnick, from, NULL, "%s.",
  537. BOT_CANTLINKTHERE);
  538. } else {
  539. i = nextbot(bot);
  540. if (i >= 0)
  541. botnet_send_link(i, from, bot, par);
  542. }
  543. }
  544. /* unlink <from@bot> <linking-bot> <undesired-bot> <reason>
  545. */
  546. static void bot_unlink(int idx, char *par)
  547. {
  548. char *from, *bot, *rfrom, *p, *undes;
  549. int i;
  550. from = newsplit(&par);
  551. bot = newsplit(&par);
  552. undes = newsplit(&par);
  553. if (!egg_strcasecmp(bot, botnetnick)) {
  554. if ((rfrom = strchr(from, ':')))
  555. rfrom++;
  556. else
  557. rfrom = from;
  558. putlog(LOG_CMDS, "*", "#%s# unlink %s (%s)", rfrom, undes, par[0] ? par :
  559. "No reason");
  560. i = botunlink(-3, undes, par[0] ? par : NULL);
  561. if (i == 1) {
  562. p = strchr(from, '@');
  563. if (p) {
  564. /* idx will change after unlink -- get new idx
  565. *
  566. * TODO: This has changed with the new lostdcc() behaviour. Check
  567. * if we can optimise the situation.
  568. */
  569. i = nextbot(p + 1);
  570. if (i >= 0)
  571. botnet_send_priv(i, botnetnick, from, NULL,
  572. "Unlinked from %s.", undes);
  573. }
  574. } else if (i == 0) {
  575. botnet_send_unlinked(-1, undes, "");
  576. p = strchr(from, '@');
  577. if (p) {
  578. /* Ditto above, about idx */
  579. i = nextbot(p + 1);
  580. if (i >= 0)
  581. botnet_send_priv(i, botnetnick, from, NULL,
  582. "%s %s.", BOT_CANTUNLINK, undes);
  583. }
  584. } else {
  585. p = strchr(from, '@');
  586. if (p) {
  587. i = nextbot(p + 1);
  588. if (i >= 0)
  589. botnet_send_priv(i, botnetnick, from, NULL,
  590. "Can't remotely unlink sharebots.");
  591. }
  592. }
  593. } else {
  594. i = nextbot(bot);
  595. if (i >= 0)
  596. botnet_send_unlink(i, from, bot, undes, par);
  597. }
  598. }
  599. /* Bot next share?
  600. */
  601. static void bot_update(int idx, char *par)
  602. {
  603. char *bot, x;
  604. int vnum;
  605. bot = newsplit(&par);
  606. x = par[0];
  607. if (x)
  608. par++;
  609. vnum = base64_to_int(par);
  610. if (in_chain(bot))
  611. updatebot(idx, bot, x, vnum);
  612. }
  613. static void bot_mtcl(char *botnick, char *code, char *par)
  614. {
  615. int oidx = 0, tcode = 0;
  616. char ret[2000];
  617. if (!par[0])
  618. return;
  619. oidx = atoi(newsplit(&par));
  620. if (!par[0])
  621. return;
  622. tcode = Tcl_GlobalEval(interp, par);
  623. if (tcode == TCL_OK)
  624. egg_snprintf(ret, sizeof ret, "r_mt %d Tcl: %s", oidx, interp->result);
  625. else
  626. egg_snprintf(ret, sizeof ret, "r_mt %d Tcl error: %s", oidx, interp->result);
  627. putbot(botnick, ret);
  628. }
  629. static void bot_rmtcl(char *botnick, char *code, char *par)
  630. {
  631. int oidx = 0;
  632. oidx = atoi(newsplit(&par));
  633. if (!par[0])
  634. return;
  635. dprintf(oidx, "(%s) %s\n", botnick, par);
  636. }
  637. /* Newbot next share?
  638. */
  639. static void bot_nlinked(int idx, char *par)
  640. {
  641. char *newbot, *next, *p, s[1024], x;
  642. int bogus = 0, i;
  643. struct userrec *u;
  644. newbot = newsplit(&par);
  645. next = newsplit(&par);
  646. s[0] = 0;
  647. if (!next[0]) {
  648. putlog(LOG_BOTS, "*", "Invalid eggnet protocol from %s (zapfing)",
  649. dcc[idx].nick);
  650. simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick,
  651. MISC_INVALIDBOT);
  652. dprintf(idx, "error invalid eggnet protocol for 'nlinked'\n");
  653. } else if ((in_chain(newbot)) || (!egg_strcasecmp(newbot, botnetnick))) {
  654. /* Loop! */
  655. putlog(LOG_BOTS, "*", "%s %s (mutual: %s)",
  656. BOT_LOOPDETECT, dcc[idx].nick, newbot);
  657. simple_sprintf(s, "%s %s: disconnecting %s", MISC_LOOP, newbot,
  658. dcc[idx].nick);
  659. dprintf(idx, "error Loop (%s)\n", newbot);
  660. }
  661. if (!s[0]) {
  662. for (p = newbot; *p; p++)
  663. if ((*p < 32) || (*p == 127) || ((p - newbot) >= HANDLEN))
  664. bogus = 1;
  665. i = nextbot(next);
  666. if (i != idx)
  667. bogus = 1;
  668. }
  669. if (bogus) {
  670. putlog(LOG_BOTS, "*", "%s %s! (%s -> %s)", BOT_BOGUSLINK, dcc[idx].nick,
  671. next, newbot);
  672. simple_sprintf(s, "%s: %s %s", BOT_BOGUSLINK, dcc[idx].nick,
  673. MISC_DISCONNECTED);
  674. dprintf(idx, "error %s (%s -> %s)\n",
  675. BOT_BOGUSLINK, next, newbot);
  676. }
  677. if (s[0]) {
  678. chatout("*** %s\n", s);
  679. botnet_send_unlinked(idx, dcc[idx].nick, s);
  680. dprintf(idx, "bye %s\n", BOT_ILLEGALLINK);
  681. killsock(dcc[idx].sock);
  682. lostdcc(idx);
  683. return;
  684. }
  685. x = par[0];
  686. if (x)
  687. par++;
  688. else
  689. x = '-';
  690. i = base64_to_int(par);
  691. botnet_send_nlinked(idx, newbot, next, x, i);
  692. if (x == '!') {
  693. #ifdef HUB
  694. chatout("*** (%s) %s %s.\n", next, NET_LINKEDTO, newbot);
  695. #else
  696. chatout("*** %s linked to botnet.\n", newbot);
  697. #endif
  698. x = '-';
  699. }
  700. addbot(newbot, dcc[idx].nick, next, x, i);
  701. check_tcl_link(newbot, next);
  702. u = get_user_by_handle(userlist, newbot);
  703. if (bot_flags(u) & BOT_REJECT) {
  704. botnet_send_reject(idx, botnetnick, NULL, newbot, NULL, NULL);
  705. putlog(LOG_BOTS, "*", "%s %s %s %s", BOT_REJECTING,
  706. newbot, MISC_FROM, dcc[idx].nick);
  707. }
  708. }
  709. static void bot_unlinked(int idx, char *par)
  710. {
  711. int i;
  712. char *bot;
  713. bot = newsplit(&par);
  714. i = nextbot(bot);
  715. if ((i >= 0) && (i != idx)) /* Bot is NOT downstream along idx, so
  716. * BOGUS! */
  717. fake_alert(idx, "direction", bot);
  718. else if (i >= 0) { /* Valid bot downstream of idx */
  719. if (par[0])
  720. //#ifdef HUB
  721. chatout("*** (%s) %s\n", lastbot(bot), par);
  722. //#else
  723. // chatout("*** %s unlinked from botnet.\n", par);
  724. //#endif
  725. botnet_send_unlinked(idx, bot, par);
  726. unvia(idx, findbot(bot));
  727. rembot(bot);
  728. }
  729. /* Otherwise it's not even a valid bot, so just ignore! */
  730. }
  731. /* trace <from@bot> <dest> <chain:chain..>
  732. */
  733. static void bot_trace(int idx, char *par)
  734. {
  735. char *from, *dest;
  736. int i;
  737. from = newsplit(&par);
  738. dest = newsplit(&par);
  739. simple_sprintf(TBUF, "%s:%s", par, botnetnick);
  740. botnet_send_traced(idx, from, TBUF);
  741. if (egg_strcasecmp(dest, botnetnick) && ((i = nextbot(dest)) > 0))
  742. botnet_send_trace(i, from, dest, par);
  743. }
  744. /* traced <to@bot> <chain:chain..>
  745. */
  746. static void bot_traced(int idx, char *par)
  747. {
  748. char *to, *p;
  749. int i, sock;
  750. to = newsplit(&par);
  751. p = strchr(to, '@');
  752. if (p == NULL)
  753. p = to;
  754. else {
  755. *p = 0;
  756. p++;
  757. }
  758. if (!egg_strcasecmp(p, botnetnick)) {
  759. time_t t = 0;
  760. char *p = par, *ss = TBUF;
  761. splitc(ss, to, ':');
  762. if (ss[0])
  763. sock = atoi(ss);
  764. else
  765. sock = (-1);
  766. if (par[0] == ':') {
  767. t = atoi(par + 1);
  768. p = strchr(par + 1, ':');
  769. if (p)
  770. p++;
  771. else
  772. p = par + 1;
  773. }
  774. for (i = 0; i < dcc_total; i++)
  775. if ((dcc[i].type->flags & DCT_CHAT) &&
  776. (!egg_strcasecmp(dcc[i].nick, to)) &&
  777. ((sock == (-1)) || (sock == dcc[i].sock))) {
  778. if (t) {
  779. int j=0;
  780. {
  781. register char *c=p;
  782. for (; *c != '\0'; c++) if (*c == ':') j++;
  783. }
  784. dprintf(i, "%s -> %s (%lu secs, %d hop%s)\n", BOT_TRACERESULT, p,
  785. now - t, j, (j != 1) ? "s" : "");
  786. } else
  787. dprintf(i, "%s -> %s\n", BOT_TRACERESULT, p);
  788. }
  789. } else {
  790. i = nextbot(p);
  791. if (p != to)
  792. *--p = '@';
  793. if (i >= 0)
  794. botnet_send_traced(i, to, par);
  795. }
  796. }
  797. void bot_timesync(int idx, char *par)
  798. {
  799. putlog(LOG_DEBUG, "@", "Got timesync from %s: %s\n", dcc[idx].nick, par);
  800. timesync = atoi(par) - now;
  801. #ifdef HUB
  802. send_timesync(-1);
  803. #endif
  804. }
  805. /* reject <from> <bot>
  806. */
  807. static void bot_reject(int idx, char *par)
  808. {
  809. char *from, *who, *destbot, *frombot;
  810. struct userrec *u;
  811. int i;
  812. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  813. return;
  814. from = newsplit(&par);
  815. frombot = strchr(from, '@');
  816. if (frombot)
  817. frombot++;
  818. else
  819. frombot = from;
  820. i = nextbot(frombot);
  821. if (i != idx) {
  822. fake_alert(idx, "direction", frombot);
  823. return;
  824. }
  825. who = newsplit(&par);
  826. if (!(destbot = strchr(who, '@'))) {
  827. /* Rejecting a bot */
  828. i = nextbot(who);
  829. if (i < 0) {
  830. botnet_send_priv(idx, botnetnick, from, NULL, "%s %s (%s)",
  831. BOT_CANTUNLINK, who, BOT_DOESNTEXIST);
  832. } else if (!egg_strcasecmp(dcc[i].nick, who)) {
  833. char s[1024];
  834. /* I'm the connection to the rejected bot */
  835. putlog(LOG_BOTS, "*", "%s %s %s", from, MISC_REJECTED, dcc[i].nick);
  836. dprintf(i, "bye %s\n", par[0] ? par : MISC_REJECTED);
  837. simple_sprintf(s, "%s %s (%s: %s)",
  838. MISC_DISCONNECTED, dcc[i].nick, from,
  839. par[0] ? par : MISC_REJECTED);
  840. chatout("*** %s\n", s);
  841. botnet_send_unlinked(i, dcc[i].nick, s);
  842. killsock(dcc[i].sock);
  843. lostdcc(i);
  844. } else {
  845. if (i >= 0)
  846. botnet_send_reject(i, from, NULL, who, NULL, par);
  847. }
  848. } else { /* Rejecting user */
  849. *destbot++ = 0;
  850. if (!egg_strcasecmp(destbot, botnetnick)) {
  851. /* Kick someone here! */
  852. int ok = 0;
  853. if (remote_boots == 1) {
  854. frombot = strchr(from, '@');
  855. if (frombot == NULL)
  856. frombot = from;
  857. else
  858. frombot++;
  859. u = get_user_by_handle(userlist, frombot);
  860. if (!(bot_flags(u) & BOT_SHARE)) {
  861. add_note(from, botnetnick, "No non sharebot boots.", -1, 0);
  862. ok = 1;
  863. }
  864. } else if (remote_boots == 0) {
  865. botnet_send_priv(idx, botnetnick, from, NULL, "%s",
  866. BOT_NOREMOTEBOOT);
  867. ok = 1;
  868. }
  869. for (i = 0; (i < dcc_total) && (!ok); i++)
  870. if ((!egg_strcasecmp(who, dcc[i].nick)) &&
  871. (dcc[i].type->flags & DCT_CHAT)) {
  872. u = get_user_by_handle(userlist, dcc[i].nick);
  873. if (u &&
  874. ((u->flags & USER_OWNER) && !(dcc[idx].user->flags & USER_ADMIN))) {
  875. add_note(from, botnetnick, BOT_NOOWNERBOOT, -1, 0);
  876. return;
  877. }
  878. do_boot(i, from, par);
  879. ok = 1;
  880. putlog(LOG_CMDS, "*", "#%s# boot %s (%s)", from, who,
  881. par[0] ? par : "No reason");
  882. }
  883. } else {
  884. i = nextbot(destbot);
  885. *--destbot = '@';
  886. if (i >= 0)
  887. botnet_send_reject(i, from, NULL, who, NULL, par);
  888. }
  889. }
  890. }
  891. static void bot_thisbot(int idx, char *par)
  892. {
  893. if (egg_strcasecmp(par, dcc[idx].nick)) {
  894. char s[1024];
  895. putlog(LOG_BOTS, "*", NET_WRONGBOT, dcc[idx].nick, par);
  896. dprintf(idx, "bye %s\n", MISC_IMPOSTER);
  897. simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick,
  898. MISC_IMPOSTER);
  899. chatout("*** %s\n", s);
  900. botnet_send_unlinked(idx, dcc[idx].nick, s);
  901. unvia(idx, findbot(dcc[idx].nick));
  902. killsock(dcc[idx].sock);
  903. lostdcc(idx);
  904. return;
  905. }
  906. if (bot_flags(dcc[idx].user) & BOT_LEAF)
  907. dcc[idx].status |= STAT_LEAF;
  908. /* Set capitalization the way they want it */
  909. noshare = 1;
  910. change_handle(dcc[idx].user, par);
  911. noshare = 0;
  912. strcpy(dcc[idx].nick, par);
  913. }
  914. static void bot_hublog(char *botnick, char *code, char *par)
  915. {
  916. #ifdef HUB
  917. int type;
  918. type = atoi(newsplit(&par));
  919. putlog(type, "@", "(%s) %s", botnick, par);
  920. #endif
  921. }
  922. static void bot_handshake(int idx, char *par)
  923. {
  924. struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick);
  925. /* We *don't* want botnet passwords migrating */
  926. noshare = 1;
  927. set_user(&USERENTRY_PASS, u, par);
  928. noshare = 0;
  929. }
  930. /* Used to send a direct msg from Tcl on one bot to Tcl on another
  931. * zapf <frombot> <tobot> <code [param]>
  932. */
  933. static void bot_zapf(int idx, char *par)
  934. {
  935. char *from, *to;
  936. int i;
  937. from = newsplit(&par);
  938. to = newsplit(&par);
  939. i = nextbot(from);
  940. if (i != idx) {
  941. fake_alert(idx, "direction", from);
  942. return;
  943. }
  944. if (!egg_strcasecmp(to, botnetnick)) {
  945. /* For me! */
  946. char *opcode;
  947. opcode = newsplit(&par);
  948. check_tcl_bot(from, opcode, par);
  949. return;
  950. }
  951. i = nextbot(to);
  952. if (i >= 0)
  953. botnet_send_zapf(i, from, to, par);
  954. }
  955. /* Used to send a global msg from Tcl on one bot to every other bot
  956. * zapf-broad <frombot> <code [param]>
  957. */
  958. static void bot_zapfbroad(int idx, char *par)
  959. {
  960. char *from, *opcode;
  961. int i;
  962. from = newsplit(&par);
  963. opcode = newsplit(&par);
  964. i = nextbot(from);
  965. if (i != idx) {
  966. fake_alert(idx, "direction", from);
  967. return;
  968. }
  969. check_tcl_bot(from, opcode, par);
  970. botnet_send_zapf_broad(idx, from, opcode, par);
  971. }
  972. /* These are still here, so that they will pass the relevant
  973. * requests through even if no filesys is loaded.
  974. *
  975. * filereject <bot:filepath> <sock:nick@bot> <reason...>
  976. */
  977. static void bot_filereject(int idx, char *par)
  978. {
  979. char *path, *to, *tobot, *p;
  980. int i;
  981. path = newsplit(&par);
  982. to = newsplit(&par);
  983. if ((tobot = strchr(to, '@')))
  984. tobot++;
  985. else
  986. tobot = to; /* Bot wants a file?! :) */
  987. if (egg_strcasecmp(tobot, botnetnick)) { /* for me! */
  988. p = strchr(to, ':');
  989. if (p != NULL) {
  990. *p = 0;
  991. for (i = 0; i < dcc_total; i++) {
  992. if (dcc[i].sock == atoi(to))
  993. dprintf(i, "%s (%s): %s\n", BOT_XFERREJECTED, path, par);
  994. }
  995. *p = ':';
  996. }
  997. /* No ':'? malformed */
  998. putlog(LOG_FILES, "*", "%s %s: %s", path, MISC_REJECTED, par);
  999. } else { /* Pass it on */
  1000. i = nextbot(tobot);
  1001. if (i >= 0)
  1002. botnet_send_filereject(i, path, to, par);
  1003. }
  1004. }
  1005. /* filereq <sock:nick@bot> <bot:file>
  1006. */
  1007. static void bot_filereq(int idx, char *tobot)
  1008. {
  1009. char *from, *path;
  1010. int i;
  1011. from = newsplit(&tobot);
  1012. if ((path = strchr(tobot, ':'))) {
  1013. *path++ = 0;
  1014. if (!egg_strcasecmp(tobot, botnetnick)) { /* For me! */
  1015. /* Process this */
  1016. module_entry *fs = module_find("filesys", 0, 0);
  1017. if (fs == NULL)
  1018. botnet_send_priv(idx, botnetnick, from, NULL, MOD_NOFILESYSMOD);
  1019. else {
  1020. Function f = fs->funcs[FILESYS_REMOTE_REQ];
  1021. f(idx, from, path);
  1022. }
  1023. } else { /* Pass it on */
  1024. i = nextbot(tobot);
  1025. if (i >= 0)
  1026. botnet_send_filereq(i, from, tobot, path);
  1027. }
  1028. }
  1029. }
  1030. /* filesend <bot:path> <sock:nick@bot> <IP#> <port> <size>
  1031. */
  1032. static void bot_filesend(int idx, char *par)
  1033. {
  1034. char *botpath, *to, *tobot, *nick;
  1035. int i;
  1036. char *nfn;
  1037. botpath = newsplit(&par);
  1038. to = newsplit(&par);
  1039. if ((tobot = strchr(to, '@'))) {
  1040. *tobot = 0;
  1041. tobot++;
  1042. } else {
  1043. tobot = to;
  1044. }
  1045. if (!egg_strcasecmp(tobot, botnetnick)) { /* For me! */
  1046. nfn = strrchr(botpath, '/');
  1047. if (nfn == NULL) {
  1048. nfn = strrchr(botpath, ':');
  1049. if (nfn == NULL)
  1050. nfn = botpath; /* That's odd. */
  1051. else
  1052. nfn++;
  1053. } else
  1054. nfn++;
  1055. if ((nick = strchr(to, ':')))
  1056. nick++;
  1057. else
  1058. nick = to;
  1059. /* Send it to 'nick' as if it's from me */
  1060. dprintf(DP_SERVER, "PRIVMSG %s :\001DCC SEND %s %s\001\n", nick, nfn, par);
  1061. } else {
  1062. i = nextbot(tobot);
  1063. if (i >= 0) {
  1064. *--tobot = '@';
  1065. botnet_send_filesend(i, botpath, to, par);
  1066. }
  1067. }
  1068. }
  1069. static void bot_error(int idx, char *par)
  1070. {
  1071. putlog(LOG_MISC | LOG_BOTS, "*", "%s: %s", dcc[idx].nick, par);
  1072. }
  1073. /* nc <bot> <sock> <newnick>
  1074. */
  1075. static void bot_nickchange(int idx, char *par)
  1076. {
  1077. char *bot, *ssock, *newnick;
  1078. int sock, i;
  1079. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  1080. return;
  1081. bot = newsplit(&par);
  1082. i = nextbot(bot);
  1083. if (i != idx) {
  1084. fake_alert(idx, "direction", bot);
  1085. return;
  1086. }
  1087. ssock = newsplit(&par);
  1088. sock = base64_to_int(ssock);
  1089. newnick = newsplit(&par);
  1090. i = partynick(bot, sock, newnick);
  1091. if (i < 0) {
  1092. fake_alert(idx, "sock#", ssock);
  1093. return;
  1094. }
  1095. chanout_but(-1, party[i].chan, "*** (%s) Nick change: %s -> %s\n",
  1096. bot, newnick, party[i].nick);
  1097. botnet_send_nkch_part(idx, i, newnick);
  1098. }
  1099. /* join <bot> <nick> <chan> <flag><sock> <from>
  1100. */
  1101. static void bot_join(int idx, char *par)
  1102. {
  1103. char *bot, *nick, *x, *y;
  1104. struct userrec *u;
  1105. int i, sock, chan, i2, linking = 0;
  1106. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  1107. return;
  1108. bot = newsplit(&par);
  1109. if (bot[0] == '!') {
  1110. linking = 1;
  1111. bot++;
  1112. }
  1113. if (b_status(idx) & STAT_LINKING) {
  1114. linking = 1;
  1115. }
  1116. nick = newsplit(&par);
  1117. x = newsplit(&par);
  1118. chan = base64_to_int(x);
  1119. y = newsplit(&par);
  1120. if ((chan < 0) || !y[0])
  1121. return; /* Woops! pre 1.2.1's send .chat off'ers
  1122. * too!! */
  1123. if (!y[0]) {
  1124. y[0] = '-';
  1125. sock = 0;
  1126. } else {
  1127. sock = base64_to_int(y + 1);
  1128. }
  1129. /* 1.1 bots always send a sock#, even on a channel change
  1130. * so if sock# is 0, this is from an old bot and we must tread softly
  1131. * grab old sock# if there is one, otherwise make up one.
  1132. */
  1133. if (sock == 0)
  1134. sock = partysock(bot, nick);
  1135. if (sock == 0)
  1136. sock = fakesock++;
  1137. i = nextbot(bot);
  1138. if (i != idx) { /* Ok, garbage from a 1.0g (who uses that
  1139. * now?) OR raistlin being evil :) */
  1140. fake_alert(idx, "direction", bot);
  1141. return;
  1142. }
  1143. u = get_user_by_handle(userlist, nick);
  1144. if (u) {
  1145. sprintf(TBUF, "@%s", bot);
  1146. touch_laston(u, TBUF, now);
  1147. }
  1148. i = addparty(bot, nick, chan, y[0], sock, par, &i2);
  1149. botnet_send_join_party(idx, linking, i2, i);
  1150. if (i != chan) {
  1151. if (i >= 0) {
  1152. if (b_numver(idx) >= NEAT_BOTNET)
  1153. chanout_but(-1, i, "*** (%s) %s %s %s.\n", bot, nick, NET_LEFTTHE,
  1154. i ? "channel" : "party line");
  1155. check_tcl_chpt(bot, nick, sock, i);
  1156. }
  1157. if ((b_numver(idx) >= NEAT_BOTNET) && !linking)
  1158. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", bot, nick, NET_JOINEDTHE,
  1159. chan ? "channel" : "party line");
  1160. check_tcl_chjn(bot, nick, chan, y[0], sock, par);
  1161. }
  1162. }
  1163. /* part <bot> <nick> <sock> [etc..]
  1164. */
  1165. static void bot_part(int idx, char *par)
  1166. {
  1167. char *bot, *nick, *etc;
  1168. struct userrec *u;
  1169. int sock, partyidx;
  1170. int silent = 0;
  1171. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  1172. return;
  1173. bot = newsplit(&par);
  1174. if (bot[0] == '!') {
  1175. silent = 1;
  1176. bot++;
  1177. }
  1178. nick = newsplit(&par);
  1179. etc = newsplit(&par);
  1180. sock = base64_to_int(etc);
  1181. if (sock == 0)
  1182. sock = partysock(bot, nick);
  1183. u = get_user_by_handle(userlist, nick);
  1184. if (u) {
  1185. sprintf(TBUF, "@%s", bot);
  1186. touch_laston(u, TBUF, now);
  1187. }
  1188. if ((partyidx = getparty(bot, sock)) != -1) {
  1189. if (party[partyidx].chan >= 0)
  1190. check_tcl_chpt(bot, nick, sock, party[partyidx].chan);
  1191. if ((b_numver(idx) >= NEAT_BOTNET) && !silent) {
  1192. register int chan = party[partyidx].chan;
  1193. if (par[0])
  1194. chanout_but(-1, chan, "*** (%s) %s %s %s (%s).\n", bot, nick,
  1195. NET_LEFTTHE,
  1196. chan ? "channel" : "party line", par);
  1197. else
  1198. chanout_but(-1, chan, "*** (%s) %s %s %s.\n", bot, nick,
  1199. NET_LEFTTHE,
  1200. chan ? "channel" : "party line");
  1201. }
  1202. botnet_send_part_party(idx, partyidx, par, silent);
  1203. remparty(bot, sock);
  1204. }
  1205. }
  1206. /* away <bot> <sock> <message>
  1207. * null message = unaway
  1208. */
  1209. static void bot_away(int idx, char *par)
  1210. {
  1211. char *bot, *etc;
  1212. int sock, partyidx, linking = 0;
  1213. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  1214. return;
  1215. bot = newsplit(&par);
  1216. if (bot[0] == '!') {
  1217. linking = 1;
  1218. bot++;
  1219. }
  1220. if (b_status(idx) & STAT_LINKING) {
  1221. linking = 1;
  1222. }
  1223. etc = newsplit(&par);
  1224. sock = base64_to_int(etc);
  1225. if (sock == 0)
  1226. sock = partysock(bot, etc);
  1227. check_tcl_away(bot, sock, par);
  1228. if (par[0]) {
  1229. partystat(bot, sock, PLSTAT_AWAY, 0);
  1230. partyaway(bot, sock, par);
  1231. } else {
  1232. partystat(bot, sock, 0, PLSTAT_AWAY);
  1233. }
  1234. partyidx = getparty(bot, sock);
  1235. if ((b_numver(idx) >= NEAT_BOTNET) && !linking) {
  1236. if (par[0])
  1237. chanout_but(-1, party[partyidx].chan,
  1238. "*** (%s) %s %s: %s.\n", bot,
  1239. party[partyidx].nick, NET_AWAY, par);
  1240. else
  1241. chanout_but(-1, party[partyidx].chan,
  1242. "*** (%s) %s %s.\n", bot,
  1243. party[partyidx].nick, NET_UNAWAY);
  1244. }
  1245. botnet_send_away(idx, bot, sock, par, linking);
  1246. }
  1247. /* (a courtesy info to help during connect bursts)
  1248. * idle <bot> <sock> <#secs> [away msg]
  1249. */
  1250. static void bot_idle(int idx, char *par)
  1251. {
  1252. char *bot, *work;
  1253. int sock, idle;
  1254. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  1255. return;
  1256. bot = newsplit(&par);
  1257. work = newsplit(&par);
  1258. sock = base64_to_int(work);
  1259. if (sock == 0)
  1260. sock = partysock(bot, work);
  1261. work = newsplit(&par);
  1262. idle = base64_to_int(work);
  1263. partysetidle(bot, sock, idle);
  1264. if (par[0]) {
  1265. partystat(bot, sock, PLSTAT_AWAY, 0);
  1266. partyaway(bot, sock, par);
  1267. }
  1268. botnet_send_idle(idx, bot, sock, idle, par);
  1269. }
  1270. void bot_share(int idx, char *par)
  1271. {
  1272. sharein(idx, par);
  1273. }
  1274. void bot_shareupdate(int idx, char *par)
  1275. {
  1276. shareupdatein(idx, par);
  1277. }
  1278. /* v <frombot> <tobot> <idx:nick>
  1279. */
  1280. static void bot_versions(int sock, char *par)
  1281. {
  1282. char *frombot = newsplit(&par), *tobot, *from;
  1283. module_entry *me;
  1284. if (nextbot(frombot) != sock)
  1285. fake_alert(sock, "versions-direction", frombot);
  1286. else if (egg_strcasecmp(tobot = newsplit(&par), botnetnick)) {
  1287. if ((sock = nextbot(tobot)) >= 0)
  1288. dprintf(sock, "v %s %s %s\n", frombot, tobot, par);
  1289. } else {
  1290. from = newsplit(&par);
  1291. botnet_send_priv(sock, botnetnick, from, frombot, "Modules loaded:\n");
  1292. for (me = module_list; me; me = me->next)
  1293. botnet_send_priv(sock, botnetnick, from, frombot, " Module: %s (v%d.%d)\n",
  1294. me->name, me->major, me->minor);
  1295. botnet_send_priv(sock, botnetnick, from, frombot, "End of module list.\n");
  1296. }
  1297. }
  1298. /* BOT COMMANDS
  1299. *
  1300. * function call should be:
  1301. * int bot_whatever(idx,"parameters");
  1302. *
  1303. * SORT these, dcc_bot uses a shortcut which requires them sorted
  1304. *
  1305. * yup, those are tokens there to allow a more efficient botnet as
  1306. * time goes on (death to slowly upgrading llama's)
  1307. */
  1308. botcmd_t C_bot[] =
  1309. {
  1310. {"a", (Function) bot_actchan},
  1311. {"aw", (Function) bot_away},
  1312. {"away", (Function) bot_away},
  1313. {"bye", (Function) bot_bye},
  1314. {"c", (Function) bot_chan2},
  1315. {"cg", (Function) bot_config},
  1316. #ifdef S_DCCPASS
  1317. {"cp", (Function) bot_cmdpass},
  1318. #endif
  1319. {"ct", (Function) bot_chat},
  1320. {"e", (Function) bot_error},
  1321. {"el", (Function) bot_endlink},
  1322. {"f!", (Function) bot_filereject},
  1323. {"fr", (Function) bot_filereq},
  1324. {"fs", (Function) bot_filesend},
  1325. {"hs", (Function) bot_handshake},
  1326. {"i", (Function) bot_idle},
  1327. {"i?", (Function) bot_infoq},
  1328. {"j", (Function) bot_join},
  1329. {"l", (Function) bot_link},
  1330. {"n", (Function) bot_nlinked},
  1331. {"nc", (Function) bot_nickchange},
  1332. {"p", (Function) bot_priv},
  1333. {"pi", (Function) bot_ping},
  1334. {"po", (Function) bot_pong},
  1335. {"pt", (Function) bot_part},
  1336. {"r", (Function) bot_reject},
  1337. {"rc", (Function) bot_remotecmd},
  1338. {"rr", (Function) bot_remotereply},
  1339. {"s", (Function) bot_share},
  1340. {"sb", (Function) bot_shareupdate},
  1341. {"t", (Function) bot_trace},
  1342. {"tb", (Function) bot_thisbot},
  1343. {"td", (Function) bot_traced},
  1344. {"ts", (Function) bot_timesync},
  1345. {"u", (Function) bot_update},
  1346. {"ul", (Function) bot_unlink},
  1347. {"un", (Function) bot_unlinked},
  1348. {"v", (Function) bot_versions},
  1349. {"vs", (Function) bot_sysname},
  1350. {"w", (Function) bot_who},
  1351. {"z", (Function) bot_zapf},
  1352. {"zb", (Function) bot_zapfbroad},
  1353. {NULL, NULL}
  1354. };
  1355. void send_remote_simul(int idx, char *bot, char *cmd, char *par)
  1356. {
  1357. char msg[SGRAB-110];
  1358. snprintf(msg, sizeof msg, "r-s %d %s %d %s %s %s", idx, dcc[idx].nick, dcc[idx].u.chat->con_flags, dcc[idx].u.chat->con_chan, cmd, par);
  1359. putbot(bot, msg);
  1360. }
  1361. /* idx nick conmask cmd par */
  1362. static void bot_rsim(char *botnick, char *code, char *par)
  1363. {
  1364. int ridx = -1, idx = -1, i = 0, rconmask;
  1365. char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX];
  1366. ridx = atoi(newsplit(&par));
  1367. nick = newsplit(&par);
  1368. rconmask = atoi(newsplit(&par));
  1369. rconchan = newsplit(&par);
  1370. cmd = newsplit(&par);
  1371. if (ridx < 0 || !nick || !cmd)
  1372. return;
  1373. for (i = 0; i < dcc_total; i++) {
  1374. if (dcc[i].simul == ridx) {
  1375. putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
  1376. dcc[i].simultime = now;
  1377. idx = i;
  1378. break;
  1379. }
  1380. }
  1381. if (idx < 0) {
  1382. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1383. putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
  1384. /* dcc[idx].sock = 666; */
  1385. dcc[idx].timeval = now;
  1386. dcc[idx].simultime = now;
  1387. dcc[idx].simul = ridx;
  1388. strcpy(dcc[idx].simulbot, botnick);
  1389. dcc[idx].status = STAT_ECHO;
  1390. dcc[idx].u.chat->con_flags = rconmask;
  1391. strcpy(dcc[idx].u.chat->con_chan, rconchan);
  1392. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  1393. strcpy(dcc[idx].nick, nick);
  1394. snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
  1395. strcpy(dcc[idx].host, buf);
  1396. dcc[idx].addr = 0;
  1397. dcc[idx].user = get_user_by_handle(userlist, nick);
  1398. }
  1399. check_tcl_dcc(cmd, idx, par);
  1400. }
  1401. void bounce_simul(int idx, char *buf)
  1402. {
  1403. char rmsg[SGRAB-110];
  1404. if (!buf || !buf[0] || !dcc[idx].simulbot || !dcc[idx].simulbot[0] || idx < 0) {
  1405. return;
  1406. }
  1407. snprintf(rmsg, sizeof rmsg, "r-sr %d %s", dcc[idx].ridx, buf); /* remote-simul[r]eturn idx buf */
  1408. putbot(dcc[idx].simulbot, rmsg);
  1409. }
  1410. #ifdef HUB
  1411. static void bot_rsimr(char *botnick, char *code, char *par)
  1412. {
  1413. int idx;
  1414. idx = atoi(newsplit(&par));
  1415. if (!par[0])
  1416. return;
  1417. dprintf(idx, "[%s] %s\n", botnick, par);
  1418. }
  1419. #endif /* HUB */
  1420. static cmd_t my_bot[] =
  1421. {
  1422. {"hl", "", (Function) bot_hublog, NULL},
  1423. {"mt", "", (Function) bot_mtcl, NULL},
  1424. {"r_mt", "", (Function) bot_rmtcl, NULL},
  1425. #ifdef HUB /* This will only allow hubs to read the return text */
  1426. {"r-sr", "", (Function) bot_rsimr, NULL},
  1427. #endif /* HUB */
  1428. {"r-s", "", (Function) bot_rsim, NULL},
  1429. {NULL, NULL, NULL, NULL}
  1430. };
  1431. void init_botcmd()
  1432. {
  1433. add_builtins(H_bot, my_bot);
  1434. }