Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.Net.Security;
- using System.Net.Sockets;
- using System.Threading;
- namespace HardChatter2
- {
- class IrcBot
- {
- public Queue<string> Messages = new Queue<string>();
- private TcpClient Bot = null;
- private NetworkStream Writer = null;
- private NetworkStream Reader = null;
- private SslStream SecureWriter;
- private SslStream SecureReader;
- public string hostname = "";
- public int port = 6667;
- public bool use_ssl = false;
- private bool is_oper = false;
- public string user_name = "xyz";
- public string real_name = "xyz";
- public string nick = "RaidBot";
- public string oper_user = "";
- public string oper_pass = "";
- public string nick_pass = "";
- public bool debug = true;
- string target = "";
- public void Init()
- {
- Bot = new TcpClient();
- Bot.Connect(hostname, port);
- if (Bot != null)
- {
- if (use_ssl)
- {
- SecureWriter = new SslStream(Bot.GetStream());
- SecureReader = new SslStream(Bot.GetStream());
- SecureReader.AuthenticateAsClient(hostname);
- SecureWriter.AuthenticateAsClient(hostname);
- }
- else
- {
- Writer = Bot.GetStream();
- Reader = Bot.GetStream();
- }
- }
- else
- {
- // add error handling...
- }
- Thread T = new Thread(new ThreadStart(Process));
- T.Start();
- }
- public void Join(string channel)
- {
- Enqueue("JOIN {0}", channel);
- }
- public void Join(string channel, string key)
- {
- Enqueue("JOIN {0} {1}", channel, key);
- }
- public void JoinChannels(params string[] channels)
- {
- foreach (string s in channels)
- {
- if (s.Contains(":"))
- {
- string[] chan = s.Split(new char[] { ':' });
- Join(chan[0], chan[1]);
- }
- else
- {
- Join(s);
- }
- }
- }
- public void Identify()
- {
- Enqueue("NICK {0}", nick);
- Enqueue("USER {0} {0} {0}: {0}", user_name, real_name);
- if (is_oper)
- {
- Enqueue("OPER {0} {1}", oper_user, oper_pass);
- }
- Enqueue("PRIVMSG NickServ IDENTIFY ", nick_pass);
- }
- public void EvalData(string[] data)
- {
- try
- {
- string nick = data[0].Split(new char[] { '!' })[0];
- string command = data[1];
- string chan = data[2];
- string action = data[3];
- if (action[0] == '!')
- {
- string cmd = action.Substring(1).ToLower();
- if (cmd == "join")
- {
- if (data.Length > 4)
- {
- Join(data[4], data[5]);
- }
- else
- {
- Join(data[4]);
- }
- }
- if (cmd == "part")
- {
- Enqueue("PART {0} {1}", chan, Combine(data, 4, data.Length));
- }
- if (cmd == "id")
- {
- Enqueue("PRIVMSG NickServ IDENTIFY {0}", nick_pass);
- }
- if (cmd == "target.set")
- {
- Enqueue("PRIVMSG {0} Tagret set to {1}", chan, Combine(data, 4, data.Length));
- target = Combine(data, 4, data.Length);
- }
- if (cmd == "wiki")
- {
- }
- if (cmd == "target")
- {
- Enqueue("PRIVMSG {0} Cureent Target: {1}", chan, target);
- }
- if (cmd == "raw")
- {
- Enqueue(Combine(data, 4, data.Length));
- }
- }
- }
- catch (Exception E)
- {
- //Console.WriteLine("ERROR -- {0} --", E);
- }
- }
- public string Combine(string[] array, int min, int max)
- {
- string s = "";
- for (int i = min; i < max; i++)
- {
- s += array[i] + " ";
- }
- return s;
- }
- public void Parse(string ircdata)
- {
- ircdata = ircdata.Replace(":", "");
- ircdata = ircdata.Trim(new char[] { '\r', '\n', '\0', ':' });
- string[] msgs = ircdata.Split(new char[] { ' ' });
- if (msgs.Length > 1 && msgs[1] == "KICK" && msgs[3] == "RaidBot")
- {
- string nick = msgs[0].Split(new char[] { '!' })[0];
- string chan = msgs[2];
- Join(chan);
- Enqueue("PRIVMSG {0} {1} kick me again and i will oven you faggot!", chan, nick);
- }
- if (msgs.Length > 2 && msgs[1] == "INVITE" && msgs[2] == "RaidBot")
- {
- string nick = msgs[0].Split(new char[] { '!' })[0];
- string chan = msgs[3];
- Join(chan);
- Enqueue("PRIVMSG {0} Hello, RaidBot here i was invited by {1} <3", chan, nick);
- }
- for (int i = 0; i < msgs.Length; i++)
- {
- if (debug)
- {
- Console.WriteLine("msgs[{0}] = {1}", i, msgs[i]);
- }
- if (msgs[i] == "PING")
- {
- Enqueue("PONG {0}", msgs[i+1]);
- }
- else if (msgs[i].ToUpper() == "\x01VERSION\x01")
- {
- Write(String.Format("NOTICE {0} {1}", nick, "Insurgency RaidBot written by xyz"));
- }
- }
- EvalData(msgs);
- }
- public void Process()
- {
- try
- {
- Thread.Sleep(100);
- bool reading = false;
- while (true)
- {
- if (Messages.Count > 0)
- { Console.WriteLine("Queue.Length = {0}", Messages.Count); }
- reading = !reading;
- byte[] buffer = new byte[1024];
- if (reading)
- {
- if (use_ssl)
- {
- if (SecureReader == null)
- {
- continue;
- }
- SecureReader.Read(buffer, 0, 1024);
- }
- else
- {
- Reader.Read(buffer, 0, 1024);
- }
- string[] msgs = Encoding.ASCII.GetString(buffer).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
- foreach (string msg in msgs)
- {
- Parse(msg);
- }
- }
- else
- {
- while (Messages.Count > 0)
- {
- Thread.Sleep(100);
- string s = Messages.Dequeue();
- Write(s);
- Console.WriteLine("REMOVED {0} from the queue", s);
- }
- }
- }
- }
- catch { }
- }
- public void Privmsg(string target, string msg)
- {
- Enqueue("PRIVMSG {0} :{1}", target, msg);
- }
- public void Notice(string target, string msg)
- {
- Enqueue("NOTICE {0} :{1}", target, msg);
- }
- public void Enqueue(string data)
- {
- Messages.Enqueue(data);
- }
- public void Enqueue(string data, params object[] args)
- {
- data = String.Format(data, args);
- Messages.Enqueue(data);
- }
- public void Write(string msg)
- {
- byte[] data = Encoding.ASCII.GetBytes(msg + "\r\n");
- if (Writer != null)
- {
- if (use_ssl)
- {
- SecureWriter.Write(data, 0, data.Length);
- }
- else
- {
- Writer.Write(data, 0, data.Length);
- }
- }
- }
- public void Write(string msg, params object[] args)
- {
- msg = String.Format(msg, args);
- Write(msg);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement