public static StreamInitIQ BuildDefaultStreamInitResult(StreamOptions choosenoption)
 {
     StreamInitIQ q = new StreamInitIQ(StreamInitIQType.Result);
     q.sid = null;
     q.mimetype = null;
     q.StreamOptions = choosenoption;
     return q;
 }
        public override bool NewIQ(IQ iq)
        {
            if (iq is StreamInitIQ)
            {
                StreamInitIQ siiq = iq as StreamInitIQ;
                if (iq.Type == IQType.result.ToString())
                {
                    /// May be a response to our pending request to send
                    ///
                    StreamInitIQ initalrequest = null;
                    if (FileSendRequests.ContainsKey(iq.ID) == true)
                    {
                        initalrequest = FileSendRequests[iq.ID];
                        FileSendRequests.Remove(iq.ID);
                    }

                    if (initalrequest != null)
                    {
                        //if (siiq.StreamOptions != StreamOptions.ibb)
                        //{
                        //    /// Tell the host we failed to send the file because we only support ibb
                        //    return true;
                        //}

                        /// Looks like they agree, start an ibb file transfer logic to perform the transfer
                        ///

                        if ((siiq.StreamOptions & StreamOptions.bytestreams) == StreamOptions.bytestreams)
                            initalrequest.FileTransferObject.FileTransferType = FileTransferType.ByteStreams;
                        else if ((siiq.StreamOptions & StreamOptions.ibb) == StreamOptions.ibb)
                            initalrequest.FileTransferObject.FileTransferType = FileTransferType.IBB;

                        XMPPClient.FileTransferManager.StartNewReceiveStream(initalrequest.FileTransferObject);
                    }

                }
                else if (iq.Type == IQType.error.ToString())
                {
                    /// May be a response to our pending request to send
                    ///
                    StreamInitIQ initalrequest = null;
                    if (FileSendRequests.ContainsKey(iq.ID) == true)
                    {
                        initalrequest = FileSendRequests[iq.ID];
                        FileSendRequests.Remove(iq.ID);
                    }

                    if (initalrequest != null)
                    {

                        XMPPClient.FileTransferManager.GotStreamErrorResponse(initalrequest.FileTransferObject, iq);
                    }

                }
                //else if (siiq.StreamInitIQType == StreamInitIQType.Offer)
                else if (iq.Type == IQType.set.ToString())
                {
                    /// They want to send a file to us?
                    /// Ask the user if it's OK, and if it is, start an ibb to receive it and send ok
                    ///

                    if ((siiq.sid == null) || (siiq.sid.Length <= 0) )
                    {
                        IQ iqresponse = new StreamInitIQ(StreamInitIQType.Result);
                        iqresponse.ID = siiq.ID;
                        iqresponse.Type = IQType.error.ToString();
                        iqresponse.To = iq.From;
                        iqresponse.From = XMPPClient.JID;
                        iqresponse.Error = new Error(ErrorType.invalidid);
                        XMPPClient.SendXMPP(iqresponse);
                        return true;
                    }
                    if ((siiq.filename == null) || (siiq.filename.Length <= 0))
                    {
                        IQ iqresponse = new StreamInitIQ(StreamInitIQType.Result);
                        iqresponse.ID = siiq.ID;
                        iqresponse.Type = IQType.error.ToString();
                        iqresponse.To = iq.From;
                        iqresponse.From = XMPPClient.JID;
                        iqresponse.Error = new Error(ErrorType.notacceptable);
                        XMPPClient.SendXMPP(iqresponse);
                        return true;
                    }

                    /// Can only do bytes streams or inband byte streams
                    if ( ((siiq.StreamOptions & StreamOptions.ibb) != StreamOptions.ibb) &&
                         ((siiq.StreamOptions & StreamOptions.bytestreams) != StreamOptions.bytestreams))
                    {
                        IQ iqresponse = new StreamInitIQ(StreamInitIQType.Result);
                        iqresponse.ID = siiq.ID;
                        iqresponse.Type = IQType.error.ToString();
                        iqresponse.To = iq.From;
                        iqresponse.From = XMPPClient.JID;
                        iqresponse.Error = new Error(ErrorType.notacceptable);
                        XMPPClient.SendXMPP(iqresponse);
                        return true;
                    }

                    FileTransfer newreq = new FileTransfer(siiq.filename, siiq.filesize, siiq.From);
                    if ((siiq.StreamOptions & StreamOptions.bytestreams) == StreamOptions.bytestreams)
                        newreq.FileTransferType = FileTransferType.ByteStreams;
                    else if ((siiq.StreamOptions & StreamOptions.ibb) == StreamOptions.ibb)
                        newreq.FileTransferType = FileTransferType.IBB;

                    newreq.sid = siiq.sid;

                    FileDownloadRequests.Add(siiq.sid, siiq);

                    XMPPClient.FileTransferManager.NewIncomingFileRequest(newreq);
                }

                return true;
            }
            return false;
        }
        public static StreamInitIQ BuildDefaultStreamInitOffer(XMPPClient client, FileTransfer trans)
        {
            StreamInitIQ q = new StreamInitIQ(StreamInitIQType.Offer);
            q.mimetype = trans.MimeType;
            q.filename = trans.FileName;
            q.sid = trans.sid;
            q.filesize = trans.BytesTotal;
            q.FileTransferObject = trans;

            q.StreamOptions = XMPP.StreamOptions.ibb;

            /// Don't offer byte streams if the server doesn't support a SOCKS 5 proxy, since windows phone can't listen for connections
            item filetransfer = client.ServerServiceDiscoveryFeatureList.GetItemByType(ItemType.SOCKS5ByteStream);

            if ((FileTransferManager.SOCKS5Proxy != null) && (FileTransferManager.SOCKS5Proxy.Length > 0))
                filetransfer = new item(); /// User has supplied a socks5 proxy, so we don't need our xmpp server to supply one to use bytestreams

            if ((filetransfer != null) && (FileTransferManager.UseIBBOnly == false))
            {
                q.StreamOptions |= StreamOptions.bytestreams;
            }

            return q;
        }