|
43 | 43 |
|
44 | 44 | /*** DOCUMENTATION
|
45 | 45 | <function name="FORMAT_NUM" language="en_US">
|
46 |
| - <synopsis> |
| 46 | + <synopsis> |
47 | 47 | Get formatted number in specific format based on a country.
|
48 | 48 | </synopsis>
|
49 | 49 | <syntax argsep=":">
|
|
79 | 79 | </description>
|
80 | 80 | </function>
|
81 | 81 | <function name="IS_VALID_NUM" language="en_US">
|
82 |
| - <synopsis> |
| 82 | + <synopsis> |
83 | 83 | Check validity of a number.
|
84 | 84 | </synopsis>
|
85 | 85 | <syntax argsep=":">
|
|
95 | 95 | <enum name="l">
|
96 | 96 | <para>Check if the number is valid only in the country specified.</para>
|
97 | 97 | </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> |
98 | 110 | </enumlist>
|
99 | 111 | </parameter>
|
100 | 112 | </syntax>
|
|
103 | 115 | </description>
|
104 | 116 | </function>
|
105 | 117 | <function name="NUM_INFO" language="en_US">
|
106 |
| - <synopsis> |
| 118 | + <synopsis> |
107 | 119 | Get information about a number.
|
108 | 120 | </synopsis>
|
109 | 121 | <syntax argsep=":">
|
|
133 | 145 | </description>
|
134 | 146 | </function>
|
135 | 147 | <function name="REGION_INFO" language="en_US">
|
136 |
| - <synopsis> |
| 148 | + <synopsis> |
137 | 149 | Get information about a country/region.
|
138 | 150 | </synopsis>
|
139 | 151 | <syntax argsep=":">
|
|
158 | 170 | #define BOOL2STR(b) b ? "Yes" : "No"
|
159 | 171 |
|
160 | 172 | 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); |
162 | 174 | static int (*get_region_fn)(char* , char* , char *);
|
163 | 175 | static int (*get_country_code_fn)(char* , char*);
|
164 | 176 | 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
|
258 | 270 | ast_cli(a->fd, " - Number: %s\n", number);
|
259 | 271 | ast_cli(a->fd, " - Country: %s\n", country);
|
260 | 272 |
|
| 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 | + |
261 | 281 | 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))); |
264 | 282 | ast_cli(a->fd, " - Country calling code: %d\n", (*get_country_code_fn)(number, country));
|
265 | 283 | (*get_region_fn)(number, country, buf);
|
266 | 284 | 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 *
|
339 | 357 | {
|
340 | 358 | char *number, *country, *option;
|
341 | 359 | int is_valid;
|
| 360 | + int opt_local, opt_short_code; |
342 | 361 |
|
343 | 362 | number = ast_strdupa(data);
|
344 | 363 |
|
| 364 | + // Get the country |
345 | 365 | if ((country = strchr(number, ':'))) {
|
346 | 366 | *country = '\0';
|
347 | 367 | country++;
|
348 | 368 | }
|
349 | 369 |
|
| 370 | + // Get the option value |
350 | 371 | if (country && (option = strchr(country, ':'))) {
|
351 | 372 | *option = '\0';
|
352 | 373 | option++;
|
353 | 374 | }
|
354 | 375 |
|
| 376 | + // Check if the number or the country is not empty |
355 | 377 | if (ast_strlen_zero(number) || ast_strlen_zero(country)) {
|
356 | 378 | ast_log(LOG_ERROR, "This function needs a number and a country: number:country\n");
|
357 | 379 | return 0;
|
358 | 380 | }
|
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 { |
360 | 402 | ast_log(LOG_ERROR, "Invalid option.\n");
|
361 | 403 | return 0;
|
362 | 404 | }
|
363 | 405 |
|
364 | 406 | // 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) { |
367 | 409 | ast_debug(3, "Number %s is %s format in country %s.\n", number, is_valid ? "a valid" : "an invalid", country);
|
368 | 410 | } else {
|
369 | 411 | 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)
|
460 | 502 |
|
461 | 503 | static int load_module(void)
|
462 | 504 | {
|
463 |
| - char *error; |
| 505 | + char *error; |
464 | 506 |
|
465 | 507 | /* Register all CLI functions for number formatting */
|
466 | 508 | ast_cli_register_multiple(cli_formatnum, ARRAY_LEN(cli_formatnum));
|
|
0 commit comments