int mailmh_folder_add_subfolder(struct mailmh_folder * parent, const char * name) { char * foldername; int r; struct mailmh_folder * folder; unsigned int array_index; chashdatum key; chashdatum data; foldername = malloc(strlen(parent->fl_filename) + strlen(name) + 2); if (foldername == NULL) return MAILMH_ERROR_MEMORY; strcpy(foldername, parent->fl_filename); strcat(foldername, MAIL_DIR_SEPARATOR_S); strcat(foldername, name); #ifdef WIN32 r = mkdir(foldername); #else r = mkdir(foldername, 0700); #endif free(foldername); if (r < 0) return MAILMH_ERROR_FOLDER; folder = mailmh_folder_new(parent, name); if (folder == NULL) return MAILMH_ERROR_MEMORY; r = carray_add(parent->fl_subfolders_tab, folder, &array_index); if (r < 0) { mailmh_folder_free(folder); return MAILMH_ERROR_MEMORY; } folder->fl_array_index = array_index; key.data = folder->fl_filename; key.len = strlen(folder->fl_filename); data.data = folder; data.len = 0; r = chash_set(parent->fl_subfolders_hash, &key, &data, NULL); if (r < 0) { carray_delete_fast(folder->fl_subfolders_tab, folder->fl_array_index); mailmh_folder_free(folder); return MAILMH_ERROR_MEMORY; } return MAILMH_NO_ERROR; }
LIBETPAN_EXPORT int mailfolder_add_child(struct mailfolder * parent, struct mailfolder * child) { unsigned int indx; int r; r = carray_add(parent->fld_children, child, &indx); if (r < 0) return MAIL_ERROR_MEMORY; child->fld_sibling_index = indx; child->fld_parent = parent; return MAIL_NO_ERROR; }
int mailmbox_append_message_uid(struct mailmbox_folder * folder, const char * data, size_t len, unsigned int * puid) { carray * tab; struct mailmbox_append_info * append_info; int res; int r; tab = carray_new(1); if (tab == NULL) { res = MAILMBOX_ERROR_MEMORY; goto err; } append_info = mailmbox_append_info_new(data, len); if (append_info == NULL) { res = MAILMBOX_ERROR_MEMORY; goto free_list; } r = carray_add(tab, append_info, NULL); if (r < 0) { res = MAILMBOX_ERROR_MEMORY; goto free_append_info; } r = mailmbox_append_message_list(folder, tab); if (puid != NULL) * puid = append_info->ai_uid; mailmbox_append_info_free(append_info); carray_free(tab); return r; free_append_info: mailmbox_append_info_free(append_info); free_list: carray_free(tab); err: return res; }
int mailmh_folder_rename_subfolder(struct mailmh_folder * src_folder, struct mailmh_folder * dst_folder, const char * new_name) { int r; struct mailmh_folder * folder; struct mailmh_folder * parent; char * new_foldername; parent = src_folder->fl_parent; if (parent == NULL) return MAILMH_ERROR_RENAME; new_foldername = malloc(strlen(dst_folder->fl_filename) + 2 + strlen(new_name)); if (new_foldername == NULL) return MAILMH_ERROR_MEMORY; strcpy(new_foldername, dst_folder->fl_filename); strcat(new_foldername, MAIL_DIR_SEPARATOR_S); strcat(new_foldername, new_name); r = rename(src_folder->fl_filename, new_foldername); free(new_foldername); if (r < 0) return MAILMH_ERROR_RENAME; r = mailmh_folder_remove_subfolder(src_folder); if (r != MAILMH_NO_ERROR) return r; folder = mailmh_folder_new(dst_folder, new_name); if (folder == NULL) return MAILMH_ERROR_MEMORY; r = carray_add(parent->fl_subfolders_tab, folder, NULL); if (r < 0) { mailmh_folder_free(folder); return MAILMH_ERROR_MEMORY; } return MAILMH_NO_ERROR; }
carray * mailstream_low_ssl_get_certificate_chain(mailstream_low * s) { #ifdef USE_SSL #ifndef USE_GNUTLS STACK_OF(X509) * skx; struct mailstream_ssl_data * ssl_data; carray * result; int skpos; ssl_data = (struct mailstream_ssl_data *) s->data; if (!(skx = SSL_get_peer_cert_chain(ssl_data->ssl_conn))) { return NULL; } result = carray_new(4); for(skpos = 0 ; skpos < sk_num(skx) ; skpos ++) { X509 * x = (X509 *) sk_value(skx, skpos); unsigned char * p; MMAPString * str; int length = i2d_X509(x, NULL); str = mmap_string_sized_new(length); p = (unsigned char *) str->str; str->len = length; i2d_X509(x, &p); carray_add(result, str, NULL); } return result; #else /* TODO: GnuTLS implementation */ return NULL; #endif #else return NULL; #endif }
int mailmh_folder_update(struct mailmh_folder * folder) { DIR * d; struct dirent * ent; struct stat buf; char * mh_seq; char filename[PATH_MAX]; int res; int r; uint32_t max_index; unsigned int i; if (stat(folder->fl_filename, &buf) == -1) { res = MAILMH_ERROR_FOLDER; goto err; } if (folder->fl_mtime == buf.st_mtime) { res = MAILMH_NO_ERROR; goto err; } folder->fl_mtime = buf.st_mtime; d = opendir(folder->fl_filename); if (d == NULL) { res = MAILMH_ERROR_FOLDER; goto err; } max_index = 0; /* clear the message list */ for(i = 0 ; i < carray_count(folder->fl_msgs_tab) ; i ++) { struct mailmh_msg_info * msg_info; chashdatum key; msg_info = carray_get(folder->fl_msgs_tab, i); if (msg_info == NULL) continue; key.data = &msg_info->msg_index; key.len = sizeof(msg_info->msg_index); chash_delete(folder->fl_msgs_hash, &key, NULL); mailmh_msg_info_free(msg_info); } carray_set_size(folder->fl_msgs_tab, 0); do { uint32_t indx; ent = readdir(d); if (ent != NULL) { snprintf(filename, PATH_MAX, "%s%c%s", folder->fl_filename, MAIL_DIR_SEPARATOR, ent->d_name); if (stat(filename, &buf) == -1) continue; if (S_ISREG(buf.st_mode)) { indx = strtoul(ent->d_name, NULL, 10); if (indx != 0) { struct mailmh_msg_info * msg_info; unsigned int array_index; chashdatum key; chashdatum data; msg_info = mailmh_msg_info_new(indx, buf.st_size, buf.st_mtime); if (msg_info == NULL) { res = MAILMH_ERROR_MEMORY; goto closedir; } r = carray_add(folder->fl_msgs_tab, msg_info, &array_index); if (r < 0) { mailmh_msg_info_free(msg_info); res = MAILMH_ERROR_MEMORY; goto closedir; } msg_info->msg_array_index = array_index; if (indx > max_index) max_index = indx; key.data = &msg_info->msg_index; key.len = sizeof(msg_info->msg_index); data.data = msg_info; data.len = 0; r = chash_set(folder->fl_msgs_hash, &key, &data, NULL); if (r < 0) { carray_delete_fast(folder->fl_msgs_tab, msg_info->msg_array_index); mailmh_msg_info_free(msg_info); res = MAILMH_ERROR_MEMORY; goto closedir; } } } else if (S_ISDIR(buf.st_mode)) { struct mailmh_folder * subfolder; unsigned int array_index; chashdatum key; chashdatum data; if (ent->d_name[0] == '.') { if (ent->d_name[1] == 0) continue; if ((ent->d_name[1] == '.') && (ent->d_name[2] == 0)) continue; } key.data = ent->d_name; key.len = strlen(ent->d_name); r = chash_get(folder->fl_subfolders_hash, &key, &data); if (r < 0) { subfolder = mailmh_folder_new(folder, ent->d_name); if (subfolder == NULL) { res = MAILMH_ERROR_MEMORY; goto closedir; } r = carray_add(folder->fl_subfolders_tab, subfolder, &array_index); if (r < 0) { mailmh_folder_free(subfolder); res = MAILMH_ERROR_MEMORY; goto closedir; } subfolder->fl_array_index = array_index; key.data = subfolder->fl_filename; key.len = strlen(subfolder->fl_filename); data.data = subfolder; data.len = 0; r = chash_set(folder->fl_subfolders_hash, &key, &data, NULL); if (r < 0) { carray_delete_fast(folder->fl_subfolders_tab, subfolder->fl_array_index); mailmh_folder_free(subfolder); res = MAILMH_ERROR_MEMORY; goto closedir; } } } } } while (ent != NULL); folder->fl_max_index = max_index; mh_seq = malloc(strlen(folder->fl_filename) + 2 + sizeof(".mh_sequences")); if (mh_seq == NULL) { res = MAILMH_ERROR_MEMORY; goto closedir; } strcpy(mh_seq, folder->fl_filename); strcat(mh_seq, MAIL_DIR_SEPARATOR_S); strcat(mh_seq, ".mh_sequences"); if (stat(mh_seq, &buf) == -1) { int fd; fd = creat(mh_seq, S_IRUSR | S_IWUSR); if (fd != -1) close(fd); } free(mh_seq); closedir(d); return MAILMH_NO_ERROR; closedir: closedir(d); err: return res; }
int mailmbox_copy_msg_list(struct mailmbox_folder * dest_folder, struct mailmbox_folder * src_folder, carray * tab) { int r; int res; carray * append_tab; unsigned int i; r = mailmbox_validate_read_lock(src_folder); if (r != MAILMBOX_NO_ERROR) { res = r; goto err; } append_tab = carray_new(carray_count(tab)); if (append_tab == NULL) { res = MAILMBOX_ERROR_MEMORY; goto src_unlock; } for(i = 0 ; i < carray_count(tab) ; i ++) { struct mailmbox_append_info * append_info; char * data; size_t len; uint32_t uid; uid = * ((uint32_t *) carray_get(tab, i)); r = mailmbox_fetch_msg_no_lock(src_folder, uid, &data, &len); if (r != MAILMBOX_NO_ERROR) { res = r; goto free_list; } append_info = mailmbox_append_info_new(data, len); if (append_info == NULL) { res = MAILMBOX_ERROR_MEMORY; goto free_list; } r = carray_add(append_tab, append_info, NULL); if (r < 0) { mailmbox_append_info_free(append_info); res = MAILMBOX_ERROR_MEMORY; goto free_list; } } r = mailmbox_append_message_list(dest_folder, append_tab); if (r != MAILMBOX_NO_ERROR) { res = r; goto src_unlock; } for(i = 0 ; i < carray_count(append_tab) ; i ++) { struct mailmbox_append_info * append_info; append_info = carray_get(append_tab, i); mailmbox_append_info_free(append_info); } carray_free(append_tab); mailmbox_read_unlock(src_folder); return MAILMBOX_NO_ERROR; free_list: for(i = 0 ; i < carray_count(append_tab) ; i ++) { struct mailmbox_append_info * append_info; append_info = carray_get(append_tab, i); mailmbox_append_info_free(append_info); } carray_free(append_tab); src_unlock: mailmbox_read_unlock(src_folder); err: return res; }
static int mail_build_thread_none(char * default_from, struct mailmessage_list * env_list, struct mailmessage_tree ** result, int (* comp_func)(struct mailmessage_tree **, struct mailmessage_tree **)) { unsigned int i; carray * rootlist; struct mailmessage_tree * root; int res; int r; root = mailmessage_tree_new(NULL, (time_t) -1, NULL); if (root == NULL) { res = MAIL_ERROR_MEMORY; goto err; } rootlist = root->node_children; for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { mailmessage * msg; struct mailmessage_tree * env_tree; char * base_subject; time_t date; msg = carray_get(env_list->msg_tab, i); if (msg == NULL) continue; if (msg->msg_fields != NULL) { date = get_date(msg); env_tree = mailmessage_tree_new(NULL, date, msg); if (env_tree == NULL) { res = MAIL_ERROR_MEMORY; goto free; } /* set parent */ env_tree->node_parent = root; r = carray_add(rootlist, env_tree, NULL); if (r < 0) { mailmessage_tree_free(env_tree); res = MAIL_ERROR_MEMORY; goto free; } r = get_extracted_subject(default_from, env_tree, &base_subject); switch (r) { case MAIL_NO_ERROR: env_tree->node_base_subject = base_subject; break; case MAIL_ERROR_SUBJECT_NOT_FOUND: break; default: res = r; goto free; } } } if (comp_func == NULL) comp_func = mailthread_tree_timecomp; r = mail_thread_sort(root, comp_func, FALSE); if (r != MAIL_NO_ERROR) { res = r; goto free; } * result = root; return MAIL_NO_ERROR; free: mailmessage_tree_free_recursive(root); err: return res; }
static int get_messages_list(mailsession * session, struct mailmessage_list ** result) { int r; char key[PATH_MAX]; struct mail_cache_db * maildb; struct db_session_state_data * data; int res; carray * msglist; unsigned int i; carray * msgtab; struct mailmessage_list * driver_msglist; data = get_data(session); r = mail_cache_db_open_lock(data->db_filename, &maildb); if (r < 0) { res = MAIL_ERROR_FILE; goto err; } r = db_get_message_list(maildb, &msglist); if (r != MAIL_NO_ERROR) { res = r; goto close_db; } msgtab = carray_new(16); if (msgtab == NULL) { res = MAIL_ERROR_MEMORY; goto close_db; } for(i = 0 ; i < carray_count(msglist) ; i ++) { uint32_t msg_num; uint32_t * pmsg_num; mailmessage * msg; size_t size; pmsg_num = carray_get(msglist, i); msg_num = * pmsg_num; free(pmsg_num); carray_set(msglist, i, NULL); snprintf(key, sizeof(key), "%lu", (unsigned long) msg_num); r = mail_cache_db_get_size(maildb, key, strlen(key), &size); if (r < 0) { continue; } msg = mailmessage_new(); if (msg == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } r = mailmessage_init(msg, session, db_message_driver, msg_num, size); if (r != MAIL_NO_ERROR) { mailmessage_free(msg); res = r; goto free_list; } r = carray_add(msgtab, msg, NULL); if (r < 0) { mailmessage_free(msg); res = MAIL_ERROR_MEMORY; goto free_list; } } carray_free(msglist); driver_msglist = mailmessage_list_new(msgtab); if (driver_msglist == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } mail_cache_db_close_unlock(data->db_filename, maildb); * result = driver_msglist; return MAIL_NO_ERROR; free_list: for(i = 0 ; i < carray_count(msgtab) ; i ++) { mailmessage * msg; msg = carray_get(msgtab, i); mailmessage_free(msg); } carray_free(msgtab); for(i = 0 ; i < carray_count(msglist) ; i ++) { uint32_t * msg; msg = carray_get(msglist, i); if (msg != NULL) free(msg); } carray_free(msglist); close_db: mail_cache_db_close_unlock(data->db_filename, maildb); err: return res; }
static int append_message_flags(mailsession * session, const char * message, size_t size, struct mail_flags * flags) { carray * msglist; unsigned int i; uint32_t * msg; uint32_t num; char key_value[PATH_MAX]; MMAPString * mmapstr; struct mail_cache_db * maildb; struct db_session_state_data * data; size_t cur_token; struct mailimf_fields * fields; int r; int res; data = get_data(session); r = mail_cache_db_open_lock(data->db_filename, &maildb); if (r < 0) { res = MAIL_ERROR_FILE; goto err; } num = 0; r = db_get_next_msg_number(maildb, &num); if (r != MAIL_NO_ERROR) { res = r; goto err; } r = db_get_message_list(maildb, &msglist); if (r != MAIL_NO_ERROR) { res = r; goto close_db; } msg = malloc(sizeof(* msg)); if (msg == NULL) { res = MAIL_ERROR_MEMORY; goto free_msglist; } * msg = num; r = carray_add(msglist, msg, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; free(msg); goto free_msglist; } r = db_set_message_list(maildb, msglist); if (r != MAIL_NO_ERROR) { res = r; goto free_msglist; } /* free msglist */ for(i = 0 ; i < carray_count(msglist) ; i ++) { msg = carray_get(msglist, i); free(msg); } carray_free(msglist); snprintf(key_value, sizeof(key_value), "%lu", (unsigned long) num); r = mail_cache_db_put(maildb, key_value, strlen(key_value), message, size); if (r < 0) { res = MAIL_ERROR_FILE; goto close_db; } /* write envelope */ cur_token = 0; r = mailimf_envelope_fields_parse(message, size, &cur_token, &fields); if (r != MAILIMF_NO_ERROR) { res = MAIL_ERROR_PARSE; goto close_db; } mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto close_db; } cur_token = 0; r = mailimf_cache_fields_write(mmapstr, &cur_token, fields); if (r != MAIL_NO_ERROR) { res = r; mmap_string_free(mmapstr); goto close_db; } snprintf(key_value, sizeof(key_value), "%lu-envelope", (unsigned long) num); r = mail_cache_db_put(maildb, key_value, strlen(key_value), mmapstr->str, mmapstr->len); mmap_string_free(mmapstr); mailimf_fields_free(fields); /* write flags */ if (flags != NULL) { snprintf(key_value, sizeof(key_value), "%lu-flags", (unsigned long) num); mmapstr = mmap_string_new(""); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto close_db; } r = generic_cache_flags_write(maildb, mmapstr, key_value, flags); mmap_string_free(mmapstr); if (r != MAIL_NO_ERROR) { res = MAIL_ERROR_FILE; goto close_db; } } mail_cache_db_close_unlock(data->db_filename, maildb); return MAIL_NO_ERROR; free_msglist: for(i = 0 ; i < carray_count(msglist) ; i ++) { msg = carray_get(msglist, i); free(msg); } carray_free(msglist); close_db: mail_cache_db_close_unlock(data->db_filename, maildb); err: return res; }
static int db_get_message_list(struct mail_cache_db * maildb, carray ** p_msglist) { carray * msglist; void * serialized; size_t serialized_len; int r; char key_value[PATH_MAX]; int res; unsigned int i; msglist = carray_new(16); if (msglist == NULL) { res = MAIL_ERROR_MEMORY; goto err; } snprintf(key_value, sizeof(key_value), "message-list"); r = mail_cache_db_get(maildb, key_value, strlen(key_value), &serialized, &serialized_len); if (r >= 0) { MMAPString * mmapstr; size_t cur_token; /* collect message list */ mmapstr = mmap_string_new_len(serialized, serialized_len); if (mmapstr == NULL) { res = MAIL_ERROR_MEMORY; goto free_msglist; } cur_token = 0; do { uint32_t num; uint32_t * msg; r = mailimf_cache_int_read(mmapstr, &cur_token, &num); if (r != MAIL_NO_ERROR) break; msg = malloc(sizeof(* msg)); if (msg == NULL) { res = MAIL_ERROR_MEMORY; mmap_string_free(mmapstr); goto free_msglist; } * msg = num; r = carray_add(msglist, msg, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; free(msg); mmap_string_free(mmapstr); goto free_msglist; } } while (1); mmap_string_free(mmapstr); } * p_msglist = msglist; return MAIL_NO_ERROR; free_msglist: for(i = 0 ; i < carray_count(msglist) ; i ++) { uint32_t * msg; msg = carray_get(msglist, i); free(msg); } carray_free(msglist); err: return res; }
static carray * mailstream_low_cfstream_get_certificate_chain(mailstream_low * s) { #if HAVE_CFNETWORK struct mailstream_cfstream_data * cfstream_data; unsigned int i; carray * result; CFArrayRef certs; CFIndex count; cfstream_data = (struct mailstream_cfstream_data *) s->data; SecTrustRef secTrust = (SecTrustRef)CFReadStreamCopyProperty(cfstream_data->readStream, kCFStreamPropertySSLPeerTrust); if (secTrust) { // SecTrustEvaluate() needs to be called before SecTrustGetCertificateCount() in Mac OS X <= 10.8 SecTrustEvaluate(secTrust, NULL); count = SecTrustGetCertificateCount(secTrust); result = carray_new(4); for(i = 0 ; i < count ; i ++) { SecCertificateRef cert = (SecCertificateRef) SecTrustGetCertificateAtIndex(secTrust, i); CFDataRef data = SecCertificateCopyData(cert); if (data == NULL) { carray_free(result); CFRelease(secTrust); return NULL; } CFIndex length = CFDataGetLength(data); const UInt8 * bytes = CFDataGetBytePtr(data); MMAPString * str = mmap_string_sized_new(length); mmap_string_append_len(str, (char*) bytes, length); carray_add(result, str, NULL); CFRelease(data); } CFRelease(secTrust); } else { certs = CFReadStreamCopyProperty(cfstream_data->readStream, kCFStreamPropertySSLPeerCertificates); if (certs) { count = CFArrayGetCount(certs); result = carray_new(4); for(i = 0 ; i < count ; i ++) { SecCertificateRef cert = (SecCertificateRef) CFArrayGetValueAtIndex(certs, i); CFDataRef data = SecCertificateCopyData(cert); if (data == NULL) { carray_free(result); CFRelease(certs); return NULL; } CFIndex length = CFDataGetLength(data); const UInt8 * bytes = CFDataGetBytePtr(data); MMAPString * str = mmap_string_sized_new(length); mmap_string_append_len(str, (char*) bytes, length); carray_add(result, str, NULL); CFRelease(data); } CFRelease(certs); } else { return NULL; } } return result; #else return NULL; #endif }
int claws_mailmbox_msg_info_update(struct claws_mailmbox_folder * folder, size_t msg_start, size_t msg_start_len, size_t msg_headers, size_t msg_headers_len, size_t msg_body, size_t msg_body_len, size_t msg_size, size_t msg_padding, uint32_t msg_uid) { struct claws_mailmbox_msg_info * info; int res; chashdatum key; chashdatum data; int r; key.data = &msg_uid; key.len = sizeof(msg_uid); r = chash_get(folder->mb_hash, &key, &data); if (r < 0) { unsigned int index; info = claws_mailmbox_msg_info_new(msg_start, msg_start_len, msg_headers, msg_headers_len, msg_body, msg_body_len, msg_size, msg_padding, msg_uid); if (info == NULL) { res = MAILMBOX_ERROR_MEMORY; goto err; } r = carray_add(folder->mb_tab, info, &index); if (r < 0) { claws_mailmbox_msg_info_free(info); res = MAILMBOX_ERROR_MEMORY; goto err; } if (msg_uid != 0) { chashdatum key; chashdatum data; key.data = &msg_uid; key.len = sizeof(msg_uid); data.data = info; data.len = 0; r = chash_set(folder->mb_hash, &key, &data, NULL); if (r < 0) { claws_mailmbox_msg_info_free(info); carray_delete(folder->mb_tab, index); res = MAILMBOX_ERROR_MEMORY; goto err; } } info->msg_index = index; } else { info = data.data; info->msg_start = msg_start; info->msg_start_len = msg_start_len; info->msg_headers = msg_headers; info->msg_headers_len = msg_headers_len; info->msg_body = msg_body; info->msg_body_len = msg_body_len; info->msg_size = msg_size; info->msg_padding = msg_padding; } return MAILMBOX_NO_ERROR; err: return res; }
static int mail_build_thread_references(char * default_from, struct mailmessage_list * env_list, struct mailmessage_tree ** result, int use_subject, int (* comp_func)(struct mailmessage_tree **, struct mailmessage_tree **)) { int r; int res; chash * msg_id_hash; unsigned int cur; struct mailmessage_tree * root; carray * rootlist; carray * msg_list; unsigned int i; chash * subject_hash; msg_id_hash = chash_new(128, CHASH_COPYNONE); if (msg_id_hash == NULL) { res = MAIL_ERROR_MEMORY; goto err; } root = mailmessage_tree_new(NULL, (time_t) -1, NULL); if (root == NULL) { res = MAIL_ERROR_MEMORY; goto free_hash; } rootlist = root->node_children; msg_list = carray_new(128); if (msg_list == NULL) { res = MAIL_ERROR_MEMORY; goto free_root; } /* collect message-ID */ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { mailmessage * msg; char * msgid; struct mailmessage_tree * env_tree; chashdatum hashkey; chashdatum hashdata; chashdatum hashold; time_t date; msg = carray_get(env_list->msg_tab, i); if (msg == NULL) continue; if (msg->msg_fields != NULL) { msgid = get_msg_id(msg); if (msgid == NULL) { msgid = mailimf_get_message_id(); } else { hashkey.data = msgid; hashkey.len = strlen(msgid); if (chash_get(msg_id_hash, &hashkey, &hashdata) == 0) msgid = mailimf_get_message_id(); else msgid = strdup(msgid); } if (msgid == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } date = get_date(msg); env_tree = mailmessage_tree_new(msgid, date, msg); if (env_tree == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } r = carray_add(msg_list, env_tree, NULL); if (r < 0) { mailmessage_tree_free(env_tree); res = MAIL_ERROR_MEMORY; goto free_list; } hashkey.data = msgid; hashkey.len = strlen(msgid); hashdata.data = env_tree; hashdata.len = 0; r = chash_set(msg_id_hash, &hashkey, &hashdata, &hashold); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_list; } } } /* (1) for all messages */ for(cur = 0 ; cur < carray_count(msg_list) ; cur ++) { struct mailmessage_tree * env_tree; mailmessage * msg; clist * ref; env_tree = carray_get(msg_list, cur); msg = env_tree->node_msg; ref = NULL; if (msg != NULL) { ref = get_ref(msg); if (ref == NULL) ref = get_in_reply_to(msg); } /* (A) Using the Message IDs in the message's references, link the corresponding messages (those whose Message-ID header line contains the given reference Message ID) together as parent/child. */ if (ref != NULL) { /* try to start a tree */ clistiter * cur_ref; chashdatum hashkey; chashdatum hashdata; chashdatum hashold; struct mailmessage_tree * env_cur_tree; struct mailmessage_tree * last_env_cur_tree; env_cur_tree = NULL; for(cur_ref = clist_begin(ref) ; cur_ref != NULL ; cur_ref = clist_next(cur_ref)) { char * msgid; last_env_cur_tree = env_cur_tree; msgid = clist_content(cur_ref); hashkey.data = msgid; hashkey.len = strlen(msgid); r = chash_get(msg_id_hash, &hashkey, &hashdata); if (r < 0) { /* not found, create a dummy message */ msgid = strdup(msgid); if (msgid == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } env_cur_tree = mailmessage_tree_new(msgid, (time_t) -1, NULL); if (env_cur_tree == NULL) { free(msgid); res = MAIL_ERROR_MEMORY; goto free_list; } r = carray_add(msg_list, env_cur_tree, NULL); if (r < 0) { mailmessage_tree_free(env_cur_tree); res = MAIL_ERROR_MEMORY; goto free_list; } hashkey.data = msgid; hashkey.len = strlen(msgid); hashdata.data = env_cur_tree; hashdata.len = 0; r = chash_set(msg_id_hash, &hashkey, &hashdata, &hashold); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_list; } } else { env_cur_tree = hashdata.data; } if (last_env_cur_tree != NULL) { if (env_cur_tree->node_parent == NULL) { /* make it one child */ if (env_cur_tree != last_env_cur_tree) { if (!is_descendant(env_cur_tree, last_env_cur_tree)) { /* set parent */ env_cur_tree->node_parent = last_env_cur_tree; r = carray_add(last_env_cur_tree->node_children, env_cur_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_list; } } } } } } /* (B) Create a parent/child link between the last reference (or NIL if there are no references) and the current message. If the current message already has a parent, it is probably the result of a truncated References header line, so break the current parent/child link before creating the new correct one. */ last_env_cur_tree = env_cur_tree; if (last_env_cur_tree != NULL) { if (env_tree->node_parent == NULL) { if (last_env_cur_tree != env_tree) { if (!is_descendant(env_tree, last_env_cur_tree)) { /* set parent */ env_tree->node_parent = last_env_cur_tree; r = carray_add(last_env_cur_tree->node_children, env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_list; } } } } } } } chash_free(msg_id_hash); msg_id_hash = NULL; /* (2) Gather together all of the messages that have no parents and make them all children (siblings of one another) of a dummy parent (the "root"). */ for(cur = 0 ; cur < carray_count(msg_list) ; cur ++) { struct mailmessage_tree * env_tree; env_tree = carray_get(msg_list, cur); if (env_tree->node_parent == NULL) { r = carray_add(rootlist, env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_list; } /* set parent */ env_tree->node_parent = root; } } carray_free(msg_list); msg_list = NULL; /* (3) Prune dummy messages from the thread tree. */ cur = 0; while (cur < carray_count(rootlist)) { r = delete_dummy(rootlist, rootlist, cur, &cur); if (r != MAIL_NO_ERROR) { res = r; goto free_list; } } /* (4) Sort the messages under the root (top-level siblings only) by sent date. */ r = mail_thread_sort(root, mailthread_tree_timecomp, FALSE); if (r != MAIL_NO_ERROR) { res = r; goto free_list; } if (use_subject) { /* (5) Gather together messages under the root that have the same extracted subject text. (A) Create a table for associating extracted subjects with messages. */ subject_hash = chash_new(128, CHASH_COPYVALUE); if (subject_hash == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } /* (B) Populate the subject table with one message per extracted subject. For each child of the root: */ for(cur = 0 ; cur < carray_count(rootlist) ; cur ++) { struct mailmessage_tree * env_tree; chashdatum key; chashdatum data; char * base_subject; env_tree = carray_get(rootlist, cur); /* (i) Find the subject of this thread by extracting the base subject from the current message, or its first child if the current message is a dummy. */ r = get_thread_subject(default_from, env_tree, &base_subject); /* (ii) If the extracted subject is empty, skip this message. */ if (r == MAIL_ERROR_SUBJECT_NOT_FOUND) { /* no subject found */ continue; } else if (r == MAIL_NO_ERROR) { if (* base_subject == '\0') { /* subject empty */ free(base_subject); continue; } else { /* do nothing */ } } else { res = r; goto free_subject_hash; } env_tree->node_base_subject = base_subject; /* (iii) Lookup the message associated with this extracted subject in the table. */ key.data = base_subject; key.len = strlen(base_subject); r = chash_get(subject_hash, &key, &data); if (r < 0) { /* (iv) If there is no message in the table with this subject, add the current message and the extracted subject to the subject table. */ data.data = &cur; data.len = sizeof(cur); r = chash_set(subject_hash, &key, &data, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } } else { /* Otherwise, replace the message in the table with the current message if the message in the table is not a dummy AND either of the following criteria are true: The current message is a dummy, OR The message in the table is a reply or forward (its original subject contains a subj-refwd part and/or a "(fwd)" subj-trailer) and the current message is not. */ struct mailmessage_tree * msg_in_table; unsigned int * iter_in_table; int replace; iter_in_table = data.data; msg_in_table = carray_get(rootlist, cur); replace = FALSE; /* message is dummy if info is NULL */ if (msg_in_table->node_msg != NULL) { if (env_tree->node_msg == NULL) replace = TRUE; else { if (env_tree->node_is_reply && !env_tree->node_is_reply) replace = TRUE; } } if (replace) { data.data = &cur; data.len = sizeof(cur); r = chash_set(subject_hash, &key, &data, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } } } } /* (C) Merge threads with the same subject. For each child of the root: */ cur = 0; while (cur < carray_count(rootlist)) { struct mailmessage_tree * env_tree; chashdatum key; chashdatum data; struct mailmessage_tree * main_tree; unsigned int * main_cur; env_tree = carray_get(rootlist, cur); if (env_tree == NULL) goto next_msg; /* (i) Find the subject of this thread as in step 4.B.i above. */ /* already done in tree->node_base_subject */ /* (ii) If the extracted subject is empty, skip this message. */ if (env_tree->node_base_subject == NULL) goto next_msg; if (* env_tree->node_base_subject == '\0') goto next_msg; /* (iii) Lookup the message associated with this extracted subject in the table. */ key.data = env_tree->node_base_subject; key.len = strlen(env_tree->node_base_subject); r = chash_get(subject_hash, &key, &data); if (r < 0) goto next_msg; /* (iv) If the message in the table is the current message, skip this message. */ main_cur = data.data; if (* main_cur == cur) goto next_msg; /* Otherwise, merge the current message with the one in the table using the following rules: */ main_tree = carray_get(rootlist, * main_cur); /* If both messages are dummies, append the current message's children to the children of the message in the table (the children of both messages become siblings), and then delete the current message. */ if ((env_tree->node_msg == NULL) && (main_tree->node_msg == NULL)) { unsigned int old_size; old_size = carray_count(main_tree->node_children); r = carray_set_size(main_tree->node_children, old_size + carray_count(env_tree->node_children)); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } for(i = 0 ; i < carray_count(env_tree->node_children) ; i ++) { struct mailmessage_tree * child; child = carray_get(env_tree->node_children, i); carray_set(main_tree->node_children, old_size + i, child); /* set parent */ child->node_parent = main_tree; } carray_set_size(env_tree->node_children, 0); /* this is the only case where children can be NULL, this is before freeing it */ mailmessage_tree_free(env_tree); carray_delete_fast(rootlist, cur); } /* If the message in the table is a dummy and the current message is not, make the current message a child of the message in the table (a sibling of it's children). */ else if (main_tree->node_msg == NULL) { r = carray_add(main_tree->node_children, env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } /* set parent */ env_tree->node_parent = main_tree; carray_delete_fast(rootlist, cur); } /* If the current message is a reply or forward and the message in the table is not, make the current message a child of the message in the table (a sibling of it's children). */ else if (env_tree->node_is_reply && !main_tree->node_is_reply) { r = carray_add(main_tree->node_children, env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } /* set parent */ env_tree->node_parent = main_tree; carray_delete_fast(rootlist, cur); } /* Otherwise, create a new dummy message and make both the current message and the message in the table children of the dummy. Then replace the message in the table with the dummy message. Note: Subject comparisons are case-insensitive, as described under "Internationalization Considerations." */ else { struct mailmessage_tree * new_main_tree; char * base_subject; unsigned int last; new_main_tree = mailmessage_tree_new(NULL, (time_t) -1, NULL); if (new_main_tree == NULL) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } /* main_tree->node_base_subject is never NULL */ base_subject = strdup(main_tree->node_base_subject); if (base_subject == NULL) { mailmessage_tree_free(new_main_tree); res = MAIL_ERROR_MEMORY; goto free_subject_hash; } new_main_tree->node_base_subject = base_subject; r = carray_add(rootlist, new_main_tree, &last); if (r < 0) { mailmessage_tree_free(new_main_tree); res = MAIL_ERROR_MEMORY; goto free_subject_hash; } r = carray_add(new_main_tree->node_children, main_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } /* set parent */ main_tree->node_parent = new_main_tree; carray_delete_fast(rootlist, * main_cur); r = carray_add(new_main_tree->node_children, env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } /* set parent */ env_tree->node_parent = new_main_tree; carray_delete_fast(rootlist, cur); data.data = &last; data.len = sizeof(last); r = chash_set(subject_hash, &key, &data, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free_subject_hash; } } continue; next_msg: cur ++; continue; } i = 0; for(cur = 0 ; cur < carray_count(rootlist) ; cur ++) { struct mailmessage_tree * env_tree; env_tree = carray_get(rootlist, cur); if (env_tree == NULL) continue; carray_set(rootlist, i, env_tree); i ++; } carray_set_size(rootlist, i); chash_free(subject_hash); } /* (6) Traverse the messages under the root and sort each set of siblings by sent date. Traverse the messages in such a way that the "youngest" set of siblings are sorted first, and the "oldest" set of siblings are sorted last (grandchildren are sorted before children, etc). In the case of an exact match on sent date or if either of the Date: headers used in a comparison can not be parsed, use the order in which the messages appear in the mailbox (that is, by sequence number) to determine the order. In the case of a dummy message (which can only occur with top-level siblings), use its first child for sorting. */ #if 0 if (comp_func != NULL) { r = mail_thread_sort(root, comp_func, TRUE); if (r != MAIL_NO_ERROR) { res = r; goto free_list; } } #endif if (comp_func == NULL) comp_func = mailthread_tree_timecomp; r = mail_thread_sort(root, comp_func, TRUE); if (r != MAIL_NO_ERROR) { res = r; goto free_list; } * result = root; return MAIL_NO_ERROR; free_subject_hash: chash_free(subject_hash); free_list: if (msg_list != NULL) { for(i = 0 ; i < carray_count(msg_list) ; i ++) mailmessage_tree_free(carray_get(msg_list, i)); carray_free(msg_list); } free_root: mailmessage_tree_free_recursive(root); free_hash: if (msg_id_hash != NULL) chash_free(msg_id_hash); err: return res; }
static int delete_dummy(carray * rootlist, carray * sibling_list, unsigned int cur, unsigned int * pnext) { struct mailmessage_tree * env_tree; int res; int r; unsigned int cur_child; unsigned int next; env_tree = carray_get(sibling_list, cur); cur_child = 0; while (cur_child < carray_count(env_tree->node_children)) { delete_dummy(rootlist, env_tree->node_children, cur_child, &cur_child); } if (env_tree->node_msg == NULL) { if (carray_count(env_tree->node_children) == 0) { /* If it is a dummy message with NO children, delete it. */ mailmessage_tree_free(env_tree); carray_delete(sibling_list, cur); next = cur; } else { /* If it is a dummy message with children, delete it, but promote its children to the current level. */ /* Do not promote the children if doing so would make them children of the root, unless there is only one child. */ cur_child = 0; if ((sibling_list != rootlist) || (carray_count(env_tree->node_children) == 1)) { while (cur_child < carray_count(env_tree->node_children)) { struct mailmessage_tree * child; child = carray_get(env_tree->node_children, cur_child); r = carray_add(sibling_list, child, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto err; } /* set new parent of the children */ child->node_parent = env_tree->node_parent; carray_delete(env_tree->node_children, cur_child); } mailmessage_tree_free(env_tree); carray_delete(sibling_list, cur); next = cur; } else next = cur + 1; } } else next = cur + 1; * pnext = next; return MAIL_NO_ERROR; err: return res; }
int mailmh_folder_add_message_uid(struct mailmh_folder * folder, const char * message, size_t size, uint32_t * pindex) { char * tmpname; int fd; size_t namesize; size_t left; ssize_t res; struct mailmh_msg_info * msg_info; uint32_t indx; int error; int r; unsigned int array_index; struct stat buf; chashdatum key; chashdatum data; namesize = strlen(folder->fl_filename) + 20; tmpname = malloc(namesize); snprintf(tmpname, namesize, "%s%ctmpXXXXXX", folder->fl_filename, MAIL_DIR_SEPARATOR); fd = mkstemp(tmpname); if (fd < 0) { error = MAILMH_ERROR_FILE; goto free; } left = size; while (left > 0) { res = write(fd, message, left); if (res == -1) { close(fd); error = MAILMH_ERROR_FILE; goto free; } left -= res; } close(fd); r = stat(tmpname, &buf); if (r < 0) { error = MAILMH_ERROR_FILE; goto free; } r = mailmh_folder_alloc_msg(folder, tmpname, &indx); if (r != MAILMH_NO_ERROR) { unlink(tmpname); error = MAILMH_ERROR_COULD_NOT_ALLOC_MSG; goto free; } free(tmpname); msg_info = mailmh_msg_info_new(indx, size, buf.st_mtime); if (msg_info == NULL) { mailmh_folder_remove_message(folder, indx); error = MAILMH_ERROR_MEMORY; goto err; } r = carray_add(folder->fl_msgs_tab, msg_info, &array_index); if (r < 0) { mailmh_folder_remove_message(folder, indx); mailmh_msg_info_free(msg_info); error = MAILMH_ERROR_MEMORY; goto err; } msg_info->msg_array_index = array_index; key.data = &indx; key.len = sizeof(indx); data.data = msg_info; data.len = 0; if (pindex != NULL) * pindex = indx; r = chash_set(folder->fl_msgs_hash, &key, &data, NULL); if (r < 0) { carray_delete_fast(folder->fl_msgs_tab, msg_info->msg_array_index); mailmh_msg_info_free(msg_info); error = MAILMH_ERROR_MEMORY; goto err; } return MAILMH_NO_ERROR; free: free(tmpname); err: return error; }
carray * mailstream_low_ssl_get_certificate_chain(mailstream_low * s) { #ifdef USE_SSL struct mailstream_ssl_data * ssl_data; carray * result; int skpos; #ifndef USE_GNUTLS STACK_OF(X509) * skx; ssl_data = (struct mailstream_ssl_data *) s->data; if (!(skx = SSL_get_peer_cert_chain(ssl_data->ssl_conn))) { return NULL; } result = carray_new(4); for(skpos = 0 ; skpos < sk_num((_STACK *) skx) ; skpos ++) { X509 * x = (X509 *) sk_value((_STACK *) skx, skpos); unsigned char * p; MMAPString * str; int length = i2d_X509(x, NULL); str = mmap_string_sized_new(length); p = (unsigned char *) str->str; str->len = length; i2d_X509(x, &p); carray_add(result, str, NULL); } return result; #else gnutls_session session = NULL; const gnutls_datum *raw_cert_list; unsigned int raw_cert_list_length; ssl_data = (struct mailstream_ssl_data *) s->data; session = ssl_data->session; raw_cert_list = gnutls_certificate_get_peers(session, &raw_cert_list_length); if (raw_cert_list && gnutls_certificate_type_get(session) == GNUTLS_CRT_X509) { result = carray_new(4); for(skpos = 0 ; skpos < raw_cert_list_length ; skpos ++) { gnutls_x509_crt cert = NULL; if (gnutls_x509_crt_init(&cert) >= 0 && gnutls_x509_crt_import(cert, &raw_cert_list[skpos], GNUTLS_X509_FMT_DER) >= 0) { size_t cert_size = 0; MMAPString * str = NULL; unsigned char * p; if (gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, NULL, &cert_size) == GNUTLS_E_SHORT_MEMORY_BUFFER) { str = mmap_string_sized_new(cert_size); p = (unsigned char *) str->str; str->len = cert_size; } if (str != NULL && gnutls_x509_crt_export(cert, GNUTLS_X509_FMT_DER, p, &cert_size) >= 0) { carray_add(result, str, NULL); } else { return NULL; } gnutls_x509_crt_deinit(cert); } } } return result; return NULL; #endif #else return NULL; #endif }
int nntp_get_messages_list(mailsession * nntp_session, mailsession * session, mailmessage_driver * driver, struct mailmessage_list ** result) { carray * tab; struct mailmessage_list * env_list; uint32_t i; int res; int r; struct nntp_session_state_data * data; struct newsnntp_group_info * group_info; uint32_t max; unsigned int cur; data = session_get_data(nntp_session); if (data->nntp_group_name == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } r = nntpdriver_select_folder(nntp_session, data->nntp_group_name); if (r != MAIL_NO_ERROR) { res = r; goto err; } group_info = data->nntp_group_info; if (group_info == NULL) { res = MAIL_ERROR_BAD_STATE; goto err; } max = group_info->grp_first; if (data->nntp_max_articles != 0) { if (group_info->grp_last - data->nntp_max_articles + 1 > max) max = group_info->grp_last - data->nntp_max_articles + 1; } tab = carray_new(128); if (tab == NULL) { res = MAIL_ERROR_MEMORY; goto err; } for(i = max ; i <= group_info->grp_last ; i++) { mailmessage * msg; msg = mailmessage_new(); if (msg == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } r = mailmessage_init(msg, session, driver, i, 0); if (r != MAIL_NO_ERROR) { mailmessage_free(msg); res = r; goto free_list; } r = carray_add(tab, msg, NULL); if (r < 0) { mailmessage_free(msg); res = MAIL_ERROR_MEMORY; goto free_list; } } env_list = mailmessage_list_new(tab); if (env_list == NULL) { res = MAIL_ERROR_MEMORY; goto free_list; } * result = env_list; return MAIL_NO_ERROR; free_list: for(cur = 0 ; cur < carray_count(tab) ; cur ++) mailmessage_free(carray_get(tab, cur)); carray_free(tab); err: return res; }
int newsfeed_add_item(struct newsfeed * feed, struct newsfeed_item * item) { return carray_add(feed->feed_item_list, item, NULL); }
static int mail_build_thread_orderedsubject(char * default_from, struct mailmessage_list * env_list, struct mailmessage_tree ** result, int (* comp_func)(struct mailmessage_tree **, struct mailmessage_tree **)) { unsigned int i; carray * rootlist; unsigned int cur; struct mailmessage_tree * root; int res; int r; struct mailmessage_tree * current_thread; root = mailmessage_tree_new(NULL, (time_t) -1, NULL); if (root == NULL) { res = MAIL_ERROR_MEMORY; goto err; } rootlist = root->node_children; /* The ORDEREDSUBJECT threading algorithm is also referred to as "poor man's threading." */ for(i = 0 ; i < carray_count(env_list->msg_tab) ; i ++) { mailmessage * msg; struct mailmessage_tree * env_tree; char * base_subject; time_t date; msg = carray_get(env_list->msg_tab, i); if (msg == NULL) continue; if (msg->msg_fields != NULL) { date = get_date(msg); env_tree = mailmessage_tree_new(NULL, date, msg); if (env_tree == NULL) { res = MAIL_ERROR_MEMORY; goto free; } /* set parent */ env_tree->node_parent = root; r = carray_add(rootlist, env_tree, NULL); if (r < 0) { mailmessage_tree_free(env_tree); res = MAIL_ERROR_MEMORY; goto free; } r = get_extracted_subject(default_from, env_tree, &base_subject); switch (r) { case MAIL_NO_ERROR: env_tree->node_base_subject = base_subject; break; case MAIL_ERROR_SUBJECT_NOT_FOUND: break; default: res = r; goto free; } } } /* The searched messages are sorted by subject and then by the sent date. */ r = mail_thread_sort(root, tree_subj_time_comp, FALSE); if (r != MAIL_NO_ERROR) { res = r; goto free; } /* The messages are then split into separate threads, with each thread containing messages with the same extracted subject text. */ current_thread = NULL; cur = 0; while (cur < carray_count(rootlist)) { struct mailmessage_tree * cur_env_tree; cur_env_tree = carray_get(rootlist, cur); if (current_thread == NULL) { current_thread = cur_env_tree; cur ++; continue; } if ((cur_env_tree->node_base_subject == NULL) || (current_thread->node_base_subject == NULL)) { current_thread = cur_env_tree; cur ++; continue; } if (strcmp(cur_env_tree->node_base_subject, current_thread->node_base_subject) == 0) { /* set parent */ cur_env_tree->node_parent = current_thread; r = carray_add(current_thread->node_children, cur_env_tree, NULL); if (r < 0) { res = MAIL_ERROR_MEMORY; goto free; } carray_delete(rootlist, cur); } else cur ++; current_thread = cur_env_tree; } /* Finally, the threads are sorted by the sent date of the first message in the thread. Note that each message in a thread is a child (as opposed to a sibling) of the previous message. */ #if 0 if (comp_func != NULL) { r = mail_thread_sort(root, comp_func, FALSE); if (r != MAIL_NO_ERROR) { res = r; goto free; } } #endif if (comp_func == NULL) comp_func = mailthread_tree_timecomp; r = mail_thread_sort(root, comp_func, FALSE); if (r != MAIL_NO_ERROR) { res = r; goto free; } * result = root; return MAIL_NO_ERROR; free: mailmessage_tree_free_recursive(root); err: return res; }