using System;
using System.Collections.Generic;
using System.Text;
using Tgstation.Server.Api.Models.Internal;
namespace Tgstation.Server.Api.Models
{
///
/// for .
///
public sealed class IrcConnectionStringBuilder : ChatConnectionStringBuilder
{
///
public override bool Valid => Address != null && Port.HasValue && Port != 0 && UseSsl.HasValue && (PasswordType.HasValue ^ Password == null);
///
/// The IP address or URL of the IRC server.
///
public string? Address { get; set; }
///
/// The port the server runs on.
///
public ushort? Port { get; set; }
///
/// The nickname for the bot to use.
///
public string? Nickname { get; set; }
///
/// If the connection should be made using SSL.
///
public bool? UseSsl { get; set; }
///
/// The optional to use.
///
public IrcPasswordType? PasswordType { get; set; }
///
/// The optional password to use.
///
public string? Password { get; set; }
///
/// Initializes a new instance of the class.
///
public IrcConnectionStringBuilder()
{
}
///
/// Initializes a new instance of the class.
///
/// The connection string.
public IrcConnectionStringBuilder(string connectionString)
{
if (connectionString == null)
throw new ArgumentNullException(nameof(connectionString));
var splits = connectionString.Split(';');
Address = splits[0];
if (splits.Length < 2)
return;
if (UInt16.TryParse(splits[1], out var port))
Port = port;
if (splits.Length < 3)
return;
Nickname = splits[2];
if (splits.Length < 4)
return;
if (Int32.TryParse(splits[3], out var intSsl))
UseSsl = Convert.ToBoolean(intSsl);
if (splits.Length < 5)
return;
if (Enum.TryParse(splits[4], out var passwordType))
switch (passwordType)
{
case IrcPasswordType.NickServ:
case IrcPasswordType.Sasl:
case IrcPasswordType.Server:
case IrcPasswordType.Oper:
PasswordType = passwordType;
break;
default:
break;
}
if (splits.Length < 6)
return;
var rest = new List(splits);
rest.RemoveRange(0, 5);
Password = String.Join(";", rest);
}
///
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(Address);
sb.Append(';');
sb.Append(Port);
sb.Append(';');
sb.Append(Nickname);
sb.Append(';');
if (UseSsl.HasValue)
sb.Append(Convert.ToInt32(UseSsl.Value));
if (PasswordType.HasValue)
{
sb.Append(';');
sb.Append((int)PasswordType);
sb.Append(';');
sb.Append(Password);
}
return sb.ToString();
}
}
}