share.c 39 KB

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