internal static void OnMessageRecievedEventHandler(FMessage mess)
 {
     var h = MessageRecievedEvent;
     if (h == null)
         return;
     foreach (var tmpSingleCast in h.GetInvocationList())
     {
         var tmpSyncInvoke = tmpSingleCast.Target as ISynchronizeInvoke;
         if (tmpSyncInvoke != null && tmpSyncInvoke.InvokeRequired)
         {
             tmpSyncInvoke.BeginInvoke(tmpSingleCast, new object[] { mess });
             continue;
         }
         h.BeginInvoke(mess, null, null);
     }
 }
Example #2
0
 private MessageBase ParseParameterToMessage(string message, object parameter, out short id)
 {
     MessageBase msgToSend = null;
     if (parameter == null) {
         id = DispatchEMessageId;
         EMessage emsg = new EMessage ();
         emsg.message = message;
         msgToSend = emsg;
     } else if (parameter is int) {
         id = DispatchIMessageId;
         IMessage imsg = new IMessage ();
         imsg.message = message;
         imsg.parameter = (int)parameter;
         msgToSend = imsg;
     } else if (parameter is float) {
         id = DispatchFMessageId;
         FMessage fmsg = new FMessage ();
         fmsg.message = message;
         fmsg.parameter = (float)parameter;
         msgToSend = fmsg;
     } else if (parameter is string) {
         id = DispatchSMessageId;
         SMessage smsg = new SMessage ();
         smsg.message = message;
         smsg.parameter = parameter as string;
         msgToSend = smsg;
     } else if (parameter is Vector3) {
         id = DispatchVMessageId;
         VMessage vmsg = new VMessage ();
         vmsg.message = message;
         vmsg.parameter = (Vector3)parameter;
         msgToSend = vmsg;
     } else if (parameter is Quaternion) {
         id = DispatchQMessageId;
         QMessage qmsg = new QMessage ();
         qmsg.message = message;
         qmsg.parameter = (Quaternion)parameter;
         msgToSend = qmsg;
     } else {
         id = -1;
         Debug.LogError ("Can't dispatch a non RPCable parameter");
     }
     return msgToSend;
 }
    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);
        }
    }
    internal void SendMessageWithMedia(FMessage message)
    {
        ProtocolTreeNode node;
        if (FMessage.Type.System == message.media_wa_type)
        {
            throw new SystemException("Cannot send system message over the network");
        }

        List<KeyValue> list = new List<KeyValue>(new KeyValue[] { new KeyValue("xmlns", "urn:xmpp:whatsapp:mms"), new KeyValue("type", FMessage.GetMessage_WA_Type_StrValue(message.media_wa_type)) });
        if (FMessage.Type.Location == message.media_wa_type)
        {
            list.AddRange(new KeyValue[] { new KeyValue("latitude", message.latitude.ToString(CultureInfo.InvariantCulture)), new KeyValue("longitude", message.longitude.ToString(CultureInfo.InvariantCulture)) });
            if (message.location_details != null)
            {
                list.Add(new KeyValue("name", message.location_details));
            }
            if (message.location_url != null)
            {
                list.Add(new KeyValue("url", message.location_url));
            }
        }
        else if (((FMessage.Type.Contact != message.media_wa_type) && (message.media_name != null)) && ((message.media_url != null) && (message.media_size > 0L)))
        {
            list.AddRange(new KeyValue[] { new KeyValue("file", message.media_name), new KeyValue("size", message.media_size.ToString(CultureInfo.InvariantCulture)), new KeyValue("url", message.media_url) });
            if (message.media_duration_seconds > 0)
            {
                list.Add(new KeyValue("seconds", message.media_duration_seconds.ToString(CultureInfo.InvariantCulture)));
            }
        }
        if ((FMessage.Type.Contact == message.media_wa_type) && (message.media_name != null))
        {
            node = new ProtocolTreeNode("media", list.ToArray(), new ProtocolTreeNode("vcard", new KeyValue[] { new KeyValue("name", message.media_name) }, WhatsAppProtocol.SYSEncoding.GetBytes(message.data)));
        }
        else
        {
            byte[] data = message.binary_data;
            if ((data == null) && !string.IsNullOrEmpty(message.data))
            {
                try
                {
                    data = Convert.FromBase64String(message.data);
                }
                catch (Exception)
                {
                }
            }
            if (data != null)
            {
                list.Add(new KeyValue("encoding", "raw"));
            }
            node = new ProtocolTreeNode("media", list.ToArray(), null, data);
        }
        this.whatsNetwork.SendData(this._binWriter.Write(node));
    }
 internal void SendMessageWithBody(FMessage message)
 {
     var child = new ProtocolTreeNode("body", null, null, Encoding.UTF8.GetBytes(message.data));
     this.whatsNetwork.SendData(this._binWriter.Write(GetMessageNode(message, child)));
 }
 internal static ProtocolTreeNode GetMessageNode(FMessage message, ProtocolTreeNode pNode)
 {
     return new ProtocolTreeNode("message", new[] { new KeyValue("to", message.key.remote_jid), new KeyValue("type", "chat"), new KeyValue("id", message.key.id) }, pNode);
 }
 public void SendStatusUpdate(string status, Action onComplete, Action<int> onError)
 {
     string id = TicketManager.GenerateId();
     FMessage message = new FMessage(new FMessage.Key("s.us", true, id));
     var messageNode = GetMessageNode(message, new ProtocolTreeNode("body", null, Encoding.UTF8.GetBytes(status)));
     this.whatsNetwork.SendData(this._binWriter.Write(messageNode));
 }
Example #8
0
        protected void SendMessageWithBody(FMessage message, bool hidden = false)
        {
            var child = new ProtocolTreeNode("body", null, null, WhatsApp.SYSEncoding.GetBytes(message.data));

            this.SendNode(GetMessageNode(message, child, hidden));
        }
 public void SendMessage(FMessage message)
 {
     if (message.media_wa_type != FMessage.Type.Undefined)
     {
         this.SendMessageWithMedia(message);
     }
     else
     {
         this.SendMessageWithBody(message);
     }
 }
 public FMessage.Builder SetInstance(FMessage message)
 {
     this.message = message;
     return this;
 }
 public FMessage.Builder NewOutgoingInstance()
 {
     if (((this.remote_jid == null) || (this.data == null)) || (this.thumb_image == null))
     {
         throw new NotSupportedException(
             "missing required property before instantiating new outgoing message");
     }
     if ((this.id != null) || (this.from_me.Value && !this.from_me.Value))
     {
         throw new NotSupportedException("invalid property set before instantiating new outgoing message");
     }
     this.message = new FMessage(this.remote_jid, this.data, this.thumb_image);
     return this;
 }
 public FMessage.Builder NewIncomingInstance()
 {
     if (((this.remote_jid == null) || !this.from_me.HasValue) || (this.id == null))
     {
         throw new NotSupportedException(
             "missing required property before instantiating new incoming message");
     }
     this.message =
         new FMessage(new FMessage.Key(this.remote_jid, this.from_me.Value, this.id));
     return this;
 }
 public FMessage.Builder Media_wa_type(FMessage.Type media_wa_type)
 {
     this.media_wa_type = new FMessage.Type?(media_wa_type);
     return this;
 }
 public FMessage.Builder Key(FMessage.Key key)
 {
     this.remote_jid = key.remote_jid;
     this.from_me = new bool?(key.from_me);
     this.id = key.id;
     return this;
 }
 public void Message(string to, string txt)
 {
     var tmpMessage = new FMessage(to, true) { key = { id = TicketManager.GenerateId() }, data = txt };
     this.WhatsParser.WhatsSendHandler.SendMessage(tmpMessage);
 }
 public void SendMessageReceived(FMessage message)
 {
     var child = new ProtocolTreeNode("received", new[] { new KeyValue("xmlns", "urn:xmpp:receipts") });
     var node = new ProtocolTreeNode("message", new[] { new KeyValue("to", message.key.remote_jid), new KeyValue("type", "chat"), new KeyValue("id", message.key.id) }, child);
     this.whatsNetwork.SendData(this._binWriter.Write(node));
 }
    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);
    }
Example #18
0
        protected void SendMessageWithMedia(FMessage message)
        {
            ProtocolTreeNode node;

            if (FMessage.Type.System == message.media_wa_type)
            {
                throw new SystemException("Cannot send system message over the network");
            }

            List <KeyValue> list = new List <KeyValue>(new KeyValue[] { new KeyValue("xmlns", "urn:xmpp:whatsapp:mms"), new KeyValue("type", FMessage.GetMessage_WA_Type_StrValue(message.media_wa_type)) });

            if (FMessage.Type.Location == message.media_wa_type)
            {
                list.AddRange(new KeyValue[] { new KeyValue("latitude", message.latitude.ToString(CultureInfo.InvariantCulture)), new KeyValue("longitude", message.longitude.ToString(CultureInfo.InvariantCulture)) });
                if (message.location_details != null)
                {
                    list.Add(new KeyValue("name", message.location_details));
                }
                if (message.location_url != null)
                {
                    list.Add(new KeyValue("url", message.location_url));
                }
            }
            else if (((FMessage.Type.Contact != message.media_wa_type) && (message.media_name != null)) && ((message.media_url != null) && (message.media_size > 0L)))
            {
                list.AddRange(new KeyValue[] { new KeyValue("file", message.media_name), new KeyValue("size", message.media_size.ToString(CultureInfo.InvariantCulture)), new KeyValue("url", message.media_url) });
                if (message.media_duration_seconds > 0)
                {
                    list.Add(new KeyValue("seconds", message.media_duration_seconds.ToString(CultureInfo.InvariantCulture)));
                }
            }
            if ((FMessage.Type.Contact == message.media_wa_type) && (message.media_name != null))
            {
                node = new ProtocolTreeNode("media", list.ToArray(), new ProtocolTreeNode("vcard", new KeyValue[] { new KeyValue("name", message.media_name) }, WhatsApp.SYSEncoding.GetBytes(message.data)));
            }
            else
            {
                byte[] data = message.binary_data;
                if ((data == null) && !string.IsNullOrEmpty(message.data))
                {
                    try
                    {
                        data = Convert.FromBase64String(message.data);
                    }
                    catch (Exception)
                    {
                    }
                }
                if (data != null)
                {
                    list.Add(new KeyValue("encoding", "raw"));
                }
                node = new ProtocolTreeNode("media", list.ToArray(), null, data);
            }
            this.SendNode(GetMessageNode(message, node));
        }