private static void TypeNotificationPicture(ProtocolTreeNode tmpChild, string tmpFrom) { foreach (ProtocolTreeNode item in (tmpChild.GetAllChildren() ?? new ProtocolTreeNode[0])) { if (ProtocolTreeNode.TagEquals(item, "set")) { string photoId = item.GetAttribute("id"); if (photoId != null) { //this.EventHandler.OnPhotoChanged(tmpFrom, item.GetAttribute("author"), photoId); WhatsEventHandler.OnPhotoChangedEventHandler(tmpFrom, item.GetAttribute("author"), photoId); } } else if (ProtocolTreeNode.TagEquals(item, "delete")) { //this.EventHandler.OnPhotoChanged(tmpFrom, item.GetAttribute("author"), null); WhatsEventHandler.OnPhotoChangedEventHandler(tmpFrom, item.GetAttribute("author"), null); } } }
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); } }
/// <summary> /// Type subject /// </summary> /// <param name="messageNode"></param> /// <param name="tmpFrom"></param> /// <param name="uJid"></param> /// <param name="tmpId"></param> /// <param name="tmpT"></param> 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 : WhatsApp.SYSEncoding.GetString(child.GetData()); if (subject != null) { WhatsEventHandler.OnGroupNewSubjectEventHandler(tmpFrom, uJid, subject, int.Parse(tmpT, CultureInfo.InvariantCulture)); } if (flag) { this.sendHandler.SendSubjectReceived(tmpFrom, tmpId); } }
/// <summary> /// Notify typing /// </summary> /// <param name="messageNode">The protocoltreenode</param> /// <param name="tmpAttrFrom">From?</param> /// <param name="tmpAttrbId">Message id</param> private void TypeNotification(ProtocolTreeNode messageNode, string tmpAttrFrom, string tmpAttrbId) { foreach (ProtocolTreeNode tmpChild in (messageNode.GetAllChildren() ?? new ProtocolTreeNode[0])) { if (ProtocolTreeNode.TagEquals(tmpChild, "notification")) { string tmpChildType = tmpChild.GetAttribute("type"); if (StringComparer.Ordinal.Equals(tmpChildType, "picture")) { TypeNotificationPicture(tmpChild, tmpAttrFrom); } } else if (ProtocolTreeNode.TagEquals(tmpChild, "request")) { this.sendHandler.SendNotificationReceived(tmpAttrFrom, tmpAttrbId); } } }
/// <summary> /// Type error /// </summary> /// <param name="messageNode"></param> /// <param name="tmpAttrbId"></param> /// <param name="tmpAttrFrom"></param> private void TypeError(ProtocolTreeNode messageNode, string tmpAttrbId, string tmpAttrFrom) { int num2 = 0; foreach (ProtocolTreeNode node in messageNode.GetAllChildren("error")) { string tmpCode = node.GetAttribute("code"); try { num2 = int.Parse(tmpCode, CultureInfo.InvariantCulture); } catch (Exception) { } } if ((tmpAttrFrom != null) && (tmpAttrbId != null)) { FMessage.Key key = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId); } }
/// <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.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(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.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 TypeError(ProtocolTreeNode messageNode, string tmpAttrbId, string tmpAttrFrom) { int num2 = 0; foreach (ProtocolTreeNode node in messageNode.GetAllChildren("error")) { string tmpCode = node.GetAttribute("code"); try { num2 = int.Parse(tmpCode, CultureInfo.InvariantCulture); } catch (Exception) { } } if ((tmpAttrFrom != null) && (tmpAttrbId != null)) { FMessage.Key key = new FMessage.Key(tmpAttrFrom, true, tmpAttrbId); //ErrorHandler handler = null; //if (trackedMessages.TryGetValue(key, out handler)) //{ // trackedMessages.Remove(key); // handler.OnError(num2); //} //else //{ // this.EventHandler.OnMessageError(key, num2); //} } }