-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDirectOSUIRCBot.cs
More file actions
242 lines (202 loc) · 6.95 KB
/
DirectOSUIRCBot.cs
File metadata and controls
242 lines (202 loc) · 6.95 KB
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
using Sync.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sync.MessageFilter;
using System.Net.Sockets;
using System.IO;
using Sync.Tools;
using System.Threading;
using Sync.Source;
using System.Text.RegularExpressions;
namespace DefaultPlugin.Clients
{
public class DirectOSUIRCBot : DefaultClient, IConfigurable
{
public const string CONST_ACTION_FLAG = "\x0001ACTION ";
TcpClient tcpClient;
NetworkStream ns;
StreamReader sr;
StreamWriter sw;
Thread recive, send;
bool workStatus = false;
Queue<string> messageQueue;
public static ConfigurationElement IRCBotName { get; set; } = "";
public static ConfigurationElement IRCBotPasswd { get; set; } = "";
public static ConfigurationElement IRCNick { get; set; } = "";
static readonly Regex msgRegex = new Regex(":(.+?)!.+?:(.*?)$");
public DirectOSUIRCBot() : base("Deliay", "DirectOsuIRCBot")
{
messageQueue = new Queue<string>();
}
public override void Restart()
{
StopWork();
StartWork();
}
public override void SendMessage(IMessageBase message)
{
SendRawMessage($"PRIVMSG {message.User} :{message.Message}");
}
private void SendRawMessage(string msg)
{
messageQueue.Enqueue(msg);
}
private void ReciveRawMessage(string msg)
{
if (!msg.Contains(@"PRIVMSG "))
{
//处理非对话消息
if (msg.StartsWith("PING "))
SendRawMessage(msg.Replace(@"PING", @"PONG"));
}
else
{
Match match = msgRegex.Match(msg);
string nick = match.Groups[1].Value;
string rawmsg = match.Groups[2].Value;
DefaultPlugin.MainMessager.RaiseMessage<ISourceClient>(new IRCMessage(nick, rawmsg));
}
}
private void ReciveLoop()
{
try
{
while (workStatus)
{
Thread.Sleep(1);
if ((tcpClient.Client.Poll(20, SelectMode.SelectRead)) && (tcpClient.Client.Available == 0))
{
IO.CurrentIO.WriteColor("[Osu!IRC]"+Language.LANG_OSUIRC_NETWORK_INTERRUPTED, ConsoleColor.Red);
ConnectAndLogin();
continue;
}
if (ns.DataAvailable)
{
string message = sr.ReadLine();
ReciveRawMessage(message);
}
}
}
catch(Exception e)
{
IO.CurrentIO.WriteColor("[Osu!IRC]Reciver occured error:"+e.Message,ConsoleColor.Red);
CurrentStatus = SourceStatus.REMOTE_DISCONNECTED;
}
IO.CurrentIO.WriteColor("[Osu!IRC]Reciver thread finish", ConsoleColor.Yellow);
}
private void SendLoop()
{
try
{
while (workStatus)
{
Thread.Sleep(1);
if (messageQueue.Count > 0)
{
if (!tcpClient.Connected)
{
//等Reciver线程处理即可
continue;
}
string message = string.Empty;
lock (messageQueue)
{
message = messageQueue.Dequeue();
}
if (message == string.Empty) continue;
if (!tcpClient.Connected)
{
workStatus = false;
CurrentStatus = SourceStatus.REMOTE_DISCONNECTED;
continue;
}
sw.WriteLine(message);
sw.Flush();
message = string.Empty;
}
}
}
catch(Exception e)
{
IO.CurrentIO.WriteColor("[Osu!IRC]Sender occured error:" + e.Message, ConsoleColor.Red);
CurrentStatus = SourceStatus.REMOTE_DISCONNECTED;
}
IO.CurrentIO.WriteColor("[Osu!IRC]Sender thread finish", ConsoleColor.Yellow);
}
public override void StartWork()
{
if (workStatus) return;
EventBus.RaiseEvent(new ClientStartWorkEvent());
ConnectAndLogin();
workStatus = true;
recive = new Thread(ReciveLoop);
send = new Thread(SendLoop);
recive.Start();
send.Start();
SendMessage(new IRCMessage(IRCNick.ToString(), "[DirectOSUIRCBot]Welcome!"));
}
private void ConnectAndLogin()
{
tcpClient = new TcpClient();
try
{
tcpClient.Connect("irc.ppy.sh", 6667);
if (!tcpClient.Connected)
{
throw new Exception("Network error!");
}
CurrentStatus = SourceStatus.CONNECTED_WAITING;
ns = tcpClient.GetStream();
sr = new StreamReader(ns);
sw = new StreamWriter(ns);
IRCLogin();
IO.CurrentIO.WriteColor("[Osu!IRC]"+Language.LANG_OSUIRC_LOGIN_SUCCESS, ConsoleColor.Green);
}
catch(Exception e)
{
IO.CurrentIO.WriteColor("[Osu!IRC]"+string.Format(Language.LANG_OSUIRC_LOGIN_FAILED,e.Message), ConsoleColor.Yellow);
}
}
private void IRCLogin()
{
sw.WriteLine($"PASS {IRCBotPasswd}");
sw.WriteLine($"USER {IRCBotName} 1 * : {IRCBotName}");
sw.WriteLine($"NICK {IRCBotName}");
sw.Flush();
this.NickName = IRCNick;
CurrentStatus = SourceStatus.CONNECTED_WORKING;
}
public override void StopWork()
{
if(tcpClient != null && tcpClient.Connected)
{
sw.Write("QUIT");
sw.Flush();
}
workStatus = false;
EventBus.RaiseEvent(new ClientStopWorkEvent());
}
public override void SwitchOtherClient()
{
StopWork();
}
public override void SwitchThisClient()
{
CurrentStatus = SourceStatus.IDLE;
}
public void onConfigurationLoad()
{
this.NickName = IRCNick;
}
public void onConfigurationSave()
{
}
public void onConfigurationReload()
{
this.NickName = IRCNick;
}
}
}