Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Net.Sockets;
- using System.Threading;
- using System.Collections;
- using System.Configuration;
- using System.Collections.Specialized;
- namespace Stream_Manager
- {
- delegate void Delegate(string text);
- public partial class IRCForm : Form
- {
- Form1 f1 = (Form1)Application.OpenForms["Form1"];
- System.Threading.Thread thread;
- delegate void TextBoxDelegate(string message);
- delegate void ClientListBoxDelegate(string clients);
- public ArrayList Clients = new ArrayList();
- bool needToClearClients = true; //if the CLients list has to be cleard
- public Thread IRCThread;
- //START: IRC Config
- //public string server = "mathiasbk.jtvirc.com";
- public string server = "192.168.1.132";
- public int port = 6667;
- public string nick = "fiskefaen";
- public string name = "fiskefaen";
- public string channel = "#bdoten";
- //END: IRC Config
- public IRCForm()
- {
- InitializeComponent();
- //MessageBox.Show(Form1.TextFileLocation);
- //server = Form1.IRChost;
- }
- private void IRCForm_Load(object sender, EventArgs e)
- {
- server = f1.IRChost;
- port = Convert.ToInt32(f1.IRCPORT);
- channel = f1.IRCChannel;
- nick = f1.IRCNick;
- name = f1.IRCName;
- IRCBot other = new IRCBot(this);
- IRCThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(other.DoIRCBot));
- IRCThread.Start();
- }
- public void updateTextBox(string msg)
- {
- if (this.IRCChatBox.InvokeRequired)
- {
- this.IRCChatBox.Invoke(new TextBoxDelegate(updateTextBox), new object[] { msg });
- }
- else
- {
- IRCChatBox.SelectionFont = new Font("Tahoma", 8, FontStyle.Regular);
- IRCChatBox.SelectionColor = Color.White;
- IRCChatBox.SelectedText += "[" + DateTime.Now.ToShortTimeString() + "] ";
- if (msg.StartsWith("<<"))
- {
- IRCChatBox.SelectionColor = Color.Red;
- }
- else
- {
- IRCChatBox.SelectionColor = Color.Green;
- }
- IRCChatBox.SelectedText += " " + msg;
- IRCChatBox.SelectedText += Environment.NewLine;
- //this.IRCChatBox.Text += "\r\n" + "["+DateTime.Now.ToShortTimeString()+"] " + msg;
- }
- }
- public void updateUserList(string users)
- {
- if (this.ClientsListBox.InvokeRequired)
- {
- this.ClientsListBox.Invoke(new ClientListBoxDelegate(updateUserList), new object[] { users });
- }
- else
- {
- if (needToClearClients) //checks if it has to clear the list before it adds the new Client names
- {
- Clients.Clear();
- ClientsListBox.Items.Clear();
- needToClearClients = false;
- }
- ClientsListBox.Items.Add(users);
- }
- }
- public void ClearClientList()
- {
- needToClearClients = true;
- }
- private void IRCForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- f1.IRCBotStartButton.Enabled = true;
- f1.CommandGrid.Enabled = true;
- IRCThread.Abort();
- }
- private void IRCChatBox_TextChanged(object sender, EventArgs e)
- {
- this.IRCChatBox.SelectionStart = this.IRCChatBox.Text.Length; //scrolls the textbox to the bottom
- this.IRCChatBox.ScrollToCaret();
- }
- }
- class IRCBot
- {
- TcpClient IRCConnection = null;
- NetworkStream ns = null;
- StreamReader sr = null;
- StreamWriter sw = null;
- public bool shouldRun = true;
- Form1 f1 = (Form1)Application.OpenForms["Form1"];
- IRCForm form1;
- public IRCBot(IRCForm _form1)
- {
- this.form1 = _form1;
- }
- public void StopIRCThread()
- {
- shouldRun = false;
- }
- public void DoIRCBot(Object obj)
- {
- var hovedformen = Form.ActiveForm as Form1;
- try
- {
- IRCConnection = new TcpClient(form1.server, form1.port);
- }
- catch
- {
- form1.updateTextBox("Connection Error! Could not connect to server.");
- //heisann.myMethod(hovedformen, "Connection Error");
- }
- try
- {
- //heisann.myMethod(hovedformen, "JAAAADDAAAA");
- ns = IRCConnection.GetStream();
- sr = new StreamReader(ns);
- sw = new StreamWriter(ns);
- //sendDAta("PASS", "etpassord");
- //sendDAta("USER", form1.nick);
- //sendDAta("NICK", form1.nick);
- sendDAta("PASS", "admin");
- sendDAta("USER", form1.nick + " +mode * :mathiasklow");
- sendDAta("NICK", form1.nick);
- IRCWork();
- }
- catch (Exception ex)
- {
- form1.updateTextBox("Communication error2: " + ex.Message);
- }
- finally
- {
- if (sr != null)
- sr.Close();
- if (sw != null)
- sw.Close();
- if (ns != null)
- ns.Close();
- if (IRCConnection != null)
- IRCConnection.Close();
- }
- }
- public void externalsendData()
- {
- }
- public void sendDAta(string cmd, string param)
- {
- var hovedformen = Form.ActiveForm as Form1;
- if (param == null)
- {
- sw.WriteLine(cmd);
- sw.Flush();
- form1.updateTextBox("<< " + cmd);
- }
- else
- {
- sw.WriteLine(cmd + " " + param);
- sw.Flush();
- if (cmd != "PASS")
- {
- form1.updateTextBox("<< " + cmd + " " + param);
- }
- }
- }
- public void StopTest()
- {
- sendDAta("QUIT", " Stopped by admin"); //if the command is quit, send the QUIT command to the server with a quit message
- //shouldRun = false; //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
- }
- public void IRCWork()
- {
- var hovedformen = Form.ActiveForm as Form1;
- ArrayList CommandsCol1 = f1.CommandsCol1;
- ArrayList CommandsCol2 = f1.CommandsCol2;
- ArrayList CommandsCol3 = f1.CommandsCol3;
- ArrayList ChannelAdmins = new ArrayList();
- ChannelAdmins.Add("mathiasbk");
- ChannelAdmins.Add("fiskefaen");
- String[] ex;
- string[] userdata;
- string data;
- while (shouldRun)
- {
- data = sr.ReadLine();
- form1.updateTextBox(">> " + data);
- char[] charSeparator = new char[] { ' ' };
- //MessageBox.Show(data);
- userdata = data.Split(':', '!', ' ');
- ex = data.Split(charSeparator, 5);
- if (ex.Length > 1 && ex[1] == "376") //Message to receive after the MOTD and To prevent "Index was outside of bounce array" error.
- {
- //MessageBox.Show(ex[1]);
- sendDAta("JOIN", form1.channel);
- }
- if (ex.Length > 1 && ex[1] == "352") // If it receives the end of the Users list
- {
- if (userdata[9] == "H@")
- {
- form1.updateUserList("@" + userdata[5]);
- }
- else
- {
- form1.updateUserList(userdata[5]);
- }
- }
- if (ex[1] == "315")
- {
- form1.ClearClientList();
- }
- if (ex[1] == "JOIN") //when someones joins the channel
- {
- sendDAta("WHO", form1.channel); //updates the list of connected users
- if(ChannelAdmins.Contains(userdata[1])) //if the connected user is admin/op
- {
- sendDAta("MODE", form1.channel + " +o " + userdata[1]); //set's the user to OP
- }
- }
- if (ex[0] == "PING") //if the bot receives a PING from the server
- {
- sendDAta("PONG", ex[1]);
- //START: Updates the list of users when it receives a PING from the server
- sendDAta("WHO", form1.channel);
- }
- if (ex.Length > 4) //is the command received long enough to be a bot command?
- {
- string command = ex[3];
- switch (command)
- {
- case ":!join":
- sendDAta("JOIN", ex[4]); //if the command is !join send the "JOIN" command to the server with the parameters set by the user
- break;
- case ":!say":
- sendDAta("PRIVMSG", ex[2] + " :test " + ex[4]); //if the command is !say, send a message to the chan (ex[2]) followed by the actual message (ex[4]).
- break;
- }
- }
- if (ex.Length > 3)
- {
- string command = ex[3];
- //Gets Commands from the GUI
- #region Check Commands array
- for (int x = 0; x <= CommandsCol1.Count-1; x++)
- {
- if (CommandsCol1[x].ToString().Contains(command))
- {
- string[] coomandcharacters = { "{song}", "{lastsong}", "{green}", "{white}"};
- string[] replacecommands = { f1.Artist.Text + " - " + f1.song.Text, f1.LastSong, "\u000309", "\u000300" };
- string receiver = "";
- //string temp = CommandsCol2[x].ToString().Replace("{song}", f1.Artist.Text + "-" + f1.song.Text).Replace("{lastsong}", f1.LastSong);
- string temp = CommandsCol2[x].ToString();
- for (int xr = 0; xr < coomandcharacters.Length; xr++)
- {
- temp = temp.Replace(coomandcharacters[xr], replacecommands[xr]);
- }
- //temp.Replace(BadCharacters[0], GoodCharacters[0]);
- MessageBox.Show(CommandsCol3[x].ToString());
- if (CommandsCol3[x].ToString() == "True")
- {
- receiver = userdata[1];
- }
- else { receiver = ex[2]; }
- sendDAta("PRIVMSG", receiver + " :" + temp);
- f1.AddToStatistics(1);
- }
- }
- #endregion
- //MessageBox.Show(CommandsCol1[0].ToString());
- switch (command)
- {
- case ":!part":
- sendDAta("PART", ex[2]);
- break;
- case ":!says":
- sendDAta("PRIVMSG", ex[2] + " " + ex[4]);
- break;
- case ":!quit":
- //sendDAta("QUIT", " Stopped by admin"); //if the command is quit, send the QUIT command to the server with a quit message
- //shouldRun = false; //turn shouldRun to false - the server will stop sending us data so trying to read it will not work and result in an error. This stops the loop from running and we will close off the connections properly
- StopTest();
- break;
- case ":!song":
- sendDAta("PRIVMSG", ex[2] + " : Current playing song: \u000309 \"" + f1.Artist.Text + "-" + f1.song.Text + "\". \u000300A Last song: \u000311\"" + f1.LastSong + "\".");
- f1.AddToStatistics(1);
- //MessageBox.Show("EX 0: " + ex[0] +". Ex 1: " + ex[1] + ". Ex 2: " + ex[2] + ". Ex 3: " + ex[3]);
- //MessageBox.Show("Ud 0: " + userdata[0] + ". UD 1: " + userdata[1] + ". UD 2: " + userdata[2] + ". UD 3: " + userdata[3]);
- break;
- case ":!kick":
- sendDAta("KICK", ex[2] + " " + userdata[1] + " : reet");
- break;
- case ":!brukere":
- sendDAta("NAMES", ex[2]);
- break;
- }
- }
- }
- //MessageBox.Show("bot stopped");
- form1.updateTextBox("\r\n Bot Quit / Crashed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement