Guest User

Untitled

a guest
Jul 16th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.66 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. using System.Linq;
  7. using System.Collections.Generic;
  8. class IrcBot
  9. {
  10. public static string SERVER = "snext.serveirc.com";
  11. private static int PORT = 6667;
  12. private static string USER = "USER predsbot 8 * :I'm a C# irc bot";
  13. private static string NICK = "Licos";
  14. private static string CHANNEL = "#preds";
  15. public static string AuthedNicks = "predsrule sniperX";
  16. private static string NickServPass = "jamie51822";
  17. //separate each nick with a space.
  18.  
  19. public static StreamWriter writer;
  20. private static bool ispinging = false;
  21.  
  22.  
  23. //
  24. //
  25. //EDIT THIS v
  26. public static void SendMessage(bool pause, string Destination,string Message)
  27. {
  28. writer.WriteLine("PRIVMSG " + Destination + " :"+Message);
  29. writer.Flush();
  30. if (pause == true)
  31. {
  32. Thread.Sleep(1000);
  33. }
  34. }
  35. public static void SendRaw(string Data)
  36. {
  37. writer.WriteLine(Data);
  38. writer.Flush();
  39. Thread.Sleep(1000);
  40. }
  41. public static void irc_OnCommand(string message, string nick, string host, bool fromchannel)
  42. {
  43. #region Methods (dont need to modify this much)
  44. string Message = message;
  45. string Command = "";
  46. bool authenticated = false;
  47. string botnick = NICK.ToLower();
  48. if (Message.ToLower().StartsWith(botnick))
  49. {
  50. List<string> splitArray = new List<string>(Message.Split(new char[] { ' ' }));
  51. splitArray.RemoveAt(0);
  52. Command = splitArray[0];
  53. splitArray.RemoveAt(0);
  54. Message = String.Join(" ", splitArray.ToArray());
  55. }
  56. Command = Command.ToLower();
  57. string[] authuser = AuthedNicks.Split(new char[] { ' ' });
  58. foreach (string User in authuser)
  59. {
  60. if (User.ToLower() == nick.ToLower())
  61. {
  62. authenticated = true;
  63. }
  64. }
  65. #endregion
  66.  
  67. if (Command == "about")
  68. {
  69. SendMessage(true, CHANNEL, "LicosBot V1.0, Wrote by Pasihavia Havia, Modified by predsrule.");
  70. }
  71. else if (Command == "shutdown")
  72. {
  73. if (authenticated == true)
  74. {
  75. SendMessage(true, nick, "Shutting Down...");
  76. Thread.Sleep(2000);
  77. Environment.Exit(0);
  78. }
  79. }
  80. if (Command == "fail")
  81. {
  82. SendMessage(true, CHANNEL, "i'm a failure");
  83. }
  84. if (Command == "restart")
  85. {
  86. if (authenticated == true)
  87. {
  88. SendMessage(true, nick, "Restarting...");
  89. SendRaw("QUIT :Restarting...");
  90. }
  91. }
  92. if (Command == "pray")
  93. {
  94. SendMessage(false, CHANNEL, "The Programmers Prayer");
  95. SendMessage(false, CHANNEL, "Our Program who are in Memory.");
  96. SendMessage(false, CHANNEL, "Hello by thy Name.");
  97. SendMessage(false, CHANNEL, "Thy Operating System come.");
  98. SendMessage(false, CHANNEL, "Thy Commands be done at the Printer as it is on the Screen.");
  99. SendMessage(false, CHANNEL, "Give us this day our daily Data,");
  100. SendMessage(false, CHANNEL, "And forgive us our I/O errors");
  101. SendMessage(false, CHANNEL, "As we forgive those whose logic circuits are faulty.");
  102. SendMessage(false, CHANNEL, "Lead us not into frustration, and deliver us from Power Surges.");
  103. SendMessage(false, CHANNEL, "For Thine is the Algorithm,");
  104. SendMessage(false, CHANNEL, "The Application,");
  105. SendMessage(false, CHANNEL, "And the Solution");
  106. SendMessage(false, CHANNEL, "Looping forever and ever.");
  107. SendMessage(false, CHANNEL, "Return");
  108. }
  109. if (Command == "say")
  110. {
  111. if (authenticated == true)
  112. {
  113. SendMessage(true, CHANNEL, Message);
  114. }
  115. }
  116. }
  117. //EDIT THIS ^
  118. //
  119. //
  120.  
  121.  
  122. public static void irc_OnChannelMessage(string message, string nick, string host)
  123. {
  124. irc_OnCommand(message, nick, host, true);
  125. }
  126. public static void irc_OnPrivMessage(string message, string nick, string host)
  127. {
  128. irc_OnCommand(message, nick, host, false);
  129. }
  130. static void Main(string[] args)
  131. {
  132. NetworkStream stream;
  133. TcpClient irc;
  134. string inputLine;
  135. StreamReader reader;
  136. try
  137. {
  138. if (ispinging == false)
  139. {
  140. PingSender ping = new PingSender();
  141. ping.Start();
  142. ispinging = true;
  143. }
  144. irc = new TcpClient(SERVER, PORT);
  145. stream = irc.GetStream();
  146. reader = new StreamReader(stream);
  147. writer = new StreamWriter(stream);
  148. writer.WriteLine(USER);
  149. writer.Flush();
  150. writer.WriteLine("NICK " + NICK);
  151. Thread.Sleep(2000);
  152. writer.Flush();
  153. writer.WriteLine("JOIN " + CHANNEL);
  154. writer.Flush();
  155. writer.WriteLine("PRIVMSG NickServ :identify "+NickServPass);
  156. writer.Flush();
  157. while (true)
  158. {
  159. while ((inputLine = reader.ReadLine()) != null)
  160. {
  161. //DONT ADD CRAP TO THIS. ADD IT TO irc_OnCommand INSTEAD
  162. Console.WriteLine(inputLine);
  163. if (inputLine.ToLower().Contains("privmsg "+CHANNEL.ToLower()))
  164. {
  165. string[] splitArray = inputLine.Split(new char[] { ':' });
  166. string[] splitArray2 = splitArray[1].Split(new char[] { '!' });
  167. string[] splitArray3 = splitArray2[1].Split(new char[] { ' ' });
  168. string compmsg = "";
  169. foreach (string msg in splitArray)
  170. {
  171. if (msg == "" || msg == splitArray[0] || msg == splitArray[1]) { }
  172. else if (compmsg == "") { compmsg = msg; }
  173. else { compmsg += ":" + msg; }
  174. }
  175. string nick = splitArray2[0];
  176. string host = splitArray3[0];
  177. string message = compmsg;
  178. irc_OnChannelMessage(message, nick, host);
  179. }
  180. else if (inputLine.Contains("PRIVMSG "+NICK))
  181. {
  182. string[] splitArray = inputLine.Split(new char[] { ':' });
  183. string[] splitArray2 = splitArray[1].Split(new char[] { '!' });
  184. string[] splitArray3 = splitArray2[1].Split(new char[] { ' ' });
  185. string compmsg = "";
  186. foreach (string msg in splitArray)
  187. {
  188. if (msg == "" || msg == splitArray[0] || msg == splitArray[1]) { }
  189. else if (compmsg == "") { compmsg = msg; }
  190. else { compmsg += ":" + msg; }
  191. }
  192. string nick = splitArray2[0];
  193. string host = splitArray3[0];
  194. string message = compmsg;
  195. irc_OnPrivMessage(message, nick, host);
  196. }
  197. }
  198. writer.Close();
  199. reader.Close();
  200. irc.Close();
  201. }
  202. }
  203. catch (Exception e)
  204. {
  205. Console.WriteLine(e.ToString());
  206. Thread.Sleep(5000);
  207. string[] argv = { };
  208. Main(argv);
  209. }
  210. }
  211. class PingSender
  212. {
  213. static string PING = "PING :";
  214. private Thread pingSender;
  215.  
  216. // Empty constructor makes instance of Thread
  217. public PingSender()
  218. {
  219. pingSender = new Thread(new ThreadStart(this.Run));
  220. }
  221.  
  222. // Starts the thread
  223. public void Start()
  224. {
  225. pingSender.Start();
  226. }
  227.  
  228. // Send PING to irc server every 15 seconds
  229. public void Run()
  230. {
  231. while (true)
  232. {
  233. try
  234. {
  235. IrcBot.writer.WriteLine(PING + IrcBot.SERVER);
  236. IrcBot.writer.Flush();
  237. Thread.Sleep(15000);
  238. }
  239. catch { Console.WriteLine("Error Pinging Server."); }
  240. }
  241. }
  242. }
  243.  
  244. }
Add Comment
Please, Sign In to add comment