This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSender.cs
executable file
·100 lines (88 loc) · 3.05 KB
/
Sender.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
using System;
using System.Text.RegularExpressions;
namespace IRC_Library
{
public sealed class Sender
{
internal static Sender FromIRC(string fromIRC)
{
if (string.IsNullOrWhiteSpace(fromIRC))
throw new ArgumentNullException(nameof(fromIRC));
int at = fromIRC.IndexOf('@');
if (at == -1)
{
if (fromIRC.IndexOf('!') == -1)
{
return new Sender(null, null, fromIRC);
}
else
{
string[] names = fromIRC.Split('!');
if (names.Length > 2 || names.Length == 0)
throw new ArgumentRegexException(nameof(fromIRC), "^(A-Za-z0-9)*((!)(A-Za-z0-9)*)?((@)(A-Za-z0-9)*)?$");
if (names.Length == 1)
return new Sender(names[0], null, null);
else
return new Sender(names[0], names[1], null);
}
}
else
{
string host = fromIRC.Substring(at + 1);
string front = fromIRC.Substring(0, at);
string[] names = front.Split('!');
if (names.Length > 2 || names.Length == 0)
throw new ArgumentRegexException(nameof(fromIRC), "^(A-Za-z0-9)*((!)(A-Za-z0-9)*)?((@)(A-Za-z0-9)*)?$");
if (names.Length == 1)
return new Sender(names[0], null, host);
else
return new Sender(names[0], names[1], host);
}
}
public Sender(string nick, string user, string host)
{
if (nick != null && !Regex.IsMatch(nick, "[A-Za-z0-9]*"))
throw new ArgumentRegexException(nameof(nick), "[A-Za-z0-9]*");
this.Nick = nick;
if (user != null && !Regex.IsMatch(user, "[A-Za-z0-9]*"))
throw new ArgumentRegexException(nameof(user), "[A-Za-z0-9]*");
this.User = user;
if (host != null && !Regex.IsMatch(host, "[A-Za-z0-9]*"))
throw new ArgumentRegexException(nameof(host), "[A-Za-z0-9]*");
this.Host = host;
}
public string Nick
{
get;
private set;
}
public string User
{
get;
private set;
}
public string Host
{
get;
private set;
}
public override string ToString()
{
return $"{Nick}!{User}@{Host}";
}
public override int GetHashCode()
{
return unchecked(Nick.GetHashCode() + User.GetHashCode() + Host.GetHashCode());
}
public override bool Equals(object obj)
{
if (obj is Sender)
{
var sender = (Sender)obj;
return sender.Nick == this.Nick && sender.User == this.User && sender.Host == this.Host;
}
else
return false;
}
}
}