示例#1
0
            public static Contact Read(XmlReader reader)
            {
                var result = new Contact();

                result.ID = JabberID.Parse(reader.GetAttribute("jid"));

                reader.Read();
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    switch (reader.LocalName)
                    {
                    case "username": result.Username = reader.ReadElementContentAsString(); break;

                    case "display-name": result.DisplayName = reader.ReadElementContentAsString(); break;

                    case "pic": result.ProfilePicture = new Uri(reader.ReadElementContentAsString()); break;

                    case "verified": result.Verified = true; reader.Skip(); break;

                    case "pubkey": reader.Skip(); break;

                    default: throw new NotImplementedException();
                    }
                }
                reader.ReadEndElement();
                return(result);
            }
示例#2
0
 private void HandleMessage(XmlNode root, JabberID from, JabberID group, XmlNode bodyNode)
 {
     if (bodyNode != null)
     {
         Console.WriteLine($"[{group}] {from}: {bodyNode.InnerText}");
     }
     else
     {
     }
 }
示例#3
0
        public override bool Equals(object obj)
        {
            if (!(obj is JabberID))
            {
                return(false);
            }
            JabberID other = (JabberID)obj;

            return(Node == other.Node && Domain == other.Domain && Resource == other.Resource);
        }
示例#4
0
        private void HandleGroupStatusUpdate(XmlNode root, JabberID from, JabberID group, XmlNode statusNode)
        {
            string message = statusNode.InnerText;

            if (message.EndsWith("has joined the chat"))
            {
                HandleMemberJoined(group, JabberID.Parse(statusNode.Attributes["jid"].InnerText));
            }
            else if (message.EndsWith("has left the chat"))
            {
                HandleMemberLeft(group, JabberID.Parse(statusNode.Attributes["jid"].InnerText));
            }
        }
示例#5
0
        public Request AddFriend(JabberID user)
        {
            var request   = new Request();
            var timestamp = request.Created.ToUnixMillisecondsTimestamp();

            var packet = new StringBuilder();

            packet.Append($"<iq type=\"set\" id=\"{request.ID}\">");
            packet.Append($"<query xmlns=\"kik:iq:friend\">");
            packet.Append($"<add jid=\"{user}\" />");
            packet.Append($"</query>");
            packet.Append($"</iq>");

            _pump.AddHandler(request.ID, request.Answer);
            _conn.Write(packet.ToString());

            return(request);
        }
示例#6
0
            public static Group Read(XmlReader reader)
            {
                var result = new Group();

                result.IsPublic = bool.Parse(reader.GetAttribute("is-public"));
                result.ID       = JabberID.Parse(reader.GetAttribute("jid"));

                reader.Read();
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    switch (reader.LocalName)
                    {
                    case "code": result.Hashtag = reader.ReadElementContentAsString(); break;

                    case "n": result.Name = reader.ReadElementContentAsString(); break;

                    case "pic": result.ProfilePicture = new Uri(reader.ReadElementContentAsString()); break;

                    case "m":
                        var role = Role.Member;
                        if (reader.GetAttribute("s") == "1")
                        {
                            role = Role.SuperAdmin;
                        }
                        else if (reader.GetAttribute("a") == "1")
                        {
                            role = Role.Admin;
                        }
                        var member_jid = JabberID.Parse(reader.ReadElementContentAsString());
                        result.Members.Add(member_jid, role);
                        break;

                    case "b":
                        var banned_jid = JabberID.Parse(reader.ReadElementContentAsString());
                        result.Members.Add(banned_jid, Role.Banned);
                        break;

                    default: throw new NotImplementedException();
                    }
                }
                reader.ReadEndElement();
                return(result);
            }
示例#7
0
        public Request UnbanFromGroup(JabberID group, JabberID member)
        {
            var request   = new Request();
            var timestamp = request.Created.ToUnixMillisecondsTimestamp();

            var packet = new StringBuilder();

            packet.Append($"<iq type=\"set\" id=\"{request.ID}\">");
            packet.Append($"<query xmlns=\"kik:groups:admin\">");
            packet.Append($"<g jid=\"{group}\">");
            packet.Append($"<b r=\"1\">{member}</b>");
            packet.Append($"</g>");
            packet.Append($"</query>");
            packet.Append($"</iq>");

            _pump.AddHandler(request.ID, request.Answer);
            _conn.Write(packet.ToString());

            return(request);
        }
示例#8
0
        public Request RemoveFriend(JabberID friendID)
        {
            Contract.Requires(friendID.IsUser);

            var request   = new Request();
            var timestamp = request.Created.ToUnixMillisecondsTimestamp();

            var packet = new StringBuilder();

            packet.Append($"<iq type=\"set\" id=\"{request.ID}\">");
            packet.Append($"<query xmlns=\"kik:iq:friend\">");
            packet.Append($"<remove jid=\"{friendID}\" />");
            packet.Append($"</query>");
            packet.Append($"</iq>");

            _pump.AddHandler(request.ID, request.Answer);
            _conn.Write(packet.ToString());

            return(request);
        }
示例#9
0
        public Request SendMessage(JabberID groupID, string message)
        {
            Contract.Requires(groupID.IsGroup);

            var request   = new Request();
            var timestamp = request.Created.ToUnixMillisecondsTimestamp();

            var packet = new StringBuilder();

            packet.Append($"<message type=\"groupchat\" to=\"{groupID}\" id=\"{request.ID}\" cts=\"{timestamp}\">");
            packet.Append($"<body>{message}</body>");
            packet.Append($"<kik push=\"true\" qos=\"true\" timestamp=\"{timestamp}\" />");
            packet.Append($"<request xmlns=\"kik:message:receipt\" r=\"true\" d=\"true\" />");
            packet.Append($"</message>");

            _pump.AddHandler(request.ID, request.Answer);
            _conn.Write(packet.ToString());

            return(request);
        }
示例#10
0
        public LoginResult Login(string username, string password)
        {
            Contract.Requires(Validation.IsValidUsername(username));
            Contract.Requires(password != null);
            Contract.Requires(IsConnected);

            string guid   = Guid.NewGuid().ToString();
            var    writer = WriteXml();

            writer.WriteStartElement("iq");
            {
                writer.WriteAttributeString("type", "set");
                writer.WriteAttributeString("id", guid);
                writer.WriteStartElement("query", "jabber:iq:register");
                {
                    writer.WriteElementString("username", username);
                    writer.WriteElementString("passkey-u", Encryption.HashPassword(username, password));
                    writer.WriteElementString("device-id", Profile.DeviceID.ToString());
                    writer.WriteElementString("version", Profile.Version);
                }
                writer.WriteEndElement();
            }
            writer.WriteEndElement();
            writer.Close();

            var ack = ReadNodes().Single();
            {
                if (ack.LocalName != "ack")
                {
                    throw new ApiException("expected ack tag");
                }
                if (ack.Attributes["id"].InnerText != guid)
                {
                    throw new ApiException("acknowledgement for different query, is someone else using the stream?");
                }
            }

            LoginResult login = new LoginResult();

            login.xdata = new Dictionary <string, string>();
            var response = ReadNodes().Single();

            Contract.Assert(response.LocalName == "iq");
            Contract.Assert(response.ChildNodes.Count == 1);
            Contract.Assert(response.ChildNodes[0].LocalName == "query");
            foreach (XmlNode child in response.ChildNodes[0].ChildNodes)
            {
                switch (child.LocalName)
                {
                case "node": login.JabberID = new JabberID(child.InnerText, JabberID.UserDomain); break;

                case "email":
                    login.IsEmailConfirmed = bool.Parse(child.Attributes["confirmed"].InnerText);
                    login.Email            = child.InnerText;
                    break;

                case "username": login.Username = child.InnerText; break;

                case "first": login.Firstname = child.InnerText; break;

                case "last": login.Lastname = child.InnerText; break;

                case "xdata":
                    foreach (XmlNode xdata in child.ChildNodes)
                    {
                        Contract.Assert(xdata.LocalName == "record");
                        login.xdata.Add(xdata.Attributes["pk"].InnerText, xdata.InnerText);
                    }
                    break;

                default: break;
                }
            }

            //login stage 2: rebuild connection

            Close();
            SetupConnection(Profile);

            string sid             = "1c2a6e90-d1f4-4c07-b0d5-7dfb939d9354";// Encryption.ConvertToKikGuid(Guid.NewGuid()).ToString();
            var    jid             = login.JabberID;
            var    jidWithResource = new JabberID(login.JabberID, "CAN" + Profile.DeviceID);

            string passwordKey = Encryption.HashPassword(username, password);
            var    timestamp   = "1496333389122";
            string cv          = Encryption.HashWithHMAC(Profile, timestamp + ":" + jid);
            string signature   = Encryption.RsaSign(Profile, $"{jid}:{Profile.Version}:{timestamp}:{sid}");

            signature = signature.Substring(0, signature.Length - 2);

            Dictionary <string, string> attribs = new Dictionary <string, string>();

            attribs.Add("p", passwordKey);
            attribs.Add("cv", cv);
            attribs.Add("n", "1");
            attribs.Add("v", Profile.Version);
            attribs.Add("conn", "WIFI");
            attribs.Add("to", "talk.kik.com");
            attribs.Add("lang", "en_US");
            attribs.Add("from", jidWithResource.ToString());
            attribs.Add("sid", sid);
            attribs.Add("signed", signature);
            attribs.Add("ts", timestamp);

            string packet = "<k";
            var    sorted = Encryption.ToKikHashMap(attribs);

            foreach (KeyValuePair <string, string> attr in sorted)
            {
                packet += $" {attr.Key}=\"{attr.Value}\"";
            }
            packet += ">";
            Write(packet);

            var answerReader = ReadXml();

            if (answerReader.LocalName != "k")
            {
                throw new ApiException("expected k tag response");
            }
            if (answerReader.GetAttribute("ok") != "1")
            {
                throw new ApiException("server did not ok the session");
            }

            return(login);
        }
示例#11
0
 public JabberID(JabberID jabberID, string resource)
     : this(jabberID.Node, jabberID.Domain, resource)
 {
 }
示例#12
0
 private void HandleIsTyping(JabberID group, JabberID from, XmlNode root, XmlNode isTypingNode)
 {
     Console.WriteLine($"[{group}] {from} is-typing");
 }
示例#13
0
 private void HandleMemberLeft(JabberID group, JabberID memberID)
 {
     Console.WriteLine($"[{group}] --- {memberID}");
     SendMessage(group, File.ReadAllText("outro.txt"));
 }
示例#14
0
 private void HandleMemberJoined(JabberID group, JabberID memberID)
 {
     Console.WriteLine($"[{group}] +++ {memberID}");
     SendMessage(group, File.ReadAllText("intro.txt"));
 }