<commit_before># routing-trunkside.cfg -- routing toward trunkside, with digits manipulation
# Copyright (C) 2009  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

route[try-trunk-side]
{
  # Now routing towards trunk-side.
  xlog("L_DBG","DEBUG -- route(invite-handler) Now routing towards trunk-side");

  # We do not allow trunk-to-trunk calls.
  if( $(avp(src_type)) == "PSTN" )
  {
      xlog("L_INFO","INFO -- route(invite-handler): Trunk-to-trunk calls are not allowed.");
      # Failure (was 403)
      sl_send_reply("404", "Trunk-to-trunk calls are not allowed");
      exit;
  }

  # ASSERT( $(avp(src_type)) == "ONNET" || $(avp(src_type)) == "ROUTE" || $(avp(src_type)) == "BYPASS" )
  $var(one_fU) = $fU;

  # NANPA-specific
  if(${NANPA})
  {
      # Full NANP destination: routed
      if( $rU =~ "^1[2-9][0-9][0-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9]$" )
      {
          xlog("L_DBG","DEBUG -- route(invite-handler) Routing NANP number");

          $var(NPA) = $(rU{s.substr,1,3});
          $var(NXX) = $(rU{s.substr,4,3});
          $var(NPANXX) = $(rU{s.substr,1,6});

          # Destination classification

          # Local NPANXX
          if( avp_db_load("$var(NPANXX)","$(avp(local_npanxx))") )
          {
              $(avp(dst_type)) = "LOCAL";
          }
          else
          # Must be a long distance call, then
          {
              $(avp(dst_type)) = "NATIONAL";
          }

          # Override: Premium NPAs, e.g. 900
          if( avp_db_load("$var(NPA)","$(avp(premium_npa))") )
          {
              $(avp(dst_type)) = "PREMIUM";
          }

          # Override: Premium NXX, e.g. 555
          if( avp_db_load("$var(NXX)","$(avp(premium_nxx))") )
          {
              $(avp(dst_type)) = "PREMIUM";
          }
      }
      # Used to do "magic matching" on the caller-id
      $var(one_fU) = "1" + $fU;
  }
  #/NANPA

  if(${FR})
  {
      # XXX Need classification
      if( $rU =~ "^33" )
      {
        $(avp(dst_type)) = "NATIONAL";
      }
      # Used to do "magic matching" on the caller-id
      $var(one_fU) = $fU;
  }

  # Now that classification is done, proceed with authorization.
  if( $(avp(src_type)) != "BYPASS" )
  {
      if( $(avp(dst_type)) == "PREMIUM" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_premium))"))
      {
          sl_send_reply("403", "Premium call not allowed");
          exit;
      }

      if( $(avp(dst_type)) == "NATIONAL" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_ld))"))
      {
          sl_send_reply("403", "Long-distance call not allowed");
          exit;
      }

      if( $(avp(dst_type)) == "LOCAL" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_local))"))
      {
          sl_send_reply("403", "Local call not allowed");
          exit;
      }

      if($(avp(dst_type)) == "INTL" && !avp_db_load("$(avp(src_subs))","$(avp(allow_intl))"))
      {
        sl_send_reply("403", "International call not allowed");
        exit;
      }
  }

  # At this point the call is authorized.

  # See if a $fU-based route exists and if so, use R${outbound_route} as the target.
  $var(one_fU) = "1" + $fU;
  xlog("L_DBG","DEBUG -- route(invite-handler): Attempting outbound_route for $fU or $var(one_fU)");
  if( avp_db_load("$fU","$avp(outbound_route)") || avp_db_load("$var(one_fU)","$avp(outbound_route)") )
  {
      xlog("L_DBG","DEBUG -- route(invite-handler): outbound_route $avp(outbound_route) was selected");
      $(avp(gwadv)) = "R" + $(avp(outbound_route)) + "A";
      if( !route(trunk-side-invite) )
      {
          sl_send_reply("504", "Could not route calling number");
          exit;
      }
      route(update-media-proxy);   # Start MP if needed
      route(initial-forwarder);   # Forward
      return;
  }

  ## XXX Starting here is the piece that will need to be replaced with DROUTING ##

  # International number: routed
  if($(avp(dst_type)) == "INTL")
  {
      # Extract the first 6 digits+"A" of the destination number.
      $(avp(gwadv)) = "+" + $(rU{s.substr,0,6}) + "A";
      if( ! route(trunk-side-invite) )
      {
          # Extract the first 3 digits+"A" of the destination number.
          $(avp(gwadv)) = "+" + $(rU{s.substr,0,3}) + "A";

          if( ! route(trunk-side-invite) )
          {
              $(avp(gwadv)) = "+A";

              if( ! route(trunk-side-invite) )
              {
                  sl_send_reply("504", "Invalid international destination -- missing routing information");
                  exit;
              }
          }
      }
      route(update-media-proxy);   # Start MP if needed
      route(iniial-forwarder);   # Forward
      return;
  }
  # /International

  # NANPA-specific
  if(${NANPA})
  {
          # Extract the NPANXXA from the destination number.
          $(avp(gwadv)) = $(rU{s.substr,1,6}) + "A";

          if( ! route(trunk-side-invite) )
          {
              # Extract the NPAA from the destination number.
              $(avp(gwadv)) = $(rU{s.substr,1,3}) + "A";

              if( ! route(trunk-side-invite) )
              {
                  $(avp(gwadv)) = "A";

                  if( ! route(trunk-side-invite) )
                  {
                      sl_send_reply("504", "Invalid national destination -- missing routing information");
                      return;
                  }
              }
          }
          route(update-media-proxy);   # Start MP if needed
          route(initial-forwarder);   # Forward
          return;
      }
  }
  # /NANPA-specific

  if(${FR})
  {
    xlog("L_DBG","DEBUG -- route(try-trunk-side) FR");
  }

}<commit_msg>Bugfixes in trunk-side OpenSIPS code<commit_after># routing-trunkside.cfg -- routing toward trunkside, with digits manipulation
# Copyright (C) 2009  <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

route[try-trunk-side]
{
  # Now routing towards trunk-side.
  xlog("L_DBG","DEBUG -- route(invite-handler) Now routing towards trunk-side");

  # We do not allow trunk-to-trunk calls.
  if( $(avp(src_type)) == "PSTN" )
  {
      xlog("L_INFO","INFO -- route(invite-handler): Trunk-to-trunk calls are not allowed.");
      # Failure (was 403)
      sl_send_reply("404", "Trunk-to-trunk calls are not allowed");
      exit;
  }

  # ASSERT( $(avp(src_type)) == "ONNET" || $(avp(src_type)) == "ROUTE" || $(avp(src_type)) == "BYPASS" )
  $var(one_fU) = $fU;

  # NANPA-specific
  if(${NANPA})
  {
      # Full NANP destination: routed
      if( $rU =~ "^1[2-9][0-9][0-9][2-9][0-9][0-9][0-9][0-9][0-9][0-9]$" )
      {
          xlog("L_DBG","DEBUG -- route(invite-handler) Routing NANP number");

          $var(NPA) = $(rU{s.substr,1,3});
          $var(NXX) = $(rU{s.substr,4,3});
          $var(NPANXX) = $(rU{s.substr,1,6});

          # Destination classification

          # Local NPANXX
          if( avp_db_load("$var(NPANXX)","$(avp(local_npanxx))") )
          {
              $(avp(dst_type)) = "LOCAL";
          }
          else
          # Must be a long distance call, then
          {
              $(avp(dst_type)) = "NATIONAL";
          }

          # Override: Premium NPAs, e.g. 900
          if( avp_db_load("$var(NPA)","$(avp(premium_npa))") )
          {
              $(avp(dst_type)) = "PREMIUM";
          }

          # Override: Premium NXX, e.g. 555
          if( avp_db_load("$var(NXX)","$(avp(premium_nxx))") )
          {
              $(avp(dst_type)) = "PREMIUM";
          }
      }
      # Used to do "magic matching" on the caller-id
      $var(one_fU) = "1" + $fU;
  }
  #/NANPA

  if(${FR})
  {
      # XXX Need classification
      if( $rU =~ "^33" )
      {
        $(avp(dst_type)) = "NATIONAL";
      }
      # Used to do "magic matching" on the caller-id
      $var(one_fU) = $fU;
  }

  # Now that classification is done, proceed with authorization.
  if( $(avp(src_type)) != "BYPASS" )
  {
      if( $(avp(dst_type)) == "PREMIUM" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_premium))"))
      {
          sl_send_reply("403", "Premium call not allowed");
          exit;
      }

      if( $(avp(dst_type)) == "NATIONAL" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_ld))"))
      {
          sl_send_reply("403", "Long-distance call not allowed");
          exit;
      }

      if( $(avp(dst_type)) == "LOCAL" && ! avp_db_load("$(avp(src_subs))","$(avp(allow_local))"))
      {
          sl_send_reply("403", "Local call not allowed");
          exit;
      }

      if($(avp(dst_type)) == "INTL" && !avp_db_load("$(avp(src_subs))","$(avp(allow_intl))"))
      {
        sl_send_reply("403", "International call not allowed");
        exit;
      }
  }

  # At this point the call is authorized.

  # See if a $fU-based route exists and if so, use R${outbound_route} as the target.
  $var(one_fU) = "1" + $fU;
  xlog("L_DBG","DEBUG -- route(invite-handler): Attempting outbound_route for $fU or $var(one_fU)");
  if( avp_db_load("$fU","$avp(outbound_route)") || avp_db_load("$var(one_fU)","$avp(outbound_route)") )
  {
      xlog("L_DBG","DEBUG -- route(invite-handler): outbound_route $avp(outbound_route) was selected");
      $(avp(gwadv)) = "R" + $(avp(outbound_route)) + "A";
      if( !route(trunk-side-invite) )
      {
          sl_send_reply("504", "Could not route calling number");
          exit;
      }
      route(update-media-proxy);   # Start MP if needed
      route(initial-forwarder);   # Forward
      return;
  }

  ## XXX Starting here is the piece that will need to be replaced with DROUTING ##

  # International number: routed
  if($(avp(dst_type)) == "INTL")
  {
      # Extract the first 6 digits+"A" of the destination number.
      $(avp(gwadv)) = "+" + $(rU{s.substr,0,6}) + "A";
      if( ! route(trunk-side-invite) )
      {
          # Extract the first 3 digits+"A" of the destination number.
          $(avp(gwadv)) = "+" + $(rU{s.substr,0,3}) + "A";

          if( ! route(trunk-side-invite) )
          {
              $(avp(gwadv)) = "+A";

              if( ! route(trunk-side-invite) )
              {
                  sl_send_reply("504", "Invalid international destination -- missing routing information");
                  exit;
              }
          }
      }
      route(update-media-proxy);   # Start MP if needed
      route(iniial-forwarder);   # Forward
      return;
  }
  # /International

  # NANPA-specific
  if(${NANPA})
  {
          # Extract the NPANXXA from the destination number.
          $(avp(gwadv)) = $(rU{s.substr,1,6}) + "A";

          if( ! route(trunk-side-invite) )
          {
              # Extract the NPAA from the destination number.
              $(avp(gwadv)) = $(rU{s.substr,1,3}) + "A";

              if( ! route(trunk-side-invite) )
              {
                  $(avp(gwadv)) = "A";

                  if( ! route(trunk-side-invite) )
                  {
                      sl_send_reply("504", "Invalid national destination -- missing routing information");
                      return;
                  }
              }
          }
          route(update-media-proxy);   # Start MP if needed
          route(initial-forwarder);   # Forward
          return;
  }
  # /NANPA-specific

  if(${FR})
  {
    xlog("L_DBG","DEBUG -- route(try-trunk-side) FR");
  }

}
{'fraction_non_alphanumeric': 0.16297251551543485, 'fraction_numerical': 0.012734746514064641, 'mean_word_length': 2.1888974556669236, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6700006', 'n_tokens_mistral': 4485, 'n_tokens_neox': 4272, 'n_words': 954}