public static TagEquals ( ProtocolTreeNode, node, string _string ) : bool | ||
node | ProtocolTreeNode, | |
_string | string | |
return | bool |
protected bool processInboundData(byte[] msgdata, bool autoReceipt = true) { try { ProtocolTreeNode node = this.reader.nextTree(msgdata); if (node != null) { if (ProtocolTreeNode.TagEquals(node, "challenge")) { this.processChallenge(node); } else if (ProtocolTreeNode.TagEquals(node, "success")) { this.loginStatus = CONNECTION_STATUS.LOGGEDIN; this.accountinfo = new AccountInfo(node.GetAttribute("status"), node.GetAttribute("kind"), node.GetAttribute("creation"), node.GetAttribute("expiration")); this.fireOnLoginSuccess(this.phoneNumber, node.GetData()); } else if (ProtocolTreeNode.TagEquals(node, "failure")) { this.loginStatus = CONNECTION_STATUS.UNAUTHORIZED; this.fireOnLoginFailed(node.children.FirstOrDefault().tag); } if (ProtocolTreeNode.TagEquals(node, "receipt")) { string from = node.GetAttribute("from"); string id = node.GetAttribute("id"); string type = node.GetAttribute("type") ?? "delivery"; switch (type) { case "delivery": //delivered to target this.fireOnGetMessageReceivedClient(from, id); break; case "read": //read by target //todo break; case "played": //played by target //todo break; } //send ack SendNotificationAck(node, type); } if (ProtocolTreeNode.TagEquals(node, "message")) { this.handleMessage(node, autoReceipt); } if (ProtocolTreeNode.TagEquals(node, "iq")) { this.handleIq(node); } if (ProtocolTreeNode.TagEquals(node, "stream:error")) { var textNode = node.GetChild("text"); if (textNode != null) { string content = WhatsApp.SYSEncoding.GetString(textNode.GetData()); Helper.DebugAdapter.Instance.fireOnPrintDebug("Error : " + content); } this.Disconnect(); } if (ProtocolTreeNode.TagEquals(node, "presence")) { //presence node this.fireOnGetPresence(node.GetAttribute("from"), node.GetAttribute("type")); } if (node.tag == "ib") { foreach (ProtocolTreeNode child in node.children) { switch (child.tag) { case "dirty": this.SendClearDirty(child.GetAttribute("type")); break; case "offline": //this.SendQrSync(null); break; default: throw new NotImplementedException(node.NodeString()); } } } if (node.tag == "chatstate") { string state = node.children.FirstOrDefault().tag; switch (state) { case "composing": this.fireOnGetTyping(node.GetAttribute("from")); break; case "paused": this.fireOnGetPaused(node.GetAttribute("from")); break; default: throw new NotImplementedException(node.NodeString()); } } if (node.tag == "ack") { string cls = node.GetAttribute("class"); if (cls == "message") { //server receipt this.fireOnGetMessageReceivedServer(node.GetAttribute("from"), node.GetAttribute("id")); } } if (node.tag == "notification") { this.handleNotification(node); } return(true); } } catch (Exception e) { throw e; } return(false); }
protected void handleIq(ProtocolTreeNode node) { if (node.GetAttribute("type") == "error") { this.fireOnError(node.GetAttribute("id"), node.GetAttribute("from"), Int32.Parse(node.GetChild("error").GetAttribute("code")), node.GetChild("error").GetAttribute("text")); } if (node.GetChild("sync") != null) { //sync result ProtocolTreeNode sync = node.GetChild("sync"); ProtocolTreeNode existing = sync.GetChild("in"); ProtocolTreeNode nonexisting = sync.GetChild("out"); //process existing first Dictionary <string, string> existingUsers = new Dictionary <string, string>(); foreach (ProtocolTreeNode child in existing.GetAllChildren()) { existingUsers.Add(System.Text.Encoding.UTF8.GetString(child.GetData()), child.GetAttribute("jid")); } //now process failed numbers List <string> failedNumbers = new List <string>(); foreach (ProtocolTreeNode child in nonexisting.GetAllChildren()) { failedNumbers.Add(System.Text.Encoding.UTF8.GetString(child.GetData())); } int index = 0; Int32.TryParse(sync.GetAttribute("index"), out index); this.fireOnGetSyncResult(index, sync.GetAttribute("sid"), existingUsers, failedNumbers.ToArray()); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && node.children.FirstOrDefault().tag == "query" ) { //last seen DateTime lastSeen = DateTime.Now.AddSeconds(double.Parse(node.children.FirstOrDefault().GetAttribute("seconds")) * -1); this.fireOnGetLastSeen(node.GetAttribute("from"), lastSeen); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && (ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "media") || ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "duplicate")) ) { //media upload this.uploadResponse = node; } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "picture") ) { //profile picture string from = node.GetAttribute("from"); string id = node.GetChild("picture").GetAttribute("id"); byte[] dat = node.GetChild("picture").GetData(); string type = node.GetChild("picture").GetAttribute("type"); if (type == "preview") { this.fireOnGetPhotoPreview(from, id, dat); } else { this.fireOnGetPhoto(from, id, dat); } } if (node.GetAttribute("type").Equals("get", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "ping")) { this.SendPong(node.GetAttribute("id")); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "group")) { //group(s) info List <WaGroupInfo> groups = new List <WaGroupInfo>(); foreach (ProtocolTreeNode group in node.children) { groups.Add(new WaGroupInfo( group.GetAttribute("id"), group.GetAttribute("owner"), long.Parse(group.GetAttribute("creation")), group.GetAttribute("subject"), long.Parse(group.GetAttribute("s_t")), group.GetAttribute("s_o") )); } this.fireOnGetGroups(groups.ToArray()); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.FirstOrDefault(), "participant")) { //group participants List <string> participants = new List <string>(); foreach (ProtocolTreeNode part in node.GetAllChildren()) { if (part.tag == "participant" && !string.IsNullOrEmpty(part.GetAttribute("jid"))) { participants.Add(part.GetAttribute("jid")); } } this.fireOnGetGroupParticipants(node.GetAttribute("from"), participants.ToArray()); } if (node.GetAttribute("type") == "result" && node.GetChild("status") != null) { foreach (ProtocolTreeNode status in node.GetChild("status").GetAllChildren()) { this.fireOnGetStatus(status.GetAttribute("jid"), "result", null, WhatsApp.SYSEncoding.GetString(status.GetData())); } } }
/// <summary> /// Notify typing chat /// </summary> /// <param name="messageNode"></param> /// <param name="tmpAttrFrom"></param> /// <param name="tmpAttrbId"></param> /// <param name="builder"></param> /// <param name="tmpAttrFromJid"></param> private void TypeChat(ProtocolTreeNode messageNode, string tmpAttrFrom, string tmpAttrbId, FMessage.Builder builder, string tmpAttrFromJid) { foreach (ProtocolTreeNode itemNode in (messageNode.GetAllChildren() ?? new ProtocolTreeNode[0])) { if (ProtocolTreeNode.TagEquals(itemNode, "composing")) { WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, true); } else if (ProtocolTreeNode.TagEquals(itemNode, "paused")) { WhatsEventHandler.OnIsTypingEventHandler(tmpAttrFrom, false); } else if (ProtocolTreeNode.TagEquals(itemNode, "body") && (tmpAttrbId != null)) { string dataString = WhatsApp.SYSEncoding.GetString(itemNode.GetData()); var tmpMessKey = new FMessage.FMessageIdentifierKey(tmpAttrFrom, false, tmpAttrbId); builder.Key(tmpMessKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance().Data(dataString); } else if (ProtocolTreeNode.TagEquals(itemNode, "media") && (tmpAttrbId != null)) { long tmpMediaSize; int tmpMediaDuration; builder.Media_wa_type(FMessage.GetMessage_WA_Type(itemNode.GetAttribute("type"))).Media_url( itemNode.GetAttribute("url")).Media_name(itemNode.GetAttribute("file")); if (long.TryParse(itemNode.GetAttribute("size"), WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpMediaSize)) { builder.Media_size(tmpMediaSize); } string tmpAttrSeconds = itemNode.GetAttribute("seconds"); if ((tmpAttrSeconds != null) && int.TryParse(tmpAttrSeconds, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpMediaDuration)) { builder.Media_duration_seconds(tmpMediaDuration); } if (builder.Media_wa_type().HasValue&& (builder.Media_wa_type().Value == FMessage.Type.Location)) { double tmpLatitude = 0; double tmpLongitude = 0; string tmpAttrLatitude = itemNode.GetAttribute("latitude"); string tmpAttrLongitude = itemNode.GetAttribute("longitude"); if ((tmpAttrLatitude == null) || (tmpAttrLongitude == null)) { tmpAttrLatitude = "0"; tmpAttrLongitude = "0"; } else if (!double.TryParse(tmpAttrLatitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLatitude) || !double.TryParse(tmpAttrLongitude, WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out tmpLongitude)) { throw new CorruptStreamException("location message exception parsing lat or long attribute: " + tmpAttrLatitude + " " + tmpAttrLongitude); } builder.Latitude(tmpLatitude).Longitude(tmpLongitude); string tmpAttrName = itemNode.GetAttribute("name"); string tmpAttrUrl = itemNode.GetAttribute("url"); if (tmpAttrName != null) { builder.Location_details(tmpAttrName); } if (tmpAttrUrl != null) { builder.Location_url(tmpAttrUrl); } } if (builder.Media_wa_type().HasValue&& (builder.Media_wa_type().Value) == FMessage.Type.Contact) { ProtocolTreeNode tmpChildMedia = itemNode.GetChild("media"); if (tmpChildMedia != null) { builder.Media_name(tmpChildMedia.GetAttribute("name")).Data(WhatsApp.SYSEncoding.GetString(tmpChildMedia.GetData())); } } else { string tmpAttrEncoding = itemNode.GetAttribute("encoding") ?? "text"; if (tmpAttrEncoding == "text") { builder.Data(WhatsApp.SYSEncoding.GetString(itemNode.GetData())); } } var tmpMessageKey = new FMessage.FMessageIdentifierKey(tmpAttrFrom, false, tmpAttrbId); builder.Key(tmpMessageKey).Remote_resource(tmpAttrFromJid).NewIncomingInstance(); } else if (ProtocolTreeNode.TagEquals(itemNode, "request")) { builder.Wants_receipt(true); } else if (ProtocolTreeNode.TagEquals(itemNode, "x")) { string str16 = itemNode.GetAttribute("xmlns"); if ("jabber:x:event".Equals(str16) && (tmpAttrbId != null)) { var tmpMessageKey = new FMessage.FMessageIdentifierKey(tmpAttrFrom, true, tmpAttrbId); } } else if (ProtocolTreeNode.TagEquals(itemNode, "received")) { if (tmpAttrbId != null) { var tmpMessageKey = new FMessage.FMessageIdentifierKey(tmpAttrFrom, true, tmpAttrbId); if (true) { string tmpAttrType = itemNode.GetAttribute("type"); if ((tmpAttrType != null) && !tmpAttrType.Equals("delivered")) { if (tmpAttrType.Equals("visible")) { this.sendHandler.SendVisibleReceiptAck(tmpAttrFrom, tmpAttrbId); } } else { this.sendHandler.SendDeliveredReceiptAck(tmpAttrFrom, tmpAttrbId); } } } } else if (ProtocolTreeNode.TagEquals(itemNode, "offline")) { builder.Offline(true); } else if (ProtocolTreeNode.TagEquals(itemNode, "notify")) { var tmpAttrName = itemNode.GetAttribute("name"); if (tmpAttrName != null) { builder.from_me = false; builder.id = tmpAttrbId; builder.remote_jid = tmpAttrFromJid; builder.serverNickname = tmpAttrName; } } } if (!builder.Timestamp().HasValue) { builder.Timestamp(new DateTime?(DateTime.Now)); } FMessage message = builder.Build(); if (message != null) { WhatsEventHandler.OnMessageRecievedEventHandler(message); } }
/// <summary> /// Process inbound data /// </summary> /// <param name="data">Data to process</param> protected void processInboundData(byte[] data) { try { List <byte> foo = new List <byte>(); if (this._incompleteBytes.Count > 0) { foreach (IncompleteMessageException e in this._incompleteBytes) { foo.AddRange(e.getInput()); } this._incompleteBytes.Clear(); } if (data != null) { foo.AddRange(data); } ProtocolTreeNode node = this.reader.nextTree(foo.ToArray()); while (node != null) { //this.WhatsParser.ParseProtocolNode(node); if (node.tag == "iq" && node.GetAttribute("type") == "error") { this.AddMessage(node); } if (ProtocolTreeNode.TagEquals(node, "challenge")) { this.processChallenge(node); } else if (ProtocolTreeNode.TagEquals(node, "success")) { this.loginStatus = CONNECTION_STATUS.LOGGEDIN; this.accountinfo = new AccountInfo(node.GetAttribute("status"), node.GetAttribute("kind"), node.GetAttribute("creation"), node.GetAttribute("expiration")); } else if (ProtocolTreeNode.TagEquals(node, "failure")) { this.loginStatus = CONNECTION_STATUS.UNAUTHORIZED; } if (ProtocolTreeNode.TagEquals(node, "message")) { this.AddMessage(node); if (node.GetChild("request") != null) { this.sendMessageReceived(node); } else if (node.GetChild("received") != null) { this.sendMessageReceived(node, "ack"); } } if (ProtocolTreeNode.TagEquals(node, "stream:error")) { Console.Write(node.NodeString()); } if (ProtocolTreeNode.TagEquals(node, "iq") && node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.First(), "query") ) { //last seen this.AddMessage(node); } if (ProtocolTreeNode.TagEquals(node, "iq") && node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && (ProtocolTreeNode.TagEquals(node.children.First(), "media") || ProtocolTreeNode.TagEquals(node.children.First(), "duplicate")) ) { //media upload this.uploadResponse = node; } if (ProtocolTreeNode.TagEquals(node, "iq") && node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.First(), "picture") ) { //profile picture this.AddMessage(node); } if (ProtocolTreeNode.TagEquals(node, "iq") && node.GetAttribute("type").Equals("get", StringComparison.OrdinalIgnoreCase) && ProtocolTreeNode.TagEquals(node.children.First(), "ping")) { this.Pong(node.GetAttribute("id")); } if (ProtocolTreeNode.TagEquals(node, "stream:error")) { var textNode = node.GetChild("text"); if (textNode != null) { string content = WhatsApp.SYSEncoding.GetString(textNode.GetData()); Console.WriteLine("Error : " + content); if (content.Equals("Replaced by new connection", StringComparison.OrdinalIgnoreCase)) { this.Disconnect(); this.Connect(); this.Login(); } } } if (ProtocolTreeNode.TagEquals(node, "presence")) { //presence node this.AddMessage(node); } node = this.reader.nextTree(); } } catch (IncompleteMessageException ex) { this._incompleteBytes.Add(ex); } }
/// <summary> /// Parse a tree node /// </summary> /// <param name="protNode">An instance of the ProtocolTreeNode class that needs to be parsed.</param> public void ParseProtocolNode(ProtocolTreeNode protNode) { if (ProtocolTreeNode.TagEquals(protNode, "iq")) { string attributeValue = protNode.GetAttribute("type"); string id = protNode.GetAttribute("id"); string str3 = protNode.GetAttribute("from"); if (attributeValue == null) { throw new Exception("Message-Corrupt: missing 'type' attribute in iq stanza"); } if (!attributeValue.Equals("result")) { if (!attributeValue.Equals("get")) { if (!attributeValue.Equals("set")) { throw new Exception("Message-Corrupt: unknown iq type attribute: " + attributeValue); } ProtocolTreeNode child = protNode.GetChild("query"); if (child != null) { string str8 = child.GetAttribute("xmlns"); if ("jabber:iq:roster" == str8) { foreach (ProtocolTreeNode node5 in child.GetAllChildren("item")) { node5.GetAttribute("jid"); node5.GetAttribute("subscription"); node5.GetAttribute("ask"); } } } } else { ProtocolTreeNode node3 = protNode.GetChild("ping"); if ((!ProtocolTreeNode.TagEquals(node3, "query") || (str3 == null)) && (ProtocolTreeNode.TagEquals(node3, "relay") && (str3 != null))) { int num; string pin = node3.GetAttribute("pin"); string tmpTimeout = node3.GetAttribute("timeout"); if (!int.TryParse(tmpTimeout ?? "0", WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out num)) { throw new CorruptStreamException("relay-iq exception parsing timeout attribute: " + tmpTimeout); } } } } } else if (ProtocolTreeNode.TagEquals(protNode, "presence")) { string str9 = protNode.GetAttribute("xmlns"); string jid = protNode.GetAttribute("from"); if (((str9 == null) || "urn:xmpp".Equals(str9)) && (jid != null)) { string str11 = protNode.GetAttribute("type"); } else if ("w".Equals(str9) && (jid != null)) { string str12 = protNode.GetAttribute("add"); string str13 = protNode.GetAttribute("remove"); string str14 = protNode.GetAttribute("status"); if (str12 == null) { if (str13 == null) { if ("dirty".Equals(str14)) { Dictionary <string, long> categories = ParseCategories(protNode); } } } } } else if (ProtocolTreeNode.TagEquals(protNode, "message")) { this.messResponseHandler.ParseMessageRecv(protNode); } }
public void ParseProtocolNode(ProtocolTreeNode protNode) { if (ProtocolTreeNode.TagEquals(protNode, "iq")) { string attributeValue = protNode.GetAttribute("type"); string id = protNode.GetAttribute("id"); string str3 = protNode.GetAttribute("from"); if (attributeValue == null) { throw new Exception("Message-Corrupt: missing 'type' attribute in iq stanza"); } if (!attributeValue.Equals("result")) { if (attributeValue.Equals("error")) { //IqResultHandler handler2 = this.PopIqHandler(id); //if (handler2 != null) //{ // handler2.ErrorNode(node); //} } else if (!attributeValue.Equals("get")) { if (!attributeValue.Equals("set")) { throw new Exception("Message-Corrupt: unknown iq type attribute: " + attributeValue); } ProtocolTreeNode child = protNode.GetChild("query"); if (child != null) { string str8 = child.GetAttribute("xmlns"); if ("jabber:iq:roster" == str8) { foreach (ProtocolTreeNode node5 in child.GetAllChildren("item")) { node5.GetAttribute("jid"); node5.GetAttribute("subscription"); node5.GetAttribute("ask"); } } } } else { ProtocolTreeNode node3 = protNode.GetChild("ping"); if (node3 != null) { //this.EventHandler.OnPing(id); } else if ((!ProtocolTreeNode.TagEquals(node3, "query") || (str3 == null)) && (ProtocolTreeNode.TagEquals(node3, "relay") && (str3 != null))) { int num; string pin = node3.GetAttribute("pin"); string tmpTimeout = node3.GetAttribute("timeout"); if ( !int.TryParse(tmpTimeout ?? "0", WhatsConstants.WhatsAppNumberStyle, CultureInfo.InvariantCulture, out num)) { //throw new CorruptStreamException( // "relay-iq exception parsing timeout attribute: " + tmpTimeout); } if (pin != null) { //this.EventHandler.OnRelayRequest(pin, num, id); } } } } else { //IqResultHandler handler = this.PopIqHandler(id); //if (handler != null) //{ // handler.Parse(node, str3); //} //else if (id.StartsWith(this.Login.User)) //{ // ProtocolNode node2 = node.GetChild(0); // ProtocolNode.Require(node2, "account"); // string str4 = node2.GetAttribute("kind"); // if ("paid".Equals(str4)) // { // this.account_kind = AccountKind.Paid; // } // else if ("free".Equals(str4)) // { // this.account_kind = AccountKind.Free; // } // else // { // this.account_kind = AccountKind.Unknown; // } // string s = node2.GetAttribute("expiration"); // if (s == null) // { // throw new IOException("no expiration"); // } // try // { // this.expire_date = long.Parse(s, CultureInfo.InvariantCulture); // } // catch (FormatException) // { // throw new IOException("invalid expire date: " + s); // } // this.EventHandler.OnAccountChange(this.account_kind, this.expire_date); //} } } else if (ProtocolTreeNode.TagEquals(protNode, "presence")) { string str9 = protNode.GetAttribute("xmlns"); string jid = protNode.GetAttribute("from"); if (((str9 == null) || "urn:xmpp".Equals(str9)) && (jid != null)) { string str11 = protNode.GetAttribute("type"); if ("unavailable".Equals(str11)) { //this.EventHandler.OnAvailable(jid, false); } else if ((str11 == null) || "available".Equals(str11)) { //this.EventHandler.OnAvailable(jid, true); } } else if ("w".Equals(str9) && (jid != null)) { string str12 = protNode.GetAttribute("add"); string str13 = protNode.GetAttribute("remove"); string str14 = protNode.GetAttribute("status"); if (str12 == null) { if (str13 == null) { if ("dirty".Equals(str14)) { Dictionary <string, long> categories = ParseCategories(protNode); //this.EventHandler.OnDirty(categories); } } //else if (this.GroupEventHandler != null) //{ // this.GroupEventHandler.OnGroupRemoveUser(jid, str13); //} } //else if (this.GroupEventHandler != null) //{ // this.GroupEventHandler.OnGroupAddUser(jid, str12); //} } } else if (ProtocolTreeNode.TagEquals(protNode, "message")) { this.messResponseHandler.ParseMessageRecv(protNode); } else if ((ProtocolTreeNode.TagEquals(protNode, "ib") /*&& (this.EventHandler != null)*/) && (protNode.GetChild("offline") != null)) { //this.EventHandler.OnOfflineMessagesCompleted(); } }