share.cc 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  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. * share.c -- part of share.mod
  22. *
  23. */
  24. #include "src/common.h"
  25. #include "src/main.h"
  26. #include "src/rfc1459.h"
  27. #include "src/botmsg.h"
  28. #include "src/misc.h"
  29. #include "src/misc_file.h"
  30. #include "src/cmds.h"
  31. #include "src/chanprog.h"
  32. #include "src/users.h"
  33. #include "src/userrec.h"
  34. #include "src/botnet.h"
  35. #include "src/auth.h"
  36. #include "src/set.h"
  37. #include "src/EncryptedStream.h"
  38. #include <bdlib/src/String.h>
  39. #include <netinet/in.h>
  40. #include <arpa/inet.h>
  41. #include <sys/stat.h>
  42. #include <stdarg.h>
  43. #include "src/chan.h"
  44. #include "src/net.h"
  45. #include "src/users.h"
  46. #include "src/egg_timer.h"
  47. #include "src/mod/transfer.mod/transfer.h"
  48. #include "src/mod/channels.mod/channels.h"
  49. #include "src/mod/irc.mod/irc.h"
  50. #include "src/mod/server.mod/server.h"
  51. void irc_init();
  52. static struct flag_record fr = { 0, 0, 0, 0 };
  53. static bd::Stream* stream_in;
  54. struct delay_mode {
  55. struct delay_mode *next;
  56. struct chanset_t *chan;
  57. int plsmns;
  58. int mode;
  59. int seconds;
  60. char *mask;
  61. };
  62. static struct delay_mode *start_delay = NULL;
  63. /* Prototypes */
  64. static void start_sending_users(int);
  65. static void stream_send_users(int);
  66. static void share_read_stream(int, bd::Stream&);
  67. #ifdef __GNUC__
  68. static void shareout_but(int, const char *, ...) __attribute__ ((format(printf, 2, 3)));
  69. #else
  70. static void shareout_but(int, const char *, ...);
  71. #endif
  72. static bool cancel_user_xfer_staylinked = 0;
  73. static void cancel_user_xfer(int, void *);
  74. #include "share.h"
  75. /*
  76. * Resync buffers
  77. */
  78. /* Store info for sharebots */
  79. struct share_msgq {
  80. struct share_msgq *next;
  81. char *msg;
  82. };
  83. typedef struct tandbuf_t {
  84. struct tandbuf_t *next;
  85. struct share_msgq *q;
  86. time_t timer;
  87. char bot[HANDLEN + 1];
  88. } tandbuf;
  89. tandbuf *tbuf = NULL;
  90. /* Create a tandem buffer for 'bot'.
  91. */
  92. static void new_tbuf(char *bot)
  93. {
  94. tandbuf **old = &tbuf, *newbuf = NULL;
  95. newbuf = (tandbuf *) calloc(1, sizeof(tandbuf));
  96. strlcpy(newbuf->bot, bot, sizeof(newbuf->bot));
  97. newbuf->q = NULL;
  98. newbuf->timer = now;
  99. newbuf->next = *old;
  100. *old = newbuf;
  101. putlog(LOG_BOTS, "*", "Creating resync buffer for %s", bot);
  102. }
  103. static void del_tbuf(tandbuf *goner)
  104. {
  105. struct share_msgq *q = NULL, *r = NULL;
  106. tandbuf *t = NULL, *old = NULL;
  107. for (t = tbuf; t; old = t, t = t->next) {
  108. if (t == goner) {
  109. if (old)
  110. old->next = t->next;
  111. else
  112. tbuf = t->next;
  113. for (q = t->q; q && q->msg[0]; q = r) {
  114. r = q->next;
  115. free(q->msg);
  116. free(q);
  117. }
  118. free(t);
  119. break;
  120. }
  121. }
  122. }
  123. /* Flush a certain bot's tbuf.
  124. */
  125. static bool flush_tbuf(char *bot)
  126. {
  127. tandbuf *t = NULL, *tnext = NULL;
  128. for (t = tbuf; t; t = tnext) {
  129. tnext = t->next;
  130. if (!strcasecmp(t->bot, bot)) {
  131. del_tbuf(t);
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. static struct share_msgq *q_addmsg(struct share_msgq *qq, char *s)
  138. {
  139. struct share_msgq *q = NULL;
  140. int cnt;
  141. size_t siz = 0;
  142. if (!qq) {
  143. q = (share_msgq *) calloc(1, sizeof *q);
  144. q->next = NULL;
  145. siz = strlen(s) + 1;
  146. q->msg = (char *) calloc(1, siz);
  147. strlcpy(q->msg, s, siz);
  148. return q;
  149. }
  150. cnt = 0;
  151. for (q = qq; q->next; q = q->next)
  152. cnt++;
  153. if (cnt > 1000)
  154. return NULL; /* Return null: did not alter queue */
  155. q->next = (share_msgq *) calloc(1, sizeof *q->next);
  156. q = q->next;
  157. q->next = NULL;
  158. siz = strlen(s) + 1;
  159. q->msg = (char *) calloc(1, siz);
  160. strlcpy(q->msg, s, siz);
  161. return qq;
  162. }
  163. /* Add stuff to a specific bot's tbuf.
  164. */
  165. static void q_tbuf(char *bot, char *s)
  166. {
  167. struct share_msgq *q = NULL;
  168. tandbuf *t = NULL;
  169. for (t = tbuf; t && t->bot[0]; t = t->next)
  170. if (!strcasecmp(t->bot, bot)) {
  171. if ((q = q_addmsg(t->q, s)))
  172. t->q = q;
  173. break;
  174. }
  175. }
  176. /* Add stuff to the resync buffers.
  177. */
  178. static void q_resync(char *s)
  179. {
  180. struct share_msgq *q = NULL;
  181. tandbuf *t = NULL;
  182. for (t = tbuf; t && t->bot[0]; t = t->next) {
  183. if ((q = q_addmsg(t->q, s)))
  184. t->q = q;
  185. }
  186. }
  187. static void q_resync_but(char *s, const char *bot)
  188. {
  189. struct share_msgq *q = NULL;
  190. tandbuf *t = NULL;
  191. for (t = tbuf; t && t->bot[0]; t = t->next) {
  192. if (strcasecmp(t->bot, bot)) {
  193. if ((q = q_addmsg(t->q, s)))
  194. t->q = q;
  195. }
  196. }
  197. }
  198. /* Dump the resync buffer for a bot.
  199. */
  200. void dump_resync(int idx)
  201. {
  202. struct share_msgq *q = NULL;
  203. tandbuf *t = NULL;
  204. for (t = tbuf; t && t->bot[0]; t = t->next)
  205. if (!strcasecmp(dcc[idx].nick, t->bot)) {
  206. for (q = t->q; q && q->msg[0]; q = q->next) {
  207. dprintf(idx, "%s", q->msg);
  208. }
  209. flush_tbuf(dcc[idx].nick);
  210. break;
  211. }
  212. }
  213. /*
  214. * Sup's delay code
  215. */
  216. static void
  217. add_delay(struct chanset_t *chan, int plsmns, int mode, char *mask)
  218. {
  219. struct delay_mode *d = (struct delay_mode *) calloc(1, sizeof(struct delay_mode));
  220. d->chan = chan;
  221. d->plsmns = plsmns;
  222. d->mode = mode;
  223. size_t mlen = strlen(mask) + 1;
  224. d->mask = (char *) calloc(1, mlen);
  225. strlcpy(d->mask, mask, mlen);
  226. d->seconds = (int) (now + randint(20));
  227. d->next = start_delay;
  228. start_delay = d;
  229. }
  230. static void
  231. del_delay(struct delay_mode *delay)
  232. {
  233. struct delay_mode *old = NULL;
  234. for (struct delay_mode *d = start_delay; d; old = d, d = d->next) {
  235. if (d == delay) {
  236. if (old)
  237. old->next = d->next;
  238. else
  239. start_delay = d->next;
  240. if (d->mask)
  241. free(d->mask);
  242. free(d);
  243. break;
  244. }
  245. }
  246. }
  247. static void
  248. check_delay()
  249. {
  250. struct delay_mode *dnext = NULL;
  251. for (struct delay_mode *d = start_delay; d; d = dnext) {
  252. dnext = d->next;
  253. if (d->seconds <= now) {
  254. add_mode(d->chan, d->plsmns, d->mode, d->mask);
  255. del_delay(d);
  256. }
  257. }
  258. }
  259. /*
  260. * Botnet commands
  261. */
  262. static void
  263. share_stick_mask(int idx, char *par)
  264. {
  265. if (dcc[idx].status & STAT_SHARE) {
  266. char *types = newsplit(&par);
  267. const char type = types[0];
  268. const char *str_type = (type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite");
  269. char *host = NULL, *val = NULL;
  270. bool yn = 0;
  271. struct chanset_t *chan = NULL;
  272. maskrec *channel_list = NULL;
  273. host = newsplit(&par);
  274. val = newsplit(&par);
  275. yn = atoi(val);
  276. noshare = 1;
  277. if (!par[0]) { /* Global ban */
  278. channel_list = (type == 'b' ? global_bans : type == 'e' ? global_exempts : global_invites);
  279. if (u_setsticky_mask(NULL, channel_list, host, yn, type) > 0) {
  280. if (conf.bot->hub)
  281. putlog(LOG_CMDS, "@", "%s: %s %s %s", dcc[idx].nick, (yn) ? "stick" : "unstick", str_type, host);
  282. else
  283. for (chan = chanset; chan ; chan = chan->next)
  284. check_this_mask(type, chan, host, yn);
  285. shareout_but(idx, "ms %c %s %d\n", type, host, yn);
  286. }
  287. } else {
  288. if ((chan = findchan_by_dname(par))) {
  289. channel_list = (type == 'b' ? chan->bans : type == 'e' ? chan->exempts : chan->invites);
  290. if (u_setsticky_mask(chan, channel_list, host, yn, type) > 0) {
  291. if (conf.bot->hub)
  292. putlog(LOG_CMDS, "@", "%s: %s %s %s %s", dcc[idx].nick, (yn) ? "stick" : "unstick", str_type, host, par);
  293. else
  294. check_this_mask(type, chan, host, yn);
  295. shareout_but(idx, "ms %c %s %d %s\n", type, host, yn, chan->dname);
  296. noshare = 0;
  297. return;
  298. }
  299. }
  300. putlog(LOG_CMDS, "@", "Rejecting invalid sticky %s: %s on %s%s", host, par, yn ? "" : " (unstick)", str_type);
  301. }
  302. noshare = 0;
  303. }
  304. }
  305. static void
  306. share_chhand(int idx, char *par)
  307. {
  308. if (dcc[idx].status & STAT_SHARE) {
  309. char *hand = NULL;
  310. struct userrec *u = NULL;
  311. hand = newsplit(&par);
  312. u = get_user_by_handle(userlist, hand);
  313. if (u) {
  314. int value = 0;
  315. shareout_but(idx, "h %s %s\n", hand, par);
  316. noshare = 1;
  317. value = change_handle(u, par);
  318. noshare = 0;
  319. if (value && conf.bot->hub)
  320. putlog(LOG_CMDS, "@", "%s: handle %s->%s", dcc[idx].nick, hand, par);
  321. }
  322. }
  323. }
  324. static void
  325. share_chattr(int idx, char *par)
  326. {
  327. if (dcc[idx].status & STAT_SHARE) {
  328. char *hand = NULL, *atr = NULL, s[100] = "";
  329. struct chanset_t *cst = NULL;
  330. struct userrec *u = NULL;
  331. struct flag_record fr2;
  332. flag_t ofl;
  333. hand = newsplit(&par);
  334. u = get_user_by_handle(userlist, hand);
  335. if (u) {
  336. atr = newsplit(&par);
  337. cst = findchan_by_dname(par);
  338. if (!par[0] || cst) {
  339. if (!(dcc[idx].status & STAT_GETTING))
  340. shareout_but(idx, "a %s %s %s\n", hand, atr, par);
  341. noshare = 1;
  342. if (par[0] && cst) {
  343. fr.match = FR_CHAN;
  344. fr2.match = FR_CHAN;
  345. if (u->bot) {
  346. fr.match |= FR_BOT;
  347. fr2.match |= FR_BOT;
  348. }
  349. break_down_flags(atr, &fr, 0);
  350. get_user_flagrec(u, &fr2, par);
  351. set_user_flagrec(u, &fr, par);
  352. noshare = 0;
  353. check_dcc_chanattrs(u, par, fr.chan, fr2.chan);
  354. build_flags(s, &fr, 0);
  355. if (conf.bot->hub) {
  356. if (!(dcc[idx].status & STAT_GETTING))
  357. putlog(LOG_CMDS, "@", "%s: chattr %s %s %s", dcc[idx].nick, hand, s, par);
  358. } else
  359. check_this_user(u->handle, 0, NULL);
  360. } else {
  361. fr.match = FR_GLOBAL;
  362. get_user_flagrec(dcc[idx].user, &fr, 0);
  363. /* Don't let bot flags be altered */
  364. ofl = fr.global;
  365. if (u->bot)
  366. fr.match |= FR_BOT;
  367. break_down_flags(atr, &fr, 0);
  368. fr.global = sanity_check(fr.global, u->bot);
  369. set_user_flagrec(u, &fr, 0);
  370. noshare = 0;
  371. check_dcc_attrs(u, ofl);
  372. build_flags(s, &fr, 0);
  373. fr.match = FR_CHAN;
  374. if (conf.bot->hub) {
  375. if (!(dcc[idx].status & STAT_GETTING))
  376. putlog(LOG_CMDS, "@", "%s: chattr %s %s", dcc[idx].nick, hand, s);
  377. } else {
  378. check_this_user(u->handle, 0, NULL);
  379. }
  380. }
  381. noshare = 0;
  382. }
  383. if (conf.bot->hub)
  384. write_userfile(-1);
  385. }
  386. }
  387. }
  388. static void
  389. share_pls_chrec(int idx, char *par)
  390. {
  391. if (dcc[idx].status & STAT_SHARE) {
  392. char *user = NULL;
  393. struct chanset_t *chan = NULL;
  394. struct userrec *u = NULL;
  395. user = newsplit(&par);
  396. if ((u = get_user_by_handle(userlist, user))) {
  397. chan = findchan_by_dname(par);
  398. if (chan) {
  399. noshare = 1;
  400. shareout_but(idx, "+cr %s %s\n", user, par);
  401. if (!get_chanrec(u, par)) {
  402. add_chanrec(u, par);
  403. if (conf.bot->hub)
  404. putlog(LOG_CMDS, "@", "%s: +chrec %s %s", dcc[idx].nick, user, par);
  405. }
  406. noshare = 0;
  407. }
  408. }
  409. }
  410. }
  411. static void
  412. share_mns_chrec(int idx, char *par)
  413. {
  414. if (dcc[idx].status & STAT_SHARE) {
  415. char *user = NULL;
  416. struct chanset_t *chan = NULL;
  417. struct userrec *u = NULL;
  418. user = newsplit(&par);
  419. if ((u = get_user_by_handle(userlist, user))) {
  420. chan = findchan_by_dname(par);
  421. if (chan) {
  422. noshare = 1;
  423. del_chanrec(u, par);
  424. shareout_but(idx, "-cr %s %s\n", user, par);
  425. noshare = 0;
  426. if (conf.bot->hub)
  427. putlog(LOG_CMDS, "@", "%s: -chrec %s %s", dcc[idx].nick, user, par);
  428. else
  429. recheck_channel(chan, 0);
  430. }
  431. }
  432. }
  433. }
  434. static void
  435. share_newuser(int idx, char *par)
  436. {
  437. if (dcc[idx].status & STAT_SHARE) {
  438. char *nick = NULL, *host = NULL, *pass = NULL;
  439. bool isbot = 0;
  440. struct userrec *u = NULL;
  441. nick = newsplit(&par);
  442. host = newsplit(&par);
  443. pass = newsplit(&par);
  444. if (nick[0] == '-') {
  445. isbot = 1;
  446. nick++;
  447. }
  448. if (!(u = get_user_by_handle(userlist, nick))) {
  449. char s[100] = "";
  450. fr.global = 0;
  451. fr.match = FR_GLOBAL;
  452. if (isbot)
  453. fr.match |= FR_BOT;
  454. break_down_flags(par, &fr, NULL);
  455. /* If user already exists, ignore command */
  456. shareout_but(idx, "n %s%s %s %s %s\n", isbot ? "-" : "", nick, host, pass, par);
  457. if (strlen(nick) > HANDLEN)
  458. nick[HANDLEN] = 0;
  459. fr.match = FR_GLOBAL;
  460. build_flags(s, &fr, 0);
  461. noshare = 1;
  462. userlist = adduser(userlist, nick, host, pass, 0, isbot);
  463. /* Support for userdefinedflag share - drummer */
  464. u = get_user_by_handle(userlist, nick);
  465. set_user_flagrec(u, &fr, 0);
  466. noshare = 0;
  467. if (conf.bot->hub)
  468. putlog(LOG_CMDS, "@", "%s: newuser %s %s", dcc[idx].nick, nick, s);
  469. write_userfile(-1);
  470. }
  471. }
  472. }
  473. static void
  474. share_killuser(int idx, char *par)
  475. {
  476. struct userrec *u = NULL;
  477. /* If user is a share bot, ignore command */
  478. if ((dcc[idx].status & STAT_SHARE) && (u = get_user_by_handle(userlist, par))) {
  479. noshare = 1;
  480. check_this_user(u->handle, 1, NULL);
  481. if (deluser(par)) {
  482. shareout_but(idx, "k %s\n", par);
  483. if (conf.bot->hub)
  484. putlog(LOG_CMDS, "@", "%s: killuser %s", dcc[idx].nick, par);
  485. write_userfile(-1);
  486. }
  487. noshare = 0;
  488. }
  489. }
  490. static void
  491. share_pls_host(int idx, char *par)
  492. {
  493. if (dcc[idx].status & STAT_SHARE) {
  494. char *hand = NULL;
  495. struct userrec *u = NULL;
  496. hand = newsplit(&par);
  497. if ((u = get_user_by_handle(userlist, hand))) {
  498. shareout_but(idx, "+h %s %s\n", hand, par);
  499. set_user(&USERENTRY_HOSTS, u, par);
  500. if (conf.bot->hub)
  501. putlog(LOG_CMDS, "@", "%s: +host %s %s", dcc[idx].nick, hand, par);
  502. else
  503. check_this_user(u->handle, 0, NULL);
  504. }
  505. }
  506. }
  507. static void
  508. share_pls_bothost(int idx, char *par)
  509. {
  510. if (dcc[idx].status & STAT_SHARE) {
  511. char *hand = NULL;
  512. struct userrec *u = NULL;
  513. hand = newsplit(&par);
  514. u = get_user_by_handle(userlist, hand);
  515. if (!(dcc[idx].status & STAT_GETTING))
  516. shareout_but(idx, "+bh %s %s\n", hand, par);
  517. /* Add bot to userlist if not there */
  518. if (u) {
  519. if (!u->bot)
  520. return; /* ignore */
  521. set_user(&USERENTRY_HOSTS, u, par);
  522. clear_chanlist();
  523. } else {
  524. userlist = adduser(userlist, hand, par, "-", 0, 1);
  525. }
  526. if (conf.bot->hub && !(dcc[idx].status & STAT_GETTING))
  527. putlog(LOG_CMDS, "@", "%s: +host %s %s", dcc[idx].nick, hand, par);
  528. }
  529. }
  530. static void
  531. share_mns_host(int idx, char *par)
  532. {
  533. if (dcc[idx].status & STAT_SHARE) {
  534. char *hand = NULL;
  535. struct userrec *u = NULL;
  536. hand = newsplit(&par);
  537. if ((u = get_user_by_handle(userlist, hand))) {
  538. shareout_but(idx, "-h %s %s\n", hand, par);
  539. noshare = 1;
  540. delhost_by_handle(hand, par);
  541. noshare = 0;
  542. if (conf.bot->hub)
  543. putlog(LOG_CMDS, "@", "%s: -host %s %s", dcc[idx].nick, hand, par);
  544. else
  545. check_this_user(hand, 2, par);
  546. }
  547. }
  548. }
  549. static void
  550. share_change(int idx, char *par)
  551. {
  552. if (dcc[idx].status & STAT_SHARE) {
  553. char *key = NULL, *hand = NULL;
  554. struct userrec *u = NULL;
  555. struct user_entry_type *uet = NULL;
  556. struct user_entry *e = NULL;
  557. key = newsplit(&par);
  558. hand = newsplit(&par);
  559. u = get_user_by_handle(userlist, hand);
  560. if (!(uet = find_entry_type(key)))
  561. /* If it's not a supported type, forget it */
  562. putlog(LOG_ERROR, "*", "Ignore ch %s from %s (unknown type)", key, dcc[idx].nick);
  563. else {
  564. if (!(dcc[idx].status & STAT_GETTING))
  565. shareout_but(idx, "c %s %s %s\n", key, hand, par);
  566. noshare = 1;
  567. if (!u && (uet == &USERENTRY_BOTADDR)) {
  568. char pass[30] = "";
  569. makepass(pass);
  570. userlist = adduser(userlist, hand, "none", pass, 0, 1);
  571. u = get_user_by_handle(userlist, hand);
  572. } else if (!u) {
  573. noshare = 0;
  574. return;
  575. }
  576. if (uet->got_share) {
  577. if (!(e = find_user_entry(uet, u))) {
  578. e = (struct user_entry *) calloc(1, sizeof(struct user_entry));
  579. e->type = uet;
  580. e->name = NULL;
  581. e->u.list = NULL;
  582. list_insert((&(u->entries)), e);
  583. }
  584. uet->got_share(u, e, par, idx);
  585. if (!e->u.list) {
  586. list_delete((struct list_type **) &(u->entries), (struct list_type *) e);
  587. free(e);
  588. }
  589. }
  590. noshare = 0;
  591. }
  592. if (uet == &USERENTRY_BOTADDR) {
  593. write_userfile(-1);
  594. }
  595. }
  596. }
  597. static void
  598. share_clearhosts(int idx, char *par)
  599. {
  600. if (dcc[idx].status & STAT_SHARE) {
  601. char *hand = newsplit(&par);
  602. struct userrec *u = get_user_by_handle(userlist, hand);
  603. if (u) {
  604. shareout_but(idx, "ch %s\n", u->handle);
  605. if (!conf.bot->hub && server_online)
  606. check_this_user(u->handle, 1, NULL);
  607. noshare = 1;
  608. set_user(&USERENTRY_HOSTS, u, (void *) "none");
  609. noshare = 0;
  610. }
  611. }
  612. }
  613. static void
  614. share_chchinfo(int idx, char *par)
  615. {
  616. if (dcc[idx].status & STAT_SHARE) {
  617. char *hand = NULL, *chan = NULL;
  618. struct chanset_t *cst = NULL;
  619. struct userrec *u = NULL;
  620. hand = newsplit(&par);
  621. if ((u = get_user_by_handle(userlist, hand))) {
  622. chan = newsplit(&par);
  623. cst = findchan_by_dname(chan);
  624. if (cst) {
  625. shareout_but(idx, "chchinfo %s %s %s\n", hand, chan, par);
  626. noshare = 1;
  627. set_handle_chaninfo(userlist, hand, chan, par);
  628. noshare = 0;
  629. if (conf.bot->hub)
  630. putlog(LOG_CMDS, "@", "%s: change info %s %s", dcc[idx].nick, chan, hand);
  631. }
  632. }
  633. }
  634. }
  635. static void share_mns_mask(int idx, char *par)
  636. {
  637. if (dcc[idx].status & STAT_SHARE) {
  638. char *types = newsplit(&par);
  639. const char type = types[0];
  640. shareout_but(idx, "-m %c %s\n", type, par);
  641. if (conf.bot->hub)
  642. putlog(LOG_CMDS, "@", "%s: cancel %s %s", dcc[idx].nick, type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite", par);
  643. str_unescape(par, '\\');
  644. noshare = 1;
  645. if (u_delmask(type, NULL, par, 1) > 0) {
  646. if (!conf.bot->hub) {
  647. struct chanset_t *chan = NULL;
  648. masklist *channel_list = NULL;
  649. for (chan = chanset; chan; chan = chan->next) {
  650. channel_list = (type == 'b' ? chan->channel.ban : type == 'e' ?
  651. chan->channel.exempt : chan->channel.invite);
  652. if (channel_list && ismasked(channel_list, par))
  653. add_delay(chan, '-', type, par);
  654. }
  655. }
  656. }
  657. noshare = 0;
  658. }
  659. }
  660. static void share_mns_maskchan(int idx, char *par)
  661. {
  662. if (dcc[idx].status & STAT_SHARE) {
  663. char *types = newsplit(&par);
  664. const char type = types[0];
  665. char *chname = NULL;
  666. struct chanset_t *chan = NULL;
  667. int value = 0;
  668. chname = newsplit(&par);
  669. chan = findchan_by_dname(chname);
  670. if (chan) {
  671. shareout_but(idx, "-mc %c %s %s\n", type, chname, par);
  672. if (conf.bot->hub)
  673. putlog(LOG_CMDS, "@", "%s: cancel %s %s on %s", dcc[idx].nick,
  674. type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite", par, chname);
  675. str_unescape(par, '\\');
  676. noshare = 1;
  677. value = u_delmask(type, chan, par, 1);
  678. noshare = 0;
  679. if (!conf.bot->hub && value > 0)
  680. add_delay(chan, '-', type, par);
  681. }
  682. }
  683. }
  684. static void share_pls_mask(int idx, char *par)
  685. {
  686. if (dcc[idx].status & STAT_SHARE) {
  687. char *types = newsplit(&par);
  688. const char type = types[0];
  689. const char *str_type = (type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite");
  690. time_t expire_time;
  691. char *mask = NULL, *tm = NULL, *from = NULL;
  692. int flags = 0;
  693. bool stick = 0;
  694. shareout_but(idx, "+m %c %s\n", type, par);
  695. mask = newsplit(&par);
  696. str_unescape(mask, '\\');
  697. tm = newsplit(&par);
  698. from = newsplit(&par);
  699. if (strchr(from, 's')) {
  700. flags |= MASKREC_STICKY;
  701. stick = 1;
  702. }
  703. if (strchr(from, 'p'))
  704. flags |= MASKREC_PERM;
  705. from = newsplit(&par);
  706. expire_time = (time_t) atoi(tm);
  707. if (expire_time != 0L)
  708. expire_time += now;
  709. noshare = 1;
  710. u_addmask(type, NULL, mask, from, par, expire_time, flags);
  711. noshare = 0;
  712. if (conf.bot->hub)
  713. putlog(LOG_CMDS, "@", "%s: global %s %s (%s:%s)", dcc[idx].nick, str_type, mask, from, par);
  714. else {
  715. for (struct chanset_t *chan = chanset; chan != NULL; chan = chan->next)
  716. check_this_mask(type, chan, mask, stick);
  717. }
  718. }
  719. }
  720. static void share_pls_maskchan(int idx, char *par)
  721. {
  722. if (dcc[idx].status & STAT_SHARE) {
  723. char *types = newsplit(&par);
  724. const char type = types[0];
  725. const char *str_type = (type == 'b' ? "ban" : type == 'e' ? "exempt" : "invite");
  726. time_t expire_time;
  727. int flags = 0;
  728. char *mask = NULL, *tm = NULL, *chname = NULL, *from = NULL;
  729. bool stick = 0;
  730. struct chanset_t *chan = NULL;
  731. mask = newsplit(&par);
  732. tm = newsplit(&par);
  733. chname = newsplit(&par);
  734. chan = findchan_by_dname(chname);
  735. if (chan) {
  736. shareout_but(idx, "+mc %c %s %s %s %s\n", type, mask, tm, chname, par);
  737. str_unescape(mask, '\\');
  738. from = newsplit(&par);
  739. if (strchr(from, 's')) {
  740. flags |= MASKREC_STICKY;
  741. stick = 1;
  742. }
  743. if (strchr(from, 'p'))
  744. flags |= MASKREC_PERM;
  745. from = newsplit(&par);
  746. if (conf.bot->hub)
  747. putlog(LOG_CMDS, "@", "%s: %s %s on %s (%s:%s)", dcc[idx].nick, str_type, mask, chname, from, par);
  748. expire_time = (time_t) atoi(tm);
  749. if (expire_time != 0L)
  750. expire_time += now;
  751. noshare = 1;
  752. u_addmask(type, chan, mask, from, par, expire_time, flags);
  753. noshare = 0;
  754. if (!conf.bot->hub)
  755. check_this_mask(type, chan, mask, stick);
  756. }
  757. }
  758. }
  759. static void
  760. share_mns_ignore(int idx, char *par)
  761. {
  762. if (dcc[idx].status & STAT_SHARE) {
  763. shareout_but(idx, "-i %s\n", par);
  764. str_unescape(par, '\\');
  765. if (conf.bot->hub)
  766. putlog(LOG_CMDS, "@", "%s: cancel ignore %s", dcc[idx].nick, par);
  767. noshare = 1;
  768. delignore(par);
  769. noshare = 0;
  770. }
  771. }
  772. /* +i <host> +<seconds-left> <from> <note>
  773. */
  774. static void
  775. share_pls_ignore(int idx, char *par)
  776. {
  777. if (dcc[idx].status & STAT_SHARE) {
  778. time_t expire_time;
  779. char *ign = NULL, *from = NULL, *ts = NULL;
  780. shareout_but(idx, "+i %s\n", par);
  781. ign = newsplit(&par);
  782. str_unescape(ign, '\\');
  783. ts = newsplit(&par);
  784. if (!atoi(ts))
  785. expire_time = 0L;
  786. else
  787. expire_time = now + atoi(ts);
  788. from = newsplit(&par);
  789. if (strchr(from, 'p'))
  790. expire_time = 0;
  791. from = newsplit(&par);
  792. if (strlen(from) > HANDLEN)
  793. from[HANDLEN] = 0;
  794. par[65] = 0;
  795. if (conf.bot->hub)
  796. putlog(LOG_CMDS, "@", "%s: ignore %s (%s: %s)", dcc[idx].nick, ign, from, par);
  797. noshare = 1;
  798. addignore(ign, from, (const char *) par, expire_time);
  799. noshare = 0;
  800. }
  801. }
  802. static void
  803. share_ufno(int idx, char *par)
  804. {
  805. putlog(LOG_BOTS, "@", "User file rejected by %s: %s", dcc[idx].nick, par);
  806. dcc[idx].status &= ~STAT_OFFERED;
  807. if (!(dcc[idx].status & STAT_GETTING))
  808. dcc[idx].status &= ~(STAT_SHARE | STAT_AGGRESSIVE);
  809. }
  810. static void
  811. share_ufyes(int idx, char *par)
  812. {
  813. if (dcc[idx].status & STAT_OFFERED) {
  814. dcc[idx].status &= ~STAT_OFFERED;
  815. dcc[idx].status |= STAT_SHARE;
  816. dcc[idx].status |= STAT_SENDING;
  817. dcc[idx].u.bot->uff_flags |= (UFF_OVERRIDE | UFF_INVITE | UFF_EXEMPT);
  818. dprintf(idx, "s feats overbots invites exempts\n");
  819. lower_bot_linked(idx);
  820. if (strstr(par, "chdefault"))
  821. dcc[idx].u.bot->uff_flags |= UFF_CHDEFAULT;
  822. if (strstr(par, "stream")) {
  823. updatebot(-1, dcc[idx].nick, '+', 0, 0, 0, NULL);
  824. /* Start up a tbuf to queue outgoing changes for this bot until the
  825. * userlist is done transferring.
  826. */
  827. new_tbuf(dcc[idx].nick);
  828. /* override shit removed here */
  829. q_tbuf(dcc[idx].nick, "s !\n");
  830. dcc[idx].status |= STAT_SENDING;
  831. stream_send_users(idx);
  832. dump_resync(idx);
  833. dcc[idx].status &= ~STAT_SENDING;
  834. } else
  835. start_sending_users(idx);
  836. putlog(LOG_BOTS, "@", "Sending user file send request to %s", dcc[idx].nick);
  837. }
  838. }
  839. static void
  840. share_userfileq(int idx, char *par)
  841. {
  842. flush_tbuf(dcc[idx].nick);
  843. if (bot_aggressive_to(dcc[idx].user)) {
  844. putlog(LOG_ERRORS, "*", "%s offered user transfer - I'm supposed to be aggressive to it [likely a hack]", dcc[idx].nick);
  845. dprintf(idx, "s un I have you marked for Agressive sharing.\n");
  846. botunlink(-2, dcc[idx].nick, "I'm aggressive to you");
  847. } else {
  848. bool ok = 1;
  849. for (int i = 0; i < dcc_total; i++)
  850. if (dcc[i].type && dcc[i].type->flags & DCT_BOT) {
  851. if ((dcc[i].status & STAT_SHARE) && (dcc[i].status & STAT_AGGRESSIVE) && (i != idx)) {
  852. ok = 0;
  853. break;
  854. }
  855. }
  856. if (!ok)
  857. dprintf(idx, "s un Already sharing.\n");
  858. else {
  859. dcc[idx].u.bot->uff_flags |= (UFF_OVERRIDE | UFF_INVITE | UFF_EXEMPT);
  860. dprintf(idx, "s uy overbots invites exempts stream chdefault\n");
  861. /* Set stat-getting to astatic void race condition (robey 23jun1996) */
  862. dcc[idx].status |= STAT_SHARE | STAT_GETTING | STAT_AGGRESSIVE;
  863. if (conf.bot->hub)
  864. putlog(LOG_BOTS, "*", "Downloading user file from %s", dcc[idx].nick);
  865. else
  866. putlog(LOG_BOTS, "*", "Downloading user file via uplink.");
  867. }
  868. }
  869. }
  870. /* us <ip> <port> <length>
  871. */
  872. static void
  873. share_ufsend(int idx, char *par)
  874. {
  875. if (bot_aggressive_to(dcc[idx].user)) {
  876. putlog(LOG_ERRORS, "*", "%s attempted to start sending userfile [compat] - I'm supposed to be aggressive to it [likely a hack]", dcc[idx].nick);
  877. dprintf(idx, "s un I have you marked for Agressive sharing.\n");
  878. botunlink(-2, dcc[idx].nick, "I'm aggressive to you");
  879. return;
  880. }
  881. char *port = NULL, *ip = NULL;
  882. char s[1024] = "";
  883. int i, sock;
  884. FILE *f = NULL;
  885. char rand[7] = "";
  886. make_rand_str(rand, sizeof(rand) - 1, 0);
  887. simple_snprintf(s, sizeof(s), "%s.share.%s", tempdir, rand);
  888. //mktemp(s); //Although safe here, g++ complains too much.
  889. if (!(b_status(idx) & STAT_SHARE)) {
  890. dprintf(idx, "s e You didn't ask; you just started sending.\n");
  891. dprintf(idx, "s e Ask before sending the userfile.\n");
  892. zapfbot(idx);
  893. } else if (dcc_total == max_dcc) {
  894. putlog(LOG_MISC, "@", "NO MORE DCC CONNECTIONS -- can't grab userfile");
  895. dprintf(idx, "s e I can't open a DCC to you; I'm full.\n");
  896. zapfbot(idx);
  897. } else if (!(f = fopen(s, "wb"))) {
  898. putlog(LOG_MISC, "@", "CAN'T WRITE USERFILE DOWNLOAD FILE!");
  899. zapfbot(idx);
  900. } else {
  901. ip = newsplit(&par);
  902. port = newsplit(&par);
  903. #ifdef USE_IPV6
  904. sock = getsock(SOCK_BINARY, AF_INET);
  905. #else
  906. sock = getsock(SOCK_BINARY); /* Don't buffer this -> mark binary. */
  907. #endif /* USE_IPV6 */
  908. int open_telnet_return = 0;
  909. if (sock < 0 || (open_telnet_return = open_telnet_dcc(sock, ip, port)) < 0) {
  910. fclose(f);
  911. if (open_telnet_return != -1 && sock != -1)
  912. killsock(sock);
  913. putlog(LOG_BOTS, "@", "Asynchronous connection failed!");
  914. dprintf(idx, "s e Can't connect to you!\n");
  915. zapfbot(idx);
  916. } else {
  917. putlog(LOG_DEBUG, "@", "Connecting to %s:%s for userfile.", ip, port);
  918. i = new_dcc(&DCC_FORK_SEND, sizeof(struct xfer_info));
  919. dcc[i].addr = my_atoul(ip);
  920. dcc[i].port = atoi(port);
  921. strlcpy(dcc[i].nick, "*users", sizeof(dcc[i].nick));
  922. dcc[i].u.xfer->filename = strdup(s);
  923. dcc[i].u.xfer->origname = dcc[i].u.xfer->filename;
  924. dcc[i].u.xfer->length = atoi(par);
  925. dcc[i].u.xfer->f = f;
  926. dcc[i].sock = sock;
  927. strlcpy(dcc[i].host, dcc[idx].nick, sizeof(dcc[i].host));
  928. dcc[idx].status |= STAT_GETTING;
  929. }
  930. }
  931. }
  932. static void
  933. share_version(int idx, char *par)
  934. {
  935. /* Cleanup any share flags */
  936. dcc[idx].status &= ~(STAT_SHARE | STAT_GETTING | STAT_SENDING | STAT_OFFERED | STAT_AGGRESSIVE);
  937. dcc[idx].u.bot->uff_flags |= (UFF_OVERRIDE | UFF_INVITE | UFF_EXEMPT);
  938. if (bot_aggressive_to(dcc[idx].user)) {
  939. dprintf(idx, "s u?\n");
  940. dcc[idx].status |= STAT_OFFERED;
  941. }
  942. // else higher_bot_linked(idx);
  943. }
  944. void
  945. hook_read_userfile()
  946. {
  947. if (!noshare) {
  948. for (int i = 0; i < dcc_total; i++) {
  949. if (dcc[i].type && (dcc[i].type->flags & DCT_BOT) && (dcc[i].status & STAT_SHARE) && !(dcc[i].status & STAT_AGGRESSIVE)
  950. && (1)) {
  951. /* Cancel any existing transfers */
  952. if (dcc[i].status & STAT_SENDING) {
  953. cancel_user_xfer_staylinked = 1;
  954. cancel_user_xfer(i, 0);
  955. }
  956. dprintf(i, "s u?\n");
  957. dcc[i].status |= STAT_OFFERED;
  958. }
  959. }
  960. }
  961. }
  962. static void
  963. share_endstartup(int idx, char *par)
  964. {
  965. dcc[idx].status &= ~STAT_GETTING;
  966. // Share any local changes out
  967. dump_resync(idx);
  968. /* Send to any other sharebots */
  969. if (conf.bot->hub || conf.bot->localhub) {
  970. have_linked_to_hub = 1;
  971. hook_read_userfile();
  972. }
  973. if (!conf.bot->hub) {
  974. /* Our hostmask may have been updated on connect, but the new userfile may not have it. */
  975. check_hostmask();
  976. }
  977. }
  978. static void
  979. share_end(int idx, char *par)
  980. {
  981. putlog(LOG_BOTS, "*", "Ending sharing with %s (%s).", dcc[idx].nick, par);
  982. cancel_user_xfer_staylinked = 1;
  983. cancel_user_xfer(idx, 0);
  984. dcc[idx].status &= ~(STAT_SHARE | STAT_GETTING | STAT_SENDING | STAT_OFFERED | STAT_AGGRESSIVE);
  985. dcc[idx].u.bot->uff_flags = 0;
  986. }
  987. static void share_userfile_line(int idx, char *par) {
  988. if (stream_in && (dcc[idx].status & STAT_GETTING)) {
  989. char *size = newsplit(&par);
  990. (*stream_in) << bd::String(par, atoi(size));
  991. (*stream_in) << '\n';
  992. }
  993. }
  994. static void share_userfile_start(int idx, char *par) {
  995. if (bot_aggressive_to(dcc[idx].user)) {
  996. putlog(LOG_ERRORS, "*", "%s attempted to initiate user transfer - I'm supposed to be aggressive to it [likely a hack]", dcc[idx].nick);
  997. dprintf(idx, "s un I have you marked for Agressive sharing.\n");
  998. botunlink(-2, dcc[idx].nick, "I'm aggressive to you");
  999. return;
  1000. }
  1001. dcc[idx].status |= STAT_GETTING;
  1002. /* Start up a tbuf to queue outgoing changes for this bot until the
  1003. * userlist is done transferring.
  1004. */
  1005. new_tbuf(dcc[idx].nick);
  1006. stream_in = new bd::Stream();
  1007. }
  1008. static void share_userfile_end(int idx, char *par) {
  1009. if (bot_aggressive_to(dcc[idx].user)) {
  1010. putlog(LOG_ERRORS, "*", "%s attempted to end user transfer - I'm supposed to be aggressive to it [likely a hack]", dcc[idx].nick);
  1011. dprintf(idx, "s un I have you marked for Agressive sharing.\n");
  1012. botunlink(-2, dcc[idx].nick, "I'm aggressive to you");
  1013. return;
  1014. }
  1015. stream_in->seek(0, SEEK_SET);
  1016. share_read_stream(idx, *stream_in);
  1017. delete stream_in;
  1018. }
  1019. /* Note: these MUST be sorted. */
  1020. static botcmd_t C_share[] = {
  1021. {"!", share_endstartup, 0},
  1022. {"+bh", share_pls_bothost, 0},
  1023. {"+cr", share_pls_chrec, 0},
  1024. {"+h", share_pls_host, 0},
  1025. {"+i", share_pls_ignore, 0},
  1026. {"+m", share_pls_mask, 0},
  1027. {"+mc", share_pls_maskchan, 0},
  1028. {"-cr", share_mns_chrec, 0},
  1029. {"-h", share_mns_host, 0},
  1030. {"-i", share_mns_ignore, 0},
  1031. {"-m", share_mns_mask, 0},
  1032. {"-mc", share_mns_maskchan, 0},
  1033. {"a", share_chattr, 0},
  1034. {"c", share_change, 0},
  1035. {"ch", share_clearhosts, 0},
  1036. {"chchinfo", share_chchinfo, 0},
  1037. {"e", share_end, 0},
  1038. {"h", share_chhand, 0},
  1039. {"k", share_killuser, 0},
  1040. {"l", share_userfile_line, 0},
  1041. {"le", share_userfile_end, 0},
  1042. {"ls", share_userfile_start, 0},
  1043. {"ms", share_stick_mask, 0},
  1044. {"n", share_newuser, 0},
  1045. {"u?", share_userfileq, 0},
  1046. {"un", share_ufno, HUB},
  1047. {"us", share_ufsend, 0},
  1048. {"uy", share_ufyes, HUB},
  1049. {"v", share_version, 0},
  1050. {NULL, NULL, 0}
  1051. };
  1052. void
  1053. sharein(int idx, char *msg)
  1054. {
  1055. char *code = newsplit(&msg);
  1056. const botcmd_t *cmd = search_botcmd_t((const botcmd_t*)&C_share, code, lengthof(C_share) - 1);
  1057. if (cmd) {
  1058. /* Found a match */
  1059. (cmd->func) (idx, msg);
  1060. }
  1061. }
  1062. void
  1063. shareout(const char *format, ...)
  1064. {
  1065. char s[601] = "";
  1066. int l;
  1067. va_list va;
  1068. va_start(va, format);
  1069. strlcpy(s, "s ", 3);
  1070. if ((l = egg_vsnprintf(s + 2, 509, format, va)) < 0)
  1071. s[2 + (l = 509)] = 0;
  1072. va_end(va);
  1073. for (int i = 0; i < dcc_total; i++) {
  1074. if (dcc[i].type && (dcc[i].type->flags & DCT_BOT) &&
  1075. (dcc[i].status & STAT_SHARE) && !(dcc[i].status & (STAT_GETTING | STAT_SENDING))) {
  1076. tputs(dcc[i].sock, s, l + 2);
  1077. }
  1078. }
  1079. q_resync(s);
  1080. }
  1081. void
  1082. shareout_prot(struct userrec *u, const char *format, ...)
  1083. {
  1084. char s[601] = "";
  1085. int l;
  1086. va_list va;
  1087. va_start(va, format);
  1088. strlcpy(s, "s ", 3);
  1089. if ((l = egg_vsnprintf(s + 2, 509, format, va)) < 0)
  1090. s[2 + (l = 509)] = 0;
  1091. va_end(va);
  1092. int localhub = nextbot(u->handle);
  1093. for (int i = 0; i < dcc_total; i++) {
  1094. if (dcc[i].type && (dcc[i].type->flags & DCT_BOT) &&
  1095. (dcc[i].status & STAT_SHARE) && !(dcc[i].status & (STAT_GETTING | STAT_SENDING)) &&
  1096. /* only send to hubs, the bot itself, or the localhub in the chain */
  1097. (dcc[i].hub || dcc[i].user == u || (localhub != -1 && i == localhub))) {
  1098. tputs(dcc[i].sock, s, l + 2);
  1099. }
  1100. }
  1101. q_resync(s);
  1102. }
  1103. static void
  1104. shareout_but(int x, const char *format, ...)
  1105. {
  1106. int l;
  1107. char s[601] = "";
  1108. va_list va;
  1109. va_start(va, format);
  1110. strlcpy(s, "s ", 3);
  1111. if ((l = egg_vsnprintf(s + 2, 509, format, va)) < 0)
  1112. s[2 + (l = 509)] = 0;
  1113. va_end(va);
  1114. for (int i = 0; i < dcc_total; i++) {
  1115. if (dcc[i].type && (dcc[i].type->flags & DCT_BOT) && (i != x) &&
  1116. (dcc[i].status & STAT_SHARE) && (!(dcc[i].status & STAT_GETTING)) &&
  1117. (!(dcc[i].status & STAT_SENDING))) {
  1118. tputs(dcc[i].sock, s, l + 2);
  1119. }
  1120. }
  1121. q_resync_but(s, dcc[x].nick);
  1122. }
  1123. /* Flush all tbufs older than 15 minutes.
  1124. */
  1125. static void
  1126. check_expired_tbufs()
  1127. {
  1128. tandbuf *t = NULL, *tnext = NULL;
  1129. for (t = tbuf; t; t = tnext) {
  1130. tnext = t->next;
  1131. if ((now - t->timer) > 300) {
  1132. putlog(LOG_BOTS, "*", "Flushing resync buffer for clonebot %s.", t->bot);
  1133. del_tbuf(t);
  1134. }
  1135. }
  1136. /* Resend userfile requests */
  1137. for (int i = 0; i < dcc_total; i++) {
  1138. if (dcc[i].type && dcc[i].type->flags & DCT_BOT) {
  1139. if (dcc[i].status & STAT_OFFERED) {
  1140. if (now - dcc[i].timeval > 120) {
  1141. if (dcc[i].user && bot_aggressive_to(dcc[i].user))
  1142. dprintf(i, "s u?\n");
  1143. /* ^ send it again in case they missed it */
  1144. }
  1145. /* If it's a share bot that hasnt been sharing, ask again */
  1146. } else if (!(dcc[i].status & STAT_SHARE)) {
  1147. if (dcc[i].user && bot_aggressive_to(dcc[i].user)) {
  1148. dprintf(i, "s u?\n");
  1149. dcc[i].status |= STAT_OFFERED;
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. /* Erase old user list, switch to new one.
  1156. */
  1157. void
  1158. finish_share(int idx)
  1159. {
  1160. int i, j = -1;
  1161. for (i = 0; i < dcc_total; i++)
  1162. if (dcc[i].type && !strcasecmp(dcc[i].nick, dcc[idx].host) && (dcc[i].type->flags & DCT_BOT)) {
  1163. j = i;
  1164. break;
  1165. }
  1166. if (j == -1)
  1167. return;
  1168. const char salt1[] = SALT1;
  1169. EncryptedStream stream(salt1);
  1170. stream.loadFile(dcc[idx].u.xfer->filename);
  1171. unlink(dcc[idx].u.xfer->filename);
  1172. share_read_stream(j, stream);
  1173. /* compress.mod
  1174. if (!uncompressfile(dcc[idx].u.xfer->filename)) {
  1175. char xx[1024] = "";
  1176. putlog(LOG_BOTS, "*", "A uff parsing function failed for the userfile!");
  1177. unlink(dcc[idx].u.xfer->filename);
  1178. dprintf(j, "bye\n");
  1179. simple_snprintf(xx, sizeof xx, "Disconnected %s (uff error)", dcc[j].nick);
  1180. botnet_send_unlinked(j, dcc[j].nick, xx);
  1181. chatout("*** %s\n", xx);
  1182. killsock(dcc[j].sock);
  1183. lostdcc(j);
  1184. return;
  1185. }
  1186. */
  1187. }
  1188. static void share_read_stream(int idx, bd::Stream& stream) {
  1189. struct userrec *u = NULL, *ou = NULL;
  1190. struct chanset_t *chan = NULL;
  1191. /*
  1192. * This is where we remove all global and channel bans/exempts/invites and
  1193. * ignores since they will be replaced by what our hub gives us.
  1194. */
  1195. noshare = 1;
  1196. while (global_bans)
  1197. u_delmask('b', NULL, global_bans->mask, 1);
  1198. while (global_ign)
  1199. delignore(global_ign->igmask);
  1200. while (global_invites)
  1201. u_delmask('I', NULL, global_invites->mask, 1);
  1202. while (global_exempts)
  1203. u_delmask('e', NULL, global_exempts->mask, 1);
  1204. for (chan = chanset; chan; chan = chan->next) {
  1205. while (chan->bans)
  1206. u_delmask('b', chan, chan->bans->mask, 1);
  1207. while (chan->exempts)
  1208. u_delmask('e', chan, chan->exempts->mask, 1);
  1209. while (chan->invites)
  1210. u_delmask('I', chan, chan->invites->mask, 1);
  1211. }
  1212. noshare = 0;
  1213. ou = userlist; /* Save old user list */
  1214. //userlist = (struct userrec *) -1; /* Do this to prevent .user messups */
  1215. userlist = NULL;
  1216. clear_cached_users();
  1217. struct cmd_pass *old_cmdpass = cmdpass;
  1218. cmdpass = NULL;
  1219. /* Read the transferred userfile. Add entries to u, which already holds
  1220. * the bot entries in non-override mode.
  1221. */
  1222. loading = 1;
  1223. checkchans(0); /* flag all the channels.. */
  1224. Context;
  1225. if (!stream_readuserfile(stream, &u)) { /* read the userfile into 'u' */
  1226. /* FAILURE */
  1227. char xx[1024] = "";
  1228. Context;
  1229. clear_userlist(u); /* Clear new, obsolete, user list. */
  1230. clear_chanlist(); /* Remove all user references from the
  1231. * channel lists. */
  1232. userlist = ou; /* Revert to old user list. */
  1233. lastuser = NULL; /* Reset last accessed user ptr. */
  1234. cache_users();
  1235. cmdpass = old_cmdpass;
  1236. checkchans(2); /* un-flag the channels, we are keeping them.. */
  1237. /* old userlist is now being used, safe to do this stuff... */
  1238. loading = 0;
  1239. putlog(LOG_MISC, "*", "%s", "CAN'T READ NEW USERFILE");
  1240. // dprintf(idx, "bye\n");
  1241. simple_snprintf(xx, sizeof xx, "Disconnected %s (can't read userfile)", dcc[idx].nick);
  1242. botnet_send_unlinked(idx, dcc[idx].nick, xx);
  1243. chatout("*** %s\n", xx);
  1244. killsock(dcc[idx].sock);
  1245. lostdcc(idx);
  1246. return;
  1247. }
  1248. /* SUCCESS! */
  1249. loading = 0;
  1250. clear_chanlist(); /* Remove all user references from the
  1251. * channel lists. */
  1252. userlist = u; /* Set new user list. */
  1253. lastuser = NULL; /* Reset last accessed user ptr. */
  1254. putlog(LOG_BOTS, "*", "%s.", "Userlist transfer complete; switched over");
  1255. /*
  1256. * Migrate:
  1257. * - unshared (got_share == 0) user entries
  1258. */
  1259. clear_userlist(ou);
  1260. /* The userfile we received may just be bogus or missing important users */
  1261. load_internal_users();
  1262. add_myself_to_userlist();
  1263. if (conf.bot->localhub)
  1264. add_child_bots();
  1265. cache_users();
  1266. /* Make sure no removed users/bots are still connected. */
  1267. check_stale_dcc_users();
  1268. write_userfile(-1);
  1269. cmdpass_free(old_cmdpass);
  1270. checkchans(1); /* remove marked channels */
  1271. var_parse_my_botset();
  1272. reaffirm_owners(); /* Make sure my owners are +a */
  1273. updatebot(-1, dcc[idx].nick, '+', 0, 0, 0, NULL);
  1274. send_sysinfo();
  1275. if (restarting && !keepnick) {
  1276. keepnick = 1;
  1277. rehash_monitor_list();
  1278. }
  1279. /* Prevents the server connect from dumping JOIN #chan */
  1280. restarting = 0;
  1281. /* If this is ever changed, do mind the restarting bool as it will prevent 001 from dumping JOINs.. */
  1282. if (reset_chans) {
  1283. if (reset_chans == 2) {
  1284. irc_init();
  1285. putlog(LOG_DEBUG, "*", "Resetting channel info for all channels...");
  1286. for (chan = chanset; chan; chan = chan->next) {
  1287. if (shouldjoin(chan) && channel_pending(chan)) { // Set when reading socksfile
  1288. chan->ircnet_status &= ~(CHAN_PEND); // Reset flags to force a reset
  1289. reset_chan_info(chan);
  1290. }
  1291. }
  1292. } else
  1293. join_chans();
  1294. reset_chans = 0;
  1295. }
  1296. }
  1297. /* Begin the user transfer process.
  1298. */
  1299. static void
  1300. ulsend(int idx, const char* data, size_t datalen)
  1301. {
  1302. char buf[1040] = "";
  1303. size_t len = simple_snprintf(buf, sizeof(buf), "s l %zu %s", datalen-1, data);/* -1 for newline */
  1304. tputs(dcc[idx].sock, buf, len);
  1305. }
  1306. static void
  1307. stream_send_users(int idx)
  1308. {
  1309. bd::Stream stream;
  1310. bool old = 0;
  1311. /* FIXME: Remove after 1.2.15 */
  1312. if (idx != -1 && !(dcc[idx].u.bot->uff_flags & UFF_CHDEFAULT)) /* channel 'default' */
  1313. old = 2;
  1314. stream_writeuserfile(stream, userlist, old);
  1315. stream.seek(0, SEEK_SET);
  1316. dprintf(idx, "s ls\n");
  1317. bd::String buf;
  1318. while (stream.tell() < stream.length()) {
  1319. buf = stream.getline(1024);
  1320. ulsend(idx, buf.c_str(), buf.length());
  1321. }
  1322. dprintf(idx, "s le\n");
  1323. putlog(LOG_BOTS, "*", "Completed userfile transfer to %s.", dcc[idx].nick);
  1324. }
  1325. static void
  1326. start_sending_users(int idx)
  1327. {
  1328. char share_file[1024] = "";
  1329. int i = 1, j = -1;
  1330. char rand[7] = "";
  1331. make_rand_str(rand, sizeof(rand) - 1, 0);
  1332. simple_snprintf(share_file, sizeof(share_file), "%s.share.%s", tempdir, rand);
  1333. //mktemp(share_file); //Although safe here, g++ complains too much.
  1334. /* FIXME: REMOVE AFTER 1.2.14 */
  1335. bool old = 0;
  1336. tand_t* bot = idx != -1 ? findbot(dcc[idx].nick) : NULL;
  1337. if (bot && bot->buildts < 1175102242) /* flood-* hacks */
  1338. old = 1;
  1339. /* FIXME: Remove after 1.2.15 */
  1340. if (idx != -1 && !(dcc[idx].u.bot->uff_flags & UFF_CHDEFAULT)) /* channel 'default' */
  1341. old = 2;
  1342. const char salt1[] = SALT1;
  1343. EncryptedStream stream(salt1);
  1344. stream_writeuserfile(stream, userlist, old);
  1345. stream.setFlags(ENC_KEEP_NEWLINES|ENC_AES_256_ECB|ENC_BASE64_BROKEN|ENC_NO_HEADER);
  1346. if (stream.writeFile(share_file)) {
  1347. putlog(LOG_MISC, "*", "ERROR writing user file to transfer.");
  1348. unlink(share_file);
  1349. }
  1350. /* compress.mod
  1351. if (!compress_file(share_file, compress_level)) {
  1352. unlink(share_file);
  1353. dprintf(idx, "s e %s\n", "uff parsing failed");
  1354. putlog(LOG_BOTS, "*", "uff parsing failed");
  1355. dcc[idx].status &= ~(STAT_SHARE | STAT_SENDING | STAT_AGGRESSIVE);
  1356. return;
  1357. }
  1358. */
  1359. if ((i = raw_dcc_send(share_file, "*users", "(users)", &j)) > 0) {
  1360. /* FIXME: the bot should be unlinked at this point */
  1361. unlink(share_file);
  1362. dprintf(idx, "s e %s\n", "Can't send userfile to you (internal error)");
  1363. putlog(LOG_BOTS, "*", "%s -- can't send userfile",
  1364. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  1365. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  1366. i == DCCSEND_BADFN ? "BAD FILE" : i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  1367. dcc[idx].status &= ~(STAT_SHARE | STAT_SENDING | STAT_AGGRESSIVE);
  1368. } else {
  1369. updatebot(-1, dcc[idx].nick, '+', 0, 0, 0, NULL);
  1370. dcc[idx].status |= STAT_SENDING;
  1371. strlcpy(dcc[j].host, dcc[idx].nick, sizeof(dcc[j].host)); /* Store bot's nick */
  1372. dprintf(idx, "s us %lu %d %lu\n", iptolong(getmyip()), dcc[j].port, dcc[j].u.xfer->length);
  1373. /* Start up a tbuf to queue outgoing changes for this bot until the
  1374. * userlist is done transferring.
  1375. */
  1376. new_tbuf(dcc[idx].nick);
  1377. /* override shit removed here */
  1378. q_tbuf(dcc[idx].nick, "s !\n");
  1379. /* Unlink the file. We don't really care whether this causes problems
  1380. * for NFS setups. It's not worth the trouble.
  1381. */
  1382. unlink(share_file);
  1383. }
  1384. }
  1385. static void (*def_dcc_bot_kill) (int, void *) = 0;
  1386. static void
  1387. cancel_user_xfer(int idx, void *x)
  1388. {
  1389. int i, j = -1;
  1390. if (cancel_user_xfer_staylinked) {
  1391. /* turn off sharing flag */
  1392. updatebot(-1, dcc[idx].nick, '-', 0, 0, 0, NULL);
  1393. }
  1394. flush_tbuf(dcc[idx].nick);
  1395. if (dcc[idx].status & STAT_SHARE) {
  1396. /* look for any transfers from this bot and kill them */
  1397. if (dcc[idx].status & STAT_GETTING) {
  1398. for (i = 0; i < dcc_total; i++)
  1399. if (dcc[i].type && !strcasecmp(dcc[i].host, dcc[idx].nick) &&
  1400. ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND)) == (DCT_FILETRAN | DCT_FILESEND))) {
  1401. j = i;
  1402. break;
  1403. }
  1404. if (j >= 0) {
  1405. killsock(dcc[j].sock);
  1406. unlink(dcc[j].u.xfer->filename);
  1407. lostdcc(j);
  1408. }
  1409. putlog(LOG_BOTS, "*", "(Userlist download aborted.)");
  1410. }
  1411. /* look for any transfers we were sending them */
  1412. if (dcc[idx].status & STAT_SENDING) {
  1413. for (i = 0; i < dcc_total; i++)
  1414. if (dcc[i].type && (!strcasecmp(dcc[i].host, dcc[idx].nick)) &&
  1415. ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND)) == DCT_FILETRAN)) {
  1416. j = i;
  1417. break;
  1418. }
  1419. if (j >= 0) {
  1420. killsock(dcc[j].sock);
  1421. unlink(dcc[j].u.xfer->filename);
  1422. lostdcc(j);
  1423. }
  1424. putlog(LOG_BOTS, "*", "(Userlist transmit aborted.)");
  1425. }
  1426. }
  1427. if (!cancel_user_xfer_staylinked)
  1428. def_dcc_bot_kill(idx, x);
  1429. cancel_user_xfer_staylinked = 0;
  1430. }
  1431. void
  1432. share_report(int idx, int details)
  1433. {
  1434. int i, j;
  1435. if (details) {
  1436. for (i = 0; i < dcc_total; i++)
  1437. if (dcc[i].type && dcc[i].type == &DCC_BOT) {
  1438. if (dcc[i].status & STAT_GETTING) {
  1439. int ok = 0;
  1440. for (j = 0; j < dcc_total; j++)
  1441. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  1442. == (DCT_FILETRAN | DCT_FILESEND)) && !strcasecmp(dcc[j].host, dcc[i].nick)) {
  1443. dprintf(idx, "Downloading userlist from %s (%d%% done)\n",
  1444. conf.bot->hub ? dcc[i].nick : "[botnet]", (int) (100.0 * ((float) dcc[j].status) / ((float) dcc[j].u.xfer->length)));
  1445. ok = 1;
  1446. break;
  1447. }
  1448. if (!ok)
  1449. dprintf(idx, "Download userlist from %s (negotiating " "botentries)\n", conf.bot->hub ? dcc[i].nick : "[botnet]");
  1450. } else if (dcc[i].status & STAT_SENDING) {
  1451. for (j = 0; j < dcc_total; j++) {
  1452. if (dcc[j].type && ((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  1453. == DCT_FILETRAN)
  1454. && !strcasecmp(dcc[j].host, dcc[i].nick)) {
  1455. if (dcc[j].type == &DCC_GET)
  1456. dprintf(idx, "Sending userlist to %s (%d%% done)\n",
  1457. dcc[i].nick,
  1458. (int) (100.0 * ((float) dcc[j].status) / ((float) dcc[j].u.xfer->length)));
  1459. else
  1460. dprintf(idx, "Sending userlist to %s (waiting for connect)\n", dcc[i].nick);
  1461. }
  1462. }
  1463. } else if (dcc[i].status & STAT_AGGRESSIVE) {
  1464. dprintf(idx, " Passively sharing with %s.\n", conf.bot->hub ? dcc[i].nick : "[botnet]");
  1465. } else if (dcc[i].status & STAT_SHARE) {
  1466. dprintf(idx, " Aggressively sharing with %s.\n", dcc[i].nick);
  1467. }
  1468. }
  1469. }
  1470. }
  1471. void
  1472. share_init()
  1473. {
  1474. if (conf.bot->hub)
  1475. timer_create_secs(60, "check_expired_tbufs", (Function) check_expired_tbufs);
  1476. else
  1477. timer_create_secs(1, "check_delay", (Function) check_delay);
  1478. def_dcc_bot_kill = DCC_BOT.kill;
  1479. DCC_BOT.kill = cancel_user_xfer;
  1480. }
  1481. /* vim: set sts=2 sw=2 ts=8 et: */