botmsg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (C) 1997 Robey Pointer
  3. * Copyright (C) 1999 - 2002 Eggheads Development Team
  4. * Copyright (C) 2002 - 2010 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 *) my_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)
  201. {
  202. if (tands > 0) {
  203. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "n %s %s %cD0gc %d %d %s %s\n", bot, next, flag,
  204. vlocalhub, (int) vbuildts, vcommit, vversion ? vversion : "");
  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\n", ptr->bot, ptr->share, ptr->localhub,
  223. (int) ptr->buildts, ptr->commit, ptr->version ? ptr->version : "");
  224. send_tand_but(idx, OBUF, len);
  225. }
  226. }
  227. void botnet_send_reject(int idx, char *fromp, char *frombot, char *top, char *tobot, char *reason)
  228. {
  229. size_t len;
  230. char to[NOTENAMELEN + 1] = "", from[NOTENAMELEN + 1] = "";
  231. if (tobot) {
  232. simple_snprintf(to, sizeof(to), "%s@%s", top, tobot);
  233. top = to;
  234. }
  235. if (frombot) {
  236. simple_snprintf(from, sizeof(from), "%s@%s", fromp, frombot);
  237. fromp = from;
  238. }
  239. if (!reason)
  240. reason = "";
  241. len = simple_snprintf(OBUF, sizeof(OBUF), "r %s %s %s\n", fromp, top, reason);
  242. tputs(dcc[idx].sock, OBUF, len);
  243. }
  244. void putallbots(const char *par)
  245. {
  246. botnet_send_zapf_broad(-1, conf.bot->nick, NULL, par);
  247. return;
  248. }
  249. void putbot(const char *bot, char *par)
  250. {
  251. if (!bot || !par || !bot[0] || !par[0])
  252. return;
  253. int i = nextbot(bot);
  254. if (i < 0)
  255. return;
  256. botnet_send_zapf(i, conf.bot->nick, bot, par);
  257. }
  258. /*
  259. botnet_send_log(...)
  260. - sends to uplink if a leaf
  261. - sends to all linked hubs if a hub
  262. */
  263. void botnet_send_log(int idx, const char *from, int type, const char *msg, bool truncate_ts)
  264. {
  265. size_t len;
  266. // Cut out timestamp
  267. if (truncate_ts)
  268. len = simple_snprintf(OBUF, sizeof(OBUF), "lo %s %d %s\n", from, type, &msg[LOG_TS_LEN + 1]); //+1 due to excess space
  269. else
  270. len = simple_snprintf(OBUF, sizeof(OBUF), "lo %s %d %s\n", from, type, msg);
  271. if (conf.bot->hub) {
  272. send_hubs_but(idx, OBUF, len);
  273. } else {
  274. send_uplink(OBUF, len);
  275. }
  276. }
  277. void botnet_send_zapf(int idx, const char *a, const char *b, const char *c)
  278. {
  279. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "z %s %s %s\n", a, b, c);
  280. tputs(dcc[idx].sock, OBUF, len);
  281. }
  282. void botnet_send_zapf_broad(int idx, const char *a, const char *b, const char *c)
  283. {
  284. if (tands > 0) {
  285. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "zb %s %s%s%s\n", a, b ? b : "", b ? " " : "", c);
  286. send_tand_but(idx, OBUF, len);
  287. }
  288. }
  289. void botnet_send_var(int idx, variable_t *var) {
  290. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "va %s %s\n", var->name, var->gdata ? var->gdata : "");
  291. tputs(dcc[idx].sock, OBUF, len);
  292. }
  293. void botnet_send_var_broad(int idx, variable_t *var) {
  294. if (tands > 0) {
  295. const size_t len = simple_snprintf(OBUF, sizeof(OBUF), "vab %s %s\n", var->name, var->gdata ? var->gdata : "");
  296. send_tand_but(idx, OBUF, len);
  297. }
  298. }
  299. void botnet_send_idle(int idx, char *bot, int sock, int idle, char *away)
  300. {
  301. if (tands > 0) {
  302. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "i %s %D %Ds%s\n", bot, sock, idle, away ? away : "");
  303. send_tand_but(idx, OBUF, len);
  304. }
  305. }
  306. void botnet_send_away(int idx, char *bot, int sock, char *msg, int linking)
  307. {
  308. if (tands > 0) {
  309. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "aw %s%s %D %s\n", ((idx >= 0) && linking) ? "!" : "", bot, sock, msg ? msg : "");
  310. send_tand_but(idx, OBUF, len);
  311. }
  312. }
  313. void botnet_send_join_idx(int useridx)
  314. {
  315. if (tands > 0) {
  316. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "j %s %s %D %c%D %s\n",
  317. conf.bot->nick, dcc[useridx].nick,
  318. dcc[useridx].type && dcc[useridx].type == &DCC_RELAYING ?
  319. dcc[useridx].u.relay->chat->channel : dcc[useridx].u.chat->channel, geticon(useridx),
  320. dcc[useridx].sock, dcc[useridx].host);
  321. send_tand_but(-1, OBUF, len);
  322. }
  323. }
  324. void botnet_send_join_party(int idx, int linking, int useridx)
  325. {
  326. if (tands > 0) {
  327. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "j %s%s %s %D %c%D %s\n", linking ? "!" : "",
  328. party[useridx].bot, party[useridx].nick,
  329. party[useridx].chan, party[useridx].flag, party[useridx].sock,
  330. party[useridx].from ? party[useridx].from : "");
  331. send_tand_but(idx, OBUF, len);
  332. }
  333. }
  334. void botnet_send_part_idx(int useridx, char *reason)
  335. {
  336. if (tands > 0) {
  337. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "pt %s %s %D %s\n", conf.bot->nick,
  338. dcc[useridx].nick, dcc[useridx].sock, reason ? reason : "");
  339. send_tand_but(-1, OBUF, len);
  340. }
  341. }
  342. void botnet_send_part_party(int idx, int partyidx, char *reason, int silent)
  343. {
  344. if (tands > 0) {
  345. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "pt %s%s %s %D %s\n",
  346. silent ? "!" : "", party[partyidx].bot,
  347. party[partyidx].nick, party[partyidx].sock, reason ? reason : "");
  348. send_tand_but(idx, OBUF, len);
  349. }
  350. }
  351. void botnet_send_nkch(int useridx, char *oldnick)
  352. {
  353. if (tands > 0) {
  354. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "nc %s %D %s\n", conf.bot->nick, dcc[useridx].sock, dcc[useridx].nick);
  355. send_tand_but(-1, OBUF, len);
  356. }
  357. }
  358. void botnet_send_nkch_part(int butidx, int useridx, char *oldnick)
  359. {
  360. if (tands > 0) {
  361. const size_t len = simple_snprintf2(OBUF, sizeof(OBUF), "nc %s %D %s\n", party[useridx].bot, party[useridx].sock, party[useridx].nick);
  362. send_tand_but(butidx, OBUF, len);
  363. }
  364. }
  365. /* This part of add_note is more relevant to the botnet than
  366. * to the notes file
  367. */
  368. int add_note(char *to, char *from, char *msg, int idx, int echo)
  369. {
  370. int i, sock;
  371. char *p = NULL, botf[81] = "", ss[81] = "", ssf[81] = "";
  372. struct userrec *u;
  373. if (strlen(msg) > 450)
  374. msg[450] = 0; /* Notes have a limit */
  375. /* note length + PRIVMSG header + nickname + date must be <512 */
  376. p = strchr(to, '@');
  377. if (p != NULL) { /* Cross-bot note */
  378. char x[21] = "";
  379. *p = 0;
  380. strlcpy(x, to, sizeof(x));
  381. x[20] = 0;
  382. *p = '@';
  383. p++;
  384. if (!strcasecmp(p, conf.bot->nick)) /* To me?? */
  385. return add_note(x, from, msg, idx, echo); /* Start over, dimwit. */
  386. if (strcasecmp(from, conf.bot->nick)) {
  387. if (strlen(from) > 40)
  388. from[40] = 0;
  389. if (strchr(from, '@')) {
  390. strlcpy(botf, from, sizeof(botf));
  391. } else
  392. simple_snprintf(botf, sizeof(botf), "%s@%s", from, conf.bot->nick);
  393. } else
  394. strlcpy(botf, conf.bot->nick, sizeof(botf));
  395. i = nextbot(p);
  396. if (i < 0) {
  397. if (idx >= 0)
  398. dprintf(idx, "That bot isn't here.\n");
  399. return NOTE_ERROR;
  400. }
  401. if ((idx >= 0) && (echo))
  402. dprintf(idx, "-> %s@%s: %s\n", x, p, msg);
  403. if (idx >= 0) {
  404. simple_snprintf(ssf, sizeof(ssf), "%d:%s", dcc[idx].sock, botf);
  405. botnet_send_priv(i, ssf, x, p, "%s", msg);
  406. } else
  407. botnet_send_priv(i, botf, x, p, "%s", msg);
  408. return NOTE_OK; /* Forwarded to the right bot */
  409. }
  410. /* Might be form "sock:nick" */
  411. splitc(ssf, from, ':');
  412. rmspace(ssf);
  413. splitc(ss, to, ':');
  414. rmspace(ss);
  415. if (!ss[0])
  416. sock = (-1);
  417. else
  418. sock = atoi(ss);
  419. if (!(u = get_user_by_handle(userlist, to))) {
  420. if (idx >= 0)
  421. dprintf(idx, "I don't know anyone by that name.\n");
  422. return NOTE_ERROR;
  423. }
  424. if (is_bot(u))
  425. return NOTE_ERROR;
  426. /* Online right now? */
  427. for (i = 0; i < dcc_total; i++) {
  428. if (dcc[i].type && (dcc[i].type->flags & DCT_CHAT) &&
  429. ((sock == (-1)) || (sock == dcc[i].sock)) &&
  430. (!strcasecmp(dcc[i].nick, to))) {
  431. char *p2 = NULL, *fr = from;
  432. int l = 0;
  433. char work[1024] = "";
  434. while ((*msg == '<') || (*msg == '>')) {
  435. p2 = newsplit(&msg);
  436. if (*p2 == '<')
  437. l += simple_snprintf(work + l, sizeof(work) - l, "via %s, ", p2 + 1);
  438. else if (*from == '@')
  439. fr = p2 + 1;
  440. }
  441. if (idx == -2 || (!strcasecmp(from, conf.bot->nick)))
  442. dprintf(i, "*** [%s] %s%s\n", fr, l ? work : "", msg);
  443. else
  444. dprintf(i, "%cNote [%s]: %s%s\n", 7, fr, l ? work : "", msg);
  445. if ((idx >= 0) && (echo))
  446. dprintf(idx, "-> %s: %s\n", to, msg);
  447. return NOTE_OK;
  448. }
  449. }
  450. /* The user is not online.. why are we getting a bot note for them? */
  451. if (idx == (-2))
  452. return NOTE_OK; /* Error msg from a tandembot: don't store */
  453. return NOTE_ERROR;
  454. }