-
Notifications
You must be signed in to change notification settings - Fork 0
/
Formatter.cs
285 lines (265 loc) · 10.1 KB
/
Formatter.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
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2 of the License, or
// (at your option) version 3.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
using System;
using System.Collections.Generic;
using System.Text;
namespace libirc
{
/// <summary>
/// This class allow you to format the mode to better look, so that
/// you can for example merge multiple modes to 1 line
/// </summary>
public class Formatter
{
private int ParametersPerOneLine = 2;
private int ModesPerOneLine = 20;
/// <summary>
/// Prefix of mode
/// </summary>
public string Prefix = "";
/// <summary>
/// This buffer contains modes that belong to channel and is only filled up when you rewrite this formatter with custom mode
/// </summary>
public string channelModes = "";
private string buffer = null;
/// <summary>
/// If this is true the produced string will remove the modes
/// </summary>
public bool Removing = false;
private List<SimpleMode> Mode = new List<SimpleMode>();
private List<SimpleMode> rMode = new List<SimpleMode>();
/// <summary>
/// Return a list of modes
/// </summary>
public List<SimpleMode> getMode
{
get
{
List<SimpleMode> mode = new List<SimpleMode>();
lock (Mode)
{
mode.AddRange(Mode);
return mode;
}
}
}
/// <summary>
/// Return a list of modes that are being removed
/// </summary>
public List<SimpleMode> getRemovingMode
{
get
{
List<SimpleMode> mode = new List<SimpleMode>();
lock (rMode)
{
mode.AddRange(rMode);
return mode;
}
}
}
public Formatter() {}
/// <summary>
/// Require number of parameters and modes
/// </summary>
/// <param name="_ParametersPerOneLine"></param>
/// <param name="_ModesPerOneLine"></param>
public Formatter(int _ParametersPerOneLine, int _ModesPerOneLine)
{
ParametersPerOneLine = _ParametersPerOneLine;
ModesPerOneLine = _ModesPerOneLine;
}
/// <summary>
/// Insert a list of modes to parser
/// </summary>
/// <param name="mode"></param>
public void InsertModes(List<SimpleMode> mode)
{
lock (Mode)
{
Mode.AddRange(mode);
}
}
/// <summary>
/// Changes the content based on buffer
/// </summary>
/// <param name="data"></param>
/// <param name="network"></param>
public void RewriteBuffer(string data, Network network)
{
lock (Mode)
{
buffer = data;
Mode.Clear();
List<string> line = new List<string>();
line.AddRange(data.Split('\n'));
rMode.Clear();
channelModes = "";
string positive = "+";
string negative = "-";
foreach (string xx in line)
{
bool rm = false;
int CurrentParam = 1;
List<string> parts = new List<string>();
parts.AddRange(xx.Split(' '));
if (parts.Count > 0)
{
if (parts[0].Contains("-") || parts[0].Contains("+"))
{
foreach (char CurrentMode in parts[0])
{
switch (CurrentMode)
{
case '+':
rm = false;
continue;
case '-':
rm = true;
continue;
}
// user mode, has a parameter
if (network.CUModes.Contains(CurrentMode) || network.PModes.Contains(CurrentMode))
{
if (parts.Count < CurrentParam + 1)
{
return;
}
SimpleMode mode = new SimpleMode(CurrentMode, parts[CurrentParam]);
if (rm)
{
rMode.Add(mode);
}
else
{
Mode.Add(mode);
}
CurrentParam++;
continue;
}
// channel special mode with parameter
if (network.SModes.Contains(CurrentMode) || network.XModes.Contains(CurrentMode))
{
if (parts.Count < CurrentParam + 1)
{
return;
}
SimpleMode mode = new SimpleMode(CurrentMode, parts[CurrentParam]);
if (rm)
{
rMode.Add(mode);
positive = positive.Replace(CurrentMode.ToString(), "");
negative += CurrentMode.ToString();
}
else
{
Mode.Add(mode);
negative = negative.Replace(CurrentMode.ToString(), "");
positive += CurrentMode.ToString();
}
CurrentParam++;
continue;
}
// channel mode
if (network.CModes.Contains(CurrentMode))
{
SimpleMode mode = new SimpleMode(CurrentMode, null);
if (rm)
{
rMode.Add(mode);
positive = positive.Replace(CurrentMode.ToString(), "");
negative += CurrentMode.ToString();
}
else
{
Mode.Add(mode);
negative = negative.Replace(CurrentMode.ToString(), "");
positive += CurrentMode.ToString();
}
continue;
}
}
}
}
}
if (positive.Length > 1)
{
channelModes += positive;
}
if (negative.Length > 1)
{
channelModes += negative;
}
}
}
private void Format()
{
string modes = "+";
if (Removing)
{
modes = "-";
}
string parameters = " ";
int CurrentMode = 1;
buffer = "";
int CurrentLine = 1;
int CurrentPm = 1;
lock (Mode)
{
if (Mode.Count == 0)
{
return;
}
foreach (SimpleMode xx in Mode)
{
if (CurrentMode > ModesPerOneLine || CurrentPm > ParametersPerOneLine)
{
buffer += Prefix + modes + parameters + "\n";
CurrentLine++;
if (Removing)
{
modes = "-";
}
else
{
modes = "+";
}
parameters = " ";
CurrentMode = 1;
CurrentPm = 1;
}
modes += xx.Mode.ToString();
CurrentMode++;
if (xx.ContainsParameter)
{
parameters += xx.Parameter + " ";
CurrentPm++;
}
}
buffer += Prefix + modes + parameters + "\n";
}
}
/// <summary>
/// String
/// </summary>
/// <returns></returns>
public override string ToString()
{
lock (Mode)
{
Format();
return buffer;
}
}
}
}