2018-06-15 02:57:03 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Teknik.MailService
|
|
|
|
|
{
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public class HMailService : IMailService
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
private readonly hMailServer.Application _App;
|
|
|
|
|
|
2018-06-21 05:01:36 +02:00
|
|
|
|
private string _Host { get; set; }
|
2018-06-15 02:57:03 +02:00
|
|
|
|
private string _Username { get; set; }
|
|
|
|
|
private string _Password { get; set; }
|
|
|
|
|
private string _Domain { get; set; }
|
|
|
|
|
|
|
|
|
|
private string _CounterServer { get; set; }
|
|
|
|
|
private string _CounterDatabase { get; set; }
|
|
|
|
|
private string _CounterUsername { get; set; }
|
|
|
|
|
private string _CounterPassword { get; set; }
|
|
|
|
|
private int _CounterPort { get; set; }
|
|
|
|
|
|
2018-06-21 05:01:36 +02:00
|
|
|
|
public HMailService(string host, string username, string password, string domain, string counterServer, string counterDatabase, string counterUsername, string counterPassword, int counterPort)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
2018-06-21 05:01:36 +02:00
|
|
|
|
_Host = host;
|
2018-06-15 02:57:03 +02:00
|
|
|
|
_Username = username;
|
|
|
|
|
_Password = password;
|
|
|
|
|
_Domain = domain;
|
|
|
|
|
|
|
|
|
|
_CounterServer = counterServer;
|
|
|
|
|
_CounterDatabase = counterDatabase;
|
|
|
|
|
_CounterUsername = counterUsername;
|
|
|
|
|
_CounterPassword = counterPassword;
|
|
|
|
|
_CounterPort = counterPort;
|
|
|
|
|
|
|
|
|
|
_App = InitApp();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void CreateAccount(string username, string password, int size)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
var domain = _App.Domains.ItemByName[_Domain];
|
|
|
|
|
var newAccount = domain.Accounts.Add();
|
|
|
|
|
newAccount.Address = username;
|
|
|
|
|
newAccount.Password = password;
|
|
|
|
|
newAccount.Active = true;
|
|
|
|
|
newAccount.MaxSize = size;
|
|
|
|
|
|
|
|
|
|
newAccount.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public bool AccountExists(string username)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
GetAccount(username);
|
|
|
|
|
// We didn't error out, so the email exists
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void DeleteAccount(string username)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
2018-06-21 05:01:36 +02:00
|
|
|
|
var account = GetAccount(username);
|
2018-06-16 08:31:52 +02:00
|
|
|
|
if (account != null)
|
|
|
|
|
{
|
|
|
|
|
account.Delete();
|
|
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void EnableAccount(string username)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
EditActivity(username, true);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void DisableAccount(string username)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
EditActivity(username, false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void EditActivity(string username, bool active)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
var account = GetAccount(username);
|
|
|
|
|
account.Active = active;
|
|
|
|
|
account.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void EditMaxEmailsPerDay(string username, int maxPerDay)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
//We need to check the actual git database
|
|
|
|
|
MysqlDatabase mySQL = new MysqlDatabase(_CounterServer, _CounterDatabase, _CounterUsername, _CounterPassword, _CounterPort);
|
|
|
|
|
string sql = @"INSERT INTO mailcounter.counts (qname, lastdate, qlimit, count) VALUES ({1}, NOW(), {0}, 0)
|
|
|
|
|
ON DUPLICATE KEY UPDATE qlimit = {0}";
|
|
|
|
|
mySQL.Execute(sql, new object[] { maxPerDay, username });
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void EditMaxSize(string username, int size)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
var account = GetAccount(username);
|
|
|
|
|
account.MaxSize = size;
|
|
|
|
|
account.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public void EditPassword(string username, string password)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
var account = GetAccount(username);
|
|
|
|
|
account.Password = password;
|
|
|
|
|
account.Save();
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-16 08:31:52 +02:00
|
|
|
|
public DateTime LastActive(string username)
|
2018-06-15 02:57:03 +02:00
|
|
|
|
{
|
|
|
|
|
var account = GetAccount(username);
|
|
|
|
|
return (DateTime)account.LastLogonTime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private hMailServer.Application InitApp()
|
|
|
|
|
{
|
|
|
|
|
var app = new hMailServer.Application();
|
|
|
|
|
app.Connect();
|
|
|
|
|
app.Authenticate(_Username, _Password);
|
|
|
|
|
|
|
|
|
|
return app;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private hMailServer.Account GetAccount(string username)
|
|
|
|
|
{
|
|
|
|
|
var domain = _App.Domains.ItemByName[_Domain];
|
|
|
|
|
return domain.Accounts.ItemByAddress[username];
|
|
|
|
|
}
|
2019-01-28 00:28:03 +01:00
|
|
|
|
|
|
|
|
|
public bool Enabled(string username)
|
|
|
|
|
{
|
|
|
|
|
var account = GetAccount(username);
|
|
|
|
|
return account.Active;
|
|
|
|
|
}
|
2018-06-15 02:57:03 +02:00
|
|
|
|
}
|
|
|
|
|
}
|