private void decodeInTree(byte[] data) { try { ProtocolTreeNode node = reader.nextTree(data, true); while (node != null) { File.AppendAllLines("xmpp.log", new string[] { node.NodeString("rx") }); //look for challengedata and forge key if (node.tag.Equals("challenge", StringComparison.InvariantCultureIgnoreCase) && !String.IsNullOrEmpty(this.password)) { this.AddListItem("ChallengeKey received, forging key..."); byte[] challengeData = node.GetData(); byte[] pass = Convert.FromBase64String(this.password); Rfc2898DeriveBytes r = new Rfc2898DeriveBytes(pass, challengeData, 16); byte[] key = r.GetBytes(20); reader.Encryptionkey = key; //reset static keys WhatsAppApi.Helper.Encryption.encryptionIncoming = null; WhatsAppApi.Helper.Encryption.encryptionOutgoing = null; } node = reader.nextTree(null, true); } } catch (IncompleteMessageException e) { } catch (Exception e) { this.AddListItem(String.Format("INDECODER ERROR: {0}", e.Message)); throw e; } }
private void NewMessageArrived(object sender, ProgressChangedEventArgs args) { if (args.UserState == null || !(args.UserState is ProtocolTreeNode[])) { return; } ProtocolTreeNode[] tmpMessages = args.UserState as ProtocolTreeNode[]; foreach (ProtocolTreeNode msg in tmpMessages) { ProtocolTreeNode contentNode = msg.GetChild("body") ?? msg.GetChild("enc"); byte[] contentData = contentNode.GetData(); String message = WhatsApp.SysEncoding.GetString(contentData); FMessage tmpMessage = new FMessage(new FMessage.FMessageIdentifierKey(msg.GetAttribute("from"), false, msg.GetAttribute("id"))); tmpMessage.binary_data = contentData; tmpMessage.data = message; WhatsEventHandler.OnMessageRecievedEventHandler(tmpMessage); } }
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": //TODO break; case "contacts": //TODO break; default: throw new NotImplementedException(node.NodeString()); } this.SendNotificationAck(node); }
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) { //text message this.fireOnGetMessage(node, node.GetAttribute("from"), node.GetAttribute("id"), node.GetAttribute("notify"), System.Text.Encoding.UTF8.GetString(node.GetChild("body").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(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(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(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(from, id, lon, lat, url, name, preview); break; case "vcard": ProtocolTreeNode vcard = media.GetChild("vcard"); name = vcard.GetAttribute("name"); dat = vcard.GetData(); this.fireOnGetMessageVcard(from, id, name, dat); break; } this.sendMessageReceived(node); } }
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()); Console.WriteLine("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); }
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 = Encoding.UTF8.GetString(itemNode.GetData()); var tmpMessKey = new FMessage.Key(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(Encoding.UTF8.GetString(tmpChildMedia.GetData())); } } else { string tmpAttrEncoding = itemNode.GetAttribute("encoding") ?? "text"; if (tmpAttrEncoding == "text") { builder.Data(Encoding.UTF8.GetString(itemNode.GetData())); } } var tmpMessageKey = new FMessage.Key(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.Key(tmpAttrFrom, true, tmpAttrbId); } } else if (ProtocolTreeNode.TagEquals(itemNode, "received")) { if (tmpAttrbId != null) { var tmpMessageKey = new FMessage.Key(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.Key().serverNickname = tmpAttrName; } } } if (!builder.Timestamp().HasValue) { builder.Timestamp(new DateTime?(DateTime.Now)); } FMessage message = builder.Build(); if (message != null) { WhatsEventHandler.OnMessageRecievedEventHandler(message); } }
private void TypeSubject(ProtocolTreeNode messageNode, string tmpFrom, string uJid, string tmpId, string tmpT) { bool flag = false; foreach (ProtocolTreeNode item in messageNode.GetAllChildren("request")) { if (item.GetAttribute("xmlns").Equals("urn:xmpp:receipts")) { flag = true; } } ProtocolTreeNode child = messageNode.GetChild("body"); //string subject = (child == null) ? null : child.GetDataString(); string subject = (child == null) ? null : WhatsAppProtocol.SYSEncoding.GetString(child.GetData()); if (subject != null) { WhatsEventHandler.OnGroupNewSubjectEventHandler(tmpFrom, uJid, subject, int.Parse(tmpT, CultureInfo.InvariantCulture)); } if (flag) { this.sendHandler.SendSubjectReceived(tmpFrom, tmpId); } }
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(), 0, child2.GetData().Length)); 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(), 0, node.GetChild("body").GetData().Length), Helpers.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"), Helpers.GetDateTimeFromTimestamp(t)); } else if (child3.tag == "remove") { this.fireOnGetParticipantRemoved(gjid, child3.GetAttribute("jid"), child3.GetAttribute("author"), Helpers.GetDateTimeFromTimestamp(t)); } else if (child3.tag == "modify") { this.fireOnGetParticipantRenamed(gjid, child3.GetAttribute("remove"), child3.GetAttribute("add"), Helpers.GetDateTimeFromTimestamp(t)); } } break; } this.SendNotificationAck(node); }