public void ParseMessageRecv(ProtocolTreeNode messageNode) { FMessage.Builder builder = new FMessage.Builder(); string tmpAttrbId = messageNode.GetAttribute("id"); string tmpAttrFrom = messageNode.GetAttribute("from"); string tmpAttrFromName = messageNode.GetAttribute(""); string tmpAttrFromJid = messageNode.GetAttribute("author") ?? ""; string tmpAttrType = messageNode.GetAttribute("type"); string tmpTAttribT = messageNode.GetAttribute("t"); long result = 0L; if (!string.IsNullOrEmpty(tmpTAttribT) && long.TryParse(tmpTAttribT, out result)) { builder.Timestamp(new DateTime?(WhatsConstants.UnixEpoch.AddSeconds((double)result))); } if ("error".Equals(tmpAttrType)) { TypeError(messageNode, tmpAttrbId, tmpAttrFrom); } else if ("subject".Equals(tmpAttrType)) { TypeSubject(messageNode, tmpAttrFrom, tmpAttrFromJid, tmpAttrbId, tmpTAttribT); } else if ("chat".Equals(tmpAttrType)) { TypeChat(messageNode, tmpAttrFrom, tmpAttrbId, builder, tmpAttrFromJid); } else if ("notification".Equals(tmpAttrType)) { TypeNotification(messageNode, tmpAttrFrom, tmpAttrbId); } }
protected void sendMessageReceived(ProtocolTreeNode msg) { ProtocolTreeNode requestNode = msg.GetChild("request"); if (requestNode == null || !requestNode.GetAttribute("xmlns").Equals("urn:xmpp:receipts", StringComparison.OrdinalIgnoreCase)) return; FMessage tmpMessage = new FMessage(new FMessage.Key(msg.GetAttribute("from"), true, msg.GetAttribute("id"))); this.WhatsParser.WhatsSendHandler.SendMessageReceived(tmpMessage); }
protected bool processInboundData(byte[] msgdata, bool autoReceipt = true) { try { ProtocolTreeNode node = this.reader.nextTree(msgdata); if (node != null) { //foreach ( ProtocolTreeNode x in node.GetAllChildren() ) //{ // Console.Write(x.GetData().ToString()); //} 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 participant = node.GetAttribute("participant"); string id = node.GetAttribute("id"); string type = node.GetAttribute("type") ?? "delivery"; switch (type) { case "delivery": //delivered to target FireOnGetMessageReceivedClient(from, participant, id); break; case "read": FireOnGetMessageReadClient(from, participant, id); //todo break; case "played": //played by target //todo break; } var list = node.GetChild("list"); if (list != null) { foreach (var receipt in list.GetAllChildren()) { FireOnGetMessageReceivedClient(from, participant, receipt.GetAttribute("id")); } } //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 FireOnGetMessageReceivedServer(node.GetAttribute("from"), node.GetAttribute("participant"), node.GetAttribute("id")); } } if (node.tag == "notification") { this.handleNotification(node); } return(true); } } catch (Exception e) { throw e; } return(false); }
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")) { } 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) { } } } } else { // todo: implent } } 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)) { } else if ((str11 == null) || "available".Equals(str11)) { } } 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); } else if ((ProtocolTreeNode.TagEquals(protNode, "ib") /*&& (this.EventHandler != null)*/) && (protNode.GetChild("offline") != null)) { } }
protected void sendMessageReceived(ProtocolTreeNode msg, string type = "read") { FMessage tmpMessage = new FMessage(new FMessage.FMessageIdentifierKey(msg.GetAttribute("from"), true, msg.GetAttribute("id"))); this.SendMessageReceived(tmpMessage, type); }
protected void handleNotification(ProtocolTreeNode node) { if (!String.IsNullOrEmpty(node.GetAttribute("notify"))) { this.fireOnGetContactName(node.GetAttribute("from"), node.GetAttribute("notify")); } string type = node.GetAttribute("type"); switch (type) { case "picture": ProtocolTreeNode child = node.children.FirstOrDefault(); this.fireOnNotificationPicture(child.tag, child.GetAttribute("jid"), child.GetAttribute("id")); break; case "status": ProtocolTreeNode child2 = node.children.FirstOrDefault(); this.fireOnGetStatus(node.GetAttribute("from"), child2.tag, node.GetAttribute("notify"), System.Text.Encoding.UTF8.GetString(child2.GetData())); break; case "subject": //fire username notify this.fireOnGetContactName(node.GetAttribute("participant"), node.GetAttribute("notify")); //fire subject notify this.fireOnGetGroupSubject(node.GetAttribute("from"), node.GetAttribute("participant"), node.GetAttribute("notify"), System.Text.Encoding.UTF8.GetString(node.GetChild("body").GetData()), GetDateTimeFromTimestamp(node.GetAttribute("t"))); break; case "contacts": //TODO break; case "participant": string gjid = node.GetAttribute("from"); string t = node.GetAttribute("t"); foreach (ProtocolTreeNode child3 in node.GetAllChildren()) { if (child3.tag == "add") { this.fireOnGetParticipantAdded(gjid, child3.GetAttribute("jid"), GetDateTimeFromTimestamp(t)); } else if (child3.tag == "remove") { this.fireOnGetParticipantRemoved(gjid, child3.GetAttribute("jid"), child3.GetAttribute("author"), GetDateTimeFromTimestamp(t)); } else if (child3.tag == "modify") { this.fireOnGetParticipantRenamed(gjid, child3.GetAttribute("remove"), child3.GetAttribute("add"), GetDateTimeFromTimestamp(t)); } } break; } this.SendNotificationAck(node); }
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>(); if (existing != null) { 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>(); if (nonexisting != null) { 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.GetChild("query") != null ) { //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) && (node.GetChild("media") != null || node.GetChild("duplicate") != null) ) { //media upload this.uploadResponse = node; } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && node.GetChild("picture") != null ) { //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) && node.GetChild("ping") != null) { this.SendPong(node.GetAttribute("id")); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && node.GetChild("group") != null) { //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"), group.GetAttribute("creation"), group.GetAttribute("subject"), group.GetAttribute("s_t"), group.GetAttribute("s_o") )); } this.fireOnGetGroups(groups.ToArray()); } if (node.GetAttribute("type").Equals("result", StringComparison.OrdinalIgnoreCase) && node.GetChild("participant") != null) { //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())); } } if (node.GetAttribute("type") == "result" && node.GetChild("privacy") != null) { Dictionary <VisibilityCategory, VisibilitySetting> settings = new Dictionary <VisibilityCategory, VisibilitySetting>(); foreach (ProtocolTreeNode child in node.GetChild("privacy").GetAllChildren("category")) { settings.Add(this.parsePrivacyCategory( child.GetAttribute("name")), this.parsePrivacySetting(child.GetAttribute("value")) ); } this.fireOnGetPrivacySettings(settings); } }
protected void handleMessage(ProtocolTreeNode node, bool autoReceipt) { if (!string.IsNullOrEmpty(node.GetAttribute("notify"))) { string name = node.GetAttribute("notify"); this.fireOnGetContactName(node.GetAttribute("from"), name); } if (node.GetAttribute("type") == "error") { throw new NotImplementedException(node.NodeString()); } if (node.GetChild("body") != null || node.GetChild("enc") != null) { // text message // encrypted messages have no body node. Instead, the encrypted cipher text is provided within the enc node var contentNode = node.GetChild("body") ?? node.GetChild("enc"); if (contentNode != null) { this.fireOnGetMessage(node, node.GetAttribute("from"), node.GetAttribute("id"), node.GetAttribute("notify"), System.Text.Encoding.UTF8.GetString(contentNode.GetData()), autoReceipt); if (autoReceipt) { this.sendMessageReceived(node); } } } if (node.GetChild("media") != null) { ProtocolTreeNode media = node.GetChild("media"); //media message //define variables in switch string file, url, from, id; int size; byte[] preview, dat; id = node.GetAttribute("id"); from = node.GetAttribute("from"); switch (media.GetAttribute("type")) { case "image": url = media.GetAttribute("url"); file = media.GetAttribute("file"); size = Int32.Parse(media.GetAttribute("size")); preview = media.GetData(); this.fireOnGetMessageImage(node, from, id, file, size, url, preview); break; case "audio": file = media.GetAttribute("file"); size = Int32.Parse(media.GetAttribute("size")); url = media.GetAttribute("url"); preview = media.GetData(); this.fireOnGetMessageAudio(node, from, id, file, size, url, preview); break; case "video": file = media.GetAttribute("file"); size = Int32.Parse(media.GetAttribute("size")); url = media.GetAttribute("url"); preview = media.GetData(); this.fireOnGetMessageVideo(node, from, id, file, size, url, preview); break; case "location": double lon = double.Parse(media.GetAttribute("longitude"), System.Globalization.CultureInfo.InvariantCulture); double lat = double.Parse(media.GetAttribute("latitude"), System.Globalization.CultureInfo.InvariantCulture); preview = media.GetData(); name = media.GetAttribute("name"); url = media.GetAttribute("url"); this.fireOnGetMessageLocation(node, from, id, lon, lat, url, name, preview); break; case "vcard": ProtocolTreeNode vcard = media.GetChild("vcard"); name = vcard.GetAttribute("name"); dat = vcard.GetData(); this.fireOnGetMessageVcard(node, from, id, name, dat); break; } this.sendMessageReceived(node); } }
public WaUploadResponse(ProtocolTreeNode node) { node = node.GetChild("duplicate"); if (node != null) { int oSize, oWidth, oHeight, oDuration, oAsampfreq, oAbitrate; this.url = node.GetAttribute("url"); this.mimetype = node.GetAttribute("mimetype"); Int32.TryParse(node.GetAttribute("size"), out oSize); this.filehash = node.GetAttribute("filehash"); this.type = node.GetAttribute("type"); Int32.TryParse(node.GetAttribute("width"), out oWidth); Int32.TryParse(node.GetAttribute("height"), out oHeight); Int32.TryParse(node.GetAttribute("duration"), out oDuration); this.acodec = node.GetAttribute("acodec"); Int32.TryParse(node.GetAttribute("asampfreq"), out oAsampfreq); this.asampfmt = node.GetAttribute("asampfmt"); Int32.TryParse(node.GetAttribute("abitrate"), out oAbitrate); this.size = oSize; this.width = oWidth; this.height = oHeight; this.duration = oDuration; this.asampfreq = oAsampfreq; this.abitrate = oAbitrate; } }