Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.36 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.             reader.Close();
  79.             command = connection.CreateCommand();
  80.             command.CommandText = "INSERT INTO channels(channelname,channelpassword) VALUES('" + name + "','" + password + "');";
  81.             command.ExecuteNonQuery();
  82.         }
  83.  
  84.         public int[] getChannelsByUser(String userName)
  85.         {
  86.  
  87.             command = connection.CreateCommand();
  88.             //Get user id
  89.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  90.             reader = command.ExecuteReader();
  91.             reader.Read();
  92.             String id = reader.GetValue(0).ToString();
  93.             reader.Close();
  94.  
  95.             //get number of channels
  96.             command.CommandText = "SELECT count(*) FROM channeluser WHERE userid='" + id + "';";
  97.             reader = command.ExecuteReader();
  98.             reader.Read();
  99.             int count = (int)Convert.ToInt32(reader.GetValue(0));
  100.             reader.Close();
  101.  
  102.             //get channels by ID
  103.             command.CommandText = "SELECT channelid FROM channeluser WHERE userid='" + id + "';";
  104.             reader = command.ExecuteReader();
  105.             int[] channels = new int[count];
  106.  
  107.             int loopCount = 0;
  108.             while (reader.Read())
  109.             {
  110.                 channels[loopCount] = (int)reader["channelid"];
  111.                 loopCount++;
  112.             }
  113.             reader.Close();
  114.  
  115.             return (channels);
  116.         }
  117.  
  118.         public int joinChannel(String userName, String channelName, String channelPassword)
  119.         {
  120.             reader.Close();
  121.             command = connection.CreateCommand();
  122.  
  123.             //Get user id
  124.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  125.             reader = command.ExecuteReader();
  126.             reader.Read();
  127.             String id = reader.GetValue(0).ToString();
  128.             reader.Close();
  129.  
  130.             //Get channel id
  131.             command.CommandText = "SELECT id FROM channels WHERE channelname='" + channelName + "';";
  132.             reader = command.ExecuteReader();
  133.             //check if id exists
  134.             String channelid = "-1";
  135.             if (reader.Read())
  136.             {
  137.                 channelid = reader.GetValue(0).ToString();
  138.             }
  139.             reader.Close();
  140.  
  141.             //Get channel password
  142.             command.CommandText = "SELECT channelpassword FROM channels WHERE channelname='" + channelName + "';";
  143.             reader = command.ExecuteReader();
  144.             reader.Read();
  145.             String channelpass = "¤";
  146.             if (reader.Read())
  147.             {
  148.                 channelpass = reader.GetValue(0).ToString();
  149.             }
  150.             reader.Close();
  151.  
  152.             //check if user is already in the channel
  153.             command.CommandText = "SELECT * FROM channeluser WHERE (channelid='" + channelid + "') AND (userid='" + id + "');";
  154.             reader = command.ExecuteReader();
  155.             if (!reader.Read())
  156.             {
  157.                 reader.Close();
  158.                 if (channelpass == channelPassword)
  159.                 {
  160.                     //Get user id
  161.                     command.CommandText = "INSERT INTO channeluser(channelid,userid) VALUES(" + channelid + "," + id + ");";
  162.                     command.ExecuteNonQuery();
  163.                     return (0);
  164.                 }
  165.                 else
  166.                 {
  167.                     return (1);
  168.                 }
  169.             }
  170.             reader.Close();
  171.             return (2);
  172.         }
  173.  
  174.         public int leaveChannel(String userName, String channelIndex)
  175.         {
  176.             int result = 0;
  177.             reader.Close();
  178.             command = connection.CreateCommand();
  179.  
  180.             //Get user id
  181.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  182.             reader = command.ExecuteReader();
  183.             reader.Read();
  184.             String id = reader.GetValue(0).ToString();
  185.             reader.Close();
  186.  
  187.             try
  188.             {
  189.                 //leave channel
  190.                 command.CommandText = "DELETE FROM channeluser WHERE (channelid='" + channelIndex + "') AND (userid='" + id + "');";
  191.                 command.ExecuteNonQuery();
  192.                 result = 0;
  193.             }
  194.             catch
  195.             {
  196.                 result = 1;
  197.             }
  198.  
  199.             return result;
  200.         }
  201.  
  202.         public String[] getChannelList()
  203.         {
  204.             String[] channels;
  205.  
  206.             //get number of channels
  207.             command.CommandText = "SELECT count(*) FROM channels;";
  208.             reader = command.ExecuteReader();
  209.             reader.Read();
  210.             int count = (int)Convert.ToInt32(reader.GetValue(0));
  211.             reader.Close();
  212.  
  213.             channels = new String[count];
  214.  
  215.             int loopCount = 0;
  216.             command.CommandText = "SELECT channelname FROM channels;";
  217.             reader = command.ExecuteReader();
  218.             while (reader.Read())
  219.             {
  220.                 channels[loopCount] = (string)reader["channelname"];
  221.                 loopCount++;
  222.             }
  223.  
  224.             return channels;
  225.         }
  226.  
  227.         public String getUserFromID(String userid)
  228.         {
  229.             command = connection.CreateCommand();
  230.             command.CommandText = "SELECT username FROM users WHERE id='" + userid + "';";
  231.             reader = command.ExecuteReader();
  232.             reader.Read();
  233.             String name = reader.GetValue(0).ToString(); // get name
  234.             reader.Close();
  235.             return name;
  236.         }
  237.  
  238.         public void newMessage(String userName, String msg)
  239.         {
  240.             command = connection.CreateCommand();
  241.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  242.             reader = command.ExecuteReader();
  243.             reader.Read();
  244.             String id = reader.GetValue(0).ToString(); // get id
  245.             reader.Close();
  246.  
  247.             command.CommandText = "INSERT INTO log(userid,message,date) VALUES(" + id + ",'" + msg + "',NOW());";
  248.             command.ExecuteNonQuery();
  249.             command.CommandText = "UPDATE users SET sent = sent + 1 WHERE id=" + id + ";";
  250.             command.ExecuteNonQuery();
  251.         }
  252.  
  253.         public string getLog(String input)
  254.         {
  255.             try
  256.             {
  257.                 String[] arguments = input.Split('-');
  258.  
  259.                 String users = null;
  260.                 bool usersb = false;
  261.                 String date = null;
  262.                 bool dateb = false;
  263.  
  264.                 foreach (string argument in arguments)
  265.                 {
  266.                     if (argument.Substring(0, 1) == "u")
  267.                     {
  268.                         String[] s;
  269.                         String noArg = argument.Substring(argument.IndexOf('u') + 1);
  270.                         if (argument.Contains(','))
  271.                         {
  272.  
  273.                             s = noArg.Split(',');
  274.                             users = "users.username='" + s[0].Trim() + "'";
  275.  
  276.                             for (int i = 1; i < s.Length; i++)
  277.                             {
  278.                                 users += " OR users.username='" + s[i].Trim() + "'";
  279.                             }
  280.  
  281.                         }
  282.                         else
  283.                         {
  284.                             users = "users.username='" + noArg.Trim() + "'";
  285.                         }
  286.                         usersb = true;
  287.  
  288.                     }
  289.                     else if (argument.Substring(0, 1) == "d")
  290.                     {
  291.                         String noArg = argument.Substring(argument.IndexOf('d') + 1).Trim();
  292.                         dateb = true;
  293.                         if (argument.Contains(','))
  294.                         {
  295.                             String[] d;
  296.                             int argCount = 0;
  297.                             d = noArg.Split(',');
  298.                             foreach (String dt in d)
  299.                             {
  300.                                 if (dt.Contains('*'))
  301.                                 {
  302.                                     String[] dtsplit = dt.Split('*');
  303.                                     foreach (String dts in dtsplit)
  304.                                     {
  305.                                         dts.Trim();
  306.                                     }
  307.                                     if (argCount == 0)
  308.                                     {
  309.                                         date += "log.date BETWEEN CAST '" + dtsplit[0] + "00' AS DATETIME AND CAST'" + dtsplit[1] + "00' DATETIME";
  310.                                         argCount++;
  311.                                     }
  312.                                     else
  313.                                     {
  314.                                         date += "OR log.date BETWEEN CAST '" + dtsplit[0] + "00' DATETIME AND CAST '" + dtsplit[1] + "00' DATETIME";
  315.                                     }
  316.  
  317.                                 }
  318.                                 else
  319.                                 {
  320.                                     if (argCount == 0)
  321.                                     {
  322.                                         date += "log.date BETWEEN CAST '" + dt + "00' DATETIME AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  323.                                         argCount++;
  324.                                     }
  325.                                     else
  326.                                     {
  327.                                         date += " OR log.date BETWEEN '" + dt + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  328.                                     }
  329.                                 }
  330.                             }
  331.                         }
  332.                         else
  333.                         {
  334.                             int argCount = 0;
  335.                             String d = noArg;
  336.                             if (d.Contains('*'))
  337.                             {
  338.                                 String[] dtsplit = d.Split('*');
  339.                                 foreach (String dts in dtsplit)
  340.                                 {
  341.                                     dts.Trim();
  342.                                 }
  343.                                 if (argCount == 0)
  344.                                 {
  345.                                     date += "log.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  346.                                     argCount++;
  347.                                 }
  348.                                 else
  349.                                 {
  350.                                     date += "OR log.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  351.                                 }
  352.  
  353.                             }
  354.                             else
  355.                             {
  356.                                 if (argCount == 0)
  357.                                 {
  358.                                     date += "log.date BETWEEN '" + d + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  359.                                     argCount++;
  360.                                 }
  361.                                 else
  362.                                 {
  363.                                     date += " OR log.date BETWEEN '" + d + "'00 AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  364.                                 }
  365.                             }
  366.                         }
  367.                     }
  368.  
  369.                 }
  370.                 String args = "SELECT users.username, log.message, log.date FROM log INNER JOIN users ON users.id=log.userid WHERE ";
  371.                 if (!usersb && dateb)
  372.                 {
  373.                     args += date;
  374.                 }
  375.                 else if (usersb && !dateb)
  376.                 {
  377.                     args += users;
  378.                 }
  379.                 else
  380.                 {
  381.                     args += users + " AND " + date;
  382.                 }
  383.                 args += " ORDER BY log.date ASC;";
  384.  
  385.  
  386.  
  387.                 String data = null;
  388.                 Console.WriteLine(args);
  389.                 Console.WriteLine();
  390.  
  391.                 command = connection.CreateCommand();
  392.                 command.CommandText = args;
  393.                 reader = command.ExecuteReader();
  394.  
  395.                 while (reader.Read())
  396.                 {
  397.                     data += "(" + reader["date"].ToString() + ") <" + reader["username"].ToString() + "> " + reader["message"].ToString() + Environment.NewLine;
  398.                 }
  399.  
  400.                 reader.Close();
  401.  
  402.                 return (data);
  403.  
  404.             }
  405.             catch (Exception ex)
  406.             {
  407.                 Console.WriteLine("    >/log ERROR: " + ex.Message);
  408.                 return (null);
  409.  
  410.             }
  411.         }
  412.  
  413.         public string getFileLog(String input)
  414.         {
  415.             try
  416.             {
  417.                 String[] arguments = input.Split('-');
  418.  
  419.                 String users = null;
  420.                 bool usersb = false;
  421.                 String date = null;
  422.                 bool dateb = false;
  423.                 String fileinfo = null;
  424.                 bool fileinfob = false;
  425.  
  426.                 foreach (string argument in arguments)
  427.                 {
  428.                     if (argument.Substring(0, 1) == "u")
  429.                     {
  430.                         String[] s;
  431.                         String noArg = argument.Substring(argument.IndexOf('u') + 1);
  432.                         if (argument.Contains(','))
  433.                         {
  434.  
  435.                             s = noArg.Split(',');
  436.                             users = "users.username='" + s[0].Trim() + "'";
  437.  
  438.                             for (int i = 1; i < s.Length; i++)
  439.                             {
  440.                                 users += " OR users.username='" + s[i].Trim() + "'";
  441.                             }
  442.  
  443.                         }
  444.                         else
  445.                         {
  446.                             users = "users.username='" + noArg.Trim() + "'";
  447.                         }
  448.                         usersb = true;
  449.  
  450.                     }
  451.                     else if (argument.Substring(0, 1) == "f")
  452.                     {
  453.                         String[] f;
  454.                         String noArg = argument.Substring(argument.IndexOf('f') + 1);
  455.                         if (argument.Contains(','))
  456.                         {
  457.  
  458.                             f = noArg.Split(',');
  459.                             fileinfo = "filetransfers.filename='" + f[0].Trim() + "'";
  460.  
  461.                             for (int i = 1; i < f.Length; i++)
  462.                             {
  463.                                 fileinfo += " OR filetransfers.filename='" + f[i].Trim() + "'";
  464.                             }
  465.  
  466.                         }
  467.                         else
  468.                         {
  469.                             fileinfo = "filetransfers.filename='" + noArg.Trim() + "'";
  470.                         }
  471.                         fileinfob = true;
  472.  
  473.                     }
  474.                     else if (argument.Substring(0, 1) == "d")
  475.                     {
  476.                         String noArg = argument.Substring(argument.IndexOf('d') + 1).Trim();
  477.                         dateb = true;
  478.                         if (argument.Contains(','))
  479.                         {
  480.                             String[] d;
  481.                             int argCount = 0;
  482.                             d = noArg.Split(',');
  483.                             foreach (String dt in d)
  484.                             {
  485.                                 if (dt.Contains('*'))
  486.                                 {
  487.                                     String[] dtsplit = dt.Split('*');
  488.                                     foreach (String dts in dtsplit)
  489.                                     {
  490.                                         dts.Trim();
  491.                                     }
  492.                                     if (argCount == 0)
  493.                                     {
  494.                                         date += "filetransfers.date BETWEEN CAST '" + dtsplit[0] + "00' AS DATETIME AND CAST'" + dtsplit[1] + "00' DATETIME";
  495.                                         argCount++;
  496.                                     }
  497.                                     else
  498.                                     {
  499.                                         date += "OR filetransfers.date BETWEEN CAST '" + dtsplit[0] + "00' DATETIME AND CAST '" + dtsplit[1] + "00' DATETIME";
  500.                                     }
  501.  
  502.                                 }
  503.                                 else
  504.                                 {
  505.                                     if (argCount == 0)
  506.                                     {
  507.                                         date += "filetransfers.date BETWEEN CAST '" + dt + "00' DATETIME AND DATE_ADD(CAST('" + dt + "00' AS datetime), INTERVAL +5 MINUTE)";
  508.                                         argCount++;
  509.                                     }
  510.                                     else
  511.                                     {
  512.                                         date += " OR filetransfers.date BETWEEN '" + dt + "00' AND DATE_ADD(CAST('" + dt + "00' AS datetime), INTERVAL +5 MINUTE)";
  513.                                     }
  514.                                 }
  515.                             }
  516.                         }
  517.                         else
  518.                         {
  519.                             int argCount = 0;
  520.                             String d = noArg;
  521.                             if (d.Contains('*'))
  522.                             {
  523.                                 String[] dtsplit = d.Split('*');
  524.                                 foreach (String dts in dtsplit)
  525.                                 {
  526.                                     dts.Trim();
  527.                                 }
  528.                                 if (argCount == 0)
  529.                                 {
  530.                                     date += "filetransfers.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  531.                                     argCount++;
  532.                                 }
  533.                                 else
  534.                                 {
  535.                                     date += "OR filetransfers.date BETWEEN '" + dtsplit[0] + "00' AND '20" + dtsplit[1] + "'";
  536.                                 }
  537.  
  538.                             }
  539.                             else
  540.                             {
  541.                                 if (argCount == 0)
  542.                                 {
  543.                                     date += "filetransfers.date BETWEEN '" + d + "00' AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  544.                                     argCount++;
  545.                                 }
  546.                                 else
  547.                                 {
  548.                                     date += " OR filetransfers.date BETWEEN '" + d + "'00 AND DATE_ADD(CAST('" + d + "00' AS datetime), INTERVAL +5 MINUTE)";
  549.                                 }
  550.                             }
  551.                         }
  552.                     }
  553.                 }
  554.                 String args = "SELECT users.username, filetransfers.filename, filetransfers.recieverID, filetransfers.size, filetransfers.date FROM filetransfers INNER JOIN users ON users.id=filetransfers.userid WHERE ";
  555.                 int argcount = 0;
  556.                 if (usersb)
  557.                 {
  558.                     if (argcount == 0)
  559.                     {
  560.                         args += users;
  561.                         argcount++;
  562.                     } else
  563.                     {
  564.                         args += " AND " + users;
  565.                     }
  566.                 }
  567.                 if (dateb)
  568.                 {
  569.                     if (argcount == 0)
  570.                     {
  571.                         args += date;
  572.                         argcount++;
  573.                     } else
  574.                     {
  575.                         args += " AND " + date;
  576.                     }
  577.                 }
  578.                 if (fileinfob)
  579.                 {
  580.                     if (argcount == 0)
  581.                     {
  582.                         args += fileinfo;
  583.                         argcount++;
  584.                     } else
  585.                     {
  586.                         args += " AND " + fileinfo;
  587.                     }
  588.                 }
  589.                
  590.                 args += " ORDER BY log.date ASC;";
  591.  
  592.  
  593.  
  594.                 String data = null;
  595.                 Console.WriteLine(args);
  596.                 Console.WriteLine();
  597.  
  598.                 command = connection.CreateCommand();
  599.                 command.CommandText = args;
  600.                 reader = command.ExecuteReader();
  601.  
  602.                 while (reader.Read())
  603.                 {
  604.                     data += "(" + reader["date"].ToString() + ") " + reader["username"].ToString() + " sent " + reader["filename"].ToString() + " which is " + reader["size"].ToString() + " bytes to " getUserFromID(reader["recieverID"].ToString()) + "." + Environment.NewLine;
  605.                 }
  606.  
  607.                 reader.Close();
  608.  
  609.                 return (data);
  610.  
  611.             }
  612.             catch (Exception ex)
  613.             {
  614.                 Console.WriteLine("    >/log ERROR: " + ex.Message);
  615.                 return (null);
  616.  
  617.             }
  618.         }
  619.  
  620.         public void fileTransfer(String userName,String toUser, String fileName, int fileSize)
  621.         {
  622.             command = connection.CreateCommand();
  623.             command.CommandText = "SELECT id FROM users WHERE username='" + userName + "';";
  624.             reader = command.ExecuteReader();
  625.             reader.Read();
  626.             String id = reader.GetValue(0).ToString(); // get id
  627.             reader.Close();
  628.  
  629.             command = connection.CreateCommand();
  630.             command.CommandText = "INSERT INTO filetransfers(userid,size,filename,time) VALUES('" + id + "','" + fileSize + "','" + fileName + "',NOW());";
  631.             command.ExecuteNonQuery();
  632.         }
  633.     }
  634. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement