share.c 43 KB

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