Advertisement
Guest User

Untitled

a guest
Jan 19th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.67 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12.  
  13. namespace FTP_Tchat_by_SpyLX
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         private string localDirectory;
  18.         private string filePath;
  19.         private string fileNameToday;
  20.         private string ftpFullPath;
  21.         private string username;
  22.         private string password;
  23.         private string[] messages;
  24.         private short tryAgainOrError;
  25.         private long size;
  26.         private long sizeChange;
  27.         private bool esc;
  28.         private WebClient request;
  29.         private FtpWebRequest requestDir;
  30.         private FtpWebResponse response;
  31.  
  32.         public Form1()
  33.         {
  34.             InitializeComponent();
  35.         }
  36.  
  37.         private void Form1_Load(object sender, EventArgs e)
  38.         {
  39.             localDirectory = @"C:\ProgramData\cmd_tchat\";
  40.            
  41.             MessageBox.Show("Connecting...");
  42.             Fload();
  43.         }
  44.  
  45.         private void Fload()
  46.         {
  47.             up:
  48.             string date = (DateTime.Now.Day + "." + DateTime.Now.Month + "." + DateTime.Now.Year);
  49.             fileNameToday = localDirectory + date + ".txt";
  50.  
  51.             // Vérification si le fichier d'aujourd'hui existe, sinon le créer
  52.             try
  53.             {
  54.                 if (!File.Exists(fileNameToday))
  55.                 {
  56.                     File.Create(fileNameToday).Close();
  57.                     goto up;
  58.                 }
  59.                 else
  60.                 {
  61.                     filePath = (fileNameToday);
  62.                 }
  63.             }
  64.             catch (DirectoryNotFoundException)
  65.             {
  66.                 Directory.CreateDirectory(localDirectory.Remove(localDirectory.Length - 1));
  67.                 goto up;
  68.             }
  69.  
  70.             string ftpHost;
  71.  
  72.             // Récupération des valeurs de ftp_settings
  73.             try
  74.             {
  75.                 ftpHost = File.ReadAllLines(localDirectory + "ftp_settings")[2];
  76.                 ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
  77.                 username = File.ReadAllLines(localDirectory + "ftp_settings")[3];
  78.                 password = File.ReadAllLines(localDirectory + "ftp_settings")[4];
  79.  
  80.                 if (username == "-")
  81.                     username = "";
  82.                 else if (password == "-")
  83.                     password = "";
  84.  
  85.             }
  86.             catch (FileNotFoundException)
  87.             {
  88.                 File.Create(localDirectory + "ftp_settings").Close();
  89.                 using (StreamWriter writer = new StreamWriter(localDirectory + "ftp_settings", true))
  90.                 {
  91.                     writer.WriteLine("# Line 3: IP Adress ; Line 4: FTP Username ; Line 5: FTP Password");
  92.                     writer.WriteLine("# Put a '-' if the value is null");
  93.                     writer.WriteLine("127.0.0.1");
  94.                     writer.WriteLine("spylx");
  95.                     writer.WriteLine("-");
  96.                     writer.Close();
  97.                 }
  98.                 goto up;
  99.             }
  100.             catch (IndexOutOfRangeException)
  101.             {
  102.                 MessageBox.Show("Une valeur est vide dans " + localDirectory + "ftp_settings");
  103.                 Application.Exit();
  104.                 goto up;
  105.             }
  106.  
  107.             // Téléchargement du fichier d'aujourd'hui
  108.             using (request = new WebClient())
  109.             {
  110.                 request.Credentials = new NetworkCredential(username, password);
  111.  
  112.                 sizeChange = size;
  113.  
  114.                 byte[] fileData;
  115.  
  116.                 // Prendre le fichier d'aujourd'hui, si il n'existe pas, le créer sur le serveur FTP
  117.                 try
  118.                 {
  119.                     fileData = request.DownloadData(ftpFullPath);
  120.                 }
  121.                 catch (WebException)
  122.                 {
  123.                     try
  124.                     {
  125.                         request.UploadFile(ftpFullPath, filePath);
  126.                         goto up;
  127.                     }
  128.                     catch (WebException)
  129.                     {
  130.                         tryAgainOrError++;
  131.                         if (tryAgainOrError <= 100)
  132.                         {
  133.                             goto up;
  134.                         }
  135.                         else
  136.                         {
  137.                             MessageBox.Show("Impossible d'accéder au serveur.");
  138.                             Application.Exit();
  139.                         }
  140.                     }
  141.                     return;
  142.                 }
  143.  
  144.                 // Écriture du fichier téléchargé ci-dessus
  145.                 using (FileStream file = File.Create(filePath))
  146.                 {
  147.                     file.Write(fileData, 0, fileData.Length);
  148.                     file.Close();
  149.                 }
  150.             }
  151.  
  152.             // Ajout du texte du fichier d'aujourd'hui dans RichTextBox1
  153.             messages = File.ReadAllLines(filePath, Encoding.UTF8);
  154.             richTextBox1.Lines = messages;
  155.  
  156.             if (!esc)
  157.             {
  158.                 SendKeys.Send("{ESC}");
  159.                 esc = true;
  160.             }
  161.         }
  162.  
  163.         private void button1_Click(object sender, EventArgs e)
  164.         {
  165.             // Envoi du nouveau message écrit sur le textBox1
  166.             using (StreamWriter writer = new StreamWriter(filePath, true))
  167.                 writer.WriteLine("[" + DateTime.Now.Hour.ToString().PadLeft(2, '0') + ':' + DateTime.Now.Minute.ToString().PadLeft(2, '0') + '.' + DateTime.Now.Second.ToString().PadLeft(2, '0') + "] " + textBox1.Text);
  168.            
  169.             request.UploadFile(ftpFullPath, filePath);
  170.  
  171.             textBox1.Text = null;
  172.             Fload();
  173.         }
  174.  
  175.         private void textBox1_TextChanged(object sender, EventArgs e)
  176.         {
  177.             // Griser le button1 si le textBox1 est vide
  178.             if (textBox1.Text == "")
  179.                 button1.Enabled = false;
  180.             else
  181.                 button1.Enabled = true;
  182.         }
  183.  
  184.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  185.         {
  186.             // Aller en bas du richTextBox1 quand il y a un nouveau message
  187.             richTextBox1.SelectionStart = richTextBox1.Text.Length;
  188.             richTextBox1.ScrollToCaret();
  189.         }
  190.  
  191.         private void TestSize()
  192.         {
  193.             // Obtenir la taille du fichier d'aujourd'hui sur le serveur FTP
  194.             try
  195.             {
  196.                 requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
  197.                 requestDir.Credentials = new NetworkCredential(username, password);
  198.                 requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
  199.                 response = (FtpWebResponse)requestDir.GetResponse();
  200.                 size = response.ContentLength;
  201.                 response.Close();
  202.             }
  203.             catch
  204.             {
  205.                 Fload();
  206.             }
  207.         }
  208.  
  209.         private void timer1_Tick(object sender, EventArgs e)
  210.         {
  211.             // Tester toutes les 1000 ticks si le fichier d'aujourd'hui du serveur FTP a été modifié, si oui, actualiser
  212.             TestSize();
  213.             if (size != sizeChange)
  214.             {
  215.                 Fload();
  216.             }
  217.         }
  218.  
  219.         private void button2_Click(object sender, EventArgs e)
  220.         {
  221.             MessageBox.Show(size.ToString());
  222.             MessageBox.Show(sizeChange.ToString());
  223.         }
  224.     }//ce soir : Mettre les nouveaux commentaires, et voir pour le bug du serv introuvable, + faire les trucs sur keep
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement