SirCmpwn

Untitled

Jun 2nd, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Meebey.SmartIrc4net;
  6. using System.Threading;
  7. using System.Net;
  8. using System.IO;
  9. using System.Diagnostics;
  10.  
  11. namespace SMProxyBot
  12. {
  13. public class IrcBot
  14. {
  15. private const string PastebinDevKey = "xxx";
  16. private const int MaxSessions = 10;
  17. private const string LoginUsername = "xxx";
  18. private const string LoginPassword = "xxx";
  19.  
  20. public List<Session> ActiveSessions;
  21. public IrcClient client = new IrcClient();
  22. public string Nickname, NickServPassword;
  23. string server, myIp, userToken;
  24. public Dictionary<string, string[]> Log;
  25.  
  26. public event EventHandler OnNewSession;
  27.  
  28. public IrcBot()
  29. {
  30. ActiveSessions = new List<Session>();
  31. Log = new Dictionary<string, string[]>();
  32. WebClient wc = new WebClient();
  33. StreamReader reader = new StreamReader(wc.OpenRead("http://checkip.dyndns.org/"));
  34. string contents = reader.ReadToEnd();
  35. reader.Close();
  36. contents = contents.Substring(76);
  37. contents = contents.Remove(contents.Length - 16);
  38. myIp = contents;
  39.  
  40. // Get login from pastebin
  41. HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://pastebin.com/api/api_login.php");
  42. hwr.ContentType = "application/x-www-form-urlencoded";
  43. hwr.Method = "POST";
  44. StreamWriter writer = new StreamWriter(hwr.GetRequestStream());
  45. writer.Write("api_dev_key=" + Uri.EscapeDataString(PastebinDevKey) + "&api_user_name=" + LoginUsername + "&api_user_password=" + LoginPassword);
  46. writer.Close();
  47. HttpWebResponse resp = (HttpWebResponse)hwr.GetResponse();
  48. reader = new StreamReader(resp.GetResponseStream());
  49. userToken = reader.ReadToEnd();
  50. reader.Close();
  51. }
  52.  
  53. public void Connect(string server, int port)
  54. {
  55. client.OnConnected += new EventHandler(client_OnConnected);
  56. this.server = server;
  57. client.Connect(server, port);
  58. }
  59.  
  60. void client_OnConnected(object sender, EventArgs e)
  61. {
  62. client.OnChannelMessage += new IrcEventHandler(client_OnChannelMessage);
  63. client.OnQueryMessage += new IrcEventHandler(client_OnQueryMessage);
  64. client.OnJoin += new JoinEventHandler(client_OnJoin);
  65. client.OnPart += new PartEventHandler(client_OnPart);
  66. client.ActiveChannelSyncing = true;
  67. client.Login(Nickname, "SMProxy Bot");
  68.  
  69. Timer timer = new Timer(new TimerCallback((obj) =>
  70. {
  71. client.RfcPing(server);
  72. }), null, 30000, 30000);
  73. Thread t = new Thread(new ThreadStart(() => client.Listen()));
  74. t.Start();
  75. }
  76.  
  77. void client_OnPart(object sender, PartEventArgs e)
  78. {
  79. if (Log.ContainsKey(e.Channel))
  80. Log.Remove(e.Channel);
  81. }
  82.  
  83. void client_OnJoin(object sender, JoinEventArgs e)
  84. {
  85. Log.Add(e.Channel, new string[0]);
  86. }
  87.  
  88. void client_OnQueryMessage(object sender, IrcEventArgs e)
  89. {
  90. if (!Log.ContainsKey(e.Data.Nick))
  91. Log.Add(e.Data.Nick, new string[] { "<" + e.Data.Nick + ">" + e.Data.Message });
  92. else
  93. Log[e.Data.Nick] = Log[e.Data.Nick].Concat(new string[] { "<" + e.Data.Nick + ">" + e.Data.Message }).ToArray();
  94. HandleMessage(e.Data.Message, e.Data.Nick, e);
  95. }
  96.  
  97. void client_OnChannelMessage(object sender, IrcEventArgs e)
  98. {
  99. if (!Log.ContainsKey(e.Data.Channel))
  100. Log.Add(e.Data.Channel, new string[] { "<" + e.Data.Nick + ">" + e.Data.Message });
  101. else
  102. Log[e.Data.Channel] = Log[e.Data.Channel].Concat(new string[] { "<" + e.Data.Nick + ">" + e.Data.Message }).ToArray();
  103. if (e.Data.Message.StartsWith(Nickname + ":"))
  104. HandleMessage(e.Data.Message.Substring(Nickname.Length + 1).Trim(), e.Data.Channel, e);
  105. }
  106.  
  107. void HandleMessage(string message, string respondTo, IrcEventArgs e)
  108. {
  109. string response = "";
  110. if (e.Data.Type == ReceiveType.ChannelMessage)
  111. response = e.Data.Nick + ": ";
  112. if (message == "help")
  113. {
  114. if (e.Data.Type == ReceiveType.ChannelMessage)
  115. response += "Use \"newsession [args]\" to launch SMProxy with the specified arguments. Ask for help in query for more details.";
  116. else
  117. {
  118. client.SendMessage(SendType.Message, e.Data.Nick, "Use \"newsession [args]\" to launch SMProxy with the specified arguments.");
  119. client.SendMessage(SendType.Message, e.Data.Nick, "Allowed arguments include -ss, -sc, -pr, -f, -!f, -sp, -ap, and -pv, as well as their long forms.");
  120. client.SendMessage(SendType.Message, e.Data.Nick, "Example usage: \"newsession c.nerd.n\". When you finish, the log will be uploaded to pastebin.com.");
  121. response = "For more information on SMProxy, see http://github.com/SirCmpwn/SMProxy";
  122. }
  123. }
  124. else if (message.StartsWith("newsession "))
  125. {
  126. respondTo = e.Data.Nick;
  127. bool shouldStart = true;
  128. if (ActiveSessions.Count != 0)
  129. {
  130. var foundSessions = ActiveSessions.Where(s => s.OwnerNick == e.Data.Nick);
  131. if (foundSessions.Count() != 0)
  132. {
  133. shouldStart = false;
  134. var timeSpan = foundSessions.First().SessionEnd - DateTime.Now;
  135. response = "You already have an active session, and may not start more. Say \"quit\" to stop early." +
  136. " Your current session expires in " + ((int)timeSpan.TotalSeconds).ToString() + " minutes.";
  137. }
  138. }
  139. else if (ActiveSessions.Count > MaxSessions)
  140. {
  141. shouldStart = false;
  142. response = "This bot is currently operating at full capacity. Please try again later.";
  143. }
  144. if (shouldStart)
  145. {
  146. Session session = new Session();
  147. session.OwnerNick = e.Data.Nick;
  148. session.SessionEnd = DateTime.Now.AddHours(1);
  149. int maxPort = 25570;
  150. if (ActiveSessions.Count != 0)
  151. maxPort = ActiveSessions.OrderBy(s => s.Port).First().Port;
  152. session.Port = maxPort;
  153. response = "New session created. Connect to " + myIp + ":" + maxPort + ". Session expires in 1 hour. Say \"quit\" to stop early.";
  154. Thread handlerThread = new Thread(new ThreadStart(() =>
  155. {
  156. session.SMProxy = new Process();
  157. session.SMProxy.StartInfo.FileName = "SMProxy/SMProxy.exe";
  158. string args = "--endpoint 0.0.0.0 --output results" + e.Data.Nick + ".txt --port " + session.Port.ToString() + " --persistent-session ";
  159. string[] providedArgs = e.Data.Message.Substring("newsession ".Length).Split(' ');
  160. string connectTo = null;
  161. for (int i = 0; i < providedArgs.Length; i++)
  162. {
  163. string arg = providedArgs[i];
  164. if (arg.StartsWith("-"))
  165. {
  166. switch (arg)
  167. {
  168. case "-ss":
  169. case "--suppress-server":
  170. args += "-ss ";
  171. break;
  172. case "-sc":
  173. case "--suppress-client":
  174. args += "-sc ";
  175. break;
  176. case "-f":
  177. case "--filter":
  178. args += "-f ";
  179. args += providedArgs[++i] + " ";
  180. break;
  181. case "-!f":
  182. args += "-!f ";
  183. args += providedArgs[++i] + " ";
  184. break;
  185. case "-pr":
  186. case "--enable-profiling":
  187. args += "-pr ";
  188. break;
  189. case "-ap":
  190. case "--add-packet":
  191. args += "-ap ";
  192. args += providedArgs[++i] + " ";
  193. break;
  194. case "-pv":
  195. case "--protocol-version":
  196. args += "-pv ";
  197. args += providedArgs[++i] + " ";
  198. break;
  199. default:
  200. response = "Invalid parameter: " + arg;
  201. shouldStart = false;
  202. break;
  203. }
  204. }
  205. else
  206. {
  207. if (connectTo == null)
  208. connectTo = arg;
  209. else
  210. {
  211. shouldStart = false;
  212. response += "Invalid parameter: " + arg;
  213. }
  214. }
  215. if (!shouldStart)
  216. break;
  217. }
  218. if (shouldStart)
  219. {
  220. args += connectTo;
  221. session.SMProxy.StartInfo.UseShellExecute = false;
  222. session.SMProxy.StartInfo.RedirectStandardOutput = true;
  223. session.SMProxy.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  224. session.SMProxy.StartInfo.CreateNoWindow = true;
  225. session.SMProxy.StartInfo.Arguments = args;
  226. session.SMProxy.Start();
  227. string output = session.SMProxy.StandardOutput.ReadToEnd();
  228. session.SMProxy.WaitForExit();
  229. if (File.Exists("results" + e.Data.Nick + ".txt"))
  230. {
  231. StreamReader fileReader = new StreamReader("results" + e.Data.Nick + ".txt");
  232. output = fileReader.ReadToEnd();
  233. fileReader.Close();
  234. }
  235. HttpWebRequest hwr = (HttpWebRequest)WebRequest.Create("http://pastebin.com/api/api_post.php");
  236. hwr.Method = "POST";
  237. hwr.ContentType = "application/x-www-form-urlencoded";
  238. StreamWriter sw = new StreamWriter(hwr.GetRequestStream());
  239. sw.Write("api_option=paste&api_dev_key=" + Uri.EscapeDataString(PastebinDevKey) + "&api_paste_code=" + Uri.EscapeDataString(output) +
  240. "&api_user_key=" + Uri.EscapeDataString(userToken));
  241. sw.Close();
  242. HttpWebResponse resp = (HttpWebResponse)hwr.GetResponse();
  243. StreamReader reader = new StreamReader(resp.GetResponseStream());
  244. string pasteUrl = reader.ReadToEnd();
  245. reader.Close();
  246. client.SendMessage(SendType.Message, session.OwnerNick, "Session complete: " + pasteUrl);
  247. ActiveSessions.Remove(session);
  248. }
  249. }));
  250. handlerThread.Start();
  251. if (shouldStart)
  252. {
  253. ActiveSessions.Add(session);
  254. if (OnNewSession != null)
  255. OnNewSession(this, null);
  256. }
  257. }
  258. }
  259. else if (message == "quit")
  260. {
  261. if (ActiveSessions.Count != 0)
  262. {
  263. var foundSessions = ActiveSessions.Where(s => s.OwnerNick == e.Data.Nick);
  264. foundSessions.First().SMProxy.Kill();
  265. response += "Session ended. Uploading log to patsebin.";
  266. }
  267. else
  268. response = "You do not currently have an active session.";
  269. }
  270. client.SendMessage(SendType.Message, respondTo, response);
  271. }
  272. }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment