Commit 7b6fcdc2 7b6fcdc28799e2c6f61873c54b93239d79fd5df2 by Sergey Poznyakoff

Use util_msgset_iterate(). Moved get_content_type() and get_hdr_value() to util.c

1 parent 2b656925
Showing 1 changed file with 146 additions and 133 deletions
...@@ -23,64 +23,78 @@ ...@@ -23,64 +23,78 @@
23 mime/attachements etc is less confusing. 23 mime/attachements etc is less confusing.
24 */ 24 */
25 25
26 struct decode_closure
27 {
28 int select_hdr;
29 };
30
26 static int print_stream __P ((stream_t, FILE *)); 31 static int print_stream __P ((stream_t, FILE *));
27 static int display_message __P ((message_t, FILE *, int)); 32 static int display_message __P ((message_t, msgset_t *msgset,
33 struct decode_closure *closure));
34 static int display_message0 __P ((FILE *, message_t, const msgset_t *, int));
28 static int mailcap_lookup __P ((const char *)); 35 static int mailcap_lookup __P ((const char *));
29 static int get_hdr_value __P ((header_t hdr, const char *name, char **value));
30 static int get_content_encoding __P ((header_t hdr, char **value)); 36 static int get_content_encoding __P ((header_t hdr, char **value));
31 static int get_content_type __P ((header_t hdr, char **value));
32 37
33 int 38 int
34 mail_decode (int argc, char **argv) 39 mail_decode (int argc, char **argv)
35 { 40 {
36 if (argc > 1) 41 msgset_t *msgset;
37 return util_msglist_command (mail_print, argc, argv, 1); 42 struct decode_closure decode_closure;
38 else 43
39 { 44 if (msgset_parse (argc, argv, &msgset))
40 message_t mesg; 45 return 1;
41 int lines = 0;
42 FILE *out = ofile;
43 attribute_t attr;
44 46
45 if (mailbox_get_message (mbox, cursor, &mesg) != 0) 47 decode_closure.select_hdr = islower (argv[0][0]);
46 return 1;
47 48
48 if (util_isdeleted (cursor)) 49 util_msgset_iterate (msgset, display_message, &decode_closure);
49 return 1;
50 50
51 message_lines (mesg, &lines); 51 msgset_free (msgset);
52 return 0;
53 }
52 54
53 if ((util_find_env("crt"))->set && lines > util_getlines ()) 55 int
54 out = popen (getenv("PAGER"), "w"); 56 display_message (message_t mesg, msgset_t *msgset,
57 struct decode_closure *closure)
58 {
59 FILE *out;
60 size_t lines = 0;
61
62 if (util_isdeleted (msgset->msg_part[0]))
63 return 1;
64
65 message_lines (mesg, &lines);
66 if ((util_find_env("crt"))->set && lines > util_getlines ())
67 out = popen (getenv("PAGER"), "w");
68 else
69 out = ofile;
70
71 display_message0 (out, mesg, msgset, closure->select_hdr);
55 72
56 display_message (mesg, out, islower (argv[0][0])); 73 if (out != ofile)
74 pclose (out);
57 75
76 /* Mark enclosing message as read */
77 if (mailbox_get_message (mbox, msgset->msg_part[0], &mesg) == 0)
78 {
79 attribute_t attr;
58 message_get_attribute (mesg, &attr); 80 message_get_attribute (mesg, &attr);
59 attribute_set_read (attr); 81 attribute_set_read (attr);
60
61 if (out != ofile)
62 pclose (out);
63
64 return 0;
65 } 82 }
66 return 1; 83 return 0;
67 } 84 }
68 85
69 static int 86 static void
70 display_message (message_t mesg, FILE *out, int select_hdr) 87 display_headers (FILE *out, message_t mesg, const msgset_t *msgset,
88 int select_hdr)
71 { 89 {
72 size_t nparts = 0;
73 header_t hdr = NULL; 90 header_t hdr = NULL;
74 size_t j;
75
76 /* Print the selected headers only. */ 91 /* Print the selected headers only. */
77 if (select_hdr) 92 if (select_hdr)
78 { 93 {
79 size_t num = 0; 94 size_t num = 0;
80 size_t i = 0; 95 size_t i = 0;
81 char buffer[512]; 96 char buffer[512];
82 97
83 hdr = NULL;
84 message_get_header (mesg, &hdr); 98 message_get_header (mesg, &hdr);
85 header_get_field_count (hdr, &num); 99 header_get_field_count (hdr, &num);
86 for (i = 1; i <= num; i++) 100 for (i = 1; i <= num; i++)
...@@ -103,85 +117,114 @@ display_message (message_t mesg, FILE *out, int select_hdr) ...@@ -103,85 +117,114 @@ display_message (message_t mesg, FILE *out, int select_hdr)
103 && header_get_stream (hdr, &stream) == 0) 117 && header_get_stream (hdr, &stream) == 0)
104 print_stream (stream, out); 118 print_stream (stream, out);
105 } 119 }
120 }
106 121
107 message_get_num_parts (mesg, &nparts); 122 void
108 for (j = 1; j <= nparts; j++) 123 display_part_header (FILE *out, const msgset_t *msgset,
109 { 124 char *type, char *encoding)
110 message_t message = NULL; 125 {
111 if (message_get_part (mesg, j, &message) == 0) 126 int size = util_screen_columns () - 3;
112 { 127 int i;
113 char *type = NULL; 128
114 char *encoding = NULL; 129 fputc ('+', out);
115 int ismime = 0; 130 for (i = 0; i <= size; i++)
131 fputc ('-', out);
132 fputc ('+', out);
133 fputc ('\n', out);
134 fprintf (out, "| Message=%d", msgset->msg_part[0]);
135 for (i = 1; i < msgset->npart; i++)
136 fprintf (out, "[%d", msgset->msg_part[i]);
137 for (i = 1; i < msgset->npart; i++)
138 fprintf (out, "]");
139 fprintf (out, "\n");
140
141 fprintf (out, "| Type=%s\n", type);
142 fprintf (out, "| encoding=%s\n", encoding);
143 fputc ('+', out);
144 for (i = 0; i <= size; i++)
145 fputc ('-', out);
146 fputc ('+', out);
147 fputc ('\n', out);
148 }
116 149
117 message_is_multipart (message, &ismime); 150 static int
118 if (ismime) 151 display_message0 (FILE *out, message_t mesg, const msgset_t *msgset,
119 { 152 int select_hdr)
120 display_message (message, out, 0); 153 {
121 continue; 154 size_t nparts = 0;
122 } 155 header_t hdr = NULL;
156 char *type;
157 char *encoding;
158 int ismime = 0;
159
160 message_get_header (mesg, &hdr);
161 util_get_content_type (hdr, &type);
162 get_content_encoding (hdr, &encoding);
163
164 message_is_multipart (mesg, &ismime);
165 if (ismime)
166 {
167 int j;
123 168
124 message_get_header (message, &hdr); 169 message_get_num_parts (mesg, &nparts);
125 170
126 get_content_type (hdr, &type); 171 for (j = 1; j <= nparts; j++)
127 get_content_encoding (hdr, &encoding); 172 {
173 message_t message = NULL;
128 174
129 if (strncasecmp (type, "message/rfc822", strlen (type)) == 0) 175 if (message_get_part (mesg, j, &message) == 0)
130 {
131 message_t submsg = NULL;
132 if (message_unencapsulate (message, &submsg, NULL) == 0)
133 display_message (submsg, out, select_hdr);
134 }
135 else if (mailcap_lookup (type))
136 { 176 {
137 /* FIXME: lookup .mailcap and do the appropriate action when 177 msgset_t *set = msgset_expand (msgset_dup (msgset),
138 an match engry is find. */ 178 msgset_make_1 (j));
139 /* Do something, spawn a process etc .... */ 179 display_message0 (out, message, set, 0);
140 } 180 msgset_free (set);
141 else if (strncasecmp (type, "text/plain", strlen ("text/plain")) == 0
142 || strncasecmp (type, "text/html", strlen ("text/html")) == 0)
143 {
144 body_t body = NULL;
145 stream_t b_stream = NULL;
146
147 if (message_get_body (message, &body) == 0 &&
148 body_get_stream (body, &b_stream) == 0)
149 {
150 stream_t d_stream = NULL;
151 stream_t stream = NULL;
152
153 /* Can we decode. */
154 if (filter_create(&d_stream, b_stream, encoding,
155 MU_FILTER_DECODE, MU_STREAM_READ) == 0)
156 stream = d_stream;
157 else
158 stream = b_stream;
159
160 print_stream (stream, out);
161 if (d_stream)
162 stream_destroy (&d_stream, NULL);
163 }
164 } 181 }
182 }
183 }
184 else if (strncasecmp (type, "message/rfc822", strlen (type)) == 0)
185 {
186 message_t submsg = NULL;
187
188 if (message_unencapsulate (mesg, &submsg, NULL) == 0)
189 display_message0 (out, submsg, msgset, select_hdr);
190 }
191 else if (mailcap_lookup (type))
192 {
193 /* FIXME: lookup .mailcap and do the appropriate action when
194 an match engry is find. */
195 /* Do something, spawn a process etc .... */
196 }
197 else /*if (strncasecmp (type, "text/plain", strlen ("text/plain")) == 0
198 || strncasecmp (type, "text/html", strlen ("text/html")) == 0)*/
199 {
200 body_t body = NULL;
201 stream_t b_stream = NULL;
202
203 display_part_header (out, msgset, type, encoding);
204 display_headers (out, mesg, msgset, select_hdr);
205
206 if (message_get_body (mesg, &body) == 0 &&
207 body_get_stream (body, &b_stream) == 0)
208 {
209 stream_t d_stream = NULL;
210 stream_t stream = NULL;
211
212 /* Can we decode. */
213 if (filter_create(&d_stream, b_stream, encoding,
214 MU_FILTER_DECODE, MU_STREAM_READ) == 0)
215 stream = d_stream;
165 else 216 else
166 { 217 stream = b_stream;
167 int size = util_screen_columns () - 3; 218
168 int i; 219 print_stream (stream, out);
169 fputc ('+', out); 220 if (d_stream)
170 for (i = 0; i <= size; i++) 221 stream_destroy (&d_stream, NULL);
171 fputc ('-', out);
172 fputc ('+', out); fputc ('\n', out);
173 fprintf (out, "| Message=%d[%d]\n", cursor, j);
174 fprintf (out, "| Type=%s\n", type);
175 fprintf (out, "| encoding=%s\n", encoding);
176 fputc ('+', out);
177 for (i = 0; i <= size; i++)
178 fputc ('-', out);
179 fputc ('+', out); fputc ('\n', out);
180 }
181 free (type);
182 free (encoding);
183 } 222 }
184 } 223 }
224
225 free (type);
226 free (encoding);
227
185 return 0; 228 return 0;
186 } 229 }
187 230
...@@ -208,25 +251,10 @@ print_stream (stream_t stream, FILE *out) ...@@ -208,25 +251,10 @@ print_stream (stream_t stream, FILE *out)
208 } 251 }
209 252
210 static int 253 static int
211 get_content_type (header_t hdr, char **value)
212 {
213 char *type = NULL;
214 get_hdr_value (hdr, MU_HEADER_CONTENT_TYPE, &type);
215 if (type == NULL || *type == '\0')
216 {
217 if (type)
218 free (type);
219 type = strdup ("text/plain"); /* Default. */
220 }
221 *value = type;
222 return 0;
223 }
224
225 static int
226 get_content_encoding (header_t hdr, char **value) 254 get_content_encoding (header_t hdr, char **value)
227 { 255 {
228 char *encoding = NULL; 256 char *encoding = NULL;
229 get_hdr_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, &encoding); 257 util_get_hdr_value (hdr, MU_HEADER_CONTENT_TRANSFER_ENCODING, &encoding);
230 if (encoding == NULL || *encoding == '\0') 258 if (encoding == NULL || *encoding == '\0')
231 { 259 {
232 if (encoding) 260 if (encoding)
...@@ -237,21 +265,6 @@ get_content_encoding (header_t hdr, char **value) ...@@ -237,21 +265,6 @@ get_content_encoding (header_t hdr, char **value)
237 return 0; 265 return 0;
238 } 266 }
239 267
240 static int
241 get_hdr_value (header_t hdr, const char *name, char **value)
242 {
243 int status = header_aget_value (hdr, name, value);
244 if (status == 0)
245 {
246 /* Remove the newlines. */
247 char *nl;
248 while ((nl = strchr (*value, '\n')) != NULL)
249 {
250 *nl = ' ';
251 }
252 }
253 return status;
254 }
255 268
256 static int 269 static int
257 mailcap_lookup (const char *type) 270 mailcap_lookup (const char *type)
......