private static void RemoveUserMailboxes(int tenant, string user, ILog log) { var mailboxIterator = new MailboxIterator(tenant, user); var mailbox = mailboxIterator.First(); if (mailboxIterator.IsDone) { log.Info("There are no user's mailboxes for deletion"); return; } while (!mailboxIterator.IsDone) { try { if (!mailbox.UserId.Equals(user)) { throw new Exception( string.Format("Mailbox (id:{0}) user '{1}' not equals to removed user: '******'", mailbox.MailBoxId, mailbox.UserId, user)); } RemoveMailboxData(mailbox, true, log); } catch (Exception ex) { log.ErrorFormat("RemoveMailboxData(MailboxId: {0}) failed. Error: {1}", mailbox.MailBoxId, ex); } mailbox = mailboxIterator.Next(); } RemoveUserMailDirectory(tenant, user, log); }
public void ClearMailGarbage(ManualResetEvent resetEvent) { _log.Debug("Begin ClearMailGarbage()"); var mailboxIterator = new MailboxIterator(_mailBoxManager); var mailbox = mailboxIterator.First(); while (!mailboxIterator.IsDone) { try { _log.Info("Search garbage on MailboxId = {0}, email = '{1}', tenant = '{2}'", mailbox.MailBoxId, mailbox.Account, mailbox.TenantId); if (!RemoveLongDeadTenantGarbageMailData(mailbox.TenantId)) { if (resetEvent.WaitOne(0)) { _log.Debug("ClearMailGarbage() is canceled"); return; } if (!RemoveTerminatedUserGarbageMailData(mailbox.TenantId, mailbox.UserId)) { if (resetEvent.WaitOne(0)) { _log.Debug("ClearMailGarbage() is canceled"); return; } RemoveGarbageMailboxData(mailbox); } } if (resetEvent.WaitOne(0)) { _log.Debug("ClearMailGarbage() is canceled"); return; } mailbox = mailboxIterator.Next(); } catch (Exception ex) { _log.Error(ex.ToString()); } if (resetEvent.WaitOne(0)) { _log.Debug("ClearMailGarbage() is canceled"); return; } } _log.Debug("End ClearMailGarbage()\r\n"); }
public void ClearMailGarbage(CancellationToken cancelToken) { Log.Debug("Begin ClearMailGarbage()"); var tasks = new List <Task>(); var mailboxIterator = new MailboxIterator(isRemoved: null, log: Log); var mailbox = mailboxIterator.First(); while (!mailboxIterator.IsDone) { try { if (cancelToken.IsCancellationRequested) { break; } var mb = mailbox; var task = Queue(() => ClearGarbage(mb), cancelToken); tasks.Add(task); if (tasks.Count == Config.MaxTasksAtOnce) { Log.Info("Wait all tasks to complete"); Task.WaitAll(tasks.ToArray()); tasks = new List <Task>(); } } catch (Exception ex) { Log.Error(ex.ToString()); } if (!cancelToken.IsCancellationRequested) { mailbox = mailboxIterator.Next(); continue; } Log.Debug("ClearMailGarbage: IsCancellationRequested. Quit."); break; } RemoveUselessMsDomains(); Log.Debug("End ClearMailGarbage()\r\n"); }
public void ClearUserMail(Guid userId, Tenant tenant = null) { var log = LoggerFactory.GetLogger(LoggerFactory.LoggerType.Log4Net, "ASC.Mail.Cleaner"); var tenantId = tenant != null ? tenant.TenantId : CoreContext.TenantManager.GetCurrentTenant().TenantId; log.Info("ClearUserMail(userId: '{0}' tenant: {1})", userId, tenantId); var userIdStr = userId.ToString(); var mailboxIterator = new MailboxIterator(_mailBoxManager, userIdStr, tenantId); var mailbox = mailboxIterator.First(); if (mailboxIterator.IsDone) { log.Info("There are no user's mailboxes for deletion"); return; } while (!mailboxIterator.IsDone) { try { if (!mailbox.UserId.Equals(userIdStr)) { throw new Exception( string.Format("Mailbox (id:{0}) user '{1}' not equals to removed user: '******'", mailbox.MailBoxId, mailbox.UserId, userIdStr)); } RemoveMailboxData(mailbox, true, log); mailbox = mailboxIterator.Next(); } catch (Exception ex) { log.Error("RemoveMailboxData(MailboxId: {0}) failed. Error: {1}", mailbox.MailBoxId, ex); } } RemoveUserMailDirectory(tenantId, userIdStr, log); }
private static void Main(string[] args) { var options = new Options(); if (!CommandLine.Parser.Default.ParseArgumentsStrict(args, options, () => Console.WriteLine(@"Bad command line parameters."))) { return; } try { var mailboxIterator = new MailboxIterator(); var mailbox = mailboxIterator.First(); if (mailbox == null) { Console.WriteLine(@"Accounts not found."); ShowAnyKey(options); return; } StringBuilder sbuilder = null; List <MailAccount> accounts = null; if (!options.NeedJson) { sbuilder = new StringBuilder(); } else { accounts = new List <MailAccount>(); } var i = 0; while (!mailboxIterator.IsDone) { if (mailbox.IsRemoved || !mailbox.IsTeamlab) { mailbox = mailboxIterator.Next(); continue; } var current = new StringBuilder(); var userInfo = mailbox.GetUserInfo(); var isUserRemoved = userInfo == null || userInfo.Equals(Constants.LostUser); var user = isUserRemoved ? "user is removed" : userInfo.UserName; current.AppendFormat("#{0}. Id: {1} MailBox: {2}, Tenant: {3}, User: '******' ID: {5}\r\n", ++i, mailbox.MailBoxId, mailbox.EMail, mailbox.TenantId, user, mailbox.UserId); current.AppendLine(); var inType = mailbox.Imap ? IMAP : POP3; current.AppendFormat("\t\t{0} settings:\r\n", inType); current.AppendFormat("\t\tLogin:\t\t{0}\r\n", mailbox.Account); string inPassword; if (!mailbox.IsOAuth) { inPassword = string.IsNullOrEmpty(mailbox.Password) ? "[-=! ERROR: Invalid Decription !=-]" : mailbox.Password; } else { inPassword = "******"; } current.AppendFormat("\t\tPassword:\t{0}\r\n", inPassword); current.AppendFormat("\t\tHost:\t\t{0}\r\n", mailbox.Server); current.AppendFormat("\t\tPort:\t\t{0}\r\n", mailbox.Port); var inAuthentication = Enum.GetName(typeof(SaslMechanism), mailbox.Authentication); current.AppendFormat("\t\tAuthType:\t{0}\r\n", inAuthentication); var inEncryption = Enum.GetName(typeof(EncryptionType), mailbox.Encryption); current.AppendFormat("\t\tEncryptType:\t{0}\r\n", inEncryption); current.AppendLine(); current.AppendFormat("\t\t{0} settings:\r\n", SMTP); current.AppendFormat("\t\tSmtpAuth:\t{0}\r\n", mailbox.SmtpAuth); if (mailbox.SmtpAuth) { current.AppendFormat("\t\tSmtpLogin:\t{0}\r\n", mailbox.SmtpAccount); if (!mailbox.IsOAuth) { current.AppendFormat("\t\tSmtPassword:\t{0}\r\n", string.IsNullOrEmpty(mailbox.SmtpPassword) ? "[-=! ERROR: Invalid Decription !=-]" : mailbox.SmtpPassword); } else { current.AppendFormat("\t\tSmtPassword:\t[-=! OAuth: no password stored !=-]\r\n"); } } current.AppendFormat("\t\tHost:\t\t{0}\r\n", mailbox.SmtpServer); current.AppendFormat("\t\tPort:\t\t{0}\r\n", mailbox.SmtpPort); var outAuthentication = Enum.GetName(typeof(SaslMechanism), mailbox.SmtpAuthentication); current.AppendFormat("\t\tAuthType:\t{0}\r\n", outAuthentication); var outEncryption = Enum.GetName(typeof(EncryptionType), mailbox.SmtpEncryption); current.AppendFormat("\t\tEncryptType:\t{0}\r\n", outEncryption); current.AppendLine(); Console.WriteLine(current.ToString()); if (!options.NeedJson) { if (sbuilder != null) { sbuilder.Append(current); } } else { if (accounts != null) { accounts.Add(new MailAccount { email = mailbox.EMail.Address, tenantId = mailbox.TenantId, user = user, userId = mailbox.UserId, settings = new List <MailAccountSetting> { new MailAccountSetting { type = inType, host = mailbox.Server, port = mailbox.Port.ToString(), authentication = inAuthentication, encryption = inEncryption, login = mailbox.Account, password = inPassword }, new MailAccountSetting { type = SMTP, host = mailbox.SmtpServer, port = mailbox.SmtpPort.ToString(), authentication = outAuthentication, encryption = outEncryption, login = mailbox.SmtpAccount, password = inPassword } } }); } } mailbox = mailboxIterator.Next(); } string text = null; if (!options.NeedJson) { if (sbuilder != null) { text = sbuilder.ToString(); } } else { text = JsonConvert.SerializeObject(accounts, Formatting.Indented); } if (!string.IsNullOrEmpty(text)) { Console.WriteLine(@"Try StoreToFile"); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, options.NeedJson ? "mailboxes.json" : "mailboxes.txt"); var pathFile = StoreToFile(text, path, true); Console.WriteLine(@"[SUCCESS] File was stored into path ""{0}""", pathFile); } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } ShowAnyKey(options); }
private static void Main() { try { XmlConfigurator.Configure(); var mailBoxManager = new MailBoxManager(new NullLogger()); var mailboxIterator = new MailboxIterator(mailBoxManager); var mailbox = mailboxIterator.First(); if (mailbox == null) { Console.WriteLine("Accounts not found."); ShowAnyKey(); return; } var sbuilder = new StringBuilder(); var i = 0; while (!mailboxIterator.IsDone) { if (!mailbox.IsRemoved) { var current = new StringBuilder(); var userInfo = mailbox.GetUserInfo(); current.AppendFormat("#{0}. Id: {1} MailBox: {2}, Tenant: {3}, User: '******' \r\n", ++i, mailbox.MailBoxId, mailbox.EMail, mailbox.TenantId, userInfo != null ? userInfo.UserName : mailbox.UserId); current.AppendLine(); current.AppendFormat("\t\t{0} settings:\r\n", mailbox.Imap ? "Imap4" : "Pop3"); current.AppendFormat("\t\tLogin:\t\t{0}\r\n", mailbox.Account); current.AppendFormat("\t\tPassword:\t{0}\r\n", string.IsNullOrEmpty(mailbox.Password) ? "[-=! ERROR: Invalid Decription !=-]" : mailbox.Password); current.AppendFormat("\t\tHost:\t\t{0}\r\n", mailbox.Server); current.AppendFormat("\t\tPort:\t\t{0}\r\n", mailbox.Port); current.AppendFormat("\t\tAuthType:\t{0}\r\n", Enum.GetName(typeof(SaslMechanism), mailbox.AuthenticationTypeIn)); current.AppendFormat("\t\tEncryptType:\t{0}\r\n", Enum.GetName(typeof(EncryptionType), mailbox.IncomingEncryptionType)); current.AppendLine(); current.Append("\t\tSmtp settings:\r\n"); current.AppendFormat("\t\tSmtpAuth:\t{0}\r\n", mailbox.SmtpAuth); if (mailbox.SmtpAuth) { current.AppendFormat("\t\tSmtpLogin:\t{0}\r\n", mailbox.SmtpAccount); current.AppendFormat("\t\tSmtPassword:\t{0}\r\n", string.IsNullOrEmpty(mailbox.SmtpPassword) ? "[-=! ERROR: Invalid Decription !=-]" : mailbox.SmtpPassword); } current.AppendFormat("\t\tHost:\t\t{0}\r\n", mailbox.SmtpServer); current.AppendFormat("\t\tPort:\t\t{0}\r\n", mailbox.SmtpPort); current.AppendFormat("\t\tAuthType:\t{0}\r\n", Enum.GetName(typeof(SaslMechanism), mailbox.AuthenticationTypeSmtp)); current.AppendFormat("\t\tEncryptType:\t{0}\r\n", Enum.GetName(typeof(EncryptionType), mailbox.OutcomingEncryptionType)); current.AppendLine(); Console.WriteLine(current.ToString()); sbuilder.Append(current); } mailbox = mailboxIterator.Next(); } Console.WriteLine("Try StoreToFile"); var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "mailboxes.txt"); var pathFile = StoreToFile(sbuilder.ToString(), path, true); Console.WriteLine("[SUCCESS] File was stored into path \"{0}\"", pathFile); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } ShowAnyKey(); }
public void ClearMailGarbage(ManualResetEvent resetEvent = null) { _log.Debug("Begin ClearMailGarbage()"); var mailboxIterator = new MailboxIterator(_mailBoxManager); var mailbox = mailboxIterator.First(); var tasks = new List <Task>(); while (!mailboxIterator.IsDone) { try { var mb = mailbox; var task = _taskFactory.StartNew(() => ClearGarbage(mb), TaskCreationOptions.LongRunning); _log.Debug("Start Task {0}", task.Id); tasks.Add(task); if (tasks.Count == Config.MaxTasksAtOnce) { _log.Info("Wait any task to complete"); var indexTask = Task.WaitAny(tasks.ToArray()); if (indexTask > -1) { var outTask = tasks[indexTask]; FreeTask(outTask, tasks); } var tasks2Free = tasks.Where( t => t.Status == TaskStatus.Canceled || t.Status == TaskStatus.Faulted || t.Status == TaskStatus.RanToCompletion).ToList(); if (tasks2Free.Any()) { _log.Info("Need free next tasks = {0}: ({1})", tasks2Free.Count, string.Join(",", tasks2Free.Select(t => t.Id.ToString(CultureInfo.InvariantCulture)))); tasks2Free.ForEach(tsk => FreeTask(tsk, tasks)); } } mailbox = mailboxIterator.Next(); } catch (Exception ex) { _log.Error(ex.ToString()); } if (resetEvent == null || !resetEvent.WaitOne(0)) { continue; } _log.Debug("ClearMailGarbage() is canceled"); } _log.Debug("End ClearMailGarbage()\r\n"); }