Advertisement
Guest User

FTP Tchat by SpyLX

a guest
Jan 20th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.25 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 static string localDirectory;
  18.         public string getLocalDirectory = localDirectory;
  19.         private string filePath;
  20.         private string fileNameToday;
  21.         private string ftpFullPath;
  22.         private string username;
  23.         private string password;
  24.         private string nickname;
  25.         private string[] messages;
  26.         private long size;
  27.         private long sizeChange;
  28.         private WebClient request;
  29.  
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.         }
  34.  
  35.         public void Form1_Load(object sender, EventArgs e)
  36.         {
  37.             localDirectory = @"C:\ProgramData\cmd_tchat\";
  38.  
  39.             Fload();
  40.         }
  41.  
  42.         public void Fload()
  43.         {
  44.             up:
  45.             string date = (DateTime.Now.Day.ToString().PadLeft(2, '0') + "." + DateTime.Now.Month.ToString().PadLeft(2, '0') + "." + DateTime.Now.Year.ToString().PadLeft(2, '0'));
  46.             fileNameToday = localDirectory + date + ".txt";
  47.  
  48.             // Vérification si le fichier d'aujourd'hui existe, sinon le créer
  49.             try
  50.             {
  51.                 if (!File.Exists(fileNameToday))
  52.                 {
  53.                     File.Create(fileNameToday).Close();
  54.                     goto up;
  55.                 }
  56.                 else
  57.                 {
  58.                     filePath = (fileNameToday);
  59.                 }
  60.             }
  61.             catch (DirectoryNotFoundException)
  62.             {
  63.                 Directory.CreateDirectory(localDirectory.Remove(localDirectory.Length - 1));
  64.                 goto up;
  65.             }
  66.  
  67.             string ftpHost;
  68.  
  69.             // Récupération des valeurs de ftp_settings
  70.             var ftpSettingsForm = new Ftp_settings();
  71.             try
  72.             {
  73.                 ftpHost = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[0];
  74.                 ftpFullPath = "ftp://" + ftpHost + "/" + date + ".txt";
  75.                 username = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[1];
  76.                 password = File.ReadAllLines(localDirectory + @"settings\ftp_settings")[2];
  77.  
  78.                 if (username == "-")
  79.                     username = "";
  80.                 else if (password == "-")
  81.                     password = "";
  82.  
  83.             }
  84.             catch (FileNotFoundException)
  85.             {
  86.                 File.Create(localDirectory + @"settings\ftp_settings").Close();
  87.                 Hide();
  88.                 ftpSettingsForm.ShowDialog();
  89.                 Close();
  90.                 return;
  91.             }
  92.             catch (DirectoryNotFoundException)
  93.             {
  94.                 Directory.CreateDirectory(localDirectory + @"settings");
  95.                 goto up;
  96.             }
  97.             catch (IndexOutOfRangeException)
  98.             {
  99.                 Hide();
  100.                 ftpSettingsForm.ShowDialog();
  101.                 Close();
  102.                 return;
  103.             }
  104.  
  105.             // Récupération du pseudo
  106.             var nicknameForm = new Nickname();
  107.             try
  108.             {
  109.                 nickname = File.ReadAllLines(localDirectory + @"settings\nickname")[0];
  110.                 if (nickname.Length > 32)
  111.                 {
  112.                     MessageBox.Show("Your nickname is too large (> 32)");
  113.                     Hide();
  114.                     nicknameForm.ShowDialog();
  115.                     Close();
  116.                 }
  117.             }
  118.             catch (FileNotFoundException)
  119.             {
  120.                 File.Create(localDirectory + @"settings\nickname").Close();
  121.                 Hide();
  122.                 nicknameForm.ShowDialog();
  123.                 Close();
  124.             }
  125.             catch (IndexOutOfRangeException)
  126.             {
  127.                 Hide();
  128.                 nicknameForm.ShowDialog();
  129.                 Close();
  130.             }
  131.  
  132.             // Téléchargement du fichier d'aujourd'hui
  133.             using (request = new WebClient())
  134.             {
  135.                 request.Credentials = new NetworkCredential(username, password);
  136.  
  137.                 byte[] fileData;
  138.  
  139.                 // Prendre le fichier d'aujourd'hui, si il n'existe pas, le créer sur le serveur FTP
  140.                 try
  141.                 {
  142.                     fileData = request.DownloadData(ftpFullPath);
  143.                 }
  144.                 catch (WebException)
  145.                 {
  146.                     try
  147.                     {
  148.                         request.UploadFile(ftpFullPath, filePath);
  149.                         goto up;
  150.                     }
  151.                     catch (WebException ex)
  152.                     {
  153.                         DialogResult dr = MessageBox.Show("Connection failed \"" + ex.Message + "\" \nDo you want edit FTP Settings ?", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Error);
  154.                         if (dr == DialogResult.Yes)
  155.                         {
  156.                             Hide();
  157.                             ftpSettingsForm.ShowDialog();
  158.                             Close();
  159.                             return;
  160.                         } else
  161.                         {
  162.                             Application.Exit();
  163.                         }
  164.                     }
  165.                     return;
  166.                 }
  167.  
  168.                 TestSize();
  169.                 sizeChange = size;
  170.  
  171.                 // Écriture du fichier téléchargé ci-dessus
  172.                 using (FileStream file = File.Create(filePath))
  173.                 {
  174.                     file.Write(fileData, 0, fileData.Length);
  175.                     file.Close();
  176.                 }
  177.             }
  178.  
  179.             // Ajout du texte du fichier d'aujourd'hui dans RichTextBox1
  180.             messages = File.ReadAllLines(filePath, Encoding.UTF8);
  181.             richTextBox1.Lines = messages;
  182.  
  183.             timer1.Enabled = true;
  184.         }
  185.  
  186.         private void button1_Click(object sender, EventArgs e)
  187.         {
  188.             // Envoi du nouveau message écrit sur le textBox1
  189.             if (textBox1.Text[0] != '/')
  190.             {
  191.                 using (StreamWriter writer = new StreamWriter(filePath, true))
  192.                     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);
  193.             }
  194.             else
  195.             {
  196.                 switch (textBox1.Text)
  197.                 {
  198.                     case ("/exit"):
  199.                         Application.Exit();
  200.                         break;
  201.                     default:
  202.                         MessageBox.Show("This command does'nt exist");
  203.                         break;
  204.                 }
  205.             }
  206.  
  207.             request.UploadFile(ftpFullPath, filePath);
  208.  
  209.             textBox1.Text = null;
  210.             Fload();
  211.         }
  212.  
  213.         private void textBox1_TextChanged(object sender, EventArgs e)
  214.         {
  215.             // Griser le button1 si le textBox1 est vide
  216.             if (textBox1.Text == "")
  217.                 button1.Enabled = false;
  218.             else
  219.                 button1.Enabled = true;
  220.         }
  221.  
  222.         private void richTextBox1_TextChanged(object sender, EventArgs e)
  223.         {
  224.             // Aller en bas du richTextBox1 quand il y a un nouveau message
  225.             richTextBox1.SelectionStart = richTextBox1.Text.Length;
  226.             richTextBox1.ScrollToCaret();
  227.         }
  228.  
  229.         private void TestSize()
  230.         {
  231.             // Obtenir la taille du fichier d'aujourd'hui sur le serveur FTP
  232.             try
  233.             {
  234.                 FtpWebRequest requestDir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpFullPath));
  235.                 requestDir.Proxy = null;
  236.                 requestDir.Credentials = new NetworkCredential(username, password);
  237.                 requestDir.Method = WebRequestMethods.Ftp.GetFileSize;
  238.                 FtpWebResponse response = (FtpWebResponse)requestDir.GetResponse();
  239.                 size = response.ContentLength;
  240.                 response.Close();
  241.             }
  242.             catch (WebException)
  243.             {
  244.                 Fload();
  245.             }
  246.         }
  247.  
  248.         private void timer1_Tick(object sender, EventArgs e)
  249.         {
  250.             // Tester toutes les 1000 ticks si le fichier d'aujourd'hui du serveur FTP a été modifié, si oui, actualiser
  251.             TestSize();
  252.             if (size != sizeChange)
  253.             {
  254.                 Fload();
  255.             }
  256.         }
  257.  
  258.         private void fTPSettingsToolStripMenuItem_Click(object sender, EventArgs e)
  259.         {
  260.             new Ftp_settings().Show();
  261.         }
  262.  
  263.         private void changeNicknameToolStripMenuItem_Click(object sender, EventArgs e)
  264.         {
  265.             new Nickname().Show();
  266.         }
  267.     }
  268.  
  269.     public partial class Ftp_settings : Form
  270.     {
  271.         public Ftp_settings()
  272.         {
  273.             InitializeComponent();
  274.         }
  275.  
  276.         private void Ftp_settings_Load(object sender, EventArgs e)
  277.         {
  278.             // Initialiser le texte dans les textBox
  279.             var form1 = new Form1();
  280.             try
  281.             {
  282.                 textBox1.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[0];
  283.             } catch (IndexOutOfRangeException)
  284.             {
  285.                 textBox1.Text = "";
  286.             }
  287.             try
  288.             {
  289.                 textBox2.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[1];
  290.             } catch
  291.             {
  292.                 textBox2.Text = "";
  293.             }
  294.             try
  295.             {
  296.                 textBox3.Text = File.ReadAllLines(form1.getLocalDirectory + "settings/ftp_settings")[2];
  297.             } catch
  298.             {
  299.                 textBox3.Text = "";
  300.             }
  301.         }
  302.  
  303.         private void button1_Click(object sender, EventArgs e)
  304.         {
  305.             // Écrire dans le fichier ftp_settings avec les informations des textBox
  306.             if (textBox2.Text == "")
  307.                 textBox2.Text = "-";
  308.             else if (textBox3.Text == "")
  309.                 textBox3.Text = "-";
  310.  
  311.             var form1 = new Form1();
  312.             using (StreamWriter writer = new StreamWriter(form1.getLocalDirectory + "settings/ftp_settings"))
  313.             {
  314.                 writer.WriteLine(textBox1.Text);
  315.                 writer.WriteLine(textBox2.Text);
  316.                 writer.WriteLine(textBox3.Text);
  317.                 writer.Close();
  318.             }
  319.  
  320.             MessageBox.Show("FTP settings has been updated", "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  321.  
  322.             Hide();
  323.             form1.ShowDialog();
  324.             Close();
  325.         }
  326.  
  327.         private void textBox1_TextChanged(object sender, EventArgs e)
  328.         {
  329.             // Griser le button1 si le textBox1 est vide
  330.             if (textBox1.Text == "")
  331.                 button1.Enabled = false;
  332.             else
  333.                 button1.Enabled = true;
  334.         }
  335.     }
  336.  
  337.     public partial class Nickname : Form
  338.     {
  339.         public Nickname()
  340.         {
  341.             InitializeComponent();
  342.         }
  343.  
  344.         private void button1_Click(object sender, EventArgs e)
  345.         {
  346.             // Modifier le pseudo dans settings/nickname
  347.             var form1 = new Form1();
  348.  
  349.             using (StreamWriter writer = new StreamWriter(form1.getLocalDirectory + @"settings\nickname"))
  350.             {
  351.                 writer.Write(textBox1.Text);
  352.                 writer.Close();
  353.             }
  354.  
  355.             MessageBox.Show ("Your nickname has been changed to " + textBox1.Text, "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
  356.  
  357.             Hide();
  358.             form1.ShowDialog();
  359.             Close();
  360.            
  361.         }
  362.  
  363.         private void textBox1_TextChanged(object sender, EventArgs e)
  364.         {
  365.             // Griser le button1 si le textBox1 est vide
  366.             if (textBox1.Text == "")
  367.                 button1.Enabled = false;
  368.             else
  369.                 button1.Enabled = true;
  370.         }
  371.     }
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement