botcmd.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 "src/mod/share.mod/share.h"
  10. #include "src/mod/update.mod/update.h"
  11. #include "net.h"
  12. #include "tclhash.h"
  13. #include "misc.h"
  14. #include "dcc.h"
  15. #include "userrec.h"
  16. #include "cfg.h"
  17. #include "dccutil.h"
  18. #include "cmds.h"
  19. #include "chanprog.h"
  20. #include "botmsg.h"
  21. #include "botnet.h"
  22. #include "tandem.h"
  23. #include "users.h"
  24. #include "chan.h"
  25. #include "modules.h"
  26. #include "core_binds.h"
  27. extern char ver[], admin[], network[], motdfile[];
  28. extern int dcc_total, remote_boots, noshare, timesync, conmask;
  29. extern struct dcc_t *dcc;
  30. extern struct chanset_t *chanset;
  31. extern struct userrec *userlist;
  32. extern time_t now, online_since;
  33. extern party_t *party;
  34. extern module_entry *module_list;
  35. static char TBUF[1024] = ""; /* Static buffer for goofy bot stuff */
  36. static char base64to[256] =
  37. {
  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, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0,
  41. 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  42. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 62, 0, 63, 0, 0, 0, 26, 27, 28,
  43. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  44. 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  47. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  48. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  49. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  51. };
  52. int base64_to_int(char *buf)
  53. {
  54. int i = 0;
  55. while (*buf) {
  56. i = i << 6;
  57. i += base64to[(int) *buf];
  58. buf++;
  59. }
  60. return i;
  61. }
  62. /* Used for 1.0 compatibility: if a join message arrives with no sock#,
  63. * i'll just grab the next "fakesock" # (incrementing to assure uniqueness)
  64. */
  65. static int fakesock = 2300;
  66. static void fake_alert(int idx, char *item, char *extra)
  67. {
  68. static unsigned long lastfake; /* The last time fake_alert was used */
  69. if (now - lastfake > 10) {
  70. /* Don't fake_alert more than once every 10secs */
  71. dprintf(idx, "ct %s NOTICE: %s (%s != %s).\n",
  72. conf.bot->nick, NET_FAKEREJECT, item, extra);
  73. putlog(LOG_BOTS, "*", "%s %s (%s != %s).", dcc[idx].nick, NET_FAKEREJECT,
  74. item, extra);
  75. lastfake = now;
  76. }
  77. }
  78. /* chan <from> <chan> <text>
  79. */
  80. static void bot_chan2(int idx, char *msg)
  81. {
  82. char *from = NULL, *p = NULL;
  83. int i, chan;
  84. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  85. return;
  86. from = newsplit(&msg);
  87. p = newsplit(&msg);
  88. chan = base64_to_int(p);
  89. /* Strip annoying control chars */
  90. for (p = from; *p;) {
  91. if ((*p < 32) || (*p == 127))
  92. /* FIXME: overlap strcpy(p, p + 1); */
  93. sprintf(p, "%s", p + 1);
  94. else
  95. p++;
  96. }
  97. p = strchr(from, '@');
  98. if (p) {
  99. sprintf(TBUF, "<%s> %s", from, msg);
  100. *p = 0;
  101. if (!partyidle(p + 1, from)) {
  102. *p = '@';
  103. fake_alert(idx, "user", from);
  104. return;
  105. }
  106. *p = '@';
  107. p++;
  108. } else {
  109. sprintf(TBUF, "*** (%s) %s", from, msg);
  110. p = from;
  111. }
  112. i = nextbot(p);
  113. if (i != idx) {
  114. fake_alert(idx, "direction", p);
  115. } else {
  116. chanout_but(-1, chan, "%s\n", TBUF);
  117. /* Send to new version bots */
  118. if (i >= 0)
  119. botnet_send_chan(idx, from, NULL, chan, msg);
  120. if (strchr(from, '@') != NULL)
  121. check_bind_chat(from, chan, msg);
  122. else
  123. check_bind_bcst(from, chan, msg);
  124. }
  125. }
  126. #ifdef S_DCCPASS
  127. void bot_cmdpass(int idx, char *par)
  128. {
  129. char *p = NULL;
  130. p = strchr(par, ' ');
  131. if (p) {
  132. *p++ = 0;
  133. botnet_send_cmdpass(idx, par, p);
  134. p--;
  135. *p = ' ';
  136. } else {
  137. botnet_send_cmdpass(idx, par, "");
  138. }
  139. set_cmd_pass(par, 0);
  140. }
  141. #endif /* S_DCCPASS */
  142. void bot_config(int idx, char *par)
  143. {
  144. got_config_share(idx, par);
  145. }
  146. void bot_remotecmd(int idx, char *par) {
  147. char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
  148. if (par[0])
  149. tbot = newsplit(&par);
  150. if (par[0])
  151. fbot = newsplit(&par);
  152. if (par[0])
  153. fhnd = newsplit(&par);
  154. if (par[0])
  155. fidx = newsplit(&par);
  156. if (!strcmp(tbot, conf.bot->nick)) {
  157. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  158. } else if (!strcmp(tbot, "*")) {
  159. botnet_send_cmd_broad(idx, fbot, fhnd, atoi(fidx), par);
  160. gotremotecmd(tbot, fbot, fhnd, fidx, par);
  161. } else {
  162. if (nextbot(tbot) != idx)
  163. botnet_send_cmd(fbot, tbot, fhnd, atoi(fidx), par);
  164. }
  165. }
  166. void bot_remotereply(int idx, char *par) {
  167. char *tbot = NULL, *fbot = NULL, *fhnd = NULL, *fidx = NULL;
  168. if (par[0])
  169. tbot = newsplit(&par);
  170. if (par[0])
  171. fbot = newsplit(&par);
  172. if (par[0])
  173. fhnd = newsplit(&par);
  174. if (par[0])
  175. fidx = newsplit(&par);
  176. if (!strcmp(tbot, conf.bot->nick)) {
  177. gotremotereply(fbot, fhnd, fidx, par);
  178. } else {
  179. if (nextbot(tbot)!= idx)
  180. botnet_send_cmdreply(fbot, tbot, fhnd, fidx, par);
  181. }
  182. }
  183. /* chat <from> <notice> -- only from bots
  184. */
  185. static void bot_chat(int idx, char *par)
  186. {
  187. char *from = NULL;
  188. int i;
  189. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  190. return;
  191. from = newsplit(&par);
  192. if (strchr(from, '@') != NULL) {
  193. fake_alert(idx, "bot", from);
  194. return;
  195. }
  196. /* Make sure the bot is valid */
  197. i = nextbot(from);
  198. if (i != idx) {
  199. fake_alert(idx, "direction", from);
  200. return;
  201. }
  202. chatout("*** (%s) %s\n", from, par);
  203. botnet_send_chat(idx, from, par);
  204. }
  205. /* actchan <from> <chan> <text>
  206. */
  207. static void bot_actchan(int idx, char *par)
  208. {
  209. char *from = NULL, *p = NULL;
  210. int i, chan;
  211. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  212. return;
  213. from = newsplit(&par);
  214. p = strchr(from, '@');
  215. if (p == NULL) {
  216. /* How can a bot do an action? */
  217. fake_alert(idx, "user@bot", from);
  218. return;
  219. }
  220. *p = 0;
  221. if (!partyidle(p + 1, from)) {
  222. *p = '@';
  223. fake_alert(idx, "user", from);
  224. return;
  225. }
  226. *p = '@';
  227. p++;
  228. i = nextbot(p);
  229. if (i != idx) {
  230. fake_alert(idx, "direction", p);
  231. return;
  232. }
  233. p = newsplit(&par);
  234. chan = base64_to_int(p);
  235. for (p = from; *p;) {
  236. if ((*p < 32) || (*p == 127))
  237. sprintf(p, "%s", p + 1);
  238. /* FIXME: overlap strcpy(p, p + 1); */
  239. else
  240. p++;
  241. }
  242. chanout_but(-1, chan, "* %s %s\n", from, par);
  243. botnet_send_act(idx, from, NULL, chan, par);
  244. check_bind_act(from, chan, par);
  245. }
  246. /* priv <from> <to> <message>
  247. */
  248. static void bot_priv(int idx, char *par)
  249. {
  250. char *from = NULL, *p = NULL, *to = TBUF, *tobot = NULL;
  251. int i;
  252. from = newsplit(&par);
  253. tobot = newsplit(&par);
  254. splitc(to, tobot, '@');
  255. p = strchr(from, '@');
  256. if (p != NULL)
  257. p++;
  258. else
  259. p = from;
  260. i = nextbot(p);
  261. if (i != idx) {
  262. fake_alert(idx, "direction", p);
  263. return;
  264. }
  265. if (!to[0])
  266. return; /* Silently ignore notes to '@bot' this
  267. * is legacy code */
  268. if (!egg_strcasecmp(tobot, conf.bot->nick)) { /* For me! */
  269. if (p == from)
  270. add_note(to, from, par, -2, 0);
  271. else {
  272. i = add_note(to, from, par, -1, 0);
  273. if (from[0] != '@')
  274. switch (i) {
  275. case NOTE_ERROR:
  276. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s.", BOT_NOSUCHUSER, to);
  277. break;
  278. case NOTE_STORED:
  279. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s", BOT_NOTESTORED2);
  280. break;
  281. case NOTE_FULL:
  282. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s", BOT_NOTEBOXFULL);
  283. break;
  284. case NOTE_AWAY:
  285. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s", to, BOT_NOTEISAWAY);
  286. break;
  287. case NOTE_FWD:
  288. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s", "Not online; note forwarded to:", to);
  289. break;
  290. case NOTE_TCL:
  291. break; /* Do nothing */
  292. case NOTE_OK:
  293. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s.", BOT_NOTESENTTO, to);
  294. break;
  295. }
  296. }
  297. } else { /* Pass it on */
  298. i = nextbot(tobot);
  299. if (i >= 0)
  300. botnet_send_priv(i, from, to, tobot, "%s", par);
  301. }
  302. }
  303. static void bot_bye(int idx, char *par)
  304. {
  305. char s[1024] = "";
  306. int users, bots;
  307. bots = bots_in_subtree(findbot(dcc[idx].nick));
  308. users = users_in_subtree(findbot(dcc[idx].nick));
  309. simple_sprintf(s, "%s %s. %s (lost %d bot%s and %d user%s)",
  310. BOT_DISCONNECTED, dcc[idx].nick, par[0] ?
  311. par : "No reason", bots, (bots != 1) ?
  312. "s" : "", users, (users != 1) ? "s" : "");
  313. putlog(LOG_BOTS, "*", "%s", s);
  314. chatout("*** %s\n", s);
  315. botnet_send_unlinked(idx, dcc[idx].nick, s);
  316. dprintf(idx, "*bye\n");
  317. killsock(dcc[idx].sock);
  318. lostdcc(idx);
  319. }
  320. static void remote_tell_who(int idx, char *nick, int chan)
  321. {
  322. int i = 10, k, l, ok = 0;
  323. char s[1024] = "", *realnick = NULL;
  324. struct chanset_t *c = NULL;
  325. realnick = strchr(nick, ':');
  326. if (realnick)
  327. realnick++;
  328. else
  329. realnick = nick;
  330. putlog(LOG_BOTS, "*", "#%s# who", realnick);
  331. strcpy(s, "Channels: ");
  332. for (c = chanset; c; c = c->next)
  333. if (!channel_secret(c) && shouldjoin(c)) {
  334. l = strlen(c->dname);
  335. if (i + l < 1021) {
  336. if (i > 10) {
  337. sprintf(s, "%s, %s", s, c->dname);
  338. } else {
  339. strcpy(s, c->dname);
  340. i += (l + 2);
  341. }
  342. }
  343. }
  344. if (i > 10) {
  345. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", s, ver);
  346. } else {
  347. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s (%s)", BOT_NOCHANNELS, ver);
  348. }
  349. if (admin[0])
  350. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "Admin: %s", admin);
  351. if (chan == 0) {
  352. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  353. "%s (* = %s, + = %s, @ = %s)",
  354. BOT_PARTYMEMBS, MISC_OWNER, MISC_MASTER, MISC_OP);
  355. } else {
  356. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  357. "%s %s%d: (* = %s, + = %s, @ = %s)\n",
  358. BOT_PEOPLEONCHAN,
  359. (chan < GLOBAL_CHANS) ? "" : "*",
  360. chan % GLOBAL_CHANS,
  361. MISC_OWNER, MISC_MASTER, MISC_OP);
  362. }
  363. for (i = 0; i < dcc_total; i++) {
  364. if (dcc[i].type->flags & DCT_REMOTEWHO)
  365. if (dcc[i].u.chat->channel == chan) {
  366. k = sprintf(s, " %c%-15s %s",
  367. (geticon(i) == '-' ? ' ' : geticon(i)),
  368. dcc[i].nick, dcc[i].host);
  369. if (now - dcc[i].timeval > 300) {
  370. unsigned long days, hrs, mins;
  371. days = (now - dcc[i].timeval) / 86400;
  372. hrs = ((now - dcc[i].timeval) - (days * 86400)) / 3600;
  373. mins = ((now - dcc[i].timeval) - (hrs * 3600)) / 60;
  374. if (days > 0)
  375. sprintf(s + k, " (%s %lud%luh)",
  376. MISC_IDLE, days, hrs);
  377. else if (hrs > 0)
  378. sprintf(s + k, " (%s %luh%lum)",
  379. MISC_IDLE, hrs, mins);
  380. else
  381. sprintf(s + k, " (%s %lum)",
  382. MISC_IDLE, mins);
  383. }
  384. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  385. if (dcc[i].u.chat->away != NULL)
  386. botnet_send_priv(idx, conf.bot->nick, nick, NULL, " %s: %s",
  387. MISC_AWAY, dcc[i].u.chat->away);
  388. }
  389. }
  390. for (i = 0; i < dcc_total; i++) {
  391. if (dcc[i].type == &DCC_BOT) {
  392. if (!ok) {
  393. ok = 1;
  394. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  395. "%s:", BOT_BOTSCONNECTED);
  396. }
  397. sprintf(s, " %s%c%-15s %s",
  398. dcc[i].status & STAT_CALLED ? "<-" : "->",
  399. dcc[i].status & STAT_SHARE ? '+' : ' ',
  400. dcc[i].nick, dcc[i].u.bot->version);
  401. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  402. }
  403. }
  404. ok = 0;
  405. for (i = 0; i < dcc_total; i++) {
  406. if (dcc[i].type->flags & DCT_REMOTEWHO) {
  407. if (dcc[i].u.chat->channel != chan) {
  408. if (!ok) {
  409. ok = 1;
  410. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s:",
  411. BOT_OTHERPEOPLE);
  412. }
  413. l = sprintf(s, " %c%-15s %s", (geticon(i) == '-' ? ' ' : geticon(i)),
  414. dcc[i].nick, dcc[i].host);
  415. if (now - dcc[i].timeval > 300) {
  416. k = (now - dcc[i].timeval) / 60;
  417. if (k < 60)
  418. sprintf(s + l, " (%s %dm)", MISC_IDLE, k);
  419. else
  420. sprintf(s + l, " (%s %dh%dm)", MISC_IDLE, k / 60, k % 60);
  421. }
  422. botnet_send_priv(idx, conf.bot->nick, nick, NULL, "%s", s);
  423. if (dcc[i].u.chat->away != NULL)
  424. botnet_send_priv(idx, conf.bot->nick, nick, NULL,
  425. " %s: %s", MISC_AWAY,
  426. dcc[i].u.chat->away);
  427. }
  428. }
  429. }
  430. }
  431. static void bot_sysname(int idx, char *par)
  432. {
  433. putlog(LOG_DEBUG, "@", "%s is using %s", dcc[idx].nick, par);
  434. dcc[idx].u.bot->sysname[0] = 0;
  435. strcpy(dcc[idx].u.bot->sysname, par);
  436. }
  437. /* who <from@bot> <tobot> <chan#>
  438. */
  439. static void bot_who(int idx, char *par)
  440. {
  441. char *from = NULL, *to = NULL, *p = NULL;
  442. int i, chan;
  443. from = newsplit(&par);
  444. p = strchr(from, '@');
  445. if (!p) {
  446. sprintf(TBUF, "%s@%s", from, dcc[idx].nick);
  447. from = TBUF;
  448. }
  449. to = newsplit(&par);
  450. if (!egg_strcasecmp(to, conf.bot->nick))
  451. to[0] = 0; /* (for me) */
  452. chan = base64_to_int(par);
  453. if (to[0]) { /* Pass it on */
  454. i = nextbot(to);
  455. if (i >= 0)
  456. botnet_send_who(i, from, to, chan);
  457. } else {
  458. remote_tell_who(idx, from, chan);
  459. }
  460. }
  461. static void bot_endlink(int idx, char *par)
  462. {
  463. dcc[idx].status &= ~STAT_LINKING;
  464. }
  465. /* info? <from@bot> -> send priv
  466. */
  467. static void bot_infoq(int idx, char *par)
  468. {
  469. char s[200] = "", s2[32] = "", *realnick = NULL;
  470. struct chanset_t *chan = 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. if (module_find("server", 0, 0)) {
  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)
  500. + strlen(conf.bot->nick) + strlen(ver) + 1) >= 200) {
  501. strcat(s,"++ ");
  502. break; /* Yegads..! */
  503. }
  504. strcat(s, chan->dname);
  505. strcat(s, ", ");
  506. }
  507. }
  508. if (s[0]) {
  509. s[strlen(s) - 2] = 0;
  510. botnet_send_priv(idx, conf.bot->nick, par, NULL,
  511. "%s <%s> (%s) [UP %s]", ver, network, s, s2);
  512. } else
  513. botnet_send_priv(idx, conf.bot->nick, par, NULL,
  514. "%s <%s> (%s) [UP %s]", ver, network, BOT_NOCHANNELS,
  515. s2);
  516. } else
  517. botnet_send_priv(idx, conf.bot->nick, par, NULL,
  518. "%s <NO_IRC> [UP %s]", ver, s2);
  519. botnet_send_infoq(idx, par);
  520. }
  521. static void bot_ping(int idx, char *par)
  522. {
  523. botnet_send_pong(idx);
  524. }
  525. static void bot_pong(int idx, char *par)
  526. {
  527. dcc[idx].status &= ~STAT_PINGED;
  528. if (dcc[idx].pingtime > (now - 120))
  529. dcc[idx].pingtime -= now;
  530. else
  531. dcc[idx].pingtime = 120;
  532. }
  533. /* link <from@bot> <who> <to-whom>
  534. */
  535. static void bot_link(int idx, char *par)
  536. {
  537. char *from = NULL, *bot = NULL, *rfrom = NULL;
  538. int i;
  539. from = newsplit(&par);
  540. bot = newsplit(&par);
  541. if (!egg_strcasecmp(bot, conf.bot->nick)) {
  542. if ((rfrom = strchr(from, ':')))
  543. rfrom++;
  544. else
  545. rfrom = from;
  546. putlog(LOG_CMDS, "*", "#%s# link %s", rfrom, par);
  547. if (botlink(from, -1, par))
  548. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s ...",
  549. BOT_LINKATTEMPT, par);
  550. else
  551. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s.",
  552. BOT_CANTLINKTHERE);
  553. } else {
  554. i = nextbot(bot);
  555. if (i >= 0)
  556. botnet_send_link(i, from, bot, par);
  557. }
  558. }
  559. /* unlink <from@bot> <linking-bot> <undesired-bot> <reason>
  560. */
  561. static void bot_unlink(int idx, char *par)
  562. {
  563. char *from = NULL, *bot = NULL, *rfrom = NULL, *p = NULL, *undes = NULL;
  564. int i;
  565. from = newsplit(&par);
  566. bot = newsplit(&par);
  567. undes = newsplit(&par);
  568. if (!egg_strcasecmp(bot, conf.bot->nick)) {
  569. if ((rfrom = strchr(from, ':')))
  570. rfrom++;
  571. else
  572. rfrom = from;
  573. putlog(LOG_CMDS, "*", "#%s# unlink %s (%s)", rfrom, undes, par[0] ? par :
  574. "No reason");
  575. i = botunlink(-3, undes, par[0] ? par : NULL);
  576. if (i == 1) {
  577. p = strchr(from, '@');
  578. if (p) {
  579. /* idx will change after unlink -- get new idx
  580. *
  581. * TODO: This has changed with the new lostdcc() behaviour. Check
  582. * if we can optimise the situation.
  583. */
  584. i = nextbot(p + 1);
  585. if (i >= 0)
  586. botnet_send_priv(i, conf.bot->nick, from, NULL,
  587. "Unlinked from %s.", undes);
  588. }
  589. } else if (i == 0) {
  590. botnet_send_unlinked(-1, undes, "");
  591. p = strchr(from, '@');
  592. if (p) {
  593. /* Ditto above, about idx */
  594. i = nextbot(p + 1);
  595. if (i >= 0)
  596. botnet_send_priv(i, conf.bot->nick, from, NULL,
  597. "%s %s.", BOT_CANTUNLINK, undes);
  598. }
  599. } else {
  600. p = strchr(from, '@');
  601. if (p) {
  602. i = nextbot(p + 1);
  603. if (i >= 0)
  604. botnet_send_priv(i, conf.bot->nick, from, NULL,
  605. "Can't remotely unlink sharebots.");
  606. }
  607. }
  608. } else {
  609. i = nextbot(bot);
  610. if (i >= 0)
  611. botnet_send_unlink(i, from, bot, undes, par);
  612. }
  613. }
  614. /* Bot next share?
  615. */
  616. static void bot_update(int idx, char *par)
  617. {
  618. char *bot = NULL, x;
  619. int vnum;
  620. bot = newsplit(&par);
  621. x = par[0];
  622. if (x)
  623. par++;
  624. vnum = base64_to_int(par);
  625. if (in_chain(bot))
  626. updatebot(idx, bot, x, vnum);
  627. }
  628. /* Newbot next share?
  629. */
  630. static void bot_nlinked(int idx, char *par)
  631. {
  632. char *newbot = NULL, *next = NULL, *p = NULL, s[1024] = "", x = 0;
  633. struct userrec *u = NULL;
  634. int bogus = 0, i;
  635. newbot = newsplit(&par);
  636. next = newsplit(&par);
  637. s[0] = 0;
  638. if (!next[0]) {
  639. putlog(LOG_BOTS, "*", "Invalid eggnet protocol from %s (zapfing)",
  640. dcc[idx].nick);
  641. simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick,
  642. MISC_INVALIDBOT);
  643. dprintf(idx, "error invalid eggnet protocol for 'nlinked'\n");
  644. } else if ((in_chain(newbot)) || (!egg_strcasecmp(newbot, conf.bot->nick))) {
  645. /* Loop! */
  646. putlog(LOG_BOTS, "*", "%s %s (mutual: %s)",
  647. BOT_LOOPDETECT, dcc[idx].nick, newbot);
  648. simple_sprintf(s, "%s %s: disconnecting %s", MISC_LOOP, newbot,
  649. dcc[idx].nick);
  650. dprintf(idx, "error Loop (%s)\n", newbot);
  651. }
  652. if (!s[0]) {
  653. for (p = newbot; *p; p++)
  654. if ((*p < 32) || (*p == 127) || ((p - newbot) >= HANDLEN))
  655. bogus = 1;
  656. i = nextbot(next);
  657. if (i != idx)
  658. bogus = 1;
  659. }
  660. if (bogus) {
  661. putlog(LOG_BOTS, "*", "%s %s! (%s -> %s)", BOT_BOGUSLINK, dcc[idx].nick,
  662. next, newbot);
  663. simple_sprintf(s, "%s: %s %s", BOT_BOGUSLINK, dcc[idx].nick,
  664. MISC_DISCONNECTED);
  665. dprintf(idx, "error %s (%s -> %s)\n",
  666. BOT_BOGUSLINK, next, newbot);
  667. }
  668. if (s[0]) {
  669. chatout("*** %s\n", s);
  670. botnet_send_unlinked(idx, dcc[idx].nick, s);
  671. dprintf(idx, "bye %s\n", BOT_ILLEGALLINK);
  672. killsock(dcc[idx].sock);
  673. lostdcc(idx);
  674. return;
  675. }
  676. x = par[0];
  677. if (x)
  678. par++;
  679. else
  680. x = '-';
  681. i = base64_to_int(par);
  682. botnet_send_nlinked(idx, newbot, next, x, i);
  683. if (x == '!') {
  684. #ifdef HUB
  685. chatout("*** (%s) %s %s.\n", next, NET_LINKEDTO, newbot);
  686. #else
  687. chatout("*** %s linked to botnet.\n", newbot);
  688. #endif
  689. x = '-';
  690. }
  691. addbot(newbot, dcc[idx].nick, next, x, i);
  692. check_bind_link(newbot, next);
  693. u = get_user_by_handle(userlist, newbot);
  694. if (bot_flags(u) & BOT_REJECT) {
  695. botnet_send_reject(idx, conf.bot->nick, NULL, newbot, NULL, NULL);
  696. putlog(LOG_BOTS, "*", "%s %s %s %s", BOT_REJECTING,
  697. newbot, MISC_FROM, dcc[idx].nick);
  698. }
  699. }
  700. static void bot_unlinked(int idx, char *par)
  701. {
  702. int i;
  703. char *bot = NULL;
  704. bot = newsplit(&par);
  705. i = nextbot(bot);
  706. if ((i >= 0) && (i != idx)) /* Bot is NOT downstream along idx, so
  707. * BOGUS! */
  708. fake_alert(idx, "direction", bot);
  709. else if (i >= 0) { /* Valid bot downstream of idx */
  710. if (par[0])
  711. /* #ifdef HUB */
  712. chatout("*** (%s) %s\n", lastbot(bot), par);
  713. /*
  714. #else
  715. chatout("*** %s unlinked from botnet.\n", par);
  716. #endif
  717. */
  718. botnet_send_unlinked(idx, bot, par);
  719. unvia(idx, findbot(bot));
  720. rembot(bot);
  721. }
  722. /* Otherwise it's not even a valid bot, so just ignore! */
  723. }
  724. /* trace <from@bot> <dest> <chain:chain..>
  725. */
  726. static void bot_trace(int idx, char *par)
  727. {
  728. char *from = NULL, *dest = NULL;
  729. int i;
  730. from = newsplit(&par);
  731. dest = newsplit(&par);
  732. simple_sprintf(TBUF, "%s:%s", par, conf.bot->nick);
  733. botnet_send_traced(idx, from, TBUF);
  734. if (egg_strcasecmp(dest, conf.bot->nick) && ((i = nextbot(dest)) > 0))
  735. botnet_send_trace(i, from, dest, par);
  736. }
  737. /* traced <to@bot> <chain:chain..>
  738. */
  739. static void bot_traced(int idx, char *par)
  740. {
  741. char *to = NULL, *p = NULL;
  742. int i, sock;
  743. to = newsplit(&par);
  744. p = strchr(to, '@');
  745. if (p == NULL)
  746. p = to;
  747. else {
  748. *p = 0;
  749. p++;
  750. }
  751. if (!egg_strcasecmp(p, conf.bot->nick)) {
  752. time_t t = 0;
  753. char *p = par, *ss = TBUF;
  754. splitc(ss, to, ':');
  755. if (ss[0])
  756. sock = atoi(ss);
  757. else
  758. sock = (-1);
  759. if (par[0] == ':') {
  760. t = atoi(par + 1);
  761. p = strchr(par + 1, ':');
  762. if (p)
  763. p++;
  764. else
  765. p = par + 1;
  766. }
  767. for (i = 0; i < dcc_total; i++)
  768. if ((dcc[i].type->flags & DCT_CHAT) &&
  769. (!egg_strcasecmp(dcc[i].nick, to)) &&
  770. ((sock == (-1)) || (sock == dcc[i].sock))) {
  771. if (t) {
  772. int j=0;
  773. {
  774. register char *c=p;
  775. for (; *c != '\0'; c++) if (*c == ':') j++;
  776. }
  777. dprintf(i, "%s -> %s (%lu secs, %d hop%s)\n", BOT_TRACERESULT, p,
  778. now - t, j, (j != 1) ? "s" : "");
  779. } else
  780. dprintf(i, "%s -> %s\n", BOT_TRACERESULT, p);
  781. }
  782. } else {
  783. i = nextbot(p);
  784. if (p != to)
  785. *--p = '@';
  786. if (i >= 0)
  787. botnet_send_traced(i, to, par);
  788. }
  789. }
  790. static void bot_buildts(int idx, char *par)
  791. {
  792. if (par && par[0])
  793. dcc[idx].u.bot->bts = atoi(par);
  794. }
  795. static void bot_timesync(int idx, char *par)
  796. {
  797. putlog(LOG_DEBUG, "@", "Got timesync from %s: %s\n", dcc[idx].nick, par);
  798. timesync = atoi(par) - now;
  799. #ifdef HUB
  800. send_timesync(-1);
  801. #endif /* HUB */
  802. }
  803. /* reject <from> <bot>
  804. */
  805. static void bot_reject(int idx, char *par)
  806. {
  807. char *from = NULL, *who = NULL, *destbot = NULL, *frombot = NULL;
  808. struct userrec *u = NULL;
  809. int i;
  810. if (bot_flags(dcc[idx].user) & BOT_ISOLATE)
  811. return;
  812. from = newsplit(&par);
  813. frombot = strchr(from, '@');
  814. if (frombot)
  815. frombot++;
  816. else
  817. frombot = from;
  818. i = nextbot(frombot);
  819. if (i != idx) {
  820. fake_alert(idx, "direction", frombot);
  821. return;
  822. }
  823. who = newsplit(&par);
  824. if (!(destbot = strchr(who, '@'))) {
  825. /* Rejecting a bot */
  826. i = nextbot(who);
  827. if (i < 0) {
  828. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s %s (%s)",
  829. BOT_CANTUNLINK, who, BOT_DOESNTEXIST);
  830. } else if (!egg_strcasecmp(dcc[i].nick, who)) {
  831. char s[1024] = "";
  832. /* I'm the connection to the rejected bot */
  833. putlog(LOG_BOTS, "*", "%s %s %s", from, MISC_REJECTED, dcc[i].nick);
  834. dprintf(i, "bye %s\n", par[0] ? par : MISC_REJECTED);
  835. simple_sprintf(s, "%s %s (%s: %s)",
  836. MISC_DISCONNECTED, dcc[i].nick, from,
  837. par[0] ? par : MISC_REJECTED);
  838. chatout("*** %s\n", s);
  839. botnet_send_unlinked(i, dcc[i].nick, s);
  840. killsock(dcc[i].sock);
  841. lostdcc(i);
  842. } else {
  843. if (i >= 0)
  844. botnet_send_reject(i, from, NULL, who, NULL, par);
  845. }
  846. } else { /* Rejecting user */
  847. *destbot++ = 0;
  848. if (!egg_strcasecmp(destbot, conf.bot->nick)) {
  849. /* Kick someone here! */
  850. int ok = 0;
  851. if (remote_boots == 1) {
  852. frombot = strchr(from, '@');
  853. if (frombot == NULL)
  854. frombot = from;
  855. else
  856. frombot++;
  857. u = get_user_by_handle(userlist, frombot);
  858. if (!(bot_flags(u) & BOT_SHARE)) {
  859. add_note(from, conf.bot->nick, "No non sharebot boots.", -1, 0);
  860. ok = 1;
  861. }
  862. } else if (remote_boots == 0) {
  863. botnet_send_priv(idx, conf.bot->nick, from, NULL, "%s",
  864. BOT_NOREMOTEBOOT);
  865. ok = 1;
  866. }
  867. for (i = 0; (i < dcc_total) && (!ok); i++)
  868. if ((!egg_strcasecmp(who, dcc[i].nick)) &&
  869. (dcc[i].type->flags & DCT_CHAT)) {
  870. u = get_user_by_handle(userlist, dcc[i].nick);
  871. if (u &&
  872. ((u->flags & USER_OWNER) && !(dcc[idx].user->flags & USER_ADMIN))) {
  873. add_note(from, conf.bot->nick, BOT_NOOWNERBOOT, -1, 0);
  874. return;
  875. }
  876. do_boot(i, from, par);
  877. ok = 1;
  878. putlog(LOG_CMDS, "*", "#%s# boot %s (%s)", from, who,
  879. par[0] ? par : "No reason");
  880. }
  881. } else {
  882. i = nextbot(destbot);
  883. *--destbot = '@';
  884. if (i >= 0)
  885. botnet_send_reject(i, from, NULL, who, NULL, par);
  886. }
  887. }
  888. }
  889. static void bot_thisbot(int idx, char *par)
  890. {
  891. if (egg_strcasecmp(par, dcc[idx].nick)) {
  892. char s[1024] = "";
  893. putlog(LOG_BOTS, "*", NET_WRONGBOT, dcc[idx].nick, par);
  894. dprintf(idx, "bye %s\n", MISC_IMPOSTER);
  895. simple_sprintf(s, "%s %s (%s)", MISC_DISCONNECTED, dcc[idx].nick,
  896. MISC_IMPOSTER);
  897. chatout("*** %s\n", s);
  898. botnet_send_unlinked(idx, dcc[idx].nick, s);
  899. unvia(idx, findbot(dcc[idx].nick));
  900. killsock(dcc[idx].sock);
  901. lostdcc(idx);
  902. return;
  903. }
  904. if (bot_flags(dcc[idx].user) & BOT_LEAF)
  905. dcc[idx].status |= STAT_LEAF;
  906. /* Set capitalization the way they want it */
  907. noshare = 1;
  908. change_handle(dcc[idx].user, par);
  909. noshare = 0;
  910. strcpy(dcc[idx].nick, par);
  911. }
  912. static void bot_hublog(char *botnick, char *code, char *par)
  913. {
  914. #ifdef HUB
  915. int type = atoi(newsplit(&par));
  916. putlog(type, "@", "(%s) %s", botnick, par);
  917. #endif /* HUB */
  918. }
  919. static void bot_handshake(int idx, char *par)
  920. {
  921. struct userrec *u = get_user_by_handle(userlist, dcc[idx].nick);
  922. /* We *don't* want botnet passwords migrating */
  923. noshare = 1;
  924. set_user(&USERENTRY_PASS, u, par);
  925. noshare = 0;
  926. }
  927. /* Used to send a direct msg from Tcl on one bot to Tcl on another
  928. * zapf <frombot> <tobot> <code [param]>
  929. */
  930. static void bot_zapf(int idx, char *par)
  931. {
  932. char *from = NULL, *to = NULL;
  933. int i;
  934. from = newsplit(&par);
  935. to = newsplit(&par);
  936. i = nextbot(from);
  937. if (i != idx) {
  938. fake_alert(idx, "direction", from);
  939. return;
  940. }
  941. if (!egg_strcasecmp(to, conf.bot->nick)) {
  942. /* For me! */
  943. char *opcode;
  944. opcode = newsplit(&par);
  945. check_bind_bot(from, opcode, par);
  946. return;
  947. }
  948. i = nextbot(to);
  949. if (i >= 0)
  950. botnet_send_zapf(i, from, to, par);
  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, sock, 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. module_entry *me = NULL;
  1181. if (nextbot(frombot) != sock)
  1182. fake_alert(sock, "versions-direction", frombot);
  1183. else if (egg_strcasecmp(tobot = newsplit(&par), conf.bot->nick)) {
  1184. if ((sock = nextbot(tobot)) >= 0)
  1185. dprintf(sock, "v %s %s %s\n", frombot, tobot, par);
  1186. } else {
  1187. from = newsplit(&par);
  1188. botnet_send_priv(sock, conf.bot->nick, from, frombot, "Modules loaded:\n");
  1189. for (me = module_list; me; me = me->next)
  1190. botnet_send_priv(sock, conf.bot->nick, from, frombot, " Module: %s\n",
  1191. me->name);
  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. #ifdef S_DCCPASS
  1215. {"cp", (Function) bot_cmdpass},
  1216. #endif
  1217. {"ct", (Function) bot_chat},
  1218. {"e", (Function) bot_error},
  1219. {"el", (Function) bot_endlink},
  1220. {"hs", (Function) bot_handshake},
  1221. {"i", (Function) bot_idle},
  1222. {"i?", (Function) bot_infoq},
  1223. {"j", (Function) bot_join},
  1224. {"l", (Function) bot_link},
  1225. {"n", (Function) bot_nlinked},
  1226. {"nc", (Function) bot_nickchange},
  1227. {"p", (Function) bot_priv},
  1228. {"pi", (Function) bot_ping},
  1229. {"po", (Function) bot_pong},
  1230. {"pt", (Function) bot_part},
  1231. {"r", (Function) bot_reject},
  1232. {"rc", (Function) bot_remotecmd},
  1233. {"rr", (Function) bot_remotereply},
  1234. {"s", (Function) bot_share},
  1235. {"sb", (Function) bot_shareupdate},
  1236. {"t", (Function) bot_trace},
  1237. {"tb", (Function) bot_thisbot},
  1238. {"td", (Function) bot_traced},
  1239. {"ts", (Function) bot_timesync},
  1240. {"u", (Function) bot_update},
  1241. {"ul", (Function) bot_unlink},
  1242. {"un", (Function) bot_unlinked},
  1243. {"v", (Function) bot_versions},
  1244. {"vs", (Function) bot_sysname},
  1245. {"w", (Function) bot_who},
  1246. {"z", (Function) bot_zapf},
  1247. {"zb", (Function) bot_zapfbroad},
  1248. {NULL, NULL}
  1249. };
  1250. void send_remote_simul(int idx, char *bot, char *cmd, char *par)
  1251. {
  1252. char msg[SGRAB - 110] = "";
  1253. egg_snprintf(msg, sizeof msg, "r-s %d %s %d %s %lu %s %s", idx, dcc[idx].nick, dcc[idx].u.chat->con_flags,
  1254. dcc[idx].u.chat->con_chan, dcc[idx].status, cmd, par);
  1255. putbot(bot, msg);
  1256. }
  1257. /* idx nick conmask cmd par */
  1258. static void bot_rsim(char *botnick, char *code, char *par)
  1259. {
  1260. int ridx = -1, idx = -1, i = 0, rconmask;
  1261. unsigned long status = 0;
  1262. char *nick = NULL, *cmd = NULL, *rconchan = NULL, buf[UHOSTMAX] = "";
  1263. ridx = atoi(newsplit(&par));
  1264. nick = newsplit(&par);
  1265. rconmask = atoi(newsplit(&par));
  1266. rconchan = newsplit(&par);
  1267. if (egg_isdigit(par[0]))
  1268. status = (unsigned long) atoi(newsplit(&par));
  1269. cmd = newsplit(&par);
  1270. if (ridx < 0 || !nick || !cmd)
  1271. return;
  1272. for (i = 0; i < dcc_total; i++) {
  1273. if (dcc[i].simul == ridx) {
  1274. putlog(LOG_DEBUG, "*", "Simul found old idx for %s: %d (ridx: %d)", nick, i, ridx);
  1275. dcc[i].simultime = now;
  1276. idx = i;
  1277. break;
  1278. }
  1279. }
  1280. if (idx < 0) {
  1281. idx = new_dcc(&DCC_CHAT, sizeof(struct chat_info));
  1282. putlog(LOG_DEBUG, "*", "Making new idx for %s@%s: %d ridx: %d", nick, botnick, idx, ridx);
  1283. /* dcc[idx].sock = 666; */
  1284. dcc[idx].timeval = now;
  1285. dcc[idx].simultime = now;
  1286. dcc[idx].simul = ridx;
  1287. dcc[idx].status = status;
  1288. strcpy(dcc[idx].simulbot, botnick);
  1289. dcc[idx].u.chat->con_flags = rconmask;
  1290. strcpy(dcc[idx].u.chat->con_chan, rconchan);
  1291. dcc[idx].u.chat->strip_flags = STRIP_ALL;
  1292. strcpy(dcc[idx].nick, nick);
  1293. egg_snprintf(buf, sizeof buf, "%s@%s", nick, botnick);
  1294. strcpy(dcc[idx].host, buf);
  1295. dcc[idx].addr = 0;
  1296. dcc[idx].user = get_user_by_handle(userlist, nick);
  1297. }
  1298. rmspace(par);
  1299. check_bind_dcc(cmd, idx, par);
  1300. }
  1301. void bounce_simul(int idx, char *buf)
  1302. {
  1303. char rmsg[SGRAB - 110] = "";
  1304. if (!buf || !buf[0] || !dcc[idx].simulbot || !dcc[idx].simulbot[0] || idx < 0)
  1305. return;
  1306. egg_snprintf(rmsg, sizeof rmsg, "r-sr %d %s", dcc[idx].simul, buf); /* remote-simul[r]eturn idx buf */
  1307. putbot(dcc[idx].simulbot, rmsg);
  1308. }
  1309. #ifdef HUB
  1310. static void bot_rsimr(char *botnick, char *code, char *par)
  1311. {
  1312. int idx = atoi(newsplit(&par));
  1313. if (!par[0])
  1314. return;
  1315. dprintf(idx, "[%s] %s\n", botnick, par);
  1316. }
  1317. #endif /* HUB */
  1318. static cmd_t my_bot[] =
  1319. {
  1320. {"hl", "", (Function) bot_hublog, NULL},
  1321. #ifdef HUB /* This will only allow hubs to read the return text */
  1322. {"r-sr", "", (Function) bot_rsimr, NULL},
  1323. #endif /* HUB */
  1324. {"r-s", "", (Function) bot_rsim, NULL},
  1325. {NULL, NULL, NULL, NULL}
  1326. };
  1327. void init_botcmd()
  1328. {
  1329. add_builtins("bot", my_bot);
  1330. }