-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatForm.cs
executable file
·168 lines (137 loc) · 5.25 KB
/
ChatForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using IRC_protocol.TwitchIrc;
using IRC_protocol.Chat;
using System.Threading;
namespace IRC_protocol
{
public partial class ChatForm : Form
{
private TwitchIrcClient client;
private readonly string username;
private string channel;
private readonly string password;
public ChatForm(LoginCredentials credentials)
{
InitializeComponent();
AdjustPanels();
username = credentials.Username;
password = credentials.Password;
channel = credentials.Channel;
client = Connect();
JoinChannelAsync(channel);
new Thread(Reconnect).Start();
}
private void Reconnect()
{
while (true)
{
if (!client.Connected)
{
client = Connect();
JoinChannelAsync(channel);
}
}
}
private async Task JoinChannelAsync(string channel)
{
bool joined = await client.JoinChannelAsync(channel);
if(joined)
{
this.channel = channel;
ChannelLabel.Text = channel;
ChannelLabel.ForeColor = Color.Black;
}
else
{
ChannelLabel.Text = "Invalid Channel!";
ChannelLabel.ForeColor = Color.Red;
}
}
private TwitchIrcClient Connect()
{
return new TwitchIrcClient(username, password);
}
private void AdjustPanels()
{
int width = MainFlowLayoutPanel.Width;
int height = MainFlowLayoutPanel.Height;
ControlFlowLayoutPanel.Height = height;
TextFlowLayoutPanel.Width = width - ControlFlowLayoutPanel.Width - 12;
TextFlowLayoutPanel.Height = height;
UpperLayoutPanel.Width = TextFlowLayoutPanel.Width;
UpperLayoutPanel.Height = TextFlowLayoutPanel.Height - SendTextFlowLayoutPanel.Height;
DebugPanel.Width = (int)Math.Floor(TextFlowLayoutPanel.Width * 0.7) - 3;
DebugPanel.Height = UpperLayoutPanel.Height;
DebugLabel.MaximumSize = new Size(DebugPanel.Width - 10, 0);
ChatPanel.Width = (int)Math.Floor(TextFlowLayoutPanel.Width * 0.3) - 4;
ChatPanel.Height = UpperLayoutPanel.Height;
ChatLabel.MaximumSize = new Size(ChatPanel.Width - 10, 0);
SendTextFlowLayoutPanel.Width = TextFlowLayoutPanel.Width;
MessageTextBox.Width = SendTextFlowLayoutPanel.Width - SendButton.Width - 9;
}
private async Task SendMessageAsync(string channel)
{
string displayMessage = $"{username}: {MessageTextBox.Text}";
AppendChatLabel(displayMessage + "\n");
await client.SendChatMessageAsync(channel, MessageTextBox.Text);
}
private void AppendDebugLabel(string text)
{
DebugLabel.Text += text;
}
private void AppendChatLabel(string text)
{
ChatLabel.Text += text;
}
private void Form_Resize(object sender, EventArgs e)
{
AdjustPanels();
}
private void Timer_Tick(object sender, EventArgs e)
{
if(client.CanRead)
{
IrcMessage message = client.ReadStreamLine();
if (!string.IsNullOrEmpty(message.ToString()))
{
AppendDebugLabel(message.ToString() + Environment.NewLine);
if (message.GetMessageType() == MessageType.Chat)
{
ChatMessage chatMsg = new ChatMessage(message);
AppendChatLabel(chatMsg.ToString() + Environment.NewLine);
}
else if(message.GetMessageType() == MessageType.Ping)
{
client.PongResponseAsync();
}
}
}
}
private async void SendButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(MessageTextBox.Text)) await SendMessageAsync(channel);
MessageTextBox.Clear();
}
private void MessageTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter) SendButton_Click(sender, e);
}
private async void ChannelButton_Click(object sender, EventArgs e)
{
await client.PartChannelAsync(channel);
ChatLabel.Text = "";
await JoinChannelAsync(ChannelTextBox.Text);
ChannelTextBox.Text = "";
}
}
}