about summary refs log tree commit diff
path: root/IrcStates/Server.cs
blob: 9371a2b8e7177f99068d833d6265e7c4b66ad796 (plain) (blame)
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
using System;
using System.Collections.Generic;
using System.Linq;
using IrcTokens;

namespace IrcStates
{
    public class Server
    {
        private readonly StatefulDecoder _decoder;

        public Dictionary<string, List<Func<string, Line, Emit>>> LineHandlers;

        private Dictionary<string, string> TempCaps;

        public Server(string name)
        {
            Name          = name;
            Registered    = false;
            Modes         = new List<string>();
            Motd          = new List<string>();
            _decoder      = new StatefulDecoder();
            LineHandlers  = new Dictionary<string, List<Func<string, Line, Emit>>>();
            Users         = new Dictionary<string, User>();
            Channels      = new Dictionary<string, Channel>();
            ISupport      = new ISupport();
            HasCap        = false;
            TempCaps      = new Dictionary<string, string>();
            AvailableCaps = new Dictionary<string, string>();
            AgreedCaps    = new List<string>();
        }

        public string Name { get; set; }
        public string NickName { get; set; }
        public string NickNameLower { get; set; }
        public string UserName { get; set; }
        public string HostName { get; set; }
        public string RealName { get; set; }
        public string Account { get; set; }
        public string Away { get; set; }

        public bool Registered { get; set; }
        public List<string> Modes { get; set; }
        public List<string> Motd { get; set; }
        public Dictionary<string, User> Users { get; set; }
        public Dictionary<string, Channel> Channels { get; set; }
        public Dictionary<string, string> AvailableCaps { get; set; }
        public List<string> AgreedCaps { get; set; }

        public ISupport ISupport { get; set; }
        public bool HasCap { get; set; }

        public override string ToString()
        {
            return $"Server(name={Name})";
        }

        public List<(Line, Emit)> Recv(byte[] data)
        {
            var lines = _decoder.Push(data, data.Length);
            if (lines == null) throw new ServerDisconnectedException();

            var emits = lines.Select(ParseTokens).ToList();
            return null;
        }

        public Emit ParseTokens(Line line)
        {
            if (line != null && !LineHandlers.ContainsKey(line.Command)) return null;
            var ret = new Emit();

            var handlers = LineHandlers[line.Command]
                .Select(callback => callback(line.Command, line))
                .Where(emit => emit != null);

            foreach (var emit in handlers)
            {
                emit.Command = line.Command;
                ret          = emit;
            }

            return ret;
        }
    }
}