Improve regexes & comment some code

This commit is contained in:
Starbeamrainbowlabs 2018-11-23 17:55:01 +00:00
parent a81f012c0c
commit 0bf68aa35c
Signed by: sbrl
GPG key ID: 1BE5172E637709C2

View file

@ -96,6 +96,14 @@ namespace RhinoReminds
} }
} }
public void SetAvatar(string avatarFilepath)
{
if (!client.Connected)
throw new InvalidOperationException("Error: The XMPP client isn't connected, so the avatar can't be updated.");
client.SetAvatar(avatarFilepath);
}
private void messageHandler(Message message) private void messageHandler(Message message)
{ {
if (!AllowedDomains.Contains("*") && !AllowedDomains.Contains(message.From.Domain)) { if (!AllowedDomains.Contains("*") && !AllowedDomains.Contains(message.From.Domain)) {
@ -185,12 +193,18 @@ namespace RhinoReminds
@"^me\s+", @"^me\s+",
@"^on\s+", @"^on\s+",
@"my", @"my",
@"you",
@"your",
@"&" // Ampersands cause a crash when sending!
}, new string[] { }, new string[] {
" ", " ",
"", "",
"", "",
"", "",
"your" "your",
@"me",
@"my",
@"and"
}, RegexOptions.IgnoreCase).Trim(); }, RegexOptions.IgnoreCase).Trim();
sendChatReply(message, $"Ok! I'll remind you {reminder} at {dateTime}."); sendChatReply(message, $"Ok! I'll remind you {reminder} at {dateTime}.");
@ -205,6 +219,11 @@ namespace RhinoReminds
} }
} }
/// <summary>
/// Sends a chat message to the specified JID.
/// </summary>
/// <param name="to">The JID to send the message to.</param>
/// <param name="message">The messaage to send.</param>
private void sendChatMessage(Jid to, string message) { private void sendChatMessage(Jid to, string message) {
//Console.WriteLine($"[Rhino/Send/Chat] Sending {message} -> {to}"); //Console.WriteLine($"[Rhino/Send/Chat] Sending {message} -> {to}");
client.SendMessage( client.SendMessage(
@ -212,6 +231,11 @@ namespace RhinoReminds
null, null, MessageType.Chat null, null, MessageType.Chat
); );
} }
/// <summary>
/// Sends a chat message in direct reply to a given incoming message.
/// </summary>
/// <param name="originalMessage">Original message.</param>
/// <param name="reply">Reply.</param>
private void sendChatReply(Message originalMessage, string reply) private void sendChatReply(Message originalMessage, string reply)
{ {
//Console.WriteLine($"[Rhino/Send/Reply] Sending {reply} -> {originalMessage.From}"); //Console.WriteLine($"[Rhino/Send/Reply] Sending {reply} -> {originalMessage.From}");
@ -272,10 +296,17 @@ namespace RhinoReminds
Console.WriteLine($"[Rhino/Reminderd] Sending notification {nextReminder}"); Console.WriteLine($"[Rhino/Reminderd] Sending notification {nextReminder}");
try
{
sendChatMessage( sendChatMessage(
nextReminder.Jid, nextReminder.Jid,
$"Hello! You asked me to remind you {nextReminder.Message} at {nextReminder.Time}.".Trim().Replace(@"\s+", " ") $"Hello! You asked me to remind you {nextReminder.Message} at {nextReminder.Time}.".Trim().Replace(@"\s+", " ")
); );
} catch (Exception error) {
Console.Error.WriteLine($"[Rhino/Reminderd] Caught error sending message to client: {error}");
Console.Error.WriteLine($"[Rhink/Reminderd] Offending reminder: {nextReminder}");
sendChatMessage(nextReminder.Jid, "Oops! I encountered an error sending you a reminder. Please contact my operator!");
}
if (nextWaitingTime.TotalMilliseconds < 0) { if (nextWaitingTime.TotalMilliseconds < 0) {
sendChatMessage( sendChatMessage(
nextReminder.Jid, nextReminder.Jid,