Ejemplo n.º 1
0
        public async Task <int> registerAsync(string name, string password)
        {
            startStream();

            Console.WriteLine("Receive: " + await receiveMsgAsync());

            string Msg = "<iq type='get' id='reg1' to='" + server + "'><query xmlns ='jabber:iq:register'/></iq> ";

            sendMsg(Msg);
            Console.WriteLine("Client: " + Msg);
            Console.WriteLine("Receive: " + await receiveMsgAsync());

            Msg = "<iq type='set' id='reg2'><query xmlns='jabber:iq:register'><username>" + name + "</username><password>" + password + "</password></query></iq>";
            Console.WriteLine("Client: " + Msg);
            sendMsg(Msg);

            var str = "<nesting>" + await receiveMsgAsync() + "</nesting>";

            Console.WriteLine("Receive: " + str);
            if (str != "<nesting><iq xml:lang='en' from='" + server + "' type='result' id='reg2'/></nesting>")
            {
                XmlSerializer ser    = new XmlSerializer(typeof(nesting));
                TextReader    reader = new StringReader(str);
                nesting       chall  = (nesting)ser.Deserialize(reader);
                if (chall.iq.ToArray()[0].error == null)
                {
                    str = "<nesting>" + await receiveMsgAsync() + "</nesting>";

                    Console.WriteLine("Receive: " + str);
                    ser    = new XmlSerializer(typeof(nesting));
                    reader = new StringReader(str);
                    chall  = (nesting)ser.Deserialize(reader);
                    if (chall.iq.ToArray()[0].error == null)
                    {
                        return(0);
                    }
                }
                Console.WriteLine("error: " + chall.iq.ToArray()[0].error.code);
                return(chall.iq.ToArray()[0].error.code);
            }
            return(0);
        }
Ejemplo n.º 2
0
        public void deserializeXml(string str)
        {
            str = "<nesting>" + str + "</nesting>";
            XmlSerializer ser    = new XmlSerializer(typeof(nesting));
            TextReader    reader = new StringReader(str);
            var           Reads  = XmlReader.Create(reader);

            try
            {
                nesting chal = (nesting)ser.Deserialize(Reads);
                if (chal.message != null)
                {
                    foreach (var chall in chal.message)
                    {
                        MW.Invoke(new MethodInvoker(() =>
                        {
                            if (chall.type == "error")
                            {
                                staticData.ChatHeads.Find(x => x.receiver == chall.from.Substring(0, chall.from.LastIndexOf('@'))).Close();
                                MessageBox.Show("The user does not excist", "No user", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            if (chall.delay != null)
                            {
                                chall.body = "(Delayed " + (Int32)((DateTime.Now - chall.delay.stamp).TotalMinutes - 180) + " minutes ago)   " + chall.body;
                            }
                            var msgW = staticData.ChatHeads.Find(x => x.receiver == chall.from.Substring(0, chall.from.LastIndexOf('@')));
                            if (msgW == null)
                            {
                                msgW          = new MsgWindow();
                                msgW.Text     = "Chat - " + chall.from.Substring(0, chall.from.LastIndexOf('@'));
                                msgW.receiver = chall.from.Substring(0, chall.from.LastIndexOf('@'));
                                msgW.sender   = staticData.Username;
                                staticData.ChatHeads.Add(msgW);
                                msgW.Show();
                            }
                            msgW.MsgB = msgW.receiver + ":    " + chall.body;
                            msgW.MsgB = Environment.NewLine;
                            if (msgW.Visible == false)
                            {
                                msgW.Show();
                            }
                        }));
                    }
                }
                if (chal.iq != null)
                {
                    foreach (var chall in chal.iq)
                    {
                        MW.Invoke(new MethodInvoker(() =>
                        {
                            if (chall.error != null)
                            {
                                return;
                            }
                            if (chall.query != null && chall.id == "roster-1")
                            {
                                friends.Clear();
                                MW.list.Items.Clear();
                                if (chall.query.item != null)
                                {
                                    foreach (var item in chall.query.item)
                                    {
                                        var name = item.jid.Substring(0, item.jid.LastIndexOf('@'));
                                        friends.Add(name);
                                        MW.list.Items.Add(name);
                                        MW.list.Refresh();
                                    }
                                }
                            }
                        }));
                    }
                }
            }
            catch
            {
                Console.WriteLine("Unrecognized XML stanza");
            }
        }