sendmail: code shrink on top of previous patches

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2013-03-18 18:47:16 +01:00
parent e82bfef839
commit a42f530e03

View File

@ -92,47 +92,37 @@ static int smtp_check(const char *fmt, int code)
// strip argument of bad chars // strip argument of bad chars
static char *sane_address(char *str) static char *sane_address(char *str)
{ {
char *s = str; char *s;
char *p = s;
int leading_space = 1;
int trailing_space = 0;
trim(str);
s = str;
while (*s) { while (*s) {
if (isspace(*s)) { if (!isalnum(*s) && !strchr("_-.@", *s)) {
trailing_space = !leading_space; bb_error_msg("bad address '%s'", str);
} else { /* returning "": */
*p++ = *s; str[0] = '\0';
if ((!isalnum(*s) && !strchr("_-.@", *s)) ||
trailing_space) {
*p = '\0';
bb_error_msg("Bad address: %s", str);
*str = '\0';
return str; return str;
} }
leading_space = 0;
}
s++; s++;
} }
*p = '\0';
return str; return str;
} }
// check for an address inside angle brackets, if not found fall back to normal // check for an address inside angle brackets, if not found fall back to normal
static char *angle_address(char *str) static char *angle_address(char *str)
{ {
char *s = str; char *s, *e;
char *e = str + strlen(str);
while (e != str && (isspace(*e) || *e == '\0')) trim(str);
e--; e = last_char_is(str, '>');
if (*e != '>') if (e) {
goto done; s = strrchr(str, '<');
if (s) {
*e = '\0'; *e = '\0';
e = strrchr(s, '<'); str = s + 1;
if (e != NULL) }
s = e + 1; }
done: return sane_address(str);
return sane_address(s);
} }
static void rcptto(const char *s) static void rcptto(const char *s)
@ -145,29 +135,27 @@ static void rcptto(const char *s)
} }
// send to a list of comma separated addresses // send to a list of comma separated addresses
static void rcptto_list(const char *_str) static void rcptto_list(const char *list)
{ {
char *str = xstrdup(_str); char *str = xstrdup(list);
int len = strlen(str);
int in_quote = 0;
char *s = str; char *s = str;
char prev = 0; char prev = 0;
int pos; int in_quote = 0;
for (pos = 0; pos < len; pos++) { while (*s) {
char ch = str[pos]; char ch = *s++;
if (ch == '"' && prev != '\\') { if (ch == '"' && prev != '\\') {
in_quote = !in_quote; in_quote = !in_quote;
} else if (!in_quote && ch == ',') { } else if (!in_quote && ch == ',') {
str[pos] = '\0'; s[-1] = '\0';
rcptto(angle_address(s)); rcptto(angle_address(str));
s = str + pos + 1; str = s;
} }
prev = ch; prev = ch;
} }
if (prev != ',') if (prev != ',')
rcptto(angle_address(s)); rcptto(angle_address(str));
free(str); free(str);
} }
@ -349,7 +337,7 @@ int sendmail_main(int argc UNUSED_PARAM, char **argv)
// analyze headers // analyze headers
// To: or Cc: headers add recipients // To: or Cc: headers add recipients
check_hdr = 0 == strncasecmp("To:", s, 3); check_hdr = (0 == strncasecmp("To:", s, 3));
has_to |= check_hdr; has_to |= check_hdr;
if (opts & OPT_t) { if (opts & OPT_t) {
if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) { if (check_hdr || 0 == strncasecmp("Bcc:" + 1, s, 3)) {