static void Main(string[] args) { bool _isRemove = false, _isCreate = false; string _geoPath = String.Empty, _geoLock = String.Empty; stGeo.GeoFilter _geof = null; stCore.IniConfig.IniFile _iniFile = null; stCore.IMessage _iLog = null; GeoUpdateMain.rootAppPath = stCore.IOBaseAssembly.BaseDataDir(); Thread.CurrentThread.Name = stCore.IOBaseAssembly.BaseName(Assembly.GetExecutingAssembly()); args.Process( () => { }, new CommandLine.Switch("geodir", val => _geoPath = string.Join("", val), "-d"), new CommandLine.Switch("clear", val => _isRemove = true, "-c"), new CommandLine.Switch("create", val => _isCreate = true, "-a"), new CommandLine.Switch("quiet", val => GeoUpdateMain.isQuiet = true, "-q") ); if (GeoUpdateMain.isQuiet) { _iLog = new IMessage(); } else { _iLog = new IMessage() { LogInfo = GeoUpdateMain.PrnInfo, LogError = GeoUpdateMain.PrnError, ProgressBar = stConsole.ProgressTxt }; string AppDesc = IOBaseAssembly.BaseDescription(Assembly.GetExecutingAssembly()); stApp.AppInformation.PrnBanner( new string[] { string.Format( "{0}: {1}", Thread.CurrentThread.Name, ((string.IsNullOrWhiteSpace(AppDesc)) ? "" : AppDesc) ), Properties.Resources.banRun } ); } try { if (string.IsNullOrWhiteSpace(_geoPath)) { try { string iniPath = Path.Combine( GeoUpdateMain.rootAppPath, "stCoCServer.ini" ); if (!File.Exists(iniPath)) { throw new FileNotFoundException( string.Format( Properties.Resources.GeoIniNotFound, iniPath ) ); } _iniFile = new stCore.IniConfig.IniFile(iniPath); if (_iniFile == null) { throw new FileLoadException( string.Format( Properties.Resources.GeoInErrorLoad, iniPath ) ); } string lng = _iniFile.Section("SYS").Get("SYSLANGConsole"); if (!string.IsNullOrWhiteSpace(lng)) { try { CultureInfo ci = null; if ((ci = CultureInfo.GetCultureInfo(lng)) != null) { System.Threading.Thread.CurrentThread.CurrentCulture = ci; System.Threading.Thread.CurrentThread.CurrentUICulture = ci; } } catch (Exception) { } } _geoPath = _iniFile.Section("SYS").Get("SYSGEOPath"); if (_geoPath == null) { throw new ArgumentNullException(Properties.Resources.GeoConfigNotFound); } } catch (Exception e) { _geoPath = Path.Combine( GeoUpdateMain.rootAppPath, "geo" ); if (!Directory.Exists(_geoPath)) { _iLog.LogError(e.Message); return; } } finally { if (_iniFile != null) { _iniFile.Clear(); } } } if (!Directory.Exists(_geoPath)) { if (_isCreate) { Directory.CreateDirectory(_geoPath); } else { throw new DirectoryNotFoundException(Properties.Resources.GeoDirNotFound); } } string[] allFiles = GeoUpdateMain.MakeFileList(_geoPath); if (_isRemove) { foreach (string fn in allFiles) { if (File.Exists(fn)) { File.Delete(fn); } } } if ( (!File.Exists(allFiles[0])) && (!File.Exists(allFiles[1])) ) { if (!GeoUpdateMain.GetUrlToFile( stGeo.MaxMindUtil.MaxMindDownloadASNURL, stGeo.MaxMindUtil.GetASNZipFileName, _geoPath, _iLog) ) { throw new FileNotFoundException(Properties.Resources.GeoFileNotFound); } } if ( (!File.Exists(allFiles[2])) && (!File.Exists(allFiles[3])) ) { if (!GeoUpdateMain.GetUrlToFile( stGeo.MaxMindUtil.MaxMindDownloadCountryURL, stGeo.MaxMindUtil.GetCountryZipFileName, _geoPath, _iLog) ) { throw new FileNotFoundException(Properties.Resources.GeoFileNotFound); } } if (File.Exists(allFiles[4])) { File.Delete(allFiles[4]); } _geof = new GeoFilter(_iLog, false); if (_geof == null) { throw new ArgumentNullException(Properties.Resources.GeoClassInitError); } _geoLock = Path.Combine(_geoPath, MaxMindUtil.GetGeoDataLockFileName); File.Create(_geoLock); if (!_geof.InitBase(_geoPath, false, stConsole.GetCursorAlign(2))) { GeoUpdateMain.PrnError(Properties.Resources.GeoClassInitError); } else { GeoUpdateMain.PrnInfo( string.Format( Properties.Resources.GeoComplette, _geoPath ) ); } } catch (Exception e) { GeoUpdateMain.PrnError(e.Message); } finally { try { if (_geof != null) { _geof.Dispose(); } if ( (!string.IsNullOrWhiteSpace(_geoLock)) && (File.Exists(_geoLock)) ) { File.Delete(_geoLock); string geoDb = Path.Combine(_geoPath, MaxMindUtil.GetGeoDataFileName); if (File.Exists(geoDb)) { File.SetCreationTime(geoDb, DateTime.Now); } } } catch (Exception e) { GeoUpdateMain.PrnError(e.Message); } } #if DEBUG Console.ReadLine(); #endif return; }
public static bool GetIniConfig(ref CoCServerConfigData.Option Opt, IMessage iLog) { if (!File.Exists(Opt.SYSCONFPath.value)) { return(false); } stCore.IniConfig.IniFile iniFile = null; try { iniFile = new stCore.IniConfig.IniFile(Opt.SYSCONFPath.value); if (iniFile == null) { throw new ArgumentException( string.Format( Properties.Resources.iniFileInternalError, Opt.SYSCONFPath.value ) ); } } catch (Exception e) { if (iLog != null) { iLog.LogError(e.Message); } return(false); } /// Calculate IP Locations/Filter int realFilters = 0; foreach (var section in iniFile.Sections) { if (section.Name.StartsWith(BuildConfig.tagFilters)) { realFilters++; } if (realFilters > BuildConfig.numFilters) { BuildConfig._InitFilter(ref Opt); } } BuildConfig.numFilters = ((realFilters > BuildConfig.numFilters) ? realFilters : BuildConfig.numFilters); /// End calculate foreach (SettingsProperty setting in Properties.Settings.Default.Properties) { string pname = setting.Name.Trim(); if (pname.StartsWith(BuildConfig.tagFilters)) { List <OptionItem> ol = (List <OptionItem>)Opt[pname]; for (int i = 0; i < BuildConfig.numFilters; i++) { OptionItem oi = ol[i]; string sname = BuildConfig.nameFilters + i; BuildConfig._GetIniValue( iniFile.Section(sname).Get(pname), ref oi ); } } else { string sname = pname.Substring(0, 3); OptionItem oi = (OptionItem)Opt[pname]; BuildConfig._GetIniValue( iniFile.Section(sname).Get(pname), ref oi ); } } iniFile.Clear(); return(true); }
public static void SetIniConfig(CoCServerConfigData.Option Opt, IMessage iLog, bool overwrite = false) { if (File.Exists(Opt.SYSCONFPath.value)) { if (!overwrite) { return; } try { File.Delete(Opt.SYSCONFPath.value); } catch (Exception e) { if (iLog != null) { iLog.LogError(e.Message); } return; } } stCore.IniConfig.IniFile iniFile = null; try { iniFile = new stCore.IniConfig.IniFile(); if (iniFile == null) { throw new ArgumentException( string.Format( Properties.Resources.iniFileInternalError, Opt.SYSCONFPath.value ) ); } } catch (Exception e) { if (iLog != null) { iLog.LogError(e.Message); } return; } List <string> sect = new List <string>(); foreach (SettingsProperty setting in Properties.Settings.Default.Properties) { string pname = setting.Name.Substring(0, 3); if (!sect.Contains(pname)) { sect.Add(pname); } } foreach (string section in sect) { foreach (SettingsProperty setting in Properties.Settings.Default.Properties) { string pname = setting.Name.Trim(); if ((section.Equals(BuildConfig.tagFilters)) && (pname.Substring(0, 3).Equals(BuildConfig.tagFilters))) { List <OptionItem> ol = (List <OptionItem>)Opt[pname]; if (ol == null) { continue; } for (int i = 0; i < BuildConfig.numFilters; i++) { OptionItem oi = ol[i]; string sname = BuildConfig.nameFilters + i; iniFile.Section(sname).Set(pname, BuildConfig._ValueToString(setting, oi)); } } else if (section.Equals(pname.Substring(0, 3))) { OptionItem oi = (OptionItem)Opt[pname]; if (oi == null) { continue; } iniFile.Section(section).Set(pname, BuildConfig._ValueToString(setting, oi)); } } } if (iLog != null) { iLog.LogInfo( string.Format( Properties.Resources.iniFileBuildNew, Opt.SYSCONFPath.value ) ); } iniFile.Save(Opt.SYSCONFPath.value); iniFile.Clear(); }
static void Main(string[] args) { stCore.IniConfig.IniFile _iniFile = null; IrcManager.rootAppPath = stCore.IOBaseAssembly.BaseDataDir(); Thread.CurrentThread.Name = stCore.IOBaseAssembly.BaseName(Assembly.GetExecutingAssembly()); string usrAction = String.Empty, usrEmail = String.Empty, usrHomeUrl = String.Empty, usrChannel = String.Empty, usrClanId = String.Empty, usrClanName = String.Empty, usrName = String.Empty, usrTopic = String.Empty, usrBotName = String.Empty, usrIPAllow = String.Empty; bool boolConfig = false, boolBatch = false, boolHelp = false; args.Process( () => { }, new CommandLine.Switch("web", val => usrHomeUrl = string.Join("", val), "-w"), new CommandLine.Switch("email", val => usrEmail = string.Join("", val), "-e"), new CommandLine.Switch("channel", val => usrChannel = string.Join("", val), "-c"), new CommandLine.Switch("clanid", val => usrClanId = string.Join("", val), "-i"), new CommandLine.Switch("claname", val => usrClanName = string.Join("", val), "-n"), new CommandLine.Switch("topic", val => usrTopic = string.Join("", val), "-t"), new CommandLine.Switch("usrname", val => usrName = string.Join("", val), "-u"), new CommandLine.Switch("botname", val => usrBotName = string.Join("", val), "-k"), new CommandLine.Switch("ipallow", val => usrIPAllow = string.Join("", val), "-l"), new CommandLine.Switch("action", val => usrAction = string.Join("", val), "-a"), new CommandLine.Switch("batch", val => boolBatch = true, "-b"), new CommandLine.Switch("config", val => boolConfig = true, "-s"), new CommandLine.Switch("help", val => boolHelp = true, "-h") ); try { if (boolHelp) { IrcManager.PrnHead(); IrcManager.PrnHelp(); IrcManager.PrnExample(); return; } string iniPath = Path.Combine( IrcManager.rootAppPath, "stCoCServer.ini" ); if (!File.Exists(iniPath)) { throw new FileNotFoundException( string.Format( Properties.Resources.fmtIniNotFound, iniPath ) ); } _iniFile = new stCore.IniConfig.IniFile(iniPath); if (_iniFile == null) { throw new FileLoadException( string.Format( Properties.Resources.fmtIniErrorLoad, iniPath ) ); } string lng = _iniFile.Section("SYS").Get("SYSLANGConsole"); if (!string.IsNullOrWhiteSpace(lng)) { try { CultureInfo ci = null; if ((ci = CultureInfo.GetCultureInfo(lng)) != null) { System.Threading.Thread.CurrentThread.CurrentCulture = ci; System.Threading.Thread.CurrentThread.CurrentUICulture = ci; } } catch (Exception) { } } usrHomeUrl = ((string.IsNullOrWhiteSpace(usrHomeUrl)) ? _iniFile.Section("DOK").Get("DOKUWikiRootUrl") : usrHomeUrl ); usrEmail = ((string.IsNullOrWhiteSpace(usrEmail)) ? _iniFile.Section("SYS").Get("SYSRegEmail") : usrEmail ); usrChannel = ((string.IsNullOrWhiteSpace(usrChannel)) ? _iniFile.Section("IRC").Get("IRCChannel") : usrChannel ); usrBotName = ((string.IsNullOrWhiteSpace(usrBotName)) ? _iniFile.Section("IRC").Get("IRCNik") : usrBotName ); usrClanId = ((string.IsNullOrWhiteSpace(usrClanId)) ? _iniFile.Section("CLA").Get("CLANTag") : usrClanId ); usrClanName = ((string.IsNullOrWhiteSpace(usrClanName)) ? _iniFile.Section("CLA").Get("CLANName") : usrClanName ); if (!string.IsNullOrWhiteSpace(usrChannel)) { if (!usrChannel.StartsWith("#")) { usrChannel = "#" + usrChannel; } } if (!string.IsNullOrWhiteSpace(usrClanId)) { if (!usrClanId.StartsWith("#")) { usrClanId = "#" + usrClanId; } } if (boolConfig) { IrcManager.PrnHead(); stConsole.WriteHeader( "\tHome Web\t- " + usrHomeUrl + Environment.NewLine + "\tREG Email\t- " + usrEmail + Environment.NewLine + "\tIRC Channel\t- " + usrChannel + Environment.NewLine + "\tClan ID\t- " + usrClanId + Environment.NewLine + "\tClan Name\t- " + usrClanName + Environment.NewLine + "\tBot Name\t- " + usrBotName + Environment.NewLine ); return; } string ircOut = String.Empty; switch (usrAction) { case "admin": case "ban": case "kick": { if (string.IsNullOrWhiteSpace(usrName)) { throw new FileLoadException( Properties.Resources.txtOptionNotNickName ); } if (string.IsNullOrWhiteSpace(usrChannel)) { throw new FileLoadException( Properties.Resources.txtOptionNotIrcChannel ); } break; } case "topic": { if (string.IsNullOrWhiteSpace(usrChannel)) { throw new FileLoadException( Properties.Resources.txtOptionNotIrcChannel ); } if (string.IsNullOrWhiteSpace(usrTopic)) { throw new FileLoadException( Properties.Resources.txtOptionNotIrcTopic ); } break; } case "channel": { if (string.IsNullOrWhiteSpace(usrHomeUrl)) { throw new FileLoadException( Properties.Resources.txtOptionNotHomeUrl ); } if (string.IsNullOrWhiteSpace(usrChannel)) { throw new FileLoadException( Properties.Resources.txtOptionNotIrcChannel ); } if (string.IsNullOrWhiteSpace(usrClanId)) { throw new FileLoadException( Properties.Resources.txtOptionNotClanId ); } if (string.IsNullOrWhiteSpace(usrClanName)) { throw new FileLoadException( Properties.Resources.txtOptionNotClanName ); } break; } case "regchan": { if (string.IsNullOrWhiteSpace(usrChannel)) { throw new FileLoadException( Properties.Resources.txtOptionNotIrcChannel ); } if (string.IsNullOrWhiteSpace(usrClanName)) { throw new FileLoadException( Properties.Resources.txtOptionNotClanName ); } if (string.IsNullOrWhiteSpace(usrBotName)) { throw new FileLoadException( Properties.Resources.txtOptionNotBotName ); } break; } case "regbot": { if (string.IsNullOrWhiteSpace(usrEmail)) { throw new FileLoadException( Properties.Resources.txtOptionNotRegEmail ); } if (!IrcManager.IsValidEmail(usrEmail)) { throw new FileLoadException( Properties.Resources.txtOptionNotValidEmail ); } break; } default: { throw new FileLoadException( string.Format( Properties.Resources.fmtOptionNotAction, usrAction ) ); } } switch (usrAction) { case "ban": case "admin": case "regchan": { if (string.IsNullOrWhiteSpace(usrIPAllow)) { usrIPAllow = "*"; } break; } default: { break; } } switch (usrAction) { case "admin": { ircOut = string.Format( Properties.Resources.fmtIRCAdmin, usrChannel, usrName, usrIPAllow ); break; } case "ban": { ircOut = string.Format( Properties.Resources.fmtIRCBan, usrChannel, usrName, usrIPAllow ); break; } case "kick": { ircOut = string.Format( Properties.Resources.fmtIRCKick, usrChannel, usrName ); break; } case "topic": { ircOut = string.Format( Properties.Resources.fmtIRCTopic, usrChannel, usrTopic ); break; } case "channel": { ircOut = string.Format( Properties.Resources.fmtIRCRegChannel, usrChannel, usrClanName ); ircOut += Environment.NewLine + string.Format( Properties.Resources.fmtIRCSet4Channel, usrChannel, "DESC", "Clash of Clans", usrClanName + " (" + usrClanId + ")" ); ircOut += Environment.NewLine + string.Format( Properties.Resources.fmtIRCSet3Channel, usrChannel, "URL", usrHomeUrl ); ircOut += Environment.NewLine + string.Format( Properties.Resources.fmtIRCSet3Channel, usrChannel, "ENTRYMSG", "\"Clan " + usrClanName + ": " + usrHomeUrl + "\"" ); break; } case "regchan": { ircOut = string.Format( Properties.Resources.fmtIRCRegChannel, usrChannel, usrClanName ); ircOut += Environment.NewLine + string.Format( Properties.Resources.fmtIRCAdmin, usrChannel, usrBotName, usrIPAllow ); break; } case "regbot": { string usrPassword1 = String.Empty, usrPassword2 = String.Empty; if (boolBatch) { usrPassword1 = IrcManager.GetRandomPassword(); } else { stConsole.Write( string.Format( Properties.Resources.fmtIRCPassword, 1) ); usrPassword1 = Console.ReadLine(); if (string.IsNullOrWhiteSpace(usrPassword1)) { throw new FileLoadException( Properties.Resources.txtOptionPasswordEmpty ); } stConsole.Write( string.Format( Properties.Resources.fmtIRCPassword, 2) ); usrPassword2 = Console.ReadLine(); if (!usrPassword1.Equals(usrPassword2)) { throw new FileLoadException( Properties.Resources.txtPasswordNotEquals ); } } ircOut = string.Format( Properties.Resources.fmtIRCRegBot, usrPassword1, usrEmail ); stConsole.WriteHeader( string.Format( Properties.Resources.fmtPasswordAddOrChange, usrPassword1, Environment.NewLine ) ); break; } default: { throw new FileLoadException( string.Format( Properties.Resources.fmtOptionNotAction, usrAction ) ); } } #if DEBUG stConsole.WriteHeader(ircOut); #endif File.WriteAllText( Path.Combine( IrcManager.rootAppPath, "command.irc" ), ircOut, Encoding.UTF8 ); } catch (Exception e) { IrcManager.PrnHead(); IrcManager.PrnHelp(); IrcManager.PrnError(e.Message); } #if DEBUG Console.ReadLine(); #endif }