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.Threading.Tasks;
- using System.Windows.Forms;
- using System.Threading;
- using System.Collections;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Text.RegularExpressions;
- namespace testCsharp
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- int port;
- string buf;
- string nick;
- string oauth;
- string server;
- string chan;
- System.Net.Sockets.TcpClient sock = new System.Net.Sockets.TcpClient();
- System.IO.TextReader input;
- System.IO.TextWriter output;
- Dictionary<string, string> colorDictionary = new Dictionary<string, string>();
- public static string GetBetween(string input, string before, string after)
- {
- return Regex.Split(Regex.Split(input, before)[1], after)[0];
- }
- private void Button1_Click(object sender, EventArgs e)
- {
- if (TextBox2.Text.Length < 1 | TextBox3.Text.Length < 1)
- {
- RichTextBox1.Clear();
- RichTextBox1.AppendText("Twitch name or token cannot be empty." + Environment.NewLine);
- }
- else
- {
- nick = TextBox2.Text;
- oauth = TextBox3.Text;
- Thread thread = new Thread(new ThreadStart(WorkThreadFunction));
- thread.Start();
- TextBox2.Clear();
- TextBox3.Clear();
- }
- }
- public void WorkThreadFunction()
- {
- try
- {
- //Get nick, owner, server, port, and channel from user
- //Connect to irc server and get input and output text streams from TcpClient.
- //Starting USER and NICK login commands
- //Process each line received from irc server
- //Display received irc message
- //Console.WriteLine(buf);
- //Send pong reply to any ping messages
- // IRC commands come in one of these formats:
- // * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
- // * :SERVER COMAND ARGS ... :DATA\r\n
- //
- //After server sends 001 command, we can set mode to bot and join a channel
- server = "irc.twitch.tv";
- port = 6667;
- chan = "aphromoo";
- sock.Connect(server, port);
- if (!sock.Connected)
- {
- this.Invoke(new MethodInvoker(() => { RichTextBox1.AppendText("Failed to connect!" + Environment.NewLine); }));
- return;
- }
- input = new System.IO.StreamReader(sock.GetStream());
- output = new System.IO.StreamWriter(sock.GetStream());
- output.Write("USER " + nick + " 0 * " + '\r' + '\n' + "PASS " + oauth + '\r' + '\n' + "NICK " + nick + '\r' + '\n');
- output.WriteLine("TWITCHCLIENT 1");
- output.Flush();
- buf = input.ReadLine();
- while (true)
- {
- try
- {
- if (!buf.Contains("#aphromoo :"))
- {
- this.Invoke(new MethodInvoker(() =>
- {
- RichTextBox1.AppendText(buf + Environment.NewLine);
- Dictionary<string, string> colorDictionary = new Dictionary<string, string>();
- if (buf.ToString().Contains(":USERCOLOR"))
- {
- string name = GetBetween(buf.ToString(), "USERCOLOR", "#");
- string color = buf.ToString().Split('#')[1];
- if (colorDictionary.ContainsKey(name) == false)
- {
- colorDictionary.Add(name, color);
- }
- else if (colorDictionary[name] != color)
- {
- colorDictionary[name] = color;
- }
- foreach (object key_loopVariable in colorDictionary.Keys)
- {
- key = key_loopVariable;
- ListBox1.Items.Add((key + ":") + colorDictionary[key]);
- }
- }
- }));
- }
- else
- {
- try
- {
- this.Invoke(new MethodInvoker(() =>
- {
- if (buf.ToString().Contains("@") && buf.ToString().Contains(".tmi.twitch.tv"))
- {
- RichTextBox2.AppendText(GetBetween(buf, "@", ".tmi") + ": " + buf.ToString().Split(':')[2] + Environment.NewLine);
- }
- else
- {
- RichTextBox1.AppendText(buf + Environment.NewLine);
- }
- }));
- }
- catch (Exception generatedExceptionName)
- {
- }
- }
- if (buf.StartsWith("PING "))
- {
- output.Write(buf.Replace("PING", "PONG") + '\r' + '\n');
- output.Flush();
- }
- if (buf[0] != ':')
- {
- continue;
- }
- if (buf.Split(' ')[1] == "001")
- {
- output.Write("MODE " + nick + " +B" + '\r' + '\n' + "JOIN #" + chan + '\r' + '\n');
- output.Flush();
- }
- buf = input.ReadLine();
- }
- catch
- {
- RichTextBox1.Clear();
- RichTextBox1.AppendText("Could not connect to chat." + Environment.NewLine);
- break; // TODO: might not be correct. Was : Exit While
- }
- }
- }
- catch (Exception ex)
- {
- // log errors
- }
- }
- public static void AppendText(RichTextBox box, string text, Color color)
- {
- box.SelectionStart = box.TextLength;
- box.SelectionLength = 0;
- box.SelectionColor = color;
- box.AppendText(text);
- box.SelectionColor = box.ForeColor;
- }
- private void TextBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (sock.Connected == true)
- {
- if (e.KeyCode == Keys.Enter)
- {
- output.WriteLine("PRIVMSG #" + chan + " :" + TextBox1.Text);
- output.Flush();
- TextBox1.Clear();
- }
- }
- else
- {
- if (e.KeyCode == Keys.Enter)
- {
- RichTextBox1.Clear();
- RichTextBox1.AppendText("Not connected to chat." + '\n');
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement