botcmd.c 36 KB

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