Exemple #1
0
        /// <summary>
        /// Selects a stream-method from the list of advertised methods.
        /// </summary>
        /// <param name="feature">The 'feature' element containing the data-form
        /// with advertised stream-methods.</param>
        /// <returns>The selected stream-method.</returns>
        /// <exception cref="ArgumentException">None of the advertised methods is
        /// supported.</exception>
        string SelectStreamMethod(XmlElement feature)
        {
            // See if we support any of the advertised stream-methods.
            DataForm  form  = FeatureNegotiation.Parse(feature);
            ListField field = form.Fields["stream-method"] as ListField;

            // Order of preference: Socks5, Ibb.
            string[] methods = new string[] {
                "http://jabber.org/protocol/bytestreams",
                "http://jabber.org/protocol/ibb"
            };
            for (int i = 0; i < methods.Length; i++)
            {
                if (ForceInBandBytestreams &&
                    methods[i] != "http://jabber.org/protocol/ibb")
                {
                    continue;
                }
                if (field.Values.Contains(methods[i]))
                {
                    return(methods[i]);
                }
            }
            throw new ArgumentException("No supported method advertised.");
        }
Exemple #2
0
        private XmlElement CreateFeatureElement(IEnumerable <string> streamOptions)
        {
            streamOptions.ThrowIfNull <IEnumerable <string> >("streamOptions");
            DataForm         form    = new RequestForm(null, null, new DataField[0]);
            HashSet <Option> options = new HashSet <Option>();

            foreach (string str in streamOptions)
            {
                options.Add(new Option(str, null));
            }
            form.Fields.Add(new ListField("stream-method", true, null, null, options, null));
            return(FeatureNegotiation.Create(form));
        }
        private string SelectStreamMethod(XmlElement feature)
        {
            ListField field = FeatureNegotiation.Parse(feature).Fields["stream-method"] as ListField;

            string[] strArray = new string[] { "http://jabber.org/protocol/bytestreams", "http://jabber.org/protocol/ibb" };
            for (int i = 0; i < strArray.Length; i++)
            {
                if ((!this.ForceInBandBytestreams || !(strArray[i] != "http://jabber.org/protocol/ibb")) && field.Values.Contains <string>(strArray[i]))
                {
                    return(strArray[i]);
                }
            }
            throw new ArgumentException("No supported method advertised.");
        }
        /// <summary>
        /// Creates the 'feature' XML element which is part of a 'Stream Initiation'
        /// request.
        /// </summary>
        /// <param name="streamOptions">An enumerable collection of accepted stream
        /// methods.</param>
        /// <returns>An XML 'feature' element.</returns>
        /// <exception cref="ArgumentNullException">The streamOptions parameter
        /// is null.</exception>
        XmlElement CreateFeatureElement(IEnumerable <string> streamOptions)
        {
            streamOptions.ThrowIfNull("streamOptions");
            // Create the data-form for stream-method selection.
            DataForm         form    = new RequestForm();
            HashSet <Option> options = new HashSet <Option>();

            foreach (string opt in streamOptions)
            {
                options.Add(new Option(opt));
            }
            form.Fields.Add(new ListField("stream-method", true, null, null,
                                          options));
            // Wrap it in a 'feature' element to create the offer.
            return(FeatureNegotiation.Create(form));
        }
Exemple #5
0
        private string ParseStreamMethod(XmlElement feature)
        {
            feature.ThrowIfNull <XmlElement>("feature");
            DataField field = FeatureNegotiation.Parse(feature).Fields["stream-method"];

            if (field == null)
            {
                throw new ArgumentException("Missing or erroneous 'stream-method' field.");
            }
            string str = field.Values.FirstOrDefault <string>();

            if (str == null)
            {
                throw new ArgumentException("No stream-method selected.");
            }
            return(str);
        }
        /// <summary>
        /// Parses the selected stream-method from the specified 'feature' XML
        /// element.
        /// </summary>
        /// <param name="feature">The 'feature' XML element.</param>
        /// <returns>The stream method contained in the 'feature' XML
        /// element.</returns>
        /// <exception cref="ArgumentNullException">The feature parameter is
        /// null.</exception>
        /// <exception cref="ArgumentException">The feature element contains
        /// invalid data.</exception>
        string ParseStreamMethod(XmlElement feature)
        {
            feature.ThrowIfNull("feature");
            DataForm form = FeatureNegotiation.Parse(feature);
            // The data-form must contain a field named 'stream-method'.
            var field = form.Fields["stream-method"];

            if (field == null)
            {
                throw new ArgumentException("Missing or erroneous 'stream-method' field.");
            }
            string selected = field.Values.FirstOrDefault();

            if (selected == null)
            {
                throw new ArgumentException("No stream-method selected.");
            }
            return(selected);
        }
Exemple #7
0
 /// <summary>
 /// Invoked whenever a 'Stream Initiation' request for file transfers
 /// is received.
 /// </summary>
 /// <param name="from">The JID of the XMPP entity that wishes to initiate
 /// the data-stream.</param>
 /// <param name="si">The 'si' element of the request.</param>
 /// <returns>The response to the SI request or an error element to include
 /// in the IQ response.</returns>
 XmlElement OnStreamInitiationRequest(Jid from, XmlElement si)
 {
     try {
         string method = SelectStreamMethod(si["feature"]);
         // If the session-id is already in use, we cannot process the request.
         string sid = si.GetAttribute("id");
         if (String.IsNullOrEmpty(sid) || siSessions.ContainsKey(sid))
         {
             return(new XmppError(ErrorType.Cancel, ErrorCondition.Conflict).Data);
         }
         // Extract file information and hand to user.
         var    file           = si["file"];
         string desc           = file["desc"] != null ? file["desc"].InnerText : null,
                name           = file.GetAttribute("name");
         int          size     = Int32.Parse(file.GetAttribute("size"));
         FileTransfer transfer = new FileTransfer(from, im.Jid, name, size,
                                                  sid, desc);
         string savePath = TransferRequest.Invoke(transfer);
         // User has rejected the request.
         if (savePath == null)
         {
             return(new XmppError(ErrorType.Cancel, ErrorCondition.NotAcceptable).Data);
         }
         // Create an SI session instance.
         SISession session = new SISession(sid, File.OpenWrite(savePath),
                                           size, true, from, im.Jid, im.GetExtension(method) as IDataStream);
         siSessions.TryAdd(sid, session);
         // Store the file's meta data.
         metaData.TryAdd(sid, new FileMetaData(name, desc));
         // Construct and return the negotiation result.
         return(Xml.Element("si", "http://jabber.org/protocol/si").Child(
                    FeatureNegotiation.Create(new SubmitForm(
                                                  new ListField("stream-method", method)))));
     } catch {
         return(new XmppError(ErrorType.Cancel, ErrorCondition.BadRequest).Data);
     }
 }
        private XmlElement OnStreamInitiationRequest(Jid from, XmlElement si)
        {
            FileStream stream = null;

            try
            {
                string str       = this.SelectStreamMethod(si["feature"]);
                string attribute = si.GetAttribute("id");
                if (string.IsNullOrEmpty(attribute) || this.siSessions.ContainsKey(attribute))
                {
                    return(new XmppError(ErrorType.Cancel, ErrorCondition.Conflict, new XmlElement[0]).Data);
                }
                XmlElement   element     = si["file"];
                string       description = (element["desc"] != null) ? element["desc"].InnerText : null;
                string       name        = element.GetAttribute("name");
                int          num         = int.Parse(element.GetAttribute("size"));
                FileTransfer transfer    = new FileTransfer(from, base.im.Jid, name, (long)num, attribute, description, 0L);
                string       path        = this.TransferRequest(transfer);
                if (path == null)
                {
                    return(new XmppError(ErrorType.Cancel, ErrorCondition.NotAcceptable, new XmlElement[0]).Data);
                }
                stream = File.OpenWrite(path);
                SISession session = new SISession(attribute, stream, (long)num, true, from, base.im.Jid, base.im.GetExtension(str) as IDataStream);
                this.siSessions.TryAdd(attribute, session);
                this.metaData.TryAdd(attribute, new FileMetaData(name, description));
                return(Xml.Element("si", "http://jabber.org/protocol/si").Child(FeatureNegotiation.Create(new SubmitForm(new DataField[] { new ListField("stream-method", str) }))));
            }
            catch
            {
                if (stream != null)
                {
                    stream.Close();
                }
                return(new XmppError(ErrorType.Cancel, ErrorCondition.BadRequest, new XmlElement[0]).Data);
            }
        }
Exemple #9
0
		/// <summary>
		/// Initializes the various XMPP extension modules.
		/// </summary>
		void LoadExtensions() {
			version = im.LoadExtension<SoftwareVersion>();
			sdisco = im.LoadExtension<ServiceDiscovery>();
			ecapa = im.LoadExtension<EntityCapabilities>();
			ping = im.LoadExtension<Ping>();
			attention = im.LoadExtension<Attention>();
			time = im.LoadExtension<EntityTime>();
			block = im.LoadExtension<BlockingCommand>();
			pep = im.LoadExtension<Pep>();
			userTune = im.LoadExtension<UserTune>();
			userAvatar = im.LoadExtension<UserAvatar>();
			userMood = im.LoadExtension<UserMood>();
			dataForms = im.LoadExtension<DataForms>();
			featureNegotiation = im.LoadExtension<FeatureNegotiation>();
			streamInitiation = im.LoadExtension<StreamInitiation>();
			siFileTransfer = im.LoadExtension<SIFileTransfer>();
			inBandBytestreams = im.LoadExtension<InBandBytestreams>();
			userActivity = im.LoadExtension<UserActivity>();
			socks5Bytestreams = im.LoadExtension<Socks5Bytestreams>();
			FileTransferSettings = new FileTransferSettings(socks5Bytestreams,
				siFileTransfer);
			serverIpCheck = im.LoadExtension<ServerIpCheck>();
			inBandRegistration = im.LoadExtension<InBandRegistration>();
			chatStateNotifications = im.LoadExtension<ChatStateNotifications>();
			bitsOfBinary = im.LoadExtension<BitsOfBinary>();
		}