Example #1
0
        /// <summary>
        /// Reads the relevant Rss feed and returns a list of RssFeedItems
        /// </summary>
        /// <param name="url"></param>
        /// <param name="item"></param>
        /// <param name="channel"></param>
        /// <returns></returns>
        public static List<RssFeedItem> ReadFeed(string url, Feed.Subscription item, string channel)
        {
            string temp = "No data were sent by server";
            try
            {
                //create a new list of the rss feed items to return
                List<RssFeedItem> rssFeedItems = new List<RssFeedItem>();

                //create a http request which will be used to retrieve the rss feed
                ServicePointManager.ServerCertificateValidationCallback = Validator;
                HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);

                XmlDocument rss = new XmlDocument();
                StreamReader xx = new StreamReader(rssFeed.GetResponse().GetResponseStream());
                temp = xx.ReadToEnd();
                rss.LoadXml(temp);

                if (url.StartsWith("http://bugzilla.wikimedia") || url.StartsWith("https://bugzilla.wikimedia"))
                {
                    if (rss.ChildNodes[1].Name.ToLower() == "feed")
                    {
                        foreach (XmlNode entry in rss.ChildNodes[1].ChildNodes)
                        {
                            if (entry.Name == "entry")
                            {
                                RssFeedItem curr = new RssFeedItem();
                                foreach (XmlNode data in entry.ChildNodes)
                                {
                                    switch (data.Name.ToLower())
                                    {
                                        case "title":
                                            curr.Title = data.InnerText;
                                            break;
                                        case "link":
                                            foreach (XmlAttribute attribute in data.Attributes)
                                            {
                                                if (attribute.Name == "href")
                                                {
                                                    curr.Link = attribute.Value;
                                                }
                                            }
                                            break;
                                        case "author":
                                            if (data.ChildNodes.Count > 0)
                                            {
                                                curr.Author = data.ChildNodes[0].InnerText;
                                            }
                                            break;
                                        case "summary":
                                            string html = HttpUtility.HtmlDecode(data.InnerText);
                                            if (html.Contains("<table>"))
                                            {
                                                XmlDocument summary = new XmlDocument();
                                                summary.LoadXml(html);
                                                foreach (XmlNode tr in summary.ChildNodes[0].ChildNodes)
                                                {
                                                    bool type = true;
                                                    string st = "";
                                                    foreach (XmlNode td in tr.ChildNodes)
                                                    {
                                                        if (type)
                                                        {
                                                            st = td.InnerText;
                                                        }
                                                        else
                                                        {
                                                            switch (st.Replace(" ", ""))
                                                            {
                                                                case "Product":
                                                                    curr.bugzilla_product = td.InnerText;
                                                                    break;
                                                                case "Status":
                                                                    curr.bugzilla_status = td.InnerText;
                                                                    break;
                                                                case "Component":
                                                                    curr.bugzilla_component = td.InnerText;
                                                                    break;
                                                                case "Assignee":
                                                                    curr.bugzilla_assignee = td.InnerText;
                                                                    break;
                                                                case "Reporter":
                                                                    curr.bugzilla_reporter = td.InnerText;
                                                                    break;
                                                                case "Resolution":
                                                                    curr.bugzilla_reso = td.InnerText;
                                                                    break;
                                                                case "Priority":
                                                                    curr.bugzilla_priority = td.InnerText;
                                                                    break;
                                                                case "Severity":
                                                                    curr.bugzilla_severity = td.InnerText;
                                                                    break;
                                                            }
                                                        }
                                                        type = !type;
                                                    }
                                                }
                                            }
                                            break;
                                        case "guid":
                                            curr.ItemId = data.InnerText;
                                            break;
                                        case "channelid":
                                            curr.ChannelId = data.InnerText;
                                            break;
                                        case "date":
                                            curr.PublishDate = data.Value;
                                            break;
                                    }
                                }
                                rssFeedItems.Add(curr);
                            }
                        }

                        return rssFeedItems;
                    }
                }

                if (rss.ChildNodes[1].Name.ToLower() == "feed")
                {
                    foreach (XmlNode entry in rss.ChildNodes[1].ChildNodes)
                    {
                        if (entry.Name == "entry")
                        {
                            RssFeedItem curr = new RssFeedItem();
                            foreach (XmlNode data in entry.ChildNodes)
                            {
                                switch (data.Name.ToLower())
                                {
                                    case "title":
                                        curr.Title = data.InnerText;
                                        break;
                                    case "link":
                                        foreach (XmlAttribute attribute in data.Attributes)
                                        {
                                            if (attribute.Name == "href")
                                            {
                                                curr.Link = attribute.Value;
                                            }
                                        }
                                        break;
                                    case "author":
                                        if (data.ChildNodes.Count > 0)
                                        {
                                            curr.Author = data.ChildNodes[0].InnerText;
                                        }
                                        break;
                                    case "summary":
                                        curr.Description = data.InnerText;
                                        break;
                                    case "guid":
                                        curr.ItemId = data.InnerText;
                                        break;
                                    case "channelid":
                                        curr.ChannelId = data.InnerText;
                                        break;
                                    case "date":
                                        curr.PublishDate = data.Value;
                                        break;
                                }
                            }
                            rssFeedItems.Add(curr);
                        }
                    }

                    return rssFeedItems;
                }

                foreach (XmlNode node in rss.ChildNodes)
                {
                    if (node.Name.ToLower() == "rss" || node.Name.ToLower() == "channel")
                    {
                        foreach (XmlNode entry in node.ChildNodes[0].ChildNodes)
                        {
                            if (entry.Name == "item")
                            {
                                RssFeedItem curr = new RssFeedItem();
                                foreach (XmlNode data in entry.ChildNodes)
                                {
                                    switch (data.Name.ToLower())
                                    {
                                        case "title":
                                            curr.Title = data.InnerText;
                                            break;
                                        case "link":
                                            curr.Link = data.InnerText;
                                            break;
                                        case "description":
                                            curr.Description = data.InnerText;
                                            break;
                                        case "guid":
                                            curr.ItemId = data.InnerText;
                                            break;
                                        case "channelid":
                                            curr.ChannelId = data.InnerText;
                                            break;
                                        case "date":
                                            curr.PublishDate = data.Value;
                                            break;
                                    }
                                }
                                rssFeedItems.Add(curr);
                            }
                        }
                        return rssFeedItems;
                    }
                }
                if (item.retries < 1)
                {
                    item.disabled = true;
                    IRC.DeliverMessage("Unable to parse the feed from " + url + " this url is probably not a valid rss, the feed will be disabled, until you re-enable it by typing @rss+ " + item.Name, channel);
                    return null;
                }
                item.retries--;
                return null;
            }
            catch (ThreadAbortException)
            {
                // if we receive this here it means someone wants to terminate this thread so let's quit it
                Core.ThreadManager.UnregisterThread(Thread.CurrentThread);
                return null;
            }
            catch (Exception fail)
            {
                RSS.m.Log("Unable to parse feed from " + url + " I will try to do that again " + item.retries + " times", true);
                RSS.m.HandleException(fail, "Feed");
                string dump = Path.GetTempFileName();
                File.WriteAllText(dump, temp);
                RSS.m.Log("Dumped the source to " + dump);
                if (item.retries < 1)
                {
                    item.disabled = true;
                    IRC.DeliverMessage("Unable to parse the feed from " + url + " this url is probably not a valid rss, the feed will be disabled, until you re-enable it by typing @rss+ " + item.Name, channel);
                    return null;
                }
                item.retries--;
                return null;
            }
        }
Example #2
0
 public override void Hook_PRIV(Channel channel, libirc.UserInfo invoker, string message)
 {
     if (message.StartsWith(Configuration.System.CommandPrefix + "rss- "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss+ ".Length);
             Feed   feed = (Feed)channel.RetrieveObject("rss");
             if (feed != null)
             {
                 feed.RemoveItem(item);
             }
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
         }
     }
     if (message.StartsWith(Configuration.System.CommandPrefix + "rss-setstyle "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss-setstyle ".Length);
             if (item.Contains(" "))
             {
                 string id   = item.Substring(0, item.IndexOf(" "));
                 string ur   = item.Substring(item.IndexOf(" ") + 1);
                 Feed   feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.StyleItem(id, ur);
                 }
                 return;
             }
             if (item != "")
             {
                 Feed feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.StyleItem(item, "");
                 }
                 return;
             }
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("Rss5", channel.Language), channel.Name);
             }
         }
         else
         {
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
             }
         }
     }
     if (message.StartsWith(Configuration.System.CommandPrefix + "rss-search+ "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss-search+ ".Length);
             Feed   feed = (Feed)channel.RetrieveObject("rss");
             if (feed != null)
             {
                 lock (feed.ScannerMatches)
                 {
                     if (feed.ScannerMatches.Contains(item))
                     {
                         IRC.DeliverMessage("This item is already being searched", channel);
                         return;
                     }
                     IRC.DeliverMessage("This item is now searched", channel);
                     feed.ScannerMatches.Add(item);
                     return;
                 }
             }
             IRC.DeliverMessage("Error, this channel doesn't have RC feed", channel);
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel, libirc.Defs.Priority.Low);
         }
     }
     if (message.StartsWith(Configuration.System.CommandPrefix + "rss-search- "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss-search+ ".Length);
             Feed   feed = (Feed)channel.RetrieveObject("rss");
             if (feed != null)
             {
                 lock (feed.ScannerMatches)
                 {
                     if (feed.ScannerMatches.Contains(item))
                     {
                         feed.ScannerMatches.Remove(item);
                         IRC.DeliverMessage("This item was removed", channel);
                         return;
                     }
                     IRC.DeliverMessage("This item was not being searched", channel);
                     return;
                 }
             }
             IRC.DeliverMessage("Error, this channel doesn't have RC feed", channel);
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel);
         }
     }
     if (message.StartsWith(Configuration.System.CommandPrefix + "rss+ "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss+ ".Length);
             if (item.Contains(" "))
             {
                 string id   = item.Substring(0, item.IndexOf(" "));
                 string ur   = item.Substring(item.IndexOf(" ") + 1);
                 Feed   feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.InsertItem(id, ur);
                 }
                 return;
             }
             if (item != "")
             {
                 Feed feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.InsertItem(item, "");
                 }
                 return;
             }
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("Rss5", channel.Language), channel.Name);
             }
         }
         else
         {
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
             }
         }
     }
     if (message.StartsWith("@rss-scanner+ "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss-scanner+ ".Length);
             if (item.Contains(" "))
             {
                 string id   = item.Substring(0, item.IndexOf(" "));
                 string ur   = item.Substring(item.IndexOf(" ") + 1);
                 Feed   feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.InsertItem(id, ur, true);
                 }
                 return;
             }
             if (item != "")
             {
                 Feed feed = (Feed)channel.RetrieveObject("rss");
                 if (feed != null)
                 {
                     feed.InsertItem(item, "", true);
                 }
                 return;
             }
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("Rss5", channel.Language), channel.Name);
             }
         }
         else
         {
             if (!channel.SuppressWarnings)
             {
                 IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
             }
         }
     }
     if (message.StartsWith("@rss-scanner- "))
     {
         if (channel.SystemUsers.IsApproved(invoker, "trust"))
         {
             string item = message.Substring("@rss-scannerx ".Length);
             Feed   feed = (Feed)channel.RetrieveObject("rss");
             if (feed != null)
             {
                 feed.RemoveItem(item);
             }
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
         }
     }
     if (message == "@rss-off")
     {
         if (channel.SystemUsers.IsApproved(invoker, "admin"))
         {
             if (!GetConfig(channel, "Rss.Enable", false))
             {
                 IRC.DeliverMessage(messages.Localize("Rss1", channel.Language), channel.Name);
                 return;
             }
             SetConfig(channel, "Rss.Enable", false);
             IRC.DeliverMessage(messages.Localize("Rss2", channel.Language), channel.Name);
             channel.SaveConfig();
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
         }
         return;
     }
     if (message == "@rss-on")
     {
         if (channel.SystemUsers.IsApproved(invoker, "admin"))
         {
             if (GetConfig(channel, "Rss.Enable", false))
             {
                 IRC.DeliverMessage(messages.Localize("Rss3", channel.Language), channel.Name);
                 return;
             }
             IRC.DeliverMessage(messages.Localize("Rss4", channel.Language), channel.Name);
             SetConfig(channel, "Rss.Enable", true);
             channel.SaveConfig();
             return;
         }
         if (!channel.SuppressWarnings)
         {
             IRC.DeliverMessage(messages.Localize("PermissionDenied", channel.Language), channel.Name);
         }
     }
 }