share.c 46 KB

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