예제 #1
0
 public ChatMessageEventArgs(string remote, IntPtr chatPtr, List <IntPtr> callChatPtrList, VATRPChatMessage chatMessage)
     : base(remote)
 {
     _chatPtrList = new List <IntPtr>();
     _chatPtrList.AddRange(callChatPtrList);
     this.ChatPtr = chatPtr;
     _chatMessage = new VATRPChatMessage(chatMessage.ContentType);
     _chatMessage = chatMessage;
 }
 public ChatMessageEventArgs(string remote, IntPtr chatPtr, List<IntPtr> callChatPtrList, VATRPChatMessage chatMessage)
     : base(remote)
 {
     _chatPtrList = new List<IntPtr>();
     _chatPtrList.AddRange(callChatPtrList);
     this.ChatPtr = chatPtr;
     _chatMessage = new VATRPChatMessage(chatMessage.ContentType);
     _chatMessage = chatMessage;
 }
예제 #3
0
        public void LoadChatRoom(VATRPChat chat)
        {
            var address = string.Format("sip:{1}@{2}",  chat.Contact.ID, preferences.ProxyHost);
            IntPtr friendAddressPtr = LinphoneAPI.linphone_core_create_address(linphoneCore, address);
            if (friendAddressPtr == IntPtr.Zero)
                return;

            IntPtr chatRoomPtr = LinphoneAPI.linphone_core_get_chat_room(linphoneCore, friendAddressPtr);
            if (chatRoomPtr == IntPtr.Zero)
                return;
            IntPtr historyListPtr = LinphoneAPI.linphone_chat_room_get_history(chatRoomPtr, 100); // load all messages

            MSList curStruct;
            do
            {
                curStruct.next = IntPtr.Zero;
                curStruct.prev = IntPtr.Zero;
                curStruct.data = IntPtr.Zero;
                curStruct = (MSList)Marshal.PtrToStructure(historyListPtr, typeof(MSList));
                if (curStruct.data != IntPtr.Zero)
                {
                    IntPtr msgPtr = LinphoneAPI.linphone_chat_message_get_text(curStruct.data);
                    var messageString = string.Empty;
                    if (msgPtr != IntPtr.Zero)
                        messageString = Marshal.PtrToStringAnsi(msgPtr);

                    if (!string.IsNullOrEmpty(messageString) && messageString.Length > 1)
                    {
                        var localTime =
                            Time.ConvertUtcTimeToLocalTime(LinphoneAPI.linphone_chat_message_get_time(curStruct.data));

                        var chatMessage = new VATRPChatMessage(MessageContentType.Text)
                        {
                            Direction =
                                LinphoneAPI.linphone_chat_message_is_outgoing(curStruct.data) == 1
                                    ? MessageDirection.Outgoing
                                    : MessageDirection.Incoming,
                            IsIncompleteMessage = false,
                            MessageTime = localTime,
                            Content = messageString,
                            IsRead = LinphoneAPI.linphone_chat_message_is_read(curStruct.data) == 1,
                            IsRTTMessage = false,
                            IsRTTStartMarker = false,
                            IsRTTEndMarker = false,
                            Chat = chat
                        };
                        chat.Messages.Add(chatMessage);
                    }
                }
                historyListPtr = curStruct.next;
            } while (curStruct.next != IntPtr.Zero);
        }
예제 #4
0
        private void OnMessageReceived(IntPtr lc, IntPtr roomPtr, IntPtr message)
        {
            if (linphoneCore == IntPtr.Zero) return;

            var callChatRoomPtrList = new List<IntPtr>();

            if (LinphoneAPI.linphone_core_in_call(linphoneCore) != 0)
            {
                lock (callLock)
                {
                    callChatRoomPtrList.AddRange(callsList.Select(call => LinphoneAPI.linphone_call_get_chat_room(call.NativeCallPtr)));
                }
            }

            lock (messagingLock)
            {
                var from = string.Empty;
                var to = string.Empty;
                IntPtr addressPtr = LinphoneAPI.linphone_chat_message_get_from_address(message);
                if (addressPtr != IntPtr.Zero)
                {
                    IntPtr addressStringPtr = LinphoneAPI.linphone_address_as_string(addressPtr);
                    if (addressStringPtr != IntPtr.Zero)
                    {
                        from = Marshal.PtrToStringAnsi(addressStringPtr);
                        LinphoneAPI.ortp_free(addressStringPtr);
                    }
                }

                addressPtr = LinphoneAPI.linphone_chat_message_get_to_address(message);
                if (addressPtr != IntPtr.Zero)
                {
                    IntPtr addressStringPtr = LinphoneAPI.linphone_address_as_string(addressPtr);
                    if (addressStringPtr != IntPtr.Zero)
                    {
                        to = Marshal.PtrToStringAnsi(addressStringPtr);
                        LinphoneAPI.ortp_free(addressStringPtr);
                    }
                }

                IntPtr msgPtr = LinphoneAPI.linphone_chat_message_get_text(message);
                var messageString = string.Empty;
                if (msgPtr != IntPtr.Zero)
                    messageString = Marshal.PtrToStringAnsi(msgPtr);

               var localTime = Time.ConvertUtcTimeToLocalTime(LinphoneAPI.linphone_chat_message_get_time(message));
                var chatMessage = new VATRPChatMessage(MessageContentType.Text)
                {
                    Direction = LinphoneAPI.linphone_chat_message_is_outgoing(message) == 1? MessageDirection.Outgoing : MessageDirection.Incoming,
                    IsIncompleteMessage = false,
                    MessageTime = localTime,
                    Content = messageString,
                    IsRTTMessage = false,
                    IsRead = LinphoneAPI.linphone_chat_message_is_read(message) == 1
                };

                if (OnChatMessageReceivedEvent != null)
                    OnChatMessageReceivedEvent(roomPtr, callChatRoomPtrList, from, chatMessage);
            }
        }
예제 #5
0
        private void OnChatMessageComposing(string remoteUser, IntPtr chatPtr, uint rttCode)
        {
            string dn, un, host;
            int port;
            System.Windows.Threading.Dispatcher dispatcher = null;
            try
            {
                dispatcher = this._serviceManager.Dispatcher;
            }
            catch (NullReferenceException)
            {
                return;
            }
            if (dispatcher != null)
                dispatcher.BeginInvoke((Action) delegate()
                {
                    if (!VATRPCall.ParseSipAddressEx(remoteUser, out dn, out un,
                        out host, out port))
                        un = "";

                    if (!un.NotBlank() )
                        return;
                    var contactAddress = string.Format("{0}@{1}", un, host);
                    var contactID = new ContactID(contactAddress, IntPtr.Zero);

                    VATRPContact contact = FindContact(contactID);

                    if (contact == null)
                    {
                        contact = new VATRPContact(contactID)
                        {
                            DisplayName = dn,
                            Fullname = dn.NotBlank() ? dn : un,
                            RegistrationName = remoteUser,
                            SipUsername = un
                        };
                        _contactSvc.AddContact(contact, "");
                    }

                    VATRPChat chat = GetChat(contact);

                    chat.UnreadMsgCount++;

                    chat.CharsCountInBubble++;
                    var rttCodeArray = new char[2];
                    var rttCodearrayLength = 1;
                    if (chat.CharsCountInBubble == 201)
                    {
                        rttCodeArray[0] = UNLF;
                        try
                        {
                            rttCodeArray[1] = Convert.ToChar(rttCode);
                        }
                        catch (Exception)
                        {

                        }
                        rttCodearrayLength = 2;
                        chat.CharsCountInBubble = 0;
                    }
                    else
                    {
                        try
                        {
                            rttCodeArray[0] = Convert.ToChar(rttCode);
                        }
                        catch
                        {

                        }
                    }

                    for (int i = 0; i < rttCodearrayLength; i++)
                    {
                        VATRPChatMessage message = chat.SearchIncompleteMessage(MessageDirection.Incoming);

                        if (message == null)
                        {
                            message = new VATRPChatMessage(MessageContentType.Text)
                            {
                                Direction = MessageDirection.Incoming,
                                IsIncompleteMessage = true,
                                Chat = chat,
                                IsRTTMarker = false,
                            };

                            chat.AddMessage(message, false);
                        }

                        try
                        {
                            var sb = new StringBuilder(message.Content);
                            switch (rttCodeArray[i])
                            {
                                case UNLF: //
                                case CR:
                                case LF:
                                    message.IsIncompleteMessage = false;
                                    break;
                                case '\b':
                                    if (sb.Length > 0)
                                        sb.Remove(sb.Length - 1, 1);
                                    break;
                                default:
                                    sb.Append(rttCodeArray[i]);
                                    break;
                            }
                            if (message.IsIncompleteMessage)
                                message.Content = sb.ToString();
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error in OnChatMessageComposing: " + ex.Message);
                            message.IsIncompleteMessage = false;
                        }
                        if (string.IsNullOrEmpty(message.Content))
                            chat.DeleteMessage(message);
                        else
                            chat.UpdateLastMessage(false);
                        this.OnConversationUpdated(chat, true);

                        if (this.RttReceived != null)
                        {
                            this.RttReceived(this, EventArgs.Empty);
                        }
                    }
                });
        }
예제 #6
0
        private void LoadMessages(VATRPChat chat, IntPtr chatRoomPtr)
        {
            IntPtr msgListPtr = LinphoneAPI.linphone_chat_room_get_history(chatRoomPtr, 0);
            if (msgListPtr != IntPtr.Zero)
            {
                MSList msMessagePtr;

                var loggedInContact = _contactSvc.FindLoggedInContact();
                if (loggedInContact == null)
                    return;
                do
                {
                    msMessagePtr.next = IntPtr.Zero;
                    msMessagePtr.prev = IntPtr.Zero;
                    msMessagePtr.data = IntPtr.Zero;

                    msMessagePtr = (MSList) Marshal.PtrToStructure(msgListPtr, typeof (MSList));
                    if (msMessagePtr.data != IntPtr.Zero)
                    {
                        IntPtr tmpPtr = LinphoneAPI.linphone_chat_message_get_from_address(msMessagePtr.data);
                        tmpPtr = LinphoneAPI.linphone_address_as_string(tmpPtr);
                        if (tmpPtr != IntPtr.Zero)
                        {
                            var fromParty = LinphoneAPI.PtrToStringUtf8(tmpPtr).TrimSipPrefix();
                            LinphoneAPI.ortp_free(tmpPtr);

                            tmpPtr = LinphoneAPI.linphone_chat_message_get_to_address(msMessagePtr.data);
                            if (tmpPtr != IntPtr.Zero)
                            {
                                tmpPtr = LinphoneAPI.linphone_address_as_string(tmpPtr);
                                var toParty = LinphoneAPI.PtrToStringUtf8(tmpPtr).TrimSipPrefix();
                                LinphoneAPI.ortp_free(tmpPtr);

                                string dn, un, host;
                                int port;

                                if (fromParty == loggedInContact.ID || toParty == loggedInContact.ID)
                                {
                                    bool isOutgoing = false;
                                    string remoteParty = fromParty;
                                    if (fromParty == loggedInContact.ID)
                                    {
                                        isOutgoing = true;
                                        remoteParty = toParty;
                                    }

                                    if (
                                        !VATRPCall.ParseSipAddressEx(remoteParty, out dn, out un,
                                            out host, out port))
                                        un = "";

                                    if (un.NotBlank())
                                    {
                                        var contactAddress = string.Format("{0}@{1}", un, host);
                                        var contactID = new ContactID(contactAddress, IntPtr.Zero);
                                        var contact = FindContact(contactID);

                                        if (contact == null)
                                        {
                                            contact = new VATRPContact(contactID)
                                            {
                                                DisplayName = dn,
                                                Fullname = un,
                                                SipUsername = un,
                                                RegistrationName = contactAddress
                                            };
                                            _contactSvc.AddContact(contact, "");
                                        }

                                        IntPtr msgPtr = LinphoneAPI.linphone_chat_message_get_text(msMessagePtr.data);
                                        var messageString = string.Empty;
                                        if (msgPtr != IntPtr.Zero)
                                            messageString = LinphoneAPI.PtrToStringUtf8(msgPtr);

                                        if (messageString.NotBlank())
                                        {
                                            var localTime =
                                                Time.ConvertUtcTimeToLocalTime(
                                                    LinphoneAPI.linphone_chat_message_get_time(msMessagePtr.data));
                                            var declineMessage = false;
                                            if (messageString.StartsWith(VATRPChatMessage.DECLINE_PREFIX))
                                            {
                                                messageString = messageString.Substring(VATRPChatMessage.DECLINE_PREFIX.Length);
                                                declineMessage = true;
                                            }
                                            var chatMessage = new VATRPChatMessage(MessageContentType.Text)
                                            {
                                                Direction =
                                                    isOutgoing
                                                        ? MessageDirection.Outgoing
                                                        : MessageDirection.Incoming,
                                                IsIncompleteMessage = false,
                                                MessageTime = localTime,
                                                Content = messageString,
                                                IsRTTMessage = false,
                                                IsDeclineMessage = declineMessage,
                                                IsRead = LinphoneAPI.linphone_chat_message_is_read(msMessagePtr.data) == 1,
                                                Status = LinphoneAPI.linphone_chat_message_get_state(msMessagePtr.data),
                                                Chat = chat
                                            };

                                            chat.AddMessage(chatMessage, false);
                                            chat.UpdateLastMessage(false);
                                        }
                                    }
                                }
                            }
                        }
                    }
                    msgListPtr = msMessagePtr.next;
                } while (msMessagePtr.next != IntPtr.Zero);

            }
        }
예제 #7
0
        public bool ComposeAndSendMessage(VATRPChat chat, string text)
        {
            VATRPChat chatID = this.FindChat(chat);
            if ((chatID == null) || (chatID.Contact == null))
            {
                return false;
            }
            var declineMessage = text.StartsWith(VATRPChatMessage.DECLINE_PREFIX);
            var message = new VATRPChatMessage(MessageContentType.Text)
            {
                Direction = MessageDirection.Outgoing,
                Status = LinphoneChatMessageState.LinphoneChatMessageStateIdle,
                MessageTime = DateTime.Now,
                IsIncompleteMessage = false,
                Chat = chatID,
                IsDeclineMessage = declineMessage
            };

            message.Content = declineMessage ? text.Substring(VATRPChatMessage.DECLINE_PREFIX.Length) : text;

            // send message to linphone
            IntPtr msgPtr = IntPtr.Zero;
            _linphoneSvc.SendChatMessage(chat, text, ref msgPtr);
            if (msgPtr != IntPtr.Zero)
                message.MessageTime = Time.ConvertUtcTimeToLocalTime(LinphoneAPI.linphone_chat_message_get_time(msgPtr));

            message.NativePtr = msgPtr;

            chat.AddMessage(message, false);
            chat.UpdateLastMessage(false);

            chat.UnreadMsgCount = 0;
            chat.Contact.UnreadMsgCount = 0;

            OnConversationUnReadStateChanged(chat);
            this.OnConversationUpdated(chat, true);

            return true;
        }
예제 #8
0
        public bool ComposeAndSendMessage(IntPtr callPtr, VATRPChat chat, char key, bool inCompleteMessage)
        {
            VATRPChat chatID = this.FindChat(chat);
            if ((chatID == null) || (chatID.Contact == null))
            {
                return false;
            }

            VATRPContact loggedContact = _contactSvc.FindLoggedInContact();

            var message = chat.SearchIncompleteMessage(MessageDirection.Outgoing);
            if (message == null)
            {
                if (key == '\r')
                    return false;

                message = new VATRPChatMessage(MessageContentType.Text)
                {
                    Direction = MessageDirection.Outgoing,
                    Status = LinphoneChatMessageState.LinphoneChatMessageStateIdle,
                    IsRTTMessage = true,
                    Chat = chatID
                };
                chatID.AddMessage(message, false);
            }
            else
            {
                message.MessageTime = DateTime.Now;
            }

            var rttCode = (uint) key;
            var createBubble = false;
            if (key != '\r')
            {
                var sb = new StringBuilder(message.Content);
                if (key == '\b')
                {
                    if (sb.Length > 0)
                        sb.Remove(sb.Length - 1, 1);
                }
                else
                    sb.Append(Convert.ToChar(rttCode));
                message.Content = sb.ToString();
                chat.UpdateLastMessage(false);
            }
            else
            {
                createBubble = true;
            }

            message.IsIncompleteMessage = !createBubble;

            // send message to linphone
            var chatPtr = chat.NativePtr;
            var msgPtr = message.NativePtr;
            _linphoneSvc.SendChar(rttCode, callPtr, ref chatPtr, ref msgPtr);

            chat.NativePtr = chatPtr;
            message.NativePtr = msgPtr;
            if (message.NativePtr != IntPtr.Zero)
                message.Status = _linphoneSvc.GetMessageStatus(message.NativePtr);

            if (createBubble)
            {
                // delete empty message
                if (!message.Content.NotBlank())
                    chatID.DeleteMessage(message);
            }
            this.OnConversationUpdated(chatID, true);
            return true;
        }
예제 #9
0
        private void OnChatMessageReceived(IntPtr chatPtr, IntPtr callChatPtr, string remoteUser, VATRPChatMessage chatMessage)
        {
            string dn, un, host;
            int port;
            if (callChatPtr == chatPtr /*&& RttEnabled*/)
            {
                return;
            }

            System.Windows.Threading.Dispatcher dispatcher = null;
            try
            {
               dispatcher = this._serviceManager.Dispatcher;
            }
            catch (NullReferenceException)
            {
                return;
            }

            if (dispatcher != null)
                dispatcher.BeginInvoke((Action) delegate()
                {
                    if (!VATRPCall.ParseSipAddressEx(remoteUser, out dn, out un,
                        out host, out port))
                        un = "";

                    if (!un.NotBlank())
                        return;

                    var contactAddress = string.Format("{0}@{1}", un, host);
                    var contactID = new ContactID(contactAddress, IntPtr.Zero);
                    VATRPContact contact = FindContact(contactID);

                    if (contact == null)
                    {
                        contact = new VATRPContact(contactID)
                        {
                            DisplayName = dn,
                            Fullname = dn.NotBlank() ? dn : un,
                            SipUsername = un,
                            RegistrationName = contactAddress
                        };
                        _contactSvc.AddContact(contact, "");
                        Contacts.Add(contact);
                        if (ContactAdded != null)
                            ContactAdded(this, new ContactEventArgs(new ContactID(contact)));
                    }

                    VATRPChat chat = GetChat(contact);
                    chat.NativePtr = chatPtr;

                    if (chat.CheckMessage(chatMessage))
                    {
                        chat.UnreadMsgCount++;
                        if (!chat.IsSelected)
                            contact.UnreadMsgCount++;
                        chatMessage.IsRTTMessage = false;
                        chatMessage.IsIncompleteMessage = false;
                        chatMessage.Chat = chat;
                        if (chatMessage.Content.StartsWith(VATRPChatMessage.DECLINE_PREFIX))
                        {
                            chatMessage.Content = chatMessage.Content.Substring(VATRPChatMessage.DECLINE_PREFIX.Length);

                            chatMessage.IsDeclineMessage = true;
                            if (ConversationDeclineMessageReceived != null)
                            {
                                var declineArgs = new DeclineMessageArgs(chatMessage.Content) { Sender = contact };
                                ConversationDeclineMessageReceived(this, declineArgs);
                            }
                        }

                        chat.AddMessage(chatMessage, false);
                        chat.UpdateLastMessage(false);

                        OnConversationUnReadStateChanged(chat);
                        this.OnConversationUpdated(chat, true);
                    }
                });
        }
예제 #10
0
        private void OnChatMessageComposing(MessageComposingEventArgs args)
        {
            if (args == null)
                return;
            System.Windows.Threading.Dispatcher dispatcher = null;
            try
            {
                dispatcher = this._serviceManager.Dispatcher;
            }
            catch (NullReferenceException)
            {
                return;
            }
            if (dispatcher != null)
                dispatcher.BeginInvoke((Action) delegate()
                {
                    VATRPChat chat = FindRTTChat(args.ChatRoomPtr);

                    if (chat == null)
                    {
                        LOG.WarnFormat("RTT Chat not found Ptr: {0} ", args.ChatRoomPtr);
                        return;
                    }

                    chat.CharsCountInBubble++;
                    var rttCodeArray = new char[2];
                    var rttCodearrayLength = 1;
                    if (chat.CharsCountInBubble == 201)
                    {
                        rttCodeArray[0] = UNLF;
                        try
                        {
                            rttCodeArray[1] = Convert.ToChar(args.RTTCode);
                        }
                        catch (Exception)
                        {

                        }
                        rttCodearrayLength = 2;
                        chat.CharsCountInBubble = 0;
                    }
                    else
                    {
                        try
                        {
                            rttCodeArray[0] = Convert.ToChar(args.RTTCode);
                        }
                        catch
                        {

                        }
                    }

                    for (int i = 0; i < rttCodearrayLength; i++)
                    {
                        VATRPChatMessage message = chat.SearchIncompleteMessage(MessageDirection.Incoming);

                        if (message == null)
                        {
                            message = new VATRPChatMessage(MessageContentType.Text)
                            {
                                Direction = MessageDirection.Incoming,
                                IsIncompleteMessage = true,
                                Chat = chat,
                                IsRTTMarker = false,
                                IsRTTMessage = true
                            };

                            chat.AddMessage(message, false);
                        }

                        try
                        {
                            var sb = new StringBuilder(message.Content);
                            switch (rttCodeArray[i])
                            {
                                case UNLF: //
                                case CR:
                                case LF:
                                    message.IsIncompleteMessage = false;
                                    break;
                                case '\b':
                                    if (sb.Length > 0)
                                        sb.Remove(sb.Length - 1, 1);
                                    break;
                                default:
                                    sb.Append(rttCodeArray[i]);
                                    break;
                            }
                            if (message.IsIncompleteMessage)
                                message.Content = sb.ToString();
                        }
                        catch (Exception ex)
                        {
                            Debug.WriteLine("Error in OnChatMessageComposing: " + ex.Message);
                            message.IsIncompleteMessage = false;
                        }
                        if (string.IsNullOrEmpty(message.Content))
                            chat.DeleteMessage(message);
                        else
                            chat.UpdateLastMessage(false);
                        this.OnConversationUpdated(chat, true);

                        if (this.RttReceived != null)
                        {
                            this.RttReceived(chat.CallPtr, EventArgs.Empty);
                        }
                    }
                });
        }
예제 #11
0
 private void OnChatMessageReceived(IntPtr chatPtr, List<IntPtr> callChatPtrList, string remoteUser,
     VATRPChatMessage chatMessage)
 {
     var args = new ChatMessageEventArgs(remoteUser, chatPtr, callChatPtrList, chatMessage);
     EnqueueReceivedMsg(args);
 }