7035ccaa43d5492da0eb60b41bb8c628297bf38e
[claws.git] / src / mh.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <errno.h>
33
34 #undef MEASURE_TIME
35
36 #ifdef MEASURE_TIME
37 #  include <sys/time.h>
38 #endif
39
40 #include "folder.h"
41 #include "mh.h"
42 #include "procmsg.h"
43 #include "procheader.h"
44 #include "utils.h"
45 #include "codeconv.h"
46 #include "statusbar.h"
47 #include "gtkutils.h"
48
49 /* Define possible missing constants for Windows. */
50 #ifdef G_OS_WIN32
51 # ifndef S_IRGRP
52 # define S_IRGRP 0
53 # define S_IWGRP 0
54 # endif
55 # ifndef S_IROTH
56 # define S_IROTH 0
57 # define S_IWOTH 0
58 # endif
59 #endif
60
61
62 static void     mh_folder_init          (Folder         *folder,
63                                          const gchar    *name,
64                                          const gchar    *path);
65
66 static Folder   *mh_folder_new          (const gchar    *name,
67                                          const gchar    *path);
68 static void     mh_folder_destroy       (Folder         *folder);
69 static gchar   *mh_fetch_msg            (Folder         *folder,
70                                          FolderItem     *item,
71                                          gint            num);
72 static MsgInfo *mh_get_msginfo          (Folder         *folder,
73                                          FolderItem     *item,
74                                          gint            num);
75 static gint     mh_add_msg              (Folder         *folder,
76                                          FolderItem     *dest,
77                                          const gchar    *file,
78                                          MsgFlags       *flags);
79 static gint     mh_add_msgs             (Folder         *folder,
80                                          FolderItem     *dest,
81                                          GSList         *file_list,
82                                          GRelation      *relation);
83 static gint     mh_copy_msg             (Folder         *folder,
84                                          FolderItem     *dest,
85                                          MsgInfo        *msginfo);
86 static gint     mh_copy_msgs            (Folder         *folder, 
87                                          FolderItem     *dest, 
88                                          MsgInfoList    *msglist, 
89                                          GRelation      *relation);
90 static gint     mh_remove_msg           (Folder         *folder,
91                                          FolderItem     *item,
92                                          gint            num);
93 static gint     mh_remove_msgs          (Folder         *folder, 
94                                          FolderItem     *item, 
95                                          MsgInfoList    *msglist, 
96                                          GRelation      *relation);
97 static gint     mh_remove_all_msg       (Folder         *folder,
98                                          FolderItem     *item);
99 static gboolean mh_is_msg_changed       (Folder         *folder,
100                                          FolderItem     *item,
101                                          MsgInfo        *msginfo);
102
103 static gint     mh_get_num_list         (Folder         *folder,
104                                          FolderItem     *item, 
105                                          GSList         **list, 
106                                          gboolean       *old_uids_valid);
107 static gint     mh_scan_tree            (Folder         *folder);
108
109 static gint    mh_create_tree           (Folder         *folder);
110 static FolderItem *mh_create_folder     (Folder         *folder,
111                                          FolderItem     *parent,
112                                          const gchar    *name);
113 static gint    mh_rename_folder         (Folder         *folder,
114                                          FolderItem     *item,
115                                          const gchar    *name);
116 static gint    mh_remove_folder         (Folder         *folder,
117                                          FolderItem     *item);
118
119 static gchar   *mh_get_new_msg_filename         (FolderItem     *dest);
120
121 static MsgInfo *mh_parse_msg                    (const gchar    *file,
122                                                  FolderItem     *item);
123 static void     mh_remove_missing_folder_items  (Folder         *folder);
124 static gchar    *mh_filename_from_utf8          (const gchar    *path);
125 static gchar    *mh_filename_to_utf8            (const gchar    *path);
126 static void     mh_scan_tree_recursive          (FolderItem     *item);
127
128 static gboolean mh_rename_folder_func           (GNode          *node,
129                                                  gpointer        data);
130 static gchar   *mh_item_get_path                (Folder *folder, 
131                                                  FolderItem *item);
132
133 static gboolean mh_scan_required        (Folder         *folder,
134                                          FolderItem     *item);
135 static int mh_item_close                (Folder         *folder,
136                                          FolderItem     *item);
137 static gint mh_get_flags                (Folder *folder, FolderItem *item,
138                                          MsgInfoList *msginfo_list, GRelation *msgflags);
139 static void mh_write_sequences          (FolderItem     *item, gboolean remove_unseen);
140
141 static FolderClass mh_class;
142
143 FolderClass *mh_get_class(void)
144 {
145         if (mh_class.idstr == NULL) {
146                 mh_class.type = F_MH;
147                 mh_class.idstr = "mh";
148                 mh_class.uistr = "MH";
149                 
150                 /* Folder functions */
151                 mh_class.new_folder = mh_folder_new;
152                 mh_class.destroy_folder = mh_folder_destroy;
153                 mh_class.set_xml = folder_local_set_xml;
154                 mh_class.get_xml = folder_local_get_xml;
155                 mh_class.scan_tree = mh_scan_tree;
156                 mh_class.create_tree = mh_create_tree;
157
158                 /* FolderItem functions */
159                 mh_class.item_get_path = mh_item_get_path;
160                 mh_class.create_folder = mh_create_folder;
161                 mh_class.rename_folder = mh_rename_folder;
162                 mh_class.remove_folder = mh_remove_folder;
163                 mh_class.get_num_list = mh_get_num_list;
164                 mh_class.scan_required = mh_scan_required;
165                 mh_class.close = mh_item_close;
166                 mh_class.get_flags = NULL; /*mh_get_flags */;
167
168                 /* Message functions */
169                 mh_class.get_msginfo = mh_get_msginfo;
170                 mh_class.fetch_msg = mh_fetch_msg;
171                 mh_class.add_msg = mh_add_msg;
172                 mh_class.add_msgs = mh_add_msgs;
173                 mh_class.copy_msg = mh_copy_msg;
174                 mh_class.copy_msgs = mh_copy_msgs;
175                 mh_class.remove_msg = mh_remove_msg;
176                 mh_class.remove_msgs = mh_remove_msgs;
177                 mh_class.remove_all_msg = mh_remove_all_msg;
178                 mh_class.is_msg_changed = mh_is_msg_changed;
179         }
180
181         return &mh_class;
182 }
183
184 static Folder *mh_folder_new(const gchar *name, const gchar *path)
185 {
186         Folder *folder;
187
188         folder = (Folder *)g_new0(MHFolder, 1);
189         folder->klass = &mh_class;
190         mh_folder_init(folder, name, path);
191
192         return folder;
193 }
194
195 static void mh_folder_destroy(Folder *folder)
196 {
197         folder_local_folder_destroy(LOCAL_FOLDER(folder));
198 }
199
200 static void mh_folder_init(Folder *folder, const gchar *name, const gchar *path)
201 {
202         folder_local_folder_init(folder, name, path);
203
204 }
205
206 gboolean mh_scan_required(Folder *folder, FolderItem *item)
207 {
208         gchar *path;
209         struct stat s;
210
211         path = folder_item_get_path(item);
212         g_return_val_if_fail(path != NULL, FALSE);
213
214         if (stat(path, &s) < 0) {
215                 FILE_OP_ERROR(path, "stat");
216                 g_free(path);
217                 return FALSE;
218         }
219
220         if ((s.st_mtime > item->mtime) &&
221                 (s.st_mtime - 3600 != item->mtime)) {
222                 debug_print("MH scan required, folder updated: %s (%ld > %ld)\n",
223                             path,
224                             (long int) s.st_mtime,
225                             (long int) item->mtime);
226                 g_free(path);
227                 return TRUE;
228         }
229
230         debug_print("MH scan not required: %s (%ld <= %ld)\n",
231                     path,
232                     (long int) s.st_mtime,
233                     (long int) item->mtime);
234         g_free(path);
235         return FALSE;
236 }
237
238 void mh_get_last_num(Folder *folder, FolderItem *item)
239 {
240         gchar *path;
241         DIR *dp;
242         struct dirent *d;
243         gint max = 0;
244         gint num;
245
246         g_return_if_fail(item != NULL);
247
248         debug_print("mh_get_last_num(): Scanning %s ...\n", item->path);
249
250         path = folder_item_get_path(item);
251         g_return_if_fail(path != NULL);
252         if (change_dir(path) < 0) {
253                 g_free(path);
254                 return;
255         }
256         g_free(path);
257
258         if ((dp = opendir(".")) == NULL) {
259                 FILE_OP_ERROR(item->path, "opendir");
260                 return;
261         }
262
263         while ((d = readdir(dp)) != NULL) {
264                 if ((num = to_number(d->d_name)) > 0 &&
265                     dirent_is_regular_file(d)) {
266                         if (max < num)
267                                 max = num;
268                 }
269         }
270         closedir(dp);
271
272         debug_print("Last number in dir %s = %d\n", item->path, max);
273         item->last_num = max;
274 }
275
276 gint mh_get_num_list(Folder *folder, FolderItem *item, GSList **list, gboolean *old_uids_valid)
277 {
278
279         gchar *path;
280         DIR *dp;
281         struct dirent *d;
282         gint num, nummsgs = 0;
283
284         g_return_val_if_fail(item != NULL, -1);
285
286         debug_print("mh_get_num_list(): Scanning %s ...\n", item->path);
287
288         *old_uids_valid = TRUE;
289
290         path = folder_item_get_path(item);
291         g_return_val_if_fail(path != NULL, -1);
292         if (change_dir(path) < 0) {
293                 g_free(path);
294                 return -1;
295         }
296         g_free(path);
297
298         if ((dp = opendir(".")) == NULL) {
299                 FILE_OP_ERROR(item->path, "opendir");
300                 return -1;
301         }
302
303         while ((d = readdir(dp)) != NULL) {
304                 if ((num = to_number(d->d_name)) > 0) {
305                         *list = g_slist_prepend(*list, GINT_TO_POINTER(num));
306                         nummsgs++;
307                 }
308         }
309         closedir(dp);
310
311         item->mtime = time(NULL);
312         return nummsgs;
313 }
314
315 static gchar *mh_fetch_msg(Folder *folder, FolderItem *item, gint num)
316 {
317         gchar *path;
318         gchar *file;
319
320         g_return_val_if_fail(item != NULL, NULL);
321         g_return_val_if_fail(num > 0, NULL);
322
323         path = folder_item_get_path(item);
324         file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(num), NULL);
325
326         if (!is_file_exist(file)) {
327                 g_free(file);
328                 g_free(path);
329                 return NULL;
330         }
331         g_free(path);
332         return file;
333 }
334
335 static MsgInfo *mh_get_msginfo(Folder *folder, FolderItem *item, gint num)
336 {
337         MsgInfo *msginfo;
338         gchar *file;
339
340         g_return_val_if_fail(item != NULL, NULL);
341         if (num <= 0)
342                 return NULL;
343
344         file = mh_fetch_msg(folder, item, num);
345         if (!file) return NULL;
346
347         msginfo = mh_parse_msg(file, item);
348         if (msginfo)
349                 msginfo->msgnum = num;
350
351         g_free(file);
352
353         return msginfo;
354 }
355
356 static gchar *mh_get_new_msg_filename(FolderItem *dest)
357 {
358         gchar *destfile;
359         gchar *destpath;
360
361         destpath = folder_item_get_path(dest);
362         g_return_val_if_fail(destpath != NULL, NULL);
363
364         if (!is_dir_exist(destpath))
365                 make_dir_hier(destpath);
366
367         for (;;) {
368                 destfile = g_strdup_printf("%s%c%d", destpath, G_DIR_SEPARATOR,
369                                            dest->last_num + 1);
370                 if (is_file_entry_exist(destfile)) {
371                         dest->last_num++;
372                         g_free(destfile);
373                 } else
374                         break;
375         }
376
377         g_free(destpath);
378
379         return destfile;
380 }
381
382 static gint mh_add_msg(Folder *folder, FolderItem *dest, const gchar *file, MsgFlags *flags)
383 {
384         gint ret;
385         GSList file_list;
386         MsgFileInfo fileinfo;
387
388         g_return_val_if_fail(file != NULL, -1);
389
390         fileinfo.msginfo = NULL;
391         fileinfo.file = (gchar *)file;
392         fileinfo.flags = flags;
393         file_list.data = &fileinfo;
394         file_list.next = NULL;
395
396         ret = mh_add_msgs(folder, dest, &file_list, NULL);
397         return ret;
398
399  
400 static gint mh_add_msgs(Folder *folder, FolderItem *dest, GSList *file_list, 
401                  GRelation *relation)
402
403         gchar *destfile;
404         GSList *cur;
405         MsgFileInfo *fileinfo;
406
407         g_return_val_if_fail(dest != NULL, -1);
408         g_return_val_if_fail(file_list != NULL, -1);
409
410         if (dest->last_num < 0) {
411                 mh_get_last_num(folder, dest);
412                 if (dest->last_num < 0) return -1;
413         }
414
415         for (cur = file_list; cur != NULL; cur = cur->next) {
416                 fileinfo = (MsgFileInfo *)cur->data;
417
418                 destfile = mh_get_new_msg_filename(dest);
419                 if (destfile == NULL) return -1;
420
421 #ifdef G_OS_UNIX
422                 if (link(fileinfo->file, destfile) < 0) {
423 #endif
424                         if (copy_file(fileinfo->file, destfile, TRUE) < 0) {
425                                 g_warning(_("can't copy message %s to %s\n"),
426                                           fileinfo->file, destfile);
427                                 g_free(destfile);
428                                 return -1;
429                         }
430 #ifdef G_OS_UNIX
431                 }
432 #endif
433
434                 if (relation != NULL)
435                         g_relation_insert(relation, fileinfo, GINT_TO_POINTER(dest->last_num + 1));
436                 g_free(destfile);
437                 dest->last_num++;
438         }
439         mh_write_sequences(dest, TRUE);
440         return dest->last_num;
441 }
442
443 static gint mh_copy_msg(Folder *folder, FolderItem *dest, MsgInfo *msginfo)
444 {
445         GSList msglist;
446
447         g_return_val_if_fail(msginfo != NULL, -1);
448
449         msglist.data = msginfo;
450         msglist.next = NULL;
451
452         return mh_copy_msgs(folder, dest, &msglist, NULL);      
453 }
454
455 static gint mh_copy_msgs(Folder *folder, FolderItem *dest, MsgInfoList *msglist, 
456                          GRelation *relation)
457 {
458         gboolean dest_need_scan = FALSE;
459         gchar *srcfile;
460         gchar *destfile;
461         gint filemode = 0;
462         FolderItemPrefs *prefs;
463         MsgInfo *msginfo = NULL;
464         gboolean remove_special_headers = FALSE;
465         MsgInfoList *cur = NULL;
466         gint curnum = 0, total = 0;
467         gchar *srcpath = NULL;
468         gboolean full_fetch = FALSE;
469
470         g_return_val_if_fail(dest != NULL, -1);
471         g_return_val_if_fail(msglist != NULL, -1);
472         
473         msginfo = (MsgInfo *)msglist->data;
474
475         g_return_val_if_fail(msginfo != NULL, -1);
476
477         if (msginfo->folder == dest) {
478                 g_warning("the src folder is identical to the dest.\n");
479                 return -1;
480         }
481
482         if (msginfo->folder->folder != dest->folder)
483                 full_fetch = TRUE;
484
485         if (dest->last_num < 0) {
486                 mh_get_last_num(folder, dest);
487                 if (dest->last_num < 0) return -1;
488         }
489
490         if ((MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
491         && !folder_has_parent_of_type(dest, F_QUEUE)
492         && !folder_has_parent_of_type(dest, F_DRAFT)) {
493                 /* as every msginfo comes from the same folder, it is supposed they
494                  * will either match the preceding condition either all or none.
495                  */
496                 remove_special_headers = TRUE;
497         } else if (!(MSG_IS_QUEUED(msginfo->flags) || MSG_IS_DRAFT(msginfo->flags))
498         && (folder_has_parent_of_type(dest, F_QUEUE)
499          || folder_has_parent_of_type(dest, F_DRAFT))) {
500                 return -1;
501         } 
502
503         prefs = dest->prefs;
504
505         srcpath = folder_item_get_path(msginfo->folder);
506
507         total = g_slist_length(msglist);
508         if (total > 100) {
509                 if (MSG_IS_MOVE(msginfo->flags))
510                         statusbar_print_all(_("Moving messages..."));
511                 else
512                         statusbar_print_all(_("Copying messages..."));
513         }
514         for (cur = msglist; cur; cur = cur->next) {
515                 gboolean moved = FALSE;
516                 msginfo = (MsgInfo *)cur->data;
517                 if (!msginfo) {
518                         goto err_reset_status;
519                 }
520                 if (!full_fetch) {
521                         srcfile = g_strconcat(srcpath, 
522                                 G_DIR_SEPARATOR_S, 
523                                 itos(msginfo->msgnum), NULL);
524                 } else {
525                         srcfile = procmsg_get_message_file(msginfo);
526                 }
527                 if (!srcfile) {
528                         goto err_reset_status;
529                 }
530                 destfile = mh_get_new_msg_filename(dest);
531                 if (!destfile) {
532                         g_free(srcfile);
533                         goto err_reset_status;
534                 }
535
536                 if (total > 100) {
537                         statusbar_progress_all(curnum, total, 100);
538                         if (curnum % 100 == 0)
539                                 GTK_EVENTS_FLUSH();
540                         curnum++;
541                 }
542
543                 debug_print("Copying message %s%c%d to %s ...\n",
544                             msginfo->folder->path, G_DIR_SEPARATOR,
545                             msginfo->msgnum, dest->path);
546
547
548                 if (remove_special_headers) {
549                         if (procmsg_remove_special_headers(srcfile, destfile) !=0) {
550                                 g_free(srcfile);
551                                 g_free(destfile);
552                                 goto err_reset_status;
553                         }
554                 } else if (MSG_IS_MOVE(msginfo->flags)) {
555                         if (move_file(srcfile, destfile, TRUE) < 0) {
556                                 FILE_OP_ERROR(srcfile, "move");
557                                 if (copy_file(srcfile, destfile, TRUE) < 0) {
558                                         FILE_OP_ERROR(srcfile, "copy");
559                                         g_free(srcfile);
560                                         g_free(destfile);
561                                         goto err_reset_status;
562                                 }
563                         }
564                 } else if (copy_file(srcfile, destfile, TRUE) < 0) {
565                         FILE_OP_ERROR(srcfile, "copy");
566                         g_free(srcfile);
567                         g_free(destfile);
568                         goto err_reset_status;
569                 } 
570                 if (prefs && prefs->enable_folder_chmod && prefs->folder_chmod) {
571                         if (chmod(destfile, prefs->folder_chmod) < 0)
572                                 FILE_OP_ERROR(destfile, "chmod");
573
574                         /* for mark file */
575                         filemode = prefs->folder_chmod;
576                         if (filemode & S_IRGRP) filemode |= S_IWGRP;
577                         if (filemode & S_IROTH) filemode |= S_IWOTH;
578                 }
579                 if (relation)
580                         g_relation_insert(relation, msginfo, GINT_TO_POINTER(dest->last_num+1));
581                 g_free(srcfile);
582                 g_free(destfile);
583                 dest->last_num++;
584         }
585
586         g_free(srcpath);
587         mh_write_sequences(dest, TRUE);
588
589         dest_need_scan = mh_scan_required(dest->folder, dest);
590         if (!dest_need_scan)
591                 dest->mtime = time(NULL);
592         
593         if (total > 100) {
594                 statusbar_progress_all(0,0,0);
595                 statusbar_pop_all();
596         }
597         return dest->last_num;
598 err_reset_status:
599         g_free(srcpath);
600         mh_write_sequences(dest, TRUE);
601         if (total > 100) {
602                 statusbar_progress_all(0,0,0);
603                 statusbar_pop_all();
604         }
605         return -1;
606
607 }
608
609 static gint mh_remove_msg(Folder *folder, FolderItem *item, gint num)
610 {
611         gboolean need_scan = FALSE;
612         gchar *file;
613
614         g_return_val_if_fail(item != NULL, -1);
615
616         file = mh_fetch_msg(folder, item, num);
617         g_return_val_if_fail(file != NULL, -1);
618
619         need_scan = mh_scan_required(folder, item);
620
621         if (g_unlink(file) < 0) {
622                 FILE_OP_ERROR(file, "unlink");
623                 g_free(file);
624                 return -1;
625         }
626
627         if (!need_scan)
628                 item->mtime = time(NULL);
629
630         g_free(file);
631         return 0;
632 }
633
634 static gint mh_remove_msgs(Folder *folder, FolderItem *item, 
635                     MsgInfoList *msglist, GRelation *relation)
636 {
637         gboolean need_scan = FALSE;
638         gchar *path, *file;
639         MsgInfoList *cur;
640
641         g_return_val_if_fail(item != NULL, -1);
642
643         path = folder_item_get_path(item);
644         
645         for (cur = msglist; cur; cur = cur->next) {
646                 MsgInfo *msginfo = (MsgInfo *)cur->data;
647                 if (msginfo == NULL)
648                         continue;
649                 file = g_strconcat(path, G_DIR_SEPARATOR_S, itos(msginfo->msgnum), NULL);
650                 if (file == NULL)
651                         continue;
652                 
653                 if (g_unlink(file) < 0) {
654                         g_free(file);
655                         continue;
656                 }
657                 
658                 g_free(file);
659         }
660
661         need_scan = mh_scan_required(folder, item);
662
663         if (!need_scan)
664                 item->mtime = time(NULL);
665
666         g_free(path);
667         return 0;
668 }
669
670 static gint mh_remove_all_msg(Folder *folder, FolderItem *item)
671 {
672         gchar *path;
673         gint val;
674
675         g_return_val_if_fail(item != NULL, -1);
676
677         path = folder_item_get_path(item);
678         g_return_val_if_fail(path != NULL, -1);
679         val = remove_all_numbered_files(path);
680         g_free(path);
681
682         mh_write_sequences(item, TRUE);
683
684         return val;
685 }
686
687 static gboolean mh_is_msg_changed(Folder *folder, FolderItem *item,
688                                   MsgInfo *msginfo)
689 {
690         struct stat s;
691
692         if (g_stat(itos(msginfo->msgnum), &s) < 0 ||
693             msginfo->size  != s.st_size || (
694                 (msginfo->mtime - s.st_mtime != 0) &&
695                 (msginfo->mtime - s.st_mtime != 3600) &&
696                 (msginfo->mtime - s.st_mtime != -3600)))
697                 return TRUE;
698
699         return FALSE;
700 }
701
702 static gint mh_scan_tree(Folder *folder)
703 {
704         FolderItem *item;
705         gchar *rootpath;
706
707         g_return_val_if_fail(folder != NULL, -1);
708
709         if (!folder->node) {
710                 item = folder_item_new(folder, folder->name, NULL);
711                 item->folder = folder;
712                 folder->node = item->node = g_node_new(item);
713         } else
714                 item = FOLDER_ITEM(folder->node->data);
715
716         rootpath = folder_item_get_path(item);
717         if (change_dir(rootpath) < 0) {
718                 g_free(rootpath);
719                 return -1;
720         }
721         g_free(rootpath);
722
723         mh_create_tree(folder);
724         mh_remove_missing_folder_items(folder);
725         mh_scan_tree_recursive(item);
726
727         return 0;
728 }
729
730 #define MAKE_DIR_IF_NOT_EXIST(dir) \
731 { \
732         if (!is_dir_exist(dir)) { \
733                 if (is_file_exist(dir)) { \
734                         g_warning("File `%s' already exists.\n" \
735                                     "Can't create folder.", dir); \
736                         return -1; \
737                 } \
738                 if (make_dir(dir) < 0) \
739                         return -1; \
740         } \
741 }
742
743 static gint mh_create_tree(Folder *folder)
744 {
745         gchar *rootpath;
746
747         g_return_val_if_fail(folder != NULL, -1);
748
749         CHDIR_RETURN_VAL_IF_FAIL(get_mail_base_dir(), -1);
750         rootpath = LOCAL_FOLDER(folder)->rootpath;
751         MAKE_DIR_IF_NOT_EXIST(rootpath);
752         CHDIR_RETURN_VAL_IF_FAIL(rootpath, -1);
753         MAKE_DIR_IF_NOT_EXIST(INBOX_DIR);
754         MAKE_DIR_IF_NOT_EXIST(OUTBOX_DIR);
755         MAKE_DIR_IF_NOT_EXIST(QUEUE_DIR);
756         MAKE_DIR_IF_NOT_EXIST(DRAFT_DIR);
757         MAKE_DIR_IF_NOT_EXIST(TRASH_DIR);
758
759         return 0;
760 }
761
762 #undef MAKE_DIR_IF_NOT_EXIST
763
764 static gchar *mh_item_get_path(Folder *folder, FolderItem *item)
765 {
766         gchar *folder_path, *path;
767         gchar *real_path;
768         g_return_val_if_fail(folder != NULL, NULL);
769         g_return_val_if_fail(item != NULL, NULL);
770
771         folder_path = g_strdup(LOCAL_FOLDER(folder)->rootpath);
772         g_return_val_if_fail(folder_path != NULL, NULL);
773
774         /* FIXME: [W32] The code below does not correctly merge
775            relative filenames; there should be a function to handle
776            this.  */
777         if ( !is_relative_filename (folder_path) ) {
778                 if (item->path)
779                         path = g_strconcat(folder_path, G_DIR_SEPARATOR_S,
780                                            item->path, NULL);
781                 else
782                         path = g_strdup(folder_path);
783         } else {
784                 if (item->path)
785                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
786                                            folder_path, G_DIR_SEPARATOR_S,
787                                            item->path, NULL);
788                 else
789                         path = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S,
790                                            folder_path, NULL);
791         }
792         g_free(folder_path);
793         real_path = mh_filename_from_utf8(path);
794         if (!is_dir_exist(real_path) && is_dir_exist(path)) {
795                 /* mmh, older version did put utf8 filenames instead of
796                  * the correct encoding */
797                 rename(path, real_path);
798                 folder_item_scan(item);
799         }
800
801         g_free(path);
802         return real_path;
803 }
804
805 static FolderItem *mh_create_folder(Folder *folder, FolderItem *parent,
806                                     const gchar *name)
807 {
808         gchar *path, *real_name;
809         gchar *fullpath;
810         FolderItem *new_item;
811         gchar *mh_sequences_filename;
812         FILE *mh_sequences_file;
813
814         g_return_val_if_fail(folder != NULL, NULL);
815         g_return_val_if_fail(parent != NULL, NULL);
816         g_return_val_if_fail(name != NULL, NULL);
817
818         path = folder_item_get_path(parent);
819         if (!is_dir_exist(path)) 
820                 if (make_dir_hier(path) != 0)
821                         return NULL;
822                 
823         real_name = mh_filename_from_utf8(name);
824         fullpath = g_strconcat(path, G_DIR_SEPARATOR_S, real_name, NULL);
825         g_free(real_name);
826         g_free(path);
827
828         if (make_dir(fullpath) < 0) {
829                 g_free(fullpath);
830                 return NULL;
831         }
832
833         g_free(fullpath);
834
835         if (parent->path)
836                 path = g_strconcat(parent->path, G_DIR_SEPARATOR_S, name,
837                                    NULL);
838         else
839                 path = g_strdup(name);
840         new_item = folder_item_new(folder, name, path);
841         folder_item_append(parent, new_item);
842
843         g_free(path);
844
845         path = folder_item_get_path(new_item);
846         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
847                                             ".mh_sequences", NULL);
848         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "a+b")) != NULL) {
849                 fclose(mh_sequences_file);
850         }
851         g_free(mh_sequences_filename);
852         g_free(path);
853
854         return new_item;
855 }
856
857 static gint mh_rename_folder(Folder *folder, FolderItem *item,
858                              const gchar *name)
859 {
860         gchar *real_name;
861         gchar *oldpath;
862         gchar *dirname;
863         gchar *newpath, *utf8newpath;
864         gchar *paths[2];
865
866         g_return_val_if_fail(folder != NULL, -1);
867         g_return_val_if_fail(item != NULL, -1);
868         g_return_val_if_fail(item->path != NULL, -1);
869         g_return_val_if_fail(name != NULL, -1);
870
871         oldpath = folder_item_get_path(item);
872         if (!is_dir_exist(oldpath))
873                 make_dir_hier(oldpath);
874
875         dirname = g_path_get_dirname(oldpath);
876         real_name = mh_filename_from_utf8(name);
877         newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S, real_name, NULL);
878         g_free(real_name);
879
880         if (g_rename(oldpath, newpath) < 0) {
881                 FILE_OP_ERROR(oldpath, "rename");
882                 g_free(oldpath);
883                 g_free(newpath);
884                 return -1;
885         }
886
887         g_free(oldpath);
888         g_free(newpath);
889
890         if (strchr(item->path, G_DIR_SEPARATOR) != NULL) {
891                 dirname = g_path_get_dirname(item->path);
892                 utf8newpath = g_strconcat(dirname, G_DIR_SEPARATOR_S,
893                                           name, NULL);
894                 g_free(dirname);
895         } else
896                 utf8newpath = g_strdup(name);
897
898         g_free(item->name);
899         item->name = g_strdup(name);
900
901         paths[0] = g_strdup(item->path);
902         paths[1] = utf8newpath;
903         g_node_traverse(item->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
904                         mh_rename_folder_func, paths);
905
906         g_free(paths[0]);
907         g_free(paths[1]);
908         return 0;
909 }
910
911 static gint mh_remove_folder(Folder *folder, FolderItem *item)
912 {
913         gchar *path;
914
915         g_return_val_if_fail(folder != NULL, -1);
916         g_return_val_if_fail(item != NULL, -1);
917         g_return_val_if_fail(item->path != NULL, -1);
918
919         path = folder_item_get_path(item);
920         if (remove_dir_recursive(path) < 0) {
921                 g_warning("can't remove directory `%s'\n", path);
922                 g_free(path);
923                 return -1;
924         }
925
926         g_free(path);
927         folder_item_remove(item);
928         return 0;
929 }
930
931 static MsgInfo *mh_parse_msg(const gchar *file, FolderItem *item)
932 {
933         MsgInfo *msginfo;
934         MsgFlags flags;
935
936         g_return_val_if_fail(item != NULL, NULL);
937         g_return_val_if_fail(file != NULL, NULL);
938
939         flags.perm_flags = MSG_NEW|MSG_UNREAD;
940         flags.tmp_flags = 0;
941
942         if (folder_has_parent_of_type(item, F_QUEUE)) {
943                 MSG_SET_TMP_FLAGS(flags, MSG_QUEUED);
944         } else if (folder_has_parent_of_type(item, F_DRAFT)) {
945                 MSG_SET_TMP_FLAGS(flags, MSG_DRAFT);
946         }
947
948         msginfo = procheader_parse_file(file, flags, FALSE, FALSE);
949         if (!msginfo) return NULL;
950
951         msginfo->msgnum = atoi(file);
952         msginfo->folder = item;
953
954         return msginfo;
955 }
956
957 static gboolean mh_remove_missing_folder_items_func(GNode *node, gpointer data)
958 {
959         FolderItem *item;
960         gchar *path;
961
962         g_return_val_if_fail(node->data != NULL, FALSE);
963
964         if (G_NODE_IS_ROOT(node))
965                 return FALSE;
966
967         item = FOLDER_ITEM(node->data);
968
969         path = folder_item_get_path(item);
970         if (!is_dir_exist(path)) {
971                 debug_print("folder '%s' not found. removing...\n", path);
972                 folder_item_remove(item);
973         }
974         g_free(path);
975
976         return FALSE;
977 }
978
979 static void mh_remove_missing_folder_items(Folder *folder)
980 {
981         g_return_if_fail(folder != NULL);
982
983         debug_print("searching missing folders...\n");
984
985         g_node_traverse(folder->node, G_POST_ORDER, G_TRAVERSE_ALL, -1,
986                         mh_remove_missing_folder_items_func, folder);
987 }
988
989 static void mh_scan_tree_recursive(FolderItem *item)
990 {
991         Folder *folder;
992 #ifdef G_OS_WIN32
993         GDir *dir;
994 #else
995         DIR *dp;
996         struct dirent *d;
997 #endif
998         const gchar *dir_name;
999         struct stat s;
1000         gchar *real_path, *entry, *utf8entry, *utf8name;
1001         gint n_msg = 0;
1002
1003         g_return_if_fail(item != NULL);
1004         g_return_if_fail(item->folder != NULL);
1005
1006         folder = item->folder;
1007
1008         real_path = item->path ? mh_filename_from_utf8(item->path) : g_strdup(".");
1009 #ifdef G_OS_WIN32
1010         dir = g_dir_open(real_path, 0, NULL);
1011         if (!dir) {
1012                 g_warning("failed to open directory: %s\n", real_path);
1013                 g_free(real_path);
1014                 return;
1015         }
1016 #else
1017         dp = opendir(real_path);
1018         if (!dp) {
1019                 FILE_OP_ERROR(real_path, "opendir");
1020                 return;
1021         }
1022 #endif
1023         g_free(real_path);
1024
1025         debug_print("scanning %s ...\n",
1026                     item->path ? item->path
1027                     : LOCAL_FOLDER(item->folder)->rootpath);
1028         if (folder->ui_func)
1029                 folder->ui_func(folder, item, folder->ui_func_data);
1030
1031 #ifdef G_OS_WIN32
1032         while ((dir_name = g_dir_read_name(dir)) != NULL) {
1033 #else
1034         while ((d = readdir(dp)) != NULL) {
1035                 dir_name = d->d_name;
1036 #endif
1037                 if (dir_name[0] == '.') continue;
1038
1039                 utf8name = mh_filename_to_utf8(dir_name);
1040                 if (item->path)
1041                         utf8entry = g_strconcat(item->path, G_DIR_SEPARATOR_S,
1042                                                 utf8name, NULL);
1043                 else
1044                         utf8entry = g_strdup(utf8name);
1045                 entry = mh_filename_from_utf8(utf8entry);
1046
1047                 if (
1048 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
1049                         d->d_type == DT_DIR ||
1050                         (d->d_type == DT_UNKNOWN &&
1051 #endif
1052                         g_stat(entry, &s) == 0 && S_ISDIR(s.st_mode)
1053 #if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
1054                         )
1055 #endif
1056                    ) {
1057                         FolderItem *new_item = NULL;
1058                         GNode *node;
1059
1060                         node = item->node;
1061                         for (node = node->children; node != NULL; node = node->next) {
1062                                 FolderItem *cur_item = FOLDER_ITEM(node->data);
1063                                 if (!strcmp2(cur_item->path, entry)) {
1064                                         new_item = cur_item;
1065                                         break;
1066                                 }
1067                         }
1068                         if (!new_item) {
1069                                 debug_print("new folder '%s' found.\n", entry);
1070                                 new_item = folder_item_new(folder, utf8name, utf8entry);
1071                                 folder_item_append(item, new_item);
1072                         }
1073
1074                         if (!item->path) {
1075                                 if (!folder->inbox &&
1076                                     !strcmp(dir_name, INBOX_DIR)) {
1077                                         new_item->stype = F_INBOX;
1078                                         folder->inbox = new_item;
1079                                 } else if (!folder->outbox &&
1080                                            !strcmp(dir_name, OUTBOX_DIR)) {
1081                                         new_item->stype = F_OUTBOX;
1082                                         folder->outbox = new_item;
1083                                 } else if (!folder->draft &&
1084                                            !strcmp(dir_name, DRAFT_DIR)) {
1085                                         new_item->stype = F_DRAFT;
1086                                         folder->draft = new_item;
1087                                 } else if (!folder->queue &&
1088                                            !strcmp(dir_name, QUEUE_DIR)) {
1089                                         new_item->stype = F_QUEUE;
1090                                         folder->queue = new_item;
1091                                 } else if (!folder->trash &&
1092                                            !strcmp(dir_name, TRASH_DIR)) {
1093                                         new_item->stype = F_TRASH;
1094                                         folder->trash = new_item;
1095                                 }
1096                         }
1097
1098                         mh_scan_tree_recursive(new_item);
1099                 } else if (to_number(dir_name) > 0) n_msg++;
1100
1101                 g_free(entry);
1102                 g_free(utf8entry);
1103                 g_free(utf8name);
1104         }
1105
1106 #ifdef G_OS_WIN32
1107         g_dir_close(dir);
1108 #else
1109         closedir(dp);
1110 #endif
1111
1112         item->mtime = time(NULL);
1113 }
1114
1115 static gboolean mh_rename_folder_func(GNode *node, gpointer data)
1116 {
1117         FolderItem *item = node->data;
1118         gchar **paths = data;
1119         const gchar *oldpath = paths[0];
1120         const gchar *newpath = paths[1];
1121         gchar *base;
1122         gchar *new_itempath;
1123         gint oldpathlen;
1124
1125         oldpathlen = strlen(oldpath);
1126         if (strncmp(oldpath, item->path, oldpathlen) != 0) {
1127                 g_warning("path doesn't match: %s, %s\n", oldpath, item->path);
1128                 return TRUE;
1129         }
1130
1131         base = item->path + oldpathlen;
1132         while (*base == G_DIR_SEPARATOR) base++;
1133         if (*base == '\0')
1134                 new_itempath = g_strdup(newpath);
1135         else
1136                 new_itempath = g_strconcat(newpath, G_DIR_SEPARATOR_S, base,
1137                                            NULL);
1138         g_free(item->path);
1139         item->path = new_itempath;
1140
1141         return FALSE;
1142 }
1143
1144 static gchar *mh_filename_from_utf8(const gchar *path)
1145 {
1146         gchar *real_path = g_filename_from_utf8(path, -1, NULL, NULL, NULL);
1147
1148         if (!real_path) {
1149                 g_warning("mh_filename_from_utf8: faild to convert character set\n");
1150                 real_path = g_strdup(path);
1151         }
1152
1153         return real_path;
1154 }
1155
1156 static gchar *mh_filename_to_utf8(const gchar *path)
1157 {
1158         gchar *utf8path = g_filename_to_utf8(path, -1, NULL, NULL, NULL);
1159         if (!utf8path) {
1160                 g_warning("mh_filename_to_utf8: faild to convert character set\n");
1161                 utf8path = g_strdup(path);
1162         }
1163
1164         return utf8path;
1165 }
1166
1167 static gint sort_cache_list_by_msgnum(gconstpointer a, gconstpointer b)
1168 {
1169         MsgInfo *msginfo_a = (MsgInfo *) a;
1170         MsgInfo *msginfo_b = (MsgInfo *) b;
1171
1172         return (msginfo_a->msgnum - msginfo_b->msgnum);
1173 }
1174
1175 static gchar *get_unseen_seq_name(void)
1176 {
1177         static gchar *seq_name = NULL;
1178         if (!seq_name) {
1179                 gchar buf[BUFFSIZE];
1180                 gchar *tmp;
1181                 gchar *profile_path = g_strconcat(
1182                         g_get_home_dir(), G_DIR_SEPARATOR_S,
1183                         ".mh_profile", NULL);
1184                 FILE *fp = g_fopen(profile_path, "r");
1185                 if (fp) {
1186                         while (fgets(buf, sizeof(buf), fp) != NULL) {
1187                                 if (!strncmp(buf, "Unseen-Sequence:", strlen("Unseen-Sequence:"))) {
1188                                         gchar *seq_tmp = buf+strlen("Unseen-Sequence:");
1189                                         while (*seq_tmp == ' ')
1190                                                 seq_tmp++;
1191                                         seq_name = g_strdup(seq_tmp);
1192                                         seq_name = strretchomp(seq_name);
1193                                         break;
1194                                 }
1195                         }
1196                         fclose(fp);
1197                 }
1198                 if (!seq_name)
1199                         seq_name = g_strdup("unseen");
1200                 tmp = g_strdup_printf("%s:", seq_name);
1201                 g_free(seq_name);
1202                 seq_name = tmp;
1203         }
1204         return seq_name;        
1205 }
1206
1207 static gint mh_get_flags(Folder *folder, FolderItem *item,
1208                            MsgInfoList *msginfo_list, GRelation *msgflags)
1209 {
1210         gchar *mh_sequences_filename;
1211         FILE *mh_sequences_file;
1212         gchar buf[BUFFSIZE];
1213         gchar *unseen_list = NULL;
1214         gchar *path;
1215         MsgInfoList *mcur = NULL;
1216 /*
1217         GTimer *timer = g_timer_new();
1218         g_timer_start(timer);
1219 */
1220         if (!item)
1221                 return 0;
1222
1223         /* don't update from .mh_sequences if the item's opened: mails may have
1224          * been marked read/unread and it's not yet written in the file. */     
1225         if (item->opened)
1226                 return 0;
1227
1228         path = folder_item_get_path(item);
1229
1230         mh_sequences_filename = g_strconcat(path, G_DIR_SEPARATOR_S,
1231                                             ".mh_sequences", NULL);
1232         g_free(path);
1233         if ((mh_sequences_file = g_fopen(mh_sequences_filename, "r+b")) != NULL) {
1234                 while (fgets(buf, sizeof(buf), mh_sequences_file) != NULL) {
1235                         if (!strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name()))) {
1236                                 unseen_list = g_strdup(buf+strlen(get_unseen_seq_name()));
1237                                 break;
1238                         }
1239                 }
1240                 fclose(mh_sequences_file);
1241         }
1242         
1243         g_free(mh_sequences_filename);
1244         
1245         if (unseen_list) {
1246                 gchar *cur = NULL;
1247                 gchar *token = NULL, *next = NULL, *boundary = NULL;
1248                 gint num = 0;
1249                 GHashTable *unseen_table = g_hash_table_new(g_direct_hash, g_direct_equal);
1250
1251                 cur = unseen_list = strretchomp(unseen_list);
1252                 debug_print("found unseen list in .mh_sequences: %s\n", unseen_list);
1253 next_token:
1254                 while (*cur && *cur == ' ')
1255                         cur++;
1256                 
1257                 if ((next = strchr(cur, ' ')) != NULL) {
1258                         token = cur;
1259                         cur = next+1;
1260                         *next = '\0';
1261                 } else {
1262                         token = cur;
1263                         cur = NULL;
1264                 }
1265                 
1266                 if ((boundary = strchr(token, '-')) != NULL) {
1267                         gchar *start, *end;
1268                         int i;
1269                         start = token;
1270                         end = boundary+1;
1271                         *boundary='\0';
1272                         for (i = atoi(start); i <= atoi(end); i++) {
1273                                 g_hash_table_insert(unseen_table, GINT_TO_POINTER(i), GINT_TO_POINTER(1));
1274                         }
1275                 } else if ((num = atoi(token)) > 0) {
1276                         g_hash_table_insert(unseen_table, GINT_TO_POINTER(num), GINT_TO_POINTER(1));
1277                 }
1278                 
1279                 if (cur)
1280                         goto next_token;
1281                 for (mcur = msginfo_list; mcur; mcur = mcur->next) {
1282                         MsgInfo *msginfo = (MsgInfo *)mcur->data;
1283                         MsgPermFlags flags = msginfo->flags.perm_flags;
1284                         if (g_hash_table_lookup(unseen_table, GINT_TO_POINTER(msginfo->msgnum))) {
1285                                 flags |= MSG_UNREAD;
1286                         } else if (!(flags & MSG_NEW)) { /* don't mark new msgs as read */
1287                                 flags &= ~(MSG_UNREAD);
1288                         }
1289                         if (flags != msginfo->flags.perm_flags)
1290                                 g_relation_insert(msgflags, msginfo, GINT_TO_POINTER(flags));
1291                 }
1292                 g_hash_table_destroy(unseen_table);
1293                 g_free(unseen_list);
1294         }
1295 /*
1296         g_timer_stop(timer);
1297         printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1298         g_timer_destroy(timer);
1299 */
1300         return 0;
1301 }
1302
1303 static void mh_write_sequences(FolderItem *item, gboolean remove_unseen)
1304 {
1305         gchar *mh_sequences_old, *mh_sequences_new;
1306         FILE *mh_sequences_old_fp, *mh_sequences_new_fp;
1307         gchar buf[BUFFSIZE];
1308         gchar *path = NULL;
1309 /*
1310         GTimer *timer = g_timer_new();
1311         g_timer_start(timer);
1312 */
1313         if (!item)
1314                 return;
1315         
1316         path = folder_item_get_path(item);
1317
1318         mh_sequences_old = g_strconcat(path, G_DIR_SEPARATOR_S,
1319                                             ".mh_sequences", NULL);
1320         mh_sequences_new = g_strconcat(path, G_DIR_SEPARATOR_S,
1321                                             ".mh_sequences.new", NULL);
1322         if ((mh_sequences_new_fp = g_fopen(mh_sequences_new, "w+b")) != NULL) {
1323                 GSList *msglist = folder_item_get_msg_list(item);
1324                 GSList *cur;
1325                 MsgInfo *info = NULL;
1326                 gint start = -1, end = -1;
1327                 gchar *sequence = g_strdup("");
1328                 msglist = g_slist_sort(msglist, sort_cache_list_by_msgnum);
1329                 cur = msglist;
1330                 
1331                 /* write the unseen sequence if we don't have to scrap it */
1332                 if (!remove_unseen) do {
1333                         info = (MsgInfo *)(cur ? cur->data:NULL);
1334                         if (info && (MSG_IS_UNREAD(info->flags) || MSG_IS_NEW(info->flags))) {
1335                                 if (start < 0)
1336                                         start = end = info->msgnum;
1337                                 else
1338                                         end = info->msgnum;
1339                         } else {
1340                                 if (start > 0 && end > 0) {
1341                                         gchar *tmp = sequence;
1342                                         if (start != end)
1343                                                 sequence = g_strdup_printf("%s %d-%d ", tmp, start, end);
1344                                         else
1345                                                 sequence = g_strdup_printf("%s %d ", tmp, start);
1346                                         g_free(tmp);
1347                                         start = end = -1;
1348                                 }
1349                         }
1350                         cur = cur ? cur->next:NULL;
1351                 } while (cur || (start > 0 && end > 0));
1352                 if (sequence && strlen(sequence)) {
1353                         fprintf(mh_sequences_new_fp, "%s%s\n", 
1354                                         get_unseen_seq_name(), sequence);
1355                         debug_print("wrote unseen sequence: '%s%s'\n", 
1356                                         get_unseen_seq_name(), sequence);
1357                 }
1358                 /* rewrite the rest of the file */
1359                 if ((mh_sequences_old_fp = g_fopen(mh_sequences_old, "r+b")) != NULL) {
1360                         while (fgets(buf, sizeof(buf), mh_sequences_old_fp) != NULL) {
1361                                 if (strncmp(buf, get_unseen_seq_name(), strlen(get_unseen_seq_name())))
1362                                         fprintf(mh_sequences_new_fp, "%s", buf);
1363                         }
1364                         fclose(mh_sequences_old_fp);
1365                 }
1366                 
1367                 fclose(mh_sequences_new_fp);
1368                 g_rename(mh_sequences_new, mh_sequences_old);
1369                 g_free(sequence);
1370                 procmsg_msg_list_free(msglist);
1371         }
1372         g_free(mh_sequences_old);
1373         g_free(mh_sequences_new);
1374         g_free(path);
1375 /*
1376         g_timer_stop(timer);
1377         printf("mh_get_flags: %f secs\n", g_timer_elapsed(timer, NULL));
1378         g_timer_destroy(timer);
1379 */
1380 }
1381
1382 static int mh_item_close(Folder *folder, FolderItem *item)
1383 {
1384         mh_write_sequences(item, FALSE);
1385         return 0;
1386 }