context: /*
   * This file is part of Schumix.
   * 
   * Copyright (C) 2010-2013 Megax <http://megax.yeahunter.hu/>
   * Copyright (C) 2013-2015 Schumix Team <http://schumix.eu/>
   * 
   * Schumix is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   * 
   * Schumix is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   * 
   * You should have received a copy of the GNU General Public License
   * along with Schumix.  If not, see <http://www.gnu.org/licenses/>.
   */
  
  using System;
  using System.Data;
  using System.Collections.Generic;
  using Schumix.Irc.Ignore;
  using Schumix.Framework;
  using Schumix.Framework.Logger;
  using Schumix.Framework.Config;
  using Schumix.Framework.Functions;
  using Schumix.Framework.Extensions;
  using Schumix.Framework.Localization;
  
  namespace Schumix.Irc.Channel
  {
  	public sealed class MyChannelInfo
  	{
  		private readonly Dictionary<string, string> ChannelFunction = new Dictionary<string, string>();
  		private readonly Dictionary<string, string> _ChannelList = new Dictionary<string, string>();
  		private readonly LocalizationConsole sLConsole = Singleton<LocalizationConsole>.Instance;
  		private readonly IrcBase sIrcBase = Singleton<IrcBase>.Instance;
  		private readonly object WriteLock = new object();
  		private readonly IgnoreChannel sIgnoreChannel;
  		private readonly Sender sSender;
  		private string _servername;
  
  		public Dictionary<string, string> CList
  		{
  			get { return _ChannelList; }
  		}
  
  		public Dictionary<string, string> CFunction
  		{
  			get { return ChannelFunction; }
  		}
  
  		public MyChannelInfo(string ServerName)
  		{
  			_servername = ServerName;
  			sSender = sIrcBase.Networks[ServerName].sSender;
  			sIgnoreChannel = sIrcBase.Networks[ServerName].sIgnoreChannel;
  		}
  
  		public void ChannelList()
  		{
  			var db = SchumixBase.DManager.Query("SELECT Channel, Password FROM channels WHERE ServerName = '{0}'", _servername);
  			if(!db.IsNull())
  			{
  				foreach(DataRow row in db.Rows)
  				{
  					string channel = row["Channel"].ToString();
  					string password = row["Password"].ToString();
  					_ChannelList.Add(channel, password);
  				}
  			}
  			else
  				Log.Error("MyChannelInfo", sLConsole.GetString("ChannelList: Failure request!"));
  		}
  
  		public bool FSelect(string Name)
  		{
  			lock(WriteLock)
  			{
  				if(IFunctionsClass.ServerList[_servername].Functions.ContainsKey(Name.ToLower()))
  					return IFunctionsClass.ServerList[_servername].Functions[Name.ToLower()] == SchumixBase.On;
  
  				return false;
  			}
  		}
  
  		public bool FSelect(string Name, string Channel)
  		{
  			lock(WriteLock)
  			{
  				if(ChannelFunction.ContainsKey(Channel.ToLower()))
  				{
  					foreach(var comma in ChannelFunction[Channel.ToLower()].Split(SchumixBase.Comma))
  					{
  						if(comma.IsNullOrEmpty())
  							continue;
  
  						string[] point = comma.Split(SchumixBase.Colon);
  
  						if(point[0] == Name.ToLower())
  							return point[1] == SchumixBase.On;
  					}
  				}
  
  				return false;
  			}
  		}
  
  		public bool SearchFunction(string Name)
  		{
  			return IFunctionsClass.ServerList[_servername].Functions.ContainsKey(Name.ToString().ToLower());
  		}
  
  		public bool SearchChannelFunction(string Name)
  		{
  			lock(WriteLock)
  			{
  				foreach(var channel in ChannelFunction)
  				{
  					foreach(var comma in channel.Value.Split(SchumixBase.Comma))
  					{
  						if(comma.IsNullOrEmpty())
  							continue;
  
  						string[] point = comma.Split(SchumixBase.Colon);
  
  						if(point[0] == Name.ToLower())
  							return true;
  					}
  				}
  
  				return false;
  			}
  		}
  
  		public bool FSelect(IFunctions Name)
  		{
  			lock(WriteLock)
  			{
  				if(IFunctionsClass.ServerList[_servername].Functions.ContainsKey(Name.ToString().ToLower()))
  					return IFunctionsClass.ServerList[_servername].Functions[Name.ToString().ToLower()] == SchumixBase.On;
  
  				return false;
  			}
  		}
  
  		public bool FSelect(IChannelFunctions Name, string Channel)
  		{
  			lock(WriteLock)
  			{
  				if(ChannelFunction.ContainsKey(Channel.ToLower()))
  				{
  					foreach(var comma in ChannelFunction[Channel.ToLower()].Split(SchumixBase.Comma))
  					{
  						if(comma.IsNullOrEmpty())
  							continue;
  
  						string[] point = comma.Split(SchumixBase.Colon);
  
  						if(point[0] == Name.ToString().ToLower())
  							return point[1] == SchumixBase.On;
  					}
  				}
  
  				return false;
  			}
  		}
  
  		public void FunctionsReload()
  		{
  			IFunctionsClass.ServerList[_servername].Functions.Clear();
  			var db = SchumixBase.DManager.Query("SELECT FunctionName, FunctionStatus FROM schumix WHERE ServerName = '{0}'", _servername);
  			if(!db.IsNull())
  			{
  				foreach(DataRow row in db.Rows)
  				{
  					string name = row["FunctionName"].ToString();
  					string status = row["FunctionStatus"].ToString();
  					IFunctionsClass.ServerList[_servername].Functions.Add(name.ToLower(), status.ToLower());
  				}
  			}
  			else
  				Log.Error("MyChannelInfo", sLConsole.GetString("FunctionReload: Failre request!"));
  		}
  
  		public void ChannelFunctionsReload()
  		{
  			ChannelFunction.Clear();
  			SchumixBase.sCacheDB.Reload("channels");
  			var db = SchumixBase.DManager.Query("SELECT Channel FROM channels WHERE ServerName = '{0}'", _servername);
  			if(!db.IsNull())
  			{
  				foreach(DataRow row in db.Rows)
  				{
  					string channel = row["Channel"].ToString();
  					var db1 = SchumixBase.DManager.QueryFirstRow("SELECT Functions FROM channels WHERE Channel = '{0}' And ServerName = '{1}'", channel, _servername);
  					if(!db1.IsNull())
  						ChannelFunction.Add(channel, db1["Functions"].ToString());
  					else
  						Log.Error("MyChannelInfo", sLConsole.GetString("ChannelFunctionReload: Failre request!"));
  				}
  			}
  			else
  				Log.Error("MyChannelInfo", sLConsole.GetString("ChannelFunctionReload: Failre request!"));
  		}
  
  		public void ChannelListReload()
  		{
  			_ChannelList.Clear();
  			var db = SchumixBase.DManager.Query("SELECT Channel, Password FROM channels WHERE ServerName = '{0}'", _servername);
  			if(!db.IsNull())
  			{
  				foreach(DataRow row in db.Rows)
  				{
  					string channel = row["Channel"].ToString();
  					string password = row["Password"].ToString();
  					_ChannelList.Add(channel, password);
  				}
  			}
  			else
  				Log.Error("MyChannelInfo", sLConsole.GetString("ChannelListReload: Failre request!"));
  		}
  
  		public string ChannelFunctions(IChannelFunctions name, string status, string channel)
  		{
  			return ChannelFunctions(name.ToString().ToLower(), status, channel);
  		}
  
  		public string ChannelFunctions(string name, string status, string channel)
  		{
  			string functions = string.Empty;
  
  			if(ChannelFunction.ContainsKey(channel.ToLower()))
  			{
  				foreach(var comma in ChannelFunction[channel.ToLower()].Split(SchumixBase.Comma))
  				{
  					if(comma.IsNullOrEmpty())
  						continue;
  
  					string[] point = comma.Split(SchumixBase.Colon);
  
  					if(point[0] != name.ToLower())
  						functions += SchumixBase.Comma + comma;
  				}
  
  				foreach(var comma in ChannelFunction[channel.ToLower()].Split(SchumixBase.Comma))
  				{
  					if(comma.IsNullOrEmpty())
  						continue;
  
  
gt: 					string[] point = comma.Split(SchumixBase.Colon);