share.c 44 KB

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