Exemple #1
0
 private async void QueryVCardHandler(object Sender, IqEventArgs e)
 {
     try
     {
         e.IqResult(await this.GetVCardXml());
     }
     catch (Exception ex)
     {
         e.IqError(ex);
     }
 }
Exemple #2
0
        private async void ClearCacheHandler(object Sender, IqEventArgs e)
        {
            try
            {
                if (e.From == this.provisioningServerAddress)
                {
                    await this.ClearInternalCache();

                    e.IqResult(string.Empty);
                }
                else
                {
                    e.IqError(new ForbiddenException("Unauthorized sender.", e.IQ));
                }
            }
            catch (Exception ex)
            {
                e.IqError(ex);
            }
        }
Exemple #3
0
        private void ForwardedTokenChallengeResponse(object Sender, IqResultEventArgs e2)
        {
            IqEventArgs e = (IqEventArgs)e2.State;

            if (e2.Ok)
            {
                e.IqResult(e2.FirstElement.OuterXml);
            }
            else
            {
                e.IqError(e2.ErrorElement.OuterXml);
            }
        }
Exemple #4
0
        private void UnsubscribeHandler(object Sender, IqEventArgs e)
        {
            string Id = XML.Attribute(e.Query, "id");
            bool   Found;

            lock (this.subscriptionsByThing)
            {
                Found = this.RemoveSubscriptionLocked(e.From, Id, true);
            }

            if (Found)
            {
                e.IqResult(string.Empty);
            }
            else
            {
                e.IqError("<error type='modify'><item-not-found xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/></error>");
            }
        }
Exemple #5
0
        private void ReqReceived(object Sender, IqEventArgs e)
        {
            if (this.requiresE2e && !e.UsesE2eEncryption)
            {
                e.IqError(new Networking.XMPP.StanzaErrors.ForbiddenException("End-to-end encryption required.", e.IQ));
                return;
            }

            string Method       = XML.Attribute(e.Query, "method");
            string Resource     = XML.Attribute(e.Query, "resource");
            string Version      = XML.Attribute(e.Query, "version");
            int    MaxChunkSize = XML.Attribute(e.Query, "maxChunkSize", 0);
            bool   Sipub        = XML.Attribute(e.Query, "sipub", true);
            bool   Ibb          = XML.Attribute(e.Query, "ibb", true);
            bool   Socks5       = XML.Attribute(e.Query, "s5", true);
            bool   Jingle       = XML.Attribute(e.Query, "jingle", true);
            Stream DataStream   = null;
            List <KeyValuePair <string, string> > HeaderFields = new List <KeyValuePair <string, string> >();
            HttpRequestHeader Header = null;

            if (MaxChunkSize <= 0 || MaxChunkSize > this.maxChunkSize)
            {
                MaxChunkSize = this.maxChunkSize;
            }

            foreach (XmlNode N in e.Query.ChildNodes)
            {
                switch (N.LocalName)
                {
                case "headers":
                    foreach (XmlNode N2 in N.ChildNodes)
                    {
                        if (N2.LocalName == "header")
                        {
                            string Key   = XML.Attribute((XmlElement)N2, "name");
                            string Value = N2.InnerText;

                            HeaderFields.Add(new KeyValuePair <string, string>(Key, Value));
                        }
                    }
                    break;

                case "data":
                    Header = new HttpRequestHeader(Method, Resource, Version, "httpx", HeaderFields.ToArray());

                    foreach (XmlNode N2 in N.ChildNodes)
                    {
                        switch (N2.LocalName)
                        {
                        case "text":
                            MemoryStream ms = new MemoryStream();

                            if (Header.ContentType is null)
                            {
                                Header.Add(new HttpField("Content-Type", "text/plain"));
                            }

                            byte[] Data = Header.ContentType.Encoding.GetBytes(N2.InnerText);
                            ms.Write(Data, 0, Data.Length);
                            DataStream = ms;
                            break;

                        case "xml":
                            ms = new MemoryStream();

                            if (Header.ContentType is null)
                            {
                                Header.Add(new HttpField("Content-Type", "text/xml"));
                            }

                            Data = Header.ContentType.Encoding.GetBytes(N2.InnerText);
                            ms.Write(Data, 0, Data.Length);
                            ms.Position = 0;
                            DataStream  = ms;
                            break;

                        case "base64":
                            ms   = new MemoryStream();
                            Data = Convert.FromBase64String(N2.InnerText);
                            ms.Write(Data, 0, Data.Length);
                            ms.Position = 0;
                            DataStream  = ms;
                            break;

                        case "chunkedBase64":
                            TemporaryFile file     = new TemporaryFile();
                            string        StreamId = XML.Attribute((XmlElement)N2, "streamId");
                            HttpxChunks.chunkedStreams.Add(e.From + " " + StreamId, new ServerChunkRecord(this, e.Id, e.From, e.To,
                                                                                                          new HttpRequest(Header, DataStream, null, e.From), e.E2eEncryption, e.E2eReference, file,
                                                                                                          MaxChunkSize, Sipub, Ibb, Socks5, Jingle));
                            return;

                        case "sipub":
                            // TODO: Implement File Transfer support.
                            break;

                        case "ibb":
                            // TODO: Implement In-band byte streams support.
                            break;

                        case "s5":
                            // TODO: Implement SOCKS5 streams support.
                            break;

                        case "jingle":
                            // TODO: Implement Jingle support.
                            break;
                        }
                    }
                    break;
                }
            }

            if (Header is null)
            {
                Header = new HttpRequestHeader(Method, Resource, Version, "httpx", HeaderFields.ToArray());
            }

            this.Process(e.Id, e.From, e.To, new HttpRequest(Header, DataStream, null, e.From), e.E2eEncryption, e.E2eReference,
                         MaxChunkSize, Sipub, Ibb, Socks5, Jingle);
        }
Exemple #6
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);
            }
        }
Exemple #7
0
        private async void SetHandler(object Sender, IqEventArgs e)
        {
            try
            {
                string ServiceToken = XML.Attribute(e.Query, "st");
                string DeviceToken  = XML.Attribute(e.Query, "dt");
                string UserToken    = XML.Attribute(e.Query, "ut");

                LinkedList <IThingReference>    Nodes          = null;
                SortedDictionary <string, bool> ParameterNames = this.provisioningClient is null ? null : new SortedDictionary <string, bool>();
                LinkedList <ControlOperation>   Operations     = new LinkedList <ControlOperation>();
                ControlParameter Parameter;
                DataForm         Form = null;
                XmlElement       E;
                string           Name;

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

                    switch (E.LocalName)
                    {
                    case "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);
                        }
                        break;

                    case "b":
                    case "cl":
                    case "d":
                    case "dt":
                    case "db":
                    case "dr":
                    case "e":
                    case "i":
                    case "l":
                    case "s":
                    case "t":
                        if (ParameterNames != null)
                        {
                            ParameterNames[XML.Attribute(E, "n")] = true;
                        }
                        break;

                    case "x":
                        Form = new DataForm(this.client, E, null, null, e.From, e.To);
                        if (Form.Type != FormType.Submit)
                        {
                            ParameterBadRequest(e);
                            return;
                        }

                        if (ParameterNames != null)
                        {
                            foreach (Field Field in Form.Fields)
                            {
                                ParameterNames[Field.Var] = true;
                            }
                        }
                        break;

                    default:
                        ParameterBadRequest(e);
                        return;
                    }
                }

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

                    switch (E.LocalName)
                    {
                    case "b":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            BooleanControlParameter BooleanControlParameter = Parameter as BooleanControlParameter;
                            if (BooleanControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new BooleanControlOperation(Node, BooleanControlParameter, XML.Attribute(E, "v", false), e));
                        }
                        break;

                    case "cl":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            ColorControlParameter ColorControlParameter = Parameter as ColorControlParameter;
                            if (ColorControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new ColorControlOperation(Node, ColorControlParameter, XML.Attribute(E, "v"), e));
                        }
                        break;

                    case "d":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            DateControlParameter DateControlParameter = Parameter as DateControlParameter;
                            if (DateControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new DateControlOperation(Node, DateControlParameter, XML.Attribute(E, "v", DateTime.MinValue), e));
                        }
                        break;

                    case "dt":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            DateTimeControlParameter DateTimeControlParameter = Parameter as DateTimeControlParameter;
                            if (DateTimeControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new DateTimeControlOperation(Node, DateTimeControlParameter, XML.Attribute(E, "v", DateTime.MinValue), e));
                        }
                        break;

                    case "db":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            DoubleControlParameter DoubleControlParameter = Parameter as DoubleControlParameter;
                            if (DoubleControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new DoubleControlOperation(Node, DoubleControlParameter, XML.Attribute(E, "v", 0.0), e));
                        }
                        break;

                    case "dr":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            DurationControlParameter DurationControlParameter = Parameter as DurationControlParameter;
                            if (DurationControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new DurationControlOperation(Node, DurationControlParameter, XML.Attribute(E, "v", Duration.Zero), e));
                        }
                        break;

                    case "e":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            string StringValue = XML.Attribute(E, "v");

                            if (Parameter is EnumControlParameter EnumControlParameter)
                            {
                                Type T = Types.GetType(XML.Attribute(E, "t"));
                                if (T is null)
                                {
                                    e.IqError("<error type='modify'><bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/><paramError xmlns=\"" +
                                              ControlClient.NamespaceControl + "\" n=\"" + Name + "\">Type not found.</paramError></error>");
                                    return;
                                }

                                if (!T.GetTypeInfo().IsEnum)
                                {
                                    e.IqError("<error type='modify'><bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/><paramError xmlns=\"" +
                                              ControlClient.NamespaceControl + "\" n=\"" + Name + "\">Type is not an enumeration.</paramError></error>");
                                    return;
                                }

                                Enum Value;

                                try
                                {
                                    Value = (Enum)Enum.Parse(T, StringValue);
                                }
                                catch (Exception)
                                {
                                    e.IqError("<error type='modify'><bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/><paramError xmlns=\"" +
                                              ControlClient.NamespaceControl + "\" n=\"" + Name + "\">Value not valid element of enumeration.</paramError></error>");
                                    return;
                                }

                                Operations.AddLast(new EnumControlOperation(Node, EnumControlParameter, Value, e));
                            }
                            else if (Parameter is StringControlParameter StringControlParameter)
                            {
                                Operations.AddLast(new StringControlOperation(Node, StringControlParameter, StringValue, e));
                            }
                            else if (Parameter is MultiLineTextControlParameter MultiLineTextControlParameter)
                            {
                                Operations.AddLast(new MultiLineTextControlOperation(Node, MultiLineTextControlParameter, StringValue, e));
                            }
                            else
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }
                        }
                        break;

                    case "i":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            Int32ControlParameter Int32ControlParameter = Parameter as Int32ControlParameter;
                            if (Int32ControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new Int32ControlOperation(Node, Int32ControlParameter, XML.Attribute(E, "v", 0), e));
                        }
                        break;

                    case "l":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            Int64ControlParameter Int64ControlParameter = Parameter as Int64ControlParameter;
                            if (Int64ControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new Int64ControlOperation(Node, Int64ControlParameter, XML.Attribute(E, "v", 0L), e));
                        }
                        break;

                    case "s":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            if (Parameter is StringControlParameter StringControlParameter)
                            {
                                Operations.AddLast(new StringControlOperation(Node, StringControlParameter, XML.Attribute(E, "v"), e));
                            }
                            else if (Parameter is MultiLineTextControlParameter MultiLineTextControlParameter)
                            {
                                Operations.AddLast(new MultiLineTextControlOperation(Node, MultiLineTextControlParameter, XML.Attribute(E, "v"), e));
                            }
                            else
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }
                        }
                        break;

                    case "t":
                        Name = XML.Attribute(E, "n");
                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameter = await this.GetParameter(Node, Name, e);

                            if (Parameter is null)
                            {
                                return;
                            }

                            TimeControlParameter TimeControlParameter = Parameter as TimeControlParameter;
                            if (TimeControlParameter is null)
                            {
                                ParameterWrongType(Name, e);
                                return;
                            }

                            Operations.AddLast(new TimeControlOperation(Node, TimeControlParameter, XML.Attribute(E, "v", TimeSpan.Zero), e));
                        }
                        break;

                    case "x":
                        Dictionary <string, ControlParameter> Parameters;

                        foreach (IThingReference Node in Nodes ?? NoNodes)
                        {
                            Parameters = await this.GetControlParametersByName(Node);

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

                            foreach (Field Field in Form.Fields)
                            {
                                if (!Parameters.TryGetValue(Field.Var, out Parameter))
                                {
                                    ParameterNotFound(Field.Var, e);
                                    return;
                                }

                                Operations.AddLast(new FormControlOperation(Node, Parameter, Field.ValueString, e));
                            }
                        }
                        break;
                    }
                }

                if (this.provisioningClient != null)
                {
                    string[] ParameterNames2 = new string[ParameterNames.Count];
                    ParameterNames.Keys.CopyTo(ParameterNames2, 0);

                    this.provisioningClient.CanControl(e.FromBareJid, Nodes, ParameterNames2,
                                                       ServiceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       DeviceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       UserToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                       (sender2, e2) =>
                    {
                        if (e2.Ok && e2.CanControl)
                        {
                            LinkedList <ControlOperation> Operations2 = null;
                            bool Restricted;

                            if (e2.Nodes != null || e2.ParameterNames != null)
                            {
                                Dictionary <IThingReference, bool> AllowedNodes = null;
                                Dictionary <string, bool> AllowedParameterNames = null;

                                Operations2 = new LinkedList <ControlOperation>();
                                Restricted  = false;

                                if (e2.Nodes != null)
                                {
                                    AllowedNodes = new Dictionary <IThingReference, bool>();
                                    foreach (IThingReference Node in e2.Nodes)
                                    {
                                        AllowedNodes[Node] = true;
                                    }
                                }

                                if (e2.ParameterNames != null)
                                {
                                    AllowedParameterNames = new Dictionary <string, bool>();
                                    foreach (string ParameterName in e2.ParameterNames)
                                    {
                                        AllowedParameterNames[ParameterName] = true;
                                    }
                                }

                                foreach (ControlOperation Operation in Operations)
                                {
                                    if (AllowedNodes != null && !AllowedNodes.ContainsKey(Operation.Node ?? ThingReference.Empty))
                                    {
                                        Restricted = true;
                                        continue;
                                    }

                                    if (AllowedParameterNames != null && !AllowedParameterNames.ContainsKey(Operation.ParameterName))
                                    {
                                        Restricted = true;
                                        continue;
                                    }

                                    Operations2.AddLast(Operation);
                                }
                            }
                            else
                            {
                                Restricted = false;
                            }

                            if (Restricted)
                            {
                                this.PerformOperations(Operations2, e, e2.Nodes, e2.ParameterNames);
                            }
                            else
                            {
                                this.PerformOperations(Operations, e, null, null);
                            }
                        }
                        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.PerformOperations(Operations, e, null, null);
                }
            }
            catch (Exception ex)
            {
                e.IqError(ex);
            }
        }
Exemple #8
0
 internal static void ParameterBadRequest(IqEventArgs e)
 {
     e.IqError("<error type='modify'><bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/></error>");
 }
Exemple #9
0
 internal static void ParameterValueInvalid(string Name, IqEventArgs e)
 {
     e.IqError("<error type='modify'><bad-request xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/><paramError xmlns=\"" +
               ControlClient.NamespaceControl + "\" n=\"" + Name + "\">Value not valid.</paramError></error>");
 }
Exemple #10
0
 internal static void NotFound(IqEventArgs e)
 {
     e.IqError("<error type='modify'><item-not-found xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/></error>");
 }
Exemple #11
0
 internal static void ParameterNotFound(string Name, IqEventArgs e)
 {
     e.IqError("<error type='modify'><item-not-found xmlns=\"urn:ietf:params:xml:ns:xmpp-stanzas\"/><paramError xmlns=\"" +
               ControlClient.NamespaceControl + "\" n=\"" + Name + "\">Parameter not found.</paramError></error>");
 }
Exemple #12
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);
            }
        }
Exemple #13
0
        private void SubscribeHandler(object Sender, IqEventArgs e)
        {
            List <ThingReference> Nodes = null;
            Dictionary <string, FieldSubscriptionRule> Fields = null;
            XmlElement E            = e.Query;
            FieldType  FieldTypes   = (FieldType)0;
            Duration   MaxAge       = null;
            Duration   MinInterval  = null;
            Duration   MaxInterval  = null;
            string     ServiceToken = string.Empty;
            string     DeviceToken  = string.Empty;
            string     UserToken    = string.Empty;
            string     NodeId;
            string     SourceId;
            string     Partition;
            string     Id  = string.Empty;
            bool       Req = false;
            bool       b;

            foreach (XmlAttribute Attr in E.Attributes)
            {
                switch (Attr.Name)
                {
                case "id":
                    Id = Attr.Value;
                    break;

                case "maxAge":
                    if (!Duration.TryParse(Attr.Value, out MaxAge))
                    {
                        MaxAge = null;
                    }
                    break;

                case "minInt":
                    if (!Duration.TryParse(Attr.Value, out MinInterval))
                    {
                        MinInterval = null;
                    }
                    break;

                case "maxInt":
                    if (!Duration.TryParse(Attr.Value, out MaxInterval))
                    {
                        MaxInterval = null;
                    }
                    break;

                case "st":
                    ServiceToken = Attr.Value;
                    break;

                case "dt":
                    DeviceToken = Attr.Value;
                    break;

                case "ut":
                    UserToken = Attr.Value;
                    break;

                case "all":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.All;
                    }
                    break;

                case "h":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Historical;
                    }
                    break;

                case "m":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Momentary;
                    }
                    break;

                case "p":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Peak;
                    }
                    break;

                case "s":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Status;
                    }
                    break;

                case "c":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Computed;
                    }
                    break;

                case "i":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Identity;
                    }
                    break;

                case "req":
                    if (!CommonTypes.TryParse(Attr.Value, out Req))
                    {
                        Req = false;
                    }
                    break;
                }
            }

            foreach (XmlNode N in E.ChildNodes)
            {
                switch (N.LocalName)
                {
                case "nd":
                    if (Nodes == null)
                    {
                        Nodes = new List <ThingReference>();
                    }

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

                    ThingReference Ref = new ThingReference(NodeId, SourceId, Partition);
                    Nodes.Add(Ref);
                    break;

                case "f":
                    if (Fields == null)
                    {
                        Fields = new Dictionary <string, FieldSubscriptionRule>();
                    }

                    string FieldName    = null;
                    double?CurrentValue = null;
                    double?ChangedBy    = null;
                    double?ChangedUp    = null;
                    double?ChangedDown  = null;
                    double d;

                    foreach (XmlAttribute Attr in N.Attributes)
                    {
                        switch (Attr.Name)
                        {
                        case "n":
                            FieldName = Attr.Value;
                            break;

                        case "v":
                            if (CommonTypes.TryParse(Attr.Value, out d))
                            {
                                CurrentValue = d;
                            }
                            break;

                        case "by":
                            if (CommonTypes.TryParse(Attr.Value, out d))
                            {
                                ChangedBy = d;
                            }
                            break;

                        case "up":
                            if (CommonTypes.TryParse(Attr.Value, out d))
                            {
                                ChangedUp = d;
                            }
                            break;

                        case "dn":
                            if (CommonTypes.TryParse(Attr.Value, out d))
                            {
                                ChangedDown = d;
                            }
                            break;
                        }
                    }

                    if (!string.IsNullOrEmpty(FieldName))
                    {
                        Fields[FieldName] = new FieldSubscriptionRule(FieldName, CurrentValue, ChangedBy, ChangedUp, ChangedDown);
                    }

                    break;
                }
            }

            if (this.provisioningClient != null)
            {
                this.provisioningClient.CanRead(e.FromBareJid, FieldTypes, Nodes, Fields.Keys,
                                                ServiceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                DeviceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                UserToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                (sender2, e2) =>
                {
                    if (e2.Ok && e2.CanRead)
                    {
                        if (e2.FieldsNames != null)
                        {
                            Dictionary <string, bool> FieldNames = new Dictionary <string, bool>();

                            foreach (string FieldName in FieldNames.Keys)
                            {
                                FieldNames[FieldName] = true;
                            }

                            LinkedList <string> ToRemove = null;

                            foreach (string FieldName in Fields.Keys)
                            {
                                if (!FieldNames.ContainsKey(FieldName))
                                {
                                    if (ToRemove == null)
                                    {
                                        ToRemove = new LinkedList <string>();
                                    }

                                    ToRemove.AddLast(FieldName);
                                }
                            }

                            if (ToRemove != null)
                            {
                                foreach (string FieldName in ToRemove)
                                {
                                    Fields.Remove(FieldName);
                                }
                            }
                        }

                        this.PerformSubscription(Req, e, Id, Fields, e2.Nodes, e2.FieldTypes,
                                                 ServiceToken, DeviceToken, UserToken, MaxAge, MinInterval, MaxInterval);
                    }
                    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.PerformSubscription(Req, e, Id, Fields, Nodes?.ToArray(), FieldTypes,
                                         ServiceToken, DeviceToken, UserToken, MaxAge, MinInterval, MaxInterval);
            }
        }
Exemple #14
0
        private void ReqHandler(object Sender, IqEventArgs e)
        {
            List <ThingReference> Nodes      = null;
            List <string>         Fields     = null;
            XmlElement            E          = e.Query;
            FieldType             FieldTypes = (FieldType)0;
            DateTime From         = DateTime.MinValue;
            DateTime To           = DateTime.MaxValue;
            DateTime When         = DateTime.MinValue;
            string   ServiceToken = string.Empty;
            string   DeviceToken  = string.Empty;
            string   UserToken    = string.Empty;
            string   NodeId;
            string   SourceId;
            string   Partition;
            string   Id = string.Empty;
            bool     b;

            foreach (XmlAttribute Attr in E.Attributes)
            {
                switch (Attr.Name)
                {
                case "id":
                    Id = Attr.Value;
                    break;

                case "from":
                    if (!XML.TryParse(Attr.Value, out From))
                    {
                        From = DateTime.MinValue;
                    }
                    break;

                case "to":
                    if (!XML.TryParse(Attr.Value, out To))
                    {
                        To = DateTime.MaxValue;
                    }
                    break;

                case "when":
                    if (!XML.TryParse(Attr.Value, out When))
                    {
                        When = DateTime.MinValue;
                    }
                    break;

                case "st":
                    ServiceToken = Attr.Value;
                    break;

                case "dt":
                    DeviceToken = Attr.Value;
                    break;

                case "ut":
                    UserToken = Attr.Value;
                    break;

                case "all":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.All;
                    }
                    break;

                case "h":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Historical;
                    }
                    break;

                case "m":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Momentary;
                    }
                    break;

                case "p":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Peak;
                    }
                    break;

                case "s":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Status;
                    }
                    break;

                case "c":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Computed;
                    }
                    break;

                case "i":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Identity;
                    }
                    break;
                }
            }

            foreach (XmlNode N in E.ChildNodes)
            {
                switch (N.LocalName)
                {
                case "nd":
                    if (Nodes == null)
                    {
                        Nodes = new List <ThingReference>();
                    }

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

                    Nodes.Add(new ThingReference(NodeId, SourceId, Partition));
                    break;

                case "f":
                    if (Fields == null)
                    {
                        Fields = new List <string>();
                    }

                    Fields.Add(XML.Attribute((XmlElement)N, "n"));
                    break;
                }
            }

            SensorDataServerRequest Request = new SensorDataServerRequest(Id, this, e.From, e.From, Nodes?.ToArray(), FieldTypes,
                                                                          Fields?.ToArray(), From, To, When, ServiceToken, DeviceToken, UserToken);

            if (this.provisioningClient != null)
            {
                this.provisioningClient.CanRead(e.FromBareJid, Request.Types, Request.Nodes, Request.FieldNames,
                                                Request.ServiceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                Request.DeviceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                Request.UserToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                (sender2, e2) =>
                {
                    if (e2.Ok && e2.CanRead)
                    {
                        Request.Nodes      = e2.Nodes;
                        Request.FieldNames = e2.FieldsNames;
                        Request.Types      = e2.FieldTypes;

                        this.AcceptRequest(Request, e, Id);
                    }
                    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.AcceptRequest(Request, e, Id);
            }
        }