Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Text.RegularExpressions
- Imports System.Threading
- Imports System.Drawing
- Public Class Form1
- Private trd As Thread
- Dim port As Integer
- Dim buf As String, nick As String, oauth As String, server As String, chan As String
- Dim sock As New System.Net.Sockets.TcpClient()
- Dim input As System.IO.TextReader
- Dim output As System.IO.TextWriter
- Dim colorDictionary As New Dictionary(Of String, String)()
- Public Shared Function GetBetween(input As String, before As String, after As String) As String
- Return Regex.Split(Regex.Split(input, before)(1), after)(0)
- End Function
- Private Sub ThreadTask()
- '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 = "scarra"
- sock.Connect(server, port)
- If Not sock.Connected Then
- Me.Invoke(New MethodInvoker(Function()
- RichTextBox1.AppendText("Failed to connect!" & Environment.NewLine)
- End Function))
- Return
- End If
- input = New System.IO.StreamReader(sock.GetStream())
- output = New System.IO.StreamWriter(sock.GetStream())
- output.Write("USER " & nick & " 0 * " & vbCr & vbLf & "PASS " & oauth & vbCr & vbLf & "NICK " & nick & vbCr & vbLf)
- output.WriteLine("TWITCHCLIENT 1")
- output.Flush()
- buf = input.ReadLine()
- While True
- Try
- If Not buf.Contains("#scarra :") Then
- Me.Invoke(New MethodInvoker(Function()
- RichTextBox1.AppendText(buf & Environment.NewLine)
- If buf.ToString().Contains(":USERCOLOR") Then
- Dim name As String = GetBetween(buf.ToString(), "USERCOLOR", "#")
- Dim color As String = buf.ToString().Split("#")(1)
- If colorDictionary.ContainsKey(name) = False Then
- colorDictionary.Add(name, color)
- ElseIf colorDictionary(name) <> color Then
- colorDictionary(name) = color
- End If
- For Each key In colorDictionary.Keys
- ListBox1.Items.Add((key & ":") + colorDictionary(key))
- Next
- End If
- End Function))
- Else
- Try
- Me.Invoke(New MethodInvoker(Function()
- If buf.ToString().Contains("@") AndAlso buf.ToString().Contains(".tmi.twitch.tv") Then
- RichTextBox2.AppendText(GetBetween(buf, "@", ".tmi") & ": " & buf.ToString().Split(":")(2) & Environment.NewLine)
- Else
- RichTextBox1.AppendText(buf & Environment.NewLine)
- End If
- End Function))
- Catch generatedExceptionName As Exception
- End Try
- End If
- If buf.StartsWith("PING ") Then
- output.Write(buf.Replace("PING", "PONG") & vbCr & vbLf)
- output.Flush()
- End If
- If buf(0) <> ":"c Then
- Continue While
- End If
- If buf.Split(" "c)(1) = "001" Then
- output.Write("MODE " & nick & " +B" & vbCr & vbLf & "JOIN #" & chan & vbCr & vbLf)
- output.Flush()
- End If
- buf = input.ReadLine()
- Catch
- RichTextBox1.Clear()
- RichTextBox1.AppendText("Could not connect to chat." & Environment.NewLine)
- Exit While
- End Try
- End While
- End Sub
- Public Shared Sub AppendText(box As RichTextBox, text As String, color As Color)
- box.SelectionStart = box.TextLength
- box.SelectionLength = 0
- box.SelectionColor = color
- box.AppendText(text)
- box.SelectionColor = box.ForeColor
- End Sub
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- If TextBox2.Text.Length < 1 Or TextBox3.Text.Length < 1 Then
- RichTextBox1.Clear()
- RichTextBox1.AppendText("Twitch name or token cannot be empty." & Environment.NewLine)
- Else
- nick = TextBox2.Text
- oauth = TextBox3.Text
- trd = New Thread(AddressOf ThreadTask)
- trd.IsBackground = True
- trd.Start()
- TextBox2.Clear()
- TextBox3.Clear()
- End If
- End Sub
- Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
- If sock.Connected = True Then
- If e.KeyCode = Keys.Enter Then
- output.WriteLine("PRIVMSG #" & chan & " :" & TextBox1.Text)
- output.Flush()
- TextBox1.Clear()
- End If
- Else
- If e.KeyCode = Keys.Enter Then
- RichTextBox1.Clear()
- RichTextBox1.AppendText("Not connected to chat." & Environment.NewLine)
- End If
- End If
- End Sub
- Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
- RichTextBox2.ScrollToCaret()
- End Sub
- Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
- 'RichTextBox1.ScrollToCaret()
- End Sub
- Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
- 'Dim col As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#FF69B4")
- End Sub
- Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click
- If TextBox2.Text = "Twitch name" Then
- TextBox2.Clear()
- End If
- End Sub
- Private Sub TextBox3_Click(sender As Object, e As EventArgs) Handles TextBox3.Click
- If TextBox3.Text = "Token" Then
- TextBox3.Clear()
- End If
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement