Guest User

Untitled

a guest
May 26th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.97 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.  
  8. struct IRCConfig
  9. {
  10. public string server;
  11. public int port;
  12. public string nick;
  13. public string name;
  14. }
  15.  
  16. class IRCBot
  17. {
  18. TcpClient IRCConnection = null;
  19. IRCConfig config;
  20. NetworkStream ns = null;
  21. StreamReader sr = null;
  22. StreamWriter sw = null;
  23.  
  24. public IRCBot(IRCConfig config)
  25. {
  26. this.config = config;
  27. try
  28. {
  29. IRCConnection = new TcpClient(config.server, config.port);
  30. }
  31. catch
  32. {
  33. Console.WriteLine("Connection Error");
  34. }
  35. try
  36. {
  37. ns = IRCConnection.GetStream();
  38. sr = new StreamReader(ns);
  39. sw = new StreamWriter(ns);
  40. }
  41. catch
  42. {
  43. Console.WriteLine("Communication error");
  44. }
  45. finally
  46. {
  47. if (sr != null)
  48. sr.Close();
  49. if (sw != null)
  50. sw.Close();
  51. if (ns != null)
  52. ns.Close();
  53. if (IRCConnection != null)
  54. IRCConnection.Close();
  55. }
  56. }
  57.  
  58. public void sendData(string cmd, string param)
  59. {
  60. if (param == null)
  61. {
  62. sw.WriteLine(cmd);
  63. sw.Flush();
  64. Console.WriteLine(cmd);
  65. }
  66. else
  67. {
  68. sw.WriteLine(cmd + " " + param);
  69. sw.Flush();
  70. Console.WriteLine(cmd + " " + param);
  71. }
  72. }
  73.  
  74. sendData("USER", config.nick + " japanbot " + " japan" + " :" + config.name);
  75. sendData("NICK", config.nick);
  76.  
  77. public void IRCWork()
  78. {
  79. string[] ex;
  80. string data;
  81. bool shouldRun = true;
  82. while (shouldRun)
  83. {
  84. data = sr.ReadLine();
  85. Console.WriteLine(data);
  86. char[] charSeparator = new char[] { ' ' };
  87. ex = data.Split(charSeparator, 5);
  88. if (ex[0] == "PING")
  89. {
  90. sendData("PONG", ex[1]);
  91. }
  92. }
  93. }
  94.  
  95. if (ex.Length > 4)
  96. { string command = ex[3];
  97. switch (command)
  98. {
  99. case ":!join":
  100. sendData("JOIN", ex[4]);
  101. break;
  102. case ":!say":
  103. sendData("PRIVMSG", ex[2] + " " + ex[4]);
  104. break;
  105. case ":!quit":
  106. sendData("QUIT", ex[4]);
  107. shouldRun = false;
  108. break;
  109. }
  110. }
  111.  
  112. if (ex.Length > 3)
  113. {
  114. string command = ex[3];
  115. switch (command)
  116. {
  117. case ":!part":
  118. sendData("PART", ex[2]);
  119. break;
  120. }
  121. }
  122.  
  123. IRCConfig conf = new IRCConfig();
  124. conf.name = "japanbot";
  125. conf.nick = "japanbot";
  126. conf.port = 6667;
  127. conf.server = "whatever";
  128. new IRCBot(conf);
  129. Console.WriteLine("jews did japanbot never forget");
  130. Console.ReadLine();
Add Comment
Please, Sign In to add comment