botcmd.c 36 KB

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