public static void SynchronizeTrash(String mailAddress) { MailAccount mailAccountTrash; MailsTask newTaskTrash; String mailAddressTrash = mailAddress + "/TRASH"; if (MailsTasksHandler.IsWorking(mailAddressTrash)) { return; } using (ISession session = NHibernateManager.OpenSession()) { mailAccountTrash = MailAccount.FindByAddress(mailAddress, session, false); try { mailAccountTrash.ConnectFull(session); } catch (NullReferenceException exc) { Log.LogException(exc, "Direccion inexistente en la base de datos: " + mailAccountTrash + "."); return; } catch (InvalidAuthenticationException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + ", ha cambiado el password."); return; } catch (SocketException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + "."); return; } Label trashLabel = Label.FindBySystemName(mailAccountTrash, "Trash", session); newTaskTrash = new MailsTask(mailAccountTrash.GetUIDLocal(session, trashLabel.Entity.SystemName, true), mailAccountTrash.GetUIDLocal(session, trashLabel.Entity.SystemName, false), mailAccountTrash.GetUIDExternalFrom(trashLabel.Entity.Name, true), mailAccountTrash.GetUIDExternalFrom(trashLabel.Entity.Name, false), trashLabel, mailAccountTrash); session.Close(); } if (!newTaskTrash.HasFinished) { MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressTrash] = newTaskTrash; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newTaskTrash); } }
public void TextFixtureSetUp() { ITransaction tran = session.BeginTransaction(); anAccount = new MailAccount("*****@*****.**", "EAAAAJEOi8buiitbWq6IHRQ/+WwSmQhmymzpaiMrjI/s6k9/"); MailAccount existingAccount = MailAccount.FindByAddress("*****@*****.**", this.session); if (existingAccount == null) { this.session.SaveOrUpdate(anAccount.Entity); } else { this.anAccount = existingAccount; } this.aMail = new Mail(new MailEntity()); this.aMail.Entity.Subject = "Mail de prueba"; this.aMail.Entity.MailAccountEntity = anAccount.Entity; this.session.SaveOrUpdate(aMail.Entity); tran.Commit(); }
public static void StartSynchronization(String mailAddress, Boolean forwardOnly = true) { MailAccount mailAccountAll; MailsTask newAllTask; MailsTask newTaskTrash; MailsTask newSpamTask; String mailAddressAll = mailAddress + "/ALL"; String mailAddressTrash = mailAddress + "/TRASH"; String mailAddressSpam = mailAddress + "/SPAM"; if (MailsTasksHandler.IsAnyWorking(new String[] { mailAddressAll, mailAddressTrash, mailAddressSpam })) { return; //alguna tarea se encuentra trabajando } using (ISession session = NHibernateManager.OpenSession()) { mailAccountAll = MailAccount.FindByAddress(mailAddress, session, false); try { mailAccountAll.ConnectFull(session); } catch (NullReferenceException exc) { Log.LogException(exc, "Direccion inexistente en la base de datos: " + mailAccountAll + "."); return; } catch (InvalidAuthenticationException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + ", ha cambiado el password."); return; } catch (SocketException exc) { Log.LogException(exc, "No se pudo conectar a imap con la direccion: " + mailAddress + "."); return; } Label allLabel = Label.FindBySystemName(mailAccountAll, "All", session); Label trashLabel = Label.FindBySystemName(mailAccountAll, "Trash", session); Label spamLabel = Label.FindBySystemName(mailAccountAll, "Junk", session); newAllTask = new MailsTask(mailAccountAll.GetUIDLocal(session, allLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, allLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(allLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(allLabel.Entity.Name, false), //firstUidExternal allLabel, mailAccountAll); newTaskTrash = new MailsTask(mailAccountAll.GetUIDLocal(session, trashLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, trashLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(trashLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(trashLabel.Entity.Name, false), //firstUidExternal trashLabel, null); newSpamTask = new MailsTask(mailAccountAll.GetUIDLocal(session, spamLabel.Entity.SystemName, true), //lastUidLocal mailAccountAll.GetUIDLocal(session, spamLabel.Entity.SystemName, false), //firstUidLocal mailAccountAll.GetUIDExternalFrom(spamLabel.Entity.Name, true), //lastUidExternal mailAccountAll.GetUIDExternalFrom(spamLabel.Entity.Name, false), //firstUidExternal spamLabel, null); session.Close(); } if (!newAllTask.HasFinished) //puede ser que no se necesite sincronizar dependiendo de los numeros { if (newAllTask.HighestUidLocal != 0) //si no es la primera vez, que sincronice hacia adelante sin limites { newAllTask.SetUnlimitedForwarding(true); } newAllTask.SetForwardOnly(forwardOnly); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressAll] = newAllTask; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newAllTask); } if (!newTaskTrash.HasFinished) { MailAccount mailAccountTrash = mailAccountAll.Clone(); //crear otra conexion a IMAP newTaskTrash.SetMailAccount(mailAccountTrash); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressTrash] = newTaskTrash; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newTaskTrash); } if (!newSpamTask.HasFinished) { MailAccount mailAccountSpam = mailAccountAll.Clone(); //crear otra conexion a IMAP newSpamTask.SetMailAccount(mailAccountSpam); MailsTasksHandler.LockTasksList(); MailsTasksHandler.TasksList[mailAddressSpam] = newSpamTask; MailsTasksHandler.UnlockTasksList(); MailsTasksHandler.StartMailsTask(newSpamTask); } }
private IList <MailAccount> ValidateUserMailAccounts(UserViewModel userView, User actualUser, ISession session) { String exceptionMessage = ""; Boolean hasMainAccount = false; Boolean checkMainAccounts = true; IList <MailAccount> connectedMailAccounts = new List <MailAccount>(); MailAccount mailAccount; MailAccount databaseMailAccount; foreach (MailAccountViewModel mailAccountView in userView.ListMailAccounts) { #region Valida Credenciales try { if (actualUser != null && String.IsNullOrEmpty(mailAccountView.Password) && actualUser.GetAccounts().Any(x => x.Entity.Address == mailAccountView.Address)) //si el usuario ya tiene la cuenta y el password esta vacio { mailAccount = new MailAccount(mailAccountView.Address, actualUser.GetAccounts().Single(x => x.Entity.Address == mailAccountView.Address).Entity.Password); } else { mailAccount = new MailAccount(mailAccountView.Address, CryptoHelper.EncryptDefaultKey(mailAccountView)); } mailAccount.ConnectLight(); //si pasa este punto es que esta bien y va a ser devuelta if (mailAccountView.IsMainAccount) { mailAccount.SetAsMainAccount(true); } else { mailAccount.SetAsMainAccount(false); } connectedMailAccounts.Add(mailAccount); } catch (ArgumentNullException) { exceptionMessage += "La dirección de correo (" + mailAccountView.Address + ") no posee contraseña.\n"; } catch (InvalidAuthenticationException) { exceptionMessage += "La dirección de correo (" + mailAccountView.Address + ") o la contraseña no son válidos.\n"; } #endregion #region Valida Cuentas Principales if (hasMainAccount && mailAccountView.IsMainAccount && checkMainAccounts) { exceptionMessage += "Sólo puede existir una dirección de correo principal.\n"; checkMainAccounts = false; } else if (!hasMainAccount && mailAccountView.IsMainAccount) { hasMainAccount = true; } #endregion #region Valida Unicidad en Base de datos databaseMailAccount = MailAccount.FindByAddress(mailAccountView.Address, session, true); if (databaseMailAccount != null && databaseMailAccount.Entity.User.Username != userView.Username) { exceptionMessage += "La dirección: " + mailAccountView.Address + " ya se encuentra " + "asociada a otro usuario Glimpse.\n"; } #endregion } #region Validaciones de Requerimientos Obligatorios if (!hasMainAccount) { exceptionMessage += "Una dirección de correo debe ser indicada como principal.\n"; } if (userView.ListMailAccounts.Count == 0) { exceptionMessage += "El usuario Glimpse debe tener al menos una dirección de correo.\n"; } #endregion #region Valida Unicidad en el Usuario var repeatedAccounts = userView.ListMailAccounts.GroupBy(x => x.Address).Where(gr => gr.Count() > 1); foreach (var repeatedAccount in repeatedAccounts) { exceptionMessage += "Se ha ingresado más de una vez la cuenta: " + repeatedAccount.Key + ", ingrese la misma una sola vez."; } #endregion if (exceptionMessage != "") { throw new GlimpseException(exceptionMessage); } else { return(connectedMailAccounts); //para no tener que conectar de vuelta despues } }
public ActionResult Login(UserViewModel userView, string returnUrl) { User user; MailAccount mailAccount; MailAccount existingMailAccount; ISession session = NHibernateManager.OpenSession(); ITransaction tran = session.BeginTransaction(); Boolean workingOffline = false; try { this.UpdateModel(userView); String cipherPassword = CryptoHelper.EncryptDefaultKey(userView); if (Glimpse.Models.User.IsEmail(userView.Username)) //si es un email { mailAccount = new MailAccount(userView.Username, cipherPassword); try { mailAccount.ConnectLight(); //si pasa este punto es que los datos ingresados son correctos } catch (SocketException) { workingOffline = true; mailAccount.ValidateCredentials(); } user = Glimpse.Models.User.FindByUsername(userView.Username, session); existingMailAccount = MailAccount.FindByAddress(userView.Username, session, false); if (user == null) { user = new User(userView.Username, cipherPassword); user.SaveOrUpdate(session); } else if (!CryptoHelper.PasswordsMatch(user.Entity.Password, cipherPassword)) { user.Entity.Password = cipherPassword; user.SaveOrUpdate(session); } if (existingMailAccount == null) { mailAccount.SetUser(user); mailAccount.SetOldestMailDate(); mailAccount.Deactivate(session); //llama a saveOrUpdate adentro } else { if (existingMailAccount.Entity.Password != mailAccount.Entity.Password) { existingMailAccount.Entity.Password = mailAccount.Entity.Password; existingMailAccount.SaveOrUpdate(session); } mailAccount.Entity = existingMailAccount.Entity; } if (!workingOffline) { mailAccount.UpdateLabels(session); } user.AddAccount(mailAccount); } else //si es un usuario glimpse { user = Glimpse.Models.User.FindByUsername(userView.Username, session); if (user == null) { this.ModelState.AddModelError("User", "Usuario inexistente."); tran.Rollback(); return(View(userView)); } else if (!CryptoHelper.PasswordsMatch(user.Entity.Password, cipherPassword)) { this.ModelState.AddModelError("User", "Contraseña incorrecta."); tran.Rollback(); return(View(userView)); } user.UpdateAccounts(session); //try //{ // user.ConnectLight(); // user.UpdateLabels(session); //} //catch (SocketException) { } } new CookieHelper().AddUsernameCookie(user.Entity.Username); FormsAuthentication.SetAuthCookie(userView.Username, true); tran.Commit(); Session[AccountController.USER_NAME] = user; return(RedirectToLocal(returnUrl)); } catch (InvalidOperationException) //model state invalido { tran.Rollback(); return(View(userView)); } catch (InvalidAuthenticationException) { tran.Rollback(); ModelState.AddModelError("", "La dirección de correo o la contraseña no son correctos."); return(View(userView)); } catch (Exception) { tran.Rollback(); ModelState.AddModelError("", "Existen problemas para iniciar sesión, intentalo de nuevo más tarde."); return(View(userView)); } finally { session.Close(); } }