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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] {0} ({1})", HttpUtility.HtmlDecode(HttpUtility.UrlDecode(StripTagsCharArray(title))), url.Host));
  57. }
  58. break;
  59. case "image":
  60. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[{0}] Size: {1}", webResponse.ContentType, ToFileSize(contentLength)));
  61. break;
  62. case "video":
  63. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Video] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  64. break;
  65. case "application":
  66. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Application] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  67. break;
  68. case "audio":
  69. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[Audio] Type: {0} | Size: {1}", webResponse.ContentType.Split('/')[1], ToFileSize(contentLength)));
  70. break;
  71. }
  72. }
  73. else
  74. {
  75. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Returned Status Code \u0002{0}\u0002 ({1})", code, url.Host));
  76. }
  77. }
  78. }
  79. catch (WebException ex)
  80. {
  81. if (ex.Response != null)
  82. {
  83. int code = (int) ((HttpWebResponse) ex.Response).StatusCode;
  84. Bot.IRC.Command.SendPrivateMessage(message.Channel, string.Format("[URL] Response Code: \u0002{0}\u0002 ({1})", code, url.Host));
  85. }
  86. }
  87. }
  88. }
  89. }
  90. }
  91. /// <summary>
  92. /// Remove HTML tags from string using char array.
  93. /// </summary>
  94. public static string StripTagsCharArray(string source)
  95. {
  96. char[] array = new char[source.Length];
  97. int arrayIndex = 0;
  98. bool inside = false;
  99. for (int i = 0; i < source.Length; i++)
  100. {
  101. char let = source[i];
  102. if (let == '<')
  103. {
  104. inside = true;
  105. continue;
  106. }
  107. if (let == '>')
  108. {
  109. inside = false;
  110. continue;
  111. }
  112. if (!inside)
  113. {
  114. array[arrayIndex] = let;
  115. arrayIndex++;
  116. }
  117. }
  118. return new string(array, 0, arrayIndex);
  119. }
  120. public static string ToFileSize(long source)
  121. {
  122. const int byteConversion = 1024;
  123. double bytes = Convert.ToDouble(source);
  124. if (bytes >= Math.Pow(byteConversion, 3)) //GB Range
  125. {
  126. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 3), 2), " GB");
  127. }
  128. else if (bytes >= Math.Pow(byteConversion, 2)) //MB Range
  129. {
  130. return string.Concat(Math.Round(bytes / Math.Pow(byteConversion, 2), 2), " MB");
  131. }
  132. else if (bytes >= byteConversion) //KB Range
  133. {
  134. return string.Concat(Math.Round(bytes / byteConversion, 2), " KB");
  135. }
  136. else //Bytes
  137. {
  138. return string.Concat(bytes, " Bytes");
  139. }
  140. }
  141. }
  142. }