Bug 6096 - callerid_parse cleanup

git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@8370 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Tilghman Lesher
2006-01-21 05:05:45 +00:00
parent 5d2cd87f41
commit 51944ec723

View File

@@ -862,6 +862,10 @@ int callerid_generate(unsigned char *buf, char *number, char *name, int flags, i
return bytes; return bytes;
} }
/*
* remove '(', ' ', ')', non-trailing '.', and '-' not in square brackets.
* Basically, remove anything that could be invalid in a pattern.
*/
void ast_shrink_phone_number(char *n) void ast_shrink_phone_number(char *n)
{ {
int x,y=0; int x,y=0;
@@ -908,53 +912,50 @@ int ast_isphonenumber(char *n)
} }
/*! \brief parse string for caller id information /*! \brief parse string for caller id information
\return returns -1 on failure, otherwise 0 \return always returns 0, as the code always returns something.
XXX note that 'name' is not parsed consistently e.g. we have
input location name
" foo bar " <123> 123 ' foo bar ' (with spaces around)
" foo bar " NULL 'foo bar' (without spaces around)
" foo bar <123>" 123 '" foo bar'
The parsing of leading and trailing space/quotes should be more consistent.
*/ */
int ast_callerid_parse(char *instr, char **name, char **location) int ast_callerid_parse(char *instr, char **name, char **location)
{ {
char *ns, *ne; char *ns, *ne, *ls, *le;
char *ls, *le;
char tmp[256]; /* Try "name" <location> format or name <location> format */
/* Try for "name" <location> format or
name <location> format */
if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) { if ((ls = strchr(instr, '<')) && (le = strchr(ls, '>'))) {
/* Found the location */ *ls = *le = '\0'; /* location found, trim off the brackets */
*le = '\0'; *location = ls + 1; /* and this is the result */
*ls = '\0'; if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
*location = ls + 1; *ns = *ne = '\0'; /* trim off the quotes */
if ((ns = strchr(instr, '\"')) && (ne = strchr(ns + 1, '\"'))) { *name = ns + 1; /* and this is the name */
/* Get name out of quotes */ } else { /* no quotes, trim off leading and trailing spaces */
*ns = '\0'; *name = ast_skip_blanks(instr);
*ne = '\0'; ast_trim_blanks(*name);
*name = ns + 1;
return 0;
} else {
/* Just trim off any trailing spaces */
*name = instr;
while(!ast_strlen_zero(instr) && (instr[strlen(instr) - 1] < 33))
instr[strlen(instr) - 1] = '\0';
/* And leading spaces */
*name = ast_skip_blanks(*name);
return 0;
} }
} else { } else { /* no valid brackets */
char tmp[256];
ast_copy_string(tmp, instr, sizeof(tmp)); ast_copy_string(tmp, instr, sizeof(tmp));
ast_shrink_phone_number(tmp); ast_shrink_phone_number(tmp);
if (ast_isphonenumber(tmp)) { if (ast_isphonenumber(tmp)) { /* Assume it's just a location */
/* Assume it's just a location */
*name = NULL; *name = NULL;
strcpy(instr, tmp); /* safe, because tmp will always be the same size or smaller than instr */
*location = instr; *location = instr;
} else { } else { /* Assume it's just a name. */
/* Assume it's just a name. Make sure it's not quoted though */
*name = instr;
while(*(*name) && ((*(*name) < 33) || (*(*name) == '\"'))) (*name)++;
ne = *name + strlen(*name) - 1;
while((ne > *name) && ((*ne < 33) || (*ne == '\"'))) { *ne = '\0'; ne--; }
*location = NULL; *location = NULL;
if ((ns = strchr(instr, '"')) && (ne = strchr(ns + 1, '"'))) {
*ns = *ne = '\0'; /* trim off the quotes */
*name = ns + 1; /* and this is the name */
} else { /* no quotes, trim off leading and trailing spaces */
*name = ast_skip_blanks(instr);
ast_trim_blanks(*name);
}
} }
return 0;
} }
return -1; return 0;
} }
static int __ast_callerid_generate(unsigned char *buf, char *name, char *number, int callwaiting, int codec) static int __ast_callerid_generate(unsigned char *buf, char *name, char *number, int callwaiting, int codec)