Beispiel #1
0
        private async void GetFormHandler(object Sender, IqEventArgs e)
        {
            try
            {
                LinkedList <IThingReference> Nodes = null;
                XmlElement E;
                string     ServiceToken = XML.Attribute(e.Query, "st");
                string     DeviceToken  = XML.Attribute(e.Query, "dt");
                string     UserToken    = XML.Attribute(e.Query, "ut");

                foreach (XmlNode N in e.Query.ChildNodes)
                {
                    E = N as XmlElement;
                    if (E is null)
                    {
                        continue;
                    }

                    if (E.LocalName == "nd")
                    {
                        if (Nodes is null)
                        {
                            Nodes = new LinkedList <IThingReference>();
                        }

                        string NodeId    = XML.Attribute(E, "id");
                        string SourceId  = XML.Attribute(E, "src");
                        string Partition = XML.Attribute(E, "pt");

                        if (this.OnGetNode is null)
                        {
                            Nodes.AddLast(new ThingReference(NodeId, SourceId, Partition));
                        }
                        else
                        {
                            IThingReference Ref = await this.OnGetNode(NodeId, SourceId, Partition);

                            if (Ref is null)
                            {
                                throw new ItemNotFoundException("Node not found.", e.IQ);
                            }

                            Nodes.AddLast(Ref);
                        }
                    }
                }

                ControlParameter[] Parameters;

                if (Nodes is null)
                {
                    Parameters = await this.GetControlParameters(null);

                    if (Parameters is null)
                    {
                        NotFound(e);
                        return;
                    }
                }
                else
                {
                    Dictionary <string, ControlParameter> Parameters1;
                    Dictionary <string, ControlParameter> Parameters2;
                    LinkedList <string> ToRemove = null;

                    Parameters  = null;
                    Parameters1 = null;

                    foreach (IThingReference Node in Nodes)
                    {
                        if (Parameters1 is null)
                        {
                            Parameters = await this.GetControlParameters(Node);

                            if (Parameters is null)
                            {
                                NotFound(e);
                                return;
                            }

                            Parameters1 = new Dictionary <string, ControlParameter>();

                            foreach (ControlParameter P in Parameters)
                            {
                                Parameters1[P.Name] = P;
                            }
                        }
                        else
                        {
                            Parameters2 = await this.GetControlParametersByName(Node);

                            if (Parameters2 is null)
                            {
                                NotFound(e);
                                return;
                            }

                            foreach (KeyValuePair <string, ControlParameter> P in Parameters1)
                            {
                                if (!Parameters2.TryGetValue(P.Key, out ControlParameter P2) || !P.Value.Equals(P2))
                                {
                                    if (ToRemove is null)
                                    {
                                        ToRemove = new LinkedList <string>();
                                    }

                                    ToRemove.AddLast(P.Key);
                                }
                            }

                            if (ToRemove != null)
                            {
                                foreach (string Key in ToRemove)
                                {
                                    Parameters1.Remove(Key);
                                }

                                ToRemove = null;
                            }
                        }
                    }

                    List <ControlParameter> Left = new List <ControlParameter>();

                    foreach (ControlParameter P in Parameters)
                    {
                        if (Parameters1.ContainsKey(P.Name))
                        {
                            Left.Add(P);
                        }
                    }

                    Parameters = Left.ToArray();
                }

                if (this.provisioningClient != null)
                {
                    int      i, c = Parameters.Length;
                    string[] ParameterNames = new string[c];

                    for (i = 0; i < c; i++)
                    {
                        ParameterNames[i] = Parameters[i].Name;
                    }

                    this.provisioningClient.CanControl(e.FromBareJid, Nodes, ParameterNames,
                                                       ServiceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       DeviceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       UserToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       (sender2, e2) =>
                    {
                        if (e2.Ok && e2.CanControl)
                        {
                            if (e2.ParameterNames != null)
                            {
                                List <ControlParameter> Parameters2 = new List <ControlParameter>();

                                foreach (ControlParameter P in Parameters)
                                {
                                    if (Array.IndexOf <string>(e2.ParameterNames, P.Name) >= 0)
                                    {
                                        Parameters2.Add(P);
                                    }
                                }

                                Parameters = Parameters2.ToArray();

                                if (Parameters.Length == 0)
                                {
                                    e.IqError("<error type='cancel'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
                                              "<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>Access denied.</text></error>");
                                    return;
                                }
                            }

                            this.ReturnForm(e, Parameters, Nodes);
                        }
                        else
                        {
                            e.IqError("<error type='cancel'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
                                      "<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>Access denied.</text></error>");
                        }
                    }, null);
                }
                else
                {
                    this.ReturnForm(e, Parameters, Nodes);
                }
            }
            catch (Exception ex)
            {
                e.IqError(ex);
            }
        }
Beispiel #2
0
        private async void GetFormHandler(object Sender, IqEventArgs e)
        {
            try
            {
                LinkedList <ThingReference> Nodes = null;
                XmlElement E;

                foreach (XmlNode N in e.Query.ChildNodes)
                {
                    E = N as XmlElement;
                    if (E == null)
                    {
                        continue;
                    }

                    if (E.LocalName == "nd")
                    {
                        if (Nodes == null)
                        {
                            Nodes = new LinkedList <ThingReference>();
                        }

                        Nodes.AddLast(new ThingReference(
                                          XML.Attribute(E, "id"),
                                          XML.Attribute(E, "src"),
                                          XML.Attribute(E, "pt")));
                    }
                }

                ControlParameter[] Parameters;

                if (Nodes == null)
                {
                    Parameters = await this.GetControlParameters(null);

                    if (Parameters == null)
                    {
                        NotFound(e);
                        return;
                    }
                }
                else
                {
                    Dictionary <string, ControlParameter> Parameters1;
                    Dictionary <string, ControlParameter> Parameters2;
                    LinkedList <string> ToRemove = null;

                    Parameters  = null;
                    Parameters1 = null;

                    foreach (ThingReference Node in Nodes)
                    {
                        if (Parameters1 == null)
                        {
                            Parameters = await this.GetControlParameters(Node);

                            if (Parameters == null)
                            {
                                NotFound(e);
                                return;
                            }

                            Parameters1 = new Dictionary <string, ControlParameter>();

                            foreach (ControlParameter P in Parameters)
                            {
                                Parameters1[P.Name] = P;
                            }
                        }
                        else
                        {
                            Parameters2 = await this.GetControlParametersByName(Node);

                            if (Parameters2 == null)
                            {
                                NotFound(e);
                                return;
                            }

                            foreach (KeyValuePair <string, ControlParameter> P in Parameters1)
                            {
                                if (!Parameters2.TryGetValue(P.Key, out ControlParameter P2) || !P.Value.Equals(P2))
                                {
                                    if (ToRemove == null)
                                    {
                                        ToRemove = new LinkedList <string>();
                                    }

                                    ToRemove.AddLast(P.Key);
                                }
                            }

                            if (ToRemove != null)
                            {
                                foreach (string Key in ToRemove)
                                {
                                    Parameters1.Remove(Key);
                                }

                                ToRemove = null;
                            }
                        }
                    }

                    List <ControlParameter> Left = new List <ControlParameter>();

                    foreach (ControlParameter P in Parameters)
                    {
                        if (Parameters1.ContainsKey(P.Name))
                        {
                            Left.Add(P);
                        }
                    }

                    Parameters = Left.ToArray();
                }

                StringBuilder  Xml    = new StringBuilder();
                XmlWriter      Output = XmlWriter.Create(Xml, XML.WriterSettings(false, true));
                ThingReference FirstNode;

                Output.WriteStartElement("x", XmppClient.NamespaceData);
                Output.WriteAttributeString("xmlns", "xdv", null, XmppClient.NamespaceDataValidate);
                Output.WriteAttributeString("xmlns", "xdl", null, XmppClient.NamespaceDataLayout);
                Output.WriteAttributeString("xmlns", "xdd", null, XmppClient.NamespaceDynamicForms);

                if (Nodes == null)
                {
                    FirstNode = null;
                    Output.WriteElementString("title", this.client.BareJID);
                }
                else
                {
                    FirstNode = Nodes.First.Value;

                    if (Nodes.First.Next == null)
                    {
                        Output.WriteElementString("title", Nodes.First.Value.NodeId);
                    }
                    else
                    {
                        Output.WriteElementString("title", Nodes.Count.ToString() + " nodes");
                    }
                }

                LinkedList <string> PagesInOrder = new LinkedList <string>();
                Dictionary <string, LinkedList <ControlParameter> > ParametersPerPage = new Dictionary <string, LinkedList <ControlParameter> >();

                foreach (ControlParameter P in Parameters)
                {
                    if (!ParametersPerPage.TryGetValue(P.Page, out LinkedList <ControlParameter> List))
                    {
                        PagesInOrder.AddLast(P.Page);
                        List = new LinkedList <ControlParameter>();
                        ParametersPerPage[P.Page] = List;
                    }

                    List.AddLast(P);
                }

                foreach (string Page in PagesInOrder)
                {
                    Output.WriteStartElement("xdl", "page", null);
                    Output.WriteAttributeString("label", Page);

                    foreach (ControlParameter P in ParametersPerPage[Page])
                    {
                        Output.WriteStartElement("xdl", "fieldref", null);
                        Output.WriteAttributeString("var", P.Name);
                        Output.WriteEndElement();
                    }

                    Output.WriteEndElement();
                }

                foreach (ControlParameter P in Parameters)
                {
                    P.ExportToForm(Output, FirstNode);
                }

                Output.WriteEndElement();
                Output.Flush();

                e.IqResult(Xml.ToString());
            }
            catch (Exception ex)
            {
                e.IqError(ex);
            }
        }