botcmd.c 35 KB

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