-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
112 lines (94 loc) · 3.29 KB
/
Program.cs
File metadata and controls
112 lines (94 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Xml.Linq;
using TelnetServer;
public static class Program
{
static SortedDictionary<string, Menu> _menus;
static void Main()
{
var alive = true;
var listener = new TcpListener(IPAddress.Any, 8080);
var hostname = Dns.GetHostName();
var entry = Dns.GetHostEntry(hostname);
Console.WriteLine($"Listener started on:");
foreach (var ipAddress in entry.AddressList)
{
Console.WriteLine(ipAddress.ToString());
}
LoadMenus();
listener.Start();
while (alive)
{
var client = listener.AcceptTcpClient();
var clientThread = new ParameterizedThreadStart(OnClientConnected);
var thread = new Thread(clientThread);
thread.Start(client);
}
listener.Stop();
}
static void OnClientConnected(object? obj)
{
if (obj is not TcpClient client)
{
return;
}
var telnetClient = new TelnetClient()
{
TcpClient = client,
Menus = _menus,
};
telnetClient.OnInitialise();
while (telnetClient != null && !telnetClient.IsDisconnected())
{
telnetClient.Update();
}
if(telnetClient != null)
{
telnetClient.TcpClient.Close();
telnetClient.TcpClient.Dispose();
}
}
static void LoadMenus()
{
var defaultMenu = new NewsMenu();
const string opmlPath = "feeds.opml";
if (File.Exists(opmlPath))
{
try
{
var doc = XDocument.Load(opmlPath);
var outlines = doc.Descendants("outline")
.Where(o => o.Attribute("xmlUrl") != null);
foreach (var outline in outlines)
{
var name = outline.Attribute("text")?.Value ?? "Unknown";
var url = outline.Attribute("xmlUrl")!.Value;
defaultMenu.Sources.Add(new Tuple<string, string>(name, url));
}
Console.WriteLine($"Loaded {defaultMenu.Sources.Count} feed(s) from {opmlPath}.");
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load {opmlPath}: {ex.Message}. Using default feeds.");
LoadDefaultSources(defaultMenu);
}
}
else
{
Console.WriteLine($"{opmlPath} not found. Using default feeds.");
LoadDefaultSources(defaultMenu);
}
_menus = new SortedDictionary<string, Menu>();
_menus["default"] = defaultMenu;
}
static void LoadDefaultSources(NewsMenu menu)
{
menu.Sources.Add(new Tuple<string, string>("BBC Top Stories", "https://feeds.bbci.co.uk/news/rss.xml"));
menu.Sources.Add(new Tuple<string, string>("Eurogamer", "https://www.eurogamer.net/feed"));
menu.Sources.Add(new Tuple<string, string>("Retro News", "https://www.retronews.com/feed/"));
menu.Sources.Add(new Tuple<string, string>("Wargamer", "https://www.wargamer.com/mainrss.xml"));
menu.Sources.Add(new Tuple<string, string>("Amstrad", "https://www.reddit.com/r/Amstrad.rss"));
}
}