botcmd.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557
  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 bounce_simul(int idx, char *buf)
  1356. {
  1357. char rmsg[SGRAB-110];
  1358. if (!buf || !buf[0] || !dcc[idx].simulbot || !dcc[idx].simulbot[0] || idx < 0) {
  1359. debug0("returning from bounce_simul");
  1360. return;
  1361. }
  1362. snprintf(rmsg, sizeof rmsg, "r-sr %d %s", idx, buf); /* remote-simul[r]eturn idx buf */
  1363. putbot(dcc[idx].simulbot, rmsg);
  1364. }
  1365. void send_remote_simul(int idx, char *bot, char *cmd, char *par)
  1366. {
  1367. char msg[SGRAB-110];
  1368. 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);
  1369. putbot(bot, msg);
  1370. }
  1371. /* idx nick conmask cmd par */
  1372. static void bot_rsim(char *botnick, char *code, char *par)
  1373. {
  1374. int ridx = -1, idx = -1, i = 0, rconmask;
  1375. char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX];
  1376. ridx = atoi(newsplit(&par));
  1377. nick = newsplit(&par);
  1378. rconmask = atoi(newsplit(&par));
  1379. rconchan = newsplit(&par);
  1380. cmd = newsplit(&par);
  1381. if (ridx < 0 || !nick || !cmd)
  1382. return;
  1383. for (i = 0; i < dcc_total; i++) {
  1384. if (dcc[i].simul == ridx) {
  1385. putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
  1386. dcc[i].simultime = now;
  1387. idx = i;
  1388. break;
  1389. }
  1390. }
  1391. if (idx < 0) {
  1392. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1393. putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
  1394. /* dcc[idx].sock = 666; */
  1395. dcc[idx].timeval = now;
  1396. dcc[idx].simultime = now;
  1397. dcc[idx].simul = ridx;
  1398. strcpy(dcc[idx].simulbot, botnick);
  1399. dcc[idx].status = STAT_ECHO;
  1400. dcc[idx].u.chat->con_flags = rconmask;
  1401. strcpy(dcc[idx].u.chat->con_chan, rconchan);
  1402. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  1403. strcpy(dcc[idx].nick, nick);
  1404. snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
  1405. strcpy(dcc[idx].host, buf);
  1406. dcc[idx].addr = 0;
  1407. dcc[idx].user = get_user_by_handle(userlist, nick);
  1408. }
  1409. check_tcl_dcc(cmd, idx, par);
  1410. }
  1411. #ifdef HUB
  1412. static void bot_rsimr(char *botnick, char *code, char *par)
  1413. {
  1414. int idx;
  1415. idx = atoi(newsplit(&par));
  1416. if (!par[0])
  1417. return;
  1418. dprintf(idx, "[%s] %s\n", botnick, par);
  1419. }
  1420. #endif /* HUB */
  1421. static cmd_t my_bot[] =
  1422. {
  1423. {"hl", "", (Function) bot_hublog, NULL},
  1424. {"mt", "", (Function) bot_mtcl, NULL},
  1425. {"r_mt", "", (Function) bot_rmtcl, NULL},
  1426. #ifdef HUB /* This will only allow hubs to read the return text */
  1427. {"r-sr", "", (Function) bot_rsimr, NULL},
  1428. #endif /* HUB */
  1429. {"r-s", "", (Function) bot_rsim, NULL},
  1430. {NULL, NULL, NULL, NULL}
  1431. };
  1432. void init_botcmd()
  1433. {
  1434. add_builtins(H_bot, my_bot);
  1435. }