Advertisement
Guest User

Untitled

a guest
May 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.29 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 = "RaidBot";
  25.         public string oper_user = "";
  26.         public string oper_pass = "";
  27.         public string nick_pass = "";
  28.         public bool debug = true;
  29.         string target = "";
  30.         public void Init()
  31.         {
  32.             Bot = new TcpClient();
  33.             Bot.Connect(hostname, port);
  34.             if (Bot != null)
  35.             {
  36.  
  37.                 if (use_ssl)
  38.                 {
  39.                     SecureWriter = new SslStream(Bot.GetStream());
  40.                     SecureReader = new SslStream(Bot.GetStream());
  41.                     SecureReader.AuthenticateAsClient(hostname);
  42.                     SecureWriter.AuthenticateAsClient(hostname);
  43.                 }
  44.                 else
  45.                 {
  46.                     Writer = Bot.GetStream();
  47.                     Reader = Bot.GetStream();
  48.                 }
  49.             }
  50.             else
  51.             {
  52.                 // add error handling...
  53.             }
  54.             Thread T = new Thread(new ThreadStart(Process));
  55.             T.Start();
  56.            
  57.         }
  58.         public void Join(string channel)
  59.         {
  60.             Enqueue("JOIN {0}", channel);
  61.         }
  62.         public void Join(string channel, string key)
  63.         {
  64.             Enqueue("JOIN {0} {1}", channel, key);
  65.         }
  66.         public void JoinChannels(params string[] channels)
  67.         {
  68.             foreach (string s in channels)
  69.             {
  70.                 if (s.Contains(":"))
  71.                 {
  72.                     string[] chan = s.Split(new char[] { ':' });
  73.                     Join(chan[0], chan[1]);
  74.                 }
  75.                 else
  76.                 {
  77.                     Join(s);
  78.                 }
  79.             }
  80.         }
  81.         public void Identify()
  82.         {
  83.             Enqueue("NICK {0}", nick);
  84.             Enqueue("USER {0} {0} {0}: {0}", user_name, real_name);
  85.            
  86.             if (is_oper)
  87.             {
  88.                 Enqueue("OPER {0} {1}", oper_user, oper_pass);
  89.             }
  90.             Enqueue("PRIVMSG NickServ IDENTIFY ", nick_pass);
  91.  
  92.         }
  93.         public void EvalData(string[] data)
  94.         {
  95.             try
  96.             {
  97.                 string nick = data[0].Split(new char[] { '!' })[0];
  98.                 string command = data[1];
  99.                 string chan = data[2];
  100.                 string action = data[3];
  101.                 if (action[0] == '!')
  102.                 {
  103.                     string cmd = action.Substring(1).ToLower();
  104.                     if (cmd == "join")
  105.                     {
  106.                         if (data.Length > 4)
  107.                         {
  108.                             Join(data[4], data[5]);
  109.                         }
  110.                         else
  111.                         {
  112.                             Join(data[4]);
  113.                         }
  114.                     }
  115.                     if (cmd == "part")
  116.                     {
  117.                         Enqueue("PART {0} {1}", chan, Combine(data, 4, data.Length));
  118.                     }
  119.                     if (cmd == "id")
  120.                     {
  121.                         Enqueue("PRIVMSG NickServ IDENTIFY {0}", nick_pass);
  122.                     }
  123.                     if (cmd == "target.set")
  124.                     {
  125.                         Enqueue("PRIVMSG {0} Tagret set to {1}", chan, Combine(data, 4, data.Length));
  126.                         target = Combine(data, 4, data.Length);
  127.  
  128.                     }
  129.                     if (cmd == "wiki")
  130.                     {
  131.                     }
  132.                     if (cmd == "target")
  133.                     {
  134.                         Enqueue("PRIVMSG {0} Cureent Target: {1}", chan, target);
  135.                     }
  136.  
  137.                     if (cmd == "raw")
  138.                     {
  139.                         Enqueue(Combine(data, 4, data.Length));
  140.                     }
  141.                 }
  142.             }
  143.             catch (Exception E)
  144.             {
  145.                 //Console.WriteLine("ERROR -- {0} --", E);
  146.             }
  147.         }
  148.         public string Combine(string[] array, int min, int max)
  149.         {
  150.             string s = "";
  151.             for (int i = min; i < max; i++)
  152.             {
  153.                 s += array[i] + " ";
  154.             }
  155.             return s;
  156.         }
  157.         public void Parse(string ircdata)
  158.         {
  159.             ircdata = ircdata.Replace(":", "");
  160.             ircdata = ircdata.Trim(new char[] { '\r', '\n', '\0', ':' });
  161.             string[] msgs = ircdata.Split(new char[] { ' ' });
  162.             if (msgs.Length > 1 && msgs[1] == "KICK" && msgs[3] == "RaidBot")
  163.             {
  164.                 string nick = msgs[0].Split(new char[] { '!' })[0];
  165.                 string chan = msgs[2];
  166.                 Join(chan);
  167.                 Enqueue("PRIVMSG {0} {1} kick me again and i will oven you faggot!", chan, nick);
  168.             }
  169.             if (msgs.Length > 2 && msgs[1] == "INVITE" && msgs[2] == "RaidBot")
  170.             {
  171.                 string nick = msgs[0].Split(new char[] { '!' })[0];
  172.                 string chan = msgs[3];
  173.                 Join(chan);
  174.                 Enqueue("PRIVMSG {0} Hello, RaidBot here i was invited by {1} <3", chan, nick);
  175.             }
  176.             for (int i = 0; i < msgs.Length; i++)
  177.             {
  178.                 if (debug)
  179.                 {
  180.                     Console.WriteLine("msgs[{0}] = {1}", i, msgs[i]);
  181.                 }
  182.                 if (msgs[i] == "PING")
  183.                 {
  184.                     Enqueue("PONG {0}", msgs[i+1]);
  185.                 }
  186.                 else if (msgs[i].ToUpper() == "\x01VERSION\x01")
  187.                 {
  188.                    
  189.                     Write(String.Format("NOTICE {0} {1}", nick, "Insurgency RaidBot written by xyz"));
  190.                 }
  191.                
  192.             }
  193.             EvalData(msgs);
  194.            
  195.         }
  196.         public void Process()
  197.         {
  198.             try
  199.             {
  200.  
  201.                 Thread.Sleep(100);
  202.                 bool reading = false;
  203.                 while (true)
  204.                 {
  205.                     if (Messages.Count > 0)
  206.                     { Console.WriteLine("Queue.Length = {0}", Messages.Count); }
  207.                     reading = !reading;
  208.                     byte[] buffer = new byte[1024];
  209.                     if (reading)
  210.                     {
  211.                         if (use_ssl)
  212.                         {
  213.                             if (SecureReader == null)
  214.                             {
  215.                                 continue;
  216.                             }
  217.                             SecureReader.Read(buffer, 0, 1024);
  218.                         }
  219.                         else
  220.                         {
  221.                             Reader.Read(buffer, 0, 1024);
  222.                         }
  223.                         string[] msgs = Encoding.ASCII.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
  224.                         foreach (string msg in msgs)
  225.                         {
  226.                             Parse(msg);
  227.  
  228.                         }
  229.                     }
  230.                     else
  231.                     {
  232.                         while (Messages.Count > 0)
  233.                         {
  234.                             Thread.Sleep(100);
  235.                             string s = Messages.Dequeue();
  236.                             Write(s);
  237.                             Console.WriteLine("REMOVED {0} from the queue", s);
  238.                         }
  239.                     }
  240.                 }
  241.             }
  242.             catch { }
  243.          
  244.         }
  245.         public void Privmsg(string target, string msg)
  246.         {
  247.             Enqueue("PRIVMSG {0} :{1}", target, msg);
  248.         }
  249.         public void Notice(string target, string msg)
  250.         {
  251.             Enqueue("NOTICE {0} :{1}", target, msg);
  252.         }
  253.         public void Enqueue(string data)
  254.         {
  255.             Messages.Enqueue(data);
  256.         }
  257.         public void Enqueue(string data, params object[] args)
  258.         {
  259.             data = String.Format(data, args);
  260.             Messages.Enqueue(data);
  261.         }
  262.         public void Write(string msg)
  263.         {
  264.             byte[] data = Encoding.ASCII.GetBytes(msg + "\r\n");
  265.             if (Writer != null)
  266.             {
  267.                 if (use_ssl)
  268.                 {
  269.                     SecureWriter.Write(data, 0, data.Length);
  270.                 }
  271.                 else
  272.                 {
  273.                     Writer.Write(data, 0, data.Length);
  274.                 }
  275.             }
  276.         }
  277.         public void Write(string msg, params object[] args)
  278.         {
  279.             msg = String.Format(msg, args);
  280.             Write(msg);
  281.         }
  282.  
  283.     }
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement