Changed handling of MH profiles.
(mh_open_folder, mh_get_dir, mh_expand_name, mh_iterate): New functions
Showing
1 changed file
with
120 additions
and
115 deletions
... | @@ -25,9 +25,9 @@ | ... | @@ -25,9 +25,9 @@ |
25 | 25 | ||
26 | char *current_folder = NULL; | 26 | char *current_folder = NULL; |
27 | size_t current_message = 0; | 27 | size_t current_message = 0; |
28 | char *ctx_name; | 28 | mh_context_t *context; |
29 | header_t ctx_header; | 29 | mh_context_t *profile; |
30 | header_t profile_header; | 30 | mh_context_t *sequences; |
31 | 31 | ||
32 | char mh_list_format[] = | 32 | char mh_list_format[] = |
33 | "%4(msg)%<(cur)+%| %>%<{replied}-%?{encrypted}E%| %>" | 33 | "%4(msg)%<(cur)+%| %>%<{replied}-%?{encrypted}E%| %>" |
... | @@ -62,56 +62,38 @@ void | ... | @@ -62,56 +62,38 @@ void |
62 | mh_init2 () | 62 | mh_init2 () |
63 | { | 63 | { |
64 | char *mh_sequences_name; | 64 | char *mh_sequences_name; |
65 | char *seq_name, *ctx_name; | ||
66 | char *p; | ||
67 | |||
68 | mu_path_folder_dir = mh_get_dir (); | ||
69 | p = getenv ("CONTEXT"); | ||
70 | if (!p) | ||
71 | p = "context"; | ||
72 | ctx_name = mh_expand_name (p, 0); | ||
73 | context = mh_context_create (ctx_name, 1); | ||
74 | mh_context_read (context); | ||
65 | 75 | ||
66 | /* Set MH context */ | ||
67 | if (current_folder) | 76 | if (current_folder) |
68 | current_folder = mu_tilde_expansion (current_folder, "/", NULL); | 77 | current_folder = mu_tilde_expansion (current_folder, "/", NULL); |
69 | else | 78 | else |
70 | current_folder = mh_profile_value ("Current-Folder", | 79 | current_folder = mh_context_get_value (context, "Current-Folder", |
71 | mh_profile_value ("Inbox", "inbox")); | 80 | mh_profile_value ("Inbox", |
72 | if (strchr (current_folder, '/') == NULL) | 81 | "inbox")); |
73 | { | ||
74 | char *mhdir = mh_profile_value ("Path", "Mail"); | ||
75 | char *p = mu_get_homedir (); | ||
76 | |||
77 | if (mhdir[0] == '/') | ||
78 | asprintf (¤t_folder, "mh:%s/%s", mhdir, current_folder); | ||
79 | else | ||
80 | asprintf (¤t_folder, "mh:%s/%s/%s", p, mhdir, current_folder); | ||
81 | if (!current_folder) | ||
82 | { | ||
83 | mh_error ("low memory"); | ||
84 | exit (1); | ||
85 | } | ||
86 | free (p); | ||
87 | } | ||
88 | else if (strchr (current_folder, ':') == NULL) | ||
89 | { | ||
90 | char *p; | ||
91 | p = xmalloc (strlen (current_folder) + 4); | ||
92 | strcat (strcpy (p, "mh:"), current_folder); | ||
93 | current_folder = p; | ||
94 | } | ||
95 | 82 | ||
96 | mh_sequences_name = mh_profile_value ("mh-sequences", MH_SEQUENCES_FILE); | 83 | mh_sequences_name = mh_profile_value ("mh-sequences", MH_SEQUENCES_FILE); |
97 | asprintf (&ctx_name, "%s/%s", current_folder+3, mh_sequences_name); | 84 | asprintf (&seq_name, "%s/%s", current_folder+3, mh_sequences_name); |
98 | if (mh_read_context_file (ctx_name, &ctx_header) == 0) | 85 | sequences = mh_context_create (seq_name, 1); |
86 | if (mh_context_read (sequences) == 0) | ||
99 | { | 87 | { |
100 | char buf[64]; | 88 | char *p = mh_context_get_value (sequences, "cur", "0"); |
101 | size_t n; | 89 | current_message = strtoul (p, NULL, 10); |
102 | |||
103 | if (!header_get_value (ctx_header, "cur", buf, sizeof buf, &n)) | ||
104 | current_message = strtoul (buf, NULL, 10); | ||
105 | } | 90 | } |
106 | } | 91 | } |
107 | 92 | ||
108 | char * | 93 | char * |
109 | mh_profile_value (char *name, char *defval) | 94 | mh_profile_value (char *name, char *defval) |
110 | { | 95 | { |
111 | char *p; | 96 | return mh_context_get_value (profile, name, defval); |
112 | if (header_aget_value (profile_header, name, &p)) | ||
113 | p = defval; | ||
114 | return p; | ||
115 | } | 97 | } |
116 | 98 | ||
117 | void | 99 | void |
... | @@ -130,7 +112,8 @@ mh_read_profile () | ... | @@ -130,7 +112,8 @@ mh_read_profile () |
130 | asprintf (&p, "%s/%s", home, MH_USER_PROFILE); | 112 | asprintf (&p, "%s/%s", home, MH_USER_PROFILE); |
131 | free (home); | 113 | free (home); |
132 | } | 114 | } |
133 | mh_read_context_file (p, &profile_header); | 115 | profile = mh_context_create (p, 1); |
116 | mh_context_read (profile); | ||
134 | } | 117 | } |
135 | 118 | ||
136 | void | 119 | void |
... | @@ -138,76 +121,8 @@ mh_save_context () | ... | @@ -138,76 +121,8 @@ mh_save_context () |
138 | { | 121 | { |
139 | char buf[64]; | 122 | char buf[64]; |
140 | snprintf (buf, sizeof buf, "%d", current_message); | 123 | snprintf (buf, sizeof buf, "%d", current_message); |
141 | if (!ctx_header) | 124 | mh_context_set_value (sequences, "cur", buf); |
142 | { | 125 | mh_context_write (sequences); |
143 | if (header_create (&ctx_header, NULL, 0, NULL)) | ||
144 | { | ||
145 | mh_error ("Can't create context: %s", strerror (errno)); | ||
146 | return; | ||
147 | } | ||
148 | } | ||
149 | header_set_value (ctx_header, "cur", buf, 1); | ||
150 | mh_write_context_file (ctx_name, ctx_header); | ||
151 | } | ||
152 | |||
153 | int | ||
154 | mh_read_context_file (char *path, header_t *header) | ||
155 | { | ||
156 | int status; | ||
157 | char *blurb; | ||
158 | struct stat st; | ||
159 | FILE *fp; | ||
160 | |||
161 | if (stat (path, &st)) | ||
162 | return errno; | ||
163 | |||
164 | blurb = malloc (st.st_size); | ||
165 | if (!blurb) | ||
166 | return ENOMEM; | ||
167 | |||
168 | fp = fopen (path, "r"); | ||
169 | if (!fp) | ||
170 | { | ||
171 | free (blurb); | ||
172 | return errno; | ||
173 | } | ||
174 | |||
175 | fread (blurb, st.st_size, 1, fp); | ||
176 | fclose (fp); | ||
177 | |||
178 | if (status = header_create (header, blurb, st.st_size, NULL)) | ||
179 | free (blurb); | ||
180 | |||
181 | return status; | ||
182 | } | ||
183 | |||
184 | int | ||
185 | mh_write_context_file (char *path, header_t header) | ||
186 | { | ||
187 | stream_t stream; | ||
188 | char buffer[512]; | ||
189 | size_t off = 0, n; | ||
190 | FILE *fp; | ||
191 | |||
192 | fp = fopen (path, "w"); | ||
193 | if (!fp) | ||
194 | { | ||
195 | mh_error ("can't write context file %s: %s", path, strerror (errno)); | ||
196 | return 1; | ||
197 | } | ||
198 | |||
199 | header_get_stream (header, &stream); | ||
200 | |||
201 | while (stream_read (stream, buffer, sizeof buffer - 1, off, &n) == 0 | ||
202 | && n != 0) | ||
203 | { | ||
204 | buffer[n] = '\0'; | ||
205 | fprintf (fp, "%s", buffer); | ||
206 | off += n; | ||
207 | } | ||
208 | |||
209 | fclose (fp); | ||
210 | return 0; | ||
211 | } | 126 | } |
212 | 127 | ||
213 | int | 128 | int |
... | @@ -431,23 +346,113 @@ mh_message_number (message_t msg, size_t *pnum) | ... | @@ -431,23 +346,113 @@ mh_message_number (message_t msg, size_t *pnum) |
431 | } | 346 | } |
432 | 347 | ||
433 | mailbox_t | 348 | mailbox_t |
434 | mh_open_folder () | 349 | mh_open_folder (const char *folder) |
435 | { | 350 | { |
436 | mailbox_t mbox = NULL; | 351 | mailbox_t mbox = NULL; |
352 | char *name; | ||
437 | 353 | ||
438 | if (mailbox_create_default (&mbox, current_folder)) | 354 | name = mh_expand_name (folder, 1); |
355 | if (mailbox_create_default (&mbox, name)) | ||
439 | { | 356 | { |
440 | mh_error ("Can't create mailbox %s: %s", | 357 | mh_error ("Can't create mailbox %s: %s", |
441 | current_folder, strerror (errno)); | 358 | name, strerror (errno)); |
442 | exit (1); | 359 | exit (1); |
443 | } | 360 | } |
444 | 361 | ||
445 | if (mailbox_open (mbox, MU_STREAM_READ)) | 362 | if (mailbox_open (mbox, MU_STREAM_READ)) |
446 | { | 363 | { |
447 | mh_error ("Can't open mailbox %s: %s", current_folder, strerror (errno)); | 364 | mh_error ("Can't open mailbox %s: %s", name, strerror (errno)); |
448 | exit (1); | 365 | exit (1); |
449 | } | 366 | } |
450 | 367 | ||
368 | free (name); | ||
369 | |||
451 | return mbox; | 370 | return mbox; |
452 | } | 371 | } |
453 | 372 | ||
373 | char * | ||
374 | mh_get_dir () | ||
375 | { | ||
376 | char *mhdir = mh_profile_value ("Path", "Mail"); | ||
377 | if (mhdir[0] != '/') | ||
378 | { | ||
379 | char *p = mu_get_homedir (); | ||
380 | asprintf (&mhdir, "%s/%s", p, mhdir); | ||
381 | free (p); | ||
382 | } | ||
383 | else | ||
384 | mhdir = strdup (mhdir); | ||
385 | return mhdir; | ||
386 | } | ||
387 | |||
388 | const char * | ||
389 | mh_expand_name (const char *name, int is_folder) | ||
390 | { | ||
391 | char *tmp = NULL; | ||
392 | char *p = NULL; | ||
393 | |||
394 | tmp = mu_tilde_expansion (name, "/", NULL); | ||
395 | if (tmp[0] == '+') | ||
396 | name = tmp + 1; | ||
397 | else | ||
398 | name = tmp; | ||
399 | |||
400 | if (is_folder) | ||
401 | asprintf (&p, "mh:%s/%s", mu_path_folder_dir, name); | ||
402 | else | ||
403 | asprintf (&p, "%s/%s", mu_path_folder_dir, name); | ||
404 | free (tmp); | ||
405 | return p; | ||
406 | } | ||
407 | |||
408 | int | ||
409 | mh_iterate (mailbox_t mbox, mh_msgset_t *msgset, | ||
410 | mh_iterator_fp itr, void *data) | ||
411 | { | ||
412 | int rc; | ||
413 | int last = 0; | ||
414 | size_t i, total = 0; | ||
415 | |||
416 | if (rc = mailbox_messages_count (mbox, &total)) | ||
417 | { | ||
418 | mh_error ("can't count messages in %s: %s", | ||
419 | current_folder, mu_errstring (rc)); | ||
420 | exit (1); | ||
421 | } | ||
422 | |||
423 | for (i = 1; i <= total; i++) | ||
424 | { | ||
425 | message_t msg; | ||
426 | size_t num; | ||
427 | int rc; | ||
428 | |||
429 | if ((rc = mailbox_get_message (mbox, i, &msg)) != 0) | ||
430 | { | ||
431 | mh_error ("can't get message %d: %s", i, mu_errstring (rc)); | ||
432 | return 1; | ||
433 | } | ||
434 | |||
435 | if ((rc = mh_message_number (msg, &num)) != 0) | ||
436 | { | ||
437 | mh_error ("can't get sequence number for message %d: %s", | ||
438 | i, mu_errstring (rc)); | ||
439 | return 1; | ||
440 | } | ||
441 | |||
442 | rc = mh_msgset_member (msgset, num); | ||
443 | if (rc == last + 1) | ||
444 | { | ||
445 | itr (mbox, msg, num, data); | ||
446 | last = rc; | ||
447 | } | ||
448 | else if (rc && last && last < msgset->count) | ||
449 | break; | ||
450 | } | ||
451 | if (last < msgset->count) | ||
452 | { | ||
453 | mh_error ("message %d does not exist", msgset->list[last]); | ||
454 | exit (1); | ||
455 | } | ||
456 | |||
457 | return 0; | ||
458 | } | ... | ... |
-
Please register or sign in to post a comment