-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMain.cs
259 lines (215 loc) · 10 KB
/
Main.cs
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.Win32;
using RossCarlson.Vatsim.Vpilot.Plugins;
using RossCarlson.Vatsim.Vpilot.Plugins.Events;
namespace vPilot_Pushover {
public class Main : IPlugin {
public static string version = "1.1.0";
// Init
private IBroker vPilot;
private INotifier notifier;
private Acars acars;
public string Name { get; } = "vPilot Pushover";
// Public variables
public string connectedCallsign = null;
// Settings
private Boolean settingsLoaded = false;
private IniFile settingsFile;
private String settingDriver = null;
private Boolean settingPrivateEnabled = false;
private Boolean settingRadioEnabled = false;
private Boolean settingSelcalEnabled = false;
private Boolean settingHoppieEnabled = false;
private Boolean settingDisconnectEnabled = false;
public String settingHoppieLogon = null;
private String settingPushoverToken = null;
private String settingPushoverUser = null;
private String settingPushoverDevice = null;
private String settingTelegramBotToken = null;
private String settingTelegramChatId = null;
/*
*
* Initilise the plugin
*
*/
public void Initialize( IBroker broker ) {
vPilot = broker;
loadSettings();
if (settingsLoaded) {
// Load the correct notifier driver
if (settingDriver.ToLower() == "pushover") {
notifier = new Drivers.Pushover();
NotifierConfig config;
config = new NotifierConfig {
settingPushoverToken = settingPushoverToken,
settingPushoverUser = settingPushoverUser,
settingPushoverDevice = settingPushoverDevice
};
notifier.init(config);
if(!notifier.hasValidConfig()) {
sendDebug("Pushover API key or user key not set. Check your vPilot-Pushover.ini");
return;
}
sendDebug("Driver set to Pushover");
} else if (settingDriver.ToLower() == "telegram") {
notifier = new Drivers.Telegram();
NotifierConfig config;
config = new NotifierConfig {
settingTelegramBotToken = settingTelegramBotToken,
settingTelegramChatId = settingTelegramChatId
};
notifier.init(config);
if (!notifier.hasValidConfig()) {
sendDebug("Telegram bot token or chat ID not set. Check your vPilot-Pushover.ini");
return;
}
sendDebug("Driver set to Telegram");
} else {
sendDebug("Driver not set correctly. Check your vPilot-Pushover.ini");
return;
}
// Subscribe to events according to settings
vPilot.NetworkConnected += onNetworkConnectedHandler;
vPilot.NetworkDisconnected += onNetworkDisconnectedHandler;
if (settingPrivateEnabled) vPilot.PrivateMessageReceived += onPrivateMessageReceivedHandler;
if (settingRadioEnabled) vPilot.RadioMessageReceived += onRadioMessageReceivedHandler;
if (settingSelcalEnabled) vPilot.SelcalAlertReceived += onSelcalAlertReceivedHandler;
// Enable ACARS if Hoppie is enabled
if (settingHoppieEnabled) {
acars = new Acars();
acars.init(this, notifier, settingHoppieLogon);
}
notifier.sendMessage($"Connected. Running version v{version}");
sendDebug($"vPilot Pushover connected and enabled on v{version}");
updateChecker();
} else {
sendDebug("vPilot Pushover plugin failed to load. Check your vPilot-Pushover.ini");
}
}
/*
*
* Send debug message to vPilot
*
*/
public void sendDebug( String text ) {
vPilot.PostDebugMessage(text);
}
/*
*
* Hook: Network connected
*
*/
private void onNetworkConnectedHandler( object sender, NetworkConnectedEventArgs e ) {
connectedCallsign = e.Callsign;
if (settingHoppieEnabled) {
acars.start();
}
}
/*
*
* Hook: Network disconnected
*
*/
private void onNetworkDisconnectedHandler( object sender, EventArgs e ) {
connectedCallsign = null;
if (settingHoppieEnabled) {
acars.stop();
}
if (settingDisconnectEnabled) {
notifier.sendMessage("Disconnected from network", "vPilot", 1);
}
}
/*
*
* Hook: Private Message
*
*/
private void onPrivateMessageReceivedHandler( object sender, PrivateMessageReceivedEventArgs e ) {
string from = e.From;
string message = e.Message;
notifier.sendMessage(message, from, 1);
}
/*
*
* Hook: Radio Message
*
*/
private void onRadioMessageReceivedHandler( object sender, RadioMessageReceivedEventArgs e ) {
string from = e.From;
string message = e.Message;
if (message.Contains(connectedCallsign)) {
notifier.sendMessage(message, from, 1);
}
}
/*
*
* Hook: SELCAL Message
*
*/
private void onSelcalAlertReceivedHandler( object sender, SelcalAlertReceivedEventArgs e ) {
string from = e.From;
notifier.sendMessage("SELCAL Alert", from, 1);
}
/*
*
* Load plugin settings
*
*/
private void loadSettings() {
RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("Software\\vPilot");
if (registryKey != null) {
string vPilotPath = (string)registryKey.GetValue("Install_Dir");
string configFile = vPilotPath + "\\Plugins\\vPilot-Pushover.ini";
settingsFile = new IniFile(configFile);
// Set all values
settingDriver = settingsFile.KeyExists("Driver", "General") ? settingsFile.Read("Driver", "General") : null;
settingPushoverToken = settingsFile.KeyExists("ApiKey", "Pushover") ? settingsFile.Read("ApiKey", "Pushover") : null;
settingPushoverUser = settingsFile.KeyExists("UserKey", "Pushover") ? settingsFile.Read("UserKey", "Pushover") : null;
settingPushoverDevice = settingsFile.KeyExists("Device", "Pushover") ? settingsFile.Read("Device", "Pushover") : null;
settingHoppieEnabled = settingsFile.KeyExists("Enabled", "Hoppie") ? Boolean.Parse(settingsFile.Read("Enabled", "Hoppie")) : false;
settingHoppieLogon = settingsFile.KeyExists("LogonCode", "Hoppie") ? settingsFile.Read("LogonCode", "Hoppie") : null;
settingPrivateEnabled = settingsFile.KeyExists("Enabled", "RelayPrivate") ? Boolean.Parse(settingsFile.Read("Enabled", "RelayPrivate")) : false;
settingRadioEnabled = settingsFile.KeyExists("Enabled", "RelayRadio") ? Boolean.Parse(settingsFile.Read("Enabled", "RelayRadio")) : false;
settingSelcalEnabled = settingsFile.KeyExists("Enabled", "RelaySelcal") ? Boolean.Parse(settingsFile.Read("Enabled", "RelaySelcal")) : false;
settingTelegramBotToken = settingsFile.KeyExists("BotToken", "Telegram") ? settingsFile.Read("BotToken", "Telegram") : null;
settingTelegramChatId = settingsFile.KeyExists("ChatId", "Telegram") ? settingsFile.Read("ChatId", "Telegram") : null;
settingDisconnectEnabled = settingsFile.KeyExists("Enabled", "Disconnect") ? Boolean.Parse(settingsFile.Read("Enabled", "Disconnect")) : false;
// Validate values
if (settingHoppieEnabled && settingHoppieLogon == null) {
sendDebug("Hoppie logon code not set. Check your vPilot-Pushover.ini");
notifier.sendMessage("Hoppie logon code not set. Check your vPilot-Pushover.ini");
}
settingsLoaded = true;
} else {
sendDebug("Registry key not found. Is vPilot installed correctly?");
}
}
/*
*
* Check if is update available
*
*/
private async void updateChecker() {
using (HttpClient httpClient = new HttpClient()) {
try {
HttpResponseMessage response = await httpClient.GetAsync("https://raw.githubusercontent.com/blt950/vPilot-Pushover/main/version.txt");
if (response.IsSuccessStatusCode) {
string responseContent = await response.Content.ReadAsStringAsync();
if (responseContent != (version)) {
await Task.Delay(5000);
sendDebug($"Update available. Latest version is v{responseContent}");
notifier.sendMessage($"Update available. Latest version is v{responseContent}. Download newest version at https://blt950.com", "vPilot Pushover Plugin");
}
} else {
sendDebug($"[Update Checker] HttpResponse request failed with status code: {response.StatusCode}");
}
} catch (Exception ex) {
sendDebug($"[Update Checker] An HttpResponse error occurred: {ex.Message}");
}
}
}
}
}