Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 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=someIP;"
  32. + "database=somedatabase;"
  33. + "uid=someuser;"
  34. + "password=somepassword;";
  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 void fileTransfer(String userName, String fileName, int fileSize)
  259. {
  260. command = connection.CreateCommand();
  261. command.CommandText = "INSERT INTO filetransfers(username,size,filename,time) VALUES('" + userName + "','" + fileSize + "','" + fileName + "',NOW());";
  262. command.ExecuteNonQuery();
  263. }
  264. }
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement