Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CreateAccount.cs
1using System.Collections.Generic;
2using System.Text;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
8
10{
15 {
20 : base(@"^create\s+account\s+(?'Name'[^\s]+)(\s+(?'Password'[^\s]+))?(\s+(?'EMail'[^\s]+))?(\s+(?'PhoneNr'[^\s]+))?$")
21 {
22 }
23
27 public override string Name => "Create";
28
37 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
38 ResponseCallbackHandler ResponseCallback)
39 {
40 int c = Arguments.Length;
41 string Name = c < 2 ? string.Empty : Arguments[1];
42 string Password = c < 3 ? string.Empty : Arguments[2];
43 string EMail = c < 4 ? string.Empty : Arguments[3];
44 string PhoneNr = c < 5 ? string.Empty : Arguments[4];
45
46 KeyValuePair<IAccount, string[]> P = await XmppServerModule.PersistenceLayer.CreateAccount(string.Empty, Name,
47 string.IsNullOrEmpty(Password) ? Hashes.BinaryToString(Gateway.NextBytes(16)) : Password,
48 string.IsNullOrEmpty(EMail) ? new XmppAddress(State.To).BareJid.Value : EMail,
49 PhoneNr, State.To);
50
51 IAccount Account = P.Key;
52
53 if (Account is null)
54 {
55 await XmppServerModule.SendErrorMessage("An account with name `" + Name + "` already exists.", string.Empty, ResponseCallback);
56
57 string[] Alternatives = P.Value;
58 if (!(Alternatives is null) && Alternatives.Length > 0)
59 {
60 StringBuilder Markdown = new StringBuilder();
61
62 Markdown.AppendLine("Alternative name suggestions:");
63 Markdown.AppendLine();
64
65 foreach (string Alternative in Alternatives)
66 {
67 Markdown.Append("* `");
68 Markdown.Append(Alternative);
69 Markdown.AppendLine("`");
70 }
71
72 await ResponseCallback(Markdown.ToString(), string.Empty);
73 }
74 }
75 else
76 {
77 StringBuilder Markdown = new StringBuilder();
78
79 Markdown.AppendLine("Information for user:");
80 Markdown.AppendLine();
81 Markdown.AppendLine("| Account ||");
82 Markdown.AppendLine("|:----|:----|");
83 Markdown.Append("| Domain: | `");
84 Markdown.Append(Gateway.Domain);
85 Markdown.AppendLine("` |");
86 Markdown.Append("| Account: | `");
87 Markdown.Append(Account.UserName);
88 Markdown.AppendLine("` |");
89 Markdown.Append("| Password: | `");
90 Markdown.Append(Account.Password);
91 Markdown.AppendLine("` |");
92
93 await ResponseCallback(Markdown.ToString(), string.Empty);
94 }
95 }
96
101 public override HelpItem[] GetHelp()
102 {
103 return new HelpItem[]
104 {
105 new HelpItem("create account NAME[ PASSWORD[ EMAIL[ PHONE]]]", "Creates a new account with the account name given by `NAME`. Optional `PASSWORD`, `EMAIL` and `PHONE` parameters can also be provided.")
106 };
107 }
108 }
109}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static CaseInsensitiveString Domain
Domain name.
Definition: Gateway.cs:2354
static byte[] NextBytes(int NrBytes)
Generates an array of random bytes.
Definition: Gateway.cs:3534
Contains information about one XMPP address.
Definition: XmppAddress.cs:9
CaseInsensitiveString BareJid
Bare JID
Definition: XmppAddress.cs:45
Contains methods for simple hash calculations.
Definition: Hashes.cs:59
static string BinaryToString(byte[] Data)
Converts an array of bytes to a string with their hexadecimal representations (in lower case).
Definition: Hashes.cs:65
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
override HelpItem[] GetHelp()
Gets help about the command.
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
Service Module hosting the XMPP broker and its components.
CaseInsensitiveString UserName
User Name
Definition: IAccount.cs:24
Interface for XMPP user accounts.
Definition: IAccount.cs:9
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.