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.IO;
- using System.Net;
- namespace FTP_Tchat_by_SpyLX
- {
- public partial class Form1 : Form
- {
- private static string localDirectory;
- public string getLocalDirectory = localDirectory;
- private string filePath;
- private string fileNameToday;
- private string ftpFullPath;
- private string username;
- private string password;
- private string nickname;
- private string[] messages;
- private long size;
- private long sizeChange;
- private static bool sFtpSettings;
- private static bool sNickname;
- public bool getSFtpSettings = sFtpSettings;
- public bool getSNickname = sNickname;
- private WebClient request;
- public Form1()
- {
- InitializeComponent();
- }
- public void Form1_Load(object sender, EventArgs e)
- {
- localDirectory = @"C:\ProgramData\cmd_tchat\";
- Fload();
- }
- public void Fload()
- {
- up:
- string date = (DateTime.Now.Day.ToString().PadLeft(2, '0') + "." + DateTime.Now.Month.ToString().PadLeft(2, '0') + "." + DateTime.Now.Year.ToString().PadLeft(2, '0'));
- fileNameToday = localDirectory + date + ".txt";
- // Vérification si le fichier d'aujourd'hui existe, sinon le créer
- try
- {
- if (!File.Exists(fileNameToday))
- {
- File.Create(fileNameToday).Close();
- goto up;
- }
- else
- {
- filePath = (fileNameToday);
- }
- }
- catch (DirectoryNotFoundException)
- {
- Directory.CreateDirectory(localDirectory.Remove(localDirectory.Length - 1));
- goto up;
- }
- string ftpHost;
- // Récupération des valeurs de ftp_settings
- var ftpSettingsForm = new Ftp_settings();
- try
- {
- sFtpSettings = false;
- ftpHost = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[0];
- ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
- username = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[1];
- password = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[2];
- if (username == "-")
- username = "";
- else if (password == "-")
- password = "";
- }
- catch (FileNotFoundException)
- {
- File.Create(localDirectory + @"settings\ftp_settings").Close();
- sFtpSettings = true;
- Hide();
- ftpSettingsForm.ShowDialog();
- Close();
- return;
- }
- catch (DirectoryNotFoundException)
- {
- Directory.CreateDirectory(localDirectory + @"settings");
- goto up;
- }
- catch (IndexOutOfRangeException)
- {
- sFtpSettings = true;
- Hide();
- ftpSettingsForm.ShowDialog();
- Close();
- return;
- }
- // Récupération du pseudo
- var nicknameForm = new Nickname();
- try
- {
- sNickname = false;
- nickname = File.ReadAllLines(localDirectory + @"settings\nickname")[0];
- if (nickname.Length > 32)
- {
- MessageBox.Show("Your nickname is too large (> 32)", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
- sNickname = true;
- Hide();
- nicknameForm.ShowDialog();
- Close();
- return;
- }
- }
- catch (FileNotFoundException)
- {
- File.Create(localDirectory + @"settings\nickname").Close();
- sNickname = true;
- Hide();
- nicknameForm.ShowDialog();
- Close();
- return;
- }
- catch (IndexOutOfRangeException)
- {
- sNickname = true;
- Hide();
- nicknameForm.ShowDialog();
- Close();
- return;
- }
- // Téléchargement du fichier d'aujourd'hui
- using (request = new WebClient())
- {
- request.Credentials = new NetworkCredential(username, password);
- byte[] fileData;
- // Prendre le fichier d'aujourd'hui, si il n'existe pas, le créer sur le serveur FTP
- try
- {
- fileData = request.DownloadData(ftpFullPath);
- }
- catch (WebException)
- {
- try
- {
- request.UploadFile(ftpFullPath, filePath);
- goto up;
- }
- catch (WebException ex)
- {
- DialogResult dr = MessageBox.Show("Connection failed \"" + ex.Message + "\" \nDo you want edit FTP Settings ?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
- if (dr == DialogResult.Yes)
- {
- sFtpSettings = true;
- Hide();
- ftpSettingsForm.ShowDialog();
- Close();
- return;
- } else
- {
- Application.Exit();
- }
- }
- return;
- }
- TestSize();
- sizeChange = size;
- // Écriture du fichier téléchargé ci-dessus
- using (FileStream file = File.Create(filePath))
- {
- file.Write(fileData, 0, fileData.Length);
- file.Close();
- }
- }
- // Ajout du texte du fichier d'aujourd'hui dans RichTextBox1
- messages = File.ReadAllLines(filePath, Encoding.UTF8);
- richTextBox1.Lines = messages;
- timer1.Enabled = true;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Envoi du nouveau message écrit sur le textBox1
- if (textBox1.Text[0] != '/')
- {
- using (StreamWriter writer = new StreamWriter(filePath, true))
- writer.WriteLine("[" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + ':' + DateTime.Now.Minute.ToString().PadLeft(2, '0') + '.' + DateTime.Now.Second.ToString().PadLeft(2, '0') + "] " + nickname + ": " + textBox1.Text);
- }
- else
- {
- switch (textBox1.Text)
- {
- case ("/exit"):
- Application.Exit();
- break;
- default:
- MessageBox.Show("This command does'nt exist", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- break;
- }
- }
- request.UploadFile(ftpFullPath, filePath);
- textBox1.Text = null;
- Fload();
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- // Griser le button1 si le textBox1 est vide
- if (textBox1.Text == "")
- button1.Enabled = false;
- else
- button1.Enabled = true;
- }
- private void richTextBox1_TextChanged(object sender, EventArgs e)
- {
- // Aller en bas du richTextBox1 quand il y a un nouveau message
- richTextBox1.SelectionStart = richTextBox1.Text.Length;
- richTextBox1.ScrollToCaret();
- }
- private void TestSize()
- {
- // Obtenir la taille du fichier d'aujourd'hui sur le serveur FTP
- try
- {
- FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
- requestDir.Proxy = null;
- requestDir.Credentials = new NetworkCredential(username, password);
- requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
- FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
- size = response.ContentLength;
- response.Close();
- }
- catch (WebException)
- {
- Fload();
- }
- }
- private void timer1_Tick(object sender, EventArgs e)
- {
- // Tester toutes les 1000 ticks si le fichier d'aujourd'hui du serveur FTP a été modifié, si oui, actualiser
- TestSize();
- if (size != sizeChange)
- {
- Fload();
- }
- }
- private void fTPSettingsToolStripMenuItem_Click(object sender, EventArgs e)
- {
- new Ftp_settings().Show();
- }
- private void changeNicknameToolStripMenuItem_Click(object sender, EventArgs e)
- {
- new Nickname().Show();
- }
- private void aToolStripMenuItem_Click(object sender, EventArgs e)
- {
- new AboutBox1().Show();
- }
- }
- public partial class Ftp_settings : Form
- {
- public Ftp_settings()
- {
- InitializeComponent();
- }
- private void Ftp_settings_Load(object sender, EventArgs e)
- {
- // Initialiser le texte dans les textBox
- var form1 = new Form1();
- try
- {
- textBox1.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[0];
- } catch (IndexOutOfRangeException)
- {
- textBox1.Text = "";
- }
- try
- {
- textBox2.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[1];
- } catch (IndexOutOfRangeException)
- {
- textBox2.Text = "";
- }
- try
- {
- textBox3.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[2];
- } catch (IndexOutOfRangeException)
- {
- textBox3.Text = "";
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Écrire dans le fichier ftp_settings avec les informations des textBox
- if (textBox2.Text == "")
- textBox2.Text = "-";
- else if (textBox3.Text == "")
- textBox3.Text = "-";
- var form1 = new Form1();
- using (StreamWriter writer = new StreamWriter(form1.getLocalDirectory + "settings/ftp_settings"))
- {
- writer.WriteLine(textBox1.Text);
- writer.WriteLine(textBox2.Text);
- writer.WriteLine(textBox3.Text);
- writer.Close();
- }
- MessageBox.Show("FTP settings has been updated", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- if (form1.getSFtpSettings)
- {
- Hide();
- form1.ShowDialog();
- Close();
- }
- else
- {
- Application.Exit();
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- // Griser le button1 si le textBox1 est vide
- if (textBox1.Text == "")
- button1.Enabled = false;
- else
- button1.Enabled = true;
- }
- }
- public partial class Nickname : Form
- {
- public Nickname()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- // Modifier le pseudo dans settings/nickname
- var form1 = new Form1();
- using (StreamWriter writer = new StreamWriter(form1.getLocalDirectory + @"settings\nickname"))
- {
- writer.Write(textBox1.Text);
- writer.Close();
- }
- MessageBox.Show ("Your nickname has been changed to " + textBox1.Text, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
- if (form1.getSNickname)
- {
- Hide();
- form1.ShowDialog();
- Close();
- } else
- {
- Application.Exit();
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- // Griser le button1 si le textBox1 est vide
- if (textBox1.Text == "")
- button1.Enabled = false;
- else
- button1.Enabled = true;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement