Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.67 KB | None | 0 0
  1. using System;
  2. using Server;
  3. using System.IO;
  4. using System.Net;
  5. using Server.Network;
  6. using System.Threading;
  7. using System.Net.Sockets;
  8.  
  9. namespace Server.Misc
  10. {
  11. public class StatusBot
  12. {
  13. public static string Nickname = "Genesis";
  14. public static string Username = "Genesis";
  15. public static string Realname = "GenesisBot";
  16. public static string Channel = "#Genesis";
  17.  
  18. public static string Host = "irc.wildirc.net";
  19. public static int Port = 6667;
  20.  
  21. public static string NickservPassword = null; // If you want to register StatusBot with nickserv, enter the password you registered the nickname to here.
  22. public static bool ReconnectOnDisc = true;
  23. public static bool Connected = false;
  24.  
  25. public static TimeSpan SpamDelay = TimeSpan.FromSeconds(5.0); // StatusBot only accepts one command every 5 seconds, so users won't spam it.
  26. public static DateTime LastCommand;
  27.  
  28. public static string Message = "";
  29.  
  30.  
  31. public static void Initialize()
  32. {
  33. Thread t = new Thread(new ThreadStart(Connect));
  34. t.Start();
  35. }
  36.  
  37. private static StreamReader SR;
  38. private static StreamWriter SW;
  39.  
  40. public static string FormatTimeSpan(TimeSpan ts)
  41. {
  42. return String.Format("{0:D2} Days, {1:D2} Hours, {2:D2} Minutes, and {3:D2} Seconds", ts.Days, ts.Hours % 24, ts.Minutes % 60, ts.Seconds % 60);
  43. }
  44.  
  45. public static void Connect()
  46. {
  47. try
  48. {
  49. TcpClient C = new TcpClient(Host, Port);
  50. NetworkStream S = C.GetStream();
  51. SW = new StreamWriter(S);
  52. SR = new StreamReader(S);
  53. }
  54. catch
  55. {
  56. Thread.Sleep(60000);
  57. Connect();
  58. }
  59.  
  60. Connected = true;
  61.  
  62. SendRaw("USER " + Username + " 0 * :" + Realname);
  63.  
  64. SetNick(Nickname);
  65.  
  66. if (NickservPassword != null)
  67. Identify(NickservPassword);
  68.  
  69. JoinChannel(Channel);
  70.  
  71. GetInput();
  72. }
  73.  
  74. public static void Stop()
  75. {
  76. Connected = false;
  77. SR.Close();
  78. SW.Close();
  79. }
  80.  
  81.  
  82. private static void GetInput()
  83. {
  84.  
  85. try
  86. {
  87. while (Connected && ParseIncoming(SR.ReadLine())) ;
  88. }
  89. catch (Exception)
  90. {
  91. // Something's Wrong... Probably Disconnected
  92. Connected = false;
  93. }
  94. if (ReconnectOnDisc)
  95. {
  96. Thread.Sleep(60000);
  97. Connect();
  98. }
  99. }
  100.  
  101. private static bool ParseIncoming(string inputLine)
  102. {
  103. if (inputLine == null)
  104. return false;
  105.  
  106. Console.WriteLine(inputLine);
  107. string[] parts = inputLine.Split(':');
  108.  
  109. if (parts.Length == 3)
  110. Message = parts[2];
  111. else
  112. Message = "";
  113.  
  114. if (inputLine.StartsWith("PING"))
  115. SendRaw("PONG " + inputLine.Substring(6));
  116.  
  117. if (Message.StartsWith("!") && LastCommand + SpamDelay <= DateTime.Now)
  118. ReadCommand(Message);
  119.  
  120. return true;
  121. }
  122.  
  123. private static void ReadCommand(string inputLine)
  124. {
  125. int userCount = NetState.Instances.Count;
  126. string uptime = FormatTimeSpan(DateTime.Now - Server.Items.Clock.ServerStart);
  127.  
  128. LastCommand = DateTime.Now;
  129.  
  130. switch (inputLine.ToLower())
  131. {
  132. case "!status":
  133. {
  134. SendMessage(Channel, String.Format("Users Online: {0} .:. Uptime {1} .:. -[StatusBot]-", userCount, uptime));
  135. break;
  136. }
  137. }
  138. }
  139.  
  140. private static void SendRaw(string Message)
  141. {
  142. try
  143. {
  144. if (Connected)
  145. {
  146. SW.WriteLine(Message);
  147. SW.Flush();
  148. }
  149. }
  150. catch
  151. {
  152. // Something's Wrong... Probably Disconnected
  153. Connected = false;
  154. }
  155. }
  156.  
  157. private static void Identify(string Password)
  158. {
  159. SendMessage("Nickserv", "Identify " + Password);
  160. }
  161.  
  162. private static void JoinChannel(string C)
  163. {
  164. SendRaw("JOIN " + C);
  165. }
  166.  
  167. private static void PartChannel(string C)
  168. {
  169. SendRaw("PART " + C);
  170. }
  171.  
  172. private static void PartChannel(string C, string Message)
  173. {
  174. SendRaw("PART " + C + " :" + Message);
  175. }
  176.  
  177. private static void Quit()
  178. {
  179. SendRaw("QUIT");
  180. Stop();
  181. }
  182.  
  183. private static void Quit(string Message)
  184. {
  185. SendRaw("QUIT :" + Message);
  186. Stop();
  187. }
  188.  
  189. private static void SendMessage(string Target, string Message)
  190. {
  191. SendRaw("PRIVMSG " + Target + " :" + Message);
  192. }
  193.  
  194. private static void SendNotice(string Target, string Message)
  195. {
  196. SendRaw("NOTICE " + Target + " :" + Message);
  197. }
  198.  
  199. private static void SetMode(string Target, string Modes)
  200. {
  201. SendRaw("MODE " + Target + " :" + Modes);
  202. }
  203.  
  204. private static void SetNick(string N)
  205. {
  206. SendRaw("NICK " + N);
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement