botcmd.c 36 KB

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