share.c 42 KB

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