diff options
Diffstat (limited to 'libsylph/customheader.c')
| -rw-r--r-- | libsylph/customheader.c | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/libsylph/customheader.c b/libsylph/customheader.c new file mode 100644 index 0000000..0f4d4a0 --- /dev/null +++ b/libsylph/customheader.c | |||
| @@ -0,0 +1,233 @@ | |||
| 1 | /* | ||
| 2 | * LibSylph -- E-Mail client library | ||
| 3 | * Copyright (C) 1999-2005 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 <stdio.h> | ||
| 28 | #include <string.h> | ||
| 29 | #include <stdlib.h> | ||
| 30 | #include <errno.h> | ||
| 31 | |||
| 32 | #include "customheader.h" | ||
| 33 | #include "prefs.h" | ||
| 34 | #include "prefs_account.h" | ||
| 35 | #include "utils.h" | ||
| 36 | |||
| 37 | |||
| 38 | void custom_header_read_config(PrefsAccount *ac) | ||
| 39 | { | ||
| 40 | gchar *rcpath; | ||
| 41 | FILE *fp; | ||
| 42 | gchar buf[PREFSBUFSIZE]; | ||
| 43 | CustomHeader *ch; | ||
| 44 | |||
| 45 | debug_print("Reading custom header configuration...\n"); | ||
| 46 | |||
| 47 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, | ||
| 48 | CUSTOM_HEADER_RC, NULL); | ||
| 49 | if ((fp = g_fopen(rcpath, "rb")) == NULL) { | ||
| 50 | if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); | ||
| 51 | g_free(rcpath); | ||
| 52 | ac->customhdr_list = NULL; | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | g_free(rcpath); | ||
| 56 | |||
| 57 | /* remove all previous headers list */ | ||
| 58 | while (ac->customhdr_list != NULL) { | ||
| 59 | ch = (CustomHeader *)ac->customhdr_list->data; | ||
| 60 | custom_header_free(ch); | ||
| 61 | ac->customhdr_list = g_slist_remove(ac->customhdr_list, ch); | ||
| 62 | } | ||
| 63 | |||
| 64 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
| 65 | ch = custom_header_read_str(buf); | ||
| 66 | if (ch) { | ||
| 67 | if (ch->account_id == ac->account_id) { | ||
| 68 | ac->customhdr_list = | ||
| 69 | g_slist_append(ac->customhdr_list, ch); | ||
| 70 | } else | ||
| 71 | custom_header_free(ch); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | fclose(fp); | ||
| 76 | } | ||
| 77 | |||
| 78 | void custom_header_write_config(PrefsAccount *ac) | ||
| 79 | { | ||
| 80 | gchar *rcpath; | ||
| 81 | PrefFile *pfile; | ||
| 82 | GSList *cur; | ||
| 83 | gchar buf[PREFSBUFSIZE]; | ||
| 84 | FILE * fp; | ||
| 85 | CustomHeader *ch; | ||
| 86 | |||
| 87 | GSList *all_hdrs = NULL; | ||
| 88 | |||
| 89 | debug_print("Writing custom header configuration...\n"); | ||
| 90 | |||
| 91 | rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, | ||
| 92 | CUSTOM_HEADER_RC, NULL); | ||
| 93 | |||
| 94 | if ((fp = g_fopen(rcpath, "rb")) == NULL) { | ||
| 95 | if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen"); | ||
| 96 | } else { | ||
| 97 | all_hdrs = NULL; | ||
| 98 | |||
| 99 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
| 100 | ch = custom_header_read_str(buf); | ||
| 101 | if (ch) { | ||
| 102 | if (ch->account_id != ac->account_id) | ||
| 103 | all_hdrs = | ||
| 104 | g_slist_append(all_hdrs, ch); | ||
| 105 | else | ||
| 106 | custom_header_free(ch); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | |||
| 110 | fclose(fp); | ||
| 111 | } | ||
| 112 | |||
| 113 | if ((pfile = prefs_file_open(rcpath)) == NULL) { | ||
| 114 | g_warning("failed to write configuration to file\n"); | ||
| 115 | g_free(rcpath); | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | |||
| 119 | for (cur = all_hdrs; cur != NULL; cur = cur->next) { | ||
| 120 | CustomHeader *hdr = (CustomHeader *)cur->data; | ||
| 121 | gchar *chstr; | ||
| 122 | |||
| 123 | chstr = custom_header_get_str(hdr); | ||
| 124 | if (fputs(chstr, pfile->fp) == EOF || | ||
| 125 | fputc('\n', pfile->fp) == EOF) { | ||
| 126 | FILE_OP_ERROR(rcpath, "fputs || fputc"); | ||
| 127 | prefs_file_close_revert(pfile); | ||
| 128 | g_free(rcpath); | ||
| 129 | g_free(chstr); | ||
| 130 | return; | ||
| 131 | } | ||
| 132 | g_free(chstr); | ||
| 133 | } | ||
| 134 | |||
| 135 | for (cur = ac->customhdr_list; cur != NULL; cur = cur->next) { | ||
| 136 | CustomHeader *hdr = (CustomHeader *)cur->data; | ||
| 137 | gchar *chstr; | ||
| 138 | |||
| 139 | chstr = custom_header_get_str(hdr); | ||
| 140 | if (fputs(chstr, pfile->fp) == EOF || | ||
| 141 | fputc('\n', pfile->fp) == EOF) { | ||
| 142 | FILE_OP_ERROR(rcpath, "fputs || fputc"); | ||
| 143 | prefs_file_close_revert(pfile); | ||
| 144 | g_free(rcpath); | ||
| 145 | g_free(chstr); | ||
| 146 | return; | ||
| 147 | } | ||
| 148 | g_free(chstr); | ||
| 149 | } | ||
| 150 | |||
| 151 | g_free(rcpath); | ||
| 152 | |||
| 153 | while (all_hdrs != NULL) { | ||
| 154 | ch = (CustomHeader *)all_hdrs->data; | ||
| 155 | custom_header_free(ch); | ||
| 156 | all_hdrs = g_slist_remove(all_hdrs, ch); | ||
| 157 | } | ||
| 158 | |||
| 159 | if (prefs_file_close(pfile) < 0) { | ||
| 160 | g_warning("failed to write configuration to file\n"); | ||
| 161 | return; | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | gchar *custom_header_get_str(CustomHeader *ch) | ||
| 166 | { | ||
| 167 | return g_strdup_printf("%i:%s: %s", | ||
| 168 | ch->account_id, ch->name, | ||
| 169 | ch->value ? ch->value : ""); | ||
| 170 | } | ||
| 171 | |||
| 172 | CustomHeader *custom_header_read_str(const gchar *buf) | ||
| 173 | { | ||
| 174 | CustomHeader *ch; | ||
| 175 | gchar *account_id_str; | ||
| 176 | gint id; | ||
| 177 | gchar *name; | ||
| 178 | gchar *value; | ||
| 179 | gchar *tmp; | ||
| 180 | |||
| 181 | Xstrdup_a(tmp, buf, return NULL); | ||
| 182 | |||
| 183 | account_id_str = tmp; | ||
| 184 | |||
| 185 | name = strchr(account_id_str, ':'); | ||
| 186 | if (!name) | ||
| 187 | return NULL; | ||
| 188 | else { | ||
| 189 | gchar *endp; | ||
| 190 | |||
| 191 | *name++ = '\0'; | ||
| 192 | id = strtol(account_id_str, &endp, 10); | ||
| 193 | if (*endp != '\0') return NULL; | ||
| 194 | } | ||
| 195 | |||
| 196 | value = strchr(name, ':'); | ||
| 197 | if (!value) return NULL; | ||
| 198 | |||
| 199 | *value++ = '\0'; | ||
| 200 | |||
| 201 | g_strstrip(name); | ||
| 202 | g_strstrip(value); | ||
| 203 | |||
| 204 | ch = g_new0(CustomHeader, 1); | ||
| 205 | ch->account_id = id; | ||
| 206 | ch->name = *name ? g_strdup(name) : NULL; | ||
| 207 | ch->value = *value ? g_strdup(value) : NULL; | ||
| 208 | |||
| 209 | return ch; | ||
| 210 | } | ||
| 211 | |||
| 212 | CustomHeader *custom_header_find(GSList *header_list, const gchar *header) | ||
| 213 | { | ||
| 214 | GSList *cur; | ||
| 215 | CustomHeader *chdr; | ||
| 216 | |||
| 217 | for (cur = header_list; cur != NULL; cur = cur->next) { | ||
| 218 | chdr = (CustomHeader *)cur->data; | ||
| 219 | if (!g_ascii_strcasecmp(chdr->name, header)) | ||
| 220 | return chdr; | ||
| 221 | } | ||
| 222 | |||
| 223 | return NULL; | ||
| 224 | } | ||
| 225 | |||
| 226 | void custom_header_free(CustomHeader *ch) | ||
| 227 | { | ||
| 228 | if (!ch) return; | ||
| 229 | |||
| 230 | g_free(ch->name); | ||
| 231 | g_free(ch->value); | ||
| 232 | g_free(ch); | ||
| 233 | } | ||
