context: using System;
  using System.Collections.Generic;
  
  namespace Server.Engines.Chat
  {
      public class Channel
      {
          private static readonly List<Channel> m_Channels = new List<Channel>();
          private readonly List<ChatUser> m_Users;
          private readonly List<ChatUser> m_Banned;
          private readonly List<ChatUser> m_Moderators;
          private readonly List<ChatUser> m_Voices;
          private string m_Name;
          private string m_Password;
          private bool m_VoiceRestricted;
          private bool m_AlwaysAvailable;
          public Channel(string name)
          {
              this.m_Name = name;
  
              this.m_Users = new List<ChatUser>();
              this.m_Banned = new List<ChatUser>();
              this.m_Moderators = new List<ChatUser>();
              this.m_Voices = new List<ChatUser>();
          }
  
          public Channel(string name, string password)
              : this(name)
          {
              this.m_Password = password;
          }
  
          public static List<Channel> Channels
          {
              get
              {
                  return m_Channels;
              }
          }
          public string Name
          {
              get
              {
                  return this.m_Name;
              }
              set
              {
                  this.SendCommand(ChatCommand.RemoveChannel, this.m_Name);
                  this.m_Name = value;
                  this.SendCommand(ChatCommand.AddChannel, this.m_Name);
                  this.SendCommand(ChatCommand.JoinedChannel, this.m_Name);
              }
          }
          public string Password
          {
              get
              {
                  return this.m_Password;
              }
              set
              {
                  string newValue = null;
  
                  if (value != null)
                  {
                      newValue = value.Trim();
  
                      if (String.IsNullOrEmpty(newValue))
                          newValue = null;
                  }
  
                  this.m_Password = newValue;
              }
          }
          public bool VoiceRestricted
          {
              get
              {
                  return this.m_VoiceRestricted;
              }
              set
              {
                  this.m_VoiceRestricted = value;
  
                  if (value)
                      this.SendMessage(56); // From now on, only moderators will have speaking privileges in this conference by default.
                  else
                      this.SendMessage(55); // From now on, everyone in the conference will have speaking privileges by default.
              }
          }
          public bool AlwaysAvailable
          {
              get
              {
                  return this.m_AlwaysAvailable;
              }
              set
              {
                  this.m_AlwaysAvailable = value;
              }
          }
          public static void SendChannelsTo(ChatUser user)
          {
              for (int i = 0; i < m_Channels.Count; ++i)
              {
                  Channel channel = m_Channels[i];
  
                  if (!channel.IsBanned(user))
                      ChatSystem.SendCommandTo(user.Mobile, ChatCommand.AddChannel, channel.Name, "0");
              }
          }
  
          public static Channel AddChannel(string name)
          {
              return AddChannel(name, null);
          }
  
          public static Channel AddChannel(string name, string password)
          {
              Channel channel = FindChannelByName(name);
  
              if (channel == null)
              {
                  channel = new Channel(name, password);
                  m_Channels.Add(channel);
              }
  
              ChatUser.GlobalSendCommand(ChatCommand.AddChannel, name, "0") ; 
  
              return channel;
          }
  
          public static void RemoveChannel(string name)
          {
              RemoveChannel(FindChannelByName(name));
          }
  
          public static void RemoveChannel(Channel channel)
          {
              if (channel == null)
                  return;
  
              if (m_Channels.Contains(channel) && channel.m_Users.Count == 0)
              {
                  ChatUser.GlobalSendCommand(ChatCommand.RemoveChannel, channel.Name) ;
  
                  channel.m_Moderators.Clear();
                  channel.m_Voices.Clear();
  
                  m_Channels.Remove(channel);
              }
          }
  
          public static Channel FindChannelByName(string name)
          {
              for (int i = 0; i < m_Channels.Count; ++i)
              {
                  Channel channel = m_Channels[i];
  
                  if (channel.m_Name == name)
                      return channel;
              }
  
              return null;
          }
  
          public static void Initialize()
          {
              AddStaticChannel("Newbie Help");
          }
  
          public static void AddStaticChannel(string name)
          {
              AddChannel(name).AlwaysAvailable = true;
          }
  
          public bool Contains(ChatUser user)
          {
              return this.m_Users.Contains(user);
          }
  
          public bool IsBanned(ChatUser user)
          {
              return this.m_Banned.Contains(user);
          }
  
          public bool CanTalk(ChatUser user)
          {
              return (!this.m_VoiceRestricted || this.m_Voices.Contains(user) || this.m_Moderators.Contains(user));
          }
  
          public bool IsModerator(ChatUser user)
          {
              return this.m_Moderators.Contains(user);
          }
  
          public bool IsVoiced(ChatUser user)
          {
              return this.m_Voices.Contains(user);
          }
  
          public bool ValidatePassword(string password)
          {
              return (this.m_Password == null || Insensitive.Equals(this.m_Password, password));
          }
  
          public bool ValidateModerator(ChatUser user)
          {
              if (user != null && !this.IsModerator(user))
              {
                  user.SendMessage(29); // You must have operator status to do this.
                  return false;
              }
  
              return true;
          }
  
          public bool ValidateAccess(ChatUser from, ChatUser target)
          {
              if (from != null && target != null && from.Mobile.AccessLevel < target.Mobile.AccessLevel)
              {
                  from.Mobile.SendMessage("Your access level is too low to do this.");
                  return false;
              }
  
              return true;
          }
  
          public bool AddUser(ChatUser user)
          {
              return this.AddUser(user, null);
          }
  
          public bool AddUser(ChatUser user, string password)
          {
              if (this.Contains(user))
              {
                  user.SendMessage(46, this.m_Name); // You are already in the conference '%1'.
                  return true;
              }
  
gt:             else if (this.IsBanned(user))