gint prefs_file_close(PrefFile *pfile) { FILE *fp, *orig_fp; gchar *path; gchar *tmppath; gchar *bakpath = NULL; gchar buf[BUFFSIZE]; cm_return_val_if_fail(pfile != NULL, -1); fp = pfile->fp; orig_fp = pfile->orig_fp; path = pfile->path; if (!pfile->writing) { fclose(fp); g_free(pfile); g_free(path); return 0; } if (orig_fp) { while (fgets(buf, sizeof(buf), orig_fp) != NULL) { /* next block */ if (buf[0] == '[') { if (fputs(buf, fp) == EOF) { g_warning("failed to write configuration to file\n"); prefs_file_close_revert(pfile); return -1; } break; } } while (fgets(buf, sizeof(buf), orig_fp) != NULL) if (fputs(buf, fp) == EOF) { g_warning("failed to write configuration to file\n"); prefs_file_close_revert(pfile); return -1; } fclose(orig_fp); } tmppath = g_strconcat(path, ".tmp", NULL); if (prefs_common_get_flush_metadata() && fsync(fileno(fp)) < 0) { FILE_OP_ERROR(tmppath, "fsync"); fclose(fp); claws_unlink(tmppath); g_free(path); g_free(tmppath); return -1; } if (fclose(fp) == EOF) { FILE_OP_ERROR(tmppath, "fclose"); claws_unlink(tmppath); g_free(path); g_free(tmppath); return -1; } if (is_file_exist(path)) { bakpath = g_strconcat(path, ".bak", NULL); #ifdef G_OS_WIN32 claws_unlink(bakpath); #endif if (g_rename(path, bakpath) < 0) { FILE_OP_ERROR(path, "rename"); claws_unlink(tmppath); g_free(path); g_free(tmppath); g_free(bakpath); return -1; } } #ifdef G_OS_WIN32 claws_unlink(path); #endif if (g_rename(tmppath, path) < 0) { FILE_OP_ERROR(tmppath, "rename"); claws_unlink(tmppath); g_free(path); g_free(tmppath); g_free(bakpath); return -1; } g_free(pfile); g_free(path); g_free(tmppath); g_free(bakpath); return 0; }
void mimeview_show_message(MimeView *mimeview, MimeInfo *mimeinfo, const gchar *file) { GtkTreeModel *model = GTK_TREE_MODEL(mimeview->store); GtkTreeIter iter; gboolean valid; mimeview_clear(mimeview); textview_clear(mimeview->messageview->textview); g_return_if_fail(file != NULL); g_return_if_fail(mimeinfo != NULL); #if USE_GPGME if (rfc2015_is_available() && prefs_common.auto_check_signatures) { FILE *fp; if ((fp = g_fopen(file, "rb")) == NULL) { FILE_OP_ERROR(file, "fopen"); return; } rfc2015_check_signature(mimeinfo, fp); fclose(fp); } else set_unchecked_signature(mimeinfo); #endif g_signal_handlers_block_by_func (G_OBJECT(mimeview->selection), G_CALLBACK(mimeview_selection_changed), mimeview); mimeview_set_multipart_tree(mimeview, mimeinfo, NULL); gtk_tree_view_expand_all(GTK_TREE_VIEW(mimeview->treeview)); g_signal_handlers_unblock_by_func (G_OBJECT(mimeview->selection), G_CALLBACK(mimeview_selection_changed), mimeview); /* search first text part */ for (valid = gtk_tree_model_get_iter_first(model, &iter); valid; valid = gtkut_tree_model_next(model, &iter)) { MimeInfo *partinfo; gtk_tree_model_get(model, &iter, COL_MIME_INFO, &partinfo, -1); if (partinfo && (partinfo->mime_type == MIME_TEXT || partinfo->mime_type == MIME_TEXT_HTML)) break; } textview_show_message(mimeview->messageview->textview, mimeinfo, file); if (!valid) valid = gtk_tree_model_get_iter_first(model, &iter); if (valid) { GtkTreePath *path; path = gtk_tree_model_get_path(model, &iter); gtk_tree_view_set_cursor(GTK_TREE_VIEW(mimeview->treeview), path, NULL, FALSE); gtk_tree_path_free(path); if (mimeview_get_selected_part(mimeview)) gtk_widget_grab_focus(mimeview->treeview); } }
gint procmsg_send_message_queue(const gchar *file) { static HeaderEntry qentry[] = {{"S:", NULL, FALSE}, {"SSV:", NULL, FALSE}, {"R:", NULL, FALSE}, {"NG:", NULL, FALSE}, {"MAID:", NULL, FALSE}, {"NAID:", NULL, FALSE}, {"SCF:", NULL, FALSE}, {"RMID:", NULL, FALSE}, {"FMID:", NULL, FALSE}, {"X-Sylpheed-Privacy-System:", NULL, FALSE}, {"X-Sylpheed-Encrypt:", NULL, FALSE}, {"X-Sylpheed-Encrypt-Data:", NULL, FALSE}, {NULL, NULL, FALSE}}; FILE *fp; gint filepos; gint mailval = 0, newsval = 0; gchar *from = NULL; gchar *smtpserver = NULL; GSList *to_list = NULL; GSList *newsgroup_list = NULL; gchar *savecopyfolder = NULL; gchar *replymessageid = NULL; gchar *fwdmessageid = NULL; gchar *privacy_system = NULL; gboolean encrypt = FALSE; gchar *encrypt_data = NULL; gchar buf[BUFFSIZE]; gint hnum; PrefsAccount *mailac = NULL, *newsac = NULL; gboolean save_clear_text = TRUE; gchar *tmp_enc_file = NULL; int local = 0; g_return_val_if_fail(file != NULL, -1); if ((fp = fopen(file, "rb")) == NULL) { FILE_OP_ERROR(file, "fopen"); return -1; } while ((hnum = procheader_get_one_field(buf, sizeof(buf), fp, qentry)) != -1) { gchar *p = buf + strlen(qentry[hnum].name); switch (hnum) { case Q_SENDER: if (from == NULL) from = g_strdup(p); break; case Q_SMTPSERVER: if (smtpserver == NULL) smtpserver = g_strdup(p); break; case Q_RECIPIENTS: to_list = address_list_append(to_list, p); break; case Q_NEWSGROUPS: newsgroup_list = newsgroup_list_append(newsgroup_list, p); break; case Q_MAIL_ACCOUNT_ID: mailac = account_find_from_id(atoi(p)); break; case Q_NEWS_ACCOUNT_ID: newsac = account_find_from_id(atoi(p)); break; case Q_SAVE_COPY_FOLDER: if (savecopyfolder == NULL) savecopyfolder = g_strdup(p); break; case Q_REPLY_MESSAGE_ID: if (replymessageid == NULL) replymessageid = g_strdup(p); break; case Q_FWD_MESSAGE_ID: if (fwdmessageid == NULL) fwdmessageid = g_strdup(p); break; case Q_PRIVACY_SYSTEM: if (privacy_system == NULL) privacy_system = g_strdup(p); break; case Q_ENCRYPT: if (p[0] == '1') encrypt = TRUE; break; case Q_ENCRYPT_DATA: if (encrypt_data == NULL) encrypt_data = g_strdup(p); break; } } filepos = ftell(fp); if (encrypt) { MimeInfo *mimeinfo; save_clear_text = (mailac != NULL && mailac->save_encrypted_as_clear_text); fclose(fp); fp = NULL; mimeinfo = procmime_scan_queue_file(file); if (!privacy_encrypt(privacy_system, mimeinfo, encrypt_data) || (fp = my_tmpfile()) == NULL || procmime_write_mimeinfo(mimeinfo, fp) < 0) { if (fp) fclose(fp); procmime_mimeinfo_free_all(mimeinfo); g_free(from); g_free(smtpserver); slist_free_strings(to_list); g_slist_free(to_list); slist_free_strings(newsgroup_list); g_slist_free(newsgroup_list); g_free(savecopyfolder); g_free(replymessageid); g_free(fwdmessageid); g_free(privacy_system); g_free(encrypt_data); return -1; } rewind(fp); if (!save_clear_text) { gchar *content = NULL; FILE *tmpfp = get_tmpfile_in_dir(get_mime_tmp_dir(), &tmp_enc_file); if (tmpfp) { fclose(tmpfp); content = file_read_stream_to_str(fp); rewind(fp); str_write_to_file(content, tmp_enc_file); g_free(content); } else { g_warning("couldn't get tempfile\n"); } } procmime_mimeinfo_free_all(mimeinfo); filepos = 0; } if (to_list) { debug_print("Sending message by mail\n"); if (!from) { g_warning("Queued message header is broken.\n"); mailval = -1; } else if (mailac && mailac->use_mail_command && mailac->mail_command && (* mailac->mail_command)) { mailval = send_message_local(mailac->mail_command, fp); local = 1; } else { if (!mailac) { mailac = account_find_from_smtp_server(from, smtpserver); if (!mailac) { g_warning("Account not found. " "Using current account...\n"); mailac = cur_account; } } if (mailac) mailval = send_message_smtp(mailac, to_list, fp); else { PrefsAccount tmp_ac; g_warning("Account not found.\n"); memset(&tmp_ac, 0, sizeof(PrefsAccount)); tmp_ac.address = from; tmp_ac.smtp_server = smtpserver; tmp_ac.smtpport = SMTP_PORT; mailval = send_message_smtp(&tmp_ac, to_list, fp); } } } fseek(fp, filepos, SEEK_SET); if (newsgroup_list && (mailval == 0)) { Folder *folder; gchar *tmp = NULL; FILE *tmpfp; /* write to temporary file */ tmp = g_strdup_printf("%s%ctmp%d", g_get_tmp_dir(), G_DIR_SEPARATOR, (gint)file); if ((tmpfp = fopen(tmp, "wb")) == NULL) { FILE_OP_ERROR(tmp, "fopen"); newsval = -1; alertpanel_error(_("Could not create temporary file for news sending.")); } else { if (change_file_mode_rw(tmpfp, tmp) < 0) { FILE_OP_ERROR(tmp, "chmod"); g_warning("can't change file mode\n"); } while ((newsval == 0) && fgets(buf, sizeof(buf), fp) != NULL) { if (fputs(buf, tmpfp) == EOF) { FILE_OP_ERROR(tmp, "fputs"); newsval = -1; alertpanel_error(_("Error when writing temporary file for news sending.")); } } fclose(tmpfp); if (newsval == 0) { debug_print("Sending message by news\n"); folder = FOLDER(newsac->folder); newsval = news_post(folder, tmp); if (newsval < 0) { alertpanel_error(_("Error occurred while posting the message to %s ."), newsac->nntp_server); } } unlink(tmp); } g_free(tmp); } fclose(fp); /* save message to outbox */ if (mailval == 0 && newsval == 0 && savecopyfolder) { FolderItem *outbox; debug_print("saving sent message...\n"); outbox = folder_find_item_from_identifier(savecopyfolder); if (!outbox) outbox = folder_get_default_outbox(); if (save_clear_text || tmp_enc_file == NULL) { procmsg_save_to_outbox(outbox, file, TRUE); } else { procmsg_save_to_outbox(outbox, tmp_enc_file, FALSE); } } if (tmp_enc_file != NULL) { unlink(tmp_enc_file); free(tmp_enc_file); tmp_enc_file = NULL; } if (replymessageid != NULL || fwdmessageid != NULL) { gchar **tokens; FolderItem *item; if (replymessageid != NULL) tokens = g_strsplit(replymessageid, "\x7f", 0); else tokens = g_strsplit(fwdmessageid, "\x7f", 0); item = folder_find_item_from_identifier(tokens[0]); /* check if queued message has valid folder and message id */ if (item != NULL && tokens[2] != NULL) { MsgInfo *msginfo; msginfo = folder_item_get_msginfo(item, atoi(tokens[1])); /* check if referring message exists and has a message id */ if ((msginfo != NULL) && (msginfo->msgid != NULL) && (strcmp(msginfo->msgid, tokens[2]) != 0)) { procmsg_msginfo_free(msginfo); msginfo = NULL; } if (msginfo == NULL) { msginfo = folder_item_get_msginfo_by_msgid(item, tokens[2]); } if (msginfo != NULL) { if (replymessageid != NULL) { procmsg_msginfo_unset_flags(msginfo, MSG_FORWARDED, 0); procmsg_msginfo_set_flags(msginfo, MSG_REPLIED, 0); } else { procmsg_msginfo_unset_flags(msginfo, MSG_REPLIED, 0); procmsg_msginfo_set_flags(msginfo, MSG_FORWARDED, 0); } procmsg_msginfo_free(msginfo); } } g_strfreev(tokens); } g_free(from); g_free(smtpserver); slist_free_strings(to_list); g_slist_free(to_list); slist_free_strings(newsgroup_list); g_slist_free(newsgroup_list); g_free(savecopyfolder); g_free(replymessageid); g_free(fwdmessageid); g_free(privacy_system); g_free(encrypt_data); return (newsval != 0 ? newsval : mailval); }
static gint pop3_write_msg_to_file(const gchar *file, const gchar *data, guint len, const gchar *prefix) { FILE *fp; const gchar *prev, *cur; cm_return_val_if_fail(file != NULL, -1); if ((fp = g_fopen(file, "wb")) == NULL) { FILE_OP_ERROR(file, "fopen"); return -1; } if (change_file_mode_rw(fp, file) < 0) FILE_OP_ERROR(file, "chmod"); if (prefix != NULL) { if (fprintf(fp, "%s\n", prefix) < 0) { FILE_OP_ERROR(file, "fprintf"); fclose(fp); claws_unlink(file); return -1; } } /* +------------------+----------------+--------------------------+ * * ^data ^prev ^cur data+len-1^ */ prev = data; while ((cur = (gchar *)my_memmem(prev, len - (prev - data), "\r\n", 2)) != NULL) { if ((cur > prev && fwrite(prev, 1, cur - prev, fp) < 1) || fputc('\n', fp) == EOF) { FILE_OP_ERROR(file, "fwrite"); g_warning("can't write to file: %s\n", file); fclose(fp); claws_unlink(file); return -1; } if (cur == data + len - 1) { prev = cur + 1; break; } if (*(cur + 1) == '\n') prev = cur + 2; else prev = cur + 1; if (prev - data < len - 1 && *prev == '.' && *(prev + 1) == '.') prev++; if (prev - data >= len) break; } if (prev - data < len && fwrite(prev, 1, len - (prev - data), fp) < 1) { FILE_OP_ERROR(file, "fwrite"); g_warning("can't write to file: %s\n", file); fclose(fp); claws_unlink(file); return -1; } if (data[len - 1] != '\r' && data[len - 1] != '\n') { if (fputc('\n', fp) == EOF) { FILE_OP_ERROR(file, "fputc"); g_warning("can't write to file: %s\n", file); fclose(fp); claws_unlink(file); return -1; } } if (fclose(fp) == EOF) { FILE_OP_ERROR(file, "fclose"); claws_unlink(file); return -1; } return 0; }
static void save_archiver_prefs(PrefsPage * _page) { struct ArchiverPrefsPage *page = (struct ArchiverPrefsPage *) _page; PrefFile *pref_file; gchar *rc_file_path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL); archiver_prefs.save_folder = gtk_editable_get_chars(GTK_EDITABLE(page->save_folder), 0, -1); if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->zip_radiobtn))) archiver_prefs.compression = COMPRESSION_ZIP; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->bzip_radiobtn))) archiver_prefs.compression = COMPRESSION_BZIP; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->compress_radiobtn))) archiver_prefs.compression = COMPRESSION_COMPRESS; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->none_radiobtn))) archiver_prefs.compression = COMPRESSION_NONE; if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->tar_radiobtn))) archiver_prefs.format = FORMAT_TAR; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->shar_radiobtn))) archiver_prefs.format = FORMAT_SHAR; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->cpio_radiobtn))) archiver_prefs.format = FORMAT_CPIO; else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->pax_radiobtn))) archiver_prefs.format = FORMAT_PAX; if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->recursive_chkbtn))) archiver_prefs.recursive = TRUE; else archiver_prefs.recursive = FALSE; if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->md5sum_chkbtn))) archiver_prefs.md5sum = TRUE; else archiver_prefs.md5sum = FALSE; if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->rename_chkbtn))) archiver_prefs.rename = TRUE; else archiver_prefs.rename = FALSE; if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->unlink_chkbtn))) archiver_prefs.unlink = TRUE; else archiver_prefs.unlink = FALSE; pref_file = prefs_write_open(rc_file_path); g_free(rc_file_path); if (!(pref_file) || (prefs_set_block_label(pref_file, PREFS_BLOCK_NAME) < 0)) return; if (prefs_write_param(param, pref_file->fp) < 0) { g_warning("failed to write Archiver Plugin configuration\n"); prefs_file_close_revert(pref_file); return; } if (fprintf(pref_file->fp, "\n") < 0) { FILE_OP_ERROR(rc_file_path, "fprintf"); prefs_file_close_revert(pref_file); } else prefs_file_close(pref_file); }
gint pop3_write_uidl_list(Pop3Session *session) { gchar *path, *tmp_path; FILE *fp; Pop3MsgInfo *msg; gint n; gchar *sanitized_uid = g_strdup(session->ac_prefs->userid); subst_for_filename(sanitized_uid); if (!session->uidl_is_valid) { g_free(sanitized_uid); return 0; } path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "uidl", G_DIR_SEPARATOR_S, session->ac_prefs->recv_server, "-", sanitized_uid, NULL); tmp_path = g_strconcat(path, ".tmp", NULL); g_free(sanitized_uid); if ((fp = g_fopen(tmp_path, "wb")) == NULL) { FILE_OP_ERROR(tmp_path, "fopen"); goto err_write; } for (n = 1; n <= session->count; n++) { msg = &session->msg[n]; if (msg->uidl && msg->received && (!msg->deleted || session->state != POP3_DONE)) TRY(fprintf(fp, "%s\t%ld\t%d\n", msg->uidl, (long int) msg->recv_time, msg->partial_recv) > 0); } if (fclose(fp) == EOF) { FILE_OP_ERROR(tmp_path, "fclose"); fp = NULL; goto err_write; } fp = NULL; #ifdef G_OS_WIN32 claws_unlink(path); #endif if (g_rename(tmp_path, path) < 0) { FILE_OP_ERROR(path, "rename"); goto err_write; } g_free(path); g_free(tmp_path); return 0; err_write: if (fp) fclose(fp); g_free(path); g_free(tmp_path); return -1; }
static void pop3_get_uidl_table(PrefsAccount *ac_prefs, Pop3Session *session) { GHashTable *table; GHashTable *partial_recv_table; gchar *path; FILE *fp; gchar buf[POPBUFSIZE]; gchar uidl[POPBUFSIZE]; time_t recv_time; time_t now; gint partial_recv; gchar *sanitized_uid = g_strdup(ac_prefs->userid); subst_for_filename(sanitized_uid); table = g_hash_table_new(g_str_hash, g_str_equal); partial_recv_table = g_hash_table_new(g_str_hash, g_str_equal); path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "uidl", G_DIR_SEPARATOR_S, ac_prefs->recv_server, "-", sanitized_uid, NULL); g_free(sanitized_uid); if ((fp = g_fopen(path, "rb")) == NULL) { if (ENOENT != errno) FILE_OP_ERROR(path, "fopen"); g_free(path); path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "uidl-", ac_prefs->recv_server, "-", ac_prefs->userid, NULL); if ((fp = g_fopen(path, "rb")) == NULL) { if (ENOENT != errno) FILE_OP_ERROR(path, "fopen"); g_free(path); session->uidl_table = table; session->partial_recv_table = partial_recv_table; return; } } g_free(path); now = time(NULL); while (fgets(buf, sizeof(buf), fp) != NULL) { gchar tmp[POPBUFSIZE]; strretchomp(buf); recv_time = RECV_TIME_NONE; partial_recv = POP3_TOTALLY_RECEIVED; if (sscanf(buf, "%s\t%ld\t%s", uidl, (long int *) &recv_time, tmp) < 3) { if (sscanf(buf, "%s\t%ld", uidl, (long int *) &recv_time) != 2) { if (sscanf(buf, "%s", uidl) != 1) continue; else { recv_time = now; strcpy(tmp, "0"); } } else { strcpy(tmp, "0"); } } if (recv_time == RECV_TIME_NONE) recv_time = RECV_TIME_RECEIVED; g_hash_table_insert(table, g_strdup(uidl), GINT_TO_POINTER(recv_time)); if (strlen(tmp) == 1) partial_recv = atoi(tmp); /* totally received ?*/ else partial_recv = POP3_MUST_COMPLETE_RECV; g_hash_table_insert(partial_recv_table, g_strdup(uidl), GINT_TO_POINTER(partial_recv)); } fclose(fp); session->uidl_table = table; session->partial_recv_table = partial_recv_table; return; }
GSList *news_get_group_list(Folder *folder) { gchar *path, *filename; FILE *fp; GSList *list = NULL; GSList *last = NULL; gchar buf[BUFFSIZE]; cm_return_val_if_fail(folder != NULL, NULL); cm_return_val_if_fail(FOLDER_CLASS(folder) == &news_class, NULL); path = folder_item_get_path(FOLDER_ITEM(folder->node->data)); if (!is_dir_exist(path)) make_dir_hier(path); filename = g_strconcat(path, G_DIR_SEPARATOR_S, NEWSGROUP_LIST, NULL); g_free(path); if ((fp = g_fopen(filename, "rb")) == NULL) { NewsSession *session; gint ok; clist *grouplist = NULL; clistiter *cur; fp = g_fopen(filename, "wb"); if (!fp) { g_free(filename); return NULL; } session = news_session_get(folder); if (!session) { fclose(fp); g_free(filename); return NULL; } ok = nntp_threaded_list(folder, &grouplist); if (ok != NEWSNNTP_NO_ERROR) { if (ok == NEWSNNTP_ERROR_STREAM) { session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; } fclose(fp); g_free(filename); return NULL; } if (grouplist) { for (cur = clist_begin(grouplist); cur; cur = clist_next(cur)) { struct newsnntp_group_info *info = (struct newsnntp_group_info *) clist_content(cur); if (fprintf(fp, "%s %d %d %c\n", info->grp_name, info->grp_last, info->grp_first, info->grp_type) < 0) { log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n")); session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; fclose(fp); g_free(filename); newsnntp_list_free(grouplist); return NULL; } } newsnntp_list_free(grouplist); } if (fclose(fp) == EOF) { log_error(LOG_PROTOCOL, ("Can't write newsgroup list\n")); session_destroy(SESSION(session)); REMOTE_FOLDER(folder)->session = NULL; g_free(filename); return NULL; } if ((fp = g_fopen(filename, "rb")) == NULL) { FILE_OP_ERROR(filename, "fopen"); g_free(filename); return NULL; } } while (fgets(buf, sizeof(buf), fp) != NULL) { gchar *p = buf; gchar *name; gint last_num; gint first_num; gchar type; NewsGroupInfo *ginfo; p = strchr(p, ' '); if (!p) continue; *p = '\0'; p++; name = buf; if (sscanf(p, "%d %d %c", &last_num, &first_num, &type) < 3) continue; ginfo = news_group_info_new(name, first_num, last_num, type); if (!last) last = list = g_slist_append(NULL, ginfo); else { last = g_slist_append(last, ginfo); last = last->next; } } fclose(fp); g_free(filename); list = g_slist_sort(list, (GCompareFunc)news_group_info_compare); return list; }
static MimeInfo *pgpmime_decrypt(MimeInfo *mimeinfo) { MimeInfo *encinfo, *decinfo, *parseinfo; gpgme_data_t cipher = NULL, plain = NULL; static gint id = 0; FILE *dstfp; gchar *fname; gpgme_verify_result_t sigstat = NULL; PrivacyDataPGP *data = NULL; gpgme_ctx_t ctx; gchar *chars; size_t len; gpgme_error_t err; if ((err = gpgme_new(&ctx)) != GPG_ERR_NO_ERROR) { debug_print(("Couldn't initialize GPG context, %s\n"), gpgme_strerror(err)); privacy_set_error(_("Couldn't initialize GPG context, %s"), gpgme_strerror(err)); return NULL; } cm_return_val_if_fail(pgpmime_is_encrypted(mimeinfo), NULL); encinfo = (MimeInfo *) g_node_nth_child(mimeinfo->node, 1)->data; cipher = sgpgme_data_from_mimeinfo(encinfo); plain = sgpgme_decrypt_verify(cipher, &sigstat, ctx); gpgme_data_release(cipher); if (plain == NULL) { debug_print("plain is null!\n"); gpgme_release(ctx); return NULL; } fname = g_strdup_printf("%s%cplaintext.%08x", get_mime_tmp_dir(), G_DIR_SEPARATOR, ++id); if ((dstfp = claws_fopen(fname, "wb")) == NULL) { FILE_OP_ERROR(fname, "claws_fopen"); privacy_set_error(_("Couldn't open decrypted file %s"), fname); g_free(fname); gpgme_data_release(plain); gpgme_release(ctx); debug_print("can't open!\n"); return NULL; } if (fprintf(dstfp, "MIME-Version: 1.0\n") < 0) { FILE_OP_ERROR(fname, "fprintf"); claws_fclose(dstfp); privacy_set_error(_("Couldn't write to decrypted file %s"), fname); g_free(fname); gpgme_data_release(plain); gpgme_release(ctx); debug_print("can't open!\n"); return NULL; } chars = sgpgme_data_release_and_get_mem(plain, &len); if (len > 0) { if (claws_fwrite(chars, 1, len, dstfp) < len) { FILE_OP_ERROR(fname, "claws_fwrite"); g_free(chars); claws_fclose(dstfp); privacy_set_error(_("Couldn't write to decrypted file %s"), fname); g_free(fname); gpgme_data_release(plain); gpgme_release(ctx); debug_print("can't open!\n"); return NULL; } } g_free(chars); if (claws_safe_fclose(dstfp) == EOF) { FILE_OP_ERROR(fname, "claws_fclose"); privacy_set_error(_("Couldn't close decrypted file %s"), fname); g_free(fname); gpgme_data_release(plain); gpgme_release(ctx); debug_print("can't open!\n"); return NULL; } parseinfo = procmime_scan_file(fname); g_free(fname); if (parseinfo == NULL) { gpgme_release(ctx); privacy_set_error(_("Couldn't parse decrypted file.")); return NULL; } decinfo = g_node_first_child(parseinfo->node) != NULL ? g_node_first_child(parseinfo->node)->data : NULL; if (decinfo == NULL) { privacy_set_error(_("Couldn't parse decrypted file parts.")); gpgme_release(ctx); return NULL; } g_node_unlink(decinfo->node); procmime_mimeinfo_free_all(&parseinfo); decinfo->tmp = TRUE; if (sigstat != NULL && sigstat->signatures != NULL) { if (decinfo->privacy != NULL) { data = (PrivacyDataPGP *) decinfo->privacy; } else { data = pgpmime_new_privacydata(); decinfo->privacy = (PrivacyData *) data; } if (data != NULL) { data->done_sigtest = TRUE; data->is_signed = TRUE; data->sigstatus = sigstat; if (data->ctx) gpgme_release(data->ctx); data->ctx = ctx; } } else gpgme_release(ctx); return decinfo; }
static void prefs_custom_header_write_config(PrefsAccount *ac) { gchar *rcpath; PrefFile *pfile; GSList *cur; gchar buf[PREFSBUFSIZE]; FILE * fp; CustomHeader *ch; GSList *all_hdrs = NULL; debug_print("Writing custom header configuration...\n"); rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, CUSTOM_HEADER_RC, NULL); if ((fp = g_fopen(rcpath, "rb")) == NULL) { if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); } else { all_hdrs = NULL; while (fgets(buf, sizeof(buf), fp) != NULL) { ch = custom_header_read_str(buf); if (ch) { if (ch->account_id != ac->account_id) all_hdrs = g_slist_append(all_hdrs, ch); else custom_header_free(ch); } } fclose(fp); } if ((pfile = prefs_write_open(rcpath)) == NULL) { g_warning("failed to write configuration to file"); g_free(rcpath); return; } for (cur = all_hdrs; cur != NULL; cur = cur->next) { CustomHeader *hdr = (CustomHeader *)cur->data; gchar *chstr; chstr = custom_header_get_str(hdr); if (fputs(chstr, pfile->fp) == EOF || fputc('\n', pfile->fp) == EOF) { FILE_OP_ERROR(rcpath, "fputs || fputc"); prefs_file_close_revert(pfile); g_free(rcpath); g_free(chstr); return; } g_free(chstr); } for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) { CustomHeader *hdr = (CustomHeader *)cur->data; gchar *chstr; chstr = custom_header_get_str(hdr); if (fputs(chstr, pfile->fp) == EOF || fputc('\n', pfile->fp) == EOF) { FILE_OP_ERROR(rcpath, "fputs || fputc"); prefs_file_close_revert(pfile); g_free(rcpath); g_free(chstr); return; } g_free(chstr); } g_free(rcpath); while (all_hdrs != NULL) { ch = (CustomHeader *)all_hdrs->data; all_hdrs = g_slist_remove(all_hdrs, ch); custom_header_free(ch); } if (prefs_file_close(pfile) < 0) { g_warning("failed to write configuration to file"); return; } }