share.c 47 KB

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