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