/
IrcCommand.cs
173 lines (141 loc) · 4.79 KB
/
IrcCommand.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
using System;
using System.Linq;
using System.Collections.Generic;
namespace TwitchChatBot
{
public class IrcCommand
{
public IrcCommand(string inPrefix,string inName, params IrcCommandParameter[] inParams)
{
mPrefix = inPrefix;
mName = inName;
mParams = inParams;
}
/* IRC-command format
<message> ::= [':' <prefix> <SPACE> ] <command> <params> <crlf>
<prefix> ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
<command> ::= <letter> { <letter> } | <number> <number> <number>
<SPACE> ::= ' ' { ' ' }
<params> ::= <SPACE> [ ':' <trailing> | <middle> <params> ]
<middle> ::= <Any *non-empty* sequence of octets not including SPACE
or NUL or CR or LF, the first of which may not be ':'>
<trailing> ::= <Any, possibly *empty*, sequence of octets not including
NUL or CR or LF>
<crlf> ::= CR LF
*/
public static IrcCommand Parse (string inCommandString)
{
string prefix = null, name = null;
List<IrcCommandParameter> parameters = new List<IrcCommandParameter>();
if (inCommandString [0] == ':') {
prefix = inCommandString.Substring(1,inCommandString.IndexOf(' ') - 1);
inCommandString = inCommandString.Substring(inCommandString.IndexOf(' ') + 1);
}
name = inCommandString.Substring(0,inCommandString.IndexOf(' '));
inCommandString = inCommandString.Substring(inCommandString.IndexOf(' ') + 1);
while (inCommandString.Length > 0)
{
if (inCommandString[0] == ':')
{
parameters.Add(inCommandString.Substring(1).AsIrcCommandTraillingParameter());
inCommandString = "";
}
else
{
int nextParamEnd = inCommandString.IndexOf(' ');
if (nextParamEnd == -1)
{
nextParamEnd = inCommandString.Length;
}
string nextParam = inCommandString.Substring(0, nextParamEnd);
parameters.Add(new IrcCommandParameter(nextParam,false)); //TODO: create an extension for not trailling - parameter case
if (nextParamEnd != inCommandString.Length)
{
inCommandString = inCommandString.Substring(nextParamEnd + 1);
}
else
{
inCommandString = "";
}
}
}
return new IrcCommand(prefix,name,parameters.ToArray());
}
public override string ToString ()
{
return (mPrefix == null ? "" : ":" + mPrefix + " ") + mName + " " + String.Join(" ",mParams.Select(p=>p.ToString()));
}
public string Name {
get{
return mName;
}
}
public string Prefix {
get {
return mPrefix;
}
}
public IrcCommandParameter[] Parameters{
get {
return mParams;
}
}
string mPrefix;
string mName;
IrcCommandParameter[] mParams;
}
public class IrcCommandParameter
{
bool mTrailling;
string mValue;
public IrcCommandParameter ( string inValue, bool inTrailling = false )
{
mValue = inValue;
mTrailling = inTrailling;
}
public override string ToString ()
{
return (mTrailling ? ":" : "") + mValue;
}
public string Value
{
get {
return mValue;
}
}
}
public static class IrcCommandParameterExtensions
{
public static IrcCommandParameter AsIrcCommandTraillingParameter(this string inParameter)
{
return new IrcCommandParameter(inParameter, true);
}
}
public abstract class IrcCommandAnalyzer
{
public abstract IrcCommand GetResponse(IrcCommand inRequestCommand);
}
public class SimpleTwitchBotIrcCommandAnalyzer : IrcCommandAnalyzer
{
public SimpleTwitchBotIrcCommandAnalyzer()
{
mDispatchTable = new Dictionary<string, Func<IrcCommand, IrcCommand>>();
mDispatchTable["PING"] = Ping;
}
public override IrcCommand GetResponse(IrcCommand inRequestCommand)
{
return mDispatchTable.ContainsKey(inRequestCommand.Name) ? mDispatchTable[inRequestCommand.Name].Invoke(inRequestCommand) : null;
}
IrcCommand Ping(IrcCommand inCommand)
{
Console.WriteLine("Ping-Pong");
//return new IrcCommand(null, "PONG", new IrcCommandParameter("tmi.twitch.tv"));
return new IrcCommand(null, "PONG", inCommand.Parameters);
}
IrcCommand Privmsg(IrcCommand inCommand)
{
return null;
}
Dictionary<string, Func<IrcCommand, IrcCommand>> mDispatchTable;
}
}