Example #1
0
        public static List <string> GetOrphanedEmail(Config config, TeknikEntities db)
        {
            List <string> foundEmail = new List <string>();

            if (config.EmailConfig.Enabled)
            {
                List <User> curUsers = db.Users.ToList();

                // Connect to hmailserver COM
                var app = new hMailServer.Application();
                app.Connect();
                app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                var domain   = app.Domains.ItemByName[config.EmailConfig.Domain];
                var accounts = domain.Accounts;
                for (int i = 0; i < accounts.Count; i++)
                {
                    var account = accounts[i];

                    bool userExists = curUsers.Exists(u => UserHelper.GetUserEmailAddress(config, u.Username) == account.Address);
                    bool isReserved = UserHelper.GetReservedUsernames(config).Exists(r => UserHelper.GetUserEmailAddress(config, r).ToLower() == account.Address.ToLower());
                    if (!userExists && !isReserved)
                    {
                        foundEmail.Add(account.Address);
                    }
                }
            }
            return(foundEmail);
        }
Example #2
0
        private hMailServer.Application InitApp()
        {
            var app = new hMailServer.Application();

            app.Connect();
            app.Authenticate(_Username, _Password);

            return(app);
        }
Example #3
0
        static void Main()
        {
            Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);

             string databaseOldErrorMessage = "The database is too old for this version of hMailServer.";

             try
             {
            CommandLineParser.Parse();

            hMailServer.Application application = new hMailServer.Application();

             try
             {
                 application.Connect();
             }
             catch (Exception ex)
             {
                 if (!ex.Message.Contains(databaseOldErrorMessage))
                     throw ex;

             }

            int from = application.Database.CurrentVersion;
            int to = application.Database.RequiredVersion;

            if (from == to)
            {
               if (!CommandLineParser.ContainsArgument("/SilentIfOk") && !CommandLineParser.IsSilent())
                  MessageBox.Show("Your hMailServer database is already up to date.", "hMailServer Administrator");

               return;
            }

            if (!Authenticator.AuthenticateUser(application))
               return;

            formMain main = new formMain(application);

            if (!main.LoadSettings())
               return;

            if (!main.CreateUpgradePath())
               return;

            if (CommandLineParser.IsSilent())
            {
               // Silently perform the upgrade
               main.DoUpgrade();
               return;
            }

            // Do it the default way.
            Application.Run(main);
             }
             catch (Exception ex)
             {
             MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + "Please check the hMailServer error log for further details.", "hMailServer Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
             }
        }
Example #4
0
        public static List <string> GetInactiveAccounts(Config config, TeknikEntities db, int maxDays)
        {
            List <string> foundUsers = new List <string>();
            List <User>   curUsers   = db.Users.ToList();

            foreach (User user in curUsers)
            {
                // If the username is reserved, don't worry about it
                if (UserHelper.UsernameReserved(config, user.Username) || user.Username == Constants.SERVERUSER)
                {
                    continue;
                }

                // If they are Premium, don't worry about it either
                if (user.AccountType == AccountType.Premium && user.AccountStatus != AccountStatus.Banned)
                {
                    continue;
                }

                #region Inactivity Finding
                DateTime lastActivity = DateTime.Now;
                try
                {
                    lastActivity = UserHelper.GetLastAccountActivity(db, config, user);
                }
                catch
                {
                    continue;
                }

                TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity);

                // If older than max days, check their current usage
                if (inactiveTime >= new TimeSpan(maxDays, 0, 0, 0, 0))
                {
                    // Check the user's usage of the service.
                    bool noData = true;

                    // Any blog comments?
                    var blogCom = db.BlogComments.Where(c => c.UserId == user.UserId);
                    noData &= !(blogCom != null && blogCom.Any());

                    // Any blog posts?
                    var blogPosts = db.BlogPosts.Where(p => p.Blog.UserId == user.UserId);
                    noData &= !(blogPosts != null && blogPosts.Any());

                    // Any podcast comments?
                    var podCom = db.PodcastComments.Where(p => p.UserId == user.UserId);
                    noData &= !(podCom != null && podCom.Any());

                    // Any email?
                    if (config.EmailConfig.Enabled && UserHelper.UserEmailExists(config, UserHelper.GetUserEmailAddress(config, user.Username)))
                    {
                        var app = new hMailServer.Application();
                        app.Connect();
                        app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                        try
                        {
                            var domain  = app.Domains.ItemByName[config.EmailConfig.Domain];
                            var account = domain.Accounts.ItemByAddress[UserHelper.GetUserEmailAddress(config, user.Username)];
                            noData &= ((account.Messages.Count == 0) && ((int)account.Size == 0));
                        }
                        catch { }
                    }

                    // Any git repos?
                    if (config.GitConfig.Enabled && UserHelper.UserGitExists(config, user.Username))
                    {
                        string email = UserHelper.GetUserEmailAddress(config, user.Username);
                        // We need to check the actual git database
                        MysqlDatabase mySQL   = new MysqlDatabase(config.GitConfig.Database.Server, config.GitConfig.Database.Database, config.GitConfig.Database.Username, config.GitConfig.Database.Password, config.GitConfig.Database.Port);
                        string        sql     = @"SELECT * FROM gogs.repository
                                        LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
                                        WHERE gogs.user.login_name = {0}";
                        var           results = mySQL.Query(sql, new object[] { email });

                        noData &= !(results != null && results.Any());
                    }

                    if (noData)
                    {
                        // They have no data, so safe to delete them.
                        foundUsers.Add(user.Username);
                    }
                    continue;
                }
                #endregion
            }
            return(foundUsers);
        }
Example #5
0
        public static List<string> GetOrphanedEmail(Config config, TeknikEntities db)
        {
            List<string> foundEmail = new List<string>();
            if (config.EmailConfig.Enabled)
            {
                List<User> curUsers = db.Users.ToList();

                // Connect to hmailserver COM
                var app = new hMailServer.Application();
                app.Connect();
                app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                var accounts = domain.Accounts;
                for (int i = 0; i < accounts.Count; i++)
                {
                    var account = accounts[i];

                    bool userExists = curUsers.Exists(u => UserHelper.GetUserEmailAddress(config, u.Username) == account.Address);
                    bool isReserved = UserHelper.GetReservedUsernames(config).Exists(r => UserHelper.GetUserEmailAddress(config, r).ToLower() == account.Address.ToLower());
                    if (!userExists && !isReserved)
                    {
                        foundEmail.Add(account.Address);
                    }
                }
            }
            return foundEmail;
        }
Example #6
0
        public static List<string> GetInactiveAccounts(Config config, TeknikEntities db, int maxDays)
        {
            List<string> foundUsers = new List<string>();
            List<User> curUsers = db.Users.ToList();
            foreach (User user in curUsers)
            {
                // If the username is reserved, don't worry about it
                if (UserHelper.UsernameReserved(config, user.Username))
                {
                    continue;
                }

                #region Inactivity Finding
                DateTime lastActivity = UserHelper.GetLastAccountActivity(db, config, user);

                TimeSpan inactiveTime = DateTime.Now.Subtract(lastActivity);

                // If older than max days, check their current usage
                if (inactiveTime >= new TimeSpan(maxDays, 0, 0, 0, 0))
                {
                    // Check the user's usage of the service.
                    bool noData = true;

                    // Any blog comments?
                    var blogCom = db.BlogComments.Where(c => c.UserId == user.UserId);
                    noData &= !(blogCom != null && blogCom.Any());

                    // Any blog posts?
                    var blogPosts = db.BlogPosts.Where(p => p.Blog.UserId == user.UserId);
                    noData &= !(blogPosts != null && blogPosts.Any());

                    // Any podcast comments?
                    var podCom = db.PodcastComments.Where(p => p.UserId == user.UserId);
                    noData &= !(podCom != null && podCom.Any());

                    // Any email?
                    if (config.EmailConfig.Enabled)
                    {
                        var app = new hMailServer.Application();
                        app.Connect();
                        app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                        try
                        {
                            var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                            var account = domain.Accounts.ItemByAddress[UserHelper.GetUserEmailAddress(config, user.Username)];
                            noData &= ((account.Messages.Count == 0) && ((int)account.Size == 0));
                        }
                        catch { }
                    }

                    // Any git repos?
                    if (config.GitConfig.Enabled)
                    {
                        string email = UserHelper.GetUserEmailAddress(config, user.Username);
                        // We need to check the actual git database
                        MysqlDatabase mySQL = new MysqlDatabase(config.GitConfig.Database);
                        string sql = @"SELECT * FROM gogs.repository
                                        LEFT JOIN gogs.action ON gogs.user.id = gogs.action.act_user_id
                                        WHERE gogs.user.login_name = {0}";
                        var results = mySQL.Query(sql, new object[] { email });

                        noData &= !(results != null && results.Any());
                    }

                    if (noData)
                    {
                        // They have no data, so safe to delete them.
                        foundUsers.Add(user.Username);
                    }
                    continue;
                }
                #endregion
            }
            return foundUsers;
        }
Example #7
0
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string databaseOldErrorMessage = "The database is too old for this version of hMailServer.";

            try
            {
                CommandLineParser.Parse();

                hMailServer.Application application = new hMailServer.Application();

                try
                {
                    application.Connect();
                }
                catch (Exception ex)
                {
                    if (!ex.Message.Contains(databaseOldErrorMessage))
                    {
                        throw ex;
                    }
                }


                int from = application.Database.CurrentVersion;
                int to   = application.Database.RequiredVersion;

                if (from == to)
                {
                    if (!CommandLineParser.ContainsArgument("/SilentIfOk") && !CommandLineParser.IsSilent())
                    {
                        MessageBox.Show("Your hMailServer database is already up to date.", "hMailServer Administrator");
                    }

                    return;
                }

                if (!Authenticator.AuthenticateUser(application))
                {
                    return;
                }

                formMain main = new formMain(application);

                if (!main.LoadSettings())
                {
                    return;
                }

                if (!main.CreateUpgradePath())
                {
                    return;
                }

                if (CommandLineParser.IsSilent())
                {
                    // Silently perform the upgrade
                    main.DoUpgrade();
                    return;
                }

                // Do it the default way.
                Application.Run(main);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message + Environment.NewLine + Environment.NewLine + "Please check the hMailServer error log for further details.", "hMailServer Administrator", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
Example #8
0
 public static void DeleteUserEmail(Config config, string email)
 {
     try
     {
         // If Email Server is enabled
         if (config.EmailConfig.Enabled)
         {
             var app = new hMailServer.Application();
             app.Connect();
             app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);
             var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
             var account = domain.Accounts.ItemByAddress[email];
             if (account != null)
             {
                 account.Delete();
             }
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Unable to delete email account.", ex);
     }
 }
Example #9
0
 public static void EditUserEmailPassword(Config config, string email, string password)
 {
     try
     {
         // If Email Server is enabled
         if (config.EmailConfig.Enabled)
         {
             var app = new hMailServer.Application();
             app.Connect();
             app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);
             var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
             var account = domain.Accounts.ItemByAddress[email];
             account.Password = password;
             account.Save();
         }
     }
     catch (Exception ex)
     {
         throw new Exception("Unable to edit email account password.", ex);
     }
 }
Example #10
0
        public static void AddUserEmail(Config config, string email, string password)
        {
            try
            {
                // If Email Server is enabled
                if (config.EmailConfig.Enabled)
                {
                    // Connect to hmailserver COM
                    var app = new hMailServer.Application();
                    app.Connect();
                    app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                    var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                    var newAccount = domain.Accounts.Add();
                    newAccount.Address = email;
                    newAccount.Password = password;
                    newAccount.Active = true;
                    newAccount.MaxSize = config.EmailConfig.MaxSize;

                    newAccount.Save();
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Unable to add email.", ex);
            }
        }
Example #11
0
        public static DateTime UserEmailLastActive(Config config, string email)
        {
            DateTime lastActive = new DateTime(1900, 1, 1);

            if (config.EmailConfig.Enabled)
            {
                var app = new hMailServer.Application();
                app.Connect();
                app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                try
                {
                    var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                    var account = domain.Accounts.ItemByAddress[email];
                    DateTime lastEmail = (DateTime)account.LastLogonTime;
                    if (lastActive < lastEmail)
                        lastActive = lastEmail;
                }
                catch { }
            }
            return lastActive;
        }
Example #12
0
        public static bool UserEmailExists(Config config, string email)
        {
            // If Email Server is enabled
            if (config.EmailConfig.Enabled)
            {
                // Connect to hmailserver COM
                var app = new hMailServer.Application();
                app.Connect();
                app.Authenticate(config.EmailConfig.Username, config.EmailConfig.Password);

                try
                {
                    var domain = app.Domains.ItemByName[config.EmailConfig.Domain];
                    var account = domain.Accounts.ItemByAddress[email];
                    // We didn't error out, so the email exists
                    return true;
                }
                catch { }
            }
            return false;
        }
Example #13
0
 public void Connect()
 {
     _object.Connect();
 }