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