ircsharp/IRCRobots/ConnectionParams.cs

60 lines
2.1 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace IRCRobots
{
public class ConnectionParams
{
public string Nickname { get; set; }
public string Host { get; set; }
public int Port { get; set; }
public bool UseTLS { get; set; }
public string? Username { get; set; } = null;
public string? Realname { get; set; } = null;
public string? Bindhost { get; set; } = null;
public string? Password { get; set; } = null;
public bool VerifyTLS { get; set; } = true;
public SASLParams? SASL { get; set; } = null;
public STSPolicy? STS { get; set; } = null;
public ResumePolicy? Resume { get; set; } = null;
public int Reconnect { get; set; } = 10; // seconds
public IEnumerable<string> AltNicknames { get; set; } = new List<string>();
public IEnumerable<string> Autojoin { get; set; } = new List<string>();
public static ConnectionParams FromHostString(string nickName, string hostString)
{
var result = new ConnectionParams {Nickname = nickName};
var ipv6host = Regex.Matches(hostString, @"\[([a-fA-F0-9:]+)\]").SingleOrDefault();
var portString = "";
if (ipv6host is { Index: 0 })
{
result.Host = ipv6host.Groups[0].Value;
portString = hostString[(ipv6host.Index + ipv6host.Length)..];
}
else
{
var s = hostString.Split(':', 2);
result.Host = s[0];
portString = s[1];
}
if (string.IsNullOrWhiteSpace(portString))
{
portString = "6667";
result.UseTLS = false;
}
else
{
result.UseTLS = portString.StartsWith('+') || portString.StartsWith('~');
result.VerifyTLS = portString.StartsWith('~');
portString = portString[1..];
}
result.Port = int.Parse(portString);
return result;
}
}
}