Skip to content

Commit c31cdfb

Browse files
committed
Change asterisk function IS_VALID_NUM to support short number options #4
1 parent 77f3ebc commit c31cdfb

File tree

1 file changed

+48
-6
lines changed

1 file changed

+48
-6
lines changed

asterisk/app_numformat.c

+48-6
Original file line numberDiff line numberDiff line change
@@ -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>
@@ -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");

0 commit comments

Comments
 (0)