share.c 44 KB

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