Example #1
0
 public BanChangedEventArgs(UserBan ban)
 {
     this.Ban = ban ?? throw new ArgumentNullException(nameof(ban));
 }
Example #2
0
        internal Channel(string name, Client client, ChannelConfig channelConfig)
        {
            this.Name          = name ?? throw new ArgumentNullException(nameof(name));
            this.client        = client ?? throw new ArgumentNullException(nameof(client));
            this.permanentlyOp = channelConfig.PermanentlyOp ?? this.client.Config.PermanentlyOp;

            this.commandRegistrationManager = new CommandRegistrationManager(client.Config.CommandPrefixes, requirePrefix: true);

            this.session.SelfJoined += (o, e) =>
            {
                if (e.Channel.IsChannel && e.Channel.Name == this.Name)
                {
                    this.IsJoined = true;
                    this.OnJoined();
                }
            };
            this.session.SelfParted += (o, e) =>
            {
                if (e.Channel.IsChannel && e.Channel.Name == this.Name)
                {
                    this.IsJoined = false;
                }
            };
            this.session.SelfKicked += (o, e) =>
            {
                if (e.Channel.IsChannel && e.Channel.Name == this.Name)
                {
                    this.IsJoined = false;
                }
            };
            this.session.PrivateMessaged += (o, e) =>
            {
                if (e.To.IsChannel && e.To.Name == this.Name)
                {
                    this.Messaged?.Invoke(this, new MessagedEventArgs(e.From, e.Text));

                    var response = this.commandRegistrationManager.TryDispatchCommand(e.From, e.Text);
                    if (response != null)
                    {
                        this.client.Session.PrivateMessage(e.To, response);
                    }
                }
            };
            this.session.ChannelModeChanged += (o, e) =>
            {
                if (e.Channel.IsChannel && e.Channel.Name == this.Name)
                {
                    foreach (var mode in e.Modes)
                    {
                        if (mode.Parameter == this.session.Nickname && mode.Mode == 'o')
                        {
                            this.IsOp = mode.Set;
                        }
                        else if (mode.Mode == 'b' || mode.Mode == 'q')
                        {
                            var    newBanList  = new List <UserBan>(this.BanList);
                            Action eventRaiser = null;
                            if (mode.Set)
                            {
                                var ban = new UserBan(mode.Parameter, e.Who.Prefix, DateTime.UtcNow, mode.Mode);
                                newBanList.Add(ban);
                                eventRaiser = () => this.BanAdded?.Invoke(this, new BanChangedEventArgs(ban));
                            }
                            else
                            {
                                var toRemove = newBanList.FirstOrDefault(x => x.Mask == mode.Parameter && x.Type == mode.Mode);
                                if (toRemove != null)
                                {
                                    newBanList.Remove(toRemove);
                                    eventRaiser = () => this.BanRemoved?.Invoke(this, new BanChangedEventArgs(toRemove));
                                }
                            }
                            this.BanList = newBanList;
                            eventRaiser?.Invoke();
                        }
                    }
                }
            };

            // These 4 form a chain - we always request bans, and when they finish we request mutes (if supported_
            this.session.AddHandler(new IrcCodeHandler(e =>
            {
                var parameters = e.Message.Parameters;
                if (parameters[1] == this.Name)
                {
                    var ban = new UserBan(parameters[2], parameters[3], DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(parameters[4]) * 1000).DateTime, 'b');
                    this.wipBanList.Add(ban);
                }
                return(false);
            }, IrcCode.RPL_BANLIST));
            this.session.AddHandler(new IrcCodeHandler(e =>
            {
                if (e.Message.Parameters[1] == this.Name)
                {
                    if (this.muteSupported)
                    {
                        this.session.Mode(this.Name, "+q");
                    }
                    else
                    {
                        this.BanList    = this.wipBanList;
                        this.wipBanList = new List <UserBan>();
                        this.BanListReloaded?.Invoke(this, new BanListReloadedEventArgs(this.BanList));
                    }
                }
                return(false);
            }, IrcCode.RPL_ENDOFBANLIST));

            if (this.muteSupported)
            {
                this.session.AddHandler(new IrcCodeHandler(e =>
                {
                    var parameters = e.Message.Parameters;
                    if (parameters[1] == this.Name && parameters[2] == "q")
                    {
                        var ban = new UserBan(parameters[3], parameters[4], DateTimeOffset.FromUnixTimeMilliseconds(long.Parse(parameters[5])).DateTime, 'q');
                        this.wipBanList.Add(ban);
                    }
                    return(false);
                }, (IrcCode)728));
                this.session.AddHandler(new IrcCodeHandler(e =>
                {
                    if (e.Message.Parameters[1] == this.Name && e.Message.Parameters[2] == "q")
                    {
                        this.BanList    = this.wipBanList;
                        this.wipBanList = new List <UserBan>();
                        this.BanListReloaded?.Invoke(this, new BanListReloadedEventArgs(this.BanList));
                    }
                    return(false);
                }, (IrcCode)729));
            }
        }