share.c 43 KB

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