pgstath / Sharp.Xmpp

Sharp.Xmpp is looking for a maintainer! Unfortunatelly I do not have currently the time needed to maintain the library. Luckily a small but vibrant community has evolved around Sharp.Xmpp. If you would like to be the project's maintainer please sent an email to pgstath@gmail.com. This should include issues, tickets and commits that you have done for Sharp.Xmpp or other similar project. Sharp.Xmpp is a multiplatform, Windows and Android, .NET XMPP client assembly.Sharp.Xmpp supports IM functionality & a variety of XMPP extensions, is simple and is extensively tested against Android Xamarin. It is a fork of the currently frozen excellent S22.Xmpp project. Sharp.Xmpp will be at the FOSSDEM 2016 Real time DevRoom!
Other
84 stars 51 forks source link

XML Stream Issue #8 bump #13

Open cerickson1492 opened 8 years ago

cerickson1492 commented 8 years ago

Hello, I am having the same issue:

' ', hexadecimal value 0x15, is an invalid character. Line 1, position 1.

I found 1 answer on this topic:

Are you trying to connect to an old-style SSL port (usually 5223) with a client that expects to do StartTLS (usually on 5222)? (0x15 is the TLS content type for "alert", which is likely the response when parsing something that is not TLS.)

I am attempting to connect to Google GCM/CCS. I am very new (yesterday) to xmpp but in playing with the agXMPP client I am betting it has something to do with sasl. I added the s22.sasl lib but I am not exactly sure how I would incorporate it or if it will even work with your fork.

Any guidance would be appreciated.

My code is just a slightly modified getting started example using tls and a port to set the client: ` string hostname = "gcm-preprod.googleapis.com"; int port = 5236; string username = "[projectid]@gcm.googleapis.com"; string password = "[api key]"; bool tls = true;

        using (XmppClient client = new XmppClient(hostname, username, password,port,tls))
        {
            // Setup any event handlers.
            // ...

            client.Connect();
            Console.WriteLine("Connected as " + client.Jid + Environment.NewLine);
            Console.WriteLine(" Type 'send <JID> <Message>' to send a chat message, or 'quit' to exit.");
            Console.WriteLine(" Example: send user@domain.com Hello!");
            Console.WriteLine();

            while (true)
            {
                Console.Write("> ");
                string s = Console.ReadLine();
                if (s.StartsWith("send "))
                {
                    Match m = Regex.Match(s, @"^send\s(?<jid>[^\s]+)\s(?<message>.+)$");
                    if (!m.Success)
                        continue;
                    string recipient = m.Groups["jid"].Value, message = m.Groups["message"].Value;
                    // Send the chat-message.
                    client.SendMessage(recipient, message);
                }
                if (s == "quit")
                    return;
            }
        } `
pgstath commented 8 years ago

Hi, I assume you are referring to this one, http://stackoverflow.com/questions/32578424/xml-serialisation-error-when-connecting-to-xmpp-server/32578743. I need to create a Sharp.Xmpp stackoverflow area.

  1. Please test it with the latest version, and not the nuget package.
  2. Enable the debug option, and look in the console for any XMPP messages, client.DebugStanzas=true; before initiating the connection.
  3. In what file/method is this exception is raised?
agimenezwally commented 8 years ago

I just faced the exact problem the user is having and I know the why and the solution.

The user is connecting to a TLS server which does not support the STARTTLS extension. In this case, the stream must be surrounded with an SSL stream, negotiate the authentication and from there the process remains unchanged.

I have done myself these modifications:

1-modify the tls bool from the core to an enum with None,StartTLS,TLSSocket and refactor all referent changes 2-If tls is TLSSocket surround the stream at XmppCore.Connect and authenticate it with the host name as target srever 3-Else continue with normal process

I can share the project but I did not docummented nothing...

Cheers.

cerickson1492 commented 8 years ago

Thanks @agimenezwally . If you don't mind please share the code. I must have gotten confused with all the libraries I was looking at. I thought since it was based on S22 that the tls was the "old tls" and not starttls.

Thanks for the help!

agimenezwally commented 8 years ago

Beware, my project is modified to work with google cloud messaging, I needed to allow null Jid's on messages, expose the Xml utilities and some modifications more I to do it "swallow" the messages from cloud. I'm going to compress it and upload to dropbox.

agimenezwally commented 8 years ago

Here it is: https://www.dropbox.com/s/gaox07t0ll6ms74/Sharp.Xmpp.rar?dl=0

agimenezwally commented 8 years ago

In case you want to know how to compose the message for GCM here it is:

    public void SendDownStream(GCMDownStream Message)
    {
        Message.message_id = Guid.NewGuid().ToString();
        Message.dry_run = true;
        string payload = JsonConvert.SerializeObject(Message);

        var msg = Sharp.Xmpp.Xml.Element("message");
        var elem = Sharp.Xmpp.Xml.Element("gcm", "google:mobile:data");

        Sharp.Xmpp.Xml.Child(msg, elem);
        elem.Text(payload);

        xc.SendMessage(new Sharp.Xmpp.Im.Message(new Message(data: elem)));
    }

Also I have added a property called "UseRooster", you must set it to false or it will block waiting a response from the server which will never arrive.

cerickson1492 commented 8 years ago

Thank you @agimenezwally that is what I am doing as well. I will look at your code and implement accordingly.

I appreciate the help!

pgstath commented 8 years ago

@agimenezwally you are welcomed to make a pull request, especially for the parts that don't conflict with the standard!

agimenezwally commented 8 years ago

Hi @pgstath

I'm not sure which parts conflict with the standar. If you mean with the standard XMPP protocol, none of the changes conflict with it as GCM uses pure XMPP, but the way I implemented it is really ugly, I mean, actually there's no mechanism to create a message with a custom payload (it's always surrounded with a body at least on the current implementation), so i had to expose internal classes (XML namespace), expose the internal constructor of IM.Message "public Message(Core.Message message)" and comment the jid checking on the message, so as I said is very ugly (the part of the TLS and the UseRooster is well implemented :D).

But I'm open to help to create such mechanism, that would be great to expose an api well formed to create custom messages with a payload from an XML element.

What do you think?

agimenezwally commented 8 years ago

Ok, I think I managed to do something acceptable:

1-Added a new constructor to IM.Message which allows to create a message from an XmlElement 2-Left the Xmpp.Xml namespace public to allow to use the functions to create/append Xml elements 3-Implemented a new event named BodylessMessage which receives messages with no body 4-BodylessMessage uses MessageEventArgs, for that I created a new constructor which does not require a Jid as these messages come without an ID (they come from the server itself, not from an user) 6-Created a property on the connection to disable roster retrieval when connection is created. 7-Created a public function to send a default Presence stanza as the usual with no roster is to not send the presence, but in case some service uses it without roster the function is available. 8-Changed Tls from bool to enum, this allows to use StartTLS extension or Ssl sockets.

Also, I have updated the references from ARSoft.Tools.Net, the new references change the DNS resolution (instead of strings they use now a DomainName class).

If all of this looks good to you I can create a pull request to do the review.

Cheers.

pgstath commented 8 years ago

Hi, Yes points: 1,2,4,5,6,8 and the ARSoft.Tools.Net update look quite good. In principle I also agree with the need to provide an easy way to built custom stanzas, thus public Xmpp.Xml. I will take a look if 3 can be included in the Message event. So please create the peer request! Regards

SupportingIT commented 7 years ago

Just checking how this is coming along, as I'm interested in this myself.

Cheers