botmsg.c 15 KB

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