-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAutoRespond.cs
More file actions
238 lines (221 loc) · 10.4 KB
/
AutoRespond.cs
File metadata and controls
238 lines (221 loc) · 10.4 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
using System;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace MinecraftClient.ChatBots
{
/// <summary>
/// This bot automatically runs actions when a user sends a message matching a specified rule
/// </summary>
class AutoRespond : ChatBot
{
private string matchesFile;
private List<RespondRule> respondRules;
private enum MessageType { Public, Private, Other };
/// <summary>
/// Create a new AutoRespond bot
/// </summary>
/// <param name="matchesFile">INI File to load matches from</param>
public AutoRespond(string matchesFile)
{
this.matchesFile = matchesFile;
}
/// <summary>
/// Describe a respond rule based on a simple match or a regex
/// </summary>
private class RespondRule
{
private Regex regex;
private string match;
private string actionPublic;
private string actionPrivate;
private string actionOther;
private bool ownersOnly;
/// <summary>
/// Create a respond rule from a regex and a reponse message or command
/// </summary>
/// <param name="regex">Regex</param>
/// <param name="actionPublic">Internal command to run for public messages</param>
/// <param name="actionPrivate">Internal command to run for private messages</param>
/// <param name="actionOther">Internal command to run for any other messages</param>
/// <param name="ownersOnly">Only match messages from bot owners</param>
public RespondRule(Regex regex, string actionPublic, string actionPrivate, string actionOther, bool ownersOnly)
{
this.regex = regex;
this.match = null;
this.actionPublic = actionPublic;
this.actionPrivate = actionPrivate;
this.actionOther = actionOther;
this.ownersOnly = ownersOnly;
}
/// <summary>
/// Create a respond rule from a match string and a reponse message or command
/// </summary>
/// <param name="match">Match string</param>
/// <param name="actionPublic">Internal command to run for public messages</param>
/// <param name="actionPrivate">Internal command to run for private messages</param>
/// <param name="ownersOnly">Only match messages from bot owners</param>
public RespondRule(string match, string actionPublic, string actionPrivate, string actionOther, bool ownersOnly)
{
this.regex = null;
this.match = match;
this.actionPublic = actionPublic;
this.actionPrivate = actionPrivate;
this.actionOther = actionOther;
this.ownersOnly = ownersOnly;
}
/// <summary>
/// Match the respond rule to the specified string and return a message or command to send if a match is detected
/// </summary>
/// <param name="username">Player who have sent the message</param>
/// <param name="message">Message to match against the regex or match string</param>
/// <param name="msgType">Type of the message public/private message, or other message</param>
/// <returns>Internal command to run as a response to this user, or null if no match has been detected</returns>
public string Match(string username, string message, MessageType msgType)
{
string toSend = null;
if (ownersOnly && (String.IsNullOrEmpty(username) || !Settings.Bots_Owners.Contains(username.ToLower())))
return null;
switch (msgType)
{
case MessageType.Public: toSend = actionPublic; break;
case MessageType.Private: toSend = actionPrivate; break;
case MessageType.Other: toSend = actionOther; break;
}
if (String.IsNullOrEmpty(toSend))
return null;
if (regex != null)
{
if (regex.IsMatch(message))
{
Match regexMatch = regex.Match(message);
for (int i = regexMatch.Groups.Count - 1; i >= 1; i--)
toSend = toSend.Replace("$" + i, regexMatch.Groups[i].Value);
toSend = toSend.Replace("$u", username);
return toSend;
}
}
else if (!String.IsNullOrEmpty(match))
{
if (message.ToLower().Contains(match.ToLower()))
{
return toSend.Replace("$u", username);
}
}
return null;
}
}
/// <summary>
/// Initialize the AutoRespond bot from the matches file
/// </summary>
public override void Initialize()
{
if (File.Exists(matchesFile))
{
Regex matchRegex = null;
string matchString = null;
string matchAction = null;
string matchActionPrivate = null;
string matchActionOther = null;
bool ownersOnly = false;
respondRules = new List<RespondRule>();
foreach (string lineRAW in File.ReadAllLines(matchesFile))
{
string line = lineRAW.Split('#')[0].Trim();
if (line.Length > 0)
{
if (line[0] == '[' && line[line.Length - 1] == ']')
{
switch (line.Substring(1, line.Length - 2).ToLower())
{
case "match":
CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly);
matchRegex = null;
matchString = null;
matchAction = null;
matchActionPrivate = null;
matchActionOther = null;
ownersOnly = false;
break;
}
}
else
{
string argName = line.Split('=')[0];
if (line.Length > (argName.Length + 1))
{
string argValue = line.Substring(argName.Length + 1);
switch (argName.ToLower())
{
case "regex": matchRegex = new Regex(argValue); break;
case "match": matchString = argValue; break;
case "action": matchAction = argValue; break;
case "actionprivate": matchActionPrivate = argValue; break;
case "actionother": matchActionOther = argValue; break;
case "ownersonly": ownersOnly = Settings.str2bool(argValue); break;
}
}
}
}
}
CheckAddMatch(matchRegex, matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly);
}
else
{
LogToConsole("File not found: '" + matchesFile + "'");
UnloadBot(); //No need to keep the bot active
}
}
/// <summary>
/// Create a new respond rule from the provided arguments, only if they are valid: at least one match and one action
/// </summary>
/// <param name="matchRegex">Matching regex</param>
/// <param name="matchString">Matching string</param>
/// <param name="matchAction">Action if the matching message is public</param>
/// <param name="matchActionPrivate">Action if the matching message is private</param>
/// <param name="ownersOnly">Only match messages from bot owners</param>
private void CheckAddMatch(Regex matchRegex, string matchString, string matchAction, string matchActionPrivate, string matchActionOther, bool ownersOnly)
{
if (matchAction != null || matchActionPrivate != null || matchActionOther != null)
{
if (matchRegex != null)
{
respondRules.Add(new RespondRule(matchRegex, matchAction, matchActionPrivate, matchActionOther, ownersOnly));
}
else if (matchString != null)
{
respondRules.Add(new RespondRule(matchString, matchAction, matchActionPrivate, matchActionOther, ownersOnly));
}
}
}
public override void GetText(string text)
{
//Remove colour codes
text = GetVerbatim(text);
//Get Message type
string sender = "", message = "";
MessageType msgType = MessageType.Other;
if (IsChatMessage(text, ref message, ref sender))
msgType = MessageType.Public;
else if (IsPrivateMessage(text, ref message, ref sender))
msgType = MessageType.Private;
else message = text;
//Do not process messages sent by the bot itself
if (msgType == MessageType.Other || sender != Settings.Username)
{
foreach (RespondRule rule in respondRules)
{
string toPerform = rule.Match(sender, message, msgType);
if (!String.IsNullOrEmpty(toPerform))
{
string response = null;
LogToConsole(toPerform);
PerformInternalCommand(toPerform, ref response);
if (!String.IsNullOrEmpty(response))
LogToConsole(response);
}
}
}
}
}
}