示例#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.");
        }
示例#2
0
        /// <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));
        }
示例#3
0
        /// <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);
        }
示例#4
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);
     }
 }