botmsg.cc 15 KB

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