Example #1
0
        private bool UserCanSeeChannel(NancyContext context, IIrcChannel channel)
        {
            /*
             * user is "aware" if:
             *   a) they can see channel config (fairly liberal)
             *   b) they have globaladmin
             */


            var currentUser = ((UserIdentity)context.CurrentUser).BotUser;

            // currentUser is "aware" if:

            // a) they can see channel config
            if (this.UserCanSeeChannelConfig(context, channel))
            {
                return(true);
            }

            // b) they have globaladmin
            if (this.botUserConfiguration.Items.Any(
                    x =>
                    x.Mask.Equals(currentUser.Mask) &&
                    !string.IsNullOrEmpty(x.GlobalFlags) &&
                    x.GlobalFlags.Contains(AccessFlags.GlobalAdmin)
                    ))
            {
                return(true);
            }

            return(false);
        }
        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);
            }
        }
Example #3
0
 public ChannelViewModel(Dispatcher dispatcher, IIrcChannel channel)
     : base(dispatcher)
 {
     _channel = channel;
     _users = new ObservableCollection<IIrcChannelUser>();
     foreach (var u in channel.Users.OrderBy(un => un.Name))
         _users.Add(u);
 }
Example #4
0
 public ChannelViewModel(Dispatcher dispatcher, IIrcChannel channel)
     : base(dispatcher)
 {
     _channel = channel;
     _users   = new ObservableCollection <IIrcChannelUser>();
     foreach (var u in channel.Users.OrderBy(un => un.Name))
     {
         _users.Add(u);
     }
 }
Example #5
0
        public XmlElement ToXmlElement(IIrcChannel item, XmlDocument doc)
        {
            var e = doc.CreateElement("channel");

            e.SetAttribute("name", item.Identifier);
            e.SetAttribute("guid", item.Guid.ToString());

            var usersElement = doc.CreateElement("users");

            e.AppendChild(usersElement);
            var stalksElement = doc.CreateElement("stalks");

            e.AppendChild(stalksElement);

            foreach (var channelUser in item.Users)
            {
                var u = doc.CreateElement("user");
                u.SetAttribute("mask", channelUser.Mask.ToString());
                bool saveable = false;

                if (!string.IsNullOrEmpty(channelUser.LocalFlags))
                {
                    u.SetAttribute("localflags", channelUser.LocalFlags);
                    saveable = true;
                }

                if (channelUser.Subscribed)
                {
                    u.SetAttribute("subscribed", XmlConvert.ToString(channelUser.Subscribed));
                    saveable = true;
                }

                if (saveable)
                {
                    usersElement.AppendChild(u);
                }
            }

            foreach (var stalk in item.Stalks)
            {
                var xmlElement = this.stalkFactory.ToXmlElement(stalk.Value, doc);
                stalksElement.AppendChild(xmlElement);
            }

            return(e);
        }
Example #6
0
        private IrcUser GetIrcUser(IIrcChannel channel, IBotUser currentUser)
        {
            if (!this.FreenodeClient.Channels.ContainsKey(channel.Identifier))
            {
                return(null);
            }

            var freenodeClientChannel = this.FreenodeClient.Channels[channel.Identifier];
            var channelUser           = freenodeClientChannel.Users.Values
                                        .FirstOrDefault(x => currentUser.Mask.Matches(x.User).GetValueOrDefault());
            var ircUser = channelUser != null
                ? channelUser.User
                : new IrcUser(this.FreenodeClient)
            {
                Account = currentUser.Mask.ToString().Substring(3), SkeletonStatus = IrcUserSkeletonStatus.Account
            };

            return(ircUser);
        }
        public IEnumerable <SubscriptionResult> GetUserSubscriptionsToStalk(IIrcChannel channel, IStalk stalk)
        {
            var userData = this.botUserConfiguration.Items.ToDictionary(
                x => x,
                y => new SubscriptionResult {
                Stalk = stalk, Channel = channel, BotUser = y
            });

            foreach (var channelUser in channel.Users.Where(x => x.Subscribed))
            {
                var botUser = this.botUserConfiguration[channelUser.Mask.ToString()];
                userData[botUser].IsSubscribed = true;
                userData[botUser].Source       = SubscriptionSource.Channel;
                userData[botUser].Complete     = true;
            }

            foreach (var stalkUser in stalk.Subscribers)
            {
                var botUser = this.botUserConfiguration[stalkUser.Mask.ToString()];

                if (stalkUser.Subscribed)
                {
                    userData[botUser].IsSubscribed = true;
                    userData[botUser].Overridden   = false;
                    userData[botUser].Source       = SubscriptionSource.Stalk;
                    userData[botUser].Complete     = true;
                }
                else
                {
                    // subscription exclusion for channel users
                    userData[botUser].IsSubscribed = false;
                    userData[botUser].Overridden   = true;
                    userData[botUser].Source       = SubscriptionSource.Stalk;
                    userData[botUser].Complete     = true;
                }
            }

            return(userData.Where(x => x.Value.Complete).Select(x => x.Value).ToList());
        }
Example #8
0
        private bool UserCanConfigureStalks(NancyContext context, IIrcChannel channel)
        {
            /*
             * user can configure if:
             *   a) they can see the config, AND
             *   b) they have at least one of:
             *      i)   *local* config
             *      ii)  *global* config
             *      iii) *global* owner
             */

            if (!this.UserCanSeeChannelConfig(context, channel))
            {
                return(false);
            }

            var currentUser = ((UserIdentity)context.CurrentUser).BotUser;
            var ircUser     = this.GetIrcUser(channel, currentUser);

            // bi) they have config flags in the channel, either locally or globally
            if (this.flagService.UserHasFlag(ircUser,
                                             AccessFlags.Configuration, channel.Identifier))
            {
                return(true);
            }

            // d) they owner flags globally
            if (this.botUserConfiguration.Items.Any(
                    x =>
                    x.Mask.Equals(currentUser.Mask) &&
                    !string.IsNullOrEmpty(x.GlobalFlags) &&
                    x.GlobalFlags.Contains(Flag.Owner)
                    ))
            {
                return(true);
            }

            return(false);
        }
        public IEnumerable <SubscriptionResult> GetUserStalkSubscriptionsInChannel(IBotUser user, IIrcChannel channel)
        {
            var channelSubscribed = channel.Users.Where(x => x.Subscribed).Any(x => x.Mask.Equals(user.Mask));

            var results = new List <SubscriptionResult>();

            foreach (var stalk in channel.Stalks)
            {
                var result = new SubscriptionResult
                {
                    Channel = channel, BotUser = user, Stalk = stalk.Value, Complete = true
                };

                if (channelSubscribed)
                {
                    result.IsSubscribed = true;
                    result.Source       = SubscriptionSource.Channel;
                }

                var subscription = stalk.Value.Subscribers.FirstOrDefault(x => x.Mask.Equals(user.Mask));
                if (subscription == null)
                {
                    // use the channel result.
                    results.Add(result);
                    continue;
                }

                if (subscription.Subscribed)
                {
                    result.IsSubscribed = true;
                    result.Source       = SubscriptionSource.Stalk;
                }
                else
                {
                    result.IsSubscribed = false;
                    result.Overridden   = true;
                    result.Source       = SubscriptionSource.Stalk;
                }

                results.Add(result);
            }

            return(results);
        }
Example #10
0
        private bool UserCanSeeChannelConfig(NancyContext context, IIrcChannel channel)
        {
            /*
             * user can see config if:
             *   a) they are a member of the channel
             *   b) they are subscribed to the channel or a stalk within the channel
             *   c) they have *local* config or localadmin flags in the channel
             *   d) they have owner flags
             *
             * Note: globaladmin does not permit viewing of the channel configuration.
             * Note: global config does not permit viewing of the channel configuration.
             *
             * This allows private channels to be protected, but still managed by global users if necessary.
             */

            var currentUser = ((UserIdentity)context.CurrentUser).BotUser;
            var ircUser     = this.GetIrcUser(channel, currentUser);

            // currentUser can see config if:

            // a) they are a member of the channel
            if (this.FreenodeClient.Channels.ContainsKey(channel.Identifier))
            {
                var ircClientChannel = this.FreenodeClient.Channels[channel.Identifier];

                if (ircClientChannel.Users.Values.Any(x => currentUser.Mask.Matches(x.User).GetValueOrDefault()))
                {
                    return(true);
                }
            }

            // b) they are subscribed to the channel or a stalk within the channel
            if (channel.Users.Any(x => x.Subscribed && x.Mask.Equals(currentUser.Mask)) ||
                channel.Stalks.Values.Any(
                    s => s.Subscribers.Any(u => u.Subscribed && u.Mask.Equals(currentUser.Mask))))
            {
                return(true);
            }

            var hasLocalConfig = false;
            var channelUser    = channel.Users.FirstOrDefault(x => x.Mask.Equals(currentUser.Mask));

            if (channelUser != null && !string.IsNullOrWhiteSpace(channelUser.LocalFlags))
            {
                hasLocalConfig = channelUser.LocalFlags.Contains(AccessFlags.Configuration);
            }

            // c) they have config or localadmin flags in the channel
            if (
                this.flagService.UserHasFlag(ircUser, AccessFlags.LocalAdmin, channel.Identifier) ||
                hasLocalConfig)
            {
                return(true);
            }

            // d) they have owner flags globally
            if (this.botUserConfiguration.Items.Any(
                    x =>
                    x.Mask.Equals(currentUser.Mask) &&
                    !string.IsNullOrEmpty(x.GlobalFlags) &&
                    x.GlobalFlags.Contains(Flag.Owner)))
            {
                return(true);
            }

            return(false);
        }
Example #11
0
        private List <ChannelDisplayUser> GetChannelDisplayUsers(IIrcChannel channel, ChannelInfoModel model)
        {
            var displayUsers = new List <ChannelDisplayUser>();

            var globalUsers    = this.botUserConfiguration.Items.ToList();
            var localUsers     = channel.Users.ToList();
            var channelMembers = model.ChannelMembers.ToList();

            foreach (var member in channelMembers)
            {
                var found = false;

                foreach (var user in channel.Users.ToList())
                {
                    if (user.Mask.Matches(member.User).GetValueOrDefault())
                    {
                        var u = new ChannelDisplayUser();
                        u.Member       = member;
                        u.LocalUser    = user;
                        u.GlobalUser   = this.botUserConfiguration.Items.FirstOrDefault(x => Equals(x.Mask, user.Mask));
                        u.Construction = "ML";

                        localUsers.Remove(user);
                        globalUsers.RemoveAll(x => Equals(x.Mask, user.Mask));

                        found = true;
                        displayUsers.Add(u);
                        break;
                    }
                }

                if (!found)
                {
                    foreach (var user in this.botUserConfiguration.Items.ToList())
                    {
                        if (user.Mask.Matches(member.User).GetValueOrDefault())
                        {
                            var u = new ChannelDisplayUser();
                            u.Member       = member;
                            u.GlobalUser   = user;
                            u.Construction = "MG";
                            globalUsers.Remove(user);

                            found = true;
                            displayUsers.Add(u);
                            break;
                        }
                    }
                }

                if (!found)
                {
                    var u = new ChannelDisplayUser();
                    u.Member       = member;
                    u.Construction = "M-";

                    displayUsers.Add(u);
                }
            }

            foreach (var user in localUsers)
            {
                var u = new ChannelDisplayUser();
                u.LocalUser    = user;
                u.GlobalUser   = this.botUserConfiguration.Items.FirstOrDefault(x => Equals(x.Mask, user.Mask));
                u.Construction = "-L";

                globalUsers.Remove(u.GlobalUser);

                displayUsers.Add(u);
            }

            // Some global users should be listed here since they have the ability to change bot config on a local basis
            foreach (var user in globalUsers.Where(x => !string.IsNullOrWhiteSpace(x.GlobalFlags))
                     .Where(
                         x => x.GlobalFlags.Contains(Flag.Owner) ||
                         x.GlobalFlags.Contains(AccessFlags.GlobalAdmin) ||
                         x.GlobalFlags.Contains(AccessFlags.LocalAdmin) ||
                         x.GlobalFlags.Contains(AccessFlags.Configuration)))
            {
                var u = new ChannelDisplayUser();
                u.GlobalUser   = this.botUserConfiguration.Items.FirstOrDefault(x => Equals(x.Mask, user.Mask));
                u.Construction = "-G";

                displayUsers.Add(u);
            }

            return(displayUsers);
        }
Example #12
0
 public ChannelEventArgs(IIrcChannel channel)
 {
     _channel = channel;
 }
 public ChannelUserEventArgs(IIrcChannel channel, IIrcChannelUser user)
     : base(channel)
 {
     _user = user;
 }
Example #14
0
 public ChannelUserEventArgs(IIrcChannel channel, IIrcChannelUser user)
     : base(channel)
 {
     _user = user;
 }
        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);
                }
            }
        }
 public ChannelInputEventArgs(string text, IIrcChannel channel)
 {
     Text = text;
     Channel = channel;
 }
Example #17
0
 public ChannelEventArgs(IIrcChannel channel)
 {
     _channel = channel;
 }
 public bool IsSubscribedToChannel(IBotUser botUser, IIrcChannel channel)
 {
     return(this.GetUserSubscriptionsToChannel(botUser).Any(x => x.Equals(channel)));
 }
 public bool IsSubscribedToStalk(IBotUser botUser, IIrcChannel channel, IStalk stalk)
 {
     return(this.GetUserSubscriptionsToStalk(channel, stalk)
            .Where(x => x.IsSubscribed)
            .Any(x => Equals(x.BotUser, botUser)));
 }
 public ChannelTopicEventArgs(IIrcChannel channel, string oldTopic)
     : base(channel)
 {
     _oldTopic = oldTopic;
 }
Example #21
0
 public ChannelTopicEventArgs(IIrcChannel channel, string oldTopic)
     : base(channel)
 {
     _oldTopic = oldTopic;
 }