Пример #1
0
newsnntp * newsnntp_new(size_t progr_rate, progress_function * progr_fun)
{
  newsnntp * f;

  f = malloc(sizeof(* f));
  if (f == NULL)
    goto err;
  
  f->nntp_stream = NULL;
  f->nntp_readonly = FALSE;

  f->nntp_progr_rate = progr_rate;
  f->nntp_progr_fun = progr_fun;

  f->nntp_stream_buffer = mmap_string_new("");
  if (f->nntp_stream_buffer == NULL)
    goto free_f;

  f->nntp_response_buffer = mmap_string_new("");
  if (f->nntp_response_buffer == NULL)
    goto free_stream_buffer;

	f->nntp_timeout = 0;

  return f;

 free_stream_buffer:
    mmap_string_free(f->nntp_stream_buffer);
 free_f:
    free(f);
 err:
    return NULL;
}
Пример #2
0
mailsmtp * mailsmtp_new(size_t progr_rate,
			progress_function * progr_fun)
{
  mailsmtp * session;

  session = malloc(sizeof(* session));
  if (session == NULL)
    goto err;

  session->stream = NULL;

  session->progr_rate = progr_rate;
  session->progr_fun = progr_fun;

  session->response = NULL;
	session->response_code = 0;

  session->line_buffer = mmap_string_new("");
  if (session->line_buffer == NULL)
    goto free_session;

  session->response_buffer = mmap_string_new("");
  if (session->response_buffer == NULL)
    goto free_line_buffer;

  session->esmtp = 0;
  session->auth = MAILSMTP_AUTH_NOT_CHECKED;
  
  session->smtp_sasl.sasl_conn = NULL;
  
  session->smtp_max_msg_size = 0;
  session->smtp_progress_fun = NULL;
  session->smtp_progress_context = NULL;

	session->smtp_timeout = 0;

  session->smtp_logger = NULL;
  session->smtp_logger_context = NULL;
  
  return session;

 free_line_buffer:
  mmap_string_free(session->line_buffer);
 free_session:
  free(session);
 err:
  return NULL;
}
Пример #3
0
static int imap_fetch_envelope(mailmessage * msg_info,
			       struct mailimf_fields ** result)
{
  struct mailimf_fields * fields;
  int r;
  struct mail_cache_db * cache_db;
  MMAPString * mmapstr;
  char filename[PATH_MAX];
  struct imap_cached_session_state_data * data;
  int res;
  
  data = get_cached_session_data(msg_info);
  if (data->imap_quoted_mb == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  snprintf(filename, PATH_MAX, "%s/%s", data->imap_quoted_mb, ENV_NAME);

  r = mail_cache_db_open_lock(filename, &cache_db);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db;
  }

  r = imapdriver_get_cached_envelope(cache_db, mmapstr,
      msg_info->msg_session, msg_info, &fields);

  if ((r != MAIL_ERROR_CACHE_MISS) && (r != MAIL_NO_ERROR)) {
    res = r;
    goto close_db;
  }
  
  r = mailmessage_fetch_envelope(get_ancestor(msg_info), &fields);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto close_db;
  }

  r = imapdriver_write_cached_envelope(cache_db, mmapstr,
      msg_info->msg_session, msg_info, fields);

  * result = fields;

  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename, cache_db);

  return MAIL_NO_ERROR;

 close_db:
  mail_cache_db_close_unlock(filename, cache_db);
 err:
  return res;
}
Пример #4
0
static int get_envelopes_list(mailsession * session,
    struct mailmessage_list * env_list)
{
  unsigned int i;
  char key[PATH_MAX];
  int r;
  struct mail_cache_db * maildb;
  int res;
  struct db_session_state_data * data;
  MMAPString * mmapstr;
  
  data = get_data(session);
  
  flags_store_process(session);
  
  r = mail_cache_db_open_lock(data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db;
  }
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;
    
    msg = carray_get(env_list->msg_tab, i);
    if (msg->msg_fields == NULL) {
      snprintf(key, sizeof(key), "%lu-envelope",
          (unsigned long) msg->msg_index);
      
      r = generic_cache_fields_read(maildb, mmapstr,
        key, &msg->msg_fields);
    }
    
    if (msg->msg_flags == NULL) {
      snprintf(key, sizeof(key), "%lu-flags",
          (unsigned long) msg->msg_index);
      
      r = generic_cache_flags_read(maildb, mmapstr,
          key, &msg->msg_flags);
    }
  }
  
  mmap_string_free(mmapstr);
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  
  return MAIL_NO_ERROR;
  
 close_db:
  mail_cache_db_close_unlock(data->db_filename, maildb);
 err:
  return res;
}
Пример #5
0
static int fetch_section(mailmessage * msg_info,
    struct mailmime * mime,
    char ** result, size_t * result_len)
{
  int r;
  int res;
  int col;
  MMAPString * str;
  
  if (msg_info->msg_mime == NULL)
    return MAIL_ERROR_INVAL;
  
  str = mmap_string_new("");
  if (str == NULL) {
    res = MAILIMF_ERROR_MEMORY;
    goto err;
  }
  
  col = 0;
  r = mailmime_write_mem(str, &col, mime);
  if (r != MAILIMF_NO_ERROR) {
    res = maildriver_imf_error_to_mail_error(r);
    goto free;
  }
  
  if (mime->mm_parent == NULL) {
    r = mmap_string_ref(str);
    if (r < 0) {
      res = MAIL_ERROR_MEMORY;
      goto free;
    }
    
    * result = str->str;
    * result_len = str->len;
    
    r = MAIL_NO_ERROR;
  }
  else {
    r = body_to_mmapstr(str->str, str->len, result, result_len);
    if (r == MAIL_NO_ERROR) {
      mmap_string_free(str);
    }
  }
  
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free;
  }
  
  return MAIL_NO_ERROR;

 free:
  mmap_string_free(str);
 err:
  return res;
}
Пример #6
0
static int feed_prefetch(mailmessage * msg_info)
{
  struct generic_message_t * msg;
  int r;
  MMAPString * str;
  const char * text;
  int col;
  struct newsfeed * feed;
  struct newsfeed_item * item;
  int res;
  
  feed = get_feed_session(msg_info);
  item = newsfeed_get_item(feed, msg_info->msg_index);
  
  str = mmap_string_new("");
  if (str == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  col = 0;
  r = mailimf_fields_write_mem(str, &col,
      msg_info->msg_fields);
  if (r != MAILIMF_NO_ERROR) {
    res = MAIL_ERROR_MEMORY;
    goto free_str;
  }
  
  if (mmap_string_append(str, "\r\n") == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_str;
  }
  
  text = newsfeed_item_get_text(item);
  if (text == NULL) {
    /* if no content, fallback on summary */
    text = newsfeed_item_get_summary(item);
  }
  if (mmap_string_append(str, text) == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto free_str;
  }
  
  msg = msg_info->msg_data;
  msg->msg_message = str->str;
  msg->msg_length = str->len;
  
  mmap_string_ref(str);
  
  return MAIL_NO_ERROR;
  
 free_str:
  mmap_string_free(str);
 err:
  return res;
}
Пример #7
0
mailpop3 * mailpop3_new(size_t progr_rate, progress_function * progr_fun)
{
  mailpop3 * f;

  f = malloc(sizeof(* f));
  if (f == NULL)
    goto err;

  f->pop3_timestamp = NULL;
  f->pop3_response = NULL;
  
  f->pop3_stream = NULL;

  f->pop3_progr_rate = progr_rate;
  f->pop3_progr_fun = progr_fun;

  f->pop3_stream_buffer = mmap_string_new("");
  if (f->pop3_stream_buffer == NULL)
    goto free_f;

  f->pop3_response_buffer = mmap_string_new("");
  if (f->pop3_response_buffer == NULL)
    goto free_stream_buffer;

  f->pop3_msg_tab = NULL;
  f->pop3_deleted_count = 0;
  f->pop3_state = POP3_STATE_DISCONNECTED;
  
#ifdef USE_SASL
  f->pop3_sasl.sasl_conn = NULL;
#endif
  
  return f;

 free_stream_buffer:
  mmap_string_free(f->pop3_stream_buffer);
 free_f:
  free(f);
 err:
  return NULL;
}
Пример #8
0
static int flags_store_process(mailsession * session)
{
  unsigned int i;
  MMAPString * mmapstr;
  int r;
  int res;
  struct mail_cache_db * maildb;
  struct db_session_state_data * data;
  struct mail_flags_store * flags_store;
  
  data = get_data(session);
  
  flags_store = data->db_flags_store;
  
  if (carray_count(flags_store->fls_tab) == 0)
    return MAIL_NO_ERROR;
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  r = mail_cache_db_open_lock(data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
    mailmessage * msg;
    char key[PATH_MAX];
    
    msg = carray_get(flags_store->fls_tab, i);
    
    snprintf(key, sizeof(key), "%lu-flags", (unsigned long) msg->msg_index);
    
    r = generic_cache_flags_write(maildb, mmapstr,
        key, msg->msg_flags);
  }
  
  mail_flags_store_clear(flags_store);
  
  mail_cache_db_close_unlock(data->db_filename, maildb);
  mmap_string_free(mmapstr);
  
  return MAIL_NO_ERROR;
  
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}
Пример #9
0
static int mh_flags_store_process(char * flags_directory, char * quoted_mb,
				  struct mail_flags_store * flags_store)
{
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  unsigned int i;
  int r;
  int res;

  if (carray_count(flags_store->fls_tab) == 0)
    return MAIL_NO_ERROR;

  if (quoted_mb == NULL)
    return MAIL_NO_ERROR;

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
	   flags_directory, quoted_mb, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }
  
  for(i = 0 ; i < carray_count(flags_store->fls_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(flags_store->fls_tab, i);

    r = mhdriver_write_cached_flags(cache_db_flags, mmapstr,
        msg->msg_uid, msg->msg_flags);
  }

  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  mail_flags_store_clear(flags_store);

  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Пример #10
0
static void display_tree(struct mailmessage_tree * env_tree,
    unsigned int * pcount)
{
  MMAPString * prefix;

  prefix = mmap_string_new("");
  if (prefix == NULL)
    return;

  display_sub_tree(prefix, env_tree, 0, 0, pcount);

  mmap_string_free(prefix);
}
Пример #11
0
static int fetch_section_mime(mailmessage * msg_info,
    struct mailmime * mime,
    char ** result, size_t * result_len)
{
  int r;
  int res;
  int col;
  MMAPString * str;

  if (msg_info->msg_mime == NULL)
    return MAIL_ERROR_INVAL;

  str = mmap_string_new("");
  if (str == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  col = 0;
  if (mime->mm_content_type != NULL) {
    r = mailmime_content_write_mem(str, &col, mime->mm_content_type);
    if (r != MAILIMF_NO_ERROR) {
      res = maildriver_imf_error_to_mail_error(r);
      goto free;
    }
  }
  if (mime->mm_mime_fields != NULL) {
    r = mailmime_fields_write_mem(str, &col, mime->mm_mime_fields);
    if (r != MAILIMF_NO_ERROR) {
      res = maildriver_imf_error_to_mail_error(r);
      goto free;
    }
  }
  mailimf_string_write_mem(str, &col, "\r\n", 2);
  
  r = mmap_string_ref(str);
  if (r < 0) {
    res = MAIL_ERROR_MEMORY;
    goto free;
  }

  * result = str->str;
  * result_len = str->len;
  
  return MAIL_NO_ERROR;

 free:
  mmap_string_free(str);
 err:
  return res;
}
Пример #12
0
static void write_article_seq(mailsession * session,
			      uint32_t first,  uint32_t last)
{
  FILE * f;
  struct nntp_session_state_data * ancestor_data;
  char seq_filename[PATH_MAX];
  struct nntp_cached_session_state_data * cached_data;
  int r;
  int fd;

  cached_data = get_cached_data(session);
  ancestor_data = get_ancestor_data(session);

  if (ancestor_data->nntp_group_name == NULL)
    return;

  snprintf(seq_filename, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_cache_directory,
      ancestor_data->nntp_group_name, SEQ_FILENAME);

  fd = creat(seq_filename, S_IRUSR | S_IWUSR);
  if (fd < 0)
    return;
  
  f = fdopen(fd, "w");
  if (f != NULL) {
    r = maillock_write_lock(seq_filename, fd);
    if (r == 0) {
      MMAPString * mmapstr;
      size_t cur_token;

      mmapstr = mmap_string_new("");
      if (mmapstr != NULL) {
	r = mail_serialize_clear(mmapstr, &cur_token);
	if (r == MAIL_NO_ERROR) {
	  r = mailimf_cache_int_write(mmapstr, &cur_token, first);
	  r = mailimf_cache_int_write(mmapstr, &cur_token, last);
	  
	  fwrite(mmapstr->str, 1, mmapstr->len, f);
	}

	mmap_string_free(mmapstr);
      }
	  
      r = maillock_write_unlock(seq_filename, fd);
    }
    fclose(f);
  }
  else
    close(fd);
}
Пример #13
0
static int get_flags(mailmessage * msg_info,
    struct mail_flags ** result)
{
  char key[PATH_MAX];
  int r;
  struct db_session_state_data * sess_data;
  struct mail_cache_db * maildb;
  int res;
  MMAPString * mmapstr;
  
  sess_data = get_session_data(msg_info);
  
  r = mail_cache_db_open_lock(sess_data->db_filename, &maildb);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  snprintf(key, sizeof(key), "%lu-flags", (unsigned long) msg_info->msg_index);
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db;
  }
  
  r = generic_cache_flags_read(maildb, mmapstr,
    key, &msg_info->msg_flags);
  
  mmap_string_free(mmapstr);
  
  if (r != MAIL_NO_ERROR) {
    msg_info->msg_flags = mail_flags_new_empty();
    if (msg_info->msg_flags == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto close_db;
    }
  }
  
  mail_cache_db_close_unlock(sess_data->db_filename, maildb);
  
  * result = msg_info->msg_flags;
  
  return MAIL_NO_ERROR;
  
 close_db:
  mail_cache_db_close_unlock(sess_data->db_filename, maildb);
 err:
  return res;
}
Пример #14
0
static int fetch_section_body(mailmessage * msg_info,
    struct mailmime * mime,
    char ** result, size_t * result_len)
{
  int r;
  int res;
  int col;
  MMAPString * str;
  
  if (msg_info->msg_mime == NULL)
    return MAIL_ERROR_INVAL;

  str = mmap_string_new("");
  if (str == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  col = 0;
  if (mime->mm_mime_fields != NULL) {
    r = mailmime_write_mem(str, &col, mime);
    if (r != MAILIMF_NO_ERROR) {
      res = maildriver_imf_error_to_mail_error(r);
      goto free;
    }
  }
  
  if (mime->mm_type == MAILMIME_MESSAGE)
    r = body_body_to_mmapstr(str->str, str->len, result, result_len);
  else
    r = body_to_mmapstr(str->str, str->len, result, result_len);
  
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free;
  }
  
  mmap_string_free(str);
  
  return MAIL_NO_ERROR;
  
 free:
  mmap_string_free(str);
 err:
  return res;
}
Пример #15
0
static int db_set_message_list(struct mail_cache_db * maildb,
    carray * msglist)
{
  MMAPString * mmapstr;
  char key_value[PATH_MAX];
  int r;
  unsigned int i;
  size_t cur_token;
  int res;
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  cur_token = 0;
  for(i = 0 ; i < carray_count(msglist) ; i ++) {
    uint32_t * msg;
    
    msg = carray_get(msglist, i);
    r = mailimf_cache_int_write(mmapstr, &cur_token, * msg);
    if (r != MAIL_NO_ERROR) {
      res = r;
      goto free_mmapstr;
    }
  }
  
  snprintf(key_value, sizeof(key_value), "message-list");
  r = mail_cache_db_put(maildb, key_value, strlen(key_value),
      mmapstr->str, mmapstr->len);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  mmap_string_free(mmapstr);
  
  return MAIL_NO_ERROR;
  
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}
Пример #16
0
MMAPString*
mmap_string_new_len (const char *init,
		     size_t len)    
{
  MMAPString *string;

  if (len <= 0)
    return mmap_string_new (init);
  else
    {
      string = mmap_string_sized_new (len);
      
      if (init)
        mmap_string_append_len (string, init, len);
      
      return string;
    }
}
Пример #17
0
static void generate_key_from_mime_section(char * key, size_t size,
					   struct mailmime * mime)
{
  clistiter * cur;
  MMAPString * gstr;
  struct mailmime_section * part;
  int r;

  snprintf(key, size, "unvalid");

  r = mailmime_get_section_id(mime, &part);
  if (r != MAILIMF_NO_ERROR)
    goto err;

  gstr = mmap_string_new("part");
  if (gstr == NULL)
    goto free_section;

  for(cur = clist_begin(part->sec_list) ;
      cur != NULL ; cur = clist_next(cur)) {
    char s[20];

    snprintf(s, 20, ".%u", * (uint32_t *) clist_content(cur));
    if (mmap_string_append(gstr, s) == NULL)
      goto free_str;
  }

  snprintf(key, size, "%s", gstr->str);

  mmap_string_free(gstr);
  mailmime_section_free(part);

  return;

 free_str:
  mmap_string_free(gstr);
 free_section:
  mailmime_section_free(part);
 err:;
}
Пример #18
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;
}
Пример #19
0
static int
mailpop3_get_content(mailpop3 * f, struct mailpop3_msg_info * msginfo,
		     char ** result, size_t * result_len)
{
  char * response;
  char * result_multiline;
  MMAPString * buffer;
  int r;

  response = read_line(f);
  if (response == NULL)
    return MAILPOP3_ERROR_STREAM;
  r = parse_response(f, response);
  if (r != RESPONSE_OK)
    return MAILPOP3_ERROR_NO_SUCH_MESSAGE;

  buffer = mmap_string_new("");
  if (buffer == NULL)
    return MAILPOP3_ERROR_MEMORY;

  result_multiline = read_multiline(f, msginfo->msg_size, buffer);
  if (result_multiline == NULL) {
    mmap_string_free(buffer);
    return MAILPOP3_ERROR_STREAM;
  }
  else {
    r = mmap_string_ref(buffer);
    if (r < 0) {
      mmap_string_free(buffer);
      return MAILPOP3_ERROR_MEMORY;
    }
    
    * result = result_multiline;
    * result_len = buffer->len;
    return MAILPOP3_NO_ERROR;
  }
}
Пример #20
0
static int newsnntp_get_content(newsnntp * f, char ** result,
				size_t * result_len)
{
  int r;
  char * response;
  MMAPString * buffer;
  char * result_multiline;

  response = read_line(f);
  if (response == NULL)
    return NEWSNNTP_ERROR_STREAM;

  r = parse_response(f, response);

  switch (r) {
  case 480:
    return NEWSNNTP_ERROR_REQUEST_AUTHORIZATION_USERNAME;
    
  case 381:
    return NEWSNNTP_WARNING_REQUEST_AUTHORIZATION_PASSWORD;
    
  case 220:
  case 221:
  case 222:
  case 223:
    buffer = mmap_string_new("");
    if (buffer == NULL)
      return NEWSNNTP_ERROR_MEMORY;

    result_multiline = read_multiline(f, 0, buffer);
    if (result_multiline == NULL) {
      mmap_string_free(buffer);
      return NEWSNNTP_ERROR_MEMORY;
    }
    else {
      r = mmap_string_ref(buffer);
      if (r < 0) {
        mmap_string_free(buffer);
        return NEWSNNTP_ERROR_MEMORY;
      }
      
      * result = result_multiline;
      * result_len = buffer->len;
      return NEWSNNTP_NO_ERROR;
    }

  case 412:
    return NEWSNNTP_ERROR_NO_NEWSGROUP_SELECTED;

  case 420:
    return NEWSNNTP_ERROR_NO_ARTICLE_SELECTED;

  case 423:
    return NEWSNNTP_ERROR_INVALID_ARTICLE_NUMBER;

  case 430:
    return NEWSNNTP_ERROR_ARTICLE_NOT_FOUND;

  default:
    return NEWSNNTP_ERROR_UNEXPECTED_RESPONSE;
  }
}
Пример #21
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;
}
Пример #22
0
static int mhdriver_cached_append_message_flags(mailsession * session,
    const char * message, size_t size, struct mail_flags * flags)
{ 
  int r;
  struct mailmh_folder * folder;
  struct mailmh_msg_info * msg_info;
  chashdatum key;
  chashdatum value;
  uint32_t uid;
  struct mh_cached_session_state_data * data;
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  char keyname[PATH_MAX];
  
  folder = get_mh_cur_folder(session);
  if (folder == NULL)
    return MAIL_ERROR_BAD_STATE;
  
  r = mailmh_folder_add_message_uid(folder,
      message, size, &uid);
  
  switch (r) {
  case MAILMH_ERROR_FILE:
    return MAIL_ERROR_DISKSPACE;
    
  case MAILMH_NO_ERROR:
    break;
    
  default:
    return mhdriver_mh_error_to_mail_error(r);
  }
  
  if (flags == NULL)
    goto exit;
  
  key.data = &uid;
  key.len = sizeof(uid);
  r = chash_get(folder->fl_msgs_hash, &key, &value);
  if (r < 0)
    return MAIL_ERROR_CACHE_MISS;
  
  msg_info = value.data;
  
  data = get_cached_data(session);
  
  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      data->mh_flags_directory, data->mh_quoted_mb, FLAGS_NAME);
  
  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0)
    goto exit;
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL)
    goto close_db_flags;
  
  snprintf(keyname, PATH_MAX, "%u-%lu-%lu-flags",
      uid, (unsigned long) msg_info->msg_mtime,
      (unsigned long) msg_info->msg_size);
  
  r = mhdriver_write_cached_flags(cache_db_flags, mmapstr, keyname, flags);
  
  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  
  if (r != MAIL_NO_ERROR)
    goto exit;
  
  return MAIL_NO_ERROR;
  
 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 exit:
  return MAIL_NO_ERROR;
}
Пример #23
0
static int mhdriver_cached_expunge_folder(mailsession * session)
{
  struct mailmh_folder * folder;
  int res;
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  struct mh_cached_session_state_data * cached_data;
  unsigned int i;
  int r;

  cached_data = get_cached_data(session);
  if (cached_data->mh_quoted_mb == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  mh_flags_store_process(cached_data->mh_flags_directory,
      cached_data->mh_quoted_mb,
      cached_data->mh_flags_store);

  folder = get_mh_cur_folder(session);
  if (folder == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->mh_flags_directory, cached_data->mh_quoted_mb, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }

  for(i = 0 ; i < carray_count(folder->fl_msgs_tab) ; i++) {
    struct mailmh_msg_info * mh_info;
    struct mail_flags * flags;
    
    mh_info = carray_get(folder->fl_msgs_tab, i);
    if (mh_info == NULL)
      continue;

    r = mhdriver_get_cached_flags(cache_db_flags, mmapstr,
        session, mh_info->msg_index, &flags);
    if (r != MAIL_NO_ERROR)
      continue;

    if (flags->fl_flags & MAIL_FLAG_DELETED) {
      r = mailmh_folder_remove_message(folder, mh_info->msg_index);
    }

    mail_flags_free(flags);
  }

  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  mailmh_folder_update(folder);
  
  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Пример #24
0
static int write_max_uid_value(mailsession * session)
{
  int r;
  char filename[PATH_MAX];
  FILE * f;
  int res;
  struct mh_cached_session_state_data * cached_data;
  struct mh_session_state_data * ancestor_data;
  int fd;
  
  MMAPString * mmapstr;
  size_t cur_token;

  cached_data = get_cached_data(session);
  ancestor_data = get_ancestor_data(session);

  if (cached_data->mh_quoted_mb == NULL)
    return MAIL_ERROR_BAD_STATE;

  snprintf(filename, PATH_MAX, "%s/%s/%s",
	   cached_data->mh_cache_directory,
	   cached_data->mh_quoted_mb, FILENAME_MAX_UID);

  fd = creat(filename, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }
  
  f = fdopen(fd, "w");
  if (f == NULL) {
    close(fd);
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close;
  }

  r = mail_serialize_clear(mmapstr, &cur_token);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_mmapstr;
  }

  r = mailimf_cache_int_write(mmapstr, &cur_token,
      ancestor_data->mh_cur_folder->fl_max_index);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_mmapstr;
  }

  r = fwrite(mmapstr->str, 1, mmapstr->len, f);
  if ((size_t) r != mmapstr->len) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }

  mmap_string_free(mmapstr);
  fclose(f);

  return MAIL_NO_ERROR;

 free_mmapstr:
  mmap_string_free(mmapstr);
 close:
  fclose(f);
 err:
  return res;
}
Пример #25
0
static int nntpdriver_cached_status_folder(mailsession * session,
    const char * mb, uint32_t * result_messages, uint32_t * result_recent,
    uint32_t * result_unseen)
{
  int res;
  struct nntp_cached_session_state_data * cached_data;
  struct nntp_session_state_data * ancestor_data;
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  uint32_t i;
  int r;
  uint32_t recent;
  uint32_t unseen;
  uint32_t first;
  uint32_t last;
  uint32_t count;
  uint32_t additionnal;

  r = nntpdriver_cached_select_folder(session, mb);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }

  read_article_seq(session, &first, &last);

  count = 0;
  recent = 0;
  unseen = 0;
  
  ancestor_data = get_ancestor_data(session);
  cached_data = get_cached_data(session);
  if (ancestor_data->nntp_group_name == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  if (ancestor_data->nntp_group_info->grp_first > first)
    first = ancestor_data->nntp_group_info->grp_first;
  if (last < first)
    last = ancestor_data->nntp_group_info->grp_last;

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }

  for(i = first ; i <= last ; i++) {
    struct mail_flags * flags;
    
    r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
        i, &flags);
    if (r == MAIL_NO_ERROR) {
      if ((flags->fl_flags & MAIL_FLAG_CANCELLED) != 0) {
        mail_flags_free(flags);
        continue;
      }
      
      count ++;
      if ((flags->fl_flags & MAIL_FLAG_NEW) != 0) {
	recent ++;
      }
      if ((flags->fl_flags & MAIL_FLAG_SEEN) == 0) {
	unseen ++;
      }
      mail_flags_free(flags);
    }
  }

  if ((count == 0) && (first != last)) {
    count = last - first + 1;
    recent = count;
    unseen = count;
  }
  
  additionnal = ancestor_data->nntp_group_info->grp_last - last;
  recent += additionnal;
  unseen += additionnal;
  
  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  * result_messages = count;
  * result_recent = recent;
  * result_unseen = unseen;
  
  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Пример #26
0
static int mhdriver_cached_status_folder(mailsession * session, const char * mb,
    uint32_t * result_messages,
    uint32_t * result_recent,
    uint32_t * result_unseen)
{
  struct mailmh_folder * folder;
  int res;
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  struct mh_cached_session_state_data * cached_data;
  unsigned int i;
  int r;
  uint32_t count;
  uint32_t recent;
  uint32_t unseen;

  r = mhdriver_cached_select_folder(session, mb);
  if (r != MAIL_NO_ERROR) {
    res = r;
    goto err;
  }
  
  count = 0;
  recent = 0;
  unseen = 0;
  
  folder = get_mh_cur_folder(session);
  if (folder == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  cached_data = get_cached_data(session);
  if (cached_data->mh_quoted_mb == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->mh_flags_directory,
      cached_data->mh_quoted_mb, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto err;
  }

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto close_db_flags;
  }

  for(i = 0 ; i < carray_count(folder->fl_msgs_tab) ; i++) {
    struct mailmh_msg_info * mh_info;
    struct mail_flags * flags;
    
    mh_info = carray_get(folder->fl_msgs_tab, i);
    if (mh_info == NULL)
      continue;

    count ++;

    r = mhdriver_get_cached_flags(cache_db_flags, mmapstr,
        session, mh_info->msg_index, 
        &flags);

    if (r != MAIL_NO_ERROR) {
      recent ++;
      unseen ++;
      continue;
    }
    
    if ((flags->fl_flags & MAIL_FLAG_NEW) != 0) {
      recent ++;
    }
    if ((flags->fl_flags & MAIL_FLAG_SEEN) == 0) {
      unseen ++;
    }
    mail_flags_free(flags);
  }
  
  mmap_string_free(mmapstr);
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);

  * result_messages = count;
  * result_recent = recent;
  * result_unseen = unseen;
  
  return MAIL_NO_ERROR;

 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Пример #27
0
static int
nntpdriver_cached_get_envelopes_list(mailsession * session,
				     struct mailmessage_list * env_list)
{
  int r;
  unsigned int i;
  struct nntp_cached_session_state_data * cached_data;
  uint32_t first;
  uint32_t last;
  struct nntp_session_state_data * ancestor_data;
  char filename_env[PATH_MAX];
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_env;
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  int res;
  char cache_dir[PATH_MAX];

  cached_data = get_cached_data(session);
  ancestor_data = get_ancestor_data(session);

  nntp_flags_store_process(cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name,
      cached_data->nntp_flags_store);

  if (ancestor_data->nntp_group_name == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  /* read articles sequence */

  read_article_seq(session, &first, &last);

  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }

  snprintf(filename_env, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_cache_directory,
      ancestor_data->nntp_group_name, ENV_NAME);

  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->nntp_flags_directory,
      ancestor_data->nntp_group_name, FLAGS_NAME);

  /* fill with cached */
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {   
    mailmessage * msg;
    struct mailimf_fields * fields;

    msg = carray_get(env_list->msg_tab, i);

    if ((msg->msg_index < first) || (msg->msg_index > last))
      continue;

    if (msg->msg_fields == NULL) {
      r = get_cached_envelope(cache_db_env, mmapstr,
          session, msg->msg_index, &fields);
      if (r == MAIL_NO_ERROR) {
        msg->msg_fields = fields;
        msg->msg_cached = TRUE;
      }
    }
  }
  
  mail_cache_db_close_unlock(filename_env, cache_db_env);

  r = mailsession_get_envelopes_list(get_ancestor(session), env_list);

  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_mmapstr;
  }

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  /* add flags */
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);
    
    if (msg->msg_flags == NULL) {
      struct mail_flags * flags;
      
      r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
          msg->msg_index, &flags);
      if (r == MAIL_NO_ERROR) {
	msg->msg_flags = flags;
      }
      else {
        msg->msg_flags = mail_flags_new_empty();
        if (msg->msg_fields == NULL) {
          msg->msg_flags->fl_flags |= MAIL_FLAG_CANCELLED;
          mailmessage_check(msg);
        }
      }
    }
  }
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  
  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto close_db_env;
  }

  /* must write cache */

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_fields != NULL) {
      if (!msg->msg_cached) {
	r = write_cached_envelope(cache_db_env, mmapstr,
            session, msg->msg_index, msg->msg_fields);
      }
    }

    if (msg->msg_flags != NULL) {
      r = nntpdriver_write_cached_flags(cache_db_flags, mmapstr,
          msg->msg_index, msg->msg_flags);
    }
  }

  first = 0;
  last = 0;
  if (carray_count(env_list->msg_tab) > 0) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, 0);
    first = msg->msg_index;

    msg = carray_get(env_list->msg_tab, carray_count(env_list->msg_tab) - 1);
    last = msg->msg_index;
  }

  /* write articles sequence */

  write_article_seq(session, first, last);

  /* flush cache */
  
  maildriver_cache_clean_up(cache_db_env, cache_db_flags, env_list);
  
  /* remove cache files */
  
  snprintf(cache_dir, PATH_MAX, "%s/%s",
      cached_data->nntp_cache_directory, ancestor_data->nntp_group_name);
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  mail_cache_db_close_unlock(filename_env, cache_db_env);
  mmap_string_free(mmapstr);

  maildriver_message_cache_clean_up(cache_dir, env_list,
      get_uid_from_filename);

  return MAIL_NO_ERROR;

 close_db_env:
  mail_cache_db_close_unlock(filename_env, cache_db_env);
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}
Пример #28
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;
}
Пример #29
0
static int nntp_get_flags(mailmessage * msg_info,
    struct mail_flags ** result)
{
  int r;
  struct mail_flags * flags;
  struct mail_cache_db * cache_db_flags;
  char filename_flags[PATH_MAX];
  int res;
  MMAPString * mmapstr;

  if (msg_info->msg_flags != NULL) {
    * result = msg_info->msg_flags;
    
    return MAIL_NO_ERROR;
  }

  flags = mail_flags_store_get(get_cached_session_data(msg_info)->nntp_flags_store, msg_info->msg_index);

  if (flags == NULL) {
    struct nntp_cached_session_state_data * cached_data;
    struct nntp_session_state_data * ancestor_data;
    
    cached_data = get_cached_session_data(msg_info);
    
    ancestor_data = get_ancestor_session_data(msg_info);
    if (ancestor_data->nntp_group_name == NULL) {
      res = MAIL_ERROR_BAD_STATE;
      goto err;
    }
    
    snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
        cached_data->nntp_flags_directory,
        ancestor_data->nntp_group_name, FLAGS_NAME);
    
    r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
    if (r < 0) {
      res = MAIL_ERROR_FILE;
      goto err;
    }
    
    mmapstr = mmap_string_new("");
    if (mmapstr == NULL) {
      res = MAIL_ERROR_MEMORY;
      goto close_db_flags;
    }
    
    r = nntpdriver_get_cached_flags(cache_db_flags, mmapstr,
        msg_info->msg_index, &flags);
    if (r != MAIL_NO_ERROR) {
      flags = mail_flags_new_empty();
      if (flags == NULL) {
	res = MAIL_ERROR_MEMORY;
	goto free_mmapstr;
      }
    }

    mmap_string_free(mmapstr);
    mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  }

  msg_info->msg_flags = flags;

  * result = flags;

  return MAIL_NO_ERROR;

 free_mmapstr:
  mmap_string_free(mmapstr);
 close_db_flags:
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
 err:
  return res;
}
Пример #30
0
static int
mhdriver_cached_get_envelopes_list(mailsession * session,
				   struct mailmessage_list * env_list)
{
  int r;
  unsigned int i;
  char filename_env[PATH_MAX];
  char filename_flags[PATH_MAX];
  struct mail_cache_db * cache_db_env;
  struct mail_cache_db * cache_db_flags;
  MMAPString * mmapstr;
  int res;
  struct mh_cached_session_state_data * cached_data;

  cached_data = get_cached_data(session);
  if (cached_data->mh_quoted_mb == NULL) {
    res = MAIL_ERROR_BAD_STATE;
    goto err;
  }

  mh_flags_store_process(cached_data->mh_flags_directory,
      cached_data->mh_quoted_mb,
      cached_data->mh_flags_store);
  
  mmapstr = mmap_string_new("");
  if (mmapstr == NULL) {
    res = MAIL_ERROR_MEMORY;
    goto err;
  }
  
  snprintf(filename_env, PATH_MAX, "%s/%s/%s",
      cached_data->mh_cache_directory,
      cached_data->mh_quoted_mb, ENV_NAME);
  
  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }

  snprintf(filename_flags, PATH_MAX, "%s/%s/%s",
      cached_data->mh_flags_directory, cached_data->mh_quoted_mb, FLAGS_NAME);

  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto close_db_env;
  }

  /* fill with cached */

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {   
    mailmessage * msg;
    struct mailimf_fields * fields;
    struct mail_flags * flags;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_fields == NULL) {
      r = get_cached_envelope(cache_db_env, mmapstr,
          msg->msg_session, msg->msg_index, &fields);
      if (r == MAIL_NO_ERROR) {
	msg->msg_cached = TRUE;
	msg->msg_fields = fields;
      }
    }
      
    if (msg->msg_flags == NULL) {
      r = mhdriver_get_cached_flags(cache_db_flags, mmapstr,
          session, msg->msg_index, &flags);
      if (r == MAIL_NO_ERROR) {
	msg->msg_flags = flags;
      }
    }
  }
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  mail_cache_db_close_unlock(filename_env, cache_db_env);

  r = mailsession_get_envelopes_list(get_ancestor(session), env_list);

  if (r != MAIL_NO_ERROR) {
    res = r;
    goto free_mmapstr;
  }
  
  r = mail_cache_db_open_lock(filename_env, &cache_db_env);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto free_mmapstr;
  }
  
  r = mail_cache_db_open_lock(filename_flags, &cache_db_flags);
  if (r < 0) {
    res = MAIL_ERROR_FILE;
    goto close_db_env;
  }
  
  /* add flags */
  
  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_flags == NULL)
      msg->msg_flags = mail_flags_new_empty();
  }

  /* must write cache */

  for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) {
    mailmessage * msg;

    msg = carray_get(env_list->msg_tab, i);

    if (msg->msg_fields != NULL) {
      if (!msg->msg_cached) {
	r = write_cached_envelope(cache_db_env, mmapstr,
            session, msg->msg_index, msg->msg_fields);
      }
    }
    
    if (msg->msg_flags != NULL) {
      r = mhdriver_write_cached_flags(cache_db_flags, mmapstr,
          msg->msg_uid, msg->msg_flags);
    }
  }

  /* flush cache */
  
  maildriver_cache_clean_up(cache_db_env, cache_db_flags, env_list);
  
  mail_cache_db_close_unlock(filename_flags, cache_db_flags);
  mail_cache_db_close_unlock(filename_env, cache_db_env);
  
  mmap_string_free(mmapstr);

  return MAIL_NO_ERROR;

 close_db_env:
  mail_cache_db_close_unlock(filename_env, cache_db_env);
 free_mmapstr:
  mmap_string_free(mmapstr);
 err:
  return res;
}