-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMain.cs
154 lines (144 loc) · 4.59 KB
/
FormMain.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
using System;
using System.Windows.Forms;
using System.IO;
using System.Text;
using System.Drawing;
using System.Collections.Generic;
using SSTPLib;
namespace DirectSSTPTester
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_Load(object sender, EventArgs e)
{
string txtPath = "default.txt";
if (File.Exists(txtPath))
{
this.textBoxMessageToSend.Text = GetText(txtPath);
}
SearchGhost();
string iconPath = Environment.ProcessPath;
Icon ico = Icon.ExtractAssociatedIcon(iconPath);
this.Icon = ico;
}
private void ButtonRefreshFMO_Click(object sender, EventArgs e)
{
SearchGhost();
}
private void SearchGhost()
{
this.comboBoxTargetName.Items.Clear();
SakuraFMO fmo = new("SakuraUnicode");
fmo.Update(true);
string[] names = fmo.GetGhostNames();
if (names.Length > 0)
{
this.comboBoxTargetName.Items.AddRange(names);
this.comboBoxTargetName.SelectedIndex = 0;
}
}
private void ButtonSend_Click(object sender, EventArgs e)
{
string s = this.textBoxMessageToSend.Text;
string t = this.comboBoxTargetName.Text;
DSSTPSender ds = new()
{
SendMessageTimeOut = 10000,
RecvMessageTimeOut = 10000
};
string r = ds.GetSSTPResponse(t, s);
ds.Dispose();
this.textBoxMessageReceived.Text = r;
}
private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
private void TextBoxMessageToSend_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// ドラッグ中のファイルやディレクトリの取得
string[] drags = (string[])e.Data.GetData(DataFormats.FileDrop);
e.Effect = DragDropEffects.None;
foreach (string d in drags)
{
if (!File.Exists(d))
{
// ファイル以外であればイベント・ハンドラを抜ける
return;
}
if (!Path.GetExtension(d).Equals(".txt"))
{
return;
}
}
e.Effect = DragDropEffects.Copy;
}
}
private void TextBoxMessageToSend_DragDrop(object sender, DragEventArgs e)
{
// ドラッグ&ドロップされたファイル
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string d in files)
{
if (!File.Exists(d))
{
continue;
}
if (!Path.GetExtension(d).Equals(".txt"))
{
continue;
}
this.textBoxMessageToSend.Text = GetText(d);
}
}
private static string GetText(string txtPath)
{
string charset = GetEncoding(txtPath);
StreamReader sr = new(txtPath, Encoding.GetEncoding(charset));
List<string> text = new();
string line;
while ((line = sr.ReadLine()) != null)
{
text.Add(line);
}
sr.Close();
return string.Join(Environment.NewLine, text.ToArray());
}
private static string GetEncoding(string txtPath)
{
StreamReader sr = new(txtPath, Encoding.GetEncoding("UTF-8"));
List<string> msg = new();
string line;
while ((line = sr.ReadLine()) != null)
{
msg.Add(line);
}
sr.Close();
string charset = "";
foreach (string s in msg)
{
if (s.StartsWith("Charset: "))
{
charset = s[9..];
break;
}
}
Encoding enc;
try
{
enc = Encoding.GetEncoding(charset);
}
catch (ArgumentException)
{
charset = "UTF-8";
}
return charset;
}
}
}