Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<Query Kind="Program">
<NuGetReference>cPanelSharp</NuGetReference>
<NuGetReference>Newtonsoft.Json</NuGetReference>
<Namespace>cPanelSharp</Namespace>
<Namespace>Newtonsoft.Json</Namespace>
<Namespace>Newtonsoft.Json.Linq</Namespace>
</Query>
cPanelClient Client = new cPanelClient("silvenga", "ghost.mxroute.com", password: Util.GetPassword("mxroute-silvenga"), cpanel: true);
void Main()
{
CreateDomain("enga.me", "silvenga.net");
}
public void CreateDomain(string domain, string forwardTo)
{
var domainExists = ListAliasDomains().Any(x => x == domain);
if (!domainExists)
{
Console.WriteLine("Creating alais.");
CreateAliasDomains(domain);
}
var forwardExists = ListDomainForwards().Any(x => x.Dest == domain);
if (!forwardExists)
{
Console.WriteLine("Creating forward.");
CreateDomainForwards(domain, forwardTo);
}
}
public IList<string> ListAliasDomains()
{
var json = Client.Api2("Park", "listparkeddomains");
var result = JObject.Parse(json);
return result["cpanelresult"]["data"].ToArray()
.Select(x => x["domain"])
.Select(x => x.ToObject<string>())
.ToList();
}
public void CreateAliasDomains(string domain)
{
Client.Api2("Park", "park", param: new
{
domain = domain
});
}
public IList<(string Forward, string Dest)> ListDomainForwards()
{
var json = Client.Api2("Email", "listdomainforwards");
var result = JObject.Parse(json);
return result["cpanelresult"]["data"].ToArray()
.Select(x => (x["forward"].ToObject<string>(), x["dest"].ToObject<string>()))
.ToList();
}
public void CreateDomainForwards(string from, string to)
{
Client.Api2("Email", "adddomainforward", param: new
{
domain = from,
destdomain = to
}).Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment