Advertisement
Guest User

Walter

a guest
Jul 1st, 2015
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.98 KB | None | 0 0
  1. using System;
  2.  
  3. using System.IO;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7.  
  8. namespace KonekoIRC
  9. {
  10.  
  11.     class KonekoIRC
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             Console.WriteLine("Initiating Walter C Dornez...");
  16.             IRC IrcConnection = new IRC();
  17.             IrcConnection.Connect("irc.cuff-link.me", 6667);
  18.  
  19.             string line;
  20.             Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):");
  21.             Console.WriteLine();
  22.             do
  23.             {
  24.                 Console.Write("   ");
  25.                 line = Console.ReadLine();
  26.                 if (line != null)
  27.                     Console.WriteLine("      " + line);
  28.                 IrcConnection.IrcWriter.WriteLine("" + line);
  29.                 IrcConnection.IrcWriter.Flush();
  30.             } while (line != null);  
  31.         }
  32.     }
  33.  
  34.     class IRC
  35.     {
  36.         #region Private Variables
  37.         private string IrcServer;
  38.         private int IrcPort;
  39.  
  40.         private TcpClient IrcConnection;
  41.         private Stream IrcStream;
  42.         private StreamReader IrcReader;
  43.         public StreamWriter IrcWriter;
  44.  
  45.         private string IrcCommand;
  46.         private Int16 IrcPingCount;
  47.         private bool IrcChannelConnected;
  48.         #endregion
  49.  
  50.         public async void Connect(string argServer, int argPort)
  51.         {
  52.             Console.WriteLine("Attempting Connection...");
  53.             this.IrcServer = argServer;
  54.             this.IrcPort = argPort;
  55.  
  56.             this.IrcConnection = new TcpClient(this.IrcServer, this.IrcPort);
  57.             this.IrcStream = this.IrcConnection.GetStream();
  58.             this.IrcReader = new StreamReader(this.IrcStream);
  59.             this.IrcWriter = new StreamWriter(this.IrcStream);
  60.             Console.WriteLine("Connected!");
  61.  
  62.             this.IrcWriter.WriteLine(String.Format("USER {0} {1} * :{2}", "Walter", "0", "Walter"));
  63.             this.IrcWriter.Flush();
  64.             this.IrcWriter.WriteLine(String.Format("NICK {0}", "WalterCDornez"));
  65.             this.IrcWriter.Flush();
  66.             Console.WriteLine("Authenticated!");
  67.  
  68.             // await Task.Run(() => { Listener(); });
  69.             await IrcStreamReaderAsync(IrcReader);
  70.             // Listener();
  71.         }
  72.  
  73.         public async Task IrcStreamReaderAsync(StreamReader argIrcReader)
  74.         {
  75.             while ((IrcCommand = await argIrcReader.ReadLineAsync()) != null)
  76.             {
  77.  
  78.                 Console.WriteLine(IrcCommand);
  79.                 if (IrcCommand.Contains("PING"))
  80.                 {
  81.  
  82.                     string[] IrcCommandArray = new string[IrcCommand.Split(' ').Length];
  83.                     IrcCommandArray = IrcCommand.Split(' ');
  84.                     string IrcPong = IrcCommandArray[1].TrimStart(':');
  85.                     this.IrcWriter.WriteLine(String.Format("PONG {0}", IrcPong));
  86.                     this.IrcWriter.Flush();
  87.  
  88.                     IrcPingCount++;
  89.                     Console.WriteLine("PONG: " + IrcPingCount.ToString());
  90.                 }
  91.                 if (IrcPingCount >= 1 & IrcChannelConnected == false)
  92.                 {
  93.                     Thread.Sleep(5000);
  94.                     Console.WriteLine("Attempting Channel Connection...");
  95.  
  96.                     this.IrcWriter.WriteLine(String.Format("JOIN {0}", "#DepravityManor"));
  97.                     this.IrcWriter.Flush();
  98.                     IrcChannelConnected = true;
  99.  
  100.                     this.IrcWriter.WriteLine("MODE WalterCDornez +B");
  101.                     this.IrcWriter.Flush();
  102.                 }
  103.             }
  104.         }
  105.  
  106.         private void Listener(){
  107.             while(true){
  108.                 while ((IrcCommand = this.IrcReader.ReadLine()) != null)
  109.                 {
  110.                     if (IrcPingCount >= 1 & IrcChannelConnected == false)
  111.                     {
  112.                         Console.WriteLine("Attempting Channel Connection...");
  113.                         this.IrcWriter.WriteLine(String.Format("JOIN {0}", "#DepravityManor"));
  114.                         this.IrcWriter.Flush();
  115.                         IrcChannelConnected = true;
  116.  
  117.                         this.IrcWriter.WriteLine("MODE WalterCDornez +B");
  118.                         this.IrcWriter.Flush();
  119.                     }
  120.  
  121.                     Console.WriteLine(IrcCommand);
  122.                     if(IrcCommand.Contains("PING")){
  123.                        
  124.                         string[] IrcCommandArray = new string[IrcCommand.Split(' ').Length];
  125.                         IrcCommandArray = IrcCommand.Split(' ');
  126.                         string IrcPong = IrcCommandArray[1].TrimStart(':');
  127.                         this.IrcWriter.WriteLine(String.Format("PONG {0}", IrcPong));
  128.                         this.IrcWriter.Flush();
  129.  
  130.                         IrcPingCount++;
  131.                         Console.WriteLine("PONG: " + IrcPingCount.ToString());                  
  132.                     }
  133.                 }
  134.             }
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement