/// <summary> /// Reads the relevant Rss feed and returns a list of RssFeedItems /// </summary> /// <param name="url"></param> /// <returns></returns> public static List<RssFeedItem> ReadFeed(string url, Feed.Item 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 = System.Web.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; Core.irc.Queue.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 fail) { throw fail; } catch (Exception fail) { RSS.m.Log("Unable to parse feed from " + url + " I will try to do that again " + item.retries.ToString() + " 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; Core.irc.Queue.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; } }
public override void Hook_PRIV(config.channel channel, User invoker, string message) { if (message.StartsWith(config.CommandPrefix + "rss- ")) { if (channel.Users.IsApproved(invoker, "trust")) { string item = message.Substring("@rss+ ".Length); Feed feed = (Feed)channel.RetrieveObject("rss"); if (feed != null) { feed.RemoveItem(item); } return; } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } } } if (message.StartsWith(config.CommandPrefix + "rss-setstyle ")) { if (channel.Users.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.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("Rss5", channel.Language), channel.Name, IRC.priority.low); } } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } } } if (message.StartsWith(config.CommandPrefix + "rss-search+ ")) { if (channel.Users.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)) { core.irc._SlowQueue.DeliverMessage("This item is already being searched", channel); return; } core.irc._SlowQueue.DeliverMessage("This item is now searched", channel); feed.ScannerMatches.Add(item); return; } } core.irc._SlowQueue.DeliverMessage("Error, this channel doesn't have RC feed", channel); return; } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel, IRC.priority.low); } } } if (message.StartsWith(config.CommandPrefix + "rss-search- ")) { if (channel.Users.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); core.irc._SlowQueue.DeliverMessage("This item was removed", channel); return; } core.irc._SlowQueue.DeliverMessage("This item was not being searched", channel); return; } } core.irc._SlowQueue.DeliverMessage("Error, this channel doesn't have RC feed", channel); return; } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel, IRC.priority.low); } } } if (message.StartsWith(config.CommandPrefix + "rss+ ")) { if (channel.Users.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.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("Rss5", channel.Language), channel.Name, IRC.priority.low); } } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } } } if (message.StartsWith("@rss-scanner+ ")) { if (channel.Users.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.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("Rss5", channel.Language), channel.Name, IRC.priority.low); } } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } } } if (message.StartsWith("@rss-scanner- ")) { if (channel.Users.IsApproved(invoker, "trust")) { string item = message.Substring("@rss-scannerx ".Length); Feed feed = (Feed)channel.RetrieveObject("rss"); if (feed != null) { feed.RemoveItem(item); } return; } else { if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } } } if (message == "@rss-off") { if (channel.Users.IsApproved(invoker, "admin")) { if (!GetConfig(channel, "Rss.Enable", false)) { core.irc._SlowQueue.DeliverMessage(messages.get("Rss1", channel.Language), channel.Name); return; } else { SetConfig(channel, "Rss.Enable", false); core.irc._SlowQueue.DeliverMessage(messages.get("Rss2", channel.Language), channel.Name); channel.SaveConfig(); return; } } if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } return; } if (message == "@rss-on") { if (channel.Users.IsApproved(invoker, "admin")) { if (GetConfig(channel, "Rss.Enable", false)) { core.irc._SlowQueue.DeliverMessage(messages.get("Rss3", channel.Language), channel.Name); return; } else { core.irc._SlowQueue.DeliverMessage(messages.get("Rss4", channel.Language), channel.Name); SetConfig(channel, "Rss.Enable", true); channel.SaveConfig(); return; } } if (!channel.suppress_warnings) { core.irc._SlowQueue.DeliverMessage(messages.get("PermissionDenied", channel.Language), channel.Name, IRC.priority.low); } return; } }