1
0

share.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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)) { /* read the userfile into 'u' */
  1235. /* FAILURE */
  1236. char xx[1024] = "";
  1237. Context;
  1238. unlink(dcc[idx].u.xfer->filename);
  1239. clear_userlist(u); /* Clear new, obsolete, user list. */
  1240. clear_chanlist(); /* Remove all user references from the
  1241. * channel lists. */
  1242. for (i = 0; i < dcc_total; i++)
  1243. dcc[i].user = get_user_by_handle(ou, dcc[i].nick);
  1244. conf.bot->u = get_user_by_handle(ou, conf.bot->nick);
  1245. userlist = ou; /* Revert to old user list. */
  1246. lastuser = NULL; /* Reset last accessed user ptr. */
  1247. checkchans(2); /* un-flag the channels, we are keeping them.. */
  1248. /* old userlist is now being used, safe to do this stuff... */
  1249. loading = 0;
  1250. putlog(LOG_MISC, "*", "%s", USERF_CANTREAD);
  1251. dprintf(idx, "bye\n");
  1252. egg_snprintf(xx, sizeof xx, "Disconnected %s (can't read userfile)", dcc[j].nick);
  1253. botnet_send_unlinked(j, dcc[j].nick, xx);
  1254. chatout("*** %s\n", xx);
  1255. killsock(dcc[j].sock);
  1256. lostdcc(j);
  1257. return;
  1258. }
  1259. /* SUCCESS! */
  1260. unlink(dcc[idx].u.xfer->filename);
  1261. loading = 0;
  1262. clear_chanlist(); /* Remove all user references from the
  1263. * channel lists. */
  1264. userlist = u; /* Set new user list. */
  1265. lastuser = NULL; /* Reset last accessed user ptr. */
  1266. putlog(LOG_BOTS, "*", "%s.", USERF_XFERDONE);
  1267. /*
  1268. * Migrate:
  1269. * - unshared (got_share == 0) user entries
  1270. */
  1271. clear_userlist(ou);
  1272. unlink(dcc[idx].u.xfer->filename); /* Done with you! */
  1273. checkchans(1); /* remove marked channels */
  1274. trigger_cfg_changed();
  1275. reaffirm_owners(); /* Make sure my owners are +a */
  1276. updatebot(-1, dcc[j].nick, '+', 0, 0, NULL);
  1277. }
  1278. #ifdef HUB
  1279. /* Begin the user transfer process.
  1280. */
  1281. static void
  1282. start_sending_users(int idx)
  1283. {
  1284. char share_file[1024] = "";
  1285. int i = 1;
  1286. egg_snprintf(share_file, sizeof share_file, "%s.share.%s.%li", tempdir, dcc[idx].nick, now);
  1287. write_tmp_userfile(share_file, userlist, idx);
  1288. /* compress.mod
  1289. if (!compress_file(share_file, compress_level)) {
  1290. unlink(share_file);
  1291. dprintf(idx, "s e %s\n", "uff parsing failed");
  1292. putlog(LOG_BOTS, "*", "uff parsing failed");
  1293. dcc[idx].status &= ~(STAT_SHARE | STAT_SENDING | STAT_AGGRESSIVE);
  1294. return;
  1295. }
  1296. */
  1297. if ((i = raw_dcc_send(share_file, "*users", "(users)", share_file)) > 0) {
  1298. unlink(share_file);
  1299. dprintf(idx, "s e %s\n", USERF_CANTSEND);
  1300. putlog(LOG_BOTS, "@", "%s -- can't send userfile",
  1301. i == DCCSEND_FULL ? "NO MORE DCC CONNECTIONS" :
  1302. i == DCCSEND_NOSOCK ? "CAN'T OPEN A LISTENING SOCKET" :
  1303. i == DCCSEND_BADFN ? "BAD FILE" : i == DCCSEND_FEMPTY ? "EMPTY FILE" : "UNKNOWN REASON!");
  1304. dcc[idx].status &= ~(STAT_SHARE | STAT_SENDING | STAT_AGGRESSIVE);
  1305. } else {
  1306. updatebot(-1, dcc[idx].nick, '+', 0, 0, NULL);
  1307. dcc[idx].status |= STAT_SENDING;
  1308. i = dcc_total - 1;
  1309. strcpy(dcc[i].host, dcc[idx].nick); /* Store bot's nick */
  1310. dprintf(idx, "s us %lu %d %lu\n", iptolong(natip[0] ? (IP) inet_addr(natip) : getmyip()), dcc[i].port, dcc[i].u.xfer->length);
  1311. /* Unlink the file. We don't really care whether this causes problems
  1312. * for NFS setups. It's not worth the trouble.
  1313. */
  1314. unlink(share_file);
  1315. }
  1316. }
  1317. #endif /* HUB */
  1318. static void (*def_dcc_bot_kill) (int, void *) = 0;
  1319. static void
  1320. cancel_user_xfer(int idx, void *x)
  1321. {
  1322. int i, j, k = 0;
  1323. if (idx < 0) {
  1324. idx = -idx;
  1325. k = 1;
  1326. updatebot(-1, dcc[idx].nick, '-', 0, 0, NULL);
  1327. }
  1328. if (dcc[idx].status & STAT_SHARE) {
  1329. if (dcc[idx].status & STAT_GETTING) {
  1330. j = 0;
  1331. for (i = 0; i < dcc_total; i++)
  1332. if (!egg_strcasecmp(dcc[i].host, dcc[idx].nick) &&
  1333. ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND)) == (DCT_FILETRAN | DCT_FILESEND)))
  1334. j = i;
  1335. if (j != 0) {
  1336. killsock(dcc[j].sock);
  1337. unlink(dcc[j].u.xfer->filename);
  1338. lostdcc(j);
  1339. }
  1340. putlog(LOG_BOTS, "*", "(Userlist download aborted.)");
  1341. }
  1342. if (dcc[idx].status & STAT_SENDING) {
  1343. j = 0;
  1344. for (i = 0; i < dcc_total; i++)
  1345. if ((!egg_strcasecmp(dcc[i].host, dcc[idx].nick)) && ((dcc[i].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  1346. == DCT_FILETRAN))
  1347. j = i;
  1348. if (j != 0) {
  1349. killsock(dcc[j].sock);
  1350. unlink(dcc[j].u.xfer->filename);
  1351. lostdcc(j);
  1352. }
  1353. putlog(LOG_BOTS, "*", "(Userlist transmit aborted.)");
  1354. }
  1355. }
  1356. if (!k)
  1357. def_dcc_bot_kill(idx, x);
  1358. }
  1359. void
  1360. share_report(int idx, int details)
  1361. {
  1362. int i, j;
  1363. if (details) {
  1364. for (i = 0; i < dcc_total; i++)
  1365. if (dcc[i].type == &DCC_BOT) {
  1366. if (dcc[i].status & STAT_GETTING) {
  1367. int ok = 0;
  1368. for (j = 0; j < dcc_total; j++)
  1369. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  1370. == (DCT_FILETRAN | DCT_FILESEND)) && !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  1371. dprintf(idx, "Downloading userlist from %s (%d%% done)\n",
  1372. dcc[i].nick, (int) (100.0 * ((float) dcc[j].status) / ((float) dcc[j].u.xfer->length)));
  1373. ok = 1;
  1374. break;
  1375. }
  1376. if (!ok)
  1377. dprintf(idx, "Download userlist from %s (negotiating " "botentries)\n", dcc[i].nick);
  1378. #ifdef HUB
  1379. } else if (dcc[i].status & STAT_SENDING) {
  1380. for (j = 0; j < dcc_total; j++) {
  1381. if (((dcc[j].type->flags & (DCT_FILETRAN | DCT_FILESEND))
  1382. == DCT_FILETRAN)
  1383. && !egg_strcasecmp(dcc[j].host, dcc[i].nick)) {
  1384. if (dcc[j].type == &DCC_GET)
  1385. dprintf(idx, "Sending userlist to %s (%d%% done)\n",
  1386. dcc[i].nick, (int) (100.0 * ((float) dcc[j].status) / ((float) dcc[j].u.xfer->length)));
  1387. else
  1388. dprintf(idx, "Sending userlist to %s (waiting for connect)\n", dcc[i].nick);
  1389. }
  1390. }
  1391. } else if (dcc[i].status & STAT_AGGRESSIVE) {
  1392. dprintf(idx, " Passively sharing with %s.\n", dcc[i].nick);
  1393. } else if (dcc[i].status & STAT_SHARE) {
  1394. dprintf(idx, " Aggressively sharing with %s.\n", dcc[i].nick);
  1395. #endif /* HUB */
  1396. }
  1397. }
  1398. }
  1399. }
  1400. void
  1401. share_init()
  1402. {
  1403. #ifdef HUB
  1404. timer_create_secs(60, "check_expired_tbufs", (Function) check_expired_tbufs);
  1405. #endif /* HUB */
  1406. #ifdef LEAF
  1407. timer_create_secs(1, "check_delay", (Function) check_delay);
  1408. #endif /* LEAF */
  1409. def_dcc_bot_kill = DCC_BOT.kill;
  1410. DCC_BOT.kill = cancel_user_xfer;
  1411. }