示例#1
0
 public FriendMessage(string msg, long time, bool local_sender, FriendMessageType t, byte[] sender_address = null, string sender_nick = "")
 {
     message           = msg;
     timestamp         = time;
     localSender       = local_sender;
     read              = false;
     type              = t;
     confirmed         = false;
     senderAddress     = sender_address;
     senderNick        = sender_nick;
     transferId        = "";
     completed         = false;
     filePath          = "";
     fileSize          = 0;
     receivedTimestamp = Clock.getTimestamp();
 }
示例#2
0
        public FriendMessage(byte[] bytes)
        {
            using (MemoryStream m = new MemoryStream(bytes))
            {
                using (BinaryReader reader = new BinaryReader(m))
                {
                    int id_len = reader.ReadInt32();
                    if (id_len > 0)
                    {
                        _id = reader.ReadBytes(id_len);
                    }
                    type        = (FriendMessageType)reader.ReadInt32();
                    message     = reader.ReadString();
                    timestamp   = reader.ReadInt64();
                    localSender = reader.ReadBoolean();
                    read        = reader.ReadBoolean();
                    confirmed   = reader.ReadBoolean();

                    int sender_address_len = reader.ReadInt32();
                    if (sender_address_len > 0)
                    {
                        senderAddress = reader.ReadBytes(sender_address_len);
                    }

                    senderNick = reader.ReadString();

                    transferId = reader.ReadString();

                    completed = reader.ReadBoolean();

                    filePath = reader.ReadString();
                    fileSize = reader.ReadUInt64();

                    receivedTimestamp = reader.ReadInt64();
                }
            }
        }
示例#3
0
        public static List <Send_0x00CD> SendLongMessage(QQUser User, string Message, FriendMessageType messageType,
                                                         long ToQQ)
        {
            var buffer = new BinaryWriter(new MemoryStream());
            var list   = new List <byte[]>();

            foreach (var chr in Message)
            {
                var bytes = Encoding.UTF8.GetBytes(chr.ToString());
                if (buffer.BaseStream.Length + bytes.Length > 699)
                {
                    list.Add(buffer.BaseStream.ToBytesArray());
                    buffer = new BinaryWriter(new MemoryStream());
                }

                buffer.Write(bytes);
            }

            if (buffer.BaseStream.Position != 0)
            {
                list.Add(buffer.BaseStream.ToBytesArray());
            }

            byte index = 0;
            var  ret   = new List <Send_0x00CD>();

            foreach (var byteBuffer in list)
            {
                ret.Add(new Send_0x00CD(User, "", messageType, ToQQ)
                {
                    _packetCount = (byte)list.Count,
                    _packetIndex = index++,
                    _message     = byteBuffer
                });
            }

            return(ret);
        }
示例#4
0
        public FriendMessage(byte[] bytes)
        {
            using (MemoryStream m = new MemoryStream(bytes))
            {
                using (BinaryReader reader = new BinaryReader(m))
                {
                    int id_len = reader.ReadInt32();
                    if (id_len > 0)
                    {
                        _id = reader.ReadBytes(id_len);
                    }
                    type        = (FriendMessageType)reader.ReadInt32();
                    message     = reader.ReadString();
                    timestamp   = reader.ReadInt64();
                    localSender = reader.ReadBoolean();
                    read        = reader.ReadBoolean();
                    confirmed   = reader.ReadBoolean();

                    int sender_address_len = reader.ReadInt32();
                    if (sender_address_len > 0)
                    {
                        senderAddress = reader.ReadBytes(sender_address_len);
                    }

                    senderNick = reader.ReadString();

                    transferId = reader.ReadString();

                    completed = reader.ReadBoolean();

                    filePath = reader.ReadString();
                    fileSize = reader.ReadUInt64();

                    receivedTimestamp = reader.ReadInt64();

                    // try/catch can be removed after upgrade
                    try
                    {
                        if (m.Position < m.Length)
                        {
                            transactionId  = reader.ReadString();
                            payableDataLen = reader.ReadInt32();

                            int reaction_count = reader.ReadInt32();
                            for (int i = 0; i < reaction_count; i++)
                            {
                                string reaction_key = reader.ReadString();
                                List <ReactionData> reaction_datas = new List <ReactionData>();
                                reactions.Add(reaction_key, reaction_datas);
                                int reaction_user_count = reader.ReadInt32();
                                for (int j = 0; j < reaction_user_count; j++)
                                {
                                    int          rd_len        = reader.ReadInt32();
                                    ReactionData reaction_data = new ReactionData(reader.ReadBytes(rd_len));
                                    reaction_datas.Add(reaction_data);
                                }
                            }
                        }
                        if (m.Position < m.Length)
                        {
                            sent = reader.ReadBoolean();
                        }
                    }
                    catch (Exception)
                    {
                        Logging.info("");
                    }
                }
            }
        }
示例#5
0
        public static List <Send_0x0002> SendLongMessage(QQUser User, string Message, FriendMessageType messageType, long Group)
        {
            var buffer = new ByteBuffer();
            var list   = new List <byte[]>();

            foreach (var chr in Message)
            {
                var bytes = Encoding.UTF8.GetBytes(chr.ToString());
                if (buffer.Length + bytes.Length > 699)
                {
                    list.Add(buffer.ToByteArray());
                    buffer.Initialize();
                }

                buffer.Put(bytes);
            }
            list.Add(buffer.ToByteArray());
            buffer.Initialize();

            byte index = 0;
            var  ret   = new List <Send_0x0002>();

            foreach (var byteBuffer in list)
            {
                ret.Add(new Send_0x0002(User, "", messageType, Group)
                {
                    _packetCount = (byte)list.Count,
                    _packetIndex = index++,
                    _message     = byteBuffer
                });
            }

            return(ret);
        }
示例#6
0
        public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, string message, bool local_sender = false, byte[] sender_address = null, long timestamp = 0, bool fire_local_notification = true)
        {
            Friend friend = getFriend(wallet_address);

            if (friend == null)
            {
                // No matching contact found in friendlist
                // Add the contact, then issue the message again?
                // TODO: need to fetch the stage 1 public key somehow here
                // Ignoring such messages for now
                //addFriend(wallet_address, "pubkey", "Unknown");
                //addMessage(wallet_address, message);

                Logging.warn("Received message but contact isn't in our contact list.");
                return(null);
            }

            Node.shouldRefreshContacts = true;

            if (!friend.online)
            {
                using (MemoryStream mw = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(mw))
                    {
                        writer.Write(wallet_address.Length);
                        writer.Write(wallet_address);

                        CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M' }, ProtocolMessageCode.getPresence, mw.ToArray(), null);
                    }
                }
            }

            string sender_nick = "";

            if (friend.bot && sender_address != null)
            {
                if (IxianHandler.getWalletStorage().isMyAddress(sender_address))
                {
                    local_sender = true;
                }
                if (!local_sender)
                {
                    if (friend.contacts.ContainsKey(sender_address) && friend.contacts[sender_address].nick != "")
                    {
                        sender_nick = friend.contacts[sender_address].nick;
                    }
                    else
                    {
                        if (!friend.contacts.ContainsKey(sender_address) || friend.contacts[sender_address].publicKey == null)
                        {
                            StreamProcessor.requestPubKey(friend, sender_address);
                        }
                        StreamProcessor.requestNickname(friend, sender_address);
                    }
                }
            }
            else
            {
                sender_nick = friend.nickname;
            }

            if (timestamp == 0)
            {
                timestamp = Clock.getTimestamp();
            }

            FriendMessage friend_message = new FriendMessage(id, message, timestamp, local_sender, type, sender_address, sender_nick);

            if (friend.bot)
            {
                if (local_sender)
                {
                    friend_message.read = true;
                }
                else
                {
                    friend.lastReceivedMessageId = id;
                    saveToStorage();
                }
            }

            lock (friend.messages)
            {
                // TODO should be optimized
                if (id != null && friend.messages.Find(x => x.id != null && x.id.SequenceEqual(id)) != null)
                {
                    Logging.warn("Message with id {0} was already in message list.", Crypto.hashToString(id));
                    return(null);
                }
                friend.messages.Add(friend_message);
            }

            // If a chat page is visible, insert the message directly
            if (friend.chat_page != null)
            {
                friend.chat_page.insertMessage(friend_message);
            }

            // Send a local push notification if Spixi is not in the foreground
            if (fire_local_notification && !local_sender)
            {
                if (App.isInForeground == false || friend.chat_page == null)
                {
                    // don't fire notification for nickname and avatar
                    if (!friend_message.id.SequenceEqual(new byte[] { 4 }) && !friend_message.id.SequenceEqual(new byte[] { 5 }))
                    {
                        DependencyService.Get <IPushService>().showLocalNotification("Spixi", "New Message", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress));
                    }
                }
            }

            ISystemAlert alert = DependencyService.Get <ISystemAlert>();

            if (alert != null)
            {
                alert.flash();
            }

            // Write to chat history
            Node.localStorage.writeMessages(wallet_address, friend.messages);

            return(friend_message);
        }
示例#7
0
        public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, string message, bool local_sender = false, byte[] sender_address = null)
        {
            foreach (Friend friend in friends)
            {
                if (!friend.walletAddress.SequenceEqual(wallet_address))
                {
                    continue;
                }

                Node.shouldRefreshContacts = true;

                if (!friend.online)
                {
                    using (MemoryStream mw = new MemoryStream())
                    {
                        using (BinaryWriter writer = new BinaryWriter(mw))
                        {
                            writer.Write(wallet_address.Length);
                            writer.Write(wallet_address);

                            CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M' }, ProtocolMessageCode.getPresence, mw.ToArray(), null);
                        }
                    }
                }

                string sender_nick = "";
                if (friend.bot)
                {
                    if (!local_sender)
                    {
                        if (friend.contacts.ContainsKey(sender_address))
                        {
                            sender_nick = friend.contacts[sender_address].nick;
                        }
                        else
                        {
                            StreamProcessor.requestNickname(friend, sender_address);
                        }
                    }
                }
                else
                {
                    sender_nick = friend.nickname;
                }

                // TODO: message date should be fetched, not generated here
                FriendMessage friend_message = new FriendMessage(id, message, Clock.getTimestamp(), local_sender, type, sender_address, sender_nick);

                if (friend.bot && local_sender)
                {
                    friend_message.read = true;
                }

                friend.messages.Add(friend_message);

                // If a chat page is visible, insert the message directly
                if (friend.chat_page != null)
                {
                    friend.chat_page.insertMessage(friend_message);
                }
                else
                {
                    //CrossLocalNotifications.Current.Show(string.Format("New message from {0}",friend.nickname), message, 100, DateTime.Now.AddSeconds(1));
                }

                // Write to chat history
                Node.localStorage.writeMessagesFile(wallet_address, friend.messages);

                return(friend_message);
            }

            // No matching contact found in friendlist
            // Add the contact, then issue the message again
            // TODO: need to fetch the stage 1 public key somehow here
            // Ignoring such messages for now
            //addFriend(wallet_address, "pubkey", "Unknown");
            //addMessage(wallet_address, message);
            return(null);
        }
示例#8
0
        public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, int channel, string message, bool local_sender = false, byte[] sender_address = null, long timestamp = 0, bool fire_local_notification = true, int payable_data_len = 0)
        {
            Friend friend = getFriend(wallet_address);

            if (friend == null)
            {
                // No matching contact found in friendlist
                // Add the contact, then issue the message again?
                // TODO: need to fetch the stage 1 public key somehow here
                // Ignoring such messages for now
                //addFriend(wallet_address, "pubkey", "Unknown");
                //addMessage(wallet_address, message);

                Logging.warn("Received message but contact isn't in our contact list.");
                return(null);
            }

            if (!friend.online)
            {
                using (MemoryStream mw = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(mw))
                    {
                        writer.WriteIxiVarInt(wallet_address.Length);
                        writer.Write(wallet_address);

                        CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M', 'H' }, ProtocolMessageCode.getPresence2, mw.ToArray(), null);
                    }
                }
            }

            bool set_read = false;

            string sender_nick = "";

            if (friend.bot && sender_address != null)
            {
                if (IxianHandler.getWalletStorage().isMyAddress(sender_address))
                {
                    if (!local_sender)
                    {
                        set_read = true;
                    }
                    local_sender = true;
                }
                if (!local_sender)
                {
                    if (friend.users.hasUser(sender_address) && friend.users.getUser(sender_address).getNick() != "")
                    {
                        sender_nick = friend.users.getUser(sender_address).getNick();
                    }
                    else
                    {
                        if (!friend.users.hasUser(sender_address) || friend.users.getUser(sender_address).publicKey == null)
                        {
                            StreamProcessor.requestBotUser(friend, sender_address);
                        }
                    }
                }
            }
            else
            {
                sender_nick = friend.nickname;
            }

            if (timestamp == 0)
            {
                timestamp = Clock.getTimestamp();
            }

            FriendMessage friend_message = new FriendMessage(id, message, timestamp, local_sender, type, sender_address, sender_nick);

            friend_message.payableDataLen = payable_data_len;

            List <FriendMessage> messages = friend.getMessages(channel);

            if (messages == null)
            {
                Logging.warn("Message with id {0} was sent to invalid channel {1}.", Crypto.hashToString(id), channel);
                return(null);
            }
            lock (messages)
            {
                // TODO should be optimized
                if (id != null)
                {
                    FriendMessage tmp_msg = messages.Find(x => x.id != null && x.id.SequenceEqual(id));

                    if (tmp_msg != null)
                    {
                        if (!tmp_msg.localSender)
                        {
                            Logging.warn("Message with id {0} was already in message list.", Crypto.hashToString(id));
                        }
                        else
                        {
                            friend.setMessageRead(channel, id);
                        }
                        if (messages.Last() == tmp_msg)
                        {
                            friend.metaData.setLastMessage(tmp_msg, channel);
                            friend.metaData.setLastReceivedMessageIds(tmp_msg.id, channel);
                            friend.saveMetaData();
                        }
                        return(null);
                    }
                    else
                    {
                        friend.metaData.setLastReceivedMessageIds(friend_message.id, channel);
                    }
                }
                else if (!local_sender)
                {
                    Logging.error("Message id sent by {0} is null!", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress));
                    return(null);
                }
                messages.Add(friend_message);
            }

            bool old_message = false;

            // Check if the message was sent before the friend was added to the contact list
            if (friend.addedTimestamp > friend_message.timestamp)
            {
                old_message = true;
            }

            if (set_read || old_message)
            {
                friend_message.confirmed = true;
                friend_message.read      = true;
            }

            friend.metaData.setLastMessage(friend_message, channel);
            friend.saveMetaData();

            // If a chat page is visible, insert the message directly
            if (friend.chat_page != null)
            {
                friend.chat_page.insertMessage(friend_message, channel);
            }
            else if (!set_read)
            {
                // Increase the unread counter if this is a new message
                if (!old_message)
                {
                    friend.metaData.unreadMessageCount++;
                }

                friend.saveMetaData();
            }

            UIHelpers.setContactStatus(friend.walletAddress, friend.online, friend.getUnreadMessageCount(), message, timestamp);

            // Only send alerts if this is a new message
            if (old_message == false)
            {
                // Send a local push notification if Spixi is not in the foreground
                if (fire_local_notification && !local_sender)
                {
                    if (App.isInForeground == false || friend.chat_page == null)
                    {
                        // don't fire notification for nickname and avatar
                        if (!friend_message.id.SequenceEqual(new byte[] { 4 }) && !friend_message.id.SequenceEqual(new byte[] { 5 }))
                        {
                            if (friend.bot == false ||
                                (friend.metaData.botInfo != null && friend.metaData.botInfo.sendNotification))
                            {
                                DependencyService.Get <IPushService>().showLocalNotification("Spixi", "New Message", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress));
                            }
                        }
                    }
                }

                ISystemAlert alert = DependencyService.Get <ISystemAlert>();
                if (alert != null)
                {
                    alert.flash();
                }
            }
            // Write to chat history
            Node.localStorage.requestWriteMessages(wallet_address, channel);

            return(friend_message);
        }