Advertisement
Guest User

Untitled

a guest
Aug 1st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 17.21 KB | None | 0 0
  1. using System;
  2. using System.Data;
  3. using System.Data.SqlClient;
  4. using MySql.Data.MySqlClient;
  5. using System.Collections;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. /*
  10.             command =connection.CreateCommand();
  11.             command.CommandText = "INSERT INTO log(userid,message,date) VALUES(1,'Hello there',NOW());";
  12.             MySqlDataReader reader;
  13.             reader = command.ExecuteReader();
  14.             Console.WriteLine("Executed Query: " + reader);
  15.  */
  16. namespace Lightchat_server
  17. {
  18.     public class SQL
  19.     {
  20.         private MySqlConnection connection = new MySqlConnection();
  21.         private MySqlDataAdapter data = new MySqlDataAdapter();
  22.         private MySqlCommand command = new MySqlCommand();
  23.         MySqlDataReader reader;
  24.  
  25.  
  26.         public void connect(Boolean flag)
  27.         {
  28.             if (flag) { Console.WriteLine("Connecting to MySQL server @ localhost"); }
  29.             //Set connection string
  30.             connection.ConnectionString =
  31.                 "server=localhost;"
  32.                 + "database=lightmunnas;"
  33.                 + "uid=root;"
  34.                 + "password=redrun11;";
  35.             //Connect
  36.             connection.Open();
  37.             if (flag) { Console.WriteLine("    >Connected"); }
  38.         }
  39.  
  40.         public void disconnect()
  41.         {
  42.             try
  43.             {
  44.                 connection.Dispose();
  45.                 connection.Close();
  46.             }
  47.             catch
  48.             {
  49.  
  50.             }
  51.         }
  52.  
  53.         public void userLogin(String userName) // on client connection
  54.         {
  55.             command = connection.CreateCommand();
  56.             command.CommandText = "SELECT * FROM users WHERE (username = '" + userName + "');";
  57.             reader = command.ExecuteReader();
  58.  
  59.             if (reader.Read() != false)//Is the client name already in the database?
  60.             {
  61.                 reader.Close(); //close reader
  62.  
  63.                 command.CommandText = "UPDATE users SET lastlogin=NOW() WHERE username ='" + userName + "';";
  64.                 command.ExecuteNonQuery();
  65.             }
  66.             else //add username to the database
  67.             {
  68.                 reader.Close(); //close reader
  69.  
  70.                 Console.WriteLine("    >Clients first time connecting, adding to database");
  71.                 command.CommandText = "INSERT INTO users(username,lastlogin,sent) VALUES('" + userName + "',NOW(),0);";
  72.                 command.ExecuteNonQuery();
  73.             }
  74.         }
  75.  
  76.         public void createChannel(String name, String password)
  77.         {
  78.             command = connection.CreateCommand();
  79.             command.CommandText = "INSERT INTO channels(channelname,channelpassword) VALUES('" + name + "','" + password + "');";
  80.             command.ExecuteNonQuery();
  81.         }
  82.  
  83.         public void newMessage(String userName, String msg)
  84.         {
  85.             command = connection.CreateCommand();
  86.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  87.             reader = command.ExecuteReader();
  88.             reader.Read();
  89.             String id = reader.GetValue(0).ToString(); // get id
  90.             reader.Close();
  91.  
  92.             command.CommandText = "INSERT INTO log(userid,message,date) VALUES(" + id + ",'" + msg + "',NOW());";
  93.             command.ExecuteNonQuery();
  94.             command.CommandText = "UPDATE users SET sent = sent + 1 WHERE id=" + id + ";";
  95.             command.ExecuteNonQuery();
  96.         }
  97.  
  98.         public string getLog(String input)
  99.         {
  100.             try
  101.             {
  102.                 String[] arguments = input.Split('-');
  103.  
  104.                 String users = null;
  105.                 bool usersb = false;
  106.                 String date = null;
  107.                 bool dateb = false;
  108.  
  109.                 foreach (string argument in arguments)
  110.                 {
  111.                     if (argument.Substring(0, 1) == "u")
  112.                     {
  113.                         String[] s;
  114.                         String noArg = argument.Substring(argument.IndexOf('u') + 1);
  115.                         if (argument.Contains(','))
  116.                         {
  117.  
  118.                             s = noArg.Split(',');
  119.                             users = "users.username='" + s[0].Trim() + "'";
  120.  
  121.                             for (int i = 1; i < s.Length; i++)
  122.                             {
  123.                                 users += " OR users.username='" + s[i].Trim() + "'";
  124.                             }
  125.  
  126.                         }
  127.                         else
  128.                         {
  129.                             users = "users.username='" + noArg.Trim() + "'";
  130.                         }
  131.                         usersb = true;
  132.  
  133.                     }
  134.                     else if (argument.Substring(0, 1) == "d")
  135.                     {
  136.                         String noArg = argument.Substring(argument.IndexOf('d') + 1).Trim();
  137.                         dateb = true;
  138.                         if (argument.Contains(','))
  139.                         {
  140.                             String[] d;
  141.                             int argCount = 0;
  142.                             d = noArg.Split(',');
  143.                             foreach (String dt in d)
  144.                             {
  145.                                 if (dt.Contains('*'))
  146.                                 {
  147.                                     String[] dtsplit = dt.Split('*');
  148.                                     foreach (String dts in dtsplit)
  149.                                     {
  150.                                         dts.Trim();
  151.                                     }
  152.                                     if (argCount == 0)
  153.                                     {
  154.                                         date += "log.date BETWEEN CAST '" + dtsplit[0] + "00' AS DATETIME AND CAST'" + dtsplit[1] + "00' DATETIME";
  155.                                         argCount++;
  156.                                     }
  157.                                     else
  158.                                     {
  159.                                         date += "OR log.date BETWEEN CAST '" + dtsplit[0] + "00' DATETIME AND CAST '" + dtsplit[1] + "00' DATETIME";
  160.                                     }
  161.  
  162.                                 }
  163.                                 else
  164.                                 {
  165.                                     if (argCount == 0)
  166.                                     {
  167.                                         date += "log.date BETWEEN CAST '" + dt + "00' DATETIME AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  168.                                         argCount++;
  169.                                     }
  170.                                     else
  171.                                     {
  172.                                         date += " OR log.date BETWEEN '" + dt + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  173.                                     }
  174.                                 }
  175.                             }
  176.                         }
  177.                         else
  178.                         {
  179.                             int argCount = 0;
  180.                             String d = noArg;
  181.                             if (d.Contains('*'))
  182.                             {
  183.                                 String[] dtsplit = d.Split('*');
  184.                                 foreach (String dts in dtsplit)
  185.                                 {
  186.                                     dts.Trim();
  187.                                 }
  188.                                 if (argCount == 0)
  189.                                 {
  190.                                     date += "log.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  191.                                     argCount++;
  192.                                 }
  193.                                 else
  194.                                 {
  195.                                     date += "OR log.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  196.                                 }
  197.  
  198.                             }
  199.                             else
  200.                             {
  201.                                 if (argCount == 0)
  202.                                 {
  203.                                     date += "log.date BETWEEN '" + d + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  204.                                     argCount++;
  205.                                 }
  206.                                 else
  207.                                 {
  208.                                     date += " OR log.date BETWEEN '" + d + "'00 AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  209.                                 }
  210.                             }
  211.                         }
  212.                     }
  213.  
  214.                 }
  215.                 String args = "SELECT users.username, log.message, log.date FROM log INNER JOIN users ON users.id=log.userid WHERE ";
  216.                 if (!usersb && dateb)
  217.                 {
  218.                     args += date;
  219.                 }
  220.                 else if (usersb && !dateb)
  221.                 {
  222.                     args += users;
  223.                 }
  224.                 else
  225.                 {
  226.                     args += users + " AND " + date;
  227.                 }
  228.                 args += " ORDER BY log.date ASC;";
  229.  
  230.  
  231.  
  232.                 String data = null;
  233.                 Console.WriteLine(args);
  234.                 Console.WriteLine();
  235.  
  236.                 command = connection.CreateCommand();
  237.                 command.CommandText = args;
  238.                 reader = command.ExecuteReader();
  239.  
  240.                 while (reader.Read())
  241.                 {
  242.                     data += "(" + reader["date"].ToString() + ") <" + reader["username"].ToString() + "> " + reader["message"].ToString() + Environment.NewLine;
  243.                 }
  244.  
  245.                 reader.Close();
  246.  
  247.                 return (data);
  248.  
  249.             }
  250.             catch (Exception ex)
  251.             {
  252.                 Console.WriteLine("    >/log ERROR: " + ex.Message);
  253.                 return (null);
  254.  
  255.             }
  256.         }
  257.  
  258.         public string getFileLog(String input)
  259.         {
  260.             try
  261.             {
  262.                 String[] arguments = input.Split('-');
  263.  
  264.                 String users = null;
  265.                 bool usersb = false;
  266.                 String date = null;
  267.                 bool dateb = false;
  268.  
  269.                 foreach (string argument in arguments)
  270.                 {
  271.                     if (argument.Substring(0, 1) == "u")
  272.                     {
  273.                         String[] s;
  274.                         String noArg = argument.Substring(argument.IndexOf('u') + 1);
  275.                         if (argument.Contains(','))
  276.                         {
  277.  
  278.                             s = noArg.Split(',');
  279.                             users = "users.username='" + s[0].Trim() + "'";
  280.  
  281.                             for (int i = 1; i < s.Length; i++)
  282.                             {
  283.                                 users += " OR users.username='" + s[i].Trim() + "'";
  284.                             }
  285.  
  286.                         }
  287.                         else
  288.                         {
  289.                             users = "users.username='" + noArg.Trim() + "'";
  290.                         }
  291.                         usersb = true;
  292.  
  293.                     }
  294.                     else if (argument.Substring(0, 1) == "d")
  295.                     {
  296.                         String noArg = argument.Substring(argument.IndexOf('d') + 1).Trim();
  297.                         dateb = true;
  298.                         if (argument.Contains(','))
  299.                         {
  300.                             String[] d;
  301.                             int argCount = 0;
  302.                             d = noArg.Split(',');
  303.                             foreach (String dt in d)
  304.                             {
  305.                                 if (dt.Contains('*'))
  306.                                 {
  307.                                     String[] dtsplit = dt.Split('*');
  308.                                     foreach (String dts in dtsplit)
  309.                                     {
  310.                                         dts.Trim();
  311.                                     }
  312.                                     if (argCount == 0)
  313.                                     {
  314.                                         date += "filelog.date BETWEEN CAST '" + dtsplit[0] + "00' AS DATETIME AND CAST'" + dtsplit[1] + "00' DATETIME";
  315.                                         argCount++;
  316.                                     }
  317.                                     else
  318.                                     {
  319.                                         date += "OR filelog.date BETWEEN CAST '" + dtsplit[0] + "00' DATETIME AND CAST '" + dtsplit[1] + "00' DATETIME";
  320.                                     }
  321.  
  322.                                 }
  323.                                 else
  324.                                 {
  325.                                     if (argCount == 0)
  326.                                     {
  327.                                         date += "filelog.date BETWEEN CAST '" + dt + "00' DATETIME AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  328.                                         argCount++;
  329.                                     }
  330.                                     else
  331.                                     {
  332.                                         date += " OR filelog.date BETWEEN '" + dt + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  333.                                     }
  334.                                 }
  335.                             }
  336.                         }
  337.                         else
  338.                         {
  339.                             int argCount = 0;
  340.                             String d = noArg;
  341.                             if (d.Contains('*'))
  342.                             {
  343.                                 String[] dtsplit = d.Split('*');
  344.                                 foreach (String dts in dtsplit)
  345.                                 {
  346.                                     dts.Trim();
  347.                                 }
  348.                                 if (argCount == 0)
  349.                                 {
  350.                                     date += "filelog.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  351.                                     argCount++;
  352.                                 }
  353.                                 else
  354.                                 {
  355.                                     date += "OR filelog.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  356.                                 }
  357.  
  358.                             }
  359.                             else
  360.                             {
  361.                                 if (argCount == 0)
  362.                                 {
  363.                                     date += "filelog.date BETWEEN '" + d + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  364.                                     argCount++;
  365.                                 }
  366.                                 else
  367.                                 {
  368.                                     date += " OR filelog.date BETWEEN '" + d + "'00 AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  369.                                 }
  370.                             }
  371.                         }
  372.                     }
  373.  
  374.                 }
  375.                 String args = "SELECT users.username, filelog.filename, filelog.size, filelog.date FROM filelog INNER JOIN users ON users.id=filelog.userid WHERE ";
  376.                 if (!usersb && dateb)
  377.                 {
  378.                     args += date;
  379.                 }
  380.                 else if (usersb && !dateb)
  381.                 {
  382.                     args += users;
  383.                 }
  384.                 else
  385.                 {
  386.                     args += users + " AND " + date;
  387.                 }
  388.                 args += " ORDER BY log.date ASC;";
  389.  
  390.  
  391.  
  392.                 String data = null;
  393.                 Console.WriteLine(args);
  394.                 Console.WriteLine();
  395.  
  396.                 command = connection.CreateCommand();
  397.                 command.CommandText = args;
  398.                 reader = command.ExecuteReader();
  399.  
  400.                 while (reader.Read())
  401.                 {
  402.                     data += "(" + reader["date"].ToString() + ") " + reader["username"].ToString() + " sent " + reader["filename"].ToString() + " which is " + reader["size"].ToString() + " bytes."  + Environment.NewLine;
  403.                 }
  404.  
  405.                 reader.Close();
  406.  
  407.                 return (data);
  408.  
  409.             }
  410.             catch (Exception ex)
  411.             {
  412.                 Console.WriteLine("    >/log ERROR: " + ex.Message);
  413.                 return (null);
  414.  
  415.             }
  416.         }
  417.  
  418.         public void fileTransfer(String userName, String fileName, int fileSize)
  419.         {
  420.             command = connection.CreateCommand();
  421.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  422.             reader = command.ExecuteReader();
  423.             reader.Read();
  424.             String id = reader.GetValue(0).ToString(); // get id
  425.             reader.Close();
  426.  
  427.             command = connection.CreateCommand();
  428.             command.CommandText = "INSERT INTO filetransfers(userid,size,filename,time) VALUES('" + id + "','" + fileSize + "','" + fileName + "',NOW());";
  429.             command.ExecuteNonQuery();
  430.         }
  431.     }
  432. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement