-
Notifications
You must be signed in to change notification settings - Fork 1
/
Twitchie.cs
267 lines (214 loc) · 5.95 KB
/
Twitchie.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;
using Twitchie2.Enums;
using Twitchie2.Events;
using Twitchie2.Messages;
namespace Twitchie2
{
public class Twitchie : IDisposable
{
private CancellationTokenSource cts;
private TcpClient tcpClient;
private TextReader reader;
private TextWriter writer;
internal static Twitchie Instance { get; private set; }
public List<TwitchIrcChannel> Channels { get; private set; }
public ChatAccount Account { get; private set; }
#region Events
public event EventHandler<RawMessageEventArgs> OnRawMessage;
public event EventHandler<MessageEventArgs> OnMessage;
public event EventHandler<RoomStateEventArgs> OnRoomState;
public event EventHandler<ModeEventArgs> OnMode;
public event EventHandler<JoinEventArgs> OnJoin;
public event EventHandler<PartEventArgs> OnPart;
public event EventHandler<NoticeEventArgs> OnNotice;
public event EventHandler<HostTargetEventArgs> OnHostTarget;
public event EventHandler<ClearChatEventArgs> OnClearChat;
public event EventHandler<ClearMessageEventArgs> OnClearMessage;
public event EventHandler<UserNoticeEventArgs> OnUserNotice;
public event EventHandler<MessageEventArgs> OnMention;
public event EventHandler<UserStateEventArgs> OnUserState;
public event EventHandler<ConnectionErrorEventArgs> OnConnectionError;
#endregion
public Twitchie() => Channels = new();
public async Task ConnectAsync()
{
cts = new();
await CreateConnectionAsync();
Instance = this;
}
private async Task CreateConnectionAsync()
{
try
{
tcpClient = new();
await tcpClient.ConnectAsync("irc.chat.twitch.tv", 6667);
if (tcpClient.Connected)
{
reader = new StreamReader(tcpClient.GetStream());
writer = new StreamWriter(tcpClient.GetStream());
}
}
catch (SocketException ex)
{
OnConnectionError?.Invoke(this, new(ex));
}
}
public async Task LoginAsync(ChatAccount account)
{
if (Account != null && Account.LoggedIn)
throw new Exception($"Twitchie is already logged in.");
Account ??= account ?? throw new ArgumentNullException(nameof(account));
await SendAsync($"PASS {Account.OauthToken}");
await SendAsync($"NICK {Account.Nickname}");
await CapAsync("membership");
await CapAsync("commands");
await CapAsync("tags");
Account.LoggedIn = true;
OnMessage += (sender, args) =>
{
if (args.Message.ToLower().Contains($"@{Account.Nickname}"))
{
OnMention?.Invoke(this, new(new(args.RawMessage)));
}
};
}
public async Task ListenAsync()
{
while (!cts.IsCancellationRequested)
{
string buffer;
try
{
buffer = await reader.ReadLineAsync();
}
catch
{
continue;
}
if (buffer == null)
{
return;
}
OnRawMessage?.Invoke(this, new(buffer));
var eventType = EventParser.ParseEventType(buffer);
await HandleIrcEventAsync(eventType, new(buffer));
}
}
private async Task HandleIrcEventAsync(EventType eventType, TwitchIrcMessage msg)
{
switch (eventType)
{
case EventType.WelcomeMessage:
foreach (var channel in Channels)
await channel.JoinAsync();
break;
case EventType.ClearChat:
OnClearChat?.Invoke(this, new(msg));
break;
case EventType.ClearMessage:
OnClearMessage?.Invoke(this, new(msg));
break;
case EventType.HostTarget:
OnHostTarget?.Invoke(this, new(msg));
break;
case EventType.Join:
OnJoin?.Invoke(this, new(msg));
break;
case EventType.Message:
OnMessage?.Invoke(this, new(msg));
break;
case EventType.Mode:
OnMode?.Invoke(this, new(msg));
break;
case EventType.Notice:
OnNotice?.Invoke(this, new(msg));
break;
case EventType.Part:
OnPart?.Invoke(this, new(msg));
break;
case EventType.RoomState:
OnRoomState?.Invoke(this, new(msg));
break;
case EventType.UserNotice:
OnUserNotice?.Invoke(this, new(msg));
break;
case EventType.UserState:
OnUserState?.Invoke(this, new(msg));
break;
case EventType.Ping:
await SendAsync("PONG :tmi.twitch.tv");
break;
}
}
public async Task PartAllAsync()
{
foreach (var channel in Channels)
{
await channel.PartAsync();
}
}
public async Task JoinChannelAsync(TwitchIrcChannel channel)
{
if (!Channels.Contains(channel))
{
await channel.JoinAsync();
Channels.Add(channel);
}
}
public async Task<bool> PartChannelAsync(TwitchIrcChannel channel)
{
await channel.PartAsync();
return Channels.Remove(channel);
}
public async Task ReconnectAsync()
{
await DisconnectAsync();
await CreateConnectionAsync();
await LoginAsync(Account);
}
public async Task DisconnectAsync()
{
await PartAllAsync();
reader.Close();
writer.Close();
tcpClient.Close();
Account.LoggedIn = false;
}
internal async Task SendAsync(string message)
{
await writer.WriteAsync($"{message}\r\n");
await writer.FlushAsync();
}
internal async Task CapAsync(string capabilities)
=> await SendAsync($"CAP REQ :twitch.tv/{capabilities}");
public async Task ChatAsync(string channel, string message)
=> await SendAsync($"PRIVMSG {channel} :{message}");
public async Task ActionAsync(string channel, string message)
=> await ChatAsync(channel, $"/me {message}");
public async Task MentionAsync(string channel, string user, string message)
=> await ChatAsync(channel, $"@{user}, {message}");
public async Task WhisperAsync(string channel, string receiver, string message)
=> await ChatAsync(channel, $"/w {receiver} {message}");
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
cts?.Dispose();
reader?.Dispose();
writer?.Dispose();
tcpClient?.Dispose();
Channels.Clear();
}
}
}
}