RhinoReminds/RhinoReminds/Program.cs

97 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using S22.Xmpp;
using S22.Xmpp.Client;
using S22.Xmpp.Im;
namespace RhinoReminds
{
public class ProgramSettings
{
2018-11-10 21:16:39 +00:00
public string AllowedDomain = "*";
public string Filepath = "./reminders.xml";
public string Jid = null;
public string AvatarFilepath = string.Empty;
public string Password = null;
}
public static class Program
{
private static ProgramSettings settings = new ProgramSettings();
public static int Main(string[] args)
{
List<string> extras = new List<string>();
for (int i = 0; i < args.Length; i++)
{
if (!args[i].StartsWith("-")) {
extras.Add(args[i]);
continue;
}
switch (args[i]) {
2018-11-10 21:16:39 +00:00
case "-h":
case "--help":
Console.WriteLine("--- RhinoReminds ---");
Console.WriteLine("> An XMPP reminder bot");
Console.WriteLine(" By Starbeamrainbowlabs");
Console.WriteLine();
Console.WriteLine("Usage:");
Console.WriteLine(" mono RhinoReminds.exe {options}");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine(" -h --help Show this message");
Console.WriteLine($" -f --file Specify where to save reminders (default: {settings.Filepath})");
2018-11-10 21:16:39 +00:00
Console.WriteLine(" --domain {domain} Set the domain users are allowed to originate at. Defaults to any domain.");
Console.WriteLine(" --avatar Update the XMPP account's avatar to the specified image. By default the avatar is not updated.");
Console.WriteLine();
Console.WriteLine("Environment Variables:");
Console.WriteLine(" XMPP_JID The JID to login to");
Console.WriteLine(" XMPP_PASSWORD The password to login with");
return 0;
case "--jid":
settings.Jid = args[++i];
break;
2018-11-10 21:16:39 +00:00
case "-d":
case "--domain":
settings.AllowedDomain = args[++i];
break;
case "--avatar":
settings.AvatarFilepath = args[++i];
break;
}
}
settings.Jid = Environment.GetEnvironmentVariable("XMPP_JID");
settings.Password = Environment.GetEnvironmentVariable("XMPP_PASSWORD");
Run();
return 0;
}
public static void Run()
{
ClientListener client = new ClientListener(settings.Jid, settings.Password) {
ReminderFilePath = settings.Filepath
};
2018-11-10 21:16:39 +00:00
client.AllowedDomains.Add(settings.AllowedDomain);
// Connect to the server & start listening
Task clientListener = client.Start();
// Update the avatar if appropriate
if(settings.AvatarFilepath != string.Empty)
client.SetAvatar(settings.AvatarFilepath);
// Make sure the program doesn't exit whilst we're connected
clientListener.Wait();
}
}
}