Advertisement
Guest User

Untitled

a guest
Aug 19th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. using System.IO;
  5. using System.Threading;
  6.  
  7. namespace System.Net {
  8. #region Delegates
  9. public delegate void CommandReceived(string IrcCommand);
  10. public delegate void TopicSet(string IrcChannel, string IrcTopic);
  11. public delegate void TopicOwner(string IrcChannel, string IrcUser, string TopicDate);
  12. public delegate void NamesList(string UserNames);
  13. public delegate void ServerMessage(string ServerMessage);
  14. public delegate void Join(string IrcChannel, string IrcUser);
  15. public delegate void Part(string IrcChannel, string IrcUser);
  16. public delegate void Mode(string IrcChannel, string IrcUser, string UserMode);
  17. public delegate void NickChange(string UserOldNick, string UserNewNick);
  18. public delegate void Kick(string IrcChannel, string UserKicker, string UserKicked, string KickMessage);
  19. public delegate void Quit(string UserQuit, string QuitMessage);
  20. #endregion
  21.  
  22. public class IRC {
  23. #region Events
  24. public event CommandReceived eventReceiving;
  25. public event TopicSet eventTopicSet;
  26. public event TopicOwner eventTopicOwner;
  27. public event NamesList eventNamesList;
  28. public event ServerMessage eventServerMessage;
  29. public event Join eventJoin;
  30. public event Part eventPart;
  31. public event Mode eventMode;
  32. public event NickChange eventNickChange;
  33. public event Kick eventKick;
  34. public event Quit eventQuit;
  35. #endregion
  36.  
  37. #region Private Variables
  38. private string ircServer;
  39. private int ircPort;
  40. private string ircNick;
  41. private string ircUser;
  42. private string ircRealName;
  43. private string ircChannel;
  44. private bool isInvisible;
  45. private TcpClient ircConnection;
  46. private NetworkStream ircStream;
  47. private StreamWriter ircWriter;
  48. private StreamReader ircReader;
  49. #endregion
  50.  
  51. #region Properties
  52. public string IrcServer {
  53. get { return this.ircServer; }
  54. set { this.ircServer = value; }
  55. } /* IrcServer */
  56.  
  57. public int IrcPort {
  58. get { return this.ircPort; }
  59. set { this.ircPort = value; }
  60. } /* IrcPort */
  61.  
  62. public string IrcNick {
  63. get { return this.ircNick; }
  64. set { this.ircNick = value; }
  65. } /* IrcNick */
  66.  
  67. public string IrcUser {
  68. get { return this.ircUser; }
  69. set { this.ircUser = value; }
  70. } /* IrcUser */
  71.  
  72. public string IrcRealName {
  73. get { return this.ircRealName; }
  74. set { this.ircRealName = value; }
  75. } /* IrcRealName */
  76.  
  77. public string IrcChannel {
  78. get { return this.ircChannel; }
  79. set { this.ircChannel = value; }
  80. } /* IrcChannel */
  81.  
  82. public bool IsInvisble {
  83. get { return this.isInvisible; }
  84. set { this.isInvisible = value; }
  85. } /* IsInvisible */
  86.  
  87. public TcpClient IrcConnection {
  88. get { return this.ircConnection; }
  89. set { this.ircConnection = value; }
  90. } /* IrcConnection */
  91.  
  92. public NetworkStream IrcStream {
  93. get { return this.ircStream; }
  94. set { this.ircStream = value; }
  95. } /* IrcStream */
  96.  
  97. public StreamWriter IrcWriter {
  98. get { return this.ircWriter; }
  99. set { this.ircWriter = value; }
  100. } /* IrcWriter */
  101.  
  102. public StreamReader IrcReader {
  103. get { return this.ircReader; }
  104. set { this.ircReader = value; }
  105. } /* IrcReader */
  106. #endregion
  107.  
  108. #region Constructor
  109. public IRC(string IrcNick, string IrcChannel) {
  110. this.IrcNick = IrcNick;
  111. this.IrcUser = System.Environment.MachineName;
  112. this.IrcRealName = "Orochi Bot";
  113. this.IrcChannel = IrcChannel;
  114. this.IsInvisble = false;
  115. } /* IRC */
  116. #endregion
  117.  
  118. #region Public Methods
  119. public void Connect(string IrcServer, int IrcPort) {
  120. this.IrcServer = IrcServer;
  121. this.IrcPort = IrcPort;
  122.  
  123. // Connect with the IRC server.
  124. this.IrcConnection = new TcpClient(this.IrcServer, this.IrcPort);
  125. this.IrcStream = this.IrcConnection.GetStream();
  126. this.IrcReader = new StreamReader(this.IrcStream);
  127. this.IrcWriter = new StreamWriter(this.IrcStream);
  128.  
  129. // Authenticate our user
  130. string isInvisible = this.IsInvisble ? "8" : "0";
  131. this.IrcWriter.WriteLine(String.Format("USER {0} {1} * :{2}", this.IrcUser, isInvisible, this.IrcRealName));
  132. this.IrcWriter.Flush();
  133. this.IrcWriter.WriteLine(String.Format("NICK {0}", this.IrcNick));
  134. this.IrcWriter.Flush();
  135. this.IrcWriter.WriteLine(String.Format("JOIN {0}", this.IrcChannel));
  136. this.IrcWriter.Flush();
  137.  
  138. // Listen for commands
  139. while (true) {
  140. string ircCommand;
  141. while ((ircCommand = this.IrcReader.ReadLine()) != null) {
  142. if (eventReceiving != null) { this.eventReceiving(ircCommand); }
  143.  
  144. string[] commandParts = new string[ircCommand.Split(' ').Length];
  145. commandParts = ircCommand.Split(' ');
  146. if (commandParts[0].Substring(0, 1) == ":") {
  147. commandParts[0] = commandParts[0].Remove(0, 1);
  148. }
  149.  
  150. if (commandParts[0] == this.IrcServer) {
  151. // Server message
  152. switch (commandParts[1]) {
  153. case "332": this.IrcTopic(commandParts); break;
  154. case "333": this.IrcTopicOwner(commandParts); break;
  155. case "353": this.IrcNamesList(commandParts); break;
  156. case "366": /*this.IrcEndNamesList(commandParts);*/ break;
  157. case "372": /*this.IrcMOTD(commandParts);*/ break;
  158. case "376": /*this.IrcEndMOTD(commandParts);*/ break;
  159. default: this.IrcServerMessage(commandParts); break;
  160. }
  161. } else if (commandParts[0] == "PING") {
  162. // Server PING, send PONG back
  163. this.IrcPing(commandParts);
  164. } else {
  165. // Normal message
  166. string commandAction = commandParts[1];
  167. switch (commandAction) {
  168. case "JOIN": this.IrcJoin(commandParts); break;
  169. case "PART": this.IrcPart(commandParts); break;
  170. case "MODE": this.IrcMode(commandParts); break;
  171. case "NICK": this.IrcNickChange(commandParts); break;
  172. case "KICK": this.IrcKick(commandParts); break;
  173. case "QUIT": this.IrcQuit(commandParts); break;
  174. }
  175. }
  176. }
  177.  
  178. this.IrcWriter.Close();
  179. this.IrcReader.Close();
  180. this.IrcConnection.Close();
  181. }
  182. } /* Connect */
  183. #endregion
  184.  
  185. #region Private Methods
  186. #region Server Messages
  187. private void IrcTopic(string[] IrcCommand) {
  188. string IrcChannel = IrcCommand[3];
  189. string IrcTopic = "";
  190. for (int intI = 4; intI < IrcCommand.Length; intI++) {
  191. IrcTopic += IrcCommand[intI] + " ";
  192. }
  193. if (eventTopicSet != null) { this.eventTopicSet(IrcChannel, IrcTopic.Remove(0, 1).Trim()); }
  194. } /* IrcTopic */
  195.  
  196. private void IrcTopicOwner(string[] IrcCommand) {
  197. string IrcChannel = IrcCommand[3];
  198. string IrcUser = IrcCommand[4].Split('!')[0];
  199. string TopicDate = IrcCommand[5];
  200. if (eventTopicOwner != null) { this.eventTopicOwner(IrcChannel, IrcUser, TopicDate); }
  201. } /* IrcTopicOwner */
  202.  
  203. private void IrcNamesList(string[] IrcCommand) {
  204. string UserNames = "";
  205. for (int intI = 5; intI < IrcCommand.Length; intI++) {
  206. UserNames += IrcCommand[intI] + " ";
  207. }
  208. if (eventNamesList != null) { this.eventNamesList(UserNames.Remove(0, 1).Trim()); }
  209. } /* IrcNamesList */
  210.  
  211. private void IrcServerMessage(string[] IrcCommand) {
  212. string ServerMessage = "";
  213. for (int intI = 1; intI < IrcCommand.Length; intI++) {
  214. ServerMessage += IrcCommand[intI] + " ";
  215. }
  216. if (eventServerMessage != null) { this.eventServerMessage(ServerMessage.Trim()); }
  217. } /* IrcServerMessage */
  218. #endregion
  219.  
  220. #region Ping
  221. private void IrcPing(string[] IrcCommand) {
  222. string PingHash = "";
  223. for (int intI = 1; intI < IrcCommand.Length; intI++) {
  224. PingHash += IrcCommand[intI] + " ";
  225. }
  226. this.IrcWriter.WriteLine("PONG " + PingHash);
  227. this.IrcWriter.Flush();
  228. } /* IrcPing */
  229. #endregion
  230.  
  231. #region User Messages
  232. private void IrcJoin(string[] IrcCommand) {
  233. string IrcChannel = IrcCommand[2];
  234. string IrcUser = IrcCommand[0].Split('!')[0];
  235. if (eventJoin != null) { this.eventJoin(IrcChannel.Remove(0, 1), IrcUser); }
  236. } /* IrcJoin */
  237.  
  238. private void IrcPart(string[] IrcCommand) {
  239. string IrcChannel = IrcCommand[2];
  240. string IrcUser = IrcCommand[0].Split('!')[0];
  241. if (eventPart != null) { this.eventPart(IrcChannel, IrcUser); }
  242. } /* IrcPart */
  243.  
  244. private void IrcMode(string[] IrcCommand) {
  245. string IrcChannel = IrcCommand[2];
  246. string IrcUser = IrcCommand[0].Split('!')[0];
  247. string UserMode = "";
  248. for (int intI = 3; intI < IrcCommand.Length; intI++) {
  249. UserMode += IrcCommand[intI] + " ";
  250. }
  251. if (UserMode.Substring(0, 1) == ":") {
  252. UserMode = UserMode.Remove(0, 1);
  253. }
  254. if (eventMode != null) { this.eventMode(IrcChannel, IrcUser, UserMode.Trim()); }
  255. } /* IrcMode */
  256.  
  257. private void IrcNickChange(string[] IrcCommand) {
  258. string UserOldNick = IrcCommand[0].Split('!')[0];
  259. string UserNewNick = IrcCommand[2].Remove(0, 1);
  260. if (eventNickChange != null) { this.eventNickChange(UserOldNick, UserNewNick); }
  261. } /* IrcNickChange */
  262.  
  263. private void IrcKick(string[] IrcCommand) {
  264. string UserKicker = IrcCommand[0].Split('!')[0];
  265. string UserKicked = IrcCommand[3];
  266. string IrcChannel = IrcCommand[2];
  267. string KickMessage = "";
  268. for (int intI = 4; intI < IrcCommand.Length; intI++) {
  269. KickMessage += IrcCommand[intI] + " ";
  270. }
  271. if (eventKick != null) { this.eventKick(IrcChannel, UserKicker, UserKicked, KickMessage.Remove(0, 1).Trim()); }
  272. } /* IrcKick */
  273.  
  274. private void IrcQuit(string[] IrcCommand) {
  275. string UserQuit = IrcCommand[0].Split('!')[0];
  276. string QuitMessage = "";
  277. for (int intI = 2; intI < IrcCommand.Length; intI++) {
  278. QuitMessage += IrcCommand[intI] + " ";
  279. }
  280. if (eventQuit != null) { this.eventQuit(UserQuit, QuitMessage.Remove(0, 1).Trim()); }
  281. } /* IrcQuit */
  282. #endregion
  283. #endregion
  284. } /* IRC */
  285. } /* System.Net */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement