/// <summary> /// Delete an existing alias /// </summary> /// <param name="config">Config</param> /// <param name="args">CMD Args</param> /// <param name="provider">Provider</param> /// <returns>Exit Code</returns> private static async Task <int> DeleteAlias(Data.MailiasConfig config, Args args, Data.IProvider provider) { var exists = (await provider.GetAliases(config)) .Any(alias => alias.SourceAddress == args.DeleteAlias); if (exists) { if (args.Force || YesNo("Really delete alias '" + args.DeleteAlias + "'?")) { var result = await provider.DeleteAliasAddress(args.DeleteAlias); if (result == Data.DeleteResult.Success) { return(0); } if (!args.Silent) { CoEx.WriteLine("Delete failed."); } return(1); } else { return(0); } } else { if (!args.Silent) { CoEx.WriteLine("Alias not found or didn't match the format of an alias which was created with this application."); } return(1); } }
/// <summary> /// Create an alias /// </summary> /// <param name="config">Config</param> /// <param name="args">CMD Args</param> /// <param name="provider">Provider</param> /// <returns>Exit code</returns> private static async Task <int> CreateAlias(Data.MailiasConfig config, Args args, Data.IProvider provider) { // Create a new random id var chars = "abcdefghijklmnopqrstuvwxyz0123456789"; var builder = new StringBuilder(); var random = new Random(); while (builder.Length < config.UniqeIdLength) { var index = random.Next(0, chars.Length - 1); builder.Append(chars[index]); } // Build the email address var prefix = string.IsNullOrWhiteSpace(config.Prefix) ? "" : config.Prefix + "."; var key = builder.ToString(); var mailAddress = $"{prefix}{args.CreateName}.{key}@{config.MailDomain}"; // Check if the email address already exists var exists = (await provider.GetAliases(config)) .FirstOrDefault(a => a.SourceAddress.StartsWith($"{prefix}{args.CreateName}.")); if (exists != null && args.Force == false) { if (!YesNo($"There is already an alias ({exists.SourceAddress}) with this name. Proceed?")) { return(1); } } // Delete the existing alias if (exists != null && args.DeleteExisting) { if (args.Force || YesNo($"Dou you want do delete the existing alias '{exists.SourceAddress}', before creating a new one?")) { var deleteExistingResult = await provider.DeleteAliasAddress(exists.SourceAddress); if (deleteExistingResult != Data.DeleteResult.Success && !args.Silent) { CoEx.WriteLine("Deleting existing alias failed."); return(1); } } } // Create the new alias var result = await provider.CreateAlias(mailAddress, config.TargetAddress); if (result == Data.CreateResult.Success || result == Data.CreateResult.AlreadyExists) { CoEx.WriteLine(mailAddress); return(0); } else { if (!args.Silent) { CoEx.WriteLine("Creation failed."); } return(1); } }