-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
IrcConnectionStringBuilder.cs
131 lines (107 loc) · 3.02 KB
/
IrcConnectionStringBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.Text;
using Tgstation.Server.Api.Models.Internal;
namespace Tgstation.Server.Api.Models
{
/// <summary>
/// <see cref="ChatConnectionStringBuilder"/> for <see cref="ChatProvider.Irc"/>.
/// </summary>
public sealed class IrcConnectionStringBuilder : ChatConnectionStringBuilder
{
/// <inheritdoc />
public override bool Valid => Address != null && Port.HasValue && Port != 0 && UseSsl.HasValue && (PasswordType.HasValue ^ Password == null);
/// <summary>
/// The IP address or URL of the IRC server.
/// </summary>
public string? Address { get; set; }
/// <summary>
/// The port the server runs on.
/// </summary>
public ushort? Port { get; set; }
/// <summary>
/// The nickname for the bot to use.
/// </summary>
public string? Nickname { get; set; }
/// <summary>
/// If the connection should be made using SSL.
/// </summary>
public bool? UseSsl { get; set; }
/// <summary>
/// The optional <see cref="IrcPasswordType"/> to use.
/// </summary>
public IrcPasswordType? PasswordType { get; set; }
/// <summary>
/// The optional password to use.
/// </summary>
public string? Password { get; set; }
/// <summary>
/// Initializes a new instance of the <see cref="IrcConnectionStringBuilder"/> class.
/// </summary>
public IrcConnectionStringBuilder()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="IrcConnectionStringBuilder"/> class.
/// </summary>
/// <param name="connectionString">The connection string.</param>
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<IrcPasswordType>(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<string>(splits);
rest.RemoveRange(0, 5);
Password = String.Join(";", rest);
}
/// <inheritdoc />
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();
}
}
}