diff options
| author | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
|---|---|---|
| committer | Simeon Simeonov | 2018-02-26 11:23:00 +0100 |
| commit | 0b3cbf57875fd692e4ba0b336fefa4bee1ed00dc (patch) | |
| tree | af916a30553c78ce9d4f2d8658656a175c5b6921 /libsylph/prefs_account.c | |
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'libsylph/prefs_account.c')
| -rw-r--r-- | libsylph/prefs_account.c | 296 |
1 files changed, 296 insertions, 0 deletions
diff --git a/libsylph/prefs_account.c b/libsylph/prefs_account.c new file mode 100644 index 0000000..1aecba9 --- /dev/null +++ b/libsylph/prefs_account.c | |||
| @@ -0,0 +1,296 @@ | |||
| 1 | /* | ||
| 2 | * LibSylph -- E-Mail client library | ||
| 3 | * Copyright (C) 1999-2017 Hiroyuki Yamamoto | ||
| 4 | * | ||
| 5 | * This library is free software; you can redistribute it and/or | ||
| 6 | * modify it under the terms of the GNU Lesser General Public | ||
| 7 | * License as published by the Free Software Foundation; either | ||
| 8 | * version 2.1 of the License, or (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This library is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
| 13 | * Lesser General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU Lesser General Public | ||
| 16 | * License along with this library; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #ifdef HAVE_CONFIG_H | ||
| 21 | # include "config.h" | ||
| 22 | #endif | ||
| 23 | |||
| 24 | #include "defs.h" | ||
| 25 | |||
| 26 | #include <glib.h> | ||
| 27 | #include <glib/gi18n.h> | ||
| 28 | #include <stdio.h> | ||
| 29 | #include <string.h> | ||
| 30 | #include <stdlib.h> | ||
| 31 | |||
| 32 | #include "prefs.h" | ||
| 33 | #include "prefs_account.h" | ||
| 34 | #include "customheader.h" | ||
| 35 | #include "account.h" | ||
| 36 | #include "utils.h" | ||
| 37 | |||
| 38 | static PrefsAccount tmp_ac_prefs; | ||
| 39 | |||
| 40 | static PrefParam param[] = { | ||
| 41 | /* Basic */ | ||
| 42 | {"account_name", NULL, &tmp_ac_prefs.account_name, P_STRING}, | ||
| 43 | {"is_default", "FALSE", &tmp_ac_prefs.is_default, P_BOOL}, | ||
| 44 | {"name", NULL, &tmp_ac_prefs.name, P_STRING}, | ||
| 45 | {"address", NULL, &tmp_ac_prefs.address, P_STRING}, | ||
| 46 | {"organization", NULL, &tmp_ac_prefs.organization, P_STRING}, | ||
| 47 | {"protocol", NULL, &tmp_ac_prefs.protocol, P_ENUM}, | ||
| 48 | {"receive_server", NULL, &tmp_ac_prefs.recv_server, P_STRING}, | ||
| 49 | {"smtp_server", NULL, &tmp_ac_prefs.smtp_server, P_STRING}, | ||
| 50 | {"nntp_server", NULL, &tmp_ac_prefs.nntp_server, P_STRING}, | ||
| 51 | {"use_nntp_auth", "FALSE", &tmp_ac_prefs.use_nntp_auth, P_BOOL}, | ||
| 52 | {"user_id", "ENV_USER", &tmp_ac_prefs.userid, P_STRING}, | ||
| 53 | {"password", NULL, &tmp_ac_prefs.passwd, P_STRING}, | ||
| 54 | {"inbox", "inbox", &tmp_ac_prefs.inbox, P_STRING}, | ||
| 55 | |||
| 56 | /* Receive */ | ||
| 57 | {"use_apop_auth", "FALSE", &tmp_ac_prefs.use_apop_auth, P_BOOL}, | ||
| 58 | {"remove_mail", "TRUE", &tmp_ac_prefs.rmmail, P_BOOL}, | ||
| 59 | {"message_leave_time", "7", &tmp_ac_prefs.msg_leave_time, P_INT}, | ||
| 60 | {"get_all_mail", "FALSE", &tmp_ac_prefs.getall, P_BOOL}, | ||
| 61 | {"enable_size_limit", "FALSE", &tmp_ac_prefs.enable_size_limit, P_BOOL}, | ||
| 62 | {"size_limit", "1024", &tmp_ac_prefs.size_limit, P_INT}, | ||
| 63 | {"filter_on_receive", "TRUE", &tmp_ac_prefs.filter_on_recv, P_BOOL}, | ||
| 64 | {"imap_check_inbox_only", "FALSE", &tmp_ac_prefs.imap_check_inbox_only, | ||
| 65 | P_BOOL}, | ||
| 66 | {"imap_filter_inbox_on_receive", "FALSE", | ||
| 67 | &tmp_ac_prefs.imap_filter_inbox_on_recv, P_BOOL}, | ||
| 68 | {"imap_auth_method", "0", &tmp_ac_prefs.imap_auth_type, P_ENUM}, | ||
| 69 | {"max_nntp_articles", "300", &tmp_ac_prefs.max_nntp_articles, P_INT}, | ||
| 70 | {"receive_at_get_all", "TRUE", &tmp_ac_prefs.recv_at_getall, P_BOOL}, | ||
| 71 | |||
| 72 | /* Send */ | ||
| 73 | {"add_date", "TRUE", &tmp_ac_prefs.add_date, P_BOOL}, | ||
| 74 | {"generate_msgid", "TRUE", &tmp_ac_prefs.gen_msgid, P_BOOL}, | ||
| 75 | {"add_custom_header", "FALSE", &tmp_ac_prefs.add_customhdr, P_BOOL}, | ||
| 76 | {"use_smtp_auth", "FALSE", &tmp_ac_prefs.use_smtp_auth, P_BOOL}, | ||
| 77 | {"smtp_auth_method", "0", &tmp_ac_prefs.smtp_auth_type, P_ENUM}, | ||
| 78 | {"smtp_user_id", NULL, &tmp_ac_prefs.smtp_userid, P_STRING}, | ||
| 79 | {"smtp_password", NULL, &tmp_ac_prefs.smtp_passwd, P_STRING}, | ||
| 80 | {"pop_before_smtp", "FALSE", &tmp_ac_prefs.pop_before_smtp, P_BOOL}, | ||
| 81 | |||
| 82 | /* Compose */ | ||
| 83 | {"signature_type", "2", &tmp_ac_prefs.sig_type, P_ENUM}, | ||
| 84 | {"signature_path", "~" G_DIR_SEPARATOR_S DEFAULT_SIGNATURE, | ||
| 85 | &tmp_ac_prefs.sig_path, P_STRING}, | ||
| 86 | {"signature_name", NULL, &tmp_ac_prefs.sig_names[0], P_STRING}, | ||
| 87 | {"signature_name2", NULL, &tmp_ac_prefs.sig_names[1], P_STRING}, | ||
| 88 | {"signature_name3", NULL, &tmp_ac_prefs.sig_names[2], P_STRING}, | ||
| 89 | {"signature_name4", NULL, &tmp_ac_prefs.sig_names[3], P_STRING}, | ||
| 90 | {"signature_name5", NULL, &tmp_ac_prefs.sig_names[4], P_STRING}, | ||
| 91 | {"signature_name6", NULL, &tmp_ac_prefs.sig_names[5], P_STRING}, | ||
| 92 | {"signature_name7", NULL, &tmp_ac_prefs.sig_names[6], P_STRING}, | ||
| 93 | {"signature_name8", NULL, &tmp_ac_prefs.sig_names[7], P_STRING}, | ||
| 94 | {"signature_name9", NULL, &tmp_ac_prefs.sig_names[8], P_STRING}, | ||
| 95 | {"signature_name10", NULL, &tmp_ac_prefs.sig_names[9], P_STRING}, | ||
| 96 | {"signature_text", NULL, &tmp_ac_prefs.sig_text, P_STRING}, | ||
| 97 | {"signature_text2", NULL, &tmp_ac_prefs.sig_texts[1], P_STRING}, | ||
| 98 | {"signature_text3", NULL, &tmp_ac_prefs.sig_texts[2], P_STRING}, | ||
| 99 | {"signature_text4", NULL, &tmp_ac_prefs.sig_texts[3], P_STRING}, | ||
| 100 | {"signature_text5", NULL, &tmp_ac_prefs.sig_texts[4], P_STRING}, | ||
| 101 | {"signature_text6", NULL, &tmp_ac_prefs.sig_texts[5], P_STRING}, | ||
| 102 | {"signature_text7", NULL, &tmp_ac_prefs.sig_texts[6], P_STRING}, | ||
| 103 | {"signature_text8", NULL, &tmp_ac_prefs.sig_texts[7], P_STRING}, | ||
| 104 | {"signature_text9", NULL, &tmp_ac_prefs.sig_texts[8], P_STRING}, | ||
| 105 | {"signature_text10", NULL, &tmp_ac_prefs.sig_texts[9], P_STRING}, | ||
| 106 | {"signature_before_quote", "FALSE", &tmp_ac_prefs.sig_before_quote, | ||
| 107 | P_BOOL}, | ||
| 108 | {"set_autocc", "FALSE", &tmp_ac_prefs.set_autocc, P_BOOL}, | ||
| 109 | {"auto_cc", NULL, &tmp_ac_prefs.auto_cc, P_STRING}, | ||
| 110 | {"set_autobcc", "FALSE", &tmp_ac_prefs.set_autobcc, P_BOOL}, | ||
| 111 | {"auto_bcc", NULL, &tmp_ac_prefs.auto_bcc, P_STRING}, | ||
| 112 | {"set_autoreplyto", "FALSE", &tmp_ac_prefs.set_autoreplyto, P_BOOL}, | ||
| 113 | {"auto_replyto", NULL, &tmp_ac_prefs.auto_replyto, P_STRING}, | ||
| 114 | |||
| 115 | /* Privacy */ | ||
| 116 | {"default_sign", "FALSE", &tmp_ac_prefs.default_sign, P_BOOL}, | ||
| 117 | {"default_encrypt", "FALSE", &tmp_ac_prefs.default_encrypt, P_BOOL}, | ||
| 118 | {"encrypt_reply", "TRUE", &tmp_ac_prefs.encrypt_reply, P_BOOL}, | ||
| 119 | {"encrypt_to_self", "TRUE", &tmp_ac_prefs.encrypt_to_self, P_BOOL}, | ||
| 120 | {"ascii_armored", "FALSE", &tmp_ac_prefs.ascii_armored, P_BOOL}, | ||
| 121 | {"clearsign", "FALSE", &tmp_ac_prefs.clearsign, P_BOOL}, | ||
| 122 | {"sign_key", NULL, &tmp_ac_prefs.sign_key, P_ENUM}, | ||
| 123 | {"sign_key_id", NULL, &tmp_ac_prefs.sign_key_id, P_STRING}, | ||
| 124 | |||
| 125 | /* SSL */ | ||
| 126 | {"ssl_pop", "0", &tmp_ac_prefs.ssl_pop, P_ENUM}, | ||
| 127 | {"ssl_imap", "0", &tmp_ac_prefs.ssl_imap, P_ENUM}, | ||
| 128 | {"ssl_nntp", "0", &tmp_ac_prefs.ssl_nntp, P_ENUM}, | ||
| 129 | {"ssl_smtp", "0", &tmp_ac_prefs.ssl_smtp, P_ENUM}, | ||
| 130 | {"use_nonblocking_ssl", "1", &tmp_ac_prefs.use_nonblocking_ssl, P_BOOL}, | ||
| 131 | |||
| 132 | /* SOCKS proxy */ | ||
| 133 | {"use_socks", "FALSE", &tmp_ac_prefs.use_socks, P_BOOL}, | ||
| 134 | {"use_socks_for_recv", "TRUE", &tmp_ac_prefs.use_socks_for_recv, P_BOOL}, | ||
| 135 | {"use_socks_for_send", "TRUE", &tmp_ac_prefs.use_socks_for_send, P_BOOL}, | ||
| 136 | {"socks_type", "1", &tmp_ac_prefs.socks_type, P_ENUM}, | ||
| 137 | {"proxy_host", NULL, &tmp_ac_prefs.proxy_host, P_STRING}, | ||
| 138 | {"proxy_port", "1080", &tmp_ac_prefs.proxy_port, P_USHORT}, | ||
| 139 | {"use_proxy_auth", "FALSE", &tmp_ac_prefs.use_proxy_auth, P_BOOL}, | ||
| 140 | {"proxy_name", NULL, &tmp_ac_prefs.proxy_name, P_STRING}, | ||
| 141 | {"proxy_pass", NULL, &tmp_ac_prefs.proxy_pass, P_STRING}, | ||
| 142 | |||
| 143 | /* Advanced */ | ||
| 144 | {"set_smtpport", "FALSE", &tmp_ac_prefs.set_smtpport, P_BOOL}, | ||
| 145 | {"smtp_port", "25", &tmp_ac_prefs.smtpport, P_USHORT}, | ||
| 146 | {"set_popport", "FALSE", &tmp_ac_prefs.set_popport, P_BOOL}, | ||
| 147 | {"pop_port", "110", &tmp_ac_prefs.popport, P_USHORT}, | ||
| 148 | {"set_imapport", "FALSE", &tmp_ac_prefs.set_imapport, P_BOOL}, | ||
| 149 | {"imap_port", "143", &tmp_ac_prefs.imapport, P_USHORT}, | ||
| 150 | {"set_nntpport", "FALSE", &tmp_ac_prefs.set_nntpport, P_BOOL}, | ||
| 151 | {"nntp_port", "119", &tmp_ac_prefs.nntpport, P_USHORT}, | ||
| 152 | {"set_domain", "FALSE", &tmp_ac_prefs.set_domain, P_BOOL}, | ||
| 153 | {"domain", NULL, &tmp_ac_prefs.domain, P_STRING}, | ||
| 154 | {"imap_directory", NULL, &tmp_ac_prefs.imap_dir, P_STRING}, | ||
| 155 | {"imap_clear_cache_on_exit", "FALSE", | ||
| 156 | &tmp_ac_prefs.imap_clear_cache_on_exit, P_BOOL}, | ||
| 157 | {"set_sent_folder", "FALSE", &tmp_ac_prefs.set_sent_folder, P_BOOL}, | ||
| 158 | {"sent_folder", NULL, &tmp_ac_prefs.sent_folder, P_STRING}, | ||
| 159 | {"set_draft_folder", "FALSE", &tmp_ac_prefs.set_draft_folder, P_BOOL}, | ||
| 160 | {"draft_folder", NULL, &tmp_ac_prefs.draft_folder, P_STRING}, | ||
| 161 | {"set_queue_folder", "FALSE", &tmp_ac_prefs.set_queue_folder, P_BOOL}, | ||
| 162 | {"queue_folder", NULL, &tmp_ac_prefs.queue_folder, P_STRING}, | ||
| 163 | {"set_trash_folder", "FALSE", &tmp_ac_prefs.set_trash_folder, P_BOOL}, | ||
| 164 | {"trash_folder", NULL, &tmp_ac_prefs.trash_folder, P_STRING}, | ||
| 165 | |||
| 166 | {NULL, NULL, NULL, P_OTHER} | ||
| 167 | }; | ||
| 168 | |||
| 169 | static gint prefs_account_get_new_id(void); | ||
| 170 | |||
| 171 | |||
| 172 | PrefsAccount *prefs_account_get_tmp_prefs(void) | ||
| 173 | { | ||
| 174 | return &tmp_ac_prefs; | ||
| 175 | } | ||
| 176 | |||
| 177 | void prefs_account_set_tmp_prefs(PrefsAccount *ac_prefs) | ||
| 178 | { | ||
| 179 | tmp_ac_prefs = *ac_prefs; | ||
| 180 | } | ||
| 181 | |||
| 182 | void prefs_account_apply_tmp_prefs(PrefsAccount *ac_prefs) | ||
| 183 | { | ||
| 184 | *ac_prefs = tmp_ac_prefs; | ||
| 185 | } | ||
| 186 | |||
| 187 | PrefParam *prefs_account_get_params(void) | ||
| 188 | { | ||
| 189 | return param; | ||
| 190 | } | ||
| 191 | |||
| 192 | PrefsAccount *prefs_account_new(void) | ||
| 193 | { | ||
| 194 | PrefsAccount *ac_prefs; | ||
| 195 | |||
| 196 | ac_prefs = g_new0(PrefsAccount, 1); | ||
| 197 | memset(&tmp_ac_prefs, 0, sizeof(PrefsAccount)); | ||
| 198 | prefs_set_default(param); | ||
| 199 | *ac_prefs = tmp_ac_prefs; | ||
| 200 | ac_prefs->account_id = prefs_account_get_new_id(); | ||
| 201 | |||
| 202 | return ac_prefs; | ||
| 203 | } | ||
| 204 | |||
| 205 | void prefs_account_read_config(PrefsAccount *ac_prefs, const gchar *label) | ||
| 206 | { | ||
| 207 | const gchar *p = label; | ||
| 208 | gchar *rcpath; | ||
| 209 | gint id; | ||
| 210 | |||
| 211 | g_return_if_fail(ac_prefs != NULL); | ||
| 212 | g_return_if_fail(label != NULL); | ||
| 213 | |||
| 214 | memset(&tmp_ac_prefs, 0, sizeof(PrefsAccount)); | ||
| 215 | |||
| 216 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL); | ||
| 217 | prefs_read_config(param, label, rcpath, NULL); | ||
| 218 | g_free(rcpath); | ||
| 219 | |||
| 220 | *ac_prefs = tmp_ac_prefs; | ||
| 221 | while (*p && !g_ascii_isdigit(*p)) p++; | ||
| 222 | id = atoi(p); | ||
| 223 | if (id < 0) g_warning("wrong account id: %d\n", id); | ||
| 224 | ac_prefs->account_id = id; | ||
| 225 | |||
| 226 | if (ac_prefs->protocol == A_APOP) { | ||
| 227 | debug_print("converting protocol A_APOP to new prefs.\n"); | ||
| 228 | ac_prefs->protocol = A_POP3; | ||
| 229 | ac_prefs->use_apop_auth = TRUE; | ||
| 230 | } | ||
| 231 | |||
| 232 | custom_header_read_config(ac_prefs); | ||
| 233 | } | ||
| 234 | |||
| 235 | void prefs_account_write_config_all(GList *account_list) | ||
| 236 | { | ||
| 237 | GList *cur; | ||
| 238 | gchar *rcpath; | ||
| 239 | PrefFile *pfile; | ||
| 240 | |||
| 241 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL); | ||
| 242 | if ((pfile = prefs_file_open(rcpath)) == NULL) { | ||
| 243 | g_free(rcpath); | ||
| 244 | return; | ||
| 245 | } | ||
| 246 | g_free(rcpath); | ||
| 247 | |||
| 248 | for (cur = account_list; cur != NULL; cur = cur->next) { | ||
| 249 | tmp_ac_prefs = *(PrefsAccount *)cur->data; | ||
| 250 | if (fprintf(pfile->fp, "[Account: %d]\n", | ||
| 251 | tmp_ac_prefs.account_id) <= 0 || | ||
| 252 | prefs_file_write_param(pfile, param) < 0) { | ||
| 253 | g_warning(_("failed to write configuration to file\n")); | ||
| 254 | prefs_file_close_revert(pfile); | ||
| 255 | return; | ||
| 256 | } | ||
| 257 | if (cur->next) { | ||
| 258 | if (fputc('\n', pfile->fp) == EOF) { | ||
| 259 | FILE_OP_ERROR(rcpath, "fputc"); | ||
| 260 | prefs_file_close_revert(pfile); | ||
| 261 | return; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | } | ||
| 265 | |||
| 266 | if (prefs_file_close(pfile) < 0) | ||
| 267 | g_warning(_("failed to write configuration to file\n")); | ||
| 268 | } | ||
| 269 | |||
| 270 | void prefs_account_free(PrefsAccount *ac_prefs) | ||
| 271 | { | ||
| 272 | if (!ac_prefs) return; | ||
| 273 | |||
| 274 | tmp_ac_prefs = *ac_prefs; | ||
| 275 | prefs_free(param); | ||
| 276 | |||
| 277 | if (ac_prefs->tmp_pass) | ||
| 278 | g_free(ac_prefs->tmp_pass); | ||
| 279 | g_free(ac_prefs); | ||
| 280 | } | ||
| 281 | |||
| 282 | static gint prefs_account_get_new_id(void) | ||
| 283 | { | ||
| 284 | GList *ac_list; | ||
| 285 | PrefsAccount *ac; | ||
| 286 | static gint last_id = 0; | ||
| 287 | |||
| 288 | for (ac_list = account_get_list(); ac_list != NULL; | ||
| 289 | ac_list = ac_list->next) { | ||
| 290 | ac = (PrefsAccount *)ac_list->data; | ||
| 291 | if (last_id < ac->account_id) | ||
| 292 | last_id = ac->account_id; | ||
| 293 | } | ||
| 294 | |||
| 295 | return last_id + 1; | ||
| 296 | } | ||
