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