示例#1
0
char * maildriver_quote_mailbox(const char * mb)
{
  MMAPString * gstr;
  char * str;

  gstr = mmap_string_new("");
  if (gstr == NULL)
    return NULL;
  
  while (* mb != 0) {
    char hex[3];

    if (((* mb >= 'a') && (* mb <= 'z')) ||
	((* mb >= 'A') && (* mb <= 'Z')) ||
	((* mb >= '0') && (* mb <= '9')))
      mmap_string_append_c(gstr, * mb);
    else {
      if (mmap_string_append_c(gstr, '%') == NULL)
	goto free;
      snprintf(hex, 3, "%02x", (unsigned char) (* mb));
      if (mmap_string_append(gstr, hex) == NULL)
	goto free;
    }
    mb ++;
  }

  str = strdup(gstr->str);
  if (str == NULL)
    goto free;

  mmap_string_free(gstr);
  
  return str;

 free:
  mmap_string_free(gstr);
  return NULL;
}
示例#2
0
static int read_response(mailsmtp * session)
{
  char * line;
  int code;

  mmap_string_assign(session->response_buffer, "");

  do {
    line = read_line(session);

    if (line != NULL) {
	code = parse_response(session, line);
        mmap_string_append_c(session->response_buffer, '\n');
      }
    else
      code = 0;
  }
  while ((code & SMTP_STATUS_CONTINUE) != 0);

  session->response = session->response_buffer->str;

  return code;
}
示例#3
0
LIBETPAN_EXPORT
int mailmime_encoded_phrase_parse(const char * default_fromcode,
    const char * message, size_t length,
    size_t * indx, const char * tocode,
    char ** result)
{
  MMAPString * gphrase;
  struct mailmime_encoded_word * word;
  int first;
  size_t cur_token;
  int r;
  int res;
  char * str;
  char * wordutf8;
  int type;

  cur_token = * indx;

  gphrase = mmap_string_new("");
  if (gphrase == NULL) {
    res = MAILIMF_ERROR_MEMORY;
    goto err;
  }

  first = TRUE;

  type = TYPE_ERROR; /* XXX - removes a gcc warning */

  while (1) {
    int has_fwd = 0;

    word = NULL;
    r = mailmime_encoded_word_parse(message, length, &cur_token, &word, &has_fwd);
    if (r == MAILIMF_NO_ERROR) {
      if ((!first) && has_fwd) {
        if (type != TYPE_ENCODED_WORD) {
          if (mmap_string_append_c(gphrase, ' ') == NULL) {
            mailmime_encoded_word_free(word);
            res = MAILIMF_ERROR_MEMORY;
            goto free;
          }
        }
      }
      type = TYPE_ENCODED_WORD;
      wordutf8 = NULL;
      r = charconv(tocode, word->wd_charset, word->wd_text,
                   strlen(word->wd_text), &wordutf8);
      switch (r) {
        case MAIL_CHARCONV_ERROR_MEMORY:
          mailmime_encoded_word_free(word);
          res = MAILIMF_ERROR_MEMORY;
          goto free;

        case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET:
          r = charconv(tocode, "iso-8859-1", word->wd_text,
                       strlen(word->wd_text), &wordutf8);
          break;
        case MAIL_CHARCONV_ERROR_CONV:
          mailmime_encoded_word_free(word);
          res = MAILIMF_ERROR_PARSE;
          goto free;
      }

      switch (r) {
        case MAIL_CHARCONV_ERROR_MEMORY:
          mailmime_encoded_word_free(word);
          res = MAILIMF_ERROR_MEMORY;
          goto free;
        case MAIL_CHARCONV_ERROR_CONV:
          mailmime_encoded_word_free(word);
          res = MAILIMF_ERROR_PARSE;
          goto free;
      }

      if (wordutf8 != NULL) {
        if (mmap_string_append(gphrase, wordutf8) == NULL) {
          mailmime_encoded_word_free(word);
          free(wordutf8);
          res = MAILIMF_ERROR_MEMORY;
          goto free;
        }
        free(wordutf8);
      }
      mailmime_encoded_word_free(word);
      first = FALSE;
    }
    else if (r == MAILIMF_ERROR_PARSE) {
      /* do nothing */
    }
    else {
      res = r;
      goto free;
    }

    if (r == MAILIMF_ERROR_PARSE) {
      char * raw_word;

      raw_word = NULL;
      r = mailmime_non_encoded_word_parse(message, length,
                                          &cur_token, &raw_word, &has_fwd);
      if (r == MAILIMF_NO_ERROR) {
        if ((!first) && has_fwd) {
          if (mmap_string_append_c(gphrase, ' ') == NULL) {
            free(raw_word);
            res = MAILIMF_ERROR_MEMORY;
            goto free;
          }
        }
        type = TYPE_WORD;

        wordutf8 = NULL;
        r = charconv(tocode, default_fromcode, raw_word,
                     strlen(raw_word), &wordutf8);

        switch (r) {
          case MAIL_CHARCONV_ERROR_MEMORY:
            free(raw_word);
            res = MAILIMF_ERROR_MEMORY;
            goto free;

          case MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET:
          case MAIL_CHARCONV_ERROR_CONV:
            free(raw_word);
            res = MAILIMF_ERROR_PARSE;
            goto free;
        }

        if (mmap_string_append(gphrase, wordutf8) == NULL) {
          free(wordutf8);
          free(raw_word);
          res = MAILIMF_ERROR_MEMORY;
          goto free;
        }

        free(wordutf8);
        free(raw_word);
        first = FALSE;
      }
      else if (r == MAILIMF_ERROR_PARSE) {
        r = mailimf_fws_parse(message, length, &cur_token);
        if (r != MAILIMF_NO_ERROR) {
          break;
        }

        if (mmap_string_append_c(gphrase, ' ') == NULL) {
          res = MAILIMF_ERROR_MEMORY;
          goto free;
        }
        first = FALSE;
        break;
      }
      else {
        res = r;
        goto free;
      }
    }
  }

  if (first) {
    if (cur_token != length) {
      res = MAILIMF_ERROR_PARSE;
      goto free;
    }
  }

  str = strdup(gphrase->str);
  if (str == NULL) {
    res = MAILIMF_ERROR_MEMORY;
    goto free;
  }
  mmap_string_free(gphrase);

  * result = str;
  * indx = cur_token;

  return MAILIMF_NO_ERROR;

free:
  mmap_string_free(gphrase);
err:
  return res;
}
示例#4
0
static char * make_quoted_printable(const char * display_charset,
    const char * phrase)
{
  char * str;
  const char * cur;
  MMAPString * mmapstr;
  int r;

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL)
    return NULL;
  
  cur = phrase;
  while (* cur != '\0') {
    const char * begin;
    const char * end;
    int do_quote;
    int quote_words;

    begin = cur;
    end = begin;
    quote_words = 0;
    do_quote = 1;

    while (* cur != '\0') {
      get_word(cur, &cur, &do_quote);
      if (do_quote) {
        quote_words = 1;
        end = cur;
      }
      else
        break;
      if (* cur != '\0')
        cur ++;
    }

    if (quote_words) {
      r = quote_word(display_charset, mmapstr, begin, end - begin);
      if (r < 0) {
        mmap_string_free(mmapstr);
        return NULL;
      }
      
      if ((* end == ' ') || (* end == '\t')) {
        if (mmap_string_append_c(mmapstr, * end) == NULL) {
          mmap_string_free(mmapstr);
          return NULL;
        }
        end ++;
      }

      if (* end != '\0') {
        if (mmap_string_append_len(mmapstr, end, cur - end) == NULL) {
          mmap_string_free(mmapstr);
          return NULL;
        }
      }
    }
    else {
      if (mmap_string_append_len(mmapstr, begin, cur - begin) == NULL) {
        mmap_string_free(mmapstr);
        return NULL;
      }
    }

    if ((* cur == ' ') || (* cur == '\t')) {
      if (mmap_string_append_c(mmapstr, * cur) == 0) {
        mmap_string_free(mmapstr);
        return NULL;
      }
      cur ++;
    }
  }

  str = strdup(mmapstr->str);
  if (str == NULL) {
    mmap_string_free(mmapstr);
    return NULL;
  }

  mmap_string_free(mmapstr);
  
  return str;
}
示例#5
0
static inline int quote_word(const char * display_charset,
    MMAPString * mmapstr, const char * word, size_t size)
{
  const char * cur;
  size_t i;
  char hex[4];
  int col;
  
  if (mmap_string_append(mmapstr, "=?") == NULL)
    return -1;
  if (mmap_string_append(mmapstr, display_charset) == NULL)
    return -1;
  if (mmap_string_append(mmapstr, "?Q?") == NULL)
    return -1;
  
  col = mmapstr->len;
  
  cur = word;
  for(i = 0 ; i < size ; i ++) {
    int do_quote_char;

    if (col + 2 /* size of "?=" */
        + 3 /* max size of newly added character */
        + 1 /* minimum column of string in a
               folded header */ >= MAX_IMF_LINE) {
      int old_pos;
      /* adds a concatened encoded word */
      
      if (mmap_string_append(mmapstr, "?=") == NULL)
        return -1;
      
      if (mmap_string_append(mmapstr, " ") == NULL)
        return -1;
      
      old_pos = mmapstr->len;
      
      if (mmap_string_append(mmapstr, "=?") == NULL)
        return -1;
      if (mmap_string_append(mmapstr, display_charset) == NULL)
        return -1;
      if (mmap_string_append(mmapstr, "?Q?") == NULL)
        return -1;
      
      col = mmapstr->len - old_pos;
    }
    
    do_quote_char = 0;
    switch (* cur) {
    case ',':
    case ':':
    case '!':
    case '"':
    case '#':
    case '$':
    case '@':
    case '[':
    case '\\':
    case ']':
    case '^':
    case '`':
    case '{':
    case '|':
    case '}':
    case '~':
    case '=':
    case '?':
    case '_':
      do_quote_char = 1;
      break;

    default:
      if (((unsigned char) * cur) >= 128)
        do_quote_char = 1;
      break;
    }

    if (do_quote_char) {
      snprintf(hex, 4, "=%2.2X", (unsigned char) * cur);
      if (mmap_string_append(mmapstr, hex) == NULL)
        return -1;
      col += 3;
    }
    else {
      if (* cur == ' ') {
        if (mmap_string_append_c(mmapstr, '_') == NULL)
          return -1;
      }
      else {
        if (mmap_string_append_c(mmapstr, * cur) == NULL)
          return -1;
      }
      col += 3;
    }
    cur ++;
  }

  if (mmap_string_append(mmapstr, "?=") == NULL)
    return -1;
  
  return 0;
}