-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwitchClient.cs
357 lines (309 loc) · 12.9 KB
/
TwitchClient.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
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
350
351
352
353
354
355
356
357
//#define READONLY
using Irc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Twitch.Models;
using System.Text.RegularExpressions;
using System.Threading;
using System.IO;
using System.Diagnostics;
namespace Twitch
{
public class TwitchClient
{
private ManualResetEvent m_KeepAliveToken = new ManualResetEvent(false);
public static readonly string Version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.Build.ToString();
private IrcClient m_Client;
private TwitchChatManager m_TwitchChatManager;
private Thread m_keepAliveThread;
private string m_Name;
private bool hasBeenDisconnected = false;
public string Name { get { return m_Name; } }
private static readonly char[] USERHOST_SIGNS = { '!', '@' };
public delegate void TwitchClientOnPartEventHandler(TwitchClient sender, TwitchClientOnPartEventArgs args);
public event TwitchClientOnPartEventHandler OnPart;
public delegate void TwitchClientOnJoinEventHandler(TwitchClient sender, TwitchClientOnJoinEventArgs args);
public event TwitchClientOnJoinEventHandler OnJoin;
public delegate void TwitchClientOnMessageEventHandler(TwitchClient sender, TwitchMessage args);
public event TwitchClientOnMessageEventHandler OnMessage;
public delegate void TwitchClientOnNoticeEventHandler(TwitchClient sender, TwitchNotice args);
public event TwitchClientOnNoticeEventHandler OnNotice;
public delegate void TwitchClientOnRoomStateChangedEventHandler(TwitchClient sender, TwitchRoomState args);
public event TwitchClientOnRoomStateChangedEventHandler OnRoomStateChanged;
public delegate void TwitchClientOnUserNoticeEventHandler(TwitchClient sender, TwitchUserNotice args);
public event TwitchClientOnUserNoticeEventHandler OnUserNotice;
public delegate void TwitchClientOnLogEventHandler(TwitchClient sender, IrcClientOnLogEventArgs args);
public event TwitchClientOnLogEventHandler OnLog;
public delegate void TwitchClientOnPerformEventHandler(TwitchClient sender);
public event TwitchClientOnPerformEventHandler OnPerform;
public delegate void TwitchClientOnDisconnectEventHandler(TwitchClient sender, bool wasManualDisconnect);
public event TwitchClientOnDisconnectEventHandler OnDisconnect;
/// <summary>
/// If sending to a user as a message, it will automatically be converted to whisper
/// </summary>
public bool AutoDetectSendWhispers = false;
public bool KeepAlive = true;
public string KeepAliveChannel = "";
public Twitch.Models.MessageLevel LogLevel
{
get { return (Twitch.Models.MessageLevel)m_Client.LogLevel; }
set { m_Client.LogLevel = (Irc.MessageLevel)value; }
}
public bool LogToConsole { get { return m_Client.LogToConsole; } set { m_Client.LogToConsole = value; } }
public bool LogEnabled { get { return m_Client.LogEnabled; } set { m_Client.LogEnabled = value; } }
public TwitchClient(string nickname, string password, string[] founders = null, string[] developers = null)
{
m_TwitchChatManager = new TwitchChatManager(founders, developers);
m_Client = new IrcClient("irc.chat.twitch.tv", 80, nickname);
m_Name = nickname;
m_Client.Password = password;
m_Client.OnChannelNickListRecived += m_client_OnChannelNickListRecived;
m_Client.OnJoin += m_client_OnJoin;
if (KeepAlive)
m_Client.OnJoin += m_client_OnJoinKeepAlive;
m_Client.OnLog += m_client_OnLog;
m_Client.OnMode += m_client_OnMode;
m_Client.OnNotice += m_client_OnNotice;
m_Client.OnPart += m_client_OnPart;
m_Client.OnPerform += m_client_OnPerform;
m_Client.OnPrivateMessage += m_client_OnPrivateMessage;
m_Client.OnQuit += m_client_OnQuit;
m_Client.OnUnknownCommand += m_client_OnUnknownCommand;
m_Client.OnDisconnect += m_client_OnDisconnect;
m_Client.LogLevel = Irc.MessageLevel.Info;
m_Client.ServerEncoding = Encoding.UTF8;
}
void m_client_OnJoinKeepAlive(IrcClient sender, IrcClientOnJoinEventArgs args)
{
//Console.WriteLine("Checking keep alive");
string channel = "#" + m_Name.ToLowerInvariant();
if (!string.IsNullOrEmpty(KeepAliveChannel))
channel = KeepAliveChannel;
if (args.IsMyself && args.Channel == channel)
{
//Console.WriteLine("Starting keep alive");
m_Client.OnJoin -= m_client_OnJoinKeepAlive;
m_keepAliveThread = new Thread(new ThreadStart(KeepAliveLoop));
m_keepAliveThread.Name = "Keep alive thread";
m_keepAliveThread.Start();
}
}
private void m_client_OnDisconnect(IrcClient sender, bool wasManualDisconnect)
{
KeepAlive = false;
m_KeepAliveToken.Set();
if (OnDisconnect != null)
OnDisconnect(this, wasManualDisconnect);
}
public void Connect()
{
if (hasBeenDisconnected)
throw new ObjectDisposedException("Cannot reconnect after a disconnection. Create a new instance of the class");
m_Client.Connect();
}
public void SendAction(string channel, string action)
{
#if !READONLY
if (m_Client.Status == IrcClient.State.Connected)
m_Client.PrivMsg(channel, "\x0001ACTION {0}\x01", action);
//m_client.PrivMsg(channel, "\x01 ACTION {0}\x01", action);
#endif
}
public void SendAction(string destination, string format, params object[] arg)
{
#if !READONLY
this.SendAction(destination, string.Format(Thread.CurrentThread.CurrentCulture, format, arg));
#endif
}
public void SendTimeout(string destination, string target, int time, string reason)
{
#if !READONLY
m_Client.PrivMsg(destination, "/timeout {0} {1} {2}", target, time, reason);
#endif
}
public void SendMessage(string channel, string message)
{
#if !READONLY
if (m_Client.Status == IrcClient.State.Connected)
if (AutoDetectSendWhispers && !channel.StartsWith("#"))
throw new NotSupportedException("Can no longer send whispers through this method");
else
m_Client.PrivMsg(channel, message);
#endif
}
public void SendMessage(string destination, string format, params object[] arg)
{
#if !READONLY
this.SendMessage(destination, string.Format(Thread.CurrentThread.CurrentCulture, format, arg));
#endif
}
/// <summary>
/// Disconnects the client and close all connection. Cannot be reused afterwards
/// </summary>
public void Disconnect()
{
hasBeenDisconnected = true;
if (m_Client != null && m_Client.Status == IrcClient.State.Connected)
m_Client.Disconnect();
KeepAlive = false;
m_KeepAliveToken.Set();
if (OnDisconnect != null)
OnDisconnect(this, true);
}
public void Join(string channel)
{
if (m_Client.Status == IrcClient.State.Connected)
m_Client.Join(channel);
}
public void Part(string channel)
{
if (m_Client.Status == IrcClient.State.Connected)
m_Client.Part(channel);
}
private void KeepAliveLoop()
{
try
{
Console.WriteLine("[{0}] Started keep alive", DateTime.Now.ToString());
Random r = new Random();
string channel = "#" + m_Name.ToLowerInvariant();
if (!string.IsNullOrEmpty(KeepAliveChannel))
channel = KeepAliveChannel;
while (KeepAlive)
{
SendMessage(channel, "Keep alive : " + DateTime.UtcNow.ToString());
/*#if DEBUG
File.AppendAllText("CitibotKeepAlive_" + Environment.MachineName + "-" + Process.GetCurrentProcess().Id.ToString() + ".log",
"Keep alive : " + DateTime.UtcNow.ToString() + "\r\n");
#endif*/
int wait = m_KeepAliveInterval * 1000;
m_LastKeepAlive = DateTime.Now;
m_KeepAliveToken.Reset();
m_KeepAliveToken.WaitOne(wait);
}
}
catch (Exception ex)
{
Console.WriteLine("[{0}] Keep alive exception", DateTime.Now.ToString());
Console.WriteLine(ex);
}
}
private DateTime m_LastKeepAlive = DateTime.Now;
private int m_KeepAliveInterval = 1 * 3600;
public DateTime LastKeepAlive
{
get { return m_LastKeepAlive; }
}
public int KeepAliveInterval
{
get
{
return m_KeepAliveInterval;
}
set
{
m_KeepAliveInterval = value;
TriggerNextKeepAlive();
}
}
public DateTime NextKeepAlive
{
get
{
return m_LastKeepAlive.AddSeconds(m_KeepAliveInterval);
}
}
public void TriggerNextKeepAlive()
{
m_KeepAliveToken.Set();
}
void m_client_OnUnknownCommand(IrcClient sender, IrcMessage message)
{
switch (message.Command)
{
case "ROOMSTATE":
var state = m_TwitchChatManager.ParseTwitchRoomStateFromIrc(message);
if (OnRoomStateChanged != null)
OnRoomStateChanged(this, state);
break;
case "USERNOTICE":
break;
default:
/*Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(message);
Console.ForegroundColor = ConsoleColor.Gray;*/
break;
}
}
void m_client_OnQuit(IrcClient sender, IrcClientOnQuitEventArgs args)
{
throw new NotImplementedException();
}
void m_client_OnPrivateMessage(IrcClient sender, IrcClientOnPrivateMessageEventArgs args)
{
var message = m_TwitchChatManager.ParseTwitchMessageFromIrc(args);
if (OnMessage != null)
OnMessage(this, message);
}
void m_client_OnPerform(IrcClient sender)
{
if (OnJoin != null || OnPart != null)
{
sender.CapabilityRequest("twitch.tv/membership");
}
sender.CapabilityRequest("twitch.tv/commands");
sender.CapabilityRequest("twitch.tv/tags");
if (OnPerform != null)
OnPerform(this);
}
void m_client_OnPart(IrcClient sender, IrcClientOnPartEventArgs args)
{
if (OnPart != null)
OnPart(this, new TwitchClientOnPartEventArgs(args.Name, args.Channel, m_Name.Equals(args.Name)));
}
void m_client_OnNotice(IrcClient sender, IrcMessage args)
{
var notice = m_TwitchChatManager.ParseTwitchNoticeFromIrc(args);
if (OnNotice != null)
OnNotice(this, notice);
}
void m_client_OnMode(IrcClient sender, IrcClientOnModeEventArgs args)
{
m_TwitchChatManager.OnModeChange(args);
}
void m_client_OnLog(IrcClient sender, IrcClientOnLogEventArgs args)
{
if (OnLog != null)
OnLog(this, args);
}
void m_client_OnJoin(IrcClient sender, IrcClientOnJoinEventArgs args)
{
if (OnJoin != null)
OnJoin(this, new TwitchClientOnJoinEventArgs(args.Name, args.Channel, args.Name.ToLowerInvariant().Equals(m_Name.ToLowerInvariant())));
}
void m_client_OnDebug(int debug)
{
Console.WriteLine(debug);
}
void m_client_OnChannelNickListRecived(IrcClient sender, IrcClientOnChannelNickListReceivedEventArgs args)
{
//if (OnJoin != null)
//{
// foreach(string user in args.NameList)
// OnJoin(this, new TwitchClientOnJoinEventArgs(user, args.Channel, user.ToLowerInvariant().Equals(m_name.ToLowerInvariant())));
//}
}
public string GetIPConnected()
{
return m_Client.GetIPConnected();
}
public enum RateLimitMode
{
None,
}
}
}