From b5ceece8d52ba0ee5b9517e02ea55111b80f295a Mon Sep 17 00:00:00 2001
From: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
Date: Fri, 17 Sep 2010 14:56:09 -0400
Subject: [PATCH] [#4071] Send IAX text message using InstantMessaging Module

---
 sflphone-common/src/iax/iaxvoiplink.cpp     |  6 +--
 sflphone-common/src/im/InstantMessaging.cpp | 41 +++++++++++++++++++--
 sflphone-common/src/im/InstantMessaging.h   |  8 +++-
 3 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/sflphone-common/src/iax/iaxvoiplink.cpp b/sflphone-common/src/iax/iaxvoiplink.cpp
index 265cec93b8..eee6c4c658 100644
--- a/sflphone-common/src/iax/iaxvoiplink.cpp
+++ b/sflphone-common/src/iax/iaxvoiplink.cpp
@@ -711,9 +711,9 @@ IAXVoIPLink::sendTextMessage (sfl::InstantMessaging *module, const std::string&
     // Must active the mutex for this session
     _mutexIAX.enterMutex();
 
-    // module->send_iax_message(call->getSession(), message.c_str());
+    module->send_iax_message (call->getSession(), callID, message.c_str());
 
-    iax_send_text (call->getSession(), message.c_str());
+    // iax_send_text (call->getSession(), message.c_str());
     _mutexIAX.leaveMutex();
 
     return true;
@@ -908,7 +908,7 @@ IAXVoIPLink::iaxHandleCallEvent (iax_event* event, IAXCall* call)
             break;
 
         case IAX_EVENT_TEXT:
-            Manager::instance ().incomingMessage (call->getCallId (), std::string ("ME"), std::string ( (const char*) event->data));
+            Manager::instance ().incomingMessage (call->getCallId (), call->getPeerNumber(), std::string ( (const char*) event->data));
             break;
 
         case IAX_EVENT_RINGA:
diff --git a/sflphone-common/src/im/InstantMessaging.cpp b/sflphone-common/src/im/InstantMessaging.cpp
index 1b22d01e3a..2896dfc69e 100644
--- a/sflphone-common/src/im/InstantMessaging.cpp
+++ b/sflphone-common/src/im/InstantMessaging.cpp
@@ -127,7 +127,7 @@ pj_status_t InstantMessaging::notify (CallID& id)
     return PJ_SUCCESS;
 }
 
-pj_status_t InstantMessaging::send (pjsip_inv_session *session, CallID& id, const std::string& text)
+pj_status_t InstantMessaging::sip_send (pjsip_inv_session *session, CallID& id, const std::string& text)
 {
 
     pjsip_method msg_method;
@@ -195,7 +195,7 @@ pj_status_t InstantMessaging::send_sip_message (pjsip_inv_session *session, Call
     /* Check the length of the message */
     if (message.length() < getMessageMaximumSize()) {
         /* No problem here */
-        send (session, id, message);
+        sip_send (session, id, message);
     }
 
     else {
@@ -207,13 +207,48 @@ pj_status_t InstantMessaging::send_sip_message (pjsip_inv_session *session, Call
 
         // Maximum is above 1500 character
         // TODO: Send every messages
-        send (session, id, multiple_messages[i]);
+        sip_send (session, id, multiple_messages[i]);
     }
 
     return PJ_SUCCESS;
 }
 
 
+bool InstantMessaging::iax_send (iax_session* session, const CallID& id, const std::string& message)
+{
+    if (iax_send_text (session, message.c_str()) != -1)
+        return true;
+    else
+        return false;
+
+
+}
+
+bool InstantMessaging::send_iax_message (iax_session* session, const CallID& id, const std::string& message)
+{
+
+    bool ret;
+
+    /* Check the length of the message */
+    if (message.length() < getMessageMaximumSize()) {
+        /* No problem here */
+        ret = iax_send (session, id, message);
+    }
+
+    else {
+        /* It exceeds the size limit of a SIP MESSAGE (1300 bytes), o plit it and send multiple messages */
+        std::vector<std::string> multiple_messages = split_message (message);
+        /* Send multiple messages */
+        // int size = multiple_messages.size();
+        int i = 0;
+
+        // Maximum is above 1500 character
+        // TODO: Send every messages
+        ret = iax_send (session, id, multiple_messages[i]);
+    }
+}
+
+
 std::vector<std::string> InstantMessaging::split_message (const std::string& text)
 {
 
diff --git a/sflphone-common/src/im/InstantMessaging.h b/sflphone-common/src/im/InstantMessaging.h
index b87fa402cb..2a87e3ea3e 100644
--- a/sflphone-common/src/im/InstantMessaging.h
+++ b/sflphone-common/src/im/InstantMessaging.h
@@ -16,6 +16,8 @@
 #include <list>
 #include <exception>
 
+#include <iax-client.h>
+
 #define EMPTY_MESSAGE   pj_str((char*)"")
 #define STR_TEXT        pj_str((char*)"text")
 #define STR_PLAIN       pj_str((char*)"plain")
@@ -129,10 +131,14 @@ class InstantMessaging
          * @return pj_status_t  0 on success
          *                      1 otherwise
          */
-        pj_status_t send (pjsip_inv_session*, CallID& id, const std::string&);
+        pj_status_t sip_send (pjsip_inv_session*, CallID& id, const std::string&);
 
         pj_status_t send_sip_message (pjsip_inv_session*, CallID& id, const std::string&);
 
+        bool iax_send (iax_session* session, const CallID& id, const std::string& message);
+
+        bool send_iax_message (iax_session *session, const CallID& id, const std::string&);
+
         std::vector<std::string> split_message (const std::string&);
 
 
-- 
GitLab