Advertisement
Guest User

Untitled

a guest
Apr 28th, 2014
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using System.Threading;
  7. using System.IO;
  8. using System.Net;
  9. using System.Net.Sockets;
  10.  
  11. namespace ConsoleClient
  12. {
  13.     class Program
  14.     {
  15.         static TcpClient client;
  16.         static StreamReader rdr;
  17.         static StreamWriter rwr;
  18.         static bool running = true;
  19.         static void Main(string[] args)
  20.         {
  21.             run();
  22.         }
  23.  
  24.         static void reset()
  25.         {
  26.             Console.ForegroundColor = ConsoleColor.Yellow;
  27.             Console.WriteLine("Resetting..");
  28.             Console.ResetColor();
  29.  
  30.             running = false;
  31.             client = null;
  32.             rdr.Dispose();
  33.             rwr.Dispose();
  34.  
  35.             run();
  36.         }
  37.  
  38.         static void run()
  39.         {
  40.             client = new TcpClient();
  41.             Console.WriteLine("-Connecting-");
  42.             client.Connect("irc.esper.net", 6667);
  43.  
  44.             rdr = new StreamReader(client.GetStream());
  45.             rwr = new StreamWriter(client.GetStream());
  46.  
  47.             new Thread(new ThreadStart(DoRead)).Start();
  48.  
  49.             Console.WriteLine("Input Ready!");
  50.             string inl = "";
  51.  
  52.             int tries = 0;
  53.             do
  54.             {
  55.                 try
  56.                 {
  57.                     inl = Console.ReadLine();
  58.  
  59.                     rwr.WriteLine(inl);
  60.                     rwr.Flush();
  61.  
  62.                     tries = 0;
  63.                 }
  64.                 catch
  65.                 {
  66.                     if (running && tries >= 3)
  67.                     {
  68.                         reset();
  69.                         break;
  70.                     }
  71.                     tries++;
  72.                    
  73.                 }
  74.             } while (inl != "exit");
  75.             running = false;
  76.         }
  77.  
  78.         static void DoRead()
  79.         {
  80.             int tries = 0;
  81.             while (running)
  82.             {
  83.                 try
  84.                 {
  85.                     string line = rdr.ReadLine();
  86.  
  87.                     Match match = Regex.Match(line, @"^(:(?<prefix>\S+) )?(?<command>\S+)( (?!:)(?<params>.+?))?( :(?<trail>.+))?$");
  88.                     if (match.Success)
  89.                     {
  90.                         string prefix = match.Groups["prefix"].Value;
  91.                         string command = match.Groups["command"].Value;
  92.                         string param = match.Groups["params"].Value;
  93.                         string trail = match.Groups["trail"].Value;
  94.  
  95.                         switch (command)
  96.                         {
  97.                             case "PING":
  98.                                 rwr.Write("PONG :" + trail + "\r\n");
  99.                                 outMsg("PING");
  100.                                 break;
  101.                             default:
  102.                                 outMsg(line);
  103.                                 break;
  104.                         }
  105.                     }
  106.                     else
  107.                         outMsg("[e]"+line);
  108.                    
  109.                 }
  110.                 catch
  111.                 {
  112.                     if (running && tries >= 3)
  113.                     {
  114.                         reset();
  115.                         break;
  116.                     }
  117.                     tries++;
  118.                 }
  119.             }
  120.         }
  121.         static void outMsg(string line)
  122.         {
  123.             Console.ForegroundColor = ConsoleColor.Red;
  124.             Console.WriteLine(line);
  125.             Console.ResetColor();
  126.         }
  127.     }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement