Advertisement
Guest User

c#

a guest
Oct 31st, 2014
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.06 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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Threading;
  11. using System.Collections;
  12. using System.Collections.Generic;
  13. using System.Diagnostics;
  14. using System.Text.RegularExpressions;
  15.  
  16. namespace testCsharp
  17. {
  18.     public partial class Form1 : Form
  19.     {
  20.  
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.  
  26.         int port;
  27.         string buf;
  28.         string nick;
  29.         string oauth;
  30.         string server;
  31.         string chan;
  32.         System.Net.Sockets.TcpClient sock = new System.Net.Sockets.TcpClient();
  33.         System.IO.TextReader input;
  34.         System.IO.TextWriter output;
  35.  
  36.         Dictionary<string, string> colorDictionary = new Dictionary<string, string>();
  37.         public static string GetBetween(string input, string before, string after)
  38.         {
  39.             return Regex.Split(Regex.Split(input, before)[1], after)[0];
  40.         }
  41.  
  42.         private void Button1_Click(object sender, EventArgs e)
  43.         {
  44.             if (TextBox2.Text.Length < 1 | TextBox3.Text.Length < 1)
  45.             {
  46.                 RichTextBox1.Clear();
  47.                 RichTextBox1.AppendText("Twitch name or token cannot be empty." + Environment.NewLine);
  48.             }
  49.             else
  50.             {
  51.                 nick = TextBox2.Text;
  52.                 oauth = TextBox3.Text;
  53.                 Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
  54.                 thread.Start();
  55.  
  56.                 TextBox2.Clear();
  57.                 TextBox3.Clear();
  58.             }
  59.         }
  60.  
  61.         public void WorkThreadFunction()
  62.         {
  63.             try
  64.             {
  65.                 //Get nick, owner, server, port, and channel from user
  66.  
  67.                 //Connect to irc server and get input and output text streams from TcpClient.
  68.  
  69.                 //Starting USER and NICK login commands
  70.  
  71.                 //Process each line received from irc server
  72.  
  73.                 //Display received irc message
  74.                 //Console.WriteLine(buf);
  75.  
  76.                 //Send pong reply to any ping messages
  77.  
  78.                 // IRC commands come in one of these formats:
  79.                 //                     * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
  80.                 //                     * :SERVER COMAND ARGS ... :DATA\r\n
  81.                 //                    
  82.  
  83.  
  84.                 //After server sends 001 command, we can set mode to bot and join a channel
  85.                 server = "irc.twitch.tv";
  86.                 port = 6667;
  87.                 chan = "aphromoo";
  88.                 sock.Connect(server, port);
  89.                 if (!sock.Connected)
  90.                 {
  91.  
  92.                     this.Invoke(new MethodInvoker(() => { RichTextBox1.AppendText("Failed to connect!" + Environment.NewLine); }));
  93.                     return;
  94.                 }
  95.                 input = new System.IO.StreamReader(sock.GetStream());
  96.                 output = new System.IO.StreamWriter(sock.GetStream());
  97.                 output.Write("USER " + nick + " 0 * " + '\r' + '\n' + "PASS " + oauth + '\r' + '\n' + "NICK " + nick + '\r' + '\n');
  98.                 output.WriteLine("TWITCHCLIENT 1");
  99.                 output.Flush();
  100.                 buf = input.ReadLine();
  101.                 while (true)
  102.                 {
  103.                     try
  104.                     {
  105.                         if (!buf.Contains("#aphromoo :"))
  106.                         {
  107.                             this.Invoke(new MethodInvoker(() =>
  108.                             {
  109.                                 RichTextBox1.AppendText(buf + Environment.NewLine);
  110.  
  111.                                 Dictionary<string, string> colorDictionary = new Dictionary<string, string>();
  112.                                 if (buf.ToString().Contains(":USERCOLOR"))
  113.                                 {
  114.                                     string name = GetBetween(buf.ToString(), "USERCOLOR", "#");
  115.                                     string color = buf.ToString().Split('#')[1];
  116.                                     if (colorDictionary.ContainsKey(name) == false)
  117.                                     {
  118.                                         colorDictionary.Add(name, color);
  119.                                     }
  120.                                     else if (colorDictionary[name] != color)
  121.                                     {
  122.                                         colorDictionary[name] = color;
  123.                                     }
  124.  
  125.                                     foreach (object key_loopVariable in colorDictionary.Keys)
  126.                                     {
  127.                                         key = key_loopVariable;
  128.                                         ListBox1.Items.Add((key + ":") + colorDictionary[key]);
  129.                                     }
  130.                                 }
  131.  
  132.                             }));
  133.                         }
  134.                         else
  135.                         {
  136.                             try
  137.                             {
  138.                                 this.Invoke(new MethodInvoker(() =>
  139.                                 {
  140.                                     if (buf.ToString().Contains("@") && buf.ToString().Contains(".tmi.twitch.tv"))
  141.                                     {
  142.                                         RichTextBox2.AppendText(GetBetween(buf, "@", ".tmi") + ": " + buf.ToString().Split(':')[2] + Environment.NewLine);
  143.                                     }
  144.                                     else
  145.                                     {
  146.                                         RichTextBox1.AppendText(buf + Environment.NewLine);
  147.                                     }
  148.  
  149.  
  150.                                 }));
  151.                             }
  152.                             catch (Exception generatedExceptionName)
  153.                             {
  154.                             }
  155.                         }
  156.                         if (buf.StartsWith("PING "))
  157.                         {
  158.                             output.Write(buf.Replace("PING", "PONG") + '\r' + '\n');
  159.                             output.Flush();
  160.                         }
  161.                         if (buf[0] != ':')
  162.                         {
  163.                             continue;
  164.                         }
  165.                         if (buf.Split(' ')[1] == "001")
  166.                         {
  167.                             output.Write("MODE " + nick + " +B" + '\r' + '\n' + "JOIN #" + chan + '\r' + '\n');
  168.                             output.Flush();
  169.                         }
  170.                         buf = input.ReadLine();
  171.                     }
  172.                     catch
  173.                     {
  174.                         RichTextBox1.Clear();
  175.                         RichTextBox1.AppendText("Could not connect to chat." + Environment.NewLine);
  176.                         break; // TODO: might not be correct. Was : Exit While
  177.                     }
  178.                 }
  179.             }
  180.             catch (Exception ex)
  181.             {
  182.                 // log errors
  183.             }
  184.            }
  185.  
  186.         public static void AppendText(RichTextBox box, string text, Color color)
  187.         {
  188.             box.SelectionStart = box.TextLength;
  189.             box.SelectionLength = 0;
  190.  
  191.             box.SelectionColor = color;
  192.             box.AppendText(text);
  193.             box.SelectionColor = box.ForeColor;
  194.         }
  195.  
  196.         private void TextBox1_KeyDown(object sender, KeyEventArgs e)
  197.         {
  198.             if (sock.Connected == true)
  199.             {
  200.                 if (e.KeyCode == Keys.Enter)
  201.                 {
  202.                     output.WriteLine("PRIVMSG #" + chan + " :" + TextBox1.Text);
  203.                     output.Flush();
  204.                     TextBox1.Clear();
  205.                 }
  206.             }
  207.             else
  208.             {
  209.                 if (e.KeyCode == Keys.Enter)
  210.                 {
  211.                     RichTextBox1.Clear();
  212.                     RichTextBox1.AppendText("Not connected to chat." + '\n');
  213.                 }
  214.             }
  215.         }
  216.  
  217.         }
  218.  
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement