The easy to use and full featured Irc Bot everyone is talking about!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Url_Parsing.cs 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System;
  2. using System.Net;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Web;
  6. using Combot.IRCServices.Messaging;
  7. namespace Combot.Modules.Plugins
  8. {
  9. public class Url_Parsing : Module
  10. {
  11. public override void Initialize()
  12. {
  13. Bot.IRC.Message.ChannelMessageReceivedEvent += HandleChannelMessage;
  14. }
  15. public void HandleChannelMessage(object sender, ChannelMessage message)
  16. {
  17. Regex urlRegex = new Regex("(https?)://([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?");
  18. if (Enabled
  19. && !Bot.ServerConfig.ChannelBlacklist.Contains(message.Channel)
  20. && !Bot.ServerConfig.NickBlacklist.Contains(message.Sender.Nickname)
  21. && !ChannelBlacklist.Contains(message.Channel)
  22. && !NickBlacklist.Contains(message.Sender.Nickname)
  23. && !Bot.IsCommand(message.Message))
  24. {
  25. if (urlRegex.IsMatch(message.Message))
  26. {
  27. MatchCollection urlMatches = urlRegex.Matches(message.Message);
  28. for (int i = 0; i < urlMatches.Count; i++)
  29. {
  30. Match urlMatch = urlMatches[i];
  31. Uri url = new Uri(urlMatch.Value);
  32. HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(url);
  33. webRequest.Method = "HEAD";
  34. webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
  35. ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  36. ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
  37. try
  38. {
  39. using (HttpWebResponse webResponse = (HttpWebResponse) webRequest.GetResponse())
  40. {
  41. int code = (int) webResponse.StatusCode;
  42. if (code == 200)
  43. {
  44. string contentType = webResponse.ContentType.Split('/')[0];
  45. long contentLength = webResponse.ContentLength;
  46. switch (contentType)
  47. {
  48. case "text":
  49. Regex ytRegex = new Regex("(((youtube.*(v=|/v/))|(youtu\\.be/))(?<ID>[-_a-zA-Z0-9]+))");
  50. if (!ytRegex.IsMatch(message.Message) || !Bot.Modules.Exists(mod => mod.Name == "YouTube"))
  51. {
  52. WebClient x = new WebClient();
  53. x.Encoding = Encoding.UTF8;
  54. string source = x.DownloadString(urlMatch.ToString());
  55. string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value;
  56. int maxTitle = Convert.ToInt32(GetOptionValue("Max Title"));
  57. if (title.Length > (int)maxTitle)
  58. {
  59. title = string.Format("{0}...", title.Substring(0, (int)maxTitle));
  60. }
  61. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] {0} ({1})", HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(title))), url.Host));
  62. }
  63. break;
  64. case "image":
  65. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[{0}] Size: {1}", webResponse.ContentType, ToFileSize(contentLength)));
  66. break;
  67. case "video":
  68. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Video] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  69. break;
  70. case "application":
  71. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Application] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  72. break;
  73. case "audio":
  74. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Audio] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  75. break;
  76. }
  77. }
  78. else
  79. {
  80. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Returned Status Code \u0002{0}\u0002 ({1})", code, url.Host));
  81. }
  82. }
  83. }
  84. catch (WebException ex)
  85. {
  86. if (ex.Response != null)
  87. {
  88. int code = (int) ((HttpWebResponse) ex.Response).StatusCode;
  89. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Response Code: \u0002{0}\u0002 ({1})", code, url.Host));
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Remove HTML tags from string using char array.
  98. /// </summary>
  99. public static string StripTagsCharArray(string source)
  100. {
  101. char[] array = new char[source.Length];
  102. int arrayIndex = 0;
  103. bool inside = false;
  104. for (int i = 0; i < source.Length; i++)
  105. {
  106. char let = source[i];
  107. if (let == '<')
  108. {
  109. inside = true;
  110. continue;
  111. }
  112. if (let == '>')
  113. {
  114. inside = false;
  115. continue;
  116. }
  117. if (!inside)
  118. {
  119. array[arrayIndex] = let;
  120. arrayIndex++;
  121. }
  122. }
  123. return new string(array, 0, arrayIndex);
  124. }
  125. public static string ToFileSize(long source)
  126. {
  127. const int byteConversion = 1024;
  128. double bytes = Convert.ToDouble(source);
  129. if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
  130. {
  131. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
  132. }
  133. else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
  134. {
  135. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
  136. }
  137. else if (bytes >= byteConversion) //KB Range
  138. {
  139. return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
  140. }
  141. else //Bytes
  142. {
  143. return string.Concat(bytes, " Bytes");
  144. }
  145. }
  146. }
  147. }