botcmd.c 36 KB

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