private void OnUriSchemeEnabledCheckedChanged(object sender, EventArgs e) { if (Properties.Settings.Default.uriSchemeEnabled != uriSchemeEnabled.Checked) { try { bool success = false; if (uriSchemeEnabled.Checked) { success = URIHandler.RegisterHandler(); } else { success = URIHandler.RemoveHandler(); } if (success) { SetParameters(); } } finally { LoadParameters(); } } }
// // TryParseURIs // // This function is really complicated because of exploits. F**k exploits. // private void TryParseURIs(String channel, String msg) { try { Regex r_finduris = new Regex( @"((?<method>[^:/?# ]+):)" + @"(//(?<host>[^/?# ]*))" + @"(?<path>[^?# ]*)" + @"(?<query>\?([^# ]*))?" + @"(?<tag>#(.*))?" ); var matchbox = r_finduris.Matches(msg); if (matchbox.Count != 0) { String outp = String.Empty; for (int i = 0; i < matchbox.Count; i++) { var match = matchbox[i]; URI uri = new URI { method = match.Groups["method"].Value, host = match.Groups["host"].Value, path = match.Groups["path"].Value, query = match.Groups["query"]?.Value ?? String.Empty, tag = match.Groups["tag"]?.Value ?? String.Empty, uri = match.Value }; // // Will the real URI please stand up? if (uri.method == "http" || uri.method == "https") { var req = WebRequest.Create(uri.uri) as HttpWebRequest; using (var resp = req.GetResponse()) if (resp.ResponseUri.Host != uri.host) { uri.method = resp.ResponseUri.Scheme; uri.host = resp.ResponseUri.Host; uri.path = resp.ResponseUri.AbsolutePath; uri.query = resp.ResponseUri.Query; uri.tag = resp.ResponseUri.Fragment; uri.uri = resp.ResponseUri.OriginalString; } } if (uri.path.Length == 0) { uri.path = "/"; } // // Make sure the method is OK. // Previously: // [22:19] <marrub> file:///srv/www/marrub/oldmen.html // [22:19] <vrobot3> [ OLD MEN OLD MEN OLD MEN OLD MEN OLD MEN OLD MEN OLD MEN OLD ... ] String[] validmethods = { "ftp", "ftps", "http", "https" }; if (!validmethods.Contains(uri.method)) { continue; } // // Try and get a decent title from the URL. URIHandler handler = URI_Default; String result = String.Empty; String referer = null; if (uri.method == "http" || uri.method == "https") { referer = uri.method + "://" + uri.host; Dictionary <String, URIHandler> handlers = new Dictionary <String, URIHandler>() { { "youtube.com", URI_Youtube }, { "youtu.be", URI_Youtube }, { "gelbooru.com", URI_Gelbooru }, { "hitbox.tv", URI_Hitbox }, }; String hostst = Regex.Replace(uri.host, @"^www\.", String.Empty, RegexOptions.Multiline); if (handlers.ContainsKey(hostst)) { handler = handlers[hostst]; } } // // Handle grabbing the title. Just get on with it if we throw an exception. try { handler(uri, referer, ref result); } catch (Exception exc) { Console.WriteLine("URL handle exception: {0}", exc.Message); continue; } // // Sanitize. result.Trim(); for (int j = result.Length - 1; j >= 0; j--) { Char ch = result[j]; if ((Char.IsWhiteSpace(ch) && ch != ' ') || Char.IsControl(ch) || Char.IsSurrogate(ch)) { result = result.Remove(j, 1); } } // // If the result is 0-length, just get rid of it. if (result.Trim().Length == 0) { continue; } // // Throw the result into the output buffer. outp += result; // // If the output is too long, we need to shorten it and break. if (outp.Length > 400 - 3) { outp = outp.Substring(0, 400 - 3); outp += "···"; break; } // // Add separators. if (i != matchbox.Count - 1) { outp += " | "; } } if (outp.Length > 0) { bot.Message(channel, "[ " + outp + " ]"); } } } catch (Exception exc) { Console.WriteLine("{0}: URL parse error: {1}", bot.n_groupname, exc.Message ?? "[unknown]"); } }