87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
< |
private StringWriter writer = new StringWriter(); |
90 |
> |
private StreamWriter writer; |
91 |
|
private SortedDictionary<string, List<Login>> logins = new SortedDictionary<string, List<Login>>(); |
92 |
+ |
private bool forward = false; |
93 |
|
private List<string> nots = new List<string>(); |
94 |
|
|
95 |
|
private void Full() |
96 |
|
{ |
97 |
+ |
if (forward) |
98 |
+ |
writer.Write("finger: forwarding service denied\r\n"); |
99 |
+ |
|
100 |
|
foreach (string not in nots) |
101 |
|
writer.Write("finger: {0}: no such user\r\n", not); |
102 |
|
|
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
< |
public Finger(bool full) |
185 |
> |
public Finger(StreamWriter writer, bool full) |
186 |
|
{ |
187 |
+ |
this.writer = writer; |
188 |
+ |
|
189 |
|
Sessions(logins); |
190 |
|
|
191 |
|
if (logins.Count < 1) |
202 |
|
} |
203 |
|
} |
204 |
|
|
205 |
< |
public Finger(ICollection<string> names) |
205 |
> |
public Finger(StreamWriter writer, ICollection<string> names) |
206 |
|
{ |
207 |
+ |
this.writer = writer; |
208 |
+ |
|
209 |
|
uint count; |
210 |
|
Net.NET_DISPLAY_USER[] users; |
211 |
|
|
216 |
|
Sessions(logins_); |
217 |
|
|
218 |
|
foreach (string name in names) |
219 |
< |
{ |
220 |
< |
bool not = true; |
221 |
< |
|
222 |
< |
for (uint index = 0; index != count; ++index) |
223 |
< |
if (users[index].usri1_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 || users[index].usri1_full_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 && !logins.ContainsKey(users[index].usri1_name)) |
216 |
< |
{ |
217 |
< |
logins.Add(users[index].usri1_name, new List<Login>()); |
218 |
< |
|
219 |
< |
logins[users[index].usri1_name].Add(new Login(users[index].usri1_name)); |
220 |
< |
|
221 |
< |
if (logins_.ContainsKey(users[index].usri1_name)) |
222 |
< |
logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]); |
219 |
> |
if (name.Contains("@")) |
220 |
> |
forward = true; |
221 |
> |
else |
222 |
> |
{ |
223 |
> |
bool not = true; |
224 |
|
|
225 |
< |
not = false; |
226 |
< |
} |
225 |
> |
for (uint index = 0; index != count; ++index) |
226 |
> |
if (users[index].usri1_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1 || users[index].usri1_full_name.IndexOf(name, StringComparison.OrdinalIgnoreCase) != -1) |
227 |
> |
try |
228 |
> |
{ |
229 |
> |
logins.Add(users[index].usri1_name, new List<Login>()); |
230 |
> |
|
231 |
> |
logins[users[index].usri1_name].Add(new Login(users[index].usri1_name)); |
232 |
> |
|
233 |
> |
if (logins_.ContainsKey(users[index].usri1_name)) |
234 |
> |
logins[users[index].usri1_name].AddRange(logins_[users[index].usri1_name]); |
235 |
> |
} |
236 |
> |
catch (ArgumentException) {} |
237 |
> |
finally |
238 |
> |
{ |
239 |
> |
not = false; |
240 |
> |
} |
241 |
|
|
242 |
< |
if (not) |
243 |
< |
nots.Add(name); |
244 |
< |
} |
242 |
> |
if (not) |
243 |
> |
nots.Add(name); |
244 |
> |
} |
245 |
|
|
246 |
|
Full(); |
247 |
|
} |
233 |
– |
|
234 |
– |
public override string ToString() |
235 |
– |
{ |
236 |
– |
return writer.ToString(); |
237 |
– |
} |
248 |
|
} |
249 |
|
|
250 |
|
private static void Do() |
259 |
|
|
260 |
|
while (true) |
261 |
|
{ |
262 |
< |
TcpClient client = listener.AcceptTcpClient(); |
262 |
> |
Socket socket = listener.AcceptSocket(); |
263 |
> |
|
264 |
> |
socket.ReceiveTimeout = 60000; |
265 |
> |
|
266 |
|
Thread thread = new Thread(new ParameterizedThreadStart(Do)); |
267 |
|
|
268 |
< |
thread.Start(client); |
268 |
> |
thread.Start(socket); |
269 |
|
} |
270 |
|
} |
271 |
|
catch (SocketException exception) |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
< |
private static void Do(object client) |
281 |
> |
private static void Do(object socket_) |
282 |
|
{ |
283 |
< |
NetworkStream stream = ((TcpClient)client).GetStream(); |
284 |
< |
byte[] buffer = new byte[1024]; |
285 |
< |
int size = 0, byte_; |
283 |
> |
Socket socket = (Socket)socket_; |
284 |
> |
NetworkStream stream = new NetworkStream(socket); |
285 |
> |
|
286 |
> |
try |
287 |
> |
{ |
288 |
> |
byte[] buffer = new byte[1024]; |
289 |
> |
int size = 0, byte_; |
290 |
|
|
291 |
< |
while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1) |
292 |
< |
buffer[size++] = (byte)byte_; |
291 |
> |
while (size != buffer.Length && (byte_ = stream.ReadByte()) != '\r' && byte_ != '\n' && byte_ != -1) |
292 |
> |
buffer[size++] = (byte)byte_; |
293 |
|
|
294 |
< |
string line = Encoding.ASCII.GetString(buffer, 0, size)/*Console.ReadLine()*/; |
278 |
< |
List<string> names = new List<string>(); |
279 |
< |
bool full = false; |
294 |
> |
string line = Encoding.ASCII.GetString(buffer, 0, size); |
295 |
|
|
296 |
< |
foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) |
282 |
< |
{ |
283 |
< |
if (name == "/W") |
284 |
< |
full = true; |
285 |
< |
else |
286 |
< |
names.Add(name); |
287 |
< |
} |
296 |
> |
Console.WriteLine("{0} [{1}]", socket.RemoteEndPoint, line); |
297 |
|
|
298 |
< |
StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false)); |
298 |
> |
List<string> names = new List<string>(); |
299 |
> |
bool full = false; |
300 |
> |
|
301 |
> |
foreach (string name in line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)) |
302 |
> |
{ |
303 |
> |
if (name.ToUpper() == "/W") |
304 |
> |
full = true; |
305 |
> |
else |
306 |
> |
names.Add(name); |
307 |
> |
} |
308 |
> |
|
309 |
> |
StreamWriter writer = new StreamWriter(stream, new UTF8Encoding(false)); |
310 |
> |
|
311 |
> |
Finger finger = names.Count > 0 ? new Finger(writer, names) : new Finger(writer, full); |
312 |
> |
|
313 |
> |
writer.Flush(); |
314 |
> |
} |
315 |
> |
catch (IOException) {} |
316 |
|
|
317 |
< |
writer/*Console*/.Write(names.Count > 0 ? new Finger(names) : new Finger(full)); |
292 |
< |
writer.Close(); |
293 |
< |
//stream.Close(); |
294 |
< |
//((TcpClient)client).Close(); |
317 |
> |
socket.Close(60); |
318 |
|
} |
319 |
|
|
320 |
|
public static int Main(string[] args) |
321 |
|
{ |
322 |
< |
Do(/*null*/); |
322 |
> |
Do(); |
323 |
|
|
324 |
|
return 0; |
325 |
|
} |