<commit_before>#include "sipfieldcomposing.h"

#include "sipconversions.h"

#include "common.h"

#include <QDebug>


// a function used within this file to add a parameter
bool tryAddParameter(std::shared_ptr<QList<SIPParameter> > &parameters,
                     QString parameterName, QString parameterValue);
bool tryAddParameter(std::shared_ptr<QList<SIPParameter> > &parameters,
                     QString parameterName);

QString composeUritype(ConnectionType type);

QString composeUritype(ConnectionType type)
{
  if (type == TCP)
  {
    return "sip:";
  }
  else if (type == TLS)
  {
    return "sips:";
  }
  else if (type == TEL)
  {
    return "tel:";
  }
  else
  {
    qDebug() << "ERROR:  Unset connection type detected while composing SIP message";
  }

  return "";
}

QString composePortString(uint16_t port)
{
  QString portString = "";

  if (port != 0)
  {
    portString = ":" + QString::number(port);
  }
  return portString;
}

bool composeSIPUri(SIP_URI& uri, QStringList& words)
{
  if (uri.realname != "")
  {
    words.push_back(uri.realname);
  }

  QString uriString = "<" + composeUritype(uri.connectionType);
  if (uriString != "")
  {
    QString parameters = "";

    for (auto& parameter : uri.parameters)
    {
      parameters += ";" + parameter.name;
      if (parameter.value != "")
      {
        parameters += "=" + parameter.value;
      }
    }

    uriString += uri.username + "@" + uri.host + composePortString(uri.port) + parameters + ">";

    words.push_back(uriString);

    return true;
  }
  return false;
}


bool getFirstRequestLine(QString& line, SIPRequest& request, QString lineEnding)
{
  if(request.requestURI.host == "")
  {
    printDebug(DEBUG_PROGRAM_ERROR, "SIPComposing", DC_SEND_SIP_REQUEST,
               "Request URI host is empty when comprising the first line.");
  }

  if(request.type == SIP_NO_REQUEST)
  {
    printDebug(DEBUG_PROGRAM_ERROR, "SIPComposing",
               DC_SEND_SIP_REQUEST, "SIP_NO_REQUEST given.");
    return false;
  }

  QString type = "";
  QString target = "";

  if(request.type != SIP_REGISTER)
  {
    type = composeUritype(request.message->to.connectionType);
    target = request.message->to.username + "@" + request.message->to.host;
  }
  else // REGISTER first line does not contain username.
  {
    type = composeUritype(request.requestURI.connectionType);
    target = request.requestURI.host;
  }

  line = requestToString(request.type) + " " + type
      + target + " SIP/" + request.message->version + lineEnding;

  return true;
}

bool getFirstResponseLine(QString& line, SIPResponse& response,
                          QString lineEnding)
{
  if(response.type == SIP_UNKNOWN_RESPONSE)
  {
    qDebug() << "WARNING: First response line failed";
    return false;
  }
  line = "SIP/" + response.message->version + " "
      + QString::number(responseToCode(response.type)) + " "
      + responseToPhrase(response.type) + lineEnding;
  return true;
}

bool includeToField(QList<SIPField> &fields,
                    std::shared_ptr<SIPMessageInfo> message)
{
  Q_ASSERT(message->to.username != "" && message->to.host != "");
  if(message->to.username == "" ||  message->to.host == "")
  {
    qDebug() << "WARNING: Composing To-field failed because host is:"<commit_msg>feature(Transport): Compose port into requestURI.<commit_after>#include "sipfieldcomposing.h"

#include "sipconversions.h"

#include "common.h"

#include <QDebug>


// a function used within this file to add a parameter
bool tryAddParameter(std::shared_ptr<QList<SIPParameter> > &parameters,
                     QString parameterName, QString parameterValue);
bool tryAddParameter(std::shared_ptr<QList<SIPParameter> > &parameters,
                     QString parameterName);

QString composeUritype(ConnectionType type);

QString composeUritype(ConnectionType type)
{
  if (type == TCP)
  {
    return "sip:";
  }
  else if (type == TLS)
  {
    return "sips:";
  }
  else if (type == TEL)
  {
    return "tel:";
  }
  else
  {
    qDebug() << "ERROR:  Unset connection type detected while composing SIP message";
  }

  return "";
}

QString composePortString(uint16_t port)
{
  QString portString = "";

  if (port != 0)
  {
    portString = ":" + QString::number(port);
  }
  return portString;
}

bool composeSIPUri(SIP_URI& uri, QStringList& words)
{
  if (uri.realname != "")
  {
    words.push_back(uri.realname);
  }

  QString uriString = "<" + composeUritype(uri.connectionType);
  if (uriString != "")
  {
    QString parameters = "";

    for (auto& parameter : uri.parameters)
    {
      parameters += ";" + parameter.name;
      if (parameter.value != "")
      {
        parameters += "=" + parameter.value;
      }
    }

    uriString += uri.username + "@" + uri.host + composePortString(uri.port) + parameters + ">";

    words.push_back(uriString);

    return true;
  }
  return false;
}


bool getFirstRequestLine(QString& line, SIPRequest& request, QString lineEnding)
{
  if(request.requestURI.host == "")
  {
    printDebug(DEBUG_PROGRAM_ERROR, "SIPComposing", DC_SEND_SIP_REQUEST,
               "Request URI host is empty when comprising the first line.");
  }

  if(request.type == SIP_NO_REQUEST)
  {
    printDebug(DEBUG_PROGRAM_ERROR, "SIPComposing",
               DC_SEND_SIP_REQUEST, "SIP_NO_REQUEST given.");
    return false;
  }

  QString type = "";
  QString target = "";
  QString port = "";

  if (request.requestURI.port != 0)
  {
    port = ":" + QString::number(request.requestURI.port);
  }


  if(request.type != SIP_REGISTER)
  {
    type = composeUritype(request.message->to.connectionType);
    target = request.message->to.username + "@" + request.message->to.host;
  }
  else // REGISTER first line does not contain username.
  {
    type = composeUritype(request.requestURI.connectionType);
    target = request.requestURI.host;
  }

  line = requestToString(request.type) + " " + type
      + target + port + " SIP/" + request.message->version + lineEnding;

  return true;
}

bool getFirstResponseLine(QString& line, SIPResponse& response,
                          QString lineEnding)
{
  if(response.type == SIP_UNKNOWN_RESPONSE)
  {
    qDebug() << "WARNING: First response line failed";
    return false;
  }
  line = "SIP/" + response.message->version + " "
      + QString::number(responseToCode(response.type)) + " "
      + responseToPhrase(response.type) + lineEnding;
  return true;
}

bool includeToField(QList<SIPField> &fields,
                    std::shared_ptr<SIPMessageInfo> message)
{
  Q_ASSERT(message->to.username != "" && message->to.host != "");
  if(message->to.username == "" ||  message->to.host == "")
  {
    qDebug() << "WARNING: Composing To-field failed because host is:"
{'fraction_non_alphanumeric': 0.13731299066804917, 'fraction_numerical': 0.0010368834246778254, 'mean_word_length': 3.16533004318322, 'pattern_counts': {'":': 3, '<': 31, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3825034', 'n_tokens_mistral': 2242, 'n_tokens_neox': 2181, 'n_words': 443}