Advertisement
Guest User

asdasd

a guest
Oct 30th, 2014
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 7.40 KB | None | 0 0
  1. Imports System.Text.RegularExpressions
  2. Imports System.Threading
  3. Imports System.Drawing
  4.  
  5. Public Class Form1
  6.     Private trd As Thread
  7.     Dim port As Integer
  8.     Dim buf As String, nick As String, oauth As String, server As String, chan As String
  9.     Dim sock As New System.Net.Sockets.TcpClient()
  10.     Dim input As System.IO.TextReader
  11.     Dim output As System.IO.TextWriter
  12.     Dim colorDictionary As New Dictionary(Of String, String)()
  13.  
  14.     Public Shared Function GetBetween(input As String, before As String, after As String) As String
  15.         Return Regex.Split(Regex.Split(input, before)(1), after)(0)
  16.     End Function
  17.     Private Sub ThreadTask()
  18.  
  19.         'Get nick, owner, server, port, and channel from user
  20.  
  21.         'Connect to irc server and get input and output text streams from TcpClient.
  22.  
  23.         'Starting USER and NICK login commands
  24.  
  25.         'Process each line received from irc server
  26.  
  27.         'Display received irc message
  28.         'Console.WriteLine(buf);
  29.  
  30.         'Send pong reply to any ping messages
  31.  
  32.         ' IRC commands come in one of these formats:
  33.         '                     * :NICK!USER@HOST COMMAND ARGS ... :DATA\r\n
  34.         '                     * :SERVER COMAND ARGS ... :DATA\r\n
  35.         '                    
  36.  
  37.  
  38.         'After server sends 001 command, we can set mode to bot and join a channel
  39.         server = "irc.twitch.tv"
  40.         port = 6667
  41.         chan = "scarra"
  42.         sock.Connect(server, port)
  43.         If Not sock.Connected Then
  44.             Me.Invoke(New MethodInvoker(Function()
  45.                                             RichTextBox1.AppendText("Failed to connect!" & Environment.NewLine)
  46.  
  47.                                         End Function))
  48.             Return
  49.         End If
  50.         input = New System.IO.StreamReader(sock.GetStream())
  51.         output = New System.IO.StreamWriter(sock.GetStream())
  52.         output.Write("USER " & nick & " 0 * " & vbCr & vbLf & "PASS " & oauth & vbCr & vbLf & "NICK " & nick & vbCr & vbLf)
  53.         output.WriteLine("TWITCHCLIENT 1")
  54.         output.Flush()
  55.         buf = input.ReadLine()
  56.         While True
  57.             Try
  58.                 If Not buf.Contains("#scarra :") Then
  59.                     Me.Invoke(New MethodInvoker(Function()
  60.                                                     RichTextBox1.AppendText(buf & Environment.NewLine)
  61.  
  62.  
  63.                                                     If buf.ToString().Contains(":USERCOLOR") Then
  64.                                                         Dim name As String = GetBetween(buf.ToString(), "USERCOLOR", "#")
  65.                                                         Dim color As String = buf.ToString().Split("#")(1)
  66.                                                         If colorDictionary.ContainsKey(name) = False Then
  67.                                                             colorDictionary.Add(name, color)
  68.                                                         ElseIf colorDictionary(name) <> color Then
  69.                                                             colorDictionary(name) = color
  70.                                                         End If
  71.  
  72.  
  73.                                                         For Each key In colorDictionary.Keys
  74.                                                             ListBox1.Items.Add((key & ":") + colorDictionary(key))
  75.                                                         Next
  76.                                                     End If
  77.  
  78.                                                 End Function))
  79.                 Else
  80.                     Try
  81.                         Me.Invoke(New MethodInvoker(Function()
  82.                                                         If buf.ToString().Contains("@") AndAlso buf.ToString().Contains(".tmi.twitch.tv") Then
  83.                                                             RichTextBox2.AppendText(GetBetween(buf, "@", ".tmi") & ": " & buf.ToString().Split(":")(2) & Environment.NewLine)
  84.                                                         Else
  85.                                                             RichTextBox1.AppendText(buf & Environment.NewLine)
  86.                                                         End If
  87.  
  88.  
  89.                                                     End Function))
  90.                     Catch generatedExceptionName As Exception
  91.                     End Try
  92.                 End If
  93.                 If buf.StartsWith("PING ") Then
  94.                     output.Write(buf.Replace("PING", "PONG") & vbCr & vbLf)
  95.                     output.Flush()
  96.                 End If
  97.                 If buf(0) <> ":"c Then
  98.                     Continue While
  99.                 End If
  100.                 If buf.Split(" "c)(1) = "001" Then
  101.                     output.Write("MODE " & nick & " +B" & vbCr & vbLf & "JOIN #" & chan & vbCr & vbLf)
  102.                     output.Flush()
  103.                 End If
  104.                 buf = input.ReadLine()
  105.             Catch
  106.                 RichTextBox1.Clear()
  107.                 RichTextBox1.AppendText("Could not connect to chat." & Environment.NewLine)
  108.                 Exit While
  109.             End Try
  110.         End While
  111.  
  112.  
  113.     End Sub
  114.  
  115.     Public Shared Sub AppendText(box As RichTextBox, text As String, color As Color)
  116.         box.SelectionStart = box.TextLength
  117.         box.SelectionLength = 0
  118.  
  119.         box.SelectionColor = color
  120.         box.AppendText(text)
  121.         box.SelectionColor = box.ForeColor
  122.     End Sub
  123.  
  124.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  125.         If TextBox2.Text.Length < 1 Or TextBox3.Text.Length < 1 Then
  126.             RichTextBox1.Clear()
  127.             RichTextBox1.AppendText("Twitch name or token cannot be empty." & Environment.NewLine)
  128.         Else
  129.             nick = TextBox2.Text
  130.             oauth = TextBox3.Text
  131.             trd = New Thread(AddressOf ThreadTask)
  132.             trd.IsBackground = True
  133.             trd.Start()
  134.  
  135.             TextBox2.Clear()
  136.             TextBox3.Clear()
  137.         End If
  138.     End Sub
  139.  
  140.     Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
  141.         If sock.Connected = True Then
  142.             If e.KeyCode = Keys.Enter Then
  143.                 output.WriteLine("PRIVMSG #" & chan & " :" & TextBox1.Text)
  144.                 output.Flush()
  145.                 TextBox1.Clear()
  146.             End If
  147.         Else
  148.             If e.KeyCode = Keys.Enter Then
  149.                 RichTextBox1.Clear()
  150.                 RichTextBox1.AppendText("Not connected to chat." & Environment.NewLine)
  151.             End If
  152.         End If
  153.     End Sub
  154.  
  155.     Private Sub RichTextBox2_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox2.TextChanged
  156.         RichTextBox2.ScrollToCaret()
  157.     End Sub
  158.  
  159.     Private Sub RichTextBox1_TextChanged(sender As Object, e As EventArgs) Handles RichTextBox1.TextChanged
  160.         'RichTextBox1.ScrollToCaret()
  161.     End Sub
  162.  
  163.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  164.         'Dim col As System.Drawing.Color = System.Drawing.ColorTranslator.FromHtml("#FF69B4")
  165.     End Sub
  166.  
  167.     Private Sub TextBox2_Click(sender As Object, e As EventArgs) Handles TextBox2.Click
  168.         If TextBox2.Text = "Twitch name" Then
  169.             TextBox2.Clear()
  170.         End If
  171.     End Sub
  172.  
  173.     Private Sub TextBox3_Click(sender As Object, e As EventArgs) Handles TextBox3.Click
  174.         If TextBox3.Text = "Token" Then
  175.             TextBox3.Clear()
  176.         End If
  177.     End Sub
  178. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement