botmsg.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  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. /* Thank you ircu :) */
  28. static char tobase64array[64] =
  29. {
  30. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
  31. 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  32. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
  33. 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  34. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  35. '[', ']'
  36. };
  37. char *int_to_base64(unsigned int val)
  38. {
  39. static char buf_base64[12] = "";
  40. buf_base64[11] = 0;
  41. if (!val) {
  42. buf_base64[10] = 'A';
  43. return buf_base64 + 10;
  44. }
  45. int i = 11;
  46. while (val) {
  47. i--;
  48. buf_base64[i] = tobase64array[val & 0x3f];
  49. val = val >> 6;
  50. }
  51. return buf_base64 + i;
  52. }
  53. char *int_to_base10(int val)
  54. {
  55. static char buf_base10[17] = "";
  56. buf_base10[16] = 0;
  57. if (!val) {
  58. buf_base10[15] = '0';
  59. return buf_base10 + 15;
  60. }
  61. int p = 0;
  62. int i = 16;
  63. if (val < 0) {
  64. p = 1;
  65. val *= -1;
  66. }
  67. while (val) {
  68. i--;
  69. buf_base10[i] = '0' + (val % 10);
  70. val /= 10;
  71. }
  72. if (p) {
  73. i--;
  74. buf_base10[i] = '-';
  75. }
  76. return buf_base10 + i;
  77. }
  78. char *unsigned_int_to_base10(unsigned int val)
  79. {
  80. static char buf_base10[16] = "";
  81. buf_base10[15] = 0;
  82. if (!val) {
  83. buf_base10[14] = '0';
  84. return buf_base10 + 14;
  85. }
  86. int i = 15;
  87. while (val) {
  88. i--;
  89. buf_base10[i] = '0' + (val % 10);
  90. val /= 10;
  91. }
  92. return buf_base10 + i;
  93. }
  94. size_t simple_sprintf (char *buf, const char *format, ...)
  95. {
  96. char *s = NULL;
  97. char *fp = (char *) format;
  98. size_t c = 0;
  99. unsigned int i;
  100. va_list va;
  101. va_start(va, format);
  102. while (*fp && c < 1023) {
  103. if (*fp == '%') {
  104. fp++;
  105. switch (*fp) {
  106. case 's':
  107. s = va_arg(va, char *);
  108. break;
  109. case 'd':
  110. case 'i':
  111. i = va_arg(va, int);
  112. s = int_to_base10(i);
  113. break;
  114. case 'D':
  115. i = va_arg(va, int);
  116. s = int_to_base64((unsigned int) i);
  117. break;
  118. case 'u':
  119. i = va_arg(va, unsigned int);
  120. s = unsigned_int_to_base10(i);
  121. break;
  122. case '%':
  123. buf[c++] = *fp++;
  124. continue;
  125. case 'c':
  126. buf[c++] = (char) va_arg(va, int);
  127. fp++;
  128. continue;
  129. default:
  130. continue;
  131. }
  132. if (s)
  133. while (*s && c < 1023)
  134. buf[c++] = *s++;
  135. fp++;
  136. } else
  137. buf[c++] = *fp++;
  138. }
  139. va_end(va);
  140. buf[c] = 0;
  141. return c;
  142. }
  143. /* Ditto for tandem bots
  144. */
  145. static void send_tand_but(int x, char *buf, size_t len)
  146. {
  147. for (int i = 0; i < dcc_total; i++) {
  148. if (dcc[i].type && (dcc[i].type == &DCC_BOT) && i != x) {
  149. tputs(dcc[i].sock, buf, len);
  150. }
  151. }
  152. }
  153. void botnet_send_cmdpass(int idx, char *cmd, char *pass)
  154. {
  155. if (tands > 0) {
  156. char *buf = (char *) my_calloc(1, strlen(cmd) + strlen(pass) + 5 + 1);
  157. sprintf(buf, "cp %s %s\n", cmd, pass);
  158. send_tand_but(idx, buf, strlen(buf));
  159. free(buf);
  160. }
  161. }
  162. int botnet_send_cmd(char * fbot, char * bot, char *fhnd, int fromidx, char * cmd) {
  163. int i = nextbot(bot);
  164. if (i >= 0) {
  165. simple_sprintf(OBUF, "rc %s %s %s %i %s\n", bot, fbot, fhnd, fromidx, cmd);
  166. tputs(dcc[i].sock, OBUF, strlen(OBUF));
  167. return 1;
  168. } else if (!strcmp(bot, conf.bot->nick)) {
  169. char tmp[24] = "";
  170. sprintf(tmp, "%i", fromidx);
  171. gotremotecmd(conf.bot->nick, conf.bot->nick, fhnd, tmp, cmd);
  172. }
  173. return 0;
  174. }
  175. void botnet_send_cmd_broad(int idx, char * fbot, char *fhnd, int fromidx, char * cmd) {
  176. if (tands > 0) {
  177. egg_snprintf(OBUF, sizeof OBUF, "rc * %s %s %i %s\n", fbot, fhnd, fromidx, cmd);
  178. send_tand_but(idx, OBUF, strlen(OBUF));
  179. }
  180. if (idx < 0) {
  181. char tmp[24] = "";
  182. sprintf(tmp, "%i", fromidx);
  183. gotremotecmd("*", conf.bot->nick, fhnd, tmp, cmd);
  184. }
  185. }
  186. void botnet_send_cmdreply(char * fbot, char * bot, char * to, char * toidx, char * ln) {
  187. int i = nextbot(bot);
  188. if (i >= 0) {
  189. egg_snprintf(OBUF, sizeof OBUF, "rr %s %s %s %s %s\n", bot, fbot, to, toidx, ln);
  190. tputs(dcc[i].sock, OBUF, strlen(OBUF));
  191. } else if (!strcmp(bot, conf.bot->nick)) {
  192. gotremotereply(conf.bot->nick, to, toidx, ln);
  193. }
  194. }
  195. void botnet_send_bye()
  196. {
  197. if (tands > 0)
  198. send_tand_but(-1, "bye\n", 5);
  199. }
  200. void botnet_send_chan(int idx, char *botnick, char *user, int chan, char *data)
  201. {
  202. if ((tands > 0) && (chan < GLOBAL_CHANS)) {
  203. size_t len;
  204. if (user) {
  205. len = simple_sprintf(OBUF, "c %s@%s %D %s\n", user, botnick, chan, data);
  206. } else {
  207. len = simple_sprintf(OBUF, "c %s %D %s\n", botnick, chan, data);
  208. }
  209. send_tand_but(idx, OBUF, len);
  210. }
  211. }
  212. void botnet_send_act(int idx, char *botnick, char *user, int chan, char *data)
  213. {
  214. if ((tands > 0) && (chan < GLOBAL_CHANS)) {
  215. size_t len;
  216. if (user) {
  217. len = simple_sprintf(OBUF, "a %s@%s %D %s\n", user, botnick, chan, data);
  218. } else {
  219. len = simple_sprintf(OBUF, "a %s %D %s\n", botnick, chan, data);
  220. }
  221. send_tand_but(idx, OBUF, len);
  222. }
  223. }
  224. void botnet_send_chat(int idx, char *botnick, char *data)
  225. {
  226. if (tands > 0) {
  227. size_t len = simple_sprintf(OBUF, "ct %s %s\n", botnick, data);
  228. send_tand_but(idx, OBUF, len);
  229. }
  230. }
  231. void botnet_send_ping(int idx)
  232. {
  233. tputs(dcc[idx].sock, "pi\n", 3);
  234. dcc[idx].pingtime = now;
  235. }
  236. void botnet_send_pong(int idx)
  237. {
  238. tputs(dcc[idx].sock, "po\n", 3);
  239. }
  240. void botnet_send_priv (int idx, char *from, char *to, char *tobot, char *format, ...)
  241. {
  242. size_t len;
  243. char tbuf[1024] = "";
  244. va_list va;
  245. va_start(va, format);
  246. egg_vsnprintf(tbuf, 450, format, va);
  247. va_end(va);
  248. tbuf[sizeof(tbuf)-1] = 0;
  249. if (tobot) {
  250. len = simple_sprintf(OBUF, "p %s %s@%s %s\n", from, to, tobot, tbuf);
  251. } else {
  252. len = simple_sprintf(OBUF, "p %s %s %s\n", from, to, tbuf);
  253. }
  254. tputs(dcc[idx].sock, OBUF, len);
  255. }
  256. void botnet_send_who(int idx, char *from, char *to, int chan)
  257. {
  258. size_t len = simple_sprintf(OBUF, "w %s %s %D\n", from, to, chan);
  259. tputs(dcc[idx].sock, OBUF, len);
  260. }
  261. void botnet_send_unlink(int idx, char *who, char *via, char *bot, char *reason)
  262. {
  263. size_t len = simple_sprintf(OBUF, "ul %s %s %s %s\n", who, via, bot, reason);
  264. tputs(dcc[idx].sock, OBUF, len);
  265. }
  266. void botnet_send_link(int idx, char *who, char *via, char *bot)
  267. {
  268. size_t len = simple_sprintf(OBUF, "l %s %s %s\n", who, via, bot);
  269. tputs(dcc[idx].sock, OBUF, len);
  270. }
  271. void botnet_send_unlinked(int idx, char *bot, char *args)
  272. {
  273. if (tands > 0) {
  274. size_t len = simple_sprintf(OBUF, "un %s %s\n", bot, args ? args : "");
  275. send_tand_but(idx, OBUF, len);
  276. }
  277. }
  278. void botnet_send_nlinked(int idx, char *bot, char *next, char flag, int vlocalhub, time_t vbuildts, char *vversion)
  279. {
  280. if (tands > 0) {
  281. size_t len = simple_sprintf(OBUF, "n %s %s %cD0gc %d %d %s\n", bot, next, flag,
  282. vlocalhub, vbuildts, vversion ? vversion : "");
  283. send_tand_but(idx, OBUF, len);
  284. }
  285. }
  286. void botnet_send_traced(int idx, char *bot, char *buf)
  287. {
  288. size_t len = simple_sprintf(OBUF, "td %s %s\n", bot, buf);
  289. tputs(dcc[idx].sock, OBUF, len);
  290. }
  291. void botnet_send_trace(int idx, char *to, char *from, char *buf)
  292. {
  293. size_t len = simple_sprintf(OBUF, "t %s %s %s:%s\n", to, from, buf, conf.bot->nick);
  294. tputs(dcc[idx].sock, OBUF, len);
  295. }
  296. void botnet_send_update(int idx, tand_t * ptr)
  297. {
  298. if (tands > 0) {
  299. /* the D0gc is a lingering hack which probably will never be able to come out. */
  300. size_t len = simple_sprintf(OBUF, "u %s %cD0gc %d %d %s\n", ptr->bot, ptr->share, ptr->localhub,
  301. ptr->buildts, ptr->version ? ptr->version : "");
  302. send_tand_but(idx, OBUF, len);
  303. }
  304. }
  305. void botnet_send_reject(int idx, char *fromp, char *frombot, char *top, char *tobot, char *reason)
  306. {
  307. size_t len;
  308. char to[NOTENAMELEN + 1] = "", from[NOTENAMELEN + 1] = "";
  309. if (tobot) {
  310. simple_sprintf(to, "%s@%s", top, tobot);
  311. top = to;
  312. }
  313. if (frombot) {
  314. simple_sprintf(from, "%s@%s", fromp, frombot);
  315. fromp = from;
  316. }
  317. if (!reason)
  318. reason = "";
  319. len = simple_sprintf(OBUF, "r %s %s %s\n", fromp, top, reason);
  320. tputs(dcc[idx].sock, OBUF, len);
  321. }
  322. void putallbots(char *par)
  323. {
  324. botnet_send_zapf_broad(-1, conf.bot->nick, NULL, par);
  325. return;
  326. }
  327. void putbot(char *bot, char *par)
  328. {
  329. if (!bot || !par || !bot[0] || !par[0])
  330. return;
  331. int i = nextbot(bot);
  332. if (i < 0)
  333. return;
  334. botnet_send_zapf(i, conf.bot->nick, bot, par);
  335. }
  336. void botnet_send_zapf(int idx, char *a, char *b, char *c)
  337. {
  338. size_t len = simple_sprintf(OBUF, "z %s %s %s\n", a, b, c);
  339. tputs(dcc[idx].sock, OBUF, len);
  340. }
  341. void botnet_send_zapf_broad(int idx, char *a, char *b, char *c)
  342. {
  343. if (tands > 0) {
  344. size_t len = simple_sprintf(OBUF, "zb %s %s%s%s\n", a, b ? b : "", b ? " " : "", c);
  345. send_tand_but(idx, OBUF, len);
  346. }
  347. }
  348. void botnet_send_cfg(int idx, struct cfg_entry * entry) {
  349. size_t len = simple_sprintf(OBUF, "cg %s %s\n", entry->name, entry->gdata ? entry->gdata : "");
  350. tputs(dcc[idx].sock, OBUF, len);
  351. }
  352. void botnet_send_cfg_broad(int idx, struct cfg_entry * entry) {
  353. if (tands > 0) {
  354. size_t len = simple_sprintf(OBUF, "cgb %s %s\n", entry->name, entry->gdata ? entry->gdata : "");
  355. send_tand_but(idx, OBUF, len);
  356. }
  357. }
  358. void botnet_send_idle(int idx, char *bot, int sock, int idle, char *away)
  359. {
  360. if (tands > 0) {
  361. size_t len = simple_sprintf(OBUF, "i %s %D %D %s\n", bot, sock, idle, away ? away : "");
  362. send_tand_but(idx, OBUF, len);
  363. }
  364. }
  365. void botnet_send_away(int idx, char *bot, int sock, char *msg, int linking)
  366. {
  367. if (tands > 0) {
  368. size_t len = simple_sprintf(OBUF, "aw %s%s %D %s\n", ((idx >= 0) && linking) ? "!" : "", bot, sock, msg ? msg : "");
  369. send_tand_but(idx, OBUF, len);
  370. }
  371. }
  372. void botnet_send_join_idx(int useridx)
  373. {
  374. if (tands > 0) {
  375. size_t len = simple_sprintf(OBUF, "j %s %s %D %c%D %s\n",
  376. conf.bot->nick, dcc[useridx].nick,
  377. dcc[useridx].type && dcc[useridx].type == &DCC_RELAYING ?
  378. dcc[useridx].u.relay->chat->channel :
  379. dcc[useridx].u.chat->channel, geticon(useridx),
  380. dcc[useridx].sock, dcc[useridx].host);
  381. send_tand_but(-1, OBUF, len);
  382. }
  383. }
  384. void botnet_send_join_party(int idx, int linking, int useridx)
  385. {
  386. if (tands > 0) {
  387. size_t len = simple_sprintf(OBUF, "j %s%s %s %D %c%D %s\n", linking ? "!" : "",
  388. party[useridx].bot, party[useridx].nick,
  389. party[useridx].chan, party[useridx].flag,
  390. party[useridx].sock,
  391. party[useridx].from ? party[useridx].from : "");
  392. send_tand_but(idx, OBUF, len);
  393. }
  394. }
  395. void botnet_send_part_idx(int useridx, char *reason)
  396. {
  397. if (tands > 0) {
  398. size_t len = simple_sprintf(OBUF, "pt %s %s %D %s\n", conf.bot->nick,
  399. dcc[useridx].nick, dcc[useridx].sock,
  400. reason ? reason : "");
  401. send_tand_but(-1, OBUF, len);
  402. }
  403. }
  404. void botnet_send_part_party(int idx, int partyidx, char *reason, int silent)
  405. {
  406. if (tands > 0) {
  407. size_t len = simple_sprintf(OBUF, "pt %s%s %s %D %s\n",
  408. silent ? "!" : "", party[partyidx].bot,
  409. party[partyidx].nick, party[partyidx].sock,
  410. reason ? reason : "");
  411. send_tand_but(idx, OBUF, len);
  412. }
  413. }
  414. void botnet_send_nkch(int useridx, char *oldnick)
  415. {
  416. if (tands > 0) {
  417. size_t len = simple_sprintf(OBUF, "nc %s %D %s\n", conf.bot->nick, dcc[useridx].sock, dcc[useridx].nick);
  418. send_tand_but(-1, OBUF, len);
  419. }
  420. }
  421. void botnet_send_nkch_part(int butidx, int useridx, char *oldnick)
  422. {
  423. if (tands > 0) {
  424. size_t len = simple_sprintf(OBUF, "nc %s %D %s\n", party[useridx].bot, party[useridx].sock, party[useridx].nick);
  425. send_tand_but(butidx, OBUF, len);
  426. }
  427. }
  428. /* This part of add_note is more relevant to the botnet than
  429. * to the notes file
  430. */
  431. int add_note(char *to, char *from, char *msg, int idx, int echo)
  432. {
  433. int status, i, iaway, sock;
  434. char *p = NULL, botf[81] = "", ss[81] = "", ssf[81] = "";
  435. struct userrec *u;
  436. if (strlen(msg) > 450)
  437. msg[450] = 0; /* Notes have a limit */
  438. /* note length + PRIVMSG header + nickname + date must be <512 */
  439. p = strchr(to, '@');
  440. if (p != NULL) { /* Cross-bot note */
  441. char x[21] = "";
  442. *p = 0;
  443. strncpy(x, to, 20);
  444. x[20] = 0;
  445. *p = '@';
  446. p++;
  447. if (!egg_strcasecmp(p, conf.bot->nick)) /* To me?? */
  448. return add_note(x, from, msg, idx, echo); /* Start over, dimwit. */
  449. if (egg_strcasecmp(from, conf.bot->nick)) {
  450. if (strlen(from) > 40)
  451. from[40] = 0;
  452. if (strchr(from, '@')) {
  453. strcpy(botf, from);
  454. } else
  455. sprintf(botf, "%s@%s", from, conf.bot->nick);
  456. } else
  457. strcpy(botf, conf.bot->nick);
  458. i = nextbot(p);
  459. if (i < 0) {
  460. if (idx >= 0)
  461. dprintf(idx, BOT_NOTHERE);
  462. return NOTE_ERROR;
  463. }
  464. if ((idx >= 0) && (echo))
  465. dprintf(idx, "-> %s@%s: %s\n", x, p, msg);
  466. if (idx >= 0) {
  467. sprintf(ssf, "%d:%s", dcc[idx].sock, botf);
  468. botnet_send_priv(i, ssf, x, p, "%s", msg);
  469. } else
  470. botnet_send_priv(i, botf, x, p, "%s", msg);
  471. return NOTE_OK; /* Forwarded to the right bot */
  472. }
  473. /* Might be form "sock:nick" */
  474. splitc(ssf, from, ':');
  475. rmspace(ssf);
  476. splitc(ss, to, ':');
  477. rmspace(ss);
  478. if (!ss[0])
  479. sock = (-1);
  480. else
  481. sock = atoi(ss);
  482. /* Don't process if there's a note binding for it */
  483. if (idx != (-2)) { /* Notes from bots don't trigger it */
  484. if (check_bind_note(from, to, msg)) {
  485. if ((idx >= 0) && (echo))
  486. dprintf(idx, "-> %s: %s\n", to, msg);
  487. return NOTE_TCL;
  488. }
  489. }
  490. if (!(u = get_user_by_handle(userlist, to))) {
  491. if (idx >= 0)
  492. dprintf(idx, USERF_UNKNOWN);
  493. return NOTE_ERROR;
  494. }
  495. if (is_bot(u)) {
  496. if (idx >= 0)
  497. dprintf(idx, BOT_NONOTES);
  498. return NOTE_ERROR;
  499. }
  500. status = NOTE_STORED;
  501. iaway = 0;
  502. /* Online right now? */
  503. for (i = 0; i < dcc_total; i++) {
  504. if (dcc[i].type && (dcc[i].type->flags & DCT_GETNOTES) &&
  505. ((sock == (-1)) || (sock == dcc[i].sock)) &&
  506. (!egg_strcasecmp(dcc[i].nick, to))) {
  507. int aok = 1;
  508. if (dcc[i].type == &DCC_CHAT)
  509. if ((dcc[i].u.chat->away != NULL) &&
  510. (idx != (-2))) {
  511. /* Only check away if it's not from a bot */
  512. aok = 0;
  513. if (idx >= 0)
  514. dprintf(idx, "%s %s: %s\n", dcc[i].nick, BOT_USERAWAY,
  515. dcc[i].u.chat->away);
  516. if (!iaway)
  517. iaway = i;
  518. status = NOTE_AWAY;
  519. }
  520. if (aok) {
  521. char *p2 = NULL, *fr = from;
  522. int l = 0;
  523. char work[1024] = "";
  524. while ((*msg == '<') || (*msg == '>')) {
  525. p2 = newsplit(&msg);
  526. if (*p2 == '<')
  527. l += simple_sprintf(work + l, "via %s, ", p2 + 1);
  528. else if (*from == '@')
  529. fr = p2 + 1;
  530. }
  531. if (idx == -2 || (!egg_strcasecmp(from, conf.bot->nick)))
  532. dprintf(i, "*** [%s] %s%s\n", fr, l ? work : "", msg);
  533. else
  534. dprintf(i, "%cNote [%s]: %s%s\n", 7, fr, l ? work : "", msg);
  535. if ((idx >= 0) && (echo))
  536. dprintf(idx, "-> %s: %s\n", to, msg);
  537. return NOTE_OK;
  538. }
  539. }
  540. }
  541. if (idx == (-2))
  542. return NOTE_OK; /* Error msg from a tandembot: don't store */
  543. status = storenote(from, to, msg, idx, NULL, 0);
  544. if (status < 0) status = NOTE_ERROR;
  545. else if (status == NOTE_AWAY) {
  546. /* User is away in all sessions -- just notify the user that a
  547. * message arrived and was stored. (only oldest session is notified.)
  548. */
  549. dprintf(iaway, "*** %s.\n", BOT_NOTEARRIVED);
  550. }
  551. return(status);
  552. }