Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Notify.cs
1using System.Collections.Generic;
2using System.Text;
3using System.Text.RegularExpressions;
4using System.Threading.Tasks;
7
9{
14 public class Notify : CommandRegEx
15 {
20 public Notify()
21 : base("^notify(\\s+(?'SubCommand'(add|remove)))?(\\s+(([^@/<>'\\\"\\s]+)@)([^@/<> '\\\"\\s]+))*$")
22 {
23 }
24
28 public override string Name => "Notify";
29
38 public override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details,
39 ResponseCallbackHandler ResponseCallback)
40 {
41 int i = 0;
42 int c = Arguments.Length;
43
44 if (c == 0)
45 {
46 StringBuilder Markdown = new StringBuilder();
47 bool First = true;
48
49 foreach (CaseInsensitiveString Jid in IoTGateway.Setup.NotificationConfiguration.Instance.Addresses)
50 {
51 if (First)
52 First = false;
53 else
54 Markdown.Append("; ");
55
56 Markdown.Append('`');
57 Markdown.Append(Jid);
58 Markdown.Append('`');
59 }
60
61 await ResponseCallback(Markdown.ToString(), string.Empty);
62 }
63 else
64 {
65 List<CaseInsensitiveString> Addresses = new List<CaseInsensitiveString>();
66 List<CaseInsensitiveString> AddressesOrg = new List<CaseInsensitiveString>();
67 CaseInsensitiveString BareSenderJid = XmppClient.GetBareJID(State.To);
68 bool IsFromBareJid = XmppClient.BareJidRegEx.IsMatch(BareSenderJid.LowerCase);
69 bool ContainsSelf = false;
70 bool Remove = false;
71 bool Relative = false;
72 bool Updated = false;
73
74 switch (Arguments[0].ToLower())
75 {
76 case "add":
77 i++;
78 Relative = true;
79 break;
80
81 case "remove":
82 i++;
83 Remove = true;
84 Relative = true;
85 break;
86 }
87
88 foreach (CaseInsensitiveString Jid in IoTGateway.Setup.NotificationConfiguration.Instance.Addresses)
89 AddressesOrg.Add(Jid);
90
91 if (Relative)
92 Addresses.AddRange(AddressesOrg);
93
94 while (i < c)
95 {
96 string JID = Arguments[i++];
98 if (cs == IoTGateway.Gateway.XmppClient.BareJID)
99 continue;
100
101 if (Remove)
102 {
103 if (Addresses.Remove(cs))
104 Updated = true;
105 }
106 else
107 {
108 if (!Addresses.Contains(cs))
109 {
110 Addresses.Add(cs);
111
112 if (Relative || !AddressesOrg.Contains(cs))
113 Updated = true;
114 }
115 }
116 }
117
118 foreach (CaseInsensitiveString Jid in Addresses)
119 {
120 if (Jid == BareSenderJid)
121 ContainsSelf = true;
122 }
123
124 if (ContainsSelf || !IsFromBareJid)
125 {
126 IoTGateway.Setup.NotificationConfiguration.Instance.Addresses = Addresses.ToArray();
127 await Persistence.Database.Update(IoTGateway.Setup.NotificationConfiguration.Instance);
128
129 if (Updated)
130 await ResponseCallback("Notification list updated.", string.Empty);
131 else
132 await ResponseCallback("Notification list up to date.", string.Empty);
133 }
134 else
135 await XmppServerModule.SendErrorMessage("Must be included: " + BareSenderJid, string.Empty, ResponseCallback);
136 }
137 }
138
143 public override HelpItem[] GetHelp()
144 {
145 return new HelpItem[]
146 {
147 new HelpItem("notify", "Lists recipients of JIDs that will be notified of events on the broker. They also have access to chat admin interface."),
148 new HelpItem("notify[ JID[ JID2[...]]]", "Sets the recipients of notifications, and those with access to chat admin interface. You cannot remove yourself from the list."),
149 new HelpItem("notify add [ JID[ JID2[...]]]", "Adds new recipients to the list of notification recipients."),
150 new HelpItem("notify remove [ JID[ JID2[...]]]", "Removes recipients from the list of notification recipients.")
151 };
152 }
153 }
154}
Manages an XMPP client connection. Implements XMPP, as defined in https://tools.ietf....
Definition: XmppClient.cs:59
static string GetBareJID(string JID)
Gets the Bare JID from a JID, which may be a Full JID.
Definition: XmppClient.cs:6901
static readonly Regex BareJidRegEx
Regular expression for Bare JIDs
Definition: XmppClient.cs:188
Represents a case-insensitive string.
string LowerCase
Lower-case representation of the case-insensitive string.
An administrative command whose syntax is validated with a regular expression.
Definition: CommandRegEx.cs:12
Displays who to notify about server events. Can also be used to update list of whom to notify....
Definition: Notify.cs:15
Notify()
Displays who to notify about server events. Can also be used to update list of whom to notify....
Definition: Notify.cs:20
override string Name
Command name
Definition: Notify.cs:28
override HelpItem[] GetHelp()
Gets help about the command.
Definition: Notify.cs:143
override async Task Execute(ChatState State, string[] Arguments, string OrgMessage, Match Details, ResponseCallbackHandler ResponseCallback)
Executes the command.
Definition: Notify.cs:38
Contains an item of information about a command.
Definition: HelpItem.cs:9
Service Module hosting the XMPP broker and its components.
delegate Task< string > ResponseCallbackHandler(string Markdown, string MessageId)
Delegate for response callback handler methods.