IRC parsing, tokenization, and state handling in C#
1// ReSharper disable AutoPropertyCanBeMadeGetOnly.Local
2
3namespace IRCStates;
4
5public class Channel
6{
7 public string Name { get; private set; }
8 public string NameLower { get; private set; }
9 public Dictionary<string, ChannelUser> Users { get; private set; } = new();
10 public string Topic { get; set; }
11 public string TopicSetter { get; set; }
12 public DateTime TopicTime { get; set; }
13 public DateTime Created { get; set; }
14 public Dictionary<string, List<string>> ListModes { get; private set; } = [];
15 public Dictionary<string, string> Modes { get; private set; } = [];
16
17 public override string ToString() => $"Channel(name={Name})";
18
19 public void SetName(string name, string nameLower)
20 {
21 Name = name;
22 NameLower = nameLower;
23 }
24
25 public void AddMode(string ch, string param, bool listMode)
26 {
27 if (listMode)
28 {
29 if (!ListModes.ContainsKey(ch))
30 {
31 ListModes[ch] = [];
32 }
33
34 if (!ListModes[ch].Contains(param))
35 {
36 ListModes[ch].Add(param ?? string.Empty);
37 }
38 }
39 else
40 {
41 Modes[ch] = param;
42 }
43 }
44
45 public void RemoveMode(string ch, string param)
46 {
47 if (ListModes.ContainsKey(ch))
48 {
49 if (!ListModes[ch].Contains(param))
50 {
51 return;
52 }
53
54 ListModes[ch].Remove(param);
55
56 if (!ListModes[ch].Any())
57 {
58 ListModes.Remove(ch);
59 }
60 }
61 else
62 {
63 Modes.Remove(ch);
64 }
65 }
66}