Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.03 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using System.Net.Sockets;
  11. using System.Threading;
  12. using System.Collections;
  13. using System.Configuration;
  14. using System.Collections.Specialized;
  15.  
  16. namespace Stream_Manager
  17. {
  18.     delegate void Delegate(string text);
  19.     public partial class IRCForm : Form
  20.     {
  21.         Form1 f1 = (Form1)Application.OpenForms["Form1"];
  22.         System.Threading.Thread thread;
  23.         delegate void TextBoxDelegate(string message);
  24.         delegate void ClientListBoxDelegate(string clients);
  25.         public ArrayList Clients = new ArrayList();
  26.         bool needToClearClients = true; //if the CLients list has to be cleard
  27.         public Thread IRCThread;
  28.         //START: IRC Config
  29.         //public string server = "mathiasbk.jtvirc.com";
  30.         public string server = "192.168.1.132";
  31.         public int port = 6667;
  32.         public string nick = "fiskefaen";
  33.         public string name = "fiskefaen";
  34.         public string channel = "#bdoten";
  35.         //END: IRC Config
  36.         public IRCForm()
  37.         {
  38.             InitializeComponent();
  39.             //MessageBox.Show(Form1.TextFileLocation);
  40.             //server = Form1.IRChost;
  41.         }
  42.  
  43.         private void IRCForm_Load(object sender, EventArgs e)
  44.         {
  45.             server = f1.IRChost;
  46.             port = Convert.ToInt32(f1.IRCPORT);
  47.             channel = f1.IRCChannel;
  48.             nick = f1.IRCNick;
  49.             name = f1.IRCName;
  50.  
  51.             IRCBot other = new IRCBot(this);
  52.             IRCThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(other.DoIRCBot));
  53.             IRCThread.Start();
  54.        }
  55.  
  56.         public void updateTextBox(string msg)
  57.         {
  58.             if (this.IRCChatBox.InvokeRequired)
  59.             {
  60.                 this.IRCChatBox.Invoke(new TextBoxDelegate(updateTextBox), new object[] { msg });
  61.             }
  62.             else
  63.             {
  64.                 IRCChatBox.SelectionFont = new Font("Tahoma", 8, FontStyle.Regular);
  65.                 IRCChatBox.SelectionColor = Color.White;
  66.                 IRCChatBox.SelectedText += "[" + DateTime.Now.ToShortTimeString() + "] ";
  67.                 if (msg.StartsWith("<<"))
  68.                 {
  69.                     IRCChatBox.SelectionColor = Color.Red;
  70.                 }
  71.                 else
  72.                 {
  73.                     IRCChatBox.SelectionColor = Color.Green;
  74.                 }
  75.                 IRCChatBox.SelectedText += " " + msg;
  76.                 IRCChatBox.SelectedText += Environment.NewLine;
  77.                 //this.IRCChatBox.Text += "\r\n" + "["+DateTime.Now.ToShortTimeString()+"] "  + msg;
  78.             }  
  79.         }
  80.  
  81.         public void updateUserList(string users)
  82.         {
  83.             if (this.ClientsListBox.InvokeRequired)
  84.             {
  85.                 this.ClientsListBox.Invoke(new ClientListBoxDelegate(updateUserList), new object[] { users });
  86.             }
  87.             else
  88.             {
  89.                 if (needToClearClients) //checks if it has to clear the list before it adds the new Client names
  90.                  {
  91.                     Clients.Clear();
  92.                     ClientsListBox.Items.Clear();
  93.                     needToClearClients = false;
  94.                 }
  95.                 ClientsListBox.Items.Add(users);                
  96.             }
  97.         }
  98.         public void ClearClientList()
  99.         {
  100.             needToClearClients = true;
  101.         }
  102.  
  103.         private void IRCForm_FormClosing(object sender, FormClosingEventArgs e)
  104.         {
  105.            
  106.             f1.IRCBotStartButton.Enabled = true;
  107.             f1.CommandGrid.Enabled = true;
  108.             IRCThread.Abort();
  109.         }
  110.  
  111.         private void IRCChatBox_TextChanged(object sender, EventArgs e)
  112.         {
  113.             this.IRCChatBox.SelectionStart = this.IRCChatBox.Text.Length; //scrolls the textbox to the bottom
  114.             this.IRCChatBox.ScrollToCaret();
  115.         }
  116.  
  117.     }
  118.     class IRCBot
  119.     {
  120.         TcpClient IRCConnection = null;
  121.         NetworkStream ns = null;
  122.         StreamReader sr = null;
  123.         StreamWriter sw = null;
  124.         public bool shouldRun = true;
  125.         Form1 f1 = (Form1)Application.OpenForms["Form1"];
  126.         IRCForm form1;
  127.  
  128.         public IRCBot(IRCForm _form1)
  129.         {
  130.             this.form1 = _form1;
  131.         }
  132.  
  133.         public void StopIRCThread()
  134.         {
  135.             shouldRun = false;
  136.         }
  137.  
  138.         public void DoIRCBot(Object obj)
  139.         {
  140.             var hovedformen = Form.ActiveForm as Form1;
  141.             try
  142.             {
  143.                 IRCConnection = new TcpClient(form1.server, form1.port);
  144.             }
  145.             catch
  146.             {
  147.                 form1.updateTextBox("Connection Error! Could not connect to server.");
  148.                 //heisann.myMethod(hovedformen, "Connection Error");
  149.             }
  150.  
  151.             try
  152.             {
  153.  
  154.                 //heisann.myMethod(hovedformen, "JAAAADDAAAA");
  155.                 ns = IRCConnection.GetStream();
  156.                 sr = new StreamReader(ns);
  157.                 sw = new StreamWriter(ns);
  158.                 //sendDAta("PASS", "etpassord");
  159.                 //sendDAta("USER", form1.nick);
  160.                 //sendDAta("NICK", form1.nick);
  161.                 sendDAta("PASS", "admin");
  162.                 sendDAta("USER", form1.nick + " +mode * :mathiasklow");
  163.                 sendDAta("NICK", form1.nick);
  164.  
  165.                 IRCWork();
  166.             }
  167.             catch (Exception ex)
  168.             {
  169.                 form1.updateTextBox("Communication error2: " + ex.Message);
  170.             }
  171.             finally
  172.             {
  173.                 if (sr != null)
  174.                     sr.Close();
  175.                 if (sw != null)
  176.                     sw.Close();
  177.                 if (ns != null)
  178.                     ns.Close();
  179.                 if (IRCConnection != null)
  180.                     IRCConnection.Close();
  181.             }
  182.         }
  183.  
  184.         public void externalsendData()
  185.         {
  186.            
  187.         }
  188.  
  189.         public void sendDAta(string cmd, string param)
  190.         {
  191.             var hovedformen = Form.ActiveForm as Form1;
  192.             if (param == null)
  193.             {
  194.                 sw.WriteLine(cmd);
  195.                 sw.Flush();
  196.                 form1.updateTextBox("<< " + cmd);
  197.             }
  198.             else
  199.             {
  200.                 sw.WriteLine(cmd + " " + param);
  201.                 sw.Flush();
  202.                 if (cmd != "PASS")
  203.                 {
  204.  
  205.                     form1.updateTextBox("<< " + cmd + " " + param);
  206.                 }
  207.             }
  208.         }
  209.         public void StopTest()
  210.         {
  211.             sendDAta("QUIT", " Stopped by admin"); //if the command is quit, send the QUIT command to the server with a quit message
  212.             //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
  213.         }
  214.         public void IRCWork()
  215.         {
  216.             var hovedformen = Form.ActiveForm as Form1;
  217.             ArrayList CommandsCol1 = f1.CommandsCol1;
  218.             ArrayList CommandsCol2 = f1.CommandsCol2;
  219.             ArrayList CommandsCol3 = f1.CommandsCol3;
  220.             ArrayList ChannelAdmins = new ArrayList();
  221.            
  222.             ChannelAdmins.Add("mathiasbk");
  223.             ChannelAdmins.Add("fiskefaen");
  224.            
  225.             String[] ex;
  226.             string[] userdata;
  227.             string data;
  228.            
  229.            
  230.            
  231.             while (shouldRun)
  232.             {
  233.                 data = sr.ReadLine();
  234.                 form1.updateTextBox(">> " + data);
  235.  
  236.                 char[] charSeparator = new char[] { ' ' };
  237.                 //MessageBox.Show(data);
  238.                 userdata = data.Split(':', '!', ' ');
  239.                 ex = data.Split(charSeparator, 5);
  240.                 if (ex.Length > 1 && ex[1] == "376") //Message to receive after the MOTD and To prevent "Index was outside of bounce array" error.
  241.                 {
  242.                     //MessageBox.Show(ex[1]);
  243.                     sendDAta("JOIN", form1.channel);
  244.                 }
  245.                 if (ex.Length > 1 && ex[1] == "352") // If it receives the end of the Users list
  246.                 {
  247.  
  248.                     if (userdata[9] == "H@")
  249.                     {
  250.                         form1.updateUserList("@" + userdata[5]);
  251.                     }
  252.                     else
  253.                     {
  254.                         form1.updateUserList(userdata[5]);
  255.                     }
  256.                 }
  257.                 if (ex[1] == "315")
  258.                 {
  259.                     form1.ClearClientList();
  260.                 }
  261.                 if (ex[1] == "JOIN") //when someones joins the channel
  262.                 {
  263.                     sendDAta("WHO", form1.channel); //updates the list of connected users
  264.                     if(ChannelAdmins.Contains(userdata[1])) //if the connected user is admin/op
  265.                     {
  266.                         sendDAta("MODE", form1.channel + " +o " + userdata[1]); //set's the user to OP
  267.                     }
  268.                 }
  269.                
  270.                 if (ex[0] == "PING") //if the bot receives a PING from the server
  271.                 {
  272.                     sendDAta("PONG", ex[1]);
  273.                     //START: Updates the list of users when it receives a PING from the server
  274.                     sendDAta("WHO", form1.channel);
  275.                 }
  276.  
  277.                 if (ex.Length > 4) //is the command received long enough to be a bot command?
  278.                 {
  279.                     string command = ex[3];
  280.                    
  281.                     switch (command)
  282.                     {
  283.                         case ":!join":
  284.                             sendDAta("JOIN", ex[4]); //if the command is !join send the "JOIN" command to the server with the parameters set by the user
  285.                             break;
  286.                         case ":!say":
  287.                             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]).
  288.                             break;
  289.                        
  290.                     }
  291.                 }
  292.                 if (ex.Length > 3)
  293.                 {
  294.                     string command = ex[3];
  295.                     //Gets Commands from the GUI
  296.                     #region Check Commands array
  297.                     for (int x = 0; x <= CommandsCol1.Count-1; x++)
  298.                     {
  299.                         if (CommandsCol1[x].ToString().Contains(command))
  300.                         {
  301.                             string[] coomandcharacters = { "{song}", "{lastsong}", "{green}", "{white}"};
  302.                             string[] replacecommands = { f1.Artist.Text + " - " + f1.song.Text, f1.LastSong, "\u000309", "\u000300" };
  303.                             string receiver = "";
  304.  
  305.                             //string temp = CommandsCol2[x].ToString().Replace("{song}", f1.Artist.Text + "-" + f1.song.Text).Replace("{lastsong}", f1.LastSong);
  306.                             string temp = CommandsCol2[x].ToString();
  307.                             for (int xr = 0; xr < coomandcharacters.Length; xr++)
  308.                             {
  309.                                 temp = temp.Replace(coomandcharacters[xr], replacecommands[xr]);
  310.                             }
  311.                             //temp.Replace(BadCharacters[0], GoodCharacters[0]);
  312.                             MessageBox.Show(CommandsCol3[x].ToString());
  313.                             if (CommandsCol3[x].ToString() == "True")
  314.                             {
  315.                                 receiver = userdata[1];
  316.                             }
  317.                             else { receiver = ex[2]; }
  318.                             sendDAta("PRIVMSG", receiver + " :" + temp);
  319.                             f1.AddToStatistics(1);
  320.                         }
  321.                     }
  322.                     #endregion
  323.  
  324.                     //MessageBox.Show(CommandsCol1[0].ToString());
  325.                     switch (command)
  326.                     {
  327.                         case ":!part":
  328.                             sendDAta("PART", ex[2]);
  329.                             break;
  330.                         case ":!says":
  331.                             sendDAta("PRIVMSG", ex[2] + " " + ex[4]);
  332.                             break;
  333.                         case ":!quit":
  334.                             //sendDAta("QUIT", " Stopped by admin"); //if the command is quit, send the QUIT command to the server with a quit message
  335.                             //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
  336.                             StopTest();
  337.                             break;
  338.  
  339.                         case ":!song":
  340.                             sendDAta("PRIVMSG", ex[2] + " : Current playing song: \u000309 \"" + f1.Artist.Text + "-" + f1.song.Text + "\". \u000300A Last song: \u000311\"" + f1.LastSong + "\".");
  341.                             f1.AddToStatistics(1);
  342.                             //MessageBox.Show("EX 0: " + ex[0] +". Ex 1: " + ex[1] + ". Ex 2: " + ex[2] + ". Ex 3: " + ex[3]);
  343.                             //MessageBox.Show("Ud 0: " + userdata[0] +  ". UD 1: " + userdata[1] + ". UD 2: " + userdata[2] + ". UD 3: " + userdata[3]);
  344.                             break;
  345.  
  346.                         case ":!kick":
  347.                             sendDAta("KICK", ex[2] + " " + userdata[1] + " : reet");
  348.                             break;
  349.  
  350.                         case ":!brukere":
  351.                             sendDAta("NAMES", ex[2]);
  352.                             break;
  353.  
  354.  
  355.                     }
  356.                 }
  357.             }
  358.             //MessageBox.Show("bot stopped");
  359.             form1.updateTextBox("\r\n Bot Quit / Crashed");
  360.         }
  361.     }
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement