Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateKey.cs
1using System.Text;
2using System.Text.RegularExpressions;
3using System.Threading.Tasks;
7
9{
13 public class CreateKey : CommandRegEx
14 {
18 public CreateKey()
19 : base("^create\\s+key\\s+(?'Accounts'[1-9]\\d{0,7})\\s+\"(?'Owner'[^\"]+)\"(\\s+(?'EMail'[^\\s]+))?$")
20 {
21 }
22
26 public override string Name => "Create";
27
36 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
37 ResponseCallbackHandler ResponseCallback)
38 {
39 int c = Arguments.Length;
40
41 if (c < 2 || !int.TryParse(Arguments[1], out int Accounts))
42 {
43 await ResponseCallback("Invalid number of accounts: \"" + Arguments[2] + "\"", string.Empty);
44 return;
45 }
46
47 string Owner;
48
49 if (c < 3 || string.IsNullOrEmpty(Owner = Arguments[2]))
50 {
51 await ResponseCallback("Owner must be specified.", string.Empty);
52 return;
53 }
54
55 string EMail = c < 4 ? string.Empty : Arguments[3];
56
57 ApiKey Key = await CreateApiKey.Create(Accounts, Owner,
58 string.IsNullOrEmpty(EMail) ? new XmppAddress(State.To).BareJid.Value : EMail,
59 State.To);
60
61 StringBuilder Markdown = new StringBuilder();
62
63 Markdown.AppendLine("Information for user:");
64 Markdown.AppendLine();
65 Markdown.AppendLine("| API Key ||");
66 Markdown.AppendLine("|:----|:----|");
67 Markdown.Append("| Key: | `");
68 Markdown.Append(Key.Key);
69 Markdown.AppendLine("` |");
70 Markdown.Append("| Secret: | `");
71 Markdown.Append(Key.Secret);
72 Markdown.AppendLine("` |");
73 Markdown.Append("| \\#Accounts: | `");
74 Markdown.Append(Key.MaxAccounts.ToString());
75 Markdown.AppendLine("` |");
76
77 await ResponseCallback(Markdown.ToString(), string.Empty);
78 }
79
84 public override HelpItem[] GetHelp()
85 {
86 return new HelpItem[]
87 {
88 new HelpItem("create key NRACCOUNTS \"OWNER\"[ EMAIL]", "Creates a new API key. It can be used to create `NRACCOUNTS` accounts. The `OWNER` and `EMAIL` parameters provide extra information.")
89 };
90 }
91 }
92}
Component managing accounts.
Definition: Accounts.cs:15
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
override HelpItem[] GetHelp()
Gets help about the command.
Definition: CreateKey.cs:84
override string Name
Command name
Definition: CreateKey.cs:26
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: CreateKey.cs:36
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Contains an item of information about a command.
Definition: HelpItem.cs:9
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.