root / src / mbox.c @ 478
History | View | Annotate | Download (10 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2001 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program 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 |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <stdio.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <sys/file.h> |
| 33 | #include <ctype.h> |
| 34 | #include <time.h> |
| 35 | |
| 36 | #include "mbox.h" |
| 37 | #include "procmsg.h" |
| 38 | #include "folder.h" |
| 39 | #include "filter.h" |
| 40 | #include "prefs_common.h" |
| 41 | #include "prefs_account.h" |
| 42 | #include "account.h" |
| 43 | #include "utils.h" |
| 44 | |
| 45 | #define MSGBUFSIZE 8192 |
| 46 | |
| 47 | #define FPUTS_TO_TMP_ABORT_IF_FAIL(s) \
|
| 48 | { \
|
| 49 | if (fputs(s, tmp_fp) == EOF) { \ |
| 50 | g_warning(_("can't write to temporary file\n")); \
|
| 51 | fclose(tmp_fp); \ |
| 52 | fclose(mbox_fp); \ |
| 53 | g_unlink(tmp_file); \ |
| 54 | g_free(tmp_file); \ |
| 55 | return -1; \ |
| 56 | } \ |
| 57 | } |
| 58 | |
| 59 | gint proc_mbox(FolderItem *dest, const gchar *mbox, GHashTable *folder_table)
|
| 60 | {
|
| 61 | FILE *mbox_fp; |
| 62 | gchar buf[MSGBUFSIZE], from_line[MSGBUFSIZE]; |
| 63 | gchar *tmp_file; |
| 64 | gint msgs = 0;
|
| 65 | |
| 66 | g_return_val_if_fail(dest != NULL, -1); |
| 67 | g_return_val_if_fail(mbox != NULL, -1); |
| 68 | |
| 69 | debug_print(_("Getting messages from %s into %s...\n"), mbox, dest->path);
|
| 70 | |
| 71 | if ((mbox_fp = g_fopen(mbox, "rb")) == NULL) { |
| 72 | FILE_OP_ERROR(mbox, "fopen");
|
| 73 | return -1; |
| 74 | } |
| 75 | |
| 76 | /* ignore empty lines on the head */
|
| 77 | do {
|
| 78 | if (fgets(buf, sizeof(buf), mbox_fp) == NULL) { |
| 79 | g_warning(_("can't read mbox file.\n"));
|
| 80 | fclose(mbox_fp); |
| 81 | return -1; |
| 82 | } |
| 83 | } while (buf[0] == '\n' || buf[0] == '\r'); |
| 84 | |
| 85 | if (strncmp(buf, "From ", 5) != 0) { |
| 86 | g_warning(_("invalid mbox format: %s\n"), mbox);
|
| 87 | fclose(mbox_fp); |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | strcpy(from_line, buf); |
| 92 | if (fgets(buf, sizeof(buf), mbox_fp) == NULL) { |
| 93 | g_warning(_("malformed mbox: %s\n"), mbox);
|
| 94 | fclose(mbox_fp); |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | tmp_file = get_tmp_file(); |
| 99 | |
| 100 | do {
|
| 101 | FILE *tmp_fp; |
| 102 | GSList *cur; |
| 103 | gchar *startp, *endp, *rpath; |
| 104 | gint empty_line; |
| 105 | gboolean is_next_msg = FALSE; |
| 106 | FilterInfo *fltinfo; |
| 107 | |
| 108 | if ((tmp_fp = g_fopen(tmp_file, "wb")) == NULL) { |
| 109 | FILE_OP_ERROR(tmp_file, "fopen");
|
| 110 | g_warning(_("can't open temporary file\n"));
|
| 111 | g_free(tmp_file); |
| 112 | fclose(mbox_fp); |
| 113 | return -1; |
| 114 | } |
| 115 | if (change_file_mode_rw(tmp_fp, tmp_file) < 0) |
| 116 | FILE_OP_ERROR(tmp_file, "chmod");
|
| 117 | |
| 118 | /* convert unix From into Return-Path */
|
| 119 | startp = from_line + 5;
|
| 120 | endp = strchr(startp, ' ');
|
| 121 | if (endp == NULL) |
| 122 | rpath = g_strdup(startp); |
| 123 | else
|
| 124 | rpath = g_strndup(startp, endp - startp); |
| 125 | g_strstrip(rpath); |
| 126 | g_snprintf(from_line, sizeof(from_line),
|
| 127 | "Return-Path: %s\n", rpath);
|
| 128 | g_free(rpath); |
| 129 | |
| 130 | FPUTS_TO_TMP_ABORT_IF_FAIL(from_line); |
| 131 | FPUTS_TO_TMP_ABORT_IF_FAIL(buf); |
| 132 | from_line[0] = '\0'; |
| 133 | |
| 134 | empty_line = 0;
|
| 135 | |
| 136 | while (fgets(buf, sizeof(buf), mbox_fp) != NULL) { |
| 137 | if (buf[0] == '\n' || buf[0] == '\r') { |
| 138 | empty_line++; |
| 139 | buf[0] = '\0'; |
| 140 | continue;
|
| 141 | } |
| 142 | |
| 143 | /* From separator */
|
| 144 | while (!strncmp(buf, "From ", 5)) { |
| 145 | strcpy(from_line, buf); |
| 146 | if (fgets(buf, sizeof(buf), mbox_fp) == NULL) { |
| 147 | buf[0] = '\0'; |
| 148 | break;
|
| 149 | } |
| 150 | |
| 151 | if (is_header_line(buf)) {
|
| 152 | is_next_msg = TRUE; |
| 153 | break;
|
| 154 | } else if (!strncmp(buf, "From ", 5)) { |
| 155 | continue;
|
| 156 | } else if (!strncmp(buf, ">From ", 6)) { |
| 157 | g_memmove(buf, buf + 1, strlen(buf));
|
| 158 | is_next_msg = TRUE; |
| 159 | break;
|
| 160 | } else {
|
| 161 | g_warning(_("unescaped From found:\n%s"),
|
| 162 | from_line); |
| 163 | break;
|
| 164 | } |
| 165 | } |
| 166 | if (is_next_msg) break; |
| 167 | |
| 168 | if (empty_line > 0) { |
| 169 | while (empty_line--)
|
| 170 | FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
|
| 171 | empty_line = 0;
|
| 172 | } |
| 173 | |
| 174 | if (from_line[0] != '\0') { |
| 175 | FPUTS_TO_TMP_ABORT_IF_FAIL(from_line); |
| 176 | from_line[0] = '\0'; |
| 177 | } |
| 178 | |
| 179 | if (buf[0] != '\0') { |
| 180 | if (!strncmp(buf, ">From ", 6)) { |
| 181 | FPUTS_TO_TMP_ABORT_IF_FAIL(buf + 1);
|
| 182 | } else
|
| 183 | FPUTS_TO_TMP_ABORT_IF_FAIL(buf); |
| 184 | |
| 185 | buf[0] = '\0'; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if (empty_line > 0) { |
| 190 | while (--empty_line)
|
| 191 | FPUTS_TO_TMP_ABORT_IF_FAIL("\n");
|
| 192 | } |
| 193 | |
| 194 | if (fclose(tmp_fp) == EOF) { |
| 195 | FILE_OP_ERROR(tmp_file, "fclose");
|
| 196 | g_warning(_("can't write to temporary file\n"));
|
| 197 | g_unlink(tmp_file); |
| 198 | g_free(tmp_file); |
| 199 | fclose(mbox_fp); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | fltinfo = filter_info_new(); |
| 204 | fltinfo->flags.perm_flags = MSG_NEW|MSG_UNREAD; |
| 205 | fltinfo->flags.tmp_flags = MSG_RECEIVED; |
| 206 | |
| 207 | if (folder_table)
|
| 208 | filter_apply(prefs_common.fltlist, tmp_file, fltinfo); |
| 209 | |
| 210 | if (fltinfo->actions[FLT_ACTION_MOVE] == FALSE &&
|
| 211 | fltinfo->actions[FLT_ACTION_DELETE] == FALSE) {
|
| 212 | if (folder_item_add_msg(dest, tmp_file, &fltinfo->flags,
|
| 213 | FALSE) < 0) {
|
| 214 | filter_info_free(fltinfo); |
| 215 | g_unlink(tmp_file); |
| 216 | g_free(tmp_file); |
| 217 | fclose(mbox_fp); |
| 218 | return -1; |
| 219 | } |
| 220 | fltinfo->dest_list = g_slist_append(fltinfo->dest_list, |
| 221 | dest); |
| 222 | } |
| 223 | |
| 224 | for (cur = fltinfo->dest_list; cur != NULL; cur = cur->next) { |
| 225 | FolderItem *drop_folder = (FolderItem *)cur->data; |
| 226 | gint val = 0;
|
| 227 | |
| 228 | if (folder_table) {
|
| 229 | val = GPOINTER_TO_INT(g_hash_table_lookup |
| 230 | (folder_table, |
| 231 | drop_folder)); |
| 232 | } |
| 233 | if (val == 0) { |
| 234 | /* force updating */
|
| 235 | if (FOLDER_IS_LOCAL(drop_folder->folder))
|
| 236 | drop_folder->mtime = 0;
|
| 237 | if (folder_table) {
|
| 238 | g_hash_table_insert(folder_table, |
| 239 | drop_folder, |
| 240 | GINT_TO_POINTER(1));
|
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | filter_info_free(fltinfo); |
| 246 | g_unlink(tmp_file); |
| 247 | |
| 248 | msgs++; |
| 249 | } while (from_line[0] != '\0'); |
| 250 | |
| 251 | g_free(tmp_file); |
| 252 | fclose(mbox_fp); |
| 253 | debug_print(_("%d messages found.\n"), msgs);
|
| 254 | |
| 255 | return msgs;
|
| 256 | } |
| 257 | |
| 258 | gint lock_mbox(const gchar *base, LockType type)
|
| 259 | {
|
| 260 | #ifdef G_OS_UNIX
|
| 261 | gint retval = 0;
|
| 262 | |
| 263 | if (type == LOCK_FILE) {
|
| 264 | gchar *lockfile, *locklink; |
| 265 | gint retry = 0;
|
| 266 | FILE *lockfp; |
| 267 | |
| 268 | lockfile = g_strdup_printf("%s.%d", base, getpid());
|
| 269 | if ((lockfp = g_fopen(lockfile, "wb")) == NULL) { |
| 270 | FILE_OP_ERROR(lockfile, "fopen");
|
| 271 | g_warning(_("can't create lock file %s\n"), lockfile);
|
| 272 | g_warning(_("use 'flock' instead of 'file' if possible.\n"));
|
| 273 | g_free(lockfile); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | fprintf(lockfp, "%d\n", getpid());
|
| 278 | fclose(lockfp); |
| 279 | |
| 280 | locklink = g_strconcat(base, ".lock", NULL); |
| 281 | while (link(lockfile, locklink) < 0) { |
| 282 | FILE_OP_ERROR(lockfile, "link");
|
| 283 | if (retry >= 5) { |
| 284 | g_warning(_("can't create %s\n"), lockfile);
|
| 285 | g_unlink(lockfile); |
| 286 | g_free(lockfile); |
| 287 | return -1; |
| 288 | } |
| 289 | if (retry == 0) |
| 290 | g_warning(_("mailbox is owned by another"
|
| 291 | " process, waiting...\n"));
|
| 292 | retry++; |
| 293 | sleep(5);
|
| 294 | } |
| 295 | g_unlink(lockfile); |
| 296 | g_free(lockfile); |
| 297 | } else if (type == LOCK_FLOCK) { |
| 298 | gint lockfd; |
| 299 | |
| 300 | #if HAVE_FLOCK
|
| 301 | if ((lockfd = open(base, O_RDONLY)) < 0) { |
| 302 | #else
|
| 303 | if ((lockfd = open(base, O_RDWR)) < 0) { |
| 304 | #endif
|
| 305 | FILE_OP_ERROR(base, "open");
|
| 306 | return -1; |
| 307 | } |
| 308 | #if HAVE_FLOCK
|
| 309 | if (flock(lockfd, LOCK_EX|LOCK_NB) < 0) { |
| 310 | perror("flock");
|
| 311 | #else
|
| 312 | #if HAVE_LOCKF
|
| 313 | if (lockf(lockfd, F_TLOCK, 0) < 0) { |
| 314 | perror("lockf");
|
| 315 | #else
|
| 316 | {
|
| 317 | #endif
|
| 318 | #endif /* HAVE_FLOCK */ |
| 319 | g_warning(_("can't lock %s\n"), base);
|
| 320 | if (close(lockfd) < 0) |
| 321 | perror("close");
|
| 322 | return -1; |
| 323 | } |
| 324 | retval = lockfd; |
| 325 | } else {
|
| 326 | g_warning(_("invalid lock type\n"));
|
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | return retval;
|
| 331 | #else
|
| 332 | return -1; |
| 333 | #endif /* G_OS_UNIX */ |
| 334 | } |
| 335 | |
| 336 | gint unlock_mbox(const gchar *base, gint fd, LockType type)
|
| 337 | {
|
| 338 | if (type == LOCK_FILE) {
|
| 339 | gchar *lockfile; |
| 340 | |
| 341 | lockfile = g_strconcat(base, ".lock", NULL); |
| 342 | if (g_unlink(lockfile) < 0) { |
| 343 | FILE_OP_ERROR(lockfile, "unlink");
|
| 344 | g_free(lockfile); |
| 345 | return -1; |
| 346 | } |
| 347 | g_free(lockfile); |
| 348 | |
| 349 | return 0; |
| 350 | } else if (type == LOCK_FLOCK) { |
| 351 | #if HAVE_FLOCK
|
| 352 | if (flock(fd, LOCK_UN) < 0) { |
| 353 | perror("flock");
|
| 354 | #else
|
| 355 | #if HAVE_LOCKF
|
| 356 | if (lockf(fd, F_ULOCK, 0) < 0) { |
| 357 | perror("lockf");
|
| 358 | #else
|
| 359 | {
|
| 360 | #endif
|
| 361 | #endif /* HAVE_FLOCK */ |
| 362 | g_warning(_("can't unlock %s\n"), base);
|
| 363 | if (close(fd) < 0) |
| 364 | perror("close");
|
| 365 | return -1; |
| 366 | } |
| 367 | |
| 368 | if (close(fd) < 0) { |
| 369 | perror("close");
|
| 370 | return -1; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | g_warning(_("invalid lock type\n"));
|
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | gint copy_mbox(const gchar *src, const gchar *dest) |
| 381 | {
|
| 382 | return copy_file(src, dest, TRUE);
|
| 383 | } |
| 384 | |
| 385 | void empty_mbox(const gchar *mbox) |
| 386 | {
|
| 387 | #if HAVE_TRUNCATE
|
| 388 | if (truncate(mbox, 0) < 0) { |
| 389 | #endif
|
| 390 | FILE *fp; |
| 391 | |
| 392 | #if HAVE_TRUNCATE
|
| 393 | FILE_OP_ERROR(mbox, "truncate");
|
| 394 | #endif
|
| 395 | if ((fp = g_fopen(mbox, "wb")) == NULL) { |
| 396 | FILE_OP_ERROR(mbox, "fopen");
|
| 397 | g_warning(_("can't truncate mailbox to zero.\n"));
|
| 398 | return;
|
| 399 | } |
| 400 | fclose(fp); |
| 401 | #if HAVE_TRUNCATE
|
| 402 | } |
| 403 | #endif
|
| 404 | } |
| 405 | |
| 406 | /* read all messages in SRC, and store them into one MBOX file. */
|
| 407 | gint export_to_mbox(FolderItem *src, const gchar *mbox)
|
| 408 | {
|
| 409 | GSList *mlist; |
| 410 | GSList *cur; |
| 411 | MsgInfo *msginfo; |
| 412 | FILE *msg_fp; |
| 413 | FILE *mbox_fp; |
| 414 | gchar buf[BUFFSIZE]; |
| 415 | |
| 416 | g_return_val_if_fail(src != NULL, -1); |
| 417 | g_return_val_if_fail(src->folder != NULL, -1); |
| 418 | g_return_val_if_fail(mbox != NULL, -1); |
| 419 | |
| 420 | debug_print(_("Exporting messages from %s into %s...\n"),
|
| 421 | src->path, mbox); |
| 422 | |
| 423 | if ((mbox_fp = g_fopen(mbox, "wb")) == NULL) { |
| 424 | FILE_OP_ERROR(mbox, "fopen");
|
| 425 | return -1; |
| 426 | } |
| 427 | |
| 428 | mlist = folder_item_get_msg_list(src, TRUE); |
| 429 | |
| 430 | for (cur = mlist; cur != NULL; cur = cur->next) { |
| 431 | msginfo = (MsgInfo *)cur->data; |
| 432 | |
| 433 | msg_fp = procmsg_open_message(msginfo); |
| 434 | if (!msg_fp) {
|
| 435 | procmsg_msginfo_free(msginfo); |
| 436 | continue;
|
| 437 | } |
| 438 | |
| 439 | strncpy2(buf, |
| 440 | msginfo->from ? msginfo->from : |
| 441 | cur_account && cur_account->address ? |
| 442 | cur_account->address : "unknown",
|
| 443 | sizeof(buf));
|
| 444 | extract_address(buf); |
| 445 | |
| 446 | fprintf(mbox_fp, "From %s %s",
|
| 447 | buf, ctime(&msginfo->date_t)); |
| 448 | |
| 449 | while (fgets(buf, sizeof(buf), msg_fp) != NULL) { |
| 450 | if (!strncmp(buf, "From ", 5)) |
| 451 | fputc('>', mbox_fp);
|
| 452 | fputs(buf, mbox_fp); |
| 453 | } |
| 454 | fputc('\n', mbox_fp);
|
| 455 | |
| 456 | fclose(msg_fp); |
| 457 | procmsg_msginfo_free(msginfo); |
| 458 | } |
| 459 | |
| 460 | g_slist_free(mlist); |
| 461 | |
| 462 | fclose(mbox_fp); |
| 463 | |
| 464 | return 0; |
| 465 | } |