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/procmsg.c | |
Initial commit for sylpheed 3.7.0
Diffstat (limited to 'libsylph/procmsg.c')
| -rw-r--r-- | libsylph/procmsg.c | 2446 |
1 files changed, 2446 insertions, 0 deletions
diff --git a/libsylph/procmsg.c b/libsylph/procmsg.c new file mode 100644 index 0000000..28d16f3 --- /dev/null +++ b/libsylph/procmsg.c | |||
| @@ -0,0 +1,2446 @@ | |||
| 1 | /* | ||
| 2 | * LibSylph -- E-Mail client library | ||
| 3 | * Copyright (C) 1999-2013 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 | #include "defs.h" | ||
| 21 | |||
| 22 | #include <glib.h> | ||
| 23 | #include <glib/gi18n.h> | ||
| 24 | #include <stdio.h> | ||
| 25 | #include <stdlib.h> | ||
| 26 | #include <errno.h> | ||
| 27 | |||
| 28 | #include "utils.h" | ||
| 29 | #include "procmsg.h" | ||
| 30 | #include "procheader.h" | ||
| 31 | #include "account.h" | ||
| 32 | #include "procmime.h" | ||
| 33 | #include "prefs_common.h" | ||
| 34 | #include "folder.h" | ||
| 35 | #include "codeconv.h" | ||
| 36 | |||
| 37 | typedef struct _MsgFlagInfo { | ||
| 38 | guint msgnum; | ||
| 39 | MsgFlags flags; | ||
| 40 | } MsgFlagInfo; | ||
| 41 | |||
| 42 | static GSList *procmsg_read_cache_queue (FolderItem *item, | ||
| 43 | gboolean scan_file); | ||
| 44 | |||
| 45 | static void mark_sum_func (gpointer key, | ||
| 46 | gpointer value, | ||
| 47 | gpointer data); | ||
| 48 | |||
| 49 | static GHashTable *procmsg_read_mark_file (FolderItem *item); | ||
| 50 | static void procmsg_write_mark_file (FolderItem *item, | ||
| 51 | GHashTable *mark_table); | ||
| 52 | |||
| 53 | static GMappedFile *procmsg_open_cache_file_mmap(FolderItem *item, | ||
| 54 | DataOpenMode mode); | ||
| 55 | |||
| 56 | static gint procmsg_cmp_by_mark (gconstpointer a, | ||
| 57 | gconstpointer b); | ||
| 58 | static gint procmsg_cmp_by_unread (gconstpointer a, | ||
| 59 | gconstpointer b); | ||
| 60 | static gint procmsg_cmp_by_mime (gconstpointer a, | ||
| 61 | gconstpointer b); | ||
| 62 | static gint procmsg_cmp_by_label (gconstpointer a, | ||
| 63 | gconstpointer b); | ||
| 64 | static gint procmsg_cmp_by_number (gconstpointer a, | ||
| 65 | gconstpointer b); | ||
| 66 | static gint procmsg_cmp_by_size (gconstpointer a, | ||
| 67 | gconstpointer b); | ||
| 68 | static gint procmsg_cmp_by_date (gconstpointer a, | ||
| 69 | gconstpointer b); | ||
| 70 | static gint procmsg_cmp_by_from (gconstpointer a, | ||
| 71 | gconstpointer b); | ||
| 72 | static gint procmsg_cmp_by_to (gconstpointer a, | ||
| 73 | gconstpointer b); | ||
| 74 | static gint procmsg_cmp_by_subject (gconstpointer a, | ||
| 75 | gconstpointer b); | ||
| 76 | |||
| 77 | |||
| 78 | GHashTable *procmsg_msg_hash_table_create(GSList *mlist) | ||
| 79 | { | ||
| 80 | GHashTable *msg_table; | ||
| 81 | |||
| 82 | if (mlist == NULL) return NULL; | ||
| 83 | |||
| 84 | msg_table = g_hash_table_new(NULL, g_direct_equal); | ||
| 85 | procmsg_msg_hash_table_append(msg_table, mlist); | ||
| 86 | |||
| 87 | return msg_table; | ||
| 88 | } | ||
| 89 | |||
| 90 | void procmsg_msg_hash_table_append(GHashTable *msg_table, GSList *mlist) | ||
| 91 | { | ||
| 92 | GSList *cur; | ||
| 93 | MsgInfo *msginfo; | ||
| 94 | |||
| 95 | if (msg_table == NULL || mlist == NULL) return; | ||
| 96 | |||
| 97 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 98 | msginfo = (MsgInfo *)cur->data; | ||
| 99 | |||
| 100 | g_hash_table_insert(msg_table, | ||
| 101 | GUINT_TO_POINTER(msginfo->msgnum), | ||
| 102 | msginfo); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | GHashTable *procmsg_to_folder_hash_table_create(GSList *mlist) | ||
| 107 | { | ||
| 108 | GHashTable *msg_table; | ||
| 109 | GSList *cur; | ||
| 110 | MsgInfo *msginfo; | ||
| 111 | |||
| 112 | if (mlist == NULL) return NULL; | ||
| 113 | |||
| 114 | msg_table = g_hash_table_new(NULL, g_direct_equal); | ||
| 115 | |||
| 116 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 117 | msginfo = (MsgInfo *)cur->data; | ||
| 118 | g_hash_table_insert(msg_table, msginfo->to_folder, msginfo); | ||
| 119 | } | ||
| 120 | |||
| 121 | return msg_table; | ||
| 122 | } | ||
| 123 | |||
| 124 | gint procmsg_read_cache_data_str(FILE *fp, gchar **str) | ||
| 125 | { | ||
| 126 | gchar buf[BUFFSIZE]; | ||
| 127 | guint32 len; | ||
| 128 | gchar *tmp = NULL; | ||
| 129 | |||
| 130 | if (fread(&len, sizeof(len), 1, fp) != 1) | ||
| 131 | return -1; | ||
| 132 | |||
| 133 | if (len > G_MAXINT) | ||
| 134 | return -1; | ||
| 135 | |||
| 136 | while (len > 0) { | ||
| 137 | size_t size = MIN(len, BUFFSIZE - 1); | ||
| 138 | |||
| 139 | if (fread(buf, size, 1, fp) != 1) { | ||
| 140 | if (tmp) | ||
| 141 | g_free(tmp); | ||
| 142 | *str = NULL; | ||
| 143 | return -1; | ||
| 144 | } | ||
| 145 | |||
| 146 | buf[size] = '\0'; | ||
| 147 | if (tmp) { | ||
| 148 | *str = g_strconcat(tmp, buf, NULL); | ||
| 149 | g_free(tmp); | ||
| 150 | tmp = *str; | ||
| 151 | } else | ||
| 152 | tmp = *str = g_strdup(buf); | ||
| 153 | |||
| 154 | len -= size; | ||
| 155 | } | ||
| 156 | |||
| 157 | return 0; | ||
| 158 | } | ||
| 159 | |||
| 160 | static gint procmsg_read_cache_data_str_mem(const gchar **p, const gchar *endp, gchar **str) | ||
| 161 | { | ||
| 162 | guint32 len; | ||
| 163 | |||
| 164 | if (endp - *p < sizeof(len)) | ||
| 165 | return -1; | ||
| 166 | |||
| 167 | memcpy(&len, *p, sizeof(len)); | ||
| 168 | *p += sizeof(len); | ||
| 169 | if (len > G_MAXINT || len > endp - *p) | ||
| 170 | return -1; | ||
| 171 | |||
| 172 | if (len > 0) { | ||
| 173 | *str = g_strndup(*p, len); | ||
| 174 | *p += len; | ||
| 175 | } | ||
| 176 | |||
| 177 | return 0; | ||
| 178 | } | ||
| 179 | |||
| 180 | #define READ_CACHE_DATA(data) \ | ||
| 181 | { \ | ||
| 182 | if (procmsg_read_cache_data_str_mem(&p, endp, &data) < 0) { \ | ||
| 183 | g_warning("Cache data is corrupted\n"); \ | ||
| 184 | procmsg_msginfo_free(msginfo); \ | ||
| 185 | procmsg_msg_list_free(mlist); \ | ||
| 186 | g_mapped_file_free(mapfile); \ | ||
| 187 | return NULL; \ | ||
| 188 | } \ | ||
| 189 | } | ||
| 190 | |||
| 191 | #define READ_CACHE_DATA_INT(n) \ | ||
| 192 | { \ | ||
| 193 | if (endp - p < sizeof(guint32)) { \ | ||
| 194 | g_warning("Cache data is corrupted\n"); \ | ||
| 195 | procmsg_msginfo_free(msginfo); \ | ||
| 196 | procmsg_msg_list_free(mlist); \ | ||
| 197 | g_mapped_file_free(mapfile); \ | ||
| 198 | return NULL; \ | ||
| 199 | } else { \ | ||
| 200 | guint32 idata; \ | ||
| 201 | memcpy(&idata, p, sizeof(idata)); \ | ||
| 202 | n = idata; \ | ||
| 203 | p += sizeof(guint32); \ | ||
| 204 | } \ | ||
| 205 | } | ||
| 206 | |||
| 207 | GSList *procmsg_read_cache(FolderItem *item, gboolean scan_file) | ||
| 208 | { | ||
| 209 | GSList *mlist = NULL; | ||
| 210 | GSList *last = NULL; | ||
| 211 | GMappedFile *mapfile; | ||
| 212 | const gchar *filep; | ||
| 213 | gsize file_len; | ||
| 214 | const gchar *p, *endp; | ||
| 215 | MsgInfo *msginfo; | ||
| 216 | MsgFlags default_flags; | ||
| 217 | guint32 num; | ||
| 218 | guint refnum; | ||
| 219 | FolderType type; | ||
| 220 | |||
| 221 | g_return_val_if_fail(item != NULL, NULL); | ||
| 222 | g_return_val_if_fail(item->folder != NULL, NULL); | ||
| 223 | type = FOLDER_TYPE(item->folder); | ||
| 224 | |||
| 225 | default_flags.perm_flags = MSG_NEW|MSG_UNREAD; | ||
| 226 | default_flags.tmp_flags = 0; | ||
| 227 | if (type == F_MH || type == F_IMAP) { | ||
| 228 | if (item->stype == F_QUEUE) { | ||
| 229 | MSG_SET_TMP_FLAGS(default_flags, MSG_QUEUED); | ||
| 230 | } else if (item->stype == F_DRAFT) { | ||
| 231 | MSG_SET_TMP_FLAGS(default_flags, MSG_DRAFT); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | if (type == F_IMAP) { | ||
| 235 | MSG_SET_TMP_FLAGS(default_flags, MSG_IMAP); | ||
| 236 | } else if (type == F_NEWS) { | ||
| 237 | MSG_SET_TMP_FLAGS(default_flags, MSG_NEWS); | ||
| 238 | } | ||
| 239 | |||
| 240 | if (type == F_MH) { | ||
| 241 | gchar *path; | ||
| 242 | |||
| 243 | path = folder_item_get_path(item); | ||
| 244 | if (change_dir(path) < 0) { | ||
| 245 | g_free(path); | ||
| 246 | return NULL; | ||
| 247 | } | ||
| 248 | g_free(path); | ||
| 249 | } | ||
| 250 | |||
| 251 | mapfile = procmsg_open_cache_file_mmap(item, DATA_READ); | ||
| 252 | if (!mapfile) { | ||
| 253 | item->cache_dirty = TRUE; | ||
| 254 | return NULL; | ||
| 255 | } | ||
| 256 | |||
| 257 | debug_print("Reading summary cache...\n"); | ||
| 258 | |||
| 259 | filep = g_mapped_file_get_contents(mapfile); | ||
| 260 | file_len = g_mapped_file_get_length(mapfile); | ||
| 261 | endp = filep + file_len; | ||
| 262 | p = filep + sizeof(guint32); /* version */ | ||
| 263 | |||
| 264 | while (endp - p >= sizeof(num)) { | ||
| 265 | msginfo = g_new0(MsgInfo, 1); | ||
| 266 | |||
| 267 | READ_CACHE_DATA_INT(msginfo->msgnum); | ||
| 268 | |||
| 269 | READ_CACHE_DATA_INT(msginfo->size); | ||
| 270 | READ_CACHE_DATA_INT(msginfo->mtime); | ||
| 271 | READ_CACHE_DATA_INT(msginfo->date_t); | ||
| 272 | READ_CACHE_DATA_INT(msginfo->flags.tmp_flags); | ||
| 273 | |||
| 274 | READ_CACHE_DATA(msginfo->fromname); | ||
| 275 | |||
| 276 | READ_CACHE_DATA(msginfo->date); | ||
| 277 | READ_CACHE_DATA(msginfo->from); | ||
| 278 | READ_CACHE_DATA(msginfo->to); | ||
| 279 | READ_CACHE_DATA(msginfo->newsgroups); | ||
| 280 | READ_CACHE_DATA(msginfo->subject); | ||
| 281 | READ_CACHE_DATA(msginfo->msgid); | ||
| 282 | READ_CACHE_DATA(msginfo->inreplyto); | ||
| 283 | |||
| 284 | READ_CACHE_DATA_INT(refnum); | ||
| 285 | for (; refnum != 0; refnum--) { | ||
| 286 | gchar *ref; | ||
| 287 | |||
| 288 | READ_CACHE_DATA(ref); | ||
| 289 | msginfo->references = | ||
| 290 | g_slist_prepend(msginfo->references, ref); | ||
| 291 | } | ||
| 292 | if (msginfo->references) | ||
| 293 | msginfo->references = | ||
| 294 | g_slist_reverse(msginfo->references); | ||
| 295 | |||
| 296 | MSG_SET_PERM_FLAGS(msginfo->flags, default_flags.perm_flags); | ||
| 297 | MSG_SET_TMP_FLAGS(msginfo->flags, default_flags.tmp_flags); | ||
| 298 | |||
| 299 | /* if the message file doesn't exist or is changed, | ||
| 300 | don't add the data */ | ||
| 301 | if ((type == F_MH && scan_file && | ||
| 302 | folder_item_is_msg_changed(item, msginfo)) || | ||
| 303 | msginfo->msgnum == 0) { | ||
| 304 | procmsg_msginfo_free(msginfo); | ||
| 305 | item->cache_dirty = TRUE; | ||
| 306 | } else { | ||
| 307 | msginfo->folder = item; | ||
| 308 | |||
| 309 | if (!mlist) | ||
| 310 | last = mlist = g_slist_append(NULL, msginfo); | ||
| 311 | else { | ||
| 312 | last = g_slist_append(last, msginfo); | ||
| 313 | last = last->next; | ||
| 314 | } | ||
| 315 | } | ||
| 316 | } | ||
| 317 | |||
| 318 | g_mapped_file_free(mapfile); | ||
| 319 | |||
| 320 | if (item->cache_queue) { | ||
| 321 | GSList *qlist; | ||
| 322 | qlist = procmsg_read_cache_queue(item, scan_file); | ||
| 323 | mlist = g_slist_concat(mlist, qlist); | ||
| 324 | } | ||
| 325 | |||
| 326 | debug_print("done.\n"); | ||
| 327 | |||
| 328 | return mlist; | ||
| 329 | } | ||
| 330 | |||
| 331 | #undef READ_CACHE_DATA | ||
| 332 | #undef READ_CACHE_DATA_INT | ||
| 333 | |||
| 334 | static GSList *procmsg_read_cache_queue(FolderItem *item, gboolean scan_file) | ||
| 335 | { | ||
| 336 | FolderType type; | ||
| 337 | MsgInfo *msginfo; | ||
| 338 | MsgFlags default_flags; | ||
| 339 | GSList *cur; | ||
| 340 | GSList *qlist = NULL; | ||
| 341 | |||
| 342 | g_return_val_if_fail(item != NULL, NULL); | ||
| 343 | g_return_val_if_fail(item->folder != NULL, NULL); | ||
| 344 | |||
| 345 | if (!item->cache_queue) | ||
| 346 | return NULL; | ||
| 347 | |||
| 348 | debug_print("Reading cache queue...\n"); | ||
| 349 | |||
| 350 | type = FOLDER_TYPE(item->folder); | ||
| 351 | default_flags.perm_flags = MSG_NEW|MSG_UNREAD; | ||
| 352 | default_flags.tmp_flags = 0; | ||
| 353 | |||
| 354 | for (cur = item->cache_queue; cur != NULL; cur = cur->next) { | ||
| 355 | msginfo = (MsgInfo *)cur->data; | ||
| 356 | |||
| 357 | debug_print("read cache queue: %s/%d\n", | ||
| 358 | item->path, msginfo->msgnum); | ||
| 359 | |||
| 360 | MSG_SET_PERM_FLAGS(msginfo->flags, default_flags.perm_flags); | ||
| 361 | MSG_SET_TMP_FLAGS(msginfo->flags, default_flags.tmp_flags); | ||
| 362 | |||
| 363 | if ((type == F_MH && scan_file && | ||
| 364 | folder_item_is_msg_changed(item, msginfo))) { | ||
| 365 | procmsg_msginfo_free(msginfo); | ||
| 366 | item->cache_dirty = TRUE; | ||
| 367 | } else { | ||
| 368 | msginfo->folder = item; | ||
| 369 | qlist = g_slist_prepend(qlist, msginfo); | ||
| 370 | } | ||
| 371 | } | ||
| 372 | |||
| 373 | g_slist_free(item->cache_queue); | ||
| 374 | item->cache_queue = NULL; | ||
| 375 | item->cache_dirty = TRUE; | ||
| 376 | |||
| 377 | return qlist; | ||
| 378 | } | ||
| 379 | |||
| 380 | static void mark_unset_new_func(gpointer key, gpointer value, gpointer data) | ||
| 381 | { | ||
| 382 | MSG_UNSET_PERM_FLAGS(*((MsgFlags *)value), MSG_NEW); | ||
| 383 | } | ||
| 384 | |||
| 385 | void procmsg_set_flags(GSList *mlist, FolderItem *item) | ||
| 386 | { | ||
| 387 | GSList *cur; | ||
| 388 | gint new = 0, unread = 0, total = 0; | ||
| 389 | gint lastnum = 0; | ||
| 390 | gint unflagged = 0; | ||
| 391 | gboolean mark_queue_exist; | ||
| 392 | MsgInfo *msginfo; | ||
| 393 | GHashTable *mark_table; | ||
| 394 | MsgFlags *flags; | ||
| 395 | |||
| 396 | g_return_if_fail(item != NULL); | ||
| 397 | g_return_if_fail(item->folder != NULL); | ||
| 398 | |||
| 399 | debug_print("Marking the messages...\n"); | ||
| 400 | |||
| 401 | mark_queue_exist = (item->mark_queue != NULL); | ||
| 402 | mark_table = procmsg_read_mark_file(item); | ||
| 403 | if (!mark_table) { | ||
| 404 | item->new = item->unread = item->total = g_slist_length(mlist); | ||
| 405 | item->updated = TRUE; | ||
| 406 | item->mark_dirty = TRUE; | ||
| 407 | return; | ||
| 408 | } | ||
| 409 | |||
| 410 | /* unset new flags if new (unflagged) messages exist */ | ||
| 411 | if (!mark_queue_exist) { | ||
| 412 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 413 | msginfo = (MsgInfo *)cur->data; | ||
| 414 | flags = g_hash_table_lookup | ||
| 415 | (mark_table, GUINT_TO_POINTER(msginfo->msgnum)); | ||
| 416 | if (!flags) { | ||
| 417 | g_hash_table_foreach(mark_table, | ||
| 418 | mark_unset_new_func, NULL); | ||
| 419 | item->mark_dirty = TRUE; | ||
| 420 | break; | ||
| 421 | } | ||
| 422 | } | ||
| 423 | } | ||
| 424 | |||
| 425 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 426 | msginfo = (MsgInfo *)cur->data; | ||
| 427 | |||
| 428 | if (lastnum < msginfo->msgnum) | ||
| 429 | lastnum = msginfo->msgnum; | ||
| 430 | |||
| 431 | flags = g_hash_table_lookup | ||
| 432 | (mark_table, GUINT_TO_POINTER(msginfo->msgnum)); | ||
| 433 | |||
| 434 | if (flags != NULL) { | ||
| 435 | /* add the permanent flags only */ | ||
| 436 | msginfo->flags.perm_flags = flags->perm_flags; | ||
| 437 | if (MSG_IS_NEW(*flags)) | ||
| 438 | ++new; | ||
| 439 | if (MSG_IS_UNREAD(*flags)) | ||
| 440 | ++unread; | ||
| 441 | if (FOLDER_TYPE(item->folder) == F_IMAP) { | ||
| 442 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_IMAP); | ||
| 443 | } else if (FOLDER_TYPE(item->folder) == F_NEWS) { | ||
| 444 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_NEWS); | ||
| 445 | } | ||
| 446 | } else { | ||
| 447 | ++unflagged; | ||
| 448 | ++new; | ||
| 449 | ++unread; | ||
| 450 | } | ||
| 451 | |||
| 452 | ++total; | ||
| 453 | } | ||
| 454 | |||
| 455 | item->new = new; | ||
| 456 | item->unread = unread; | ||
| 457 | item->total = total; | ||
| 458 | item->unmarked_num = unflagged; | ||
| 459 | item->last_num = lastnum; | ||
| 460 | item->updated = TRUE; | ||
| 461 | |||
| 462 | if (unflagged > 0) | ||
| 463 | item->mark_dirty = TRUE; | ||
| 464 | |||
| 465 | debug_print("new: %d unread: %d unflagged: %d total: %d\n", | ||
| 466 | new, unread, unflagged, total); | ||
| 467 | |||
| 468 | hash_free_value_mem(mark_table); | ||
| 469 | g_hash_table_destroy(mark_table); | ||
| 470 | } | ||
| 471 | |||
| 472 | static void mark_all_read_func(gpointer key, gpointer value, gpointer data) | ||
| 473 | { | ||
| 474 | MSG_UNSET_PERM_FLAGS(*((MsgFlags *)value), MSG_NEW|MSG_UNREAD); | ||
| 475 | } | ||
| 476 | |||
| 477 | void procmsg_mark_all_read(FolderItem *item) | ||
| 478 | { | ||
| 479 | GHashTable *mark_table; | ||
| 480 | |||
| 481 | debug_print("Marking all messages as read\n"); | ||
| 482 | |||
| 483 | mark_table = procmsg_read_mark_file(item); | ||
| 484 | if (mark_table) { | ||
| 485 | g_hash_table_foreach(mark_table, mark_all_read_func, NULL); | ||
| 486 | procmsg_write_mark_file(item, mark_table); | ||
| 487 | hash_free_value_mem(mark_table); | ||
| 488 | g_hash_table_destroy(mark_table); | ||
| 489 | } | ||
| 490 | |||
| 491 | if (item->mark_queue) { | ||
| 492 | GSList *cur; | ||
| 493 | MsgFlagInfo *flaginfo; | ||
| 494 | |||
| 495 | for (cur = item->mark_queue; cur != NULL; cur = cur->next) { | ||
| 496 | flaginfo = (MsgFlagInfo *)cur->data; | ||
| 497 | MSG_UNSET_PERM_FLAGS | ||
| 498 | (flaginfo->flags, MSG_NEW|MSG_UNREAD); | ||
| 499 | } | ||
| 500 | item->mark_dirty = TRUE; | ||
| 501 | } | ||
| 502 | |||
| 503 | item->new = item->unread = 0; | ||
| 504 | } | ||
| 505 | |||
| 506 | static FolderSortType cmp_func_sort_type; | ||
| 507 | |||
| 508 | GSList *procmsg_sort_msg_list(GSList *mlist, FolderSortKey sort_key, | ||
| 509 | FolderSortType sort_type) | ||
| 510 | { | ||
| 511 | GCompareFunc cmp_func; | ||
| 512 | |||
| 513 | switch (sort_key) { | ||
| 514 | case SORT_BY_MARK: | ||
| 515 | cmp_func = procmsg_cmp_by_mark; break; | ||
| 516 | case SORT_BY_UNREAD: | ||
| 517 | cmp_func = procmsg_cmp_by_unread; break; | ||
| 518 | case SORT_BY_MIME: | ||
| 519 | cmp_func = procmsg_cmp_by_mime; break; | ||
| 520 | case SORT_BY_LABEL: | ||
| 521 | cmp_func = procmsg_cmp_by_label; break; | ||
| 522 | case SORT_BY_NUMBER: | ||
| 523 | cmp_func = procmsg_cmp_by_number; break; | ||
| 524 | case SORT_BY_SIZE: | ||
| 525 | cmp_func = procmsg_cmp_by_size; break; | ||
| 526 | case SORT_BY_DATE: | ||
| 527 | cmp_func = procmsg_cmp_by_date; break; | ||
| 528 | case SORT_BY_FROM: | ||
| 529 | cmp_func = procmsg_cmp_by_from; break; | ||
| 530 | case SORT_BY_SUBJECT: | ||
| 531 | cmp_func = procmsg_cmp_by_subject; break; | ||
| 532 | case SORT_BY_TO: | ||
| 533 | cmp_func = procmsg_cmp_by_to; break; | ||
| 534 | default: | ||
| 535 | return mlist; | ||
| 536 | } | ||
| 537 | |||
| 538 | cmp_func_sort_type = sort_type; | ||
| 539 | |||
| 540 | mlist = g_slist_sort(mlist, cmp_func); | ||
| 541 | |||
| 542 | return mlist; | ||
| 543 | } | ||
| 544 | |||
| 545 | gint procmsg_get_last_num_in_msg_list(GSList *mlist) | ||
| 546 | { | ||
| 547 | GSList *cur; | ||
| 548 | MsgInfo *msginfo; | ||
| 549 | gint last = 0; | ||
| 550 | |||
| 551 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 552 | msginfo = (MsgInfo *)cur->data; | ||
| 553 | if (msginfo && msginfo->msgnum > last) | ||
| 554 | last = msginfo->msgnum; | ||
| 555 | } | ||
| 556 | |||
| 557 | return last; | ||
| 558 | } | ||
| 559 | |||
| 560 | void procmsg_msg_list_free(GSList *mlist) | ||
| 561 | { | ||
| 562 | GSList *cur; | ||
| 563 | MsgInfo *msginfo; | ||
| 564 | |||
| 565 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 566 | msginfo = (MsgInfo *)cur->data; | ||
| 567 | procmsg_msginfo_free(msginfo); | ||
| 568 | } | ||
| 569 | g_slist_free(mlist); | ||
| 570 | } | ||
| 571 | |||
| 572 | void procmsg_write_cache(MsgInfo *msginfo, FILE *fp) | ||
| 573 | { | ||
| 574 | MsgTmpFlags flags = msginfo->flags.tmp_flags & MSG_CACHED_FLAG_MASK; | ||
| 575 | GSList *cur; | ||
| 576 | |||
| 577 | WRITE_CACHE_DATA_INT(msginfo->msgnum, fp); | ||
| 578 | WRITE_CACHE_DATA_INT(msginfo->size, fp); | ||
| 579 | WRITE_CACHE_DATA_INT(msginfo->mtime, fp); | ||
| 580 | WRITE_CACHE_DATA_INT(msginfo->date_t, fp); | ||
| 581 | WRITE_CACHE_DATA_INT(flags, fp); | ||
| 582 | |||
| 583 | WRITE_CACHE_DATA(msginfo->fromname, fp); | ||
| 584 | |||
| 585 | WRITE_CACHE_DATA(msginfo->date, fp); | ||
| 586 | WRITE_CACHE_DATA(msginfo->from, fp); | ||
| 587 | WRITE_CACHE_DATA(msginfo->to, fp); | ||
| 588 | WRITE_CACHE_DATA(msginfo->newsgroups, fp); | ||
| 589 | WRITE_CACHE_DATA(msginfo->subject, fp); | ||
| 590 | WRITE_CACHE_DATA(msginfo->msgid, fp); | ||
| 591 | WRITE_CACHE_DATA(msginfo->inreplyto, fp); | ||
| 592 | |||
| 593 | WRITE_CACHE_DATA_INT(g_slist_length(msginfo->references), fp); | ||
| 594 | for (cur = msginfo->references; cur != NULL; cur = cur->next) { | ||
| 595 | WRITE_CACHE_DATA((gchar *)cur->data, fp); | ||
| 596 | } | ||
| 597 | } | ||
| 598 | |||
| 599 | void procmsg_write_flags(MsgInfo *msginfo, FILE *fp) | ||
| 600 | { | ||
| 601 | MsgPermFlags flags = msginfo->flags.perm_flags; | ||
| 602 | |||
| 603 | WRITE_CACHE_DATA_INT(msginfo->msgnum, fp); | ||
| 604 | WRITE_CACHE_DATA_INT(flags, fp); | ||
| 605 | } | ||
| 606 | |||
| 607 | void procmsg_write_cache_list(FolderItem *item, GSList *mlist) | ||
| 608 | { | ||
| 609 | FILE *fp; | ||
| 610 | GSList *cur; | ||
| 611 | |||
| 612 | g_return_if_fail(item != NULL); | ||
| 613 | |||
| 614 | debug_print("Writing summary cache (%s)\n", item->path); | ||
| 615 | |||
| 616 | fp = procmsg_open_cache_file(item, DATA_WRITE); | ||
| 617 | if (fp == NULL) | ||
| 618 | return; | ||
| 619 | |||
| 620 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 621 | MsgInfo *msginfo = (MsgInfo *)cur->data; | ||
| 622 | procmsg_write_cache(msginfo, fp); | ||
| 623 | } | ||
| 624 | |||
| 625 | if (item->cache_queue) | ||
| 626 | procmsg_flush_cache_queue(item, fp); | ||
| 627 | |||
| 628 | fclose(fp); | ||
| 629 | item->cache_dirty = FALSE; | ||
| 630 | } | ||
| 631 | |||
| 632 | void procmsg_write_flags_list(FolderItem *item, GSList *mlist) | ||
| 633 | { | ||
| 634 | FILE *fp; | ||
| 635 | GSList *cur; | ||
| 636 | |||
| 637 | g_return_if_fail(item != NULL); | ||
| 638 | |||
| 639 | debug_print("Writing summary flags (%s)\n", item->path); | ||
| 640 | |||
| 641 | fp = procmsg_open_mark_file(item, DATA_WRITE); | ||
| 642 | if (fp == NULL) | ||
| 643 | return; | ||
| 644 | |||
| 645 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 646 | MsgInfo *msginfo = (MsgInfo *)cur->data; | ||
| 647 | procmsg_write_flags(msginfo, fp); | ||
| 648 | } | ||
| 649 | |||
| 650 | if (item->mark_queue) | ||
| 651 | procmsg_flush_mark_queue(item, fp); | ||
| 652 | |||
| 653 | fclose(fp); | ||
| 654 | item->mark_dirty = FALSE; | ||
| 655 | } | ||
| 656 | |||
| 657 | static gint cmp_by_item(gconstpointer a, gconstpointer b) | ||
| 658 | { | ||
| 659 | const MsgInfo *msginfo1 = a; | ||
| 660 | const MsgInfo *msginfo2 = b; | ||
| 661 | |||
| 662 | if (msginfo1->folder == msginfo2->folder) | ||
| 663 | return msginfo1->msgnum - msginfo2->msgnum; | ||
| 664 | |||
| 665 | return msginfo1->folder - msginfo2->folder; | ||
| 666 | } | ||
| 667 | |||
| 668 | void procmsg_write_flags_for_multiple_folders(GSList *mlist) | ||
| 669 | { | ||
| 670 | GSList *tmp_list, *cur; | ||
| 671 | FolderItem *prev_item = NULL; | ||
| 672 | FILE *fp = NULL; | ||
| 673 | |||
| 674 | if (!mlist) | ||
| 675 | return; | ||
| 676 | |||
| 677 | tmp_list = g_slist_copy(mlist); | ||
| 678 | tmp_list = g_slist_sort(tmp_list, cmp_by_item); | ||
| 679 | |||
| 680 | for (cur = tmp_list; cur != NULL; cur = cur->next) { | ||
| 681 | MsgInfo *msginfo = (MsgInfo *)cur->data; | ||
| 682 | FolderItem *item = msginfo->folder; | ||
| 683 | |||
| 684 | if (prev_item != item) { | ||
| 685 | if (fp) | ||
| 686 | fclose(fp); | ||
| 687 | fp = procmsg_open_mark_file(item, DATA_APPEND); | ||
| 688 | if (!fp) { | ||
| 689 | g_warning("can't open mark file\n"); | ||
| 690 | break; | ||
| 691 | } | ||
| 692 | item->updated = TRUE; | ||
| 693 | } | ||
| 694 | procmsg_write_flags(msginfo, fp); | ||
| 695 | prev_item = item; | ||
| 696 | } | ||
| 697 | |||
| 698 | if (fp) | ||
| 699 | fclose(fp); | ||
| 700 | g_slist_free(tmp_list); | ||
| 701 | } | ||
| 702 | |||
| 703 | void procmsg_flush_mark_queue(FolderItem *item, FILE *fp) | ||
| 704 | { | ||
| 705 | MsgFlagInfo *flaginfo; | ||
| 706 | MsgInfo msginfo = {0}; | ||
| 707 | gboolean append = FALSE; | ||
| 708 | GSList *qlist, *cur; | ||
| 709 | |||
| 710 | g_return_if_fail(item != NULL); | ||
| 711 | |||
| 712 | if (!item->mark_queue) | ||
| 713 | return; | ||
| 714 | |||
| 715 | debug_print("flushing mark_queue: %s ...\n", item->path); | ||
| 716 | |||
| 717 | if (!fp) { | ||
| 718 | append = TRUE; | ||
| 719 | fp = procmsg_open_mark_file(item, DATA_APPEND); | ||
| 720 | g_return_if_fail(fp != NULL); | ||
| 721 | } | ||
| 722 | |||
| 723 | qlist = g_slist_reverse(item->mark_queue); | ||
| 724 | item->mark_queue = NULL; | ||
| 725 | |||
| 726 | for (cur = qlist; cur != NULL; cur = cur->next) { | ||
| 727 | flaginfo = (MsgFlagInfo *)cur->data; | ||
| 728 | |||
| 729 | msginfo.msgnum = flaginfo->msgnum; | ||
| 730 | msginfo.flags = flaginfo->flags; | ||
| 731 | procmsg_write_flags(&msginfo, fp); | ||
| 732 | g_free(flaginfo); | ||
| 733 | } | ||
| 734 | |||
| 735 | g_slist_free(qlist); | ||
| 736 | |||
| 737 | if (append) | ||
| 738 | fclose(fp); | ||
| 739 | } | ||
| 740 | |||
| 741 | void procmsg_add_mark_queue(FolderItem *item, gint num, MsgFlags flags) | ||
| 742 | { | ||
| 743 | MsgFlagInfo *flaginfo; | ||
| 744 | |||
| 745 | flaginfo = g_new(MsgFlagInfo, 1); | ||
| 746 | flaginfo->msgnum = num; | ||
| 747 | flaginfo->flags = flags; | ||
| 748 | item->mark_queue = g_slist_prepend(item->mark_queue, flaginfo); | ||
| 749 | } | ||
| 750 | |||
| 751 | void procmsg_flaginfo_list_free(GSList *flaglist) | ||
| 752 | { | ||
| 753 | GSList *cur; | ||
| 754 | MsgFlagInfo *flaginfo; | ||
| 755 | |||
| 756 | for (cur = flaglist; cur != NULL; cur = cur->next) { | ||
| 757 | flaginfo = (MsgFlagInfo *)cur->data; | ||
| 758 | g_free(flaginfo); | ||
| 759 | } | ||
| 760 | g_slist_free(flaglist); | ||
| 761 | } | ||
| 762 | |||
| 763 | void procmsg_flush_cache_queue(FolderItem *item, FILE *fp) | ||
| 764 | { | ||
| 765 | MsgInfo *msginfo; | ||
| 766 | gboolean append = FALSE; | ||
| 767 | GSList *qlist, *cur; | ||
| 768 | |||
| 769 | g_return_if_fail(item != NULL); | ||
| 770 | |||
| 771 | if (!item->cache_queue) | ||
| 772 | return; | ||
| 773 | |||
| 774 | debug_print("flushing cache_queue: %s ...\n", item->path); | ||
| 775 | |||
| 776 | if (!fp) { | ||
| 777 | append = TRUE; | ||
| 778 | fp = procmsg_open_cache_file(item, DATA_APPEND); | ||
| 779 | g_return_if_fail(fp != NULL); | ||
| 780 | } | ||
| 781 | |||
| 782 | qlist = g_slist_reverse(item->cache_queue); | ||
| 783 | item->cache_queue = NULL; | ||
| 784 | |||
| 785 | for (cur = qlist; cur != NULL; cur = cur->next) { | ||
| 786 | msginfo = (MsgInfo *)cur->data; | ||
| 787 | |||
| 788 | debug_print("flush cache queue: %s/%d\n", | ||
| 789 | item->path, msginfo->msgnum); | ||
| 790 | procmsg_write_cache(msginfo, fp); | ||
| 791 | procmsg_msginfo_free(msginfo); | ||
| 792 | } | ||
| 793 | |||
| 794 | g_slist_free(qlist); | ||
| 795 | |||
| 796 | if (append) | ||
| 797 | fclose(fp); | ||
| 798 | } | ||
| 799 | |||
| 800 | void procmsg_add_cache_queue(FolderItem *item, gint num, MsgInfo *msginfo) | ||
| 801 | { | ||
| 802 | MsgInfo *queue_msginfo; | ||
| 803 | |||
| 804 | g_return_if_fail(msginfo != NULL); | ||
| 805 | |||
| 806 | queue_msginfo = procmsg_msginfo_copy(msginfo); | ||
| 807 | queue_msginfo->msgnum = num; | ||
| 808 | queue_msginfo->folder = item; | ||
| 809 | if (queue_msginfo->file_path) { | ||
| 810 | g_free(queue_msginfo->file_path); | ||
| 811 | queue_msginfo->file_path = NULL; | ||
| 812 | } | ||
| 813 | |||
| 814 | debug_print("procmsg_add_cache_queue: add msg cache: %s/%d\n", | ||
| 815 | item->path, num); | ||
| 816 | item->cache_queue = g_slist_prepend(item->cache_queue, queue_msginfo); | ||
| 817 | } | ||
| 818 | |||
| 819 | gboolean procmsg_flush_folder(FolderItem *item) | ||
| 820 | { | ||
| 821 | gboolean flushed = FALSE; | ||
| 822 | gint n_new, n_unread, n_total, n_min, n_max; | ||
| 823 | |||
| 824 | g_return_val_if_fail(item != NULL, FALSE); | ||
| 825 | g_return_val_if_fail(item->folder != NULL, FALSE); | ||
| 826 | |||
| 827 | if (FOLDER_TYPE(item->folder) != F_MH || item->last_num < 0) { | ||
| 828 | folder_item_scan(item); | ||
| 829 | return TRUE; | ||
| 830 | } | ||
| 831 | |||
| 832 | if (item->mark_queue && !item->opened) | ||
| 833 | flushed = TRUE; | ||
| 834 | procmsg_get_mark_sum(item, &n_new, &n_unread, &n_total, &n_min, &n_max, | ||
| 835 | 0); | ||
| 836 | item->unmarked_num = 0; | ||
| 837 | item->new = n_new; | ||
| 838 | item->unread = n_unread; | ||
| 839 | item->total = n_total; | ||
| 840 | |||
| 841 | if (item->cache_queue && !item->opened) { | ||
| 842 | procmsg_flush_cache_queue(item, NULL); | ||
| 843 | flushed = TRUE; | ||
| 844 | } | ||
| 845 | |||
| 846 | if (flushed) | ||
| 847 | debug_print("procmsg_flush_folder: flushed %s\n", item->path); | ||
| 848 | |||
| 849 | return flushed; | ||
| 850 | } | ||
| 851 | |||
| 852 | static void procmsg_flush_folder_foreach_func(gpointer key, gpointer val, | ||
| 853 | gpointer data) | ||
| 854 | { | ||
| 855 | procmsg_flush_folder(FOLDER_ITEM(key)); | ||
| 856 | } | ||
| 857 | |||
| 858 | void procmsg_flush_folder_foreach(GHashTable *folder_table) | ||
| 859 | { | ||
| 860 | g_hash_table_foreach(folder_table, procmsg_flush_folder_foreach_func, | ||
| 861 | NULL); | ||
| 862 | } | ||
| 863 | |||
| 864 | void procmsg_add_flags(FolderItem *item, gint num, MsgFlags flags) | ||
| 865 | { | ||
| 866 | FILE *fp; | ||
| 867 | MsgInfo msginfo; | ||
| 868 | |||
| 869 | g_return_if_fail(item != NULL); | ||
| 870 | |||
| 871 | if (item->opened) { | ||
| 872 | procmsg_add_mark_queue(item, num, flags); | ||
| 873 | return; | ||
| 874 | } | ||
| 875 | |||
| 876 | if ((fp = procmsg_open_mark_file(item, DATA_APPEND)) == NULL) { | ||
| 877 | g_warning(_("can't open mark file\n")); | ||
| 878 | return; | ||
| 879 | } | ||
| 880 | |||
| 881 | msginfo.msgnum = num; | ||
| 882 | msginfo.flags = flags; | ||
| 883 | |||
| 884 | procmsg_write_flags(&msginfo, fp); | ||
| 885 | fclose(fp); | ||
| 886 | } | ||
| 887 | |||
| 888 | struct MarkSum { | ||
| 889 | gint *new; | ||
| 890 | gint *unread; | ||
| 891 | gint *total; | ||
| 892 | gint *min; | ||
| 893 | gint *max; | ||
| 894 | gint first; | ||
| 895 | }; | ||
| 896 | |||
| 897 | static void mark_sum_func(gpointer key, gpointer value, gpointer data) | ||
| 898 | { | ||
| 899 | MsgFlags *flags = value; | ||
| 900 | gint num = GPOINTER_TO_INT(key); | ||
| 901 | struct MarkSum *marksum = data; | ||
| 902 | |||
| 903 | if (marksum->first <= num) { | ||
| 904 | if (MSG_IS_NEW(*flags)) (*marksum->new)++; | ||
| 905 | if (MSG_IS_UNREAD(*flags)) (*marksum->unread)++; | ||
| 906 | if (num > *marksum->max) *marksum->max = num; | ||
| 907 | if (num < *marksum->min || *marksum->min == 0) *marksum->min = num; | ||
| 908 | (*marksum->total)++; | ||
| 909 | } | ||
| 910 | |||
| 911 | g_free(flags); | ||
| 912 | } | ||
| 913 | |||
| 914 | void procmsg_get_mark_sum(FolderItem *item, | ||
| 915 | gint *new, gint *unread, gint *total, | ||
| 916 | gint *min, gint *max, | ||
| 917 | gint first) | ||
| 918 | { | ||
| 919 | GHashTable *mark_table; | ||
| 920 | struct MarkSum marksum; | ||
| 921 | |||
| 922 | *new = *unread = *total = *min = *max = 0; | ||
| 923 | marksum.new = new; | ||
| 924 | marksum.unread = unread; | ||
| 925 | marksum.total = total; | ||
| 926 | marksum.min = min; | ||
| 927 | marksum.max = max; | ||
| 928 | marksum.first = first; | ||
| 929 | |||
| 930 | mark_table = procmsg_read_mark_file(item); | ||
| 931 | |||
| 932 | if (mark_table) { | ||
| 933 | g_hash_table_foreach(mark_table, mark_sum_func, &marksum); | ||
| 934 | g_hash_table_destroy(mark_table); | ||
| 935 | } | ||
| 936 | } | ||
| 937 | |||
| 938 | static GHashTable *procmsg_read_mark_file(FolderItem *item) | ||
| 939 | { | ||
| 940 | FILE *fp; | ||
| 941 | GHashTable *mark_table = NULL; | ||
| 942 | guint32 idata; | ||
| 943 | guint num; | ||
| 944 | MsgFlags *flags; | ||
| 945 | MsgPermFlags perm_flags; | ||
| 946 | GSList *cur; | ||
| 947 | |||
| 948 | if ((fp = procmsg_open_mark_file(item, DATA_READ)) == NULL) | ||
| 949 | return NULL; | ||
| 950 | |||
| 951 | mark_table = g_hash_table_new(NULL, g_direct_equal); | ||
| 952 | |||
| 953 | while (fread(&idata, sizeof(idata), 1, fp) == 1) { | ||
| 954 | num = idata; | ||
| 955 | if (fread(&idata, sizeof(idata), 1, fp) != 1) break; | ||
| 956 | perm_flags = idata; | ||
| 957 | |||
| 958 | flags = g_hash_table_lookup(mark_table, GUINT_TO_POINTER(num)); | ||
| 959 | if (flags != NULL) | ||
| 960 | g_free(flags); | ||
| 961 | |||
| 962 | flags = g_new0(MsgFlags, 1); | ||
| 963 | flags->perm_flags = perm_flags; | ||
| 964 | |||
| 965 | g_hash_table_insert(mark_table, GUINT_TO_POINTER(num), flags); | ||
| 966 | } | ||
| 967 | |||
| 968 | fclose(fp); | ||
| 969 | |||
| 970 | if (item->mark_queue) { | ||
| 971 | g_hash_table_foreach(mark_table, mark_unset_new_func, NULL); | ||
| 972 | item->mark_dirty = TRUE; | ||
| 973 | } | ||
| 974 | |||
| 975 | for (cur = item->mark_queue; cur != NULL; cur = cur->next) { | ||
| 976 | MsgFlagInfo *flaginfo = (MsgFlagInfo *)cur->data; | ||
| 977 | |||
| 978 | flags = g_hash_table_lookup(mark_table, | ||
| 979 | GUINT_TO_POINTER(flaginfo->msgnum)); | ||
| 980 | if (flags != NULL) | ||
| 981 | g_free(flags); | ||
| 982 | |||
| 983 | flags = g_new0(MsgFlags, 1); | ||
| 984 | flags->perm_flags = flaginfo->flags.perm_flags; | ||
| 985 | |||
| 986 | g_hash_table_insert(mark_table, | ||
| 987 | GUINT_TO_POINTER(flaginfo->msgnum), flags); | ||
| 988 | |||
| 989 | } | ||
| 990 | |||
| 991 | if (item->mark_queue && !item->opened) { | ||
| 992 | procmsg_write_mark_file(item, mark_table); | ||
| 993 | procmsg_flaginfo_list_free(item->mark_queue); | ||
| 994 | item->mark_queue = NULL; | ||
| 995 | item->mark_dirty = FALSE; | ||
| 996 | } | ||
| 997 | |||
| 998 | return mark_table; | ||
| 999 | } | ||
| 1000 | |||
| 1001 | static void write_mark_func(gpointer key, gpointer value, gpointer data) | ||
| 1002 | { | ||
| 1003 | MsgInfo msginfo; | ||
| 1004 | |||
| 1005 | msginfo.msgnum = GPOINTER_TO_UINT(key); | ||
| 1006 | msginfo.flags.perm_flags = ((MsgFlags *)value)->perm_flags; | ||
| 1007 | procmsg_write_flags(&msginfo, (FILE *)data); | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | static void procmsg_write_mark_file(FolderItem *item, GHashTable *mark_table) | ||
| 1011 | { | ||
| 1012 | FILE *fp; | ||
| 1013 | |||
| 1014 | if ((fp = procmsg_open_mark_file(item, DATA_WRITE)) == NULL) { | ||
| 1015 | g_warning("procmsg_write_mark_file: cannot open mark file."); | ||
| 1016 | return; | ||
| 1017 | } | ||
| 1018 | g_hash_table_foreach(mark_table, write_mark_func, fp); | ||
| 1019 | fclose(fp); | ||
| 1020 | } | ||
| 1021 | |||
| 1022 | FILE *procmsg_open_data_file(const gchar *file, guint version, | ||
| 1023 | DataOpenMode mode, gchar *buf, size_t buf_size) | ||
| 1024 | { | ||
| 1025 | FILE *fp; | ||
| 1026 | guint32 data_ver = 0; | ||
| 1027 | |||
| 1028 | g_return_val_if_fail(file != NULL, NULL); | ||
| 1029 | |||
| 1030 | if (mode == DATA_WRITE) { | ||
| 1031 | if ((fp = g_fopen(file, "wb")) == NULL) { | ||
| 1032 | if (errno == EACCES) { | ||
| 1033 | change_file_mode_rw(NULL, file); | ||
| 1034 | if ((fp = g_fopen(file, "wb")) == NULL) { | ||
| 1035 | FILE_OP_ERROR(file, "procmsg_open_data_file: fopen"); | ||
| 1036 | return NULL; | ||
| 1037 | } | ||
| 1038 | } else { | ||
| 1039 | FILE_OP_ERROR(file, "procmsg_open_data_file: fopen"); | ||
| 1040 | return NULL; | ||
| 1041 | } | ||
| 1042 | } | ||
| 1043 | if (change_file_mode_rw(fp, file) < 0) | ||
| 1044 | FILE_OP_ERROR(file, "chmod"); | ||
| 1045 | |||
| 1046 | WRITE_CACHE_DATA_INT(version, fp); | ||
| 1047 | return fp; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | /* check version */ | ||
| 1051 | if ((fp = g_fopen(file, "rb")) == NULL) { | ||
| 1052 | if (errno == EACCES) { | ||
| 1053 | change_file_mode_rw(NULL, file); | ||
| 1054 | if ((fp = g_fopen(file, "rb")) == NULL) { | ||
| 1055 | FILE_OP_ERROR(file, "procmsg_open_data_file: fopen"); | ||
| 1056 | } | ||
| 1057 | } else { | ||
| 1058 | debug_print("Mark/Cache file '%s' not found\n", file); | ||
| 1059 | } | ||
| 1060 | } | ||
| 1061 | |||
| 1062 | if (fp) { | ||
| 1063 | if (buf && buf_size > 0) | ||
| 1064 | setvbuf(fp, buf, _IOFBF, buf_size); | ||
| 1065 | if (fread(&data_ver, sizeof(data_ver), 1, fp) != 1) { | ||
| 1066 | g_warning("%s: cannot read mark/cache file (truncated?)\n", file); | ||
| 1067 | fclose(fp); | ||
| 1068 | fp = NULL; | ||
| 1069 | } else if (version != data_ver) { | ||
| 1070 | g_message("%s: Mark/Cache version is different (%u != %u). Discarding it.\n", | ||
| 1071 | file, data_ver, version); | ||
| 1072 | fclose(fp); | ||
| 1073 | fp = NULL; | ||
| 1074 | } | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | if (mode == DATA_READ) | ||
| 1078 | return fp; | ||
| 1079 | |||
| 1080 | if (fp) { | ||
| 1081 | /* reopen with append mode */ | ||
| 1082 | fclose(fp); | ||
| 1083 | if ((fp = g_fopen(file, "ab")) == NULL) { | ||
| 1084 | if (errno == EACCES) { | ||
| 1085 | change_file_mode_rw(NULL, file); | ||
| 1086 | if ((fp = g_fopen(file, "ab")) == NULL) { | ||
| 1087 | FILE_OP_ERROR(file, "procmsg_open_data_file: fopen"); | ||
| 1088 | } | ||
| 1089 | } else { | ||
| 1090 | FILE_OP_ERROR(file, "procmsg_open_data_file: fopen"); | ||
| 1091 | } | ||
| 1092 | } | ||
| 1093 | } else { | ||
| 1094 | /* open with overwrite mode if mark file doesn't exist or | ||
| 1095 | version is different */ | ||
| 1096 | fp = procmsg_open_data_file(file, version, DATA_WRITE, buf, | ||
| 1097 | buf_size); | ||
| 1098 | } | ||
| 1099 | |||
| 1100 | return fp; | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | static GMappedFile *procmsg_open_cache_file_mmap(FolderItem *item, | ||
| 1104 | DataOpenMode mode) | ||
| 1105 | { | ||
| 1106 | gchar *cachefile; | ||
| 1107 | GMappedFile *map = NULL; | ||
| 1108 | GError *error = NULL; | ||
| 1109 | gsize size; | ||
| 1110 | guint32 data_ver = 0; | ||
| 1111 | gchar *p; | ||
| 1112 | |||
| 1113 | if (mode != DATA_READ) | ||
| 1114 | return NULL; | ||
| 1115 | |||
| 1116 | cachefile = folder_item_get_cache_file(item); | ||
| 1117 | if (cachefile) { | ||
| 1118 | map = g_mapped_file_new(cachefile, FALSE, &error); | ||
| 1119 | if (!map) { | ||
| 1120 | if (error && error->code == G_FILE_ERROR_NOENT) | ||
| 1121 | debug_print("%s: mark/cache file not found\n", cachefile); | ||
| 1122 | else if (error) | ||
| 1123 | g_warning("%s: cannot open mark/cache file: %s", cachefile, error->message); | ||
| 1124 | else | ||
| 1125 | g_warning("%s: cannot open mark/cache file", cachefile); | ||
| 1126 | if (error) | ||
| 1127 | g_error_free(error); | ||
| 1128 | g_free(cachefile); | ||
| 1129 | return NULL; | ||
| 1130 | } | ||
| 1131 | size = g_mapped_file_get_length(map); | ||
| 1132 | if (size < sizeof(data_ver)) { | ||
| 1133 | g_warning("%s: cannot read mark/cache file (truncated?)", cachefile); | ||
| 1134 | g_mapped_file_free(map); | ||
| 1135 | g_free(cachefile); | ||
| 1136 | return NULL; | ||
| 1137 | } | ||
| 1138 | p = g_mapped_file_get_contents(map); | ||
| 1139 | data_ver = *(guint32 *)p; | ||
| 1140 | if (CACHE_VERSION != data_ver) { | ||
| 1141 | g_message("%s: Mark/Cache version is different (%u != %u). Discarding it.\n", | ||
| 1142 | cachefile, data_ver, CACHE_VERSION); | ||
| 1143 | g_mapped_file_free(map); | ||
| 1144 | g_free(cachefile); | ||
| 1145 | return NULL; | ||
| 1146 | } | ||
| 1147 | g_free(cachefile); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | return map; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | FILE *procmsg_open_cache_file(FolderItem *item, DataOpenMode mode) | ||
| 1154 | { | ||
| 1155 | gchar *cachefile; | ||
| 1156 | FILE *fp; | ||
| 1157 | |||
| 1158 | cachefile = folder_item_get_cache_file(item); | ||
| 1159 | fp = procmsg_open_data_file(cachefile, CACHE_VERSION, mode, NULL, 0); | ||
| 1160 | g_free(cachefile); | ||
| 1161 | |||
| 1162 | return fp; | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | FILE *procmsg_open_mark_file(FolderItem *item, DataOpenMode mode) | ||
| 1166 | { | ||
| 1167 | gchar *markfile; | ||
| 1168 | FILE *fp; | ||
| 1169 | |||
| 1170 | markfile = folder_item_get_mark_file(item); | ||
| 1171 | fp = procmsg_open_data_file(markfile, MARK_VERSION, mode, NULL, 0); | ||
| 1172 | g_free(markfile); | ||
| 1173 | |||
| 1174 | return fp; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | void procmsg_clear_cache(FolderItem *item) | ||
| 1178 | { | ||
| 1179 | FILE *fp; | ||
| 1180 | |||
| 1181 | fp = procmsg_open_cache_file(item, DATA_WRITE); | ||
| 1182 | if (fp) | ||
| 1183 | fclose(fp); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | void procmsg_clear_mark(FolderItem *item) | ||
| 1187 | { | ||
| 1188 | FILE *fp; | ||
| 1189 | |||
| 1190 | fp = procmsg_open_mark_file(item, DATA_WRITE); | ||
| 1191 | if (fp) | ||
| 1192 | fclose(fp); | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | /* return the reversed thread tree */ | ||
| 1196 | GNode *procmsg_get_thread_tree(GSList *mlist) | ||
| 1197 | { | ||
| 1198 | GNode *root, *parent, *node, *next; | ||
| 1199 | GHashTable *table; | ||
| 1200 | MsgInfo *msginfo; | ||
| 1201 | const gchar *msgid; | ||
| 1202 | GSList *reflist; | ||
| 1203 | |||
| 1204 | root = g_node_new(NULL); | ||
| 1205 | table = g_hash_table_new(g_str_hash, g_str_equal); | ||
| 1206 | |||
| 1207 | for (; mlist != NULL; mlist = mlist->next) { | ||
| 1208 | msginfo = (MsgInfo *)mlist->data; | ||
| 1209 | parent = root; | ||
| 1210 | |||
| 1211 | /* only look for the real parent first */ | ||
| 1212 | if (msginfo->inreplyto) { | ||
| 1213 | parent = g_hash_table_lookup(table, msginfo->inreplyto); | ||
| 1214 | if (parent == NULL) | ||
| 1215 | parent = root; | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | node = g_node_insert_data_before | ||
| 1219 | (parent, parent == root ? parent->children : NULL, | ||
| 1220 | msginfo); | ||
| 1221 | if ((msgid = msginfo->msgid) && | ||
| 1222 | g_hash_table_lookup(table, msgid) == NULL) | ||
| 1223 | g_hash_table_insert(table, (gchar *)msgid, node); | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | /* complete the unfinished threads */ | ||
| 1227 | for (node = root->children; node != NULL; ) { | ||
| 1228 | next = node->next; | ||
| 1229 | msginfo = (MsgInfo *)node->data; | ||
| 1230 | parent = NULL; | ||
| 1231 | |||
| 1232 | if (msginfo->inreplyto) | ||
| 1233 | parent = g_hash_table_lookup(table, msginfo->inreplyto); | ||
| 1234 | |||
| 1235 | /* try looking for the indirect parent */ | ||
| 1236 | if (!parent && msginfo->references) { | ||
| 1237 | for (reflist = msginfo->references; | ||
| 1238 | reflist != NULL; reflist = reflist->next) | ||
| 1239 | if ((parent = g_hash_table_lookup | ||
| 1240 | (table, reflist->data)) != NULL) | ||
| 1241 | break; | ||
| 1242 | } | ||
| 1243 | |||
| 1244 | /* node should not be the parent, and node should not | ||
| 1245 | be an ancestor of parent (circular reference) */ | ||
| 1246 | if (parent && parent != node && | ||
| 1247 | !g_node_is_ancestor(node, parent)) { | ||
| 1248 | g_node_unlink(node); | ||
| 1249 | g_node_insert_before | ||
| 1250 | (parent, parent->children, node); | ||
| 1251 | } | ||
| 1252 | node = next; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | g_hash_table_destroy(table); | ||
| 1256 | |||
| 1257 | return root; | ||
| 1258 | } | ||
| 1259 | |||
| 1260 | static gboolean procmsg_thread_date_func(GNode *node, gpointer data) | ||
| 1261 | { | ||
| 1262 | guint *tdate = (guint *)data; | ||
| 1263 | MsgInfo *msginfo = (MsgInfo *)node->data; | ||
| 1264 | |||
| 1265 | if (*tdate < msginfo->date_t) | ||
| 1266 | *tdate = msginfo->date_t; | ||
| 1267 | |||
| 1268 | return FALSE; | ||
| 1269 | } | ||
| 1270 | |||
| 1271 | guint procmsg_get_thread_date(GNode *node) | ||
| 1272 | { | ||
| 1273 | guint tdate = 0; | ||
| 1274 | |||
| 1275 | g_return_val_if_fail(node != NULL && node->parent != NULL && | ||
| 1276 | node->parent->parent == NULL, 0); | ||
| 1277 | |||
| 1278 | g_node_traverse(node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, | ||
| 1279 | procmsg_thread_date_func, &tdate); | ||
| 1280 | |||
| 1281 | return tdate; | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | gint procmsg_move_messages(GSList *mlist) | ||
| 1285 | { | ||
| 1286 | GSList *cur, *movelist = NULL; | ||
| 1287 | MsgInfo *msginfo; | ||
| 1288 | FolderItem *dest = NULL; | ||
| 1289 | GHashTable *hash; | ||
| 1290 | gint val = 0; | ||
| 1291 | |||
| 1292 | if (!mlist) return 0; | ||
| 1293 | |||
| 1294 | hash = procmsg_to_folder_hash_table_create(mlist); | ||
| 1295 | folder_item_scan_foreach(hash); | ||
| 1296 | g_hash_table_destroy(hash); | ||
| 1297 | |||
| 1298 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 1299 | msginfo = (MsgInfo *)cur->data; | ||
| 1300 | if (!dest) { | ||
| 1301 | dest = msginfo->to_folder; | ||
| 1302 | movelist = g_slist_append(movelist, msginfo); | ||
| 1303 | } else if (dest == msginfo->to_folder) { | ||
| 1304 | movelist = g_slist_append(movelist, msginfo); | ||
| 1305 | } else { | ||
| 1306 | val = folder_item_move_msgs(dest, movelist); | ||
| 1307 | g_slist_free(movelist); | ||
| 1308 | movelist = NULL; | ||
| 1309 | if (val == -1) | ||
| 1310 | return val; | ||
| 1311 | dest = msginfo->to_folder; | ||
| 1312 | movelist = g_slist_append(movelist, msginfo); | ||
| 1313 | } | ||
| 1314 | } | ||
| 1315 | |||
| 1316 | if (movelist) { | ||
| 1317 | val = folder_item_move_msgs(dest, movelist); | ||
| 1318 | g_slist_free(movelist); | ||
| 1319 | } | ||
| 1320 | |||
| 1321 | return val == -1 ? -1 : 0; | ||
| 1322 | } | ||
| 1323 | |||
| 1324 | gint procmsg_copy_messages(GSList *mlist) | ||
| 1325 | { | ||
| 1326 | GSList *cur, *copylist = NULL; | ||
| 1327 | MsgInfo *msginfo; | ||
| 1328 | FolderItem *dest = NULL; | ||
| 1329 | GHashTable *hash; | ||
| 1330 | gint val = 0; | ||
| 1331 | |||
| 1332 | if (!mlist) return 0; | ||
| 1333 | |||
| 1334 | hash = procmsg_to_folder_hash_table_create(mlist); | ||
| 1335 | folder_item_scan_foreach(hash); | ||
| 1336 | g_hash_table_destroy(hash); | ||
| 1337 | |||
| 1338 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 1339 | msginfo = (MsgInfo *)cur->data; | ||
| 1340 | if (!dest) { | ||
| 1341 | dest = msginfo->to_folder; | ||
| 1342 | copylist = g_slist_append(copylist, msginfo); | ||
| 1343 | } else if (dest == msginfo->to_folder) { | ||
| 1344 | copylist = g_slist_append(copylist, msginfo); | ||
| 1345 | } else { | ||
| 1346 | val = folder_item_copy_msgs(dest, copylist); | ||
| 1347 | g_slist_free(copylist); | ||
| 1348 | copylist = NULL; | ||
| 1349 | if (val == -1) | ||
| 1350 | return val; | ||
| 1351 | dest = msginfo->to_folder; | ||
| 1352 | copylist = g_slist_append(copylist, msginfo); | ||
| 1353 | } | ||
| 1354 | } | ||
| 1355 | |||
| 1356 | if (copylist) { | ||
| 1357 | val = folder_item_copy_msgs(dest, copylist); | ||
| 1358 | g_slist_free(copylist); | ||
| 1359 | } | ||
| 1360 | |||
| 1361 | return val == -1 ? -1 : 0; | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | gint procmsg_add_messages_from_queue(FolderItem *dest, GSList *mlist, | ||
| 1365 | gboolean is_move) | ||
| 1366 | { | ||
| 1367 | MsgInfo *msginfo; | ||
| 1368 | GSList *cur; | ||
| 1369 | gchar *file; | ||
| 1370 | FILE *fp; | ||
| 1371 | gchar buf[BUFFSIZE]; | ||
| 1372 | gchar *dest_file; | ||
| 1373 | gboolean is_error = FALSE; | ||
| 1374 | FolderItem *src; | ||
| 1375 | MsgFlags flags; | ||
| 1376 | |||
| 1377 | g_return_val_if_fail(dest != NULL, -1); | ||
| 1378 | g_return_val_if_fail(mlist != NULL, -1); | ||
| 1379 | |||
| 1380 | msginfo = (MsgInfo *)mlist->data; | ||
| 1381 | if (!msginfo || !msginfo->folder || msginfo->folder->stype != F_QUEUE || | ||
| 1382 | !MSG_IS_QUEUED(msginfo->flags) || dest->stype == F_QUEUE) | ||
| 1383 | return -1; | ||
| 1384 | |||
| 1385 | debug_print("procmsg_add_messages_from_queue: adding messages from queue folder\n"); | ||
| 1386 | |||
| 1387 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 1388 | msginfo = (MsgInfo *)cur->data; | ||
| 1389 | flags = msginfo->flags; | ||
| 1390 | if (!MSG_IS_QUEUED(flags)) | ||
| 1391 | return -1; | ||
| 1392 | MSG_UNSET_TMP_FLAGS(flags, MSG_QUEUED); | ||
| 1393 | src = msginfo->folder; | ||
| 1394 | file = procmsg_get_message_file(msginfo); | ||
| 1395 | if (!file) | ||
| 1396 | return -1; | ||
| 1397 | if ((fp = g_fopen(file, "rb")) == NULL) { | ||
| 1398 | FILE_OP_ERROR(file, "folder_item_move_msgs: fopen"); | ||
| 1399 | g_free(file); | ||
| 1400 | return -1; | ||
| 1401 | } | ||
| 1402 | while (fgets(buf, sizeof(buf), fp) != NULL) { | ||
| 1403 | if (buf[0] == '\r' || buf[0] == '\n') | ||
| 1404 | break; | ||
| 1405 | } | ||
| 1406 | if (ferror(fp)) { | ||
| 1407 | fclose(fp); | ||
| 1408 | g_free(file); | ||
| 1409 | return -1; | ||
| 1410 | } | ||
| 1411 | |||
| 1412 | dest_file = get_tmp_file(); | ||
| 1413 | debug_print("copy queued msg: %s -> %s\n", file, dest_file); | ||
| 1414 | |||
| 1415 | if (copy_file_part(fp, ftell(fp), G_MAXINT, dest_file) < 0) { | ||
| 1416 | fclose(fp); | ||
| 1417 | is_error = TRUE; | ||
| 1418 | } else { | ||
| 1419 | fclose(fp); | ||
| 1420 | if (folder_item_add_msg(dest, dest_file, &flags, TRUE) < 0) { | ||
| 1421 | g_unlink(dest_file); | ||
| 1422 | is_error = TRUE; | ||
| 1423 | } else if (is_move && | ||
| 1424 | folder_item_remove_msg(src, msginfo) < 0) | ||
| 1425 | is_error = TRUE; | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | g_free(dest_file); | ||
| 1429 | g_free(file); | ||
| 1430 | if (is_error) | ||
| 1431 | return -1; | ||
| 1432 | } | ||
| 1433 | |||
| 1434 | return 0; | ||
| 1435 | } | ||
| 1436 | |||
| 1437 | gchar *procmsg_get_message_file_path(MsgInfo *msginfo) | ||
| 1438 | { | ||
| 1439 | gchar *path, *file; | ||
| 1440 | |||
| 1441 | g_return_val_if_fail(msginfo != NULL, NULL); | ||
| 1442 | |||
| 1443 | if (msginfo->encinfo && msginfo->encinfo->plaintext_file) | ||
| 1444 | file = g_strdup(msginfo->encinfo->plaintext_file); | ||
| 1445 | else if (msginfo->file_path) | ||
| 1446 | return g_strdup(msginfo->file_path); | ||
| 1447 | else { | ||
| 1448 | gchar nstr[16]; | ||
| 1449 | path = folder_item_get_path(msginfo->folder); | ||
| 1450 | file = g_strconcat(path, G_DIR_SEPARATOR_S, | ||
| 1451 | utos_buf(nstr, msginfo->msgnum), NULL); | ||
| 1452 | g_free(path); | ||
| 1453 | } | ||
| 1454 | |||
| 1455 | return file; | ||
| 1456 | } | ||
| 1457 | |||
| 1458 | gchar *procmsg_get_message_file(MsgInfo *msginfo) | ||
| 1459 | { | ||
| 1460 | gchar *filename = NULL; | ||
| 1461 | |||
| 1462 | g_return_val_if_fail(msginfo != NULL, NULL); | ||
| 1463 | |||
| 1464 | if (msginfo->file_path) | ||
| 1465 | return g_strdup(msginfo->file_path); | ||
| 1466 | |||
| 1467 | filename = folder_item_fetch_msg(msginfo->folder, msginfo->msgnum); | ||
| 1468 | if (!filename) | ||
| 1469 | debug_print(_("can't fetch message %d\n"), msginfo->msgnum); | ||
| 1470 | |||
| 1471 | return filename; | ||
| 1472 | } | ||
| 1473 | |||
| 1474 | GSList *procmsg_get_message_file_list(GSList *mlist) | ||
| 1475 | { | ||
| 1476 | GSList *file_list = NULL; | ||
| 1477 | MsgInfo *msginfo; | ||
| 1478 | MsgFileInfo *fileinfo; | ||
| 1479 | gchar *file; | ||
| 1480 | |||
| 1481 | while (mlist != NULL) { | ||
| 1482 | msginfo = (MsgInfo *)mlist->data; | ||
| 1483 | file = procmsg_get_message_file(msginfo); | ||
| 1484 | if (!file) { | ||
| 1485 | procmsg_message_file_list_free(file_list); | ||
| 1486 | return NULL; | ||
| 1487 | } | ||
| 1488 | fileinfo = g_new(MsgFileInfo, 1); | ||
| 1489 | fileinfo->file = file; | ||
| 1490 | fileinfo->flags = g_new(MsgFlags, 1); | ||
| 1491 | *fileinfo->flags = msginfo->flags; | ||
| 1492 | file_list = g_slist_prepend(file_list, fileinfo); | ||
| 1493 | mlist = mlist->next; | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | file_list = g_slist_reverse(file_list); | ||
| 1497 | |||
| 1498 | return file_list; | ||
| 1499 | } | ||
| 1500 | |||
| 1501 | void procmsg_message_file_list_free(GSList *file_list) | ||
| 1502 | { | ||
| 1503 | GSList *cur; | ||
| 1504 | MsgFileInfo *fileinfo; | ||
| 1505 | |||
| 1506 | for (cur = file_list; cur != NULL; cur = cur->next) { | ||
| 1507 | fileinfo = (MsgFileInfo *)cur->data; | ||
| 1508 | g_free(fileinfo->file); | ||
| 1509 | g_free(fileinfo->flags); | ||
| 1510 | g_free(fileinfo); | ||
| 1511 | } | ||
| 1512 | |||
| 1513 | g_slist_free(file_list); | ||
| 1514 | } | ||
| 1515 | |||
| 1516 | FILE *procmsg_open_message(MsgInfo *msginfo) | ||
| 1517 | { | ||
| 1518 | FILE *fp; | ||
| 1519 | gchar *file; | ||
| 1520 | |||
| 1521 | g_return_val_if_fail(msginfo != NULL, NULL); | ||
| 1522 | |||
| 1523 | file = procmsg_get_message_file_path(msginfo); | ||
| 1524 | g_return_val_if_fail(file != NULL, NULL); | ||
| 1525 | |||
| 1526 | if (!is_file_exist(file)) { | ||
| 1527 | g_free(file); | ||
| 1528 | file = procmsg_get_message_file(msginfo); | ||
| 1529 | if (!file) | ||
| 1530 | return NULL; | ||
| 1531 | } | ||
| 1532 | |||
| 1533 | if ((fp = g_fopen(file, "rb")) == NULL) { | ||
| 1534 | FILE_OP_ERROR(file, "procmsg_open_message: fopen"); | ||
| 1535 | g_free(file); | ||
| 1536 | return NULL; | ||
| 1537 | } | ||
| 1538 | |||
| 1539 | g_free(file); | ||
| 1540 | |||
| 1541 | if (MSG_IS_QUEUED(msginfo->flags)) { | ||
| 1542 | gchar buf[BUFFSIZE]; | ||
| 1543 | |||
| 1544 | while (fgets(buf, sizeof(buf), fp) != NULL) | ||
| 1545 | if (buf[0] == '\r' || buf[0] == '\n') break; | ||
| 1546 | } | ||
| 1547 | |||
| 1548 | return fp; | ||
| 1549 | } | ||
| 1550 | |||
| 1551 | static DecryptMessageFunc decrypt_message_func = NULL; | ||
| 1552 | static gboolean auto_decrypt = TRUE; | ||
| 1553 | |||
| 1554 | void procmsg_set_decrypt_message_func(DecryptMessageFunc func) | ||
| 1555 | { | ||
| 1556 | decrypt_message_func = func; | ||
| 1557 | } | ||
| 1558 | |||
| 1559 | void procmsg_set_auto_decrypt_message(gboolean enabled) | ||
| 1560 | { | ||
| 1561 | auto_decrypt = enabled; | ||
| 1562 | } | ||
| 1563 | |||
| 1564 | FILE *procmsg_open_message_decrypted(MsgInfo *msginfo, MimeInfo **mimeinfo) | ||
| 1565 | { | ||
| 1566 | FILE *fp; | ||
| 1567 | |||
| 1568 | if (decrypt_message_func && auto_decrypt) | ||
| 1569 | return decrypt_message_func(msginfo, mimeinfo); | ||
| 1570 | |||
| 1571 | *mimeinfo = NULL; | ||
| 1572 | if ((fp = procmsg_open_message(msginfo)) == NULL) | ||
| 1573 | return NULL; | ||
| 1574 | *mimeinfo = procmime_scan_mime_header(fp); | ||
| 1575 | |||
| 1576 | return fp; | ||
| 1577 | } | ||
| 1578 | |||
| 1579 | gboolean procmsg_msg_exist(MsgInfo *msginfo) | ||
| 1580 | { | ||
| 1581 | gchar *path; | ||
| 1582 | gboolean ret; | ||
| 1583 | |||
| 1584 | if (!msginfo) return FALSE; | ||
| 1585 | |||
| 1586 | path = folder_item_get_path(msginfo->folder); | ||
| 1587 | change_dir(path); | ||
| 1588 | ret = !folder_item_is_msg_changed(msginfo->folder, msginfo); | ||
| 1589 | g_free(path); | ||
| 1590 | |||
| 1591 | return ret; | ||
| 1592 | } | ||
| 1593 | |||
| 1594 | gboolean procmsg_trash_messages_exist(void) | ||
| 1595 | { | ||
| 1596 | FolderItem *trash; | ||
| 1597 | GList *cur; | ||
| 1598 | |||
| 1599 | for (cur = folder_get_list(); cur != NULL; cur = cur->next) { | ||
| 1600 | trash = FOLDER(cur->data)->trash; | ||
| 1601 | if (trash && trash->total > 0) | ||
| 1602 | return TRUE; | ||
| 1603 | } | ||
| 1604 | |||
| 1605 | return FALSE; | ||
| 1606 | } | ||
| 1607 | |||
| 1608 | void procmsg_empty_trash(FolderItem *trash) | ||
| 1609 | { | ||
| 1610 | if (!trash) | ||
| 1611 | return; | ||
| 1612 | |||
| 1613 | g_return_if_fail(trash->stype == F_TRASH || trash->stype == F_JUNK); | ||
| 1614 | |||
| 1615 | if (trash->total > 0) { | ||
| 1616 | debug_print("Emptying messages in %s ...\n", trash->path); | ||
| 1617 | |||
| 1618 | folder_item_remove_all_msg(trash); | ||
| 1619 | procmsg_clear_cache(trash); | ||
| 1620 | procmsg_clear_mark(trash); | ||
| 1621 | trash->cache_dirty = FALSE; | ||
| 1622 | trash->mark_dirty = FALSE; | ||
| 1623 | } | ||
| 1624 | } | ||
| 1625 | |||
| 1626 | void procmsg_empty_all_trash(void) | ||
| 1627 | { | ||
| 1628 | FolderItem *trash; | ||
| 1629 | GList *cur; | ||
| 1630 | |||
| 1631 | for (cur = folder_get_list(); cur != NULL; cur = cur->next) { | ||
| 1632 | trash = FOLDER(cur->data)->trash; | ||
| 1633 | procmsg_empty_trash(trash); | ||
| 1634 | } | ||
| 1635 | } | ||
| 1636 | |||
| 1637 | static gboolean remove_all_cached_messages_func(GNode *node, gpointer data) | ||
| 1638 | { | ||
| 1639 | FolderItem *item; | ||
| 1640 | gchar *dir; | ||
| 1641 | |||
| 1642 | g_return_val_if_fail(node->data != NULL, FALSE); | ||
| 1643 | |||
| 1644 | item = FOLDER_ITEM(node->data); | ||
| 1645 | if (!item->path || item->stype == F_VIRTUAL) | ||
| 1646 | return FALSE; | ||
| 1647 | |||
| 1648 | dir = folder_item_get_path(item); | ||
| 1649 | if (is_dir_exist(dir)) { | ||
| 1650 | debug_print("removing all cached messages in '%s' ...\n", | ||
| 1651 | item->path); | ||
| 1652 | remove_all_numbered_files(dir); | ||
| 1653 | } | ||
| 1654 | g_free(dir); | ||
| 1655 | |||
| 1656 | return FALSE; | ||
| 1657 | } | ||
| 1658 | |||
| 1659 | void procmsg_remove_all_cached_messages(Folder *folder) | ||
| 1660 | { | ||
| 1661 | g_return_if_fail(folder != NULL); | ||
| 1662 | g_return_if_fail(FOLDER_IS_REMOTE(folder)); | ||
| 1663 | |||
| 1664 | debug_print("Removing all caches in the mailbox '%s' ...\n", | ||
| 1665 | folder->name); | ||
| 1666 | |||
| 1667 | g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, | ||
| 1668 | remove_all_cached_messages_func, NULL); | ||
| 1669 | } | ||
| 1670 | |||
| 1671 | gint procmsg_save_to_outbox(FolderItem *outbox, const gchar *file) | ||
| 1672 | { | ||
| 1673 | gint num; | ||
| 1674 | MsgFlags flag = {0, 0}; | ||
| 1675 | |||
| 1676 | debug_print("saving sent message...\n"); | ||
| 1677 | |||
| 1678 | if (!outbox) | ||
| 1679 | outbox = folder_get_default_outbox(); | ||
| 1680 | g_return_val_if_fail(outbox != NULL, -1); | ||
| 1681 | |||
| 1682 | folder_item_scan(outbox); | ||
| 1683 | if ((num = folder_item_add_msg(outbox, file, &flag, FALSE)) < 0) { | ||
| 1684 | g_warning("can't save message\n"); | ||
| 1685 | return -1; | ||
| 1686 | } | ||
| 1687 | procmsg_flush_folder(outbox); | ||
| 1688 | |||
| 1689 | return 0; | ||
| 1690 | } | ||
| 1691 | |||
| 1692 | static guint print_id = 0; | ||
| 1693 | |||
| 1694 | static gint print_command_exec(const gchar *file, const gchar *cmdline) | ||
| 1695 | { | ||
| 1696 | static const gchar *def_cmd = "lpr %s"; | ||
| 1697 | gchar buf[1024]; | ||
| 1698 | |||
| 1699 | #ifdef G_OS_WIN32 | ||
| 1700 | if (canonicalize_file_replace(file) < 0) | ||
| 1701 | return -1; | ||
| 1702 | #endif | ||
| 1703 | |||
| 1704 | if (cmdline && str_find_format_times(cmdline, 's') == 1) | ||
| 1705 | g_snprintf(buf, sizeof(buf) - 1, cmdline, file); | ||
| 1706 | else { | ||
| 1707 | if (cmdline) { | ||
| 1708 | g_warning(_("Print command line is invalid: `%s'\n"), | ||
| 1709 | cmdline); | ||
| 1710 | return -1; | ||
| 1711 | } | ||
| 1712 | |||
| 1713 | #ifdef G_OS_WIN32 | ||
| 1714 | execute_print_file(file); | ||
| 1715 | return 0; | ||
| 1716 | #else | ||
| 1717 | g_snprintf(buf, sizeof(buf) - 1, def_cmd, file); | ||
| 1718 | #endif | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | g_strchomp(buf); | ||
| 1722 | if (buf[strlen(buf) - 1] != '&') | ||
| 1723 | strcat(buf, "&"); | ||
| 1724 | |||
| 1725 | if (system(buf) != 0) | ||
| 1726 | return -1; | ||
| 1727 | |||
| 1728 | return 0; | ||
| 1729 | } | ||
| 1730 | |||
| 1731 | static void procmsg_write_headers(MsgInfo *msginfo, MimeInfo *partinfo, | ||
| 1732 | FILE *fp, FILE *dest_fp, | ||
| 1733 | const gchar *encoding, gboolean all_headers) | ||
| 1734 | { | ||
| 1735 | GPtrArray *headers; | ||
| 1736 | gint i; | ||
| 1737 | |||
| 1738 | if (all_headers) | ||
| 1739 | headers = procheader_get_header_array_asis(fp, NULL); | ||
| 1740 | else | ||
| 1741 | headers = procheader_get_header_array_for_display(fp, NULL); | ||
| 1742 | |||
| 1743 | for (i = 0; i < headers->len; i++) { | ||
| 1744 | Header *hdr; | ||
| 1745 | gchar *file_str; | ||
| 1746 | const gchar *body; | ||
| 1747 | |||
| 1748 | hdr = g_ptr_array_index(headers, i); | ||
| 1749 | |||
| 1750 | if (partinfo) { | ||
| 1751 | if (!g_ascii_strcasecmp(hdr->name, "Subject") || | ||
| 1752 | !g_ascii_strcasecmp(hdr->name, "From") || | ||
| 1753 | !g_ascii_strcasecmp(hdr->name, "To") || | ||
| 1754 | !g_ascii_strcasecmp(hdr->name, "Cc")) { | ||
| 1755 | unfold_line(hdr->body); | ||
| 1756 | } | ||
| 1757 | |||
| 1758 | body = hdr->body; | ||
| 1759 | while (g_ascii_isspace(*body)) | ||
| 1760 | body++; | ||
| 1761 | } else { | ||
| 1762 | if (!g_ascii_strcasecmp(hdr->name, "Subject")) | ||
| 1763 | body = msginfo->subject; | ||
| 1764 | else if (!g_ascii_strcasecmp(hdr->name, "From")) | ||
| 1765 | body = msginfo->from; | ||
| 1766 | else if (!g_ascii_strcasecmp(hdr->name, "To")) | ||
| 1767 | body = msginfo->to; | ||
| 1768 | else if (!g_ascii_strcasecmp(hdr->name, "Cc")) { | ||
| 1769 | unfold_line(hdr->body); | ||
| 1770 | body = hdr->body; | ||
| 1771 | while (g_ascii_isspace(*body)) | ||
| 1772 | body++; | ||
| 1773 | } else { | ||
| 1774 | body = hdr->body; | ||
| 1775 | while (g_ascii_isspace(*body)) | ||
| 1776 | body++; | ||
| 1777 | } | ||
| 1778 | } | ||
| 1779 | |||
| 1780 | if (body && *body != '\0') { | ||
| 1781 | file_str = conv_codeset_strdup | ||
| 1782 | (body, CS_INTERNAL, encoding); | ||
| 1783 | fprintf(dest_fp, "%s: %s\n", hdr->name, | ||
| 1784 | file_str ? file_str : body); | ||
| 1785 | g_free(file_str); | ||
| 1786 | } else { | ||
| 1787 | fprintf(dest_fp, "%s: (none)\n", hdr->name); | ||
| 1788 | } | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | procheader_header_array_destroy(headers); | ||
| 1792 | } | ||
| 1793 | |||
| 1794 | void procmsg_print_message(MsgInfo *msginfo, const gchar *cmdline, | ||
| 1795 | gboolean all_headers) | ||
| 1796 | { | ||
| 1797 | gchar *prtmp; | ||
| 1798 | |||
| 1799 | g_return_if_fail(msginfo != NULL); | ||
| 1800 | |||
| 1801 | prtmp = g_strdup_printf("%s%cprinttmp-%08x.txt", | ||
| 1802 | get_mime_tmp_dir(), G_DIR_SEPARATOR, | ||
| 1803 | print_id++); | ||
| 1804 | |||
| 1805 | if (procmsg_save_message_as_text(msginfo, prtmp, | ||
| 1806 | conv_get_locale_charset_str(), | ||
| 1807 | all_headers) == 0) | ||
| 1808 | print_command_exec(prtmp, cmdline); | ||
| 1809 | |||
| 1810 | g_free(prtmp); | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | void procmsg_print_message_part(MsgInfo *msginfo, MimeInfo *partinfo, | ||
| 1814 | const gchar *cmdline, gboolean all_headers) | ||
| 1815 | { | ||
| 1816 | FILE *msgfp, *tmpfp, *prfp; | ||
| 1817 | gchar *prtmp; | ||
| 1818 | gchar buf[BUFFSIZE]; | ||
| 1819 | |||
| 1820 | if ((msgfp = procmsg_open_message(msginfo)) == NULL) { | ||
| 1821 | return; | ||
| 1822 | } | ||
| 1823 | |||
| 1824 | if ((tmpfp = procmime_get_text_content | ||
| 1825 | (partinfo, msgfp, conv_get_locale_charset_str())) == NULL) { | ||
| 1826 | fclose(msgfp); | ||
| 1827 | return; | ||
| 1828 | } | ||
| 1829 | fclose(msgfp); | ||
| 1830 | |||
| 1831 | prtmp = g_strdup_printf("%s%cprinttmp-%08x.txt", | ||
| 1832 | get_mime_tmp_dir(), G_DIR_SEPARATOR, | ||
| 1833 | print_id++); | ||
| 1834 | if ((prfp = g_fopen(prtmp, "w")) == NULL) { | ||
| 1835 | FILE_OP_ERROR(prtmp, "procmsg_print_message_part: fopen"); | ||
| 1836 | g_free(prtmp); | ||
| 1837 | fclose(tmpfp); | ||
| 1838 | return; | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | while (fgets(buf, sizeof(buf), tmpfp) != NULL) | ||
| 1842 | fputs(buf, prfp); | ||
| 1843 | |||
| 1844 | fclose(prfp); | ||
| 1845 | fclose(tmpfp); | ||
| 1846 | |||
| 1847 | print_command_exec(prtmp, cmdline); | ||
| 1848 | |||
| 1849 | g_free(prtmp); | ||
| 1850 | } | ||
| 1851 | |||
| 1852 | gint procmsg_save_message_as_text(MsgInfo *msginfo, const gchar *dest, | ||
| 1853 | const gchar *encoding, gboolean all_headers) | ||
| 1854 | { | ||
| 1855 | MimeInfo *mimeinfo, *partinfo; | ||
| 1856 | FILE *fp; | ||
| 1857 | FILE *tmpfp; | ||
| 1858 | FILE *destfp; | ||
| 1859 | gchar buf[BUFFSIZE]; | ||
| 1860 | gchar *part_str; | ||
| 1861 | gint ret = 0; | ||
| 1862 | |||
| 1863 | g_return_val_if_fail(msginfo != NULL, -1); | ||
| 1864 | g_return_val_if_fail(dest != NULL, -1); | ||
| 1865 | |||
| 1866 | mimeinfo = procmime_scan_message(msginfo); | ||
| 1867 | if (!mimeinfo) | ||
| 1868 | return -1; | ||
| 1869 | if ((fp = procmsg_open_message(msginfo)) == NULL) { | ||
| 1870 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 1871 | return -1; | ||
| 1872 | } | ||
| 1873 | if ((destfp = g_fopen(dest, "w")) == NULL) { | ||
| 1874 | fclose(fp); | ||
| 1875 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 1876 | return -1; | ||
| 1877 | } | ||
| 1878 | procmsg_write_headers(msginfo, mimeinfo, fp, destfp, encoding, all_headers); | ||
| 1879 | fputc('\n', destfp); | ||
| 1880 | |||
| 1881 | partinfo = mimeinfo; | ||
| 1882 | |||
| 1883 | while (partinfo != NULL) { | ||
| 1884 | if (fseek(fp, partinfo->fpos, SEEK_SET) < 0) | ||
| 1885 | break; | ||
| 1886 | |||
| 1887 | if (partinfo->filename || partinfo->name) | ||
| 1888 | g_snprintf(buf, sizeof(buf), "\n[%s %s (%s)]\n", | ||
| 1889 | partinfo->filename ? partinfo->filename : | ||
| 1890 | partinfo->name, | ||
| 1891 | partinfo->content_type, | ||
| 1892 | to_human_readable(partinfo->content_size)); | ||
| 1893 | else | ||
| 1894 | g_snprintf(buf, sizeof(buf), "\n[%s (%s)]\n", | ||
| 1895 | partinfo->content_type, | ||
| 1896 | to_human_readable(partinfo->content_size)); | ||
| 1897 | part_str = conv_codeset_strdup(buf, CS_INTERNAL, encoding); | ||
| 1898 | if (!part_str) | ||
| 1899 | part_str = g_strdup(buf); | ||
| 1900 | |||
| 1901 | if (partinfo->mime_type == MIME_TEXT || | ||
| 1902 | partinfo->mime_type == MIME_TEXT_HTML) { | ||
| 1903 | if (!partinfo->main && | ||
| 1904 | partinfo->parent && | ||
| 1905 | partinfo->parent->children != partinfo) { | ||
| 1906 | fputs(part_str, destfp); | ||
| 1907 | } | ||
| 1908 | |||
| 1909 | if ((tmpfp = procmime_get_text_content(partinfo, fp, encoding)) == NULL) { | ||
| 1910 | g_free(part_str); | ||
| 1911 | break; | ||
| 1912 | } | ||
| 1913 | while (fgets(buf, sizeof(buf), tmpfp) != NULL) | ||
| 1914 | fputs(buf, destfp); | ||
| 1915 | |||
| 1916 | fclose(tmpfp); | ||
| 1917 | } else if (partinfo->mime_type == MIME_MESSAGE_RFC822) { | ||
| 1918 | fputs(part_str, destfp); | ||
| 1919 | while (fgets(buf, sizeof(buf), fp) != NULL) | ||
| 1920 | if (buf[0] == '\r' || buf[0] == '\n') break; | ||
| 1921 | procmsg_write_headers(msginfo, partinfo, fp, destfp, encoding, all_headers); | ||
| 1922 | fputc('\n', destfp); | ||
| 1923 | } else if (partinfo->mime_type != MIME_MULTIPART) { | ||
| 1924 | fputs(part_str, destfp); | ||
| 1925 | } | ||
| 1926 | |||
| 1927 | g_free(part_str); | ||
| 1928 | |||
| 1929 | if (partinfo->parent && partinfo->parent->content_type && | ||
| 1930 | !g_ascii_strcasecmp(partinfo->parent->content_type, | ||
| 1931 | "multipart/alternative")) | ||
| 1932 | partinfo = partinfo->parent->next; | ||
| 1933 | else | ||
| 1934 | partinfo = procmime_mimeinfo_next(partinfo); | ||
| 1935 | } | ||
| 1936 | |||
| 1937 | if (fclose(destfp) == EOF) { | ||
| 1938 | FILE_OP_ERROR(dest, "fclose"); | ||
| 1939 | g_unlink(dest); | ||
| 1940 | ret = -1; | ||
| 1941 | } | ||
| 1942 | |||
| 1943 | fclose(fp); | ||
| 1944 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 1945 | |||
| 1946 | return ret; | ||
| 1947 | } | ||
| 1948 | |||
| 1949 | /** | ||
| 1950 | * procmsg_concat_partial_messages: | ||
| 1951 | * @mlist: list of MsgInfo* including message/partial messages. | ||
| 1952 | * @file: output file name of concatenated message. | ||
| 1953 | * | ||
| 1954 | * Concatenate @mlist which consists of message/partial messages and | ||
| 1955 | * output to @file. If @mlist has different partial id, the first one | ||
| 1956 | * is used. | ||
| 1957 | * | ||
| 1958 | * Return value: 0 on success, or -1 if failed. | ||
| 1959 | **/ | ||
| 1960 | gint procmsg_concat_partial_messages(GSList *mlist, const gchar *file) | ||
| 1961 | { | ||
| 1962 | static HeaderEntry hentry[] = {{"Content-Type:", NULL, FALSE}, | ||
| 1963 | {NULL, NULL, FALSE}}; | ||
| 1964 | FILE *fp; | ||
| 1965 | gchar buf[BUFFSIZE]; | ||
| 1966 | FILE *tmp_fp; | ||
| 1967 | gchar *part_id = NULL; | ||
| 1968 | gint total = 0; | ||
| 1969 | MsgInfo *msg_array[100] = {NULL}; | ||
| 1970 | MsgInfo *msginfo; | ||
| 1971 | MimeInfo *mimeinfo; | ||
| 1972 | GSList *cur; | ||
| 1973 | gint i; | ||
| 1974 | |||
| 1975 | g_return_val_if_fail(mlist != NULL, -1); | ||
| 1976 | g_return_val_if_fail(file != NULL, -1); | ||
| 1977 | |||
| 1978 | debug_print("procmsg_concat_partial_messages\n"); | ||
| 1979 | |||
| 1980 | for (cur = mlist; cur != NULL; cur = cur->next) { | ||
| 1981 | gint n = 0; | ||
| 1982 | gint t = 0; | ||
| 1983 | gchar *cur_id = NULL; | ||
| 1984 | |||
| 1985 | msginfo = (MsgInfo *)cur->data; | ||
| 1986 | |||
| 1987 | fp = procmsg_open_message_decrypted(msginfo, &mimeinfo); | ||
| 1988 | if (!fp) | ||
| 1989 | continue; | ||
| 1990 | if (!mimeinfo->content_type || | ||
| 1991 | g_ascii_strcasecmp(mimeinfo->content_type, "message/partial") != 0) | ||
| 1992 | goto skip; | ||
| 1993 | |||
| 1994 | rewind(fp); | ||
| 1995 | if (procheader_get_one_field(buf, sizeof(buf), fp, hentry) == -1) | ||
| 1996 | goto skip; | ||
| 1997 | |||
| 1998 | procmime_scan_content_type_partial(buf + strlen(hentry[0].name), | ||
| 1999 | &t, &cur_id, &n); | ||
| 2000 | if (n == 0 || n > 100 || t > 100 || (t > 0 && n > t)) { | ||
| 2001 | debug_print("bad partial number (%d/%d), skip\n", n, t); | ||
| 2002 | g_free(cur_id); | ||
| 2003 | goto skip; | ||
| 2004 | } | ||
| 2005 | |||
| 2006 | debug_print("partial: %d/%d id=%s\n", n, t, cur_id); | ||
| 2007 | if (!part_id) | ||
| 2008 | part_id = g_strdup(cur_id); | ||
| 2009 | if (total == 0) | ||
| 2010 | total = t; | ||
| 2011 | |||
| 2012 | if ((t > 0 && total != t) || (total > 0 && n > total) || | ||
| 2013 | strcmp(part_id, cur_id) != 0) { | ||
| 2014 | debug_print("skip\n"); | ||
| 2015 | g_free(cur_id); | ||
| 2016 | goto skip; | ||
| 2017 | } | ||
| 2018 | |||
| 2019 | msg_array[n - 1] = msginfo; | ||
| 2020 | |||
| 2021 | g_free(cur_id); | ||
| 2022 | skip: | ||
| 2023 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 2024 | fclose(fp); | ||
| 2025 | } | ||
| 2026 | |||
| 2027 | if (!part_id) { | ||
| 2028 | debug_print("piece not found\n"); | ||
| 2029 | return -1; | ||
| 2030 | } | ||
| 2031 | |||
| 2032 | debug_print("part_id = %s , total = %d\n", part_id, total); | ||
| 2033 | g_free(part_id); | ||
| 2034 | |||
| 2035 | if (total == 0) { | ||
| 2036 | debug_print("total number not found\n"); | ||
| 2037 | return -1; | ||
| 2038 | } | ||
| 2039 | |||
| 2040 | /* check if all pieces exist */ | ||
| 2041 | for (i = 0; i < total; i++) { | ||
| 2042 | if (msg_array[i] == NULL) { | ||
| 2043 | debug_print("message part %d not exist\n", i + 1); | ||
| 2044 | return -1; | ||
| 2045 | } | ||
| 2046 | } | ||
| 2047 | |||
| 2048 | /* concatenate parts */ | ||
| 2049 | if ((tmp_fp = g_fopen(file, "wb")) == NULL) { | ||
| 2050 | FILE_OP_ERROR(file, "fopen"); | ||
| 2051 | return -1; | ||
| 2052 | } | ||
| 2053 | |||
| 2054 | for (i = 0; i < total; i++) { | ||
| 2055 | msginfo = msg_array[i]; | ||
| 2056 | off_t out_size; | ||
| 2057 | gint empty_line_size = 0; | ||
| 2058 | |||
| 2059 | fp = procmsg_open_message_decrypted(msginfo, &mimeinfo); | ||
| 2060 | if (!fp) { | ||
| 2061 | g_warning("cannot open message part %d\n", i + 1); | ||
| 2062 | fclose(tmp_fp); | ||
| 2063 | g_unlink(file); | ||
| 2064 | return -1; | ||
| 2065 | } | ||
| 2066 | |||
| 2067 | /* write out first headers */ | ||
| 2068 | if (i == 0) { | ||
| 2069 | rewind(fp); | ||
| 2070 | while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) { | ||
| 2071 | if (!g_ascii_strncasecmp(buf, "Content-", 8) || | ||
| 2072 | !g_ascii_strncasecmp(buf, "Subject", 7) || | ||
| 2073 | !g_ascii_strncasecmp(buf, "Message-ID", 10) || | ||
| 2074 | !g_ascii_strncasecmp(buf, "Encrypted", 9) || | ||
| 2075 | !g_ascii_strncasecmp(buf, "MIME-Version", 12)) | ||
| 2076 | continue; | ||
| 2077 | fputs(buf, tmp_fp); | ||
| 2078 | fputs("\n", tmp_fp); | ||
| 2079 | } | ||
| 2080 | |||
| 2081 | while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) { | ||
| 2082 | if (!g_ascii_strncasecmp(buf, "Content-", 8) || | ||
| 2083 | !g_ascii_strncasecmp(buf, "Subject", 7) || | ||
| 2084 | !g_ascii_strncasecmp(buf, "Message-ID", 10) || | ||
| 2085 | !g_ascii_strncasecmp(buf, "Encrypted", 9) || | ||
| 2086 | !g_ascii_strncasecmp(buf, "MIME-Version", 12)) { | ||
| 2087 | fputs(buf, tmp_fp); | ||
| 2088 | fputs("\n", tmp_fp); | ||
| 2089 | } | ||
| 2090 | } | ||
| 2091 | |||
| 2092 | /* header-body separator */ | ||
| 2093 | fputs("\n", tmp_fp); | ||
| 2094 | } | ||
| 2095 | |||
| 2096 | out_size = get_left_file_size(fp); | ||
| 2097 | if (out_size < 0) { | ||
| 2098 | g_warning("cannot tell left file size of part %d\n", i + 1); | ||
| 2099 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 2100 | fclose(fp); | ||
| 2101 | fclose(tmp_fp); | ||
| 2102 | g_unlink(file); | ||
| 2103 | return -1; | ||
| 2104 | } | ||
| 2105 | empty_line_size = get_last_empty_line_size(fp, out_size); | ||
| 2106 | if (empty_line_size < 0) { | ||
| 2107 | g_warning("cannot get last empty line size of part %d\n", i + 1); | ||
| 2108 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 2109 | fclose(fp); | ||
| 2110 | fclose(tmp_fp); | ||
| 2111 | g_unlink(file); | ||
| 2112 | return -1; | ||
| 2113 | } | ||
| 2114 | |||
| 2115 | if (append_file_part(fp, ftell(fp), out_size - empty_line_size, | ||
| 2116 | tmp_fp) < 0) { | ||
| 2117 | g_warning("write failed\n"); | ||
| 2118 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 2119 | fclose(fp); | ||
| 2120 | fclose(tmp_fp); | ||
| 2121 | g_unlink(file); | ||
| 2122 | return -1; | ||
| 2123 | } | ||
| 2124 | |||
| 2125 | procmime_mimeinfo_free_all(mimeinfo); | ||
| 2126 | fclose(fp); | ||
| 2127 | } | ||
| 2128 | |||
| 2129 | fclose(tmp_fp); | ||
| 2130 | |||
| 2131 | return 0; | ||
| 2132 | } | ||
| 2133 | |||
| 2134 | static gboolean procmsg_get_flags(FolderItem *item, gint num, | ||
| 2135 | MsgPermFlags *flags) | ||
| 2136 | { | ||
| 2137 | FILE *fp; | ||
| 2138 | guint32 idata; | ||
| 2139 | gint read_num; | ||
| 2140 | MsgPermFlags perm_flags; | ||
| 2141 | gboolean found = FALSE; | ||
| 2142 | GSList *cur; | ||
| 2143 | |||
| 2144 | if ((fp = procmsg_open_mark_file(item, DATA_READ)) == NULL) | ||
| 2145 | return FALSE; | ||
| 2146 | |||
| 2147 | while (fread(&idata, sizeof(idata), 1, fp) == 1) { | ||
| 2148 | read_num = idata; | ||
| 2149 | if (fread(&idata, sizeof(idata), 1, fp) != 1) | ||
| 2150 | break; | ||
| 2151 | perm_flags = idata; | ||
| 2152 | if (read_num == num) { | ||
| 2153 | *flags = perm_flags; | ||
| 2154 | found = TRUE; | ||
| 2155 | break; | ||
| 2156 | } | ||
| 2157 | } | ||
| 2158 | |||
| 2159 | fclose(fp); | ||
| 2160 | if (found) | ||
| 2161 | return TRUE; | ||
| 2162 | |||
| 2163 | for (cur = item->mark_queue; cur != NULL; cur = cur->next) { | ||
| 2164 | MsgFlagInfo *flaginfo = (MsgFlagInfo *)cur->data; | ||
| 2165 | |||
| 2166 | if (flaginfo->msgnum == num) { | ||
| 2167 | *flags = flaginfo->flags.perm_flags; | ||
| 2168 | found = TRUE; | ||
| 2169 | break; | ||
| 2170 | } | ||
| 2171 | } | ||
| 2172 | |||
| 2173 | return found; | ||
| 2174 | } | ||
| 2175 | |||
| 2176 | MsgInfo *procmsg_get_msginfo(FolderItem *item, gint num) | ||
| 2177 | { | ||
| 2178 | MsgInfo *msginfo; | ||
| 2179 | FolderType type; | ||
| 2180 | |||
| 2181 | g_return_val_if_fail(item->folder != NULL, NULL); | ||
| 2182 | |||
| 2183 | msginfo = folder_item_get_msginfo(item, num); | ||
| 2184 | if (!msginfo) | ||
| 2185 | return NULL; | ||
| 2186 | |||
| 2187 | type = FOLDER_TYPE(item->folder); | ||
| 2188 | if (type == F_MH || type == F_IMAP) { | ||
| 2189 | if (item->stype == F_QUEUE) { | ||
| 2190 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_QUEUED); | ||
| 2191 | } else if (item->stype == F_DRAFT) { | ||
| 2192 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_DRAFT); | ||
| 2193 | } | ||
| 2194 | } | ||
| 2195 | if (type == F_IMAP) { | ||
| 2196 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_IMAP); | ||
| 2197 | } else if (type == F_NEWS) { | ||
| 2198 | MSG_SET_TMP_FLAGS(msginfo->flags, MSG_NEWS); | ||
| 2199 | } | ||
| 2200 | |||
| 2201 | if (type == F_MH || type == F_NEWS) { | ||
| 2202 | MsgPermFlags flags = 0; | ||
| 2203 | if (procmsg_get_flags(item, num, &flags)) | ||
| 2204 | msginfo->flags.perm_flags = flags; | ||
| 2205 | } | ||
| 2206 | |||
| 2207 | return msginfo; | ||
| 2208 | } | ||
| 2209 | |||
| 2210 | MsgInfo *procmsg_msginfo_copy(MsgInfo *msginfo) | ||
| 2211 | { | ||
| 2212 | MsgInfo *newmsginfo; | ||
| 2213 | |||
| 2214 | if (msginfo == NULL) return NULL; | ||
| 2215 | |||
| 2216 | newmsginfo = g_new0(MsgInfo, 1); | ||
| 2217 | |||
| 2218 | #define MEMBCOPY(mmb) newmsginfo->mmb = msginfo->mmb | ||
| 2219 | #define MEMBDUP(mmb) newmsginfo->mmb = msginfo->mmb ? \ | ||
| 2220 | g_strdup(msginfo->mmb) : NULL | ||
| 2221 | |||
| 2222 | MEMBCOPY(msgnum); | ||
| 2223 | MEMBCOPY(size); | ||
| 2224 | MEMBCOPY(mtime); | ||
| 2225 | MEMBCOPY(date_t); | ||
| 2226 | |||
| 2227 | MEMBCOPY(flags); | ||
| 2228 | |||
| 2229 | MEMBDUP(fromname); | ||
| 2230 | |||
| 2231 | MEMBDUP(date); | ||
| 2232 | MEMBDUP(from); | ||
| 2233 | MEMBDUP(to); | ||
| 2234 | MEMBDUP(cc); | ||
| 2235 | MEMBDUP(newsgroups); | ||
| 2236 | MEMBDUP(subject); | ||
| 2237 | MEMBDUP(msgid); | ||
| 2238 | MEMBDUP(inreplyto); | ||
| 2239 | |||
| 2240 | MEMBCOPY(folder); | ||
| 2241 | MEMBCOPY(to_folder); | ||
| 2242 | |||
| 2243 | MEMBDUP(xface); | ||
| 2244 | |||
| 2245 | MEMBDUP(file_path); | ||
| 2246 | |||
| 2247 | if (msginfo->encinfo) { | ||
| 2248 | newmsginfo->encinfo = g_new0(MsgEncryptInfo, 1); | ||
| 2249 | MEMBDUP(encinfo->plaintext_file); | ||
| 2250 | MEMBDUP(encinfo->sigstatus); | ||
| 2251 | MEMBDUP(encinfo->sigstatus_full); | ||
| 2252 | MEMBCOPY(encinfo->decryption_failed); | ||
| 2253 | } | ||
| 2254 | |||
| 2255 | return newmsginfo; | ||
| 2256 | } | ||
| 2257 | |||
| 2258 | MsgInfo *procmsg_msginfo_get_full_info(MsgInfo *msginfo) | ||
| 2259 | { | ||
| 2260 | MsgInfo *full_msginfo; | ||
| 2261 | gchar *file; | ||
| 2262 | |||
| 2263 | if (msginfo == NULL) return NULL; | ||
| 2264 | |||
| 2265 | file = procmsg_get_message_file(msginfo); | ||
| 2266 | if (!file) { | ||
| 2267 | g_warning("procmsg_msginfo_get_full_info(): can't get message file.\n"); | ||
| 2268 | return NULL; | ||
| 2269 | } | ||
| 2270 | |||
| 2271 | full_msginfo = procheader_parse_file(file, msginfo->flags, TRUE); | ||
| 2272 | g_free(file); | ||
| 2273 | if (!full_msginfo) return NULL; | ||
| 2274 | |||
| 2275 | full_msginfo->msgnum = msginfo->msgnum; | ||
| 2276 | full_msginfo->size = msginfo->size; | ||
| 2277 | full_msginfo->mtime = msginfo->mtime; | ||
| 2278 | full_msginfo->folder = msginfo->folder; | ||
| 2279 | full_msginfo->to_folder = msginfo->to_folder; | ||
| 2280 | |||
| 2281 | full_msginfo->file_path = g_strdup(msginfo->file_path); | ||
| 2282 | |||
| 2283 | if (msginfo->encinfo) { | ||
| 2284 | full_msginfo->encinfo = g_new0(MsgEncryptInfo, 1); | ||
| 2285 | full_msginfo->encinfo->plaintext_file = | ||
| 2286 | g_strdup(msginfo->encinfo->plaintext_file); | ||
| 2287 | full_msginfo->encinfo->sigstatus = | ||
| 2288 | g_strdup(msginfo->encinfo->sigstatus); | ||
| 2289 | full_msginfo->encinfo->sigstatus_full = | ||
| 2290 | g_strdup(msginfo->encinfo->sigstatus_full); | ||
| 2291 | full_msginfo->encinfo->decryption_failed = | ||
| 2292 | msginfo->encinfo->decryption_failed; | ||
| 2293 | } | ||
| 2294 | |||
| 2295 | return full_msginfo; | ||
| 2296 | } | ||
| 2297 | |||
| 2298 | gboolean procmsg_msginfo_equal(MsgInfo *msginfo_a, MsgInfo *msginfo_b) | ||
| 2299 | { | ||
| 2300 | if (!msginfo_a || !msginfo_b) | ||
| 2301 | return FALSE; | ||
| 2302 | |||
| 2303 | if (msginfo_a == msginfo_b) | ||
| 2304 | return TRUE; | ||
| 2305 | |||
| 2306 | if (msginfo_a->folder == msginfo_b->folder && | ||
| 2307 | msginfo_a->msgnum == msginfo_b->msgnum && | ||
| 2308 | msginfo_a->size == msginfo_b->size && | ||
| 2309 | msginfo_a->mtime == msginfo_b->mtime) | ||
| 2310 | return TRUE; | ||
| 2311 | |||
| 2312 | return FALSE; | ||
| 2313 | } | ||
| 2314 | |||
| 2315 | void procmsg_msginfo_free(MsgInfo *msginfo) | ||
| 2316 | { | ||
| 2317 | if (msginfo == NULL) return; | ||
| 2318 | |||
| 2319 | g_free(msginfo->xface); | ||
| 2320 | |||
| 2321 | g_free(msginfo->fromname); | ||
| 2322 | |||
| 2323 | g_free(msginfo->date); | ||
| 2324 | g_free(msginfo->from); | ||
| 2325 | g_free(msginfo->to); | ||
| 2326 | g_free(msginfo->cc); | ||
| 2327 | g_free(msginfo->newsgroups); | ||
| 2328 | g_free(msginfo->subject); | ||
| 2329 | g_free(msginfo->msgid); | ||
| 2330 | g_free(msginfo->inreplyto); | ||
| 2331 | |||
| 2332 | slist_free_strings(msginfo->references); | ||
| 2333 | g_slist_free(msginfo->references); | ||
| 2334 | |||
| 2335 | g_free(msginfo->file_path); | ||
| 2336 | |||
| 2337 | if (msginfo->encinfo) { | ||
| 2338 | g_free(msginfo->encinfo->plaintext_file); | ||
| 2339 | g_free(msginfo->encinfo->sigstatus); | ||
| 2340 | g_free(msginfo->encinfo->sigstatus_full); | ||
| 2341 | g_free(msginfo->encinfo); | ||
| 2342 | } | ||
| 2343 | |||
| 2344 | g_free(msginfo); | ||
| 2345 | } | ||
| 2346 | |||
| 2347 | gint procmsg_cmp_msgnum_for_sort(gconstpointer a, gconstpointer b) | ||
| 2348 | { | ||
| 2349 | const MsgInfo *msginfo1 = a; | ||
| 2350 | const MsgInfo *msginfo2 = b; | ||
| 2351 | |||
| 2352 | if (!msginfo1 || !msginfo2) | ||
| 2353 | return 0; | ||
| 2354 | |||
| 2355 | return msginfo1->msgnum - msginfo2->msgnum; | ||
| 2356 | } | ||
| 2357 | |||
| 2358 | #define CMP_FUNC_DEF(func_name, val) \ | ||
| 2359 | static gint func_name(gconstpointer a, gconstpointer b) \ | ||
| 2360 | { \ | ||
| 2361 | const MsgInfo *msginfo1 = a; \ | ||
| 2362 | const MsgInfo *msginfo2 = b; \ | ||
| 2363 | gint ret; \ | ||
| 2364 | \ | ||
| 2365 | if (!msginfo1 || !msginfo2) \ | ||
| 2366 | return 0; \ | ||
| 2367 | \ | ||
| 2368 | ret = (val); \ | ||
| 2369 | if (ret == 0) \ | ||
| 2370 | ret = msginfo1->date_t - msginfo2->date_t; \ | ||
| 2371 | \ | ||
| 2372 | return ret * (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); \ | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | CMP_FUNC_DEF(procmsg_cmp_by_mark, | ||
| 2376 | MSG_IS_MARKED(msginfo1->flags) - MSG_IS_MARKED(msginfo2->flags)) | ||
| 2377 | CMP_FUNC_DEF(procmsg_cmp_by_unread, | ||
| 2378 | MSG_IS_UNREAD(msginfo1->flags) - MSG_IS_UNREAD(msginfo2->flags)) | ||
| 2379 | CMP_FUNC_DEF(procmsg_cmp_by_mime, | ||
| 2380 | MSG_IS_MIME(msginfo1->flags) - MSG_IS_MIME(msginfo2->flags)) | ||
| 2381 | CMP_FUNC_DEF(procmsg_cmp_by_label, | ||
| 2382 | MSG_GET_COLORLABEL(msginfo1->flags) - | ||
| 2383 | MSG_GET_COLORLABEL(msginfo2->flags)) | ||
| 2384 | CMP_FUNC_DEF(procmsg_cmp_by_size, msginfo1->size - msginfo2->size) | ||
| 2385 | |||
| 2386 | #undef CMP_FUNC_DEF | ||
| 2387 | #define CMP_FUNC_DEF(func_name, val) \ | ||
| 2388 | static gint func_name(gconstpointer a, gconstpointer b) \ | ||
| 2389 | { \ | ||
| 2390 | const MsgInfo *msginfo1 = a; \ | ||
| 2391 | const MsgInfo *msginfo2 = b; \ | ||
| 2392 | \ | ||
| 2393 | if (!msginfo1 || !msginfo2) \ | ||
| 2394 | return 0; \ | ||
| 2395 | \ | ||
| 2396 | return (val) * (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); \ | ||
| 2397 | } | ||
| 2398 | |||
| 2399 | CMP_FUNC_DEF(procmsg_cmp_by_number, msginfo1->msgnum - msginfo2->msgnum) | ||
| 2400 | CMP_FUNC_DEF(procmsg_cmp_by_date, msginfo1->date_t - msginfo2->date_t) | ||
| 2401 | |||
| 2402 | #undef CMP_FUNC_DEF | ||
| 2403 | #define CMP_FUNC_DEF(func_name, var_name) \ | ||
| 2404 | static gint func_name(gconstpointer a, gconstpointer b) \ | ||
| 2405 | { \ | ||
| 2406 | const MsgInfo *msginfo1 = a; \ | ||
| 2407 | const MsgInfo *msginfo2 = b; \ | ||
| 2408 | gint ret; \ | ||
| 2409 | \ | ||
| 2410 | if (!msginfo1->var_name) \ | ||
| 2411 | return (msginfo2->var_name != NULL) * \ | ||
| 2412 | (cmp_func_sort_type == SORT_ASCENDING ? -1 : 1);\ | ||
| 2413 | if (!msginfo2->var_name) \ | ||
| 2414 | return (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); \ | ||
| 2415 | \ | ||
| 2416 | ret = g_ascii_strcasecmp \ | ||
| 2417 | (msginfo1->var_name, msginfo2->var_name); \ | ||
| 2418 | if (ret == 0) \ | ||
| 2419 | ret = msginfo1->date_t - msginfo2->date_t; \ | ||
| 2420 | \ | ||
| 2421 | return ret * (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); \ | ||
| 2422 | } | ||
| 2423 | |||
| 2424 | CMP_FUNC_DEF(procmsg_cmp_by_from, fromname) | ||
| 2425 | CMP_FUNC_DEF(procmsg_cmp_by_to, to) | ||
| 2426 | |||
| 2427 | #undef CMP_FUNC_DEF | ||
| 2428 | |||
| 2429 | static gint procmsg_cmp_by_subject(gconstpointer a, gconstpointer b) | ||
| 2430 | { | ||
| 2431 | const MsgInfo *msginfo1 = a; | ||
| 2432 | const MsgInfo *msginfo2 = b; | ||
| 2433 | gint ret; | ||
| 2434 | |||
| 2435 | if (!msginfo1->subject) | ||
| 2436 | return (msginfo2->subject != NULL) * | ||
| 2437 | (cmp_func_sort_type == SORT_ASCENDING ? -1 : 1); | ||
| 2438 | if (!msginfo2->subject) | ||
| 2439 | return (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); | ||
| 2440 | |||
| 2441 | ret = subject_compare_for_sort(msginfo1->subject, msginfo2->subject); | ||
| 2442 | if (ret == 0) | ||
| 2443 | ret = msginfo1->date_t - msginfo2->date_t; | ||
| 2444 | |||
| 2445 | return ret * (cmp_func_sort_type == SORT_ASCENDING ? 1 : -1); | ||
| 2446 | } | ||
