Advertisement
Guest User

Untitled

a guest
May 21st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 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 = "xyz";
  23.         public string real_name = "xyz";
  24.         public string nick = "RaidBot2";
  25.         public string oper_user = "";
  26.         public string oper_pass = "";
  27.         public string nick_pass = "";
  28.         public void Init()
  29.         {
  30.             Bot = new TcpClient();
  31.             Bot.Connect(hostname, port);
  32.             if (Bot != null)
  33.             {
  34.  
  35.                 if (use_ssl)
  36.                 {
  37.                     SecureWriter = new SslStream(Bot.GetStream());
  38.                     SecureReader = new SslStream(Bot.GetStream());
  39.                     SecureReader.AuthenticateAsClient(hostname);
  40.                     SecureWriter.AuthenticateAsClient(hostname);
  41.                 }
  42.                 else
  43.                 {
  44.                     Writer = Bot.GetStream();
  45.                     Reader = Bot.GetStream();
  46.                 }
  47.             }
  48.             else
  49.             {
  50.                 // add error handling...
  51.             }
  52.             Thread T = new Thread(new ThreadStart(Process));
  53.             T.Start();
  54.            
  55.         }
  56.         public void Join(string channel)
  57.         {
  58.             Enqueue("JOIN {0}", channel);
  59.         }
  60.         public void Join(string channel, string key)
  61.         {
  62.             Enqueue("JOIN {0} {1}", channel, key);
  63.         }
  64.         public void JoinChannels(params string[] channels)
  65.         {
  66.             foreach (string s in channels)
  67.             {
  68.                 if (s.Contains(":"))
  69.                 {
  70.                     string[] chan = s.Split(new char[] { ':' });
  71.                     Join(chan[0], chan[1]);
  72.                 }
  73.                 else
  74.                 {
  75.                     Join(s);
  76.                 }
  77.             }
  78.         }
  79.         public void Identify()
  80.         {
  81.            
  82.             Enqueue("USER {0} {0} {0} :{1}", user_name, real_name);
  83.             Enqueue("NICK {0}", nick);
  84.             if (is_oper)
  85.             {
  86.                 Enqueue("OPER {0} {1}", oper_user, oper_pass);
  87.             }
  88.             Enqueue("PRIVMSG NickServ IDENTIFY :", nick_pass);
  89.  
  90.         }
  91.         public void EvalData(string data)
  92.         {
  93.         }
  94.         public void Parse(string ircdata)
  95.         {
  96.             ircdata = ircdata.Replace(":", "");
  97.             ircdata = ircdata.Trim(new char[] { '\r', '\n', '\0', ':' });
  98.             string[] msgs = ircdata.Split(new char[] { ' ' });
  99.             for (int i = 0; i < msgs.Length; i++)
  100.             {
  101.                 if (debug)
  102.                 {
  103.                     Console.WriteLine("msgs[{0}] = {1}", i, msgs[i]);
  104.                 }
  105.                
  106.             }
  107.             EvalData(ircdata);
  108.            
  109.         }
  110.         public void Process()
  111.         {
  112.            
  113.             Thread.Sleep(100);
  114.             bool reading = true;
  115.             while(true)
  116.             {
  117.                 if (Messages.Count > 0)
  118.                 { Console.WriteLine("Queue.Length = {0}", Messages.Count); }
  119.                 reading = !reading;
  120.                 byte[] buffer = new byte[1024];
  121.                 if(reading)
  122.                 {
  123.                     if (use_ssl)
  124.                     {
  125.                         if (SecureReader == null)
  126.                         {
  127.                             continue;
  128.                         }
  129.                         SecureReader.Read(buffer, 0, 1024);
  130.                     }
  131.                     else
  132.                     {
  133.                         Reader.Read(buffer, 0, 1024);
  134.                     }
  135.                     string[] msgs = Encoding.ASCII.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  136.                     foreach (string msg in msgs)
  137.                     {
  138.                         Parse(msg);
  139.                        
  140.                     }
  141.                 }
  142.                 else
  143.                 {
  144.                     while(Messages.Count > 0)
  145.                     {
  146.                         Thread.Sleep(100);
  147.                         string s = Messages.Dequeue();
  148.                         Write(s);
  149.                         Console.WriteLine("REMOVED {0} from the queue", s);
  150.                     }
  151.                 }
  152.                
  153.             }
  154.            
  155.         }
  156.         public void Privmsg(string target, string msg)
  157.         {
  158.             Enqueue("PRIVMSG {0} :{1}", target, msg);
  159.         }
  160.         public void Notice(string target, string msg)
  161.         {
  162.             Enqueue("NOTICE {0} :{1}", target, msg);
  163.         }
  164.         public void Enqueue(string data)
  165.         {
  166.             Messages.Enqueue(data);
  167.         }
  168.         public void Enqueue(string data, params object[] args)
  169.         {
  170.             data = String.Format(data, args);
  171.             Messages.Enqueue(data);
  172.         }
  173.         public void Write(string msg)
  174.         {
  175.             byte[] data = Encoding.ASCII.GetBytes(msg + "\r\n");
  176.             if (Writer != null)
  177.             {
  178.                 if (use_ssl)
  179.                 {
  180.                     SecureWriter.Write(data, 0, data.Length);
  181.                 }
  182.                 else
  183.                 {
  184.                     Writer.Write(data, 0, data.Length);
  185.                 }
  186.             }
  187.         }
  188.         public void Write(string msg, params object[] args)
  189.         {
  190.             msg = String.Format(msg, args);
  191.             Write(msg);
  192.         }
  193.  
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement