Guest User

Untitled

a guest
Feb 25th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.01 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Net.Sockets;
  6. using System.IO;
  7. using System.Threading;
  8. using System.Data;
  9. using Newtonsoft.Json;
  10. using System.Net;
  11. using System.Diagnostics;
  12. using System.Text.RegularExpressions;
  13.  
  14.  
  15. namespace Gambly
  16. {
  17. public class IrcClient
  18. {
  19. private string userName = "";
  20. private TcpClient tcpClient;
  21. private StreamReader inputStream;
  22. private StreamWriter outputStream;
  23. private string channel = "";
  24. private bool IS_CONNECTED = false;
  25. private Thread listen, count, sender,pingsender;
  26. private string toFind;
  27. static List<Gambler> gamblers;
  28. static Entity viewers;
  29. private Queue<String> commands;
  30. private Config configs;
  31.  
  32. public IrcClient(string ip, int port, string userName, string password, string channel)
  33. {
  34. try
  35. {
  36. Console.Write(Config.time() + ">> Loading IRClient");
  37. this.userName = userName;
  38. gamblers = new List<Gambler>();
  39. tcpClient = new TcpClient(ip, port);
  40. inputStream = new StreamReader(tcpClient.GetStream());
  41. configs = new Config();
  42. viewers = new Entity();
  43. commands = new Queue<string>();
  44. outputStream = new StreamWriter(tcpClient.GetStream());
  45. this.channel = channel;
  46.  
  47. outputStream.WriteLine("PASS " + password);
  48. outputStream.WriteLine("NICK " + userName);
  49. outputStream.WriteLine("USER " + userName + " 8 * :" + userName);
  50.  
  51. outputStream.Flush();
  52.  
  53. toFind = "#" + channel + " :";
  54. Console.WriteLine("...done!");
  55.  
  56. LoadFiles();
  57.  
  58. listen = new Thread(ChatListener);
  59. listen.Start();
  60.  
  61. sender = new Thread(ChatSender);
  62. sender.Start();
  63.  
  64. count = new Thread(GetViewers);
  65. count.Start();
  66.  
  67. sendChatMessage("I'm back bitches!");
  68. }
  69. catch (Exception)
  70. {
  71. Console.WriteLine("Connection has been lost");
  72. }
  73.  
  74.  
  75. }
  76.  
  77. public void LoadFiles()
  78. {
  79. try
  80. {
  81. var file = System.IO.File.ReadAllText(configs.CONFIG_FILE);
  82. gamblers = JsonConvert.DeserializeObject<List<Gambler>>(file);
  83. }
  84. catch (Exception)
  85. {
  86. Console.WriteLine("file error");
  87. }
  88. }
  89.  
  90.  
  91. public void SaveFiles()
  92. {
  93. try
  94. {
  95. var file = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
  96. System.IO.File.WriteAllText(configs.CONFIG_FILE, file);
  97. }
  98. catch (Exception)
  99. {
  100. Console.WriteLine("file");
  101. }
  102. }
  103. public bool getBool()
  104. {
  105. return IS_CONNECTED;
  106. }
  107.  
  108. void ChatListener()
  109. {
  110. joinRoom(channel);
  111. while (true)
  112. {
  113.  
  114.  
  115. try
  116. {
  117. String message = readMessage();
  118. Console.WriteLine(message);
  119. if (message.Contains("PING"))
  120. {
  121. sendIrcMessage("PONG :tmi.twitch.tv\r\n");
  122. sendIrcMessage("PONG");
  123. sendIrcMessage("PING");
  124. sendIrcMessage("PING :tmi.twitch.tv\r\n");
  125. pongChat();
  126. }
  127.  
  128. if (message.Contains("PRIVMSG"))
  129. {
  130.  
  131. var myText = message.Substring(message.IndexOf(toFind) + toFind.Length);
  132. //listBox1.Invoke(new Action(() => textBox1.AppendText(texto)));
  133. if (myText.StartsWith("!"))
  134. {
  135. commands.Enqueue(message);
  136. Console.WriteLine(Config.time() + "queued command! by:" + getUser(message));
  137.  
  138. }
  139. }
  140.  
  141. }
  142. catch (Exception a)
  143. {
  144. Console.WriteLine("ChatListener failed " + a.Message);
  145. }
  146. }
  147. }
  148. void PingSender()
  149. {
  150. while (true)
  151. {
  152. try
  153. {
  154. sendIrcMessage("PONG :tmi.twitch.tv\r\n");
  155. Thread.Sleep(60000 * 5);
  156. }
  157. catch (Exception)
  158. {
  159. Console.WriteLine("erro ping");
  160. }
  161.  
  162.  
  163. }
  164. }
  165. void ChatSender()
  166. {
  167. while (true)
  168. {
  169. Thread.Sleep(320);
  170. try
  171. {
  172.  
  173. if (commands.Count > 0)
  174. {
  175. string ex = commands.Dequeue();
  176. var myText = ex.Substring(ex.IndexOf(toFind) + toFind.Length);
  177. var text = ex.Split(';');
  178. var user = getUser(text[2]);
  179.  
  180. if (myText.Contains("!coins"))
  181. {
  182. Console.WriteLine(user);
  183. var op = gamblers.Find(x => x.name == user.ToLower());
  184. sendChatMessage(op.name + " have " + op.points.ToString("N0") + " Bcoins");
  185. }
  186.  
  187. if (myText.Contains("!time"))
  188. {
  189. var stp = new Stopwatch();
  190. stp.Start();
  191. var op = gamblers.FirstOrDefault(x => x.name == user.ToLower());
  192. TimeSpan guess = DateTime.Now - op.register;
  193. sendChatMessage(String.Format("{0} days, {1} hours, {2} minutes.", guess.Days, guess.Hours, guess.Minutes));
  194. stp.Stop();
  195. Console.WriteLine(stp.Elapsed);
  196.  
  197. }
  198.  
  199. }
  200. }
  201. catch (Exception a)
  202. {
  203. Console.WriteLine(a.Message);
  204. }
  205. }
  206. }
  207. void GetViewers()
  208. {
  209. while (true)
  210. {
  211. try
  212. {
  213. using (WebClient client = new WebClient())
  214. {
  215. // Console.Write(Config.time() + ">> Loading Viewers"); // *ch
  216.  
  217. client.Headers["Client-ID"] = "tg7b4plvzsngwrwnjuj1ay5gvkryv6"; // Deve ser fornecida pelo usuario!
  218. client.Encoding = Encoding.UTF8;
  219. var s = client.DownloadString("https://tmi.twitch.tv/group/user/" + channel + "/chatters");
  220. var viewers = JsonConvert.DeserializeObject<Entity>(s);
  221. //viewers.chatters.viewers.Concat(viewers.chatters.moderators);
  222. viewers.chatters.viewers.AddRange(viewers.chatters.moderators);
  223. viewers.chatters.viewers.ForEach(viewer =>
  224. {
  225. if (gamblers.Exists(x => x.name == viewer))
  226. {
  227. // Console.WriteLine(Config.time() + " Já existe!");
  228. var op = gamblers.Find(x => x.name == viewer);
  229. op.points += configs.POINT_REWARD;
  230. }
  231. else
  232. {
  233. gamblers.Add(new Gambler(viewer, DateTime.Now, 0));
  234. }
  235. });
  236. //Console.WriteLine("...done! {0} New Viewers {1}", i, stp.Elapsed);
  237. s = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
  238. System.IO.File.WriteAllText(configs.CONFIG_FILE, s);
  239. }
  240.  
  241. Thread.Sleep(60000 * 10);
  242. }
  243. catch (Exception z)
  244. {
  245. Console.WriteLine(Config.time() + "[ERROR]" + z.Message + z.InnerException);
  246. }
  247. }
  248. }
  249.  
  250. public void joinRoom(string channel)
  251. {
  252. this.channel = channel;
  253. outputStream.WriteLine("JOIN #" + channel + "\r\n"); //
  254. outputStream.WriteLine("CAP REQ :twitch.tv/tags\r\n");
  255. outputStream.Flush();
  256. }
  257.  
  258.  
  259.  
  260. public void leaveRoom(string channel)
  261. {
  262. outputStream.WriteLine("PART #" + channel + "");
  263. IS_CONNECTED = false;
  264. outputStream.Flush();
  265. }
  266.  
  267. public void sendIrcMessage(string message)
  268. {
  269. outputStream.WriteLine(message);
  270. outputStream.Flush();
  271. }
  272.  
  273. public void sendChatMessage(string message)
  274. {
  275. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :" + message + "\r\n");
  276. //sendIrcMessage(":[email protected] PRIVMSG #barbosza: HAHA!");
  277. }
  278.  
  279. public void sendPrivateMessage(string message, string user)
  280. {
  281. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :/w " + user + " " + message + "\r\n");
  282. //sendIrcMessage(":[email protected] PRIVMSG #barbosza: HAHA!");
  283. }
  284.  
  285. public void deleteChatMessage(string username)
  286. {
  287. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :.timeout " + username + " 1\r\n");
  288. }
  289.  
  290. public void banClient(string username)
  291. {
  292. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PRIVMSG #" + channel + " :.ban " + username + "\r\n");
  293. }
  294.  
  295. public void pongChat()
  296. {
  297. sendIrcMessage(":" + userName + "!" + userName + "@" + userName + ".tmi.twitch.tv PONG " + channel + "\r\n");
  298. }
  299.  
  300.  
  301. public string getChannel()
  302. {
  303. return this.channel;
  304. }
  305.  
  306. public string readMessage()
  307. {
  308. string message;
  309. try
  310. {
  311. message = inputStream.ReadLine();
  312. if (message == null)
  313. {
  314. return "null";
  315. }
  316. return message;
  317. }
  318. catch (Exception)
  319. {
  320. return "null";
  321. }
  322. }
  323.  
  324.  
  325. public string thisChannel(string a)
  326. {
  327. return this.channel;
  328. }
  329.  
  330.  
  331. public string getUser(string message)
  332. {
  333. try
  334. {
  335. return message.Substring(message.LastIndexOf('=') + 1);
  336. }
  337. catch (Exception a)
  338. {
  339. return "[ERROR] Cannot get user!" + a.Message;
  340. }
  341. }
  342.  
  343. public void runCMD(string text)
  344. {
  345. try
  346. {
  347.  
  348. if (text.Contains("csv"))
  349. {
  350.  
  351. using (StreamWriter fp = new StreamWriter(@"A:\gamblers.csv"))
  352. {
  353. foreach (Gambler a in gamblers)
  354. {
  355. fp.WriteLine("{0}&{1}&{2}&{3}", a.name, a.points, a.register, a.prison_time);
  356. }
  357. fp.Close();
  358. }
  359. Console.WriteLine(Config.time() + "CSV file was exported succcesfully");
  360. }
  361. else if (text.StartsWith("add"))
  362. {
  363. string[] args = text.Split(' ');
  364. var opz = gamblers.FirstOrDefault(X => X.name == args[1].ToLower());
  365. if (opz == null) return;
  366. opz.points += int.Parse(args[2]);
  367. Console.WriteLine(Config.time() + "{0} added {1} points to {2}", "You", args[2], args[1]);
  368.  
  369. }
  370. else if (text.StartsWith("tax"))
  371. {
  372. string[] args = text.Split(' ');
  373. float tax_value = float.Parse(args[1]) / 100;
  374. gamblers.ForEach(X => X.points = Convert.ToInt64(X.points * (1 - tax_value)));
  375. }
  376. else if (text.Contains("quit"))
  377. {
  378. var s = JsonConvert.SerializeObject(gamblers, Formatting.Indented);
  379. System.IO.File.WriteAllText(configs.CONFIG_FILE, s);
  380. System.Environment.Exit(0);
  381. }
  382. else if (text.Contains("ban"))
  383. {
  384. string[] cmd = text.Split(' ');
  385. var opz = gamblers.FirstOrDefault(X => X.name == cmd[1].ToLower());
  386. if (opz == null) return;
  387. var op = new DateTime();
  388. op = DateTime.Now.AddDays(double.Parse(cmd[2]));
  389. opz.prison_time = op;
  390. banClient(cmd[1].ToLower());
  391. sendChatMessage(String.Format("{0} was banished for {1} days", cmd[1], cmd[2]));
  392. }
  393. else
  394. {
  395. Console.WriteLine("Comando Inválido!");
  396. }
  397.  
  398.  
  399. }
  400. catch (Exception f)
  401. {
  402. Console.WriteLine(Config.time() + f.Message);
  403. }
  404. }
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment