Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Enter.cs
1using System.Text;
2using System.Threading.Tasks;
6
8{
13 {
17 public Enter()
18 : base()
19 {
20 }
21
25 public override string Name => "Enter";
26
34 public override bool AppliesTo(string CommandLine, string[] Arguments, out object Details)
35 {
36 Details = null;
37 int c = Arguments.Length;
38 return c == 2 || c == 3;
39 }
40
48 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, ResponseCallbackHandler ResponseCallback)
49 {
50 if (Gateway.MucClient is null)
51 {
52 await ResponseCallback("Multi-User Chat disabled.", string.Empty);
53 return;
54 }
55
56 ParseRoom(Arguments[0], out string RoomId, out string Domain);
57
58 string NickName = Arguments[1];
59 string Password = Arguments.Length > 2 ? Arguments[2] : string.Empty;
60
61 await Execute(RoomId, Domain, NickName, Password, this.Permanent, true, ResponseCallback);
62 }
63
64 internal static Task Execute(string RoomId, string Domain, string NickName, string Password, bool Permanent,
65 bool RegisterIfRequested, ResponseCallbackHandler ResponseCallback)
66 {
67 Gateway.MucClient.EnterRoom(RoomId, Domain, NickName, Password, async (Sender, e) =>
68 {
69 if (e.Ok)
70 {
71 StringBuilder sb = new StringBuilder();
72 bool First = true;
73
74 sb.Append("Entered in `");
75 sb.Append(e.RoomId);
76 sb.Append('@');
77 sb.Append(e.Domain);
78 sb.Append("` as `");
79 sb.Append(e.NickName);
80 sb.Append("`.");
81
82 foreach (MucStatus Status in e.MucStatus)
83 {
84 if (First)
85 {
86 First = false;
87 sb.Append(" Status: ");
88 }
89 else
90 sb.Append(", ");
91
92 sb.Append('`');
93 sb.Append(Status.ToString());
94 sb.Append('`');
95 }
96
97 if (!First)
98 sb.Append(".");
99
100 await ResponseCallback(sb.ToString(), string.Empty);
101 await XmppServerModule.Instance.EnterRoom(RoomId, Domain, NickName, Password, Permanent);
102
103 if (e.Role == Role.None || e.Role == Role.Visitor)
104 {
105 await ResponseCallback("Requesting voice privileges...", string.Empty);
106 await Gateway.MucClient.RequestVoice(RoomId, Domain);
107 }
108 }
109 else if (e.StanzaError is Networking.XMPP.StanzaErrors.RegistrationRequiredException && RegisterIfRequested)
110 {
111 await ResponseCallback("Room registration required.", string.Empty);
112 await ResponseCallback("Requesting registration form...", string.Empty);
113
114 await Gateway.MucClient.GetRoomRegistrationForm(RoomId, Domain, async (sender2, e2) =>
115 {
116 if (e2.Ok && !(e2.Form is null))
117 {
118 foreach (Field F in e2.Form.Fields)
119 {
120 switch (F.Var)
121 {
122 case "muc#register_first":
123 string s = IoTGateway.Setup.LegalIdentityConfiguration.Instance.FirstName;
124 if (string.IsNullOrEmpty(s))
125 s = "N/A";
126
127 await F.SetValue(s);
128 break;
129
130 case "muc#register_last":
131 s = IoTGateway.Setup.LegalIdentityConfiguration.Instance.LastName;
132 if (string.IsNullOrEmpty(s))
133 s = "N/A";
134
135 await F.SetValue(s);
136 break;
137
138 case "muc#register_roomnick":
139 await F.SetValue(NickName);
140 break;
141
142 case "muc#register_url":
143 case "muc#register_faqentry":
144 await F.SetValue(Gateway.GetUrl("/"));
145 break;
146
147 case "muc#register_email":
148 await F.SetValue(Gateway.XmppClient.BareJID);
149 break;
150 }
151 }
152
153 await ResponseCallback("Submitting registration form...", string.Empty);
154 await e2.Form.Submit();
155 }
156 else if (e2.AlreadyRegistered)
157 await ResponseCallback("Already registered with nick name `" + e2.UserName + "`", string.Empty);
158 else
159 await ResponseCallback(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to get registration form." : e2.ErrorText, string.Empty);
160
161 }, (sender2, e2) =>
162 {
163 if (e2.Ok)
164 return ResponseCallback("Room registration submitted. You can enter the room as soon as the request has been approved...", string.Empty);
165 else
166 return ResponseCallback(string.IsNullOrEmpty(e2.ErrorText) ? "Unable to register with room." : e2.ErrorText, string.Empty);
167 }, null);
168 }
169 else
170 await ResponseCallback(string.IsNullOrEmpty(e.ErrorText) ? "Unable to enter room." : e.ErrorText, string.Empty);
171 }, null);
172
173 return Task.CompletedTask;
174 }
175
179 public virtual bool Permanent => false;
180
181 internal static void ParseRoom(string s, out string RoomId, out string Domain)
182 {
183 int i = s.IndexOf('@');
184
185 if (i < 0)
186 {
187 RoomId = s;
189 }
190 else
191 {
192 RoomId = s.Substring(0, i);
193 Domain = s.Substring(i + 1);
194 }
195 }
196
197 public override HelpItem[] GetHelp()
198 {
199 return new HelpItem[] { new HelpItem(this.Name + " " + this.ParameterName + " " + this.Parameter2Name + "[ PASSWORD]", this.HelpParagraphs) };
200 }
201
205 public override string ParameterName => "ROOM";
206
210 public override string Parameter2Name => "NICKNAME";
211
215 public override string[] HelpParagraphs => new string[]
216 {
217 "Temporarily enters a Multi-User Chat Room named `ROOM`, using the nick-name `NICKNAME`.",
218 "You can leave the room issuing a `leave` command. The broker also leaves the room when it is restarted.",
219 "An optional `PASSWORD` can be provided to enter password-protected rooms."
220 };
221 }
222}
Static class managing the runtime environment of the IoT Gateway.
Definition: Gateway.cs:126
static MultiUserChatClient MucClient
XMPP Multi-User Chat Protocol (MUC) Client.
Definition: Gateway.cs:3267
Task EnterRoom(string RoomId, string Domain, string NickName, EventHandlerAsync< UserPresenceEventArgs > Callback, object State)
Enter a chat room.
string ComponentAddress
Publish/Subscribe component address.
An administrative command taking two parameters.
Contains an item of information about a command.
Definition: HelpItem.cs:9
Asks the broker to enter a room.
Definition: Enter.cs:13
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: Enter.cs:48
override string Name
Command name
Definition: Enter.cs:25
Enter()
Asks the broker to enter a room.
Definition: Enter.cs:17
override HelpItem[] GetHelp()
Gets help about the command.
Definition: Enter.cs:197
override bool AppliesTo(string CommandLine, string[] Arguments, out object Details)
If the command is applicable to the given command line.
Definition: Enter.cs:34
virtual bool Permanent
If entering the room permanently (true) or temporarily (false).
Definition: Enter.cs:179
Service Module hosting the XMPP broker and its components.
Role
Role enumeration
Definition: Enumerations.cs:42
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.