/// <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)); }
/// <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); } }