Add AllowedDOmains property

This commit is contained in:
Starbeamrainbowlabs 2018-11-10 21:16:39 +00:00
parent a28845d2bd
commit f04c754037
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
2 changed files with 28 additions and 8 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
@ -22,6 +23,8 @@ namespace RhinoReminds
public string Hostname => Jid.Split('@')[1]; public string Hostname => Jid.Split('@')[1];
private readonly string password; private readonly string password;
public readonly List<string> AllowedDomains = new List<string>();
public string ReminderFilePath { get; set; } = "./reminders.xml"; public string ReminderFilePath { get; set; } = "./reminders.xml";
private ReminderList reminderList = new ReminderList(); private ReminderList reminderList = new ReminderList();
@ -62,6 +65,10 @@ namespace RhinoReminds
#region XMPP Event Handling #region XMPP Event Handling
private bool subscriptionRequestHandler(Jid from) private bool subscriptionRequestHandler(Jid from)
{ {
if (!AllowedDomains.Contains("*") && !AllowedDomains.Contains(from.Domain)) {
sendChatMessage(from, "Sorry! The domain of your JID doesn't match the ones in my allowed list.");
return false;
}
Console.WriteLine($"[Rhino/SubscriptionRequest] Approving subscription from {from}"); Console.WriteLine($"[Rhino/SubscriptionRequest] Approving subscription from {from}");
return true; return true;
} }
@ -90,6 +97,11 @@ namespace RhinoReminds
private void messageHandler(Message message) private void messageHandler(Message message)
{ {
if (!AllowedDomains.Contains("*") && !AllowedDomains.Contains(message.From.Domain)) {
sendChatMessage(message.From, "Sorry! The domain of your JID doesn't match the ones in my allowed list.");
return;
}
string messageText = message.Body; string messageText = message.Body;
string[] parts = Regex.Split(messageText.Trim(), @"\s+"); string[] parts = Regex.Split(messageText.Trim(), @"\s+");
string instruction = parts[0].ToLower(); string instruction = parts[0].ToLower();
@ -117,8 +129,6 @@ namespace RhinoReminds
break; break;
case "remind": case "remind":
Console.WriteLine("[Rhino/Reciever] Identified remind request");
DateTime dateTime; string rawDateTimeString; DateTime dateTime; string rawDateTimeString;
try { try {
dateTime = AIRecogniser.RecogniseDateTime(messageText, out rawDateTimeString); dateTime = AIRecogniser.RecogniseDateTime(messageText, out rawDateTimeString);
@ -183,10 +193,10 @@ namespace RhinoReminds
Reminder nextReminder = reminderList.GetNextReminder(); Reminder nextReminder = reminderList.GetNextReminder();
reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => { reminderList.OnReminderListUpdate += (object sender, Reminder newReminder) => {
Reminder newNextReminder = reminderList.GetNextReminder(); Reminder newNextReminder = reminderList.GetNextReminder();
Console.WriteLine("[Rhino/Reminderd/Canceller] Reminder added - comparing."); //Console.WriteLine("[Rhino/Reminderd/Canceller] Reminder added - comparing.");
Console.WriteLine($"[Rhino/Reminderd/Canceller] {nextReminder} / {newNextReminder}"); //Console.WriteLine($"[Rhino/Reminderd/Canceller] {nextReminder} / {newNextReminder}");
if (nextReminder != newNextReminder) { if (nextReminder != newNextReminder) {
Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling"); //Console.WriteLine($"[Rhino/Reminderd/Canceller] Cancelling");
nextReminder = newNextReminder; nextReminder = newNextReminder;
cancellationSource.Cancel(); cancellationSource.Cancel();
} }
@ -199,15 +209,15 @@ namespace RhinoReminds
if (nextReminder != null) { if (nextReminder != null) {
nextWaitingTime = nextReminder.Time - DateTime.Now; nextWaitingTime = nextReminder.Time - DateTime.Now;
if (DateTime.Now < nextReminder.Time) { if (DateTime.Now < nextReminder.Time) {
Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}"); //Console.WriteLine($"[Rhino/Reminderd] Sleeping for {nextWaitingTime}");
await Task.Delay(nextWaitingTime, cancellationToken); await Task.Delay(nextWaitingTime, cancellationToken);
} }
} else { } else {
Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted"); //Console.WriteLine("[Rhino/Reminderd] Sleeping until interrupted");
await Task.Delay(Timeout.Infinite, cancellationToken); await Task.Delay(Timeout.Infinite, cancellationToken);
} }
} catch (TaskCanceledException) { } catch (TaskCanceledException) {
Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating"); //Console.WriteLine("[Rhino/Reminderd] Sleep interrupted, recalculating");
cancellationSource = new CancellationTokenSource(); cancellationSource = new CancellationTokenSource();
cancellationToken = cancellationSource.Token; cancellationToken = cancellationSource.Token;
continue; continue;

View File

@ -9,6 +9,7 @@ namespace RhinoReminds
{ {
public class ProgramSettings public class ProgramSettings
{ {
public string AllowedDomain = "*";
public string Filepath = "./reminders.xml"; public string Filepath = "./reminders.xml";
public string Jid = null; public string Jid = null;
@ -30,6 +31,7 @@ namespace RhinoReminds
} }
switch (args[i]) { switch (args[i]) {
case "-h":
case "--help": case "--help":
Console.WriteLine("--- RhinoReminds ---"); Console.WriteLine("--- RhinoReminds ---");
Console.WriteLine("> An XMPP reminder bot"); Console.WriteLine("> An XMPP reminder bot");
@ -41,6 +43,7 @@ namespace RhinoReminds
Console.WriteLine("Options:"); Console.WriteLine("Options:");
Console.WriteLine(" -h --help Show this message"); Console.WriteLine(" -h --help Show this message");
Console.WriteLine($" -f --file Specify where to save reminders (default: {settings.Filepath})"); Console.WriteLine($" -f --file Specify where to save reminders (default: {settings.Filepath})");
Console.WriteLine(" --domain {domain} Set the domain users are allowed to originate at. Defaults to any domain.");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Environment Variables:"); Console.WriteLine("Environment Variables:");
Console.WriteLine(" XMPP_JID The JID to login to"); Console.WriteLine(" XMPP_JID The JID to login to");
@ -50,6 +53,12 @@ namespace RhinoReminds
case "--jid": case "--jid":
settings.Jid = args[++i]; settings.Jid = args[++i];
break; break;
case "-d":
case "--domain":
settings.AllowedDomain = args[++i];
break;
} }
} }
@ -66,6 +75,7 @@ namespace RhinoReminds
ClientListener client = new ClientListener(settings.Jid, settings.Password) { ClientListener client = new ClientListener(settings.Jid, settings.Password) {
ReminderFilePath = settings.Filepath ReminderFilePath = settings.Filepath
}; };
client.AllowedDomains.Add(settings.AllowedDomain);
client.Start().Wait(); client.Start().Wait();
} }
} }