Skip to content

Commit 7d5713c

Browse files
committed
Merge branch 'release/1.3.0'
2 parents e90031b + ffa167d commit 7d5713c

13 files changed

+243
-170
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 2.8)
22

33
project(libastphonenumber)
44
set(libastphonenumber_VERSION_MAJOR 1)
5-
set(libastphonenumber_VERSION_MINOR 2)
5+
set(libastphonenumber_VERSION_MINOR 3)
66

77
if(UNIX)
88
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")

asterisk/app_numformat.c

+53-11
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
/*** DOCUMENTATION
4545
<function name="FORMAT_NUM" language="en_US">
46-
<synopsis>
46+
<synopsis>
4747
Get formatted number in specific format based on a country.
4848
</synopsis>
4949
<syntax argsep=":">
@@ -79,7 +79,7 @@
7979
</description>
8080
</function>
8181
<function name="IS_VALID_NUM" language="en_US">
82-
<synopsis>
82+
<synopsis>
8383
Check validity of a number.
8484
</synopsis>
8585
<syntax argsep=":">
@@ -95,6 +95,18 @@
9595
<enum name="l">
9696
<para>Check if the number is valid only in the country specified.</para>
9797
</enum>
98+
<enum name="s">
99+
<para>Check if the number is a valid short number.</para>
100+
</enum>
101+
<enum name="ls">
102+
<para>Check if the number is a valid short number only in the country specified.</para>
103+
</enum>
104+
<enum name="a">
105+
<para>Check if the number is a valid number or a valid short number.</para>
106+
</enum>
107+
<enum name="la">
108+
<para>Check if the number is a valid number or a valid short number only in the country specified.</para>
109+
</enum>
98110
</enumlist>
99111
</parameter>
100112
</syntax>
@@ -103,7 +115,7 @@
103115
</description>
104116
</function>
105117
<function name="NUM_INFO" language="en_US">
106-
<synopsis>
118+
<synopsis>
107119
Get information about a number.
108120
</synopsis>
109121
<syntax argsep=":">
@@ -133,7 +145,7 @@
133145
</description>
134146
</function>
135147
<function name="REGION_INFO" language="en_US">
136-
<synopsis>
148+
<synopsis>
137149
Get information about a country/region.
138150
</synopsis>
139151
<syntax argsep=":">
@@ -158,7 +170,7 @@
158170
#define BOOL2STR(b) b ? "Yes" : "No"
159171

160172
static int (*num_format_fn)(char *, char *, enum phone_format, char *);
161-
static int (*is_valid_number_fn)(char* , char*, int);
173+
static int (*is_valid_number_fn)(char* , char*, int, int);
162174
static int (*get_region_fn)(char* , char* , char *);
163175
static int (*get_country_code_fn)(char* , char*);
164176
static enum phone_type (*get_number_type_fn)(char* , char*);
@@ -258,9 +270,15 @@ static char *cli_formatnum_e164(struct ast_cli_entry *e, int cmd, struct ast_cli
258270
ast_cli(a->fd, " - Number: %s\n", number);
259271
ast_cli(a->fd, " - Country: %s\n", country);
260272

273+
ast_cli(a->fd, "Number validation\n");
274+
ast_cli(a->fd, " - Is valid number: %s\n", BOOL2STR((*is_valid_number_fn)(number, country, 0, 0)));
275+
ast_cli(a->fd, " - Is valid short number: %s\n", BOOL2STR((*is_valid_number_fn)(number, country, 0, 1)));
276+
ast_cli(a->fd, " - Is valid number or short number: %s\n", BOOL2STR((*is_valid_number_fn)(number, country, 0, 2)));
277+
ast_cli(a->fd, " - Is valid number in %s: %s\n", country, BOOL2STR((*is_valid_number_fn)(number, country, 1, 0)));
278+
ast_cli(a->fd, " - Is valid short number in %s: %s\n", country, BOOL2STR((*is_valid_number_fn)(number, country, 1, 1)));
279+
ast_cli(a->fd, " - Is valid number or short number in %s: %s\n", country, BOOL2STR((*is_valid_number_fn)(number, country, 1, 2)));
280+
261281
ast_cli(a->fd, "Number information\n");
262-
ast_cli(a->fd, " - Is valid number: %s\n", BOOL2STR((*is_valid_number_fn)(number, country, 0)));
263-
ast_cli(a->fd, " - Is valid number in %s: %s\n", country, BOOL2STR((*is_valid_number_fn)(number, country, 1)));
264282
ast_cli(a->fd, " - Country calling code: %d\n", (*get_country_code_fn)(number, country));
265283
(*get_region_fn)(number, country, buf);
266284
ast_cli(a->fd, " - Country: %s\n", buf);
@@ -339,31 +357,55 @@ static int valid_num_func_read(struct ast_channel *chan, const char *cmd, char *
339357
{
340358
char *number, *country, *option;
341359
int is_valid;
360+
int opt_local, opt_short_code;
342361

343362
number = ast_strdupa(data);
344363

364+
// Get the country
345365
if ((country = strchr(number, ':'))) {
346366
*country = '\0';
347367
country++;
348368
}
349369

370+
// Get the option value
350371
if (country && (option = strchr(country, ':'))) {
351372
*option = '\0';
352373
option++;
353374
}
354375

376+
// Check if the number or the country is not empty
355377
if (ast_strlen_zero(number) || ast_strlen_zero(country)) {
356378
ast_log(LOG_ERROR, "This function needs a number and a country: number:country\n");
357379
return 0;
358380
}
359-
if(!ast_strlen_zero(option) && strcasecmp(option, "l")) {
381+
382+
// Check validity of the option
383+
if (ast_strlen_zero(option)) {
384+
opt_local = 0;
385+
opt_short_code = 0;
386+
} else if (!strcasecmp(option, "l")) {
387+
opt_local = 1;
388+
opt_short_code = 0;
389+
} else if (!strcasecmp(option, "s")) {
390+
opt_local = 0;
391+
opt_short_code = 1;
392+
} else if (!strcasecmp(option, "ls")) {
393+
opt_local = 1;
394+
opt_short_code = 1;
395+
} else if (!strcasecmp(option, "a")) {
396+
opt_local = 0;
397+
opt_short_code = 2;
398+
} else if (!strcasecmp(option, "la")) {
399+
opt_local = 1;
400+
opt_short_code = 2;
401+
} else {
360402
ast_log(LOG_ERROR, "Invalid option.\n");
361403
return 0;
362404
}
363405

364406
// Check validity of the number
365-
is_valid = (*is_valid_number_fn)(number, country, (!ast_strlen_zero(option) && *option == 'l'));
366-
if (!ast_strlen_zero(option) && *option == 'l') {
407+
is_valid = (*is_valid_number_fn)(number, country, opt_local, opt_short_code);
408+
if (opt_local) {
367409
ast_debug(3, "Number %s is %s format in country %s.\n", number, is_valid ? "a valid" : "an invalid", country);
368410
} else {
369411
ast_debug(3, "Number %s is %s format.\n", number, is_valid ? "a valid" : "an invalid");
@@ -460,7 +502,7 @@ static int unload_module(void)
460502

461503
static int load_module(void)
462504
{
463-
char *error;
505+
char *error;
464506

465507
/* Register all CLI functions for number formatting */
466508
ast_cli_register_multiple(cli_formatnum, ARRAY_LEN(cli_formatnum));

debian/changelog

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
libastphonenumber (1.3.0) all; urgency=medium
2+
3+
* Feature: Support short code checks on 'IS_VALID_NUM' function #4
4+
5+
-- Sebastien De Fauw <sdefauw@gmail.com> Thu, 09 Jul 2020 14:04:30 +0200
6+
17
libastphonenumber (1.2.1) all; urgency=medium
28

39
* Bugfix: Segfault when using NUM_INFO without all required arguments
@@ -6,7 +12,7 @@ libastphonenumber (1.2.1) all; urgency=medium
612

713
libastphonenumber (1.2.0) all; urgency=medium
814

9-
* Improvment: Remove some char when for some formatting
15+
* Improvement: Remove some char when for some formatting
1016
* Improvement: Review testing section
1117
* Bugfix: Link to the right library version
1218
* Bugfix: Fix debian installation issue between both packages

src/formatting.cpp

+33-35
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11

2-
#include <iostream>
32
#include <string>
43

54
#include "formatting.h"
6-
#include "phonenumbers/base/logging.h"
75
#include "phonenumbers/phonenumber.pb.h"
86
#include "phonenumbers/phonenumberutil.h"
97

@@ -14,55 +12,55 @@
1412
using i18n::phonenumbers::PhoneNumber;
1513
using i18n::phonenumbers::PhoneNumberUtil;
1614

17-
int num_format(char* number, char* country, enum phone_format type, char* formatted) {
15+
int num_format(char *number, char *country, enum phone_format type, char *formatted) {
1816

1917
strcpy(formatted, number);
2018

2119
PhoneNumber pnumber;
2220

23-
const PhoneNumberUtil& phone_util = *PhoneNumberUtil::GetInstance();
21+
const PhoneNumberUtil &phone_util = *PhoneNumberUtil::GetInstance();
2422
phone_util.Parse(number, country, &pnumber);
2523
if (!phone_util.IsValidNumber(pnumber)) {
2624
strcpy(formatted, number);
2725
return -1;
2826
}
2927

3028
std::string formatted_number;
31-
switch (type){
32-
case PLUSE164:
33-
phone_util.Format(pnumber, PhoneNumberUtil::E164, &formatted_number);
34-
break;
35-
case E164:
36-
phone_util.Format(pnumber, PhoneNumberUtil::E164, &formatted_number);
37-
if(formatted_number.at(0) == '+') {
38-
formatted_number = formatted_number.erase(0, 1);
39-
}
40-
break;
41-
case INTERNATIONAL:
42-
phone_util.Format(pnumber, PhoneNumberUtil::INTERNATIONAL, &formatted_number);
43-
break;
44-
case EXTERNAL_CALL:
45-
phone_util.FormatOutOfCountryCallingNumber(pnumber, country, &formatted_number);
46-
REMOVE_SPACES(formatted_number);
29+
switch (type) {
30+
case PLUSE164:
31+
phone_util.Format(pnumber, PhoneNumberUtil::E164, &formatted_number);
32+
break;
33+
case E164:
34+
phone_util.Format(pnumber, PhoneNumberUtil::E164, &formatted_number);
35+
if (formatted_number.at(0) == '+') {
36+
formatted_number = formatted_number.erase(0, 1);
37+
}
38+
break;
39+
case INTERNATIONAL:
40+
phone_util.Format(pnumber, PhoneNumberUtil::INTERNATIONAL, &formatted_number);
41+
break;
42+
case EXTERNAL_CALL:
43+
phone_util.FormatOutOfCountryCallingNumber(pnumber, country, &formatted_number);
44+
REMOVE_SPACES(formatted_number);
4745
REMOVE_DASHES(formatted_number);
48-
break;
49-
case NATIONAL:
50-
phone_util.Format(pnumber, PhoneNumberUtil::NATIONAL, &formatted_number);
51-
break;
52-
case NATIONAL_COMPACT:
53-
phone_util.Format(pnumber, PhoneNumberUtil::NATIONAL, &formatted_number);
54-
REMOVE_SPACES(formatted_number);
46+
break;
47+
case NATIONAL:
48+
phone_util.Format(pnumber, PhoneNumberUtil::NATIONAL, &formatted_number);
49+
break;
50+
case NATIONAL_COMPACT:
51+
phone_util.Format(pnumber, PhoneNumberUtil::NATIONAL, &formatted_number);
52+
REMOVE_SPACES(formatted_number);
5553
REMOVE_DASHES(formatted_number);
5654
REMOVE_CHAR(formatted_number, ')');
5755
REMOVE_CHAR(formatted_number, '(');
58-
break;
59-
case NATIONAL_SHORT:
60-
formatted_number = std::to_string(pnumber.national_number());
61-
break;
62-
default:
63-
formatted_number = number;
64-
break;
65-
}
56+
break;
57+
case NATIONAL_SHORT:
58+
formatted_number = std::to_string(pnumber.national_number());
59+
break;
60+
default:
61+
formatted_number = number;
62+
break;
63+
}
6664

6765
strcpy(formatted, formatted_number.c_str());
6866

src/formatting.h

+12-12
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ extern "C" {
1111
* Example: 0473012345 in BE
1212
*/
1313
enum phone_format {
14-
// +32473012345
14+
// +32473012345
1515
PLUSE164,
1616

17-
// 32473012345
17+
// 32473012345
1818
E164,
1919

20-
// +32 473 01 23 45
20+
// +32 473 01 23 45
2121
INTERNATIONAL,
2222

23-
// 003247012345
24-
EXTERNAL_CALL,
23+
// 003247012345
24+
EXTERNAL_CALL,
2525

26-
// 0473 01 23 45
27-
NATIONAL,
26+
// 0473 01 23 45
27+
NATIONAL,
2828

29-
// 0473012345
30-
NATIONAL_COMPACT,
29+
// 0473012345
30+
NATIONAL_COMPACT,
3131

32-
// 473012345
33-
NATIONAL_SHORT
32+
// 473012345
33+
NATIONAL_SHORT
3434
};
3535

3636
/**
@@ -42,7 +42,7 @@ enum phone_format {
4242
* @param formatted Number that will be formatted.
4343
* @return 0 if there is no issue, otherwise 1
4444
*/
45-
int num_format(char* number, char* country, enum phone_format type, char* formatted);
45+
int num_format(char *number, char *country, enum phone_format type, char *formatted);
4646

4747
#ifdef __cplusplus
4848
}

0 commit comments

Comments
 (0)