Exemplo n.º 1
0
        private ChannelUser NewUserFromXmlElement(XmlElement element)
        {
            var mask = element.Attributes["mask"].Value;

            // Flags
            var localFlags = element.GetAttribute("localflags");

            if (string.IsNullOrWhiteSpace(localFlags))
            {
                localFlags = null;
            }

            var  subscribedText = element.GetAttribute("subscribed");
            bool subscribed;

            if (!bool.TryParse(subscribedText, out subscribed))
            {
                subscribed = false;
            }

            // TODO: Hack in a delay for stored $a masks until T1236 is fixed
            if (this.freenodeClient.ExtBanTypes == null)
            {
                Thread.Sleep(5000);
            }

            var userMask = new IrcUserMask(mask, this.freenodeClient);

            return(new ChannelUser(userMask)
            {
                LocalFlags = localFlags,
                Subscribed = subscribed
            });
        }
Exemplo n.º 2
0
        // ReSharper disable once UnusedMember.Global
        protected IEnumerable <CommandResponse> RegisterMode()
        {
            if (this.User.Account == null)
            {
                yield return(new CommandResponse
                {
                    Message = "Please identify to NickServ first!",
                    Destination = CommandResponseDestination.PrivateMessage
                });

                yield break;
            }

            var mask = new IrcUserMask(string.Format("$a:{0}", this.User.Account), this.Client);
            var user = new BotUser(mask);

            this.botUserConfiguration.Add(user);
            this.botUserConfiguration.Save();

            yield return(new CommandResponse
            {
                Message = string.Format("Registered using NickServ account {0}.", this.User.Account),
                Destination = CommandResponseDestination.PrivateMessage
            });

            yield return(new CommandResponse
            {
                Message = string.Format(
                    "Please note, to receive notifications via email, you need to provide your email address (=account email <address>). By providing your email address, you agree to the privacy policy: {0}",
                    this.appConfig.PrivacyPolicy),
                Destination = CommandResponseDestination.PrivateMessage
            });
        }
        public bool SubscribeChannel(IrcUserMask mask, IIrcChannel channel)
        {
            var channelUser = channel.Users.FirstOrDefault(x => x.Mask.ToString() == mask.ToString());

            if (channelUser == null)
            {
                channelUser = new ChannelUser(mask);
                channel.Users.Add(channelUser);
            }

            if (channelUser.Subscribed)
            {
                return(false);
            }
            else
            {
                channelUser.Subscribed = true;

                // remove any overrides
                var channelSubscriptions = this.GetUserStalkSubscriptionsInChannel(new BotUser(mask), channel);
                foreach (var subscriptionResult in channelSubscriptions.Where(x => x.Source == SubscriptionSource.Stalk))
                {
                    subscriptionResult.Stalk.Subscribers.RemoveAll(x => x.Mask.Equals(mask));
                }

                return(true);
            }
        }
Exemplo n.º 4
0
        public void LocalSetup()
        {
            this.Setup();

            this.stalkMock     = new Mock <IStalk>();
            this.rcMock        = new Mock <IRecentChange>();
            this.botUser       = new Mock <IBotUser>();
            this.channelConfig = new Mock <IChannelConfiguration>();

            var client = new Mock <IIrcClient>();

            client.Setup(x => x.ExtBanDelimiter).Returns("$");
            client.Setup(x => x.ExtBanTypes).Returns("a");
            var mask = new IrcUserMask("$a:abc", client.Object);

            this.botUser.Setup(x => x.Mask).Returns(mask);

            this.channelConfig.Setup(x => x[It.IsAny <string>()]).Returns(new IrcChannel("foo"));

            this.stalkMock.Setup(s => s.Identifier).Returns("s1");
            this.stalkMock.Setup(s => s.Description).Returns("test desc");
            this.stalkMock.Setup(s => s.ExpiryTime).Returns(DateTime.MaxValue);
            this.stalkMock.Setup(s => s.SearchTree).Returns(new TrueNode());
            this.stalkMock.Setup(s => s.Subscribers).Returns(new List <StalkUser>());

            this.rcMock.Setup(s => s.Url).Returns("http://enwp.org");
            this.rcMock.Setup(s => s.Page).Returns("Foo");
            this.rcMock.Setup(s => s.User).Returns("Me");
            this.rcMock.Setup(s => s.EditSummary).Returns("test");
            this.rcMock.Setup(s => s.SizeDiff).Returns(4);
            this.rcMock.Setup(s => s.EditFlags).Returns("Minor");
        }
Exemplo n.º 5
0
        private IEnumerable <StalkUser> PopulateSubscribers(XmlNodeList xmlElementChildNodes)
        {
            foreach (var elementChildNode in xmlElementChildNodes)
            {
                var e = elementChildNode as XmlElement;
                if (e == null || e.Name != "user")
                {
                    continue;
                }

                var mask = e.Attributes["mask"].Value;

                var inverse = false;

                if (e.Attributes["unsubscribed"] != null)
                {
                    inverse = XmlConvert.ToBoolean(e.Attributes["unsubscribed"].Value);
                }

                // TODO: Hack in a delay for stored $a masks until T1236 is fixed
                if (this.freenodeClient.ExtBanTypes == null)
                {
                    Thread.Sleep(5000);
                }

                var ircUserMask = new IrcUserMask(mask, this.freenodeClient);

                yield return(new StalkUser(ircUserMask, !inverse));
            }
        }
        public void OnReceivedMessage(object sender, MessageReceivedEventArgs e)
        {
            if (e.IsNotice)
            {
                return;
            }

            var source = e.User;

            // first check is performance-related, second is thread safety related.
            if (this.rcUserMaskCache == null)
            {
                lock (this)
                {
                    try
                    {
                        if (this.rcUserMaskCache == null)
                        {
                            this.rcUserMaskCache = new IrcUserMask(this.appConfig.RcUser, e.Client);
                        }
                    }
                    catch (Exception ex)
                    {
                        this.logger.ErrorFormat(ex, "Encountered error building mask, on receipt of {0}", e.Message);
                        return;
                    }
                }
            }

            if (!this.rcUserMaskCache.Matches(source).GetValueOrDefault(false))
            {
                this.logger.WarnFormat(
                    "Received private message from {0} instead of expected {1}!",
                    source.ToString(),
                    this.appConfig.RcUser);
                return;
            }

            var rcMessage = e.Message;

            IRecentChange rc;

            try
            {
                rc = this.rcParser.Parse(rcMessage, e.Target);
            }
            catch (BugException ex)
            {
                this.bugReporter.ReportBug(ex);
                return;
            }
            catch (FormatException ex)
            {
                this.logger.ErrorFormat(ex, "Error processing received message: {0}", e.Message);
                return;
            }

            this.rcHandler.HandleRcEvent(e, rc);
        }
Exemplo n.º 7
0
        public void LocalSetup()
        {
            var client = new Mock <IIrcClient>();

            client.Setup(x => x.ExtBanDelimiter).Returns("$");
            client.Setup(x => x.ExtBanTypes).Returns("ajrxz");

            this.mask = new IrcUserMask("$a:stwalkerster", client.Object);
        }
        public void LocalSetup()
        {
            this.ircClient = new Mock <IIrcClient>();
            this.ircClient.Setup(x => x.ExtBanTypes).Returns(string.Empty);

            this.channel           = new Mock <IIrcChannel>();
            this.stalk             = new Mock <IStalk>();
            this.botUserConfigMock = new Mock <IBotUserConfiguration>();
            this.channelConfigMock = new Mock <IChannelConfiguration>();

            this.channel.Setup(x => x.Identifier).Returns("#channel");
            this.stalk.Setup(x => x.Channel).Returns("#channel");

            this.subscriptionHelper = new SubscriptionHelper(this.LoggerMock.Object, this.botUserConfigMock.Object, this.channelConfigMock.Object);
            this.mask = new IrcUserMask("*!*@*", this.ircClient.Object);
        }
Exemplo n.º 9
0
 internal BotUser(IrcUserMask mask,
                  string flags,
                  string emailAddress,
                  string emailConfirmationToken,
                  string deletionConfirmationToken,
                  bool emailAddressConfirmed,
                  DateTime?emailConfirmationTimestamp,
                  DateTime?deletionConfirmationTimestamp,
                  Guid webGuid,
                  string webPassword)
 {
     this.Mask                          = mask;
     this.GlobalFlags                   = flags;
     this.EmailConfirmationToken        = emailConfirmationToken;
     this.DeletionConfirmationToken     = deletionConfirmationToken;
     this.EmailAddressConfirmed         = emailAddressConfirmed;
     this.EmailConfirmationTimestamp    = emailConfirmationTimestamp;
     this.DeletionConfirmationTimestamp = deletionConfirmationTimestamp;
     this.WebGuid                       = webGuid;
     this.WebPassword                   = webPassword;
     this.emailAddress                  = emailAddress;
 }
Exemplo n.º 10
0
        private void PreCacheOwnerMask(IUser user)
        {
            if (this.ownerMask != null)
            {
                return;
            }

            lock (this)
            {
                if (this.ownerMask != null)
                {
                    return;
                }

                var ircUser = user as IrcUser;
                if (ircUser == null)
                {
                    return;
                }

                var client = ircUser.Client;
                this.ownerMask = new IrcUserMask(this.appConfiguration.Owner, client);
            }
        }
Exemplo n.º 11
0
 public BotUser(IrcUserMask mask)
 {
     this.Mask    = mask;
     this.WebGuid = Guid.NewGuid();
 }
Exemplo n.º 12
0
        public IBotUser NewFromXmlElement(XmlElement element)
        {
            var mask = element.Attributes["mask"].Value;

            // Email attribute
            var email = element.GetAttribute("mail");

            if (string.IsNullOrWhiteSpace(email))
            {
                email = null;
            }

            // Flags
            var globalflags = element.GetAttribute("globalflags");

            if (string.IsNullOrWhiteSpace(globalflags))
            {
                globalflags = null;
            }

            // Email confirmation hash
            var confirmationHash = element.GetAttribute("mailconfirmhash");

            if (string.IsNullOrWhiteSpace(confirmationHash))
            {
                confirmationHash = null;
            }

            // Deletion confirmation hash
            var deleteConfirmationHash = element.GetAttribute("deleteconfirmhash");

            if (string.IsNullOrWhiteSpace(deleteConfirmationHash))
            {
                deleteConfirmationHash = null;
            }

            // web GUID
            var webGuid          = Guid.NewGuid();
            var webGuidAttribute = element.GetAttribute("webguid");

            if (!string.IsNullOrWhiteSpace(webGuidAttribute))
            {
                webGuid = Guid.Parse(webGuidAttribute);
            }

            var webPassword = element.GetAttribute("webpassword");

            if (string.IsNullOrWhiteSpace(webPassword))
            {
                webPassword = null;
            }

            // Mail confirmed
            var  mailConfirmedText = element.GetAttribute("mailconfirmed");
            bool mailConfirmed;

            if (!bool.TryParse(mailConfirmedText, out mailConfirmed))
            {
                mailConfirmed = false;
            }

            var      timeAttribute   = element.Attributes["deletetimestamp"];
            DateTime?deleteTimestamp = null;

            if (timeAttribute != null)
            {
                deleteTimestamp = this.ParseDate(mask, timeAttribute.Value, "delete confirmation timestamp");
            }

            timeAttribute = element.Attributes["emailtimestamp"];
            DateTime?emailTimestamp = null;

            if (timeAttribute != null)
            {
                emailTimestamp = this.ParseDate(mask, timeAttribute.Value, "email confirmation timestamp");
            }

            // TODO: Hack in a delay for stored $a masks until T1236 is fixed
            if (this.freenodeClient.ExtBanTypes == null || this.freenodeClient.ExtBanDelimiter == null)
            {
                Thread.Sleep(5000);
            }

            var userMask = new IrcUserMask(mask, this.freenodeClient);

            var u = new BotUser(
                userMask,
                globalflags,
                email,
                confirmationHash,
                deleteConfirmationHash,
                mailConfirmed,
                emailTimestamp,
                deleteTimestamp,
                webGuid,
                webPassword);

            return(u);
        }
Exemplo n.º 13
0
 public ChannelUser(IrcUserMask mask)
 {
     this.Mask = mask;
 }
Exemplo n.º 14
0
        public bool SubscribeStalk(IrcUserMask mask, IIrcChannel channel, IStalk stalk, out SubscriptionSource source)
        {
            if (channel.Identifier != stalk.Channel)
            {
                throw new Exception("Mismatch between stalk channel and channel!");
            }

            var stalkSubscriber   = stalk.Subscribers.FirstOrDefault(x => x.Mask.ToString() == mask.ToString());
            var channelSubscriber = channel.Users.FirstOrDefault(x => x.Mask.ToString() == mask.ToString());

            this.logger.DebugFormat(
                "Subscription request for {0} to {1} in {2}",
                mask,
                stalk.Identifier,
                channel.Identifier);

            if (stalkSubscriber != null)
            {
                if (stalkSubscriber.Subscribed)
                {
                    if (channelSubscriber != null)
                    {
                        if (channelSubscriber.Subscribed)
                        {
                            // subscribed to channel
                            // subscribed to stalk
                            this.logger.WarnFormat(
                                "Found subscription request from stalk- ({0}) and channel-subscribed ({1}) user ({2})",
                                stalk.Identifier,
                                channel.Identifier,
                                mask);

                            this.logger.DebugFormat(
                                "Unsubscribing from stalk - already subscribed to stalk and channel");
                            stalk.Subscribers.Remove(stalkSubscriber);
                            source = SubscriptionSource.Channel;
                            return(false);
                        }
                        else
                        {
                            // not subscribed to channel
                            // subscribed to stalk
                            this.logger.DebugFormat("Not subscribing - already subscribed to stalk");
                            source = SubscriptionSource.Stalk;
                            return(false);
                        }
                    }
                    else
                    {
                        // not subscribed to channel
                        // subscribed to stalk
                        this.logger.DebugFormat("Not subscribing - already subscribed to stalk");
                        source = SubscriptionSource.Stalk;
                        return(false);
                    }
                }
                else
                {
                    if (channelSubscriber != null)
                    {
                        if (channelSubscriber.Subscribed)
                        {
                            // forcibly unsubscribed from stalk
                            // subscribed to channel
                            this.logger.DebugFormat("Removing forced unsubscribe - already subscribed to channel");
                            stalk.Subscribers.Remove(stalkSubscriber);
                            source = SubscriptionSource.Channel;
                            return(true);
                        }
                        else
                        {
                            // forcibly unsubscribed from stalk
                            // not subscribed to channel
                            this.logger.WarnFormat(
                                "Found subscription request from stalk-force-unsubscribed ({0}) and channel-unsubscribed ({1}) user ({2})",
                                stalk.Identifier,
                                channel.Identifier,
                                mask);
                            this.logger.DebugFormat("Converting forced unsubscribe to stalk subscription");
                            stalkSubscriber.Subscribed = true;
                            source = SubscriptionSource.Stalk;
                            return(true);
                        }
                    }
                    else
                    {
                        // forcibly unsubscribed from stalk
                        // not subscribed to channel
                        this.logger.WarnFormat(
                            "Found subscription request from stalk-force-unsubscribed ({0}) and channel-unsubscribed ({1}) user ({2})",
                            stalk.Identifier,
                            channel.Identifier,
                            mask);
                        this.logger.DebugFormat("Converting forced unsubscribe to stalk subscription");
                        stalkSubscriber.Subscribed = true;
                        source = SubscriptionSource.Stalk;
                        return(true);
                    }
                }
            }
            else
            {
                if (channelSubscriber != null)
                {
                    if (channelSubscriber.Subscribed)
                    {
                        // already subscribed to channel
                        // not subscribed to stalk
                        source = SubscriptionSource.Channel;
                        return(false);
                    }
                    else
                    {
                        // not subscribed to channel
                        // not subscribed to stalk
                        this.logger.DebugFormat("Subscribing to stalk");
                        stalkSubscriber = new StalkUser(mask, true);
                        stalk.Subscribers.Add(stalkSubscriber);
                        source = SubscriptionSource.Stalk;
                        return(true);
                    }
                }
                else
                {
                    // not subscribed to channel
                    // not subscribed to stalk
                    this.logger.DebugFormat("Subscribing to stalk");
                    stalkSubscriber = new StalkUser(mask, true);
                    stalk.Subscribers.Add(stalkSubscriber);
                    source = SubscriptionSource.Stalk;
                    return(true);
                }
            }
        }
Exemplo n.º 15
0
 public StalkUser(IrcUserMask mask, bool subscribed)
 {
     this.Mask       = mask;
     this.Subscribed = subscribed;
 }