PageRenderTime 29ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/BlogEngine/DotNetSlave.BusinessLogic/Ping/Manager.cs

#
C# | 115 lines | 62 code | 17 blank | 36 comment | 6 complexity | fac2f107e2877e130f91b311cff8d2f4 MD5 | raw file
  1namespace BlogEngine.Core.Ping
  2{
  3    using System;
  4    using System.Collections.Generic;
  5    using System.Linq;
  6    using System.Text.RegularExpressions;
  7
  8    /// <summary>
  9    /// Manages to send out trackbacks and then pingbacks if trackbacks aren't supported by the linked site.
 10    /// </summary>
 11    public static class Manager
 12    {
 13        #region Constants and Fields
 14
 15        /// <summary>
 16        ///     Regex used to find the trackback link on a remote web page.
 17        /// </summary>
 18        private static readonly Regex TrackbackLinkRegex = new Regex(
 19            "trackback:ping=\"([^\"]+)\"", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 20
 21        // private static readonly Regex urlsRegex = new Regex(@"\<a\s+href=""(http://.*?)"".*\>.+\<\/a\>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 22        // private static readonly Regex urlsRegex = new Regex(@"<a[^(href)]?href=""([^""]+)""[^>]?>([^<]+)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 23
 24        /// <summary>
 25        ///     Regex used to find all hyperlinks.
 26        /// </summary>
 27        private static readonly Regex UrlsRegex = new Regex(
 28            @"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
 29
 30        #endregion
 31
 32        #region Public Methods
 33
 34        /// <summary>
 35        /// Sends the trackback or pingback message.
 36        ///     <remarks>
 37        /// It will try to send a trackback message first, and if the refered web page
 38        ///         doesn't support trackbacks, a pingback is sent.
 39        ///     </remarks>
 40        /// </summary>
 41        /// <param name="item">
 42        /// The publishable item.
 43        /// </param>
 44        /// <param name="itemUrl">
 45        /// The item Url.
 46        /// </param>
 47        public static void Send(IPublishable item, Uri itemUrl)
 48        {
 49            foreach (var url in GetUrlsFromContent(item.Content))
 50            {
 51                var trackbackSent = false;
 52
 53                if (BlogSettings.Instance.EnableTrackBackSend)
 54                {
 55                    // ignoreRemoteDownloadSettings should be set to true
 56                    // for backwards compatibilty with Utils.DownloadWebPage.
 57                    var remoteFile = new RemoteFile(url, true);
 58                    var pageContent = remoteFile.GetFileAsString(); // ReadFromWeb(url);
 59                    var trackbackUrl = GetTrackBackUrlFromPage(pageContent);
 60
 61                    if (trackbackUrl != null)
 62                    {
 63                        var message = new TrackbackMessage(item, trackbackUrl, itemUrl);
 64                        trackbackSent = Trackback.Send(message);
 65                    }
 66                }
 67
 68                if (!trackbackSent && BlogSettings.Instance.EnablePingBackSend)
 69                {
 70                    Pingback.Send(itemUrl, url);
 71                }
 72            }
 73        }
 74
 75        #endregion
 76
 77        #region Methods
 78
 79        /// <summary>
 80        /// Examines the web page source code to retrieve the trackback link from the RDF.
 81        /// </summary>
 82        /// <param name="input">The input.</param>
 83        /// <returns>The trackback Uri</returns>
 84        private static Uri GetTrackBackUrlFromPage(string input)
 85        {
 86            var url = TrackbackLinkRegex.Match(input).Groups[1].ToString().Trim();
 87            Uri uri;
 88
 89            return Uri.TryCreate(url, UriKind.Absolute, out uri) ? uri : null;
 90        }
 91
 92        /// <summary>
 93        /// Gets all the URLs from the specified string.
 94        /// </summary>
 95        /// <param name="content">The content.</param>
 96        /// <returns>A list of Uri</returns>
 97        private static IEnumerable<Uri> GetUrlsFromContent(string content)
 98        {
 99            var urlsList = new List<Uri>();
100            foreach (var url in
101                UrlsRegex.Matches(content).Cast<Match>().Select(myMatch => myMatch.Groups["url"].ToString().Trim()))
102            {
103                Uri uri;
104                if (Uri.TryCreate(url, UriKind.Absolute, out uri))
105                {
106                    urlsList.Add(uri);
107                }
108            }
109
110            return urlsList;
111        }
112
113        #endregion
114    }
115}