/// <summary> /// Called when [message]. /// </summary> /// <param name="b">The b.</param> /// <param name="sender">The sender.</param> /// <param name="channel">The channel.</param> /// <param name="message">The message.</param> public void OnMessage(Bot b, string sender, string channel, string message) { if (this.config.Channels.Contains(channel)) { string responseString = string.Empty; if (message.ToLower().Equals("!help")) { b.SendMessage(sender, this.OnHelp()); } else if (message.ToLower().Equals("!roulette")) { while (string.IsNullOrEmpty(responseString)) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://api.imgur.com/2/image/" + this.RandomHash()); request.Timeout = 5000; HttpWebResponse response = request.GetResponse() as HttpWebResponse; Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); responseString = Regex.Match(reader.ReadToEnd(), @"\<original\b[^>]*\>\s*(?<Original>[\s\S]*?)\</original\>", RegexOptions.IgnoreCase).Groups["Original"].Value; b.SendMessage(channel, responseString); response.Close(); stream.Close(); reader.Close(); } catch { } } } } }
/// <summary> /// Called when [message]. /// </summary> /// <param name="b">The b.</param> /// <param name="sender">The sender.</param> /// <param name="channel">The channel.</param> /// <param name="message">The message.</param> public void OnMessage(Bot b, string sender, string channel, string message) { if (this.config.Channels.Contains(channel)) { if (message.ToLower().Equals("!help")) { b.SendMessage(sender, this.OnHelp()); } foreach (string s in message.Split(' ')) { Uri uri; if (Uri.TryCreate(s, UriKind.Absolute, out uri)) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Timeout = 10000; HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response.ContentType.Contains("html")) { Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream); string title = string.Empty; char[] read = new char[1024]; int count = reader.Read(read, 0, read.Length); StringBuilder content = new StringBuilder(string.Empty); while (string.IsNullOrEmpty(title) && count > 0) { content.Append(new string(read, 0, count)); title = Regex.Match(content.ToString(), @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value; count = reader.Read(read, 0, read.Length); } stream.Close(); response.Close(); reader.Close(); b.SendMessage(channel, string.Format("Title: {0}", title)); } } catch (Exception e) { Console.WriteLine(e.StackTrace); } } } } }
/// <summary> /// Called when [private message]. /// </summary> /// <param name="b">The b.</param> /// <param name="sender">The sender.</param> /// <param name="message">The message.</param> public void OnPrivateMessage(Bot b, string sender, string message) { // Do nothing. }
/// <summary> /// Main entry point. /// </summary> /// <param name="args">The args.</param> public static void Main(string[] args) { ConfigFile config = ConfigFile.Deserialize(@"config.xml"); Bot bot = new Bot(config.Servers[0]); bot.Run(); }