Fixed handling of default mailbox in the absence of other mailbox arguments.
Showing
2 changed files
with
60 additions
and
36 deletions
... | @@ -28,7 +28,24 @@ char *temp_filename; | ... | @@ -28,7 +28,24 @@ char *temp_filename; |
28 | FILE *temp_file; | 28 | FILE *temp_file; |
29 | mailbox_t mbox; | 29 | mailbox_t mbox; |
30 | 30 | ||
31 | /* Open temporary file for collecting incoming message */ | 31 | void |
32 | collect_open_default () | ||
33 | { | ||
34 | size_t nmesg; | ||
35 | |||
36 | if (mailbox_create (&mbox, default_mailbox) != 0 | ||
37 | || mailbox_open (mbox, MU_STREAM_RDWR) != 0) | ||
38 | { | ||
39 | util_error ("can't open default mailbox %s: %s", | ||
40 | default_mailbox, strerror (errno)); | ||
41 | exit (1); | ||
42 | } | ||
43 | |||
44 | /* Suck in the messages */ | ||
45 | mailbox_messages_count (mbox, &nmesg); | ||
46 | } | ||
47 | |||
48 | /* Open temporary file for collecting incoming messages */ | ||
32 | void | 49 | void |
33 | collect_open_mailbox_file () | 50 | collect_open_mailbox_file () |
34 | { | 51 | { |
... | @@ -84,6 +101,9 @@ collect_create_mailbox () | ... | @@ -84,6 +101,9 @@ collect_create_mailbox () |
84 | size_t count; | 101 | size_t count; |
85 | size_t nmesg; | 102 | size_t nmesg; |
86 | 103 | ||
104 | if (!temp_file) | ||
105 | return; | ||
106 | |||
87 | fclose (temp_file); | 107 | fclose (temp_file); |
88 | 108 | ||
89 | if (mailbox_create (&mbox, temp_filename) != 0 | 109 | if (mailbox_create (&mbox, temp_filename) != 0 |
... | @@ -111,33 +131,34 @@ collect_output () | ... | @@ -111,33 +131,34 @@ collect_output () |
111 | size_t i, count = 0; | 131 | size_t i, count = 0; |
112 | mailbox_t outbox = NULL; | 132 | mailbox_t outbox = NULL; |
113 | int saved_umask; | 133 | int saved_umask; |
114 | 134 | ||
115 | if (!output_mailbox) | 135 | if (!temp_filename) |
136 | { | ||
137 | mailbox_expunge (mbox); | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | if (!default_mailbox) | ||
116 | { | 142 | { |
117 | if (!user_name) | 143 | if (!user_name) |
118 | return 0; | 144 | return 0; |
119 | asprintf (&output_mailbox, "%s/%s", _PATH_MAILDIR, user_name); | 145 | asprintf (&default_mailbox, "%s/%s", _PATH_MAILDIR, user_name); |
120 | if (!output_mailbox) | 146 | if (!default_mailbox) |
121 | { | 147 | { |
122 | fprintf (stderr, "guimb: not enough memory\n"); | 148 | fprintf (stderr, "guimb: not enough memory\n"); |
123 | return 1; | 149 | return 1; |
124 | } | 150 | } |
125 | } | 151 | } |
126 | 152 | ||
127 | if (store_mailbox) | ||
128 | unlink (output_mailbox); | ||
129 | |||
130 | if (user_name) | 153 | if (user_name) |
131 | { | 154 | saved_umask = umask (077); |
132 | saved_umask = umask (077); | ||
133 | } | ||
134 | 155 | ||
135 | if (mailbox_create_default (&outbox, output_mailbox) != 0 | 156 | if (mailbox_create_default (&outbox, default_mailbox) != 0 |
136 | || mailbox_open (outbox, MU_STREAM_RDWR|MU_STREAM_CREAT) != 0) | 157 | || mailbox_open (outbox, MU_STREAM_RDWR|MU_STREAM_CREAT) != 0) |
137 | { | 158 | { |
138 | mailbox_destroy (&outbox); | 159 | mailbox_destroy (&outbox); |
139 | fprintf (stderr, "guimb: can't open output mailbox %s: %s\n", | 160 | fprintf (stderr, "guimb: can't open output mailbox %s: %s\n", |
140 | output_mailbox, strerror (errno)); | 161 | default_mailbox, strerror (errno)); |
141 | return 1; | 162 | return 1; |
142 | } | 163 | } |
143 | 164 | ||
... | @@ -171,6 +192,9 @@ collect_drop_mailbox () | ... | @@ -171,6 +192,9 @@ collect_drop_mailbox () |
171 | { | 192 | { |
172 | mailbox_close (mbox); | 193 | mailbox_close (mbox); |
173 | mailbox_destroy (&mbox); | 194 | mailbox_destroy (&mbox); |
174 | unlink (temp_filename); | 195 | if (temp_filename) |
175 | free (temp_filename); | 196 | { |
197 | unlink (temp_filename); | ||
198 | free (temp_filename); | ||
199 | } | ||
176 | } | 200 | } | ... | ... |
... | @@ -35,8 +35,7 @@ char *program_file; | ... | @@ -35,8 +35,7 @@ char *program_file; |
35 | char *program_expr; | 35 | char *program_expr; |
36 | int debug_guile; | 36 | int debug_guile; |
37 | char *user_name; | 37 | char *user_name; |
38 | char *output_mailbox; | 38 | char *default_mailbox; |
39 | int store_mailbox; | ||
40 | static void usage (void); | 39 | static void usage (void); |
41 | 40 | ||
42 | static int g_size; | 41 | static int g_size; |
... | @@ -87,8 +86,7 @@ main (int argc, char *argv[]) | ... | @@ -87,8 +86,7 @@ main (int argc, char *argv[]) |
87 | usage (); | 86 | usage (); |
88 | exit (0); | 87 | exit (0); |
89 | case 'm': | 88 | case 'm': |
90 | output_mailbox = optarg; | 89 | default_mailbox = optarg; |
91 | store_mailbox = 1; | ||
92 | break; | 90 | break; |
93 | case 'u': | 91 | case 'u': |
94 | user_name = optarg; | 92 | user_name = optarg; |
... | @@ -133,23 +131,26 @@ main (int argc, char *argv[]) | ... | @@ -133,23 +131,26 @@ main (int argc, char *argv[]) |
133 | list_append (lst, smtp_record); | 131 | list_append (lst, smtp_record); |
134 | } | 132 | } |
135 | 133 | ||
136 | collect_open_mailbox_file (); | 134 | if (default_mailbox && !argv[optind]) |
137 | if (store_mailbox && !argv[optind]) | ||
138 | { | 135 | { |
139 | append_arg (output_mailbox); | 136 | append_arg (default_mailbox); |
140 | collect_append_file (output_mailbox); | 137 | collect_open_default (); |
141 | } | 138 | } |
142 | 139 | else | |
143 | if (argv[optind]) | ||
144 | { | 140 | { |
145 | for (; argv[optind]; optind++) | 141 | collect_open_mailbox_file (); |
142 | |||
143 | if (argv[optind]) | ||
146 | { | 144 | { |
147 | append_arg (argv[optind]); | 145 | for (; argv[optind]; optind++) |
148 | collect_append_file (argv[optind]); | 146 | { |
147 | append_arg (argv[optind]); | ||
148 | collect_append_file (argv[optind]); | ||
149 | } | ||
149 | } | 150 | } |
151 | else | ||
152 | collect_append_file ("-"); | ||
150 | } | 153 | } |
151 | else if (!store_mailbox) | ||
152 | collect_append_file ("-"); | ||
153 | 154 | ||
154 | append_arg (NULL); | 155 | append_arg (NULL); |
155 | g_argc--; | 156 | g_argc--; |
... | @@ -175,12 +176,11 @@ static char usage_str[] = | ... | @@ -175,12 +176,11 @@ static char usage_str[] = |
175 | "Any arguments between -{ and -} are passed to the Scheme program verbatim.\n" | 176 | "Any arguments between -{ and -} are passed to the Scheme program verbatim.\n" |
176 | "When both --file and --expression are specified, file is evaluated first.\n" | 177 | "When both --file and --expression are specified, file is evaluated first.\n" |
177 | "If no mailboxes are specified, the standard input is read.\n\n" | 178 | "If no mailboxes are specified, the standard input is read.\n\n" |
178 | "The semantics of the default mailbox differs depending on whether\n" | 179 | "The semantics of the default mailbox depends on whether more mailbox\n" |
179 | "more mailbox arguments are specified in the command line. If they\n" | 180 | "arguments are specified in the command line. If they are, any messages\n" |
180 | "are, any messages that are not deleted after executing the script\n" | 181 | "that are not deleted after executing the script are appended to the default\n" |
181 | "are appended to the default mailbox. Otherwise its contents is read,\n" | 182 | "mailbox. Otherwise its contents is read, processed and *replaced* by\n" |
182 | "processed and *replaced* by messages that remain undeleted after\n" | 183 | "messages that remain undeleted after executing the script.\n"; |
183 | "executing the script.\n"; | ||
184 | 184 | ||
185 | void | 185 | void |
186 | usage () | 186 | usage () | ... | ... |
-
Please register or sign in to post a comment