Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.IO;
- using System.Net;
- using System.Net.Sockets;
- namespace ConsoleClient
- {
- class Program
- {
- static TcpClient client;
- static StreamReader rdr;
- static StreamWriter rwr;
- static bool running = true;
- static void Main(string[] args)
- {
- run();
- }
- static void reset()
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Resetting..");
- Console.ResetColor();
- running = false;
- client = null;
- rdr.Dispose();
- rwr.Dispose();
- run();
- }
- static void run()
- {
- client = new TcpClient();
- Console.WriteLine("-Connecting-");
- client.Connect("irc.esper.net", 6667);
- rdr = new StreamReader(client.GetStream());
- rwr = new StreamWriter(client.GetStream());
- new Thread(new ThreadStart(DoRead)).Start();
- Console.WriteLine("Input Ready!");
- string inl = "";
- int tries = 0;
- do
- {
- try
- {
- inl = Console.ReadLine();
- rwr.WriteLine(inl);
- rwr.Flush();
- tries = 0;
- }
- catch
- {
- if (running && tries >= 3)
- {
- reset();
- break;
- }
- tries++;
- }
- } while (inl != "exit");
- running = false;
- }
- static void DoRead()
- {
- int tries = 0;
- while (running)
- {
- try
- {
- string line = rdr.ReadLine();
- Match match = Regex.Match(line, @"^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$");
- if (match.Success)
- {
- string prefix = match.Groups["prefix"].Value;
- string command = match.Groups["command"].Value;
- string param = match.Groups["params"].Value;
- string trail = match.Groups["trail"].Value;
- switch (command)
- {
- case "PING":
- rwr.Write("PONG :" + trail + "\r\n");
- outMsg("PING");
- break;
- default:
- outMsg(line);
- break;
- }
- }
- else
- outMsg("[e]"+line);
- }
- catch
- {
- if (running && tries >= 3)
- {
- reset();
- break;
- }
- tries++;
- }
- }
- }
- static void outMsg(string line)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(line);
- Console.ResetColor();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement