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 = "RaidBot2";
- public string oper_user = "";
- public string oper_pass = "";
- public string nick_pass = "";
- 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("USER {0} {0} {0} :{1}", user_name, real_name);
- Enqueue("NICK {0}", nick);
- if (is_oper)
- {
- Enqueue("OPER {0} {1}", oper_user, oper_pass);
- }
- Enqueue("PRIVMSG NickServ IDENTIFY :", nick_pass);
- }
- public void EvalData(string data)
- {
- }
- public void Parse(string ircdata)
- {
- ircdata = ircdata.Replace(":", "");
- ircdata = ircdata.Trim(new char[] { '\r', '\n', '\0', ':' });
- string[] msgs = ircdata.Split(new char[] { ' ' });
- for (int i = 0; i < msgs.Length; i++)
- {
- if (debug)
- {
- Console.WriteLine("msgs[{0}] = {1}", i, msgs[i]);
- }
- }
- EvalData(ircdata);
- }
- public void Process()
- {
- Thread.Sleep(100);
- bool reading = true;
- 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);
- }
- }
- }
- }
- 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