Advertisement
Guest User

Untitled

a guest
May 21st, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.76 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. using System.Net.Security;
  6. using System.Net.Sockets;
  7. using System.Threading;
  8. namespace HardChatter2
  9. {
  10.     class IrcBot
  11.     {
  12.         public Queue<string> Messages = new Queue<string>();
  13.         private TcpClient Bot = null;
  14.         private NetworkStream Writer = null;
  15.         private NetworkStream Reader = null;
  16.         private SslStream SecureWriter;
  17.         private SslStream SecureReader;
  18.         public string hostname = "";
  19.         public int port = 6667;
  20.         public bool use_ssl = false;
  21.         private bool is_oper = false;
  22.         public string user_name = "";
  23.         public string real_name = "";
  24.         public string nick = "";
  25.         public string oper_user = "";
  26.         public string oper_pass = "";
  27.         public string nick_pass = "";
  28.         public void Init()
  29.         {
  30.             if (Bot != null)
  31.             {
  32.  
  33.                 if (use_ssl)
  34.                 {
  35.                     SecureWriter = new SslStream(Bot.GetStream());
  36.                     SecureReader = new SslStream(Bot.GetStream());
  37.                     SecureReader.AuthenticateAsClient(hostname);
  38.                     SecureWriter.AuthenticateAsClient(hostname);
  39.                 }
  40.                 else
  41.                 {
  42.                     Writer = Bot.GetStream();
  43.                     Reader = Bot.GetStream();
  44.                 }
  45.             }
  46.             else
  47.             {
  48.                 // add error handling...
  49.             }
  50.         }
  51.         public void Join(string channel)
  52.         {
  53.             Enqueue("JOIN {0}", channel);
  54.         }
  55.         public void Join(string channel, string key)
  56.         {
  57.             Enqueue("JOIN {0} {1}", channel, key);
  58.         }
  59.         public void JoinChannels(string[] channels)
  60.         {
  61.             foreach (string s in channels)
  62.             {
  63.                 if (s.Contains(":"))
  64.                 {
  65.                     string[] chan = s.Split(new char[] { ':' });
  66.                     Join(chan[0], chan[1]);
  67.                 }
  68.                 else
  69.                 {
  70.                     Join(s);
  71.                 }
  72.             }
  73.         }
  74.         public void Identify()
  75.         {
  76.             Enqueue("NICK {0}", nick);
  77.             Enqueue("USER {0} 8 * :{1}", user_name, real_name);
  78.             if (is_oper)
  79.             {
  80.                 Enqueue("OPER {0} {1}", oper_user, oper_pass);
  81.             }
  82.             Enqueue("PRIVMSG NickServ :", nick_pass);
  83.  
  84.         }
  85.         public void Parse(string data)
  86.         {
  87.         }
  88.         public void Process()
  89.         {
  90.             Thread.Sleep(100);
  91.             bool reading = true;
  92.             while(true)
  93.             {
  94.                 byte[] buffer = new byte[1024];
  95.                 if(reading)
  96.                 {
  97.                     if (use_ssl)
  98.                     {
  99.                         SecureReader.Read(buffer, 0, 1024);
  100.                     }
  101.                     else
  102.                     {
  103.                         Reader.Read(buffer, 0, 1024);
  104.                     }
  105.                     string[] msgs = Encoding.ASCII.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  106.                     foreach (string msg in msgs)
  107.                     {
  108.                         Parse(msg);
  109.                     }
  110.                 }
  111.                 else
  112.                 {
  113.                     while(Messages.Count > 0)
  114.                     {
  115.                         Thread.Sleep(100);
  116.                         Write(Messages.Dequeue());
  117.                     }
  118.                 }
  119.                 reading = !reading;
  120.             }
  121.         }
  122.         public void Privmsg(string target, string msg)
  123.         {
  124.             Enqueue("PRIVMSG {0} :{1}", target, msg);
  125.         }
  126.         public void Notice(string target, string msg)
  127.         {
  128.             Enqueue("NOICE {0} :{1}", target, msg);
  129.         }
  130.         public void Enqueue(string data)
  131.         {
  132.             Messages.Enqueue(data);
  133.         }
  134.         public void Enqueue(string data, params object[] args)
  135.         {
  136.             data = String.Format(data, args);
  137.             Messages.Enqueue(data);
  138.         }
  139.         public void Write(string msg)
  140.         {
  141.             byte[] data = Encoding.ASCII.GetBytes(msg + "\r\n");
  142.             if (Writer != null)
  143.             {
  144.                 if (use_ssl)
  145.                 {
  146.                     SecureWriter.Write(data, 0, data.Length);
  147.                 }
  148.                 else
  149.                 {
  150.                     Writer.Write(data, 0, data.Length);
  151.                 }
  152.             }
  153.         }
  154.         public void Write(string msg, params object[] args)
  155.         {
  156.             msg = String.Format(msg, args);
  157.             Write(msg);
  158.         }
  159.  
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement