2019-02-18 20:30:38 -05:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Microsoft.SyndicationFeed.Rss;
|
|
|
|
|
using SharedLibraryCore.Configuration;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using Microsoft.SyndicationFeed;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using SharedLibraryCore.Helpers;
|
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
|
|
namespace AutomessageFeed
|
|
|
|
|
{
|
|
|
|
|
public class Plugin : IPlugin
|
|
|
|
|
{
|
|
|
|
|
public string Name => "Automessage Feed";
|
|
|
|
|
|
|
|
|
|
public float Version => (float)Utilities.GetVersionAsDouble();
|
|
|
|
|
|
|
|
|
|
public string Author => "RaidMax";
|
|
|
|
|
|
|
|
|
|
private int _currentFeedItem;
|
2020-02-11 17:44:06 -05:00
|
|
|
|
private readonly IConfigurationHandler<Configuration> _configurationHandler;
|
|
|
|
|
|
|
|
|
|
public Plugin(IConfigurationHandlerFactory configurationHandlerFactory)
|
|
|
|
|
{
|
|
|
|
|
_configurationHandler = configurationHandlerFactory.GetConfigurationHandler<Configuration>("AutomessageFeedPluginSettings");
|
|
|
|
|
}
|
2019-02-18 20:30:38 -05:00
|
|
|
|
|
2019-02-19 20:39:09 -05:00
|
|
|
|
private async Task<string> GetNextFeedItem(Server server)
|
2019-02-18 20:30:38 -05:00
|
|
|
|
{
|
|
|
|
|
var items = new List<string>();
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
using (var reader = XmlReader.Create(_configurationHandler.Configuration().FeedUrl, new XmlReaderSettings() { Async = true }))
|
2019-02-18 20:30:38 -05:00
|
|
|
|
{
|
|
|
|
|
var feedReader = new RssFeedReader(reader);
|
|
|
|
|
|
|
|
|
|
while (await feedReader.Read())
|
|
|
|
|
{
|
|
|
|
|
switch (feedReader.ElementType)
|
|
|
|
|
{
|
|
|
|
|
case SyndicationElementType.Item:
|
|
|
|
|
var item = await feedReader.ReadItem();
|
|
|
|
|
items.Add(Regex.Replace(item.Title, @"\<.+\>.*\</.+\>", ""));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (_currentFeedItem < items.Count && (_configurationHandler.Configuration().MaxFeedItems == 0 || _currentFeedItem < _configurationHandler.Configuration().MaxFeedItems))
|
2019-02-18 20:30:38 -05:00
|
|
|
|
{
|
|
|
|
|
_currentFeedItem++;
|
|
|
|
|
return items[_currentFeedItem - 1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_currentFeedItem = 0;
|
|
|
|
|
return Utilities.CurrentLocalization.LocalizationIndex["PLUGINS_AUTOMESSAGEFEED_NO_ITEMS"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnEventAsync(GameEvent E, Server S)
|
|
|
|
|
{
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task OnLoadAsync(IManager manager)
|
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
if (_configurationHandler.Configuration() == null)
|
2019-02-18 20:30:38 -05:00
|
|
|
|
{
|
2020-02-11 17:44:06 -05:00
|
|
|
|
_configurationHandler.Set((Configuration)new Configuration().Generate());
|
|
|
|
|
await _configurationHandler.Save();
|
2019-02-18 20:30:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
manager.GetMessageTokens().Add(new MessageToken("FEED", GetNextFeedItem));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnTickAsync(Server S)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task OnUnloadAsync()
|
|
|
|
|
{
|
2019-08-24 11:02:53 -04:00
|
|
|
|
return Task.CompletedTask;
|
2019-02-18 20:30:38 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|