botmsg.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * botmsg.c -- handles:
  3. * formatting of messages to be sent on the botnet
  4. * sending differnet messages to different versioned bots
  5. *
  6. * by Darrin Smith (beldin@light.iinet.net.au)
  7. *
  8. */
  9. #include "common.h"
  10. #include "misc.h"
  11. #include "dcc.h"
  12. #include "userrec.h"
  13. #include "main.h"
  14. #include "set.h"
  15. #include "net.h"
  16. #include "users.h"
  17. #include "botmsg.h"
  18. #include "dccutil.h"
  19. #include "cmds.h"
  20. #include "chanprog.h"
  21. #include "botnet.h"
  22. #include "tandem.h"
  23. #include "core_binds.h"
  24. #include <stdarg.h>
  25. static char OBUF[SGRAB - 110] = "";
  26. void send_uplink(const char *msg, size_t len)
  27. {
  28. if (uplink_idx == -1 || !valid_idx(uplink_idx) || dcc[uplink_idx].sock == -1 ||
  29. (dcc[uplink_idx].type != &DCC_BOT) || !dcc[uplink_idx].hub)
  30. return;
  31. tputs(dcc[uplink_idx].sock, (char *) msg, len);
  32. }
  33. void send_hubs(const char *msg, size_t len)
  34. {
  35. send_hubs_but(-1, msg, len);
  36. }
  37. void send_hubs_but(int idx, const char *msg, size_t len)
  38. {
  39. int i = 0;
  40. for (i = 0; i < dcc_total; i++) {
  41. if (dcc[i].type && (dcc[i].type == &DCC_BOT) && i != idx && dcc[i].hub) {
  42. tputs(dcc[i].sock, (char *) msg, len);
  43. }
  44. }
  45. }
  46. /* Ditto for tandem bots
  47. */
  48. static void send_tand_but(int x, char *buf, size_t len)
  49. {
  50. int i = 0;
  51. for (i = 0; i < dcc_total; i++) {
  52. if (dcc[i].type && (dcc[i].type == &DCC_BOT) && i != x) {
  53. tputs(dcc[i].sock, buf, len);
  54. }
  55. }
  56. }
  57. void botnet_send_cmdpass(int idx, char *cmd, char *pass)
  58. {
  59. if (tands > 0) {
  60. char *buf = (char *) my_calloc(1, strlen(cmd) + strlen(pass) + 5 + 1);
  61. simple_sprintf(buf, "cp %s %s\n", cmd, pass);
  62. send_tand_but(idx, buf, strlen(buf));
  63. free(buf);
  64. }
  65. }
  66. int botnet_send_cmd(char * fbot, char * bot, char *fhnd, int fromidx, char * cmd) {
  67. int i = nextbot(bot);
  68. if (i >= 0) {
  69. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "rc %s %s %s %i %s\n", bot, fbot, fhnd, fromidx, cmd);
  70. tputs(dcc[i].sock, OBUF, len);
  71. return 1;
  72. } else if (!egg_strcasecmp(bot, conf.bot->nick)) {
  73. char tmp[24] = "";
  74. simple_sprintf(tmp, "%i", fromidx);
  75. gotremotecmd(conf.bot->nick, conf.bot->nick, fhnd, tmp, cmd);
  76. }
  77. return 0;
  78. }
  79. void botnet_send_cmd_broad(int idx, char * fbot, char *fhnd, int fromidx, char * cmd) {
  80. if (tands > 0) {
  81. simple_snprintf(OBUF, sizeof OBUF, "rc * %s %s %i %s\n", fbot, fhnd, fromidx, cmd);
  82. send_tand_but(idx, OBUF, strlen(OBUF));
  83. }
  84. if (idx < 0) {
  85. char tmp[24] = "";
  86. simple_sprintf(tmp, "%i", fromidx);
  87. gotremotecmd("*", conf.bot->nick, fhnd, tmp, cmd);
  88. }
  89. }
  90. void botnet_send_cmdreply(char * fbot, char * bot, char * to, char * toidx, char * ln) {
  91. int i = nextbot(bot);
  92. if (i >= 0) {
  93. const size_t len = simple_snprintf(OBUF, sizeof OBUF, "rr %s %s %s %s %s\n", bot, fbot, to, toidx, ln);
  94. tputs(dcc[i].sock, OBUF, len);
  95. } else if (!egg_strcasecmp(bot, conf.bot->nick)) {
  96. gotremotereply(conf.bot->nick, to, toidx, ln);
  97. }
  98. }
  99. void botnet_send_bye(const char *reason)
  100. {
  101. if (tands > 0) {
  102. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "bye %s\n", reason ? reason : "No reason");
  103. send_tand_but(-1, OBUF, len);
  104. }
  105. }
  106. void botnet_send_chan(int idx, char *botnick, char *user, int chan, char *data)
  107. {
  108. if ((tands > 0) && (chan < GLOBAL_CHANS)) {
  109. size_t len;
  110. if (user) {
  111. len = simple_snprintf2(OBUF, sizeof(OBUF), "c %s@%s %D %s\n", user, botnick, chan, data);
  112. } else {
  113. len = simple_snprintf2(OBUF, sizeof(OBUF), "c %s %D %s\n", botnick, chan, data);
  114. }
  115. send_tand_but(idx, OBUF, len);
  116. }
  117. }
  118. void botnet_send_act(int idx, char *botnick, char *user, int chan, char *data)
  119. {
  120. if ((tands > 0) && (chan < GLOBAL_CHANS)) {
  121. size_t len;
  122. if (user) {
  123. len = simple_snprintf2(OBUF, sizeof(OBUF), "a %s@%s %D %s\n", user, botnick, chan, data);
  124. } else {
  125. len = simple_snprintf2(OBUF, sizeof(OBUF), "a %s %D %s\n", botnick, chan, data);
  126. }
  127. send_tand_but(idx, OBUF, len);
  128. }
  129. }
  130. void botnet_send_chat(int idx, char *botnick, char *data)
  131. {
  132. if (tands > 0) {
  133. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "ct %s %s\n", botnick, data);
  134. send_hubs_but(idx, OBUF, len);
  135. }
  136. }
  137. void botnet_send_ping(int idx)
  138. {
  139. tputs(dcc[idx].sock, "pi\n", 3);
  140. dcc[idx].pingtime = now;
  141. }
  142. void botnet_send_pong(int idx)
  143. {
  144. tputs(dcc[idx].sock, "po\n", 3);
  145. }
  146. void botnet_send_priv (int idx, char *from, char *to, char *tobot, const char *format, ...)
  147. {
  148. size_t len;
  149. char tbuf[1024] = "";
  150. va_list va;
  151. va_start(va, format);
  152. egg_vsnprintf(tbuf, sizeof(tbuf), format, va);
  153. va_end(va);
  154. if (tobot) {
  155. len = simple_snprintf(OBUF, sizeof(OBUF), "p %s %s@%s %s\n", from, to, tobot, tbuf);
  156. } else {
  157. len = simple_snprintf(OBUF, sizeof(OBUF), "p %s %s %s\n", from, to, tbuf);
  158. }
  159. tputs(dcc[idx].sock, OBUF, len);
  160. }
  161. void botnet_send_who(int idx, char *from, char *to, int chan)
  162. {
  163. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "w %s %s %D\n", from, to, chan);
  164. tputs(dcc[idx].sock, OBUF, len);
  165. }
  166. void botnet_send_unlink(int idx, char *who, char *via, char *bot, char *reason)
  167. {
  168. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "ul %s %s %s %s\n", who, via, bot, reason);
  169. tputs(dcc[idx].sock, OBUF, len);
  170. }
  171. void botnet_send_link(int idx, char *who, char *via, char *bot)
  172. {
  173. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "l %s %s %s\n", who, via, bot);
  174. tputs(dcc[idx].sock, OBUF, len);
  175. }
  176. void botnet_send_unlinked(int idx, char *bot, char *args)
  177. {
  178. if (tands > 0) {
  179. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "un %s %s\n", bot, args ? args : "");
  180. send_tand_but(idx, OBUF, len);
  181. }
  182. }
  183. void botnet_send_nlinked(int idx, char *bot, char *next, char flag, int vlocalhub, time_t vbuildts, char *vversion)
  184. {
  185. if (tands > 0) {
  186. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "n %s %s %cD0gc %d %d %s\n", bot, next, flag,
  187. vlocalhub, (int) vbuildts, vversion ? vversion : "");
  188. send_tand_but(idx, OBUF, len);
  189. }
  190. }
  191. void botnet_send_traced(int idx, char *bot, char *buf)
  192. {
  193. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "td %s %s\n", bot, buf);
  194. tputs(dcc[idx].sock, OBUF, len);
  195. }
  196. void botnet_send_trace(int idx, char *to, char *from, char *buf)
  197. {
  198. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "t %s %s %s:%s\n", to, from, buf, conf.bot->nick);
  199. tputs(dcc[idx].sock, OBUF, len);
  200. }
  201. void botnet_send_update(int idx, tand_t * ptr)
  202. {
  203. if (tands > 0) {
  204. /* the D0gc is a lingering hack which probably will never be able to come out. */
  205. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "u %s %cD0gc %d %d %s\n", ptr->bot, ptr->share, ptr->localhub,
  206. (int) ptr->buildts, ptr->version ? ptr->version : "");
  207. send_tand_but(idx, OBUF, len);
  208. }
  209. }
  210. void botnet_send_reject(int idx, char *fromp, char *frombot, char *top, char *tobot, char *reason)
  211. {
  212. size_t len;
  213. char to[NOTENAMELEN + 1] = "", from[NOTENAMELEN + 1] = "";
  214. if (tobot) {
  215. simple_sprintf(to, "%s@%s", top, tobot);
  216. top = to;
  217. }
  218. if (frombot) {
  219. simple_sprintf(from, "%s@%s", fromp, frombot);
  220. fromp = from;
  221. }
  222. if (!reason)
  223. reason = "";
  224. len = simple_snprintf(OBUF, sizeof(OBUF), "r %s %s %s\n", fromp, top, reason);
  225. tputs(dcc[idx].sock, OBUF, len);
  226. }
  227. void putallbots(const char *par)
  228. {
  229. botnet_send_zapf_broad(-1, conf.bot->nick, NULL, par);
  230. return;
  231. }
  232. void putbot(const char *bot, char *par)
  233. {
  234. if (!bot || !par || !bot[0] || !par[0])
  235. return;
  236. int i = nextbot(bot);
  237. if (i < 0)
  238. return;
  239. botnet_send_zapf(i, conf.bot->nick, bot, par);
  240. }
  241. /*
  242. botnet_send_log(...)
  243. - sends to uplink if a leaf
  244. - sends to all linked hubs if a hub
  245. */
  246. void botnet_send_log(int idx, const char *from, int type, const char *msg)
  247. {
  248. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "lo %s %d %s\n", from, type, msg);
  249. if (conf.bot->hub) {
  250. send_hubs_but(idx, OBUF, len);
  251. } else {
  252. send_uplink(OBUF, len);
  253. }
  254. }
  255. void botnet_send_zapf(int idx, const char *a, const char *b, const char *c)
  256. {
  257. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "z %s %s %s\n", a, b, c);
  258. tputs(dcc[idx].sock, OBUF, len);
  259. }
  260. void botnet_send_zapf_broad(int idx, const char *a, const char *b, const char *c)
  261. {
  262. if (tands > 0) {
  263. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "zb %s %s%s%s\n", a, b ? b : "", b ? " " : "", c);
  264. send_tand_but(idx, OBUF, len);
  265. }
  266. }
  267. void botnet_send_var(int idx, variable_t *var) {
  268. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "va %s %s\n", var->name, var->gdata ? var->gdata : "");
  269. tputs(dcc[idx].sock, OBUF, len);
  270. }
  271. void botnet_send_var_broad(int idx, variable_t *var) {
  272. if (tands > 0) {
  273. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "vab %s %s\n", var->name, var->gdata ? var->gdata : "");
  274. send_tand_but(idx, OBUF, len);
  275. }
  276. }
  277. void botnet_send_idle(int idx, char *bot, int sock, int idle, char *away)
  278. {
  279. if (tands > 0) {
  280. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "i %s %D %Ds%s\n", bot, sock, idle, away ? away : "");
  281. send_tand_but(idx, OBUF, len);
  282. }
  283. }
  284. void botnet_send_away(int idx, char *bot, int sock, char *msg, int linking)
  285. {
  286. if (tands > 0) {
  287. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "aw %s%s %D %s\n", ((idx >= 0) && linking) ? "!" : "", bot, sock, msg ? msg : "");
  288. send_tand_but(idx, OBUF, len);
  289. }
  290. }
  291. void botnet_send_join_idx(int useridx)
  292. {
  293. if (tands > 0) {
  294. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "j %s %s %D %c%D %s\n",
  295. conf.bot->nick, dcc[useridx].nick,
  296. dcc[useridx].type && dcc[useridx].type == &DCC_RELAYING ?
  297. dcc[useridx].u.relay->chat->channel : dcc[useridx].u.chat->channel, geticon(useridx),
  298. dcc[useridx].sock, dcc[useridx].host);
  299. send_tand_but(-1, OBUF, len);
  300. }
  301. }
  302. void botnet_send_join_party(int idx, int linking, int useridx)
  303. {
  304. if (tands > 0) {
  305. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "j %s%s %s %D %c%D %s\n", linking ? "!" : "",
  306. party[useridx].bot, party[useridx].nick,
  307. party[useridx].chan, party[useridx].flag, party[useridx].sock,
  308. party[useridx].from ? party[useridx].from : "");
  309. send_tand_but(idx, OBUF, len);
  310. }
  311. }
  312. void botnet_send_part_idx(int useridx, char *reason)
  313. {
  314. if (tands > 0) {
  315. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "pt %s %s %D %s\n", conf.bot->nick,
  316. dcc[useridx].nick, dcc[useridx].sock, reason ? reason : "");
  317. send_tand_but(-1, OBUF, len);
  318. }
  319. }
  320. void botnet_send_part_party(int idx, int partyidx, char *reason, int silent)
  321. {
  322. if (tands > 0) {
  323. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "pt %s%s %s %D %s\n",
  324. silent ? "!" : "", party[partyidx].bot,
  325. party[partyidx].nick, party[partyidx].sock, reason ? reason : "");
  326. send_tand_but(idx, OBUF, len);
  327. }
  328. }
  329. void botnet_send_nkch(int useridx, char *oldnick)
  330. {
  331. if (tands > 0) {
  332. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "nc %s %D %s\n", conf.bot->nick, dcc[useridx].sock, dcc[useridx].nick);
  333. send_tand_but(-1, OBUF, len);
  334. }
  335. }
  336. void botnet_send_nkch_part(int butidx, int useridx, char *oldnick)
  337. {
  338. if (tands > 0) {
  339. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "nc %s %D %s\n", party[useridx].bot, party[useridx].sock, party[useridx].nick);
  340. send_tand_but(butidx, OBUF, len);
  341. }
  342. }
  343. /* This part of add_note is more relevant to the botnet than
  344. * to the notes file
  345. */
  346. int add_note(char *to, char *from, char *msg, int idx, int echo)
  347. {
  348. int status, i, sock;
  349. char *p = NULL, botf[81] = "", ss[81] = "", ssf[81] = "";
  350. struct userrec *u;
  351. if (strlen(msg) > 450)
  352. msg[450] = 0; /* Notes have a limit */
  353. /* note length + PRIVMSG header + nickname + date must be <512 */
  354. p = strchr(to, '@');
  355. if (p != NULL) { /* Cross-bot note */
  356. char x[21] = "";
  357. *p = 0;
  358. strncpy(x, to, 20);
  359. x[20] = 0;
  360. *p = '@';
  361. p++;
  362. if (!egg_strcasecmp(p, conf.bot->nick)) /* To me?? */
  363. return add_note(x, from, msg, idx, echo); /* Start over, dimwit. */
  364. if (egg_strcasecmp(from, conf.bot->nick)) {
  365. if (strlen(from) > 40)
  366. from[40] = 0;
  367. if (strchr(from, '@')) {
  368. strcpy(botf, from);
  369. } else
  370. simple_sprintf(botf, "%s@%s", from, conf.bot->nick);
  371. } else
  372. strcpy(botf, conf.bot->nick);
  373. i = nextbot(p);
  374. if (i < 0) {
  375. if (idx >= 0)
  376. dprintf(idx, BOT_NOTHERE);
  377. return NOTE_ERROR;
  378. }
  379. if ((idx >= 0) && (echo))
  380. dprintf(idx, "-> %s@%s: %s\n", x, p, msg);
  381. if (idx >= 0) {
  382. simple_sprintf(ssf, "%d:%s", dcc[idx].sock, botf);
  383. botnet_send_priv(i, ssf, x, p, "%s", msg);
  384. } else
  385. botnet_send_priv(i, botf, x, p, "%s", msg);
  386. return NOTE_OK; /* Forwarded to the right bot */
  387. }
  388. /* Might be form "sock:nick" */
  389. splitc(ssf, from, ':');
  390. rmspace(ssf);
  391. splitc(ss, to, ':');
  392. rmspace(ss);
  393. if (!ss[0])
  394. sock = (-1);
  395. else
  396. sock = atoi(ss);
  397. if (!(u = get_user_by_handle(userlist, to))) {
  398. if (idx >= 0)
  399. dprintf(idx, "I don't know anyone by that name.\n");
  400. return NOTE_ERROR;
  401. }
  402. if (is_bot(u))
  403. return NOTE_ERROR;
  404. status = NOTE_ERROR;
  405. /* Online right now? */
  406. for (i = 0; i < dcc_total; i++) {
  407. if (dcc[i].type && (dcc[i].type->flags & DCT_CHAT) &&
  408. ((sock == (-1)) || (sock == dcc[i].sock)) &&
  409. (!egg_strcasecmp(dcc[i].nick, to))) {
  410. char *p2 = NULL, *fr = from;
  411. int l = 0;
  412. char work[1024] = "";
  413. while ((*msg == '<') || (*msg == '>')) {
  414. p2 = newsplit(&msg);
  415. if (*p2 == '<')
  416. l += simple_sprintf(work + l, "via %s, ", p2 + 1);
  417. else if (*from == '@')
  418. fr = p2 + 1;
  419. }
  420. if (idx == -2 || (!egg_strcasecmp(from, conf.bot->nick)))
  421. dprintf(i, "*** [%s] %s%s\n", fr, l ? work : "", msg);
  422. else
  423. dprintf(i, "%cNote [%s]: %s%s\n", 7, fr, l ? work : "", msg);
  424. if ((idx >= 0) && (echo))
  425. dprintf(idx, "-> %s: %s\n", to, msg);
  426. return NOTE_OK;
  427. }
  428. }
  429. /* The user is not online.. why are we getting a bot note for them? */
  430. if (idx == (-2))
  431. return NOTE_OK; /* Error msg from a tandembot: don't store */
  432. return NOTE_ERROR;
  433. }