Advertisement
Guest User

FTP Tchat by SpyLX 21/01/2018 2:28

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