LCOV - code coverage report
Current view: top level - foo/src/im - instant_messaging.cpp (source / functions) Hit Total Coverage
Test: jami-coverage-filtered.info Lines: 43 92 46.7 %
Date: 2025-12-18 10:07:43 Functions: 5 9 55.6 %

          Line data    Source code
       1             : /*
       2             :  *  Copyright (C) 2004-2025 Savoir-faire Linux Inc.
       3             :  *
       4             :  *  This program is free software: you can redistribute it and/or modify
       5             :  *  it under the terms of the GNU General Public License as published by
       6             :  *  the Free Software Foundation, either version 3 of the License, or
       7             :  *  (at your option) any later version.
       8             :  *
       9             :  *  This program is distributed in the hope that it will be useful,
      10             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
      12             :  *  GNU General Public License for more details.
      13             :  *
      14             :  *  You should have received a copy of the GNU General Public License
      15             :  *  along with this program. If not, see <https://www.gnu.org/licenses/>.
      16             :  */
      17             : #include "instant_messaging.h"
      18             : 
      19             : #include "logger.h"
      20             : #include "connectivity/sip_utils.h"
      21             : 
      22             : #include <pjsip_ua.h>
      23             : #include <pjsip.h>
      24             : 
      25             : namespace jami {
      26             : 
      27             : using sip_utils::CONST_PJ_STR;
      28             : 
      29             : /**
      30             :  * the pair<string, string> we receive is expected to be in the format <MIME type, payload>
      31             :  * the MIME type is in the format "type/subtype"
      32             :  * in the header it will be presented as "Content-Type: type/subtype"
      33             :  * following the RFC spec, this header line can also contain other parameters in the format:
      34             :  *     Content-Type: type/subtype; arg=value; arg=value; …
      35             :  * thus we also accept the key of the map to be in such a format:
      36             :  *     type/subtype; arg=value; arg=value; …
      37             :  */
      38             : static void
      39         234 : createMessageBody(pj_pool_t* pool, const std::pair<std::string, std::string>& payload, pjsip_msg_body** body_p)
      40             : {
      41             :     /* parse the key:
      42             :      * 1. split by ';'
      43             :      * 2. parse the first result by spliting by '/' into a type and subtype
      44             :      * 3. parse any following strings into arg=value by splitting by '='
      45             :      */
      46         234 :     std::string_view mimeType, parameters;
      47         234 :     auto sep = payload.first.find(';');
      48         234 :     if (std::string::npos == sep) {
      49         234 :         mimeType = payload.first;
      50             :     } else {
      51           0 :         mimeType = std::string_view(payload.first).substr(0, sep);
      52           0 :         parameters = std::string_view(payload.first).substr(sep + 1);
      53             :     }
      54             : 
      55             :     // split MIME type to type and subtype
      56         234 :     sep = mimeType.find('/');
      57         234 :     if (std::string::npos == sep) {
      58           0 :         JAMI_WARNING("Bad MIME type: '{}'", mimeType);
      59           0 :         throw im::InstantMessageException("Invalid MIME type");
      60             :     }
      61             : 
      62         234 :     auto type = sip_utils::CONST_PJ_STR(mimeType.substr(0, sep));
      63         234 :     auto subtype = sip_utils::CONST_PJ_STR(mimeType.substr(sep + 1));
      64         234 :     auto message = sip_utils::CONST_PJ_STR(payload.second);
      65             : 
      66             :     // create part
      67         234 :     *body_p = pjsip_msg_body_create(pool, &type, &subtype, &message);
      68             : 
      69         234 :     if (not parameters.size())
      70         234 :         return;
      71             : 
      72             :     // now attempt to add parameters one by one
      73             :     do {
      74           0 :         sep = parameters.find(';');
      75           0 :         auto paramPair = parameters.substr(0, sep);
      76           0 :         if (paramPair.empty())
      77           0 :             break;
      78             : 
      79             :         // split paramPair into arg and value by '='
      80           0 :         auto paramSplit = paramPair.find('=');
      81           0 :         if (std::string::npos == paramSplit) {
      82           0 :             JAMI_WARNING("Bad parameter: '{}'", paramPair);
      83           0 :             throw im::InstantMessageException("Invalid parameter");
      84             :         }
      85             : 
      86           0 :         auto arg = sip_utils::CONST_PJ_STR(paramPair.substr(0, paramSplit));
      87           0 :         auto value = sip_utils::CONST_PJ_STR(paramPair.substr(paramSplit + 1));
      88           0 :         pj_strtrim(&arg);
      89           0 :         pj_strtrim(&value);
      90             :         pj_str_t arg_pj, value_pj;
      91           0 :         pjsip_param* param = PJ_POOL_ALLOC_T(pool, pjsip_param);
      92           0 :         param->name = *pj_strdup(pool, &arg_pj, &arg);
      93           0 :         param->value = *pj_strdup(pool, &value_pj, &value);
      94           0 :         pj_list_push_back(&(*body_p)->content_type.param, param);
      95             : 
      96             :         // next parameter?
      97           0 :         if (std::string::npos != sep)
      98           0 :             parameters = parameters.substr(sep + 1);
      99           0 :     } while (std::string::npos != sep);
     100             : }
     101             : 
     102             : void
     103         234 : im::fillPJSIPMessageBody(pjsip_tx_data& tdata, const std::map<std::string, std::string>& payloads)
     104             : {
     105             :     // multi-part body?
     106         234 :     if (payloads.size() == 1) {
     107         234 :         createMessageBody(tdata.pool, *payloads.begin(), &tdata.msg->body);
     108         234 :         return;
     109             :     }
     110             : 
     111             :     /* if Ctype is not specified "multipart/mixed" will be used
     112             :      * if the boundary is not specified, a random one will be generateAudioPort
     113             :      * FIXME: generate boundary and check that none of the message parts contain it before
     114             :      *        calling this function; however the probability of this happenings if quite low as
     115             :      *        the randomly generated string is fairly long
     116             :      */
     117           0 :     tdata.msg->body = pjsip_multipart_create(tdata.pool, nullptr, nullptr);
     118             : 
     119           0 :     for (const auto& pair : payloads) {
     120           0 :         auto part = pjsip_multipart_create_part(tdata.pool);
     121           0 :         if (not part) {
     122           0 :             JAMI_ERR("pjsip_multipart_create_part failed: not enough memory");
     123           0 :             throw InstantMessageException("Internal SIP error");
     124             :         }
     125             : 
     126           0 :         createMessageBody(tdata.pool, pair, &part->body);
     127             : 
     128           0 :         auto status = pjsip_multipart_add_part(tdata.pool, tdata.msg->body, part);
     129           0 :         if (status != PJ_SUCCESS) {
     130           0 :             JAMI_ERR("pjsip_multipart_add_part failed: %s", sip_utils::sip_strerror(status).c_str());
     131           0 :             throw InstantMessageException("Internal SIP error");
     132             :         }
     133             :     }
     134             : }
     135             : 
     136             : void
     137         230 : im::sendSipMessage(pjsip_inv_session* session, const std::map<std::string, std::string>& payloads)
     138             : {
     139         230 :     if (payloads.empty()) {
     140           0 :         JAMI_WARN("The payloads argument is empty; ignoring message");
     141           0 :         return;
     142             :     }
     143             : 
     144         231 :     constexpr pjsip_method msg_method = {PJSIP_OTHER_METHOD, CONST_PJ_STR(sip_utils::SIP_METHODS::MESSAGE)};
     145             : 
     146             :     {
     147         231 :         auto dialog = session->dlg;
     148         231 :         sip_utils::PJDialogLock dialog_lock {dialog};
     149             : 
     150         234 :         pjsip_tx_data* tdata = nullptr;
     151         234 :         auto status = pjsip_dlg_create_request(dialog, &msg_method, -1, &tdata);
     152         234 :         if (status != PJ_SUCCESS) {
     153           0 :             JAMI_ERR("pjsip_dlg_create_request failed: %s", sip_utils::sip_strerror(status).c_str());
     154           0 :             throw InstantMessageException("Internal SIP error");
     155             :         }
     156             : 
     157         234 :         fillPJSIPMessageBody(*tdata, payloads);
     158             : 
     159         234 :         status = pjsip_dlg_send_request(dialog, tdata, -1, nullptr);
     160         234 :         if (status != PJ_SUCCESS) {
     161           0 :             JAMI_ERR("pjsip_dlg_send_request failed: %s", sip_utils::sip_strerror(status).c_str());
     162           0 :             throw InstantMessageException("Internal SIP error");
     163             :         }
     164         234 :     }
     165             : }
     166             : 
     167             : /**
     168             :  * Creates std::pair with the Content-Type header contents as the first value and the message
     169             :  * payload as the second value.
     170             :  *
     171             :  * The format of the first value will be:
     172             :  *     type/subtype[; *[; arg=value]]
     173             :  *     eg: "text/plain;id=1234;part=2;of=1001"
     174             :  */
     175             : static std::pair<std::string, std::string>
     176         228 : parseMessageBody(const pjsip_msg_body* body)
     177             : {
     178         456 :     std::string header = sip_utils::as_view(body->content_type.type) + "/"
     179         456 :                          + sip_utils::as_view(body->content_type.subtype);
     180             : 
     181             :     // iterate over parameters
     182         228 :     auto param = body->content_type.param.next;
     183         228 :     while (param != &body->content_type.param) {
     184           0 :         header += ";" + sip_utils::as_view(param->name) + "=" + sip_utils::as_view(param->value);
     185           0 :         param = param->next;
     186             :     }
     187             : 
     188             :     // get the payload, assume we can interpret it as chars
     189         456 :     return {std::move(header), std::string(static_cast<char*>(body->data), (size_t) body->len)};
     190         228 : }
     191             : 
     192             : /**
     193             :  * Parses given SIP message into a map where the key is the contents of the Content-Type header
     194             :  * (along with any parameters) and the value is the message payload.
     195             :  *
     196             :  * @param msg received SIP message
     197             :  *
     198             :  * @return map of content types and message payloads
     199             :  */
     200             : std::map<std::string, std::string>
     201         228 : im::parseSipMessage(const pjsip_msg* msg)
     202             : {
     203         228 :     std::map<std::string, std::string> ret;
     204             : 
     205         228 :     if (!msg->body) {
     206           0 :         JAMI_WARN("Message body is empty");
     207           0 :         return ret;
     208             :     }
     209             : 
     210             :     // check if its a multipart message
     211         228 :     constexpr pj_str_t typeMultipart {CONST_PJ_STR("multipart")};
     212             : 
     213         228 :     if (pj_strcmp(&typeMultipart, &msg->body->content_type.type) != 0) {
     214             :         // treat as single content type message
     215         228 :         ret.emplace(parseMessageBody(msg->body));
     216             :     } else {
     217             :         /* multipart type message, we will treat it as multipart/mixed even if the subtype is
     218             :          * something else, eg: related
     219             :          */
     220           0 :         auto part = pjsip_multipart_get_first_part(msg->body);
     221           0 :         while (part != nullptr) {
     222           0 :             ret.emplace(parseMessageBody(part->body));
     223           0 :             part = pjsip_multipart_get_next_part(msg->body, part);
     224             :         }
     225             :     }
     226             : 
     227         228 :     return ret;
     228           0 : }
     229             : 
     230             : } // namespace jami

Generated by: LCOV version 1.14