about summary refs log tree commit diff
path: root/IrcStates/Server.cs
blob: a5528903a89aa92b8c95fe940b90d28517561134 (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using IrcTokens;

namespace IrcStates
{
    public class Server
    {
        public const string WhoType = "525"; // randomly generated
        private readonly StatefulDecoder _decoder;

        private Dictionary<string, string> TempCaps;

        public Server(string name)
        {
            Name          = name;
            Registered    = false;
            Modes         = new List<string>();
            Motd          = new List<string>();
            _decoder      = new StatefulDecoder();
            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 string CaseFold(string str)
        {
            return Casemap.CaseFold(ISupport.CaseMapping, str);
        }

        public bool CaseFoldEquals(string s1, string s2)
        {
            return CaseFold(s1) == CaseFold(s2);
        }

        public bool IsMe(string nickname)
        {
            return CaseFold(nickname) == NickNameLower;
        }

        public bool HasUser(string nickname)
        {
            return Users.ContainsKey(CaseFold(nickname));
        }

        private void AddUser(string nickname, string nicknameLower)
        {
            var user = CreateUser(nickname, nicknameLower);
            Users[nicknameLower] = user;
        }

        private User CreateUser(string nickname, string nicknameLower)
        {
            var user = new User();
            user.SetNickName(nickname, nicknameLower);
            return user;
        }

        public bool IsChannel(string target)
        {
            return !string.IsNullOrEmpty(target) &&
                   ISupport.ChanTypes.Contains(target[0].ToString(CultureInfo.InvariantCulture));
        }

        public bool HasChannel(string name)
        {
            return Channels.ContainsKey(CaseFold(name));
        }

        public Channel GetChannel(string name)
        {
            return HasChannel(name) ? Channels[name] : null;
        }

        public List<(Line, Emit)> Recv(byte[] data)
        {
            if (data == null) return null;

            var lines = _decoder.Push(data, data.Length);
            if (lines == null) throw new ServerDisconnectedException();

            return lines.Select(l => (l, Parse(l))).ToList();
        }

        public Emit Parse(Line line)
        {
            if (line == null) return null;

            switch (line.Command)
            {
                case Numeric.RPL_WELCOME:  return HandleWelcome(line);
                case Numeric.RPL_ISUPPORT: return HandleISupport(line);
                case Numeric.RPL_MOTDSTART:
                case Numeric.RPL_MOTD:
                    return HandleMotd(line);
                case Commands.Nick:             return HandleNick(line);
                case Commands.Join:             return HandleJoin(line);
                case Commands.Part:             return HandlePart(line);
                case Commands.Kick:             return HandleKick(line);
                case Commands.Quit:             return HandleQuit(line);
                case Commands.Error:            return HandleError(line);
                case Numeric.RPL_NAMREPLY:      return HandleNames(line);
                case Numeric.RPL_CREATIONTIME:  return HandleCreationTime(line);
                case Commands.Topic:            return HandleTopic(line);
                case Numeric.RPL_TOPIC:         return HandleTopicNumeric(line);
                case Numeric.RPL_TOPICWHOTIME:  return HandleTopicTime(line);
                case Commands.Mode:             return HandleMode(line);
                case Numeric.RPL_CHANNELMODEIS: return HandleChannelModeIs(line);
                case Numeric.RPL_UMODEIS:       return HandleUModeIs(line);
                case Commands.Privmsg:
                case Commands.Notice:
                case Commands.Tagmsg:
                    return HandleMessage(line);
                case Numeric.RPL_VISIBLEHOST: return HandleVisibleHost(line);
                case Numeric.RPL_WHOREPLY:    return HandleWhoReply(line);
                case Numeric.RPL_WHOSPCRPL:   return HandleWhox(line);
                case Numeric.RPL_WHOISUSER:   return HandleWhoIsUser(line);
                case Commands.Chghost:        return HandleChghost(line);
                case Commands.Setname:        return HandleSetname(line);
                case Commands.Away:           return HandleAway(line);
                case Commands.Account:        return HandleAccount(line);
                case Commands.Cap:            return HandleCap(line);
                case Numeric.RPL_LOGGEDIN:    return HandleLoggedIn(line);
                case Numeric.RPL_LOGGEDOUT:   return HandleLoggedOut(line);
            }

            return new Emit();
        }

        private Emit HandleSetname(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleAway(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleAccount(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleCap(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleLoggedIn(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleChghost(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleWhoIsUser(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleWhox(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleWhoReply(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleVisibleHost(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleMessage(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleUModeIs(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleChannelModeIs(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleMode(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleTopicTime(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleTopicNumeric(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleTopic(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleCreationTime(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleNames(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleError(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleQuit(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleLoggedOut(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleKick(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandlePart(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleJoin(Line line)
        {
            throw new NotImplementedException();
        }

        private Emit HandleNick(Line line)
        {
            var nick = line.Params[0];
            var nickLower = CaseFold(line.Hostmask.NickName);

            var emit = new Emit();

            if (Users.ContainsKey(nickLower))
            {
                var user = Users[nick];
                emit.User = user;

                var oldNickLower = user.NickNameLower;
                var newNickLower = CaseFold(nick);
                user.SetNickName(nick, newNickLower);
                Users[newNickLower] = user;
                foreach (var channelLower in user.Channels)
                {
                    var channel = Channels[channelLower];
                    var channelUser = channel.Users[oldNickLower];
                    channel.Users.Remove(oldNickLower);
                    channel.Users[newNickLower] = channelUser;
                }
            }

            if (nickLower == NickNameLower)
            {
                emit.Self = true;
                NickName = nick;
                NickNameLower = CaseFold(nick);
            }

            return emit;
        }

        private Emit HandleMotd(Line line)
        {
            if (line.Command == Numeric.RPL_MOTDSTART)
            {
                Motd.Clear();
            }

            var emit = new Emit {Text = line.Params[1]};
            Motd.Add(line.Params[1]);
            return emit;
        }

        private Emit HandleISupport(Line line)
        {
            ISupport = new ISupport();
            ISupport.Parse(line.Params.Skip(1).SkipLast(1));
            return new Emit();
        }


        private Emit HandleWelcome(Line line)
        {
            NickName      = line.Params[0];
            NickNameLower = CaseFold(line.Params[0]);
            Registered    = true;
            return new Emit();
        }
    }
}