static void AddHostsItem(NetAddress address, HostAliases aliases, string comment) { if (aliases.Count == 0) { throw new HostNotSpecifiedException(); } // Remove duplicates foreach (HostName host in aliases) { var lines = Hosts.FindAll(item => item.Valid && item.IP.Type == address.Type && item.Aliases.Contains(host)); foreach (var line in lines) { if (line.Aliases.Count == 1) { Hosts.Remove(line); Console.WriteLine("[REMOVED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); } else { line.Aliases.Remove(host); Console.WriteLine("[UPDATED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); } } } // New host var new_item = new HostsItem(address, aliases, comment == null ? "" : comment.Trim()); Hosts.Add(new_item); Console.WriteLine("[ADDED] {0} {1}", new_item.IP.ToString(), new_item.Aliases.ToString()); }
public HostsItem(HostsItem source) { this.Valid = source.Valid; this.Enabled = source.Enabled; this.Hidden = source.Hidden; this.IP = source.IP; this.Aliases = source.Aliases == null ? null : new HostAliases(source.Aliases); this.Comment = source.Comment; this.ResetFormat = source.ResetFormat; this.SourceString = source.SourceString; this.SourceHash = source.SourceHash; }
static void RunAddMode() { if (ArgsQueue.Count == 0) { throw new HostNotSpecifiedException(); } HostAliases aliases = new HostAliases(); NetAddress address = new NetAddress("127.0.0.1"); string comment = ""; bool inComment = false; while (ArgsQueue.Count > 0) { string arg = ArgsQueue.Dequeue(); if (inComment) { comment += arg + " "; continue; } if (arg.Length > 0 && arg[0] == '#') { inComment = true; comment = (arg.Length > 1) ? (arg.Substring(1) + " ") : ""; continue; } arg = arg.ToLower(); NetAddress TestAddress = NetAddress.TryCreate(arg); if (TestAddress != null) { address = TestAddress; continue; } HostName TestHostName = HostName.TryCreate(arg); if (TestHostName != null) { aliases.Add(TestHostName); continue; } throw new Exception(String.Format("Unknown argument '{0}'", arg)); } HostsItem line = new HostsItem(address, aliases, comment); Hosts.Add(line); Console.WriteLine("[ADDED] {0} {1}", line.IP.ToString(), line.Aliases.ToString()); }