botmsg.c 14 KB

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