Commit 7b6fcdc2 7b6fcdc28799e2c6f61873c54b93239d79fd5df2 by Sergey Poznyakoff

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

1 parent 2b656925
...@@ -23,56 +23,71 @@ ...@@ -23,56 +23,71 @@
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
39 {
40 message_t mesg;
41 int lines = 0;
42 FILE *out = ofile;
43 attribute_t attr;
44 43
45 if (mailbox_get_message (mbox, cursor, &mesg) != 0) 44 if (msgset_parse (argc, argv, &msgset))
46 return 1; 45 return 1;
47 46
48 if (util_isdeleted (cursor)) 47 decode_closure.select_hdr = islower (argv[0][0]);
48
49 util_msgset_iterate (msgset, display_message, &decode_closure);
50
51 msgset_free (msgset);
52 return 0;
53 }
54
55 int
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]))
49 return 1; 63 return 1;
50 64
51 message_lines (mesg, &lines); 65 message_lines (mesg, &lines);
52
53 if ((util_find_env("crt"))->set && lines > util_getlines ()) 66 if ((util_find_env("crt"))->set && lines > util_getlines ())
54 out = popen (getenv("PAGER"), "w"); 67 out = popen (getenv("PAGER"), "w");
68 else
69 out = ofile;
55 70
56 display_message (mesg, out, islower (argv[0][0])); 71 display_message0 (out, mesg, msgset, closure->select_hdr);
57
58 message_get_attribute (mesg, &attr);
59 attribute_set_read (attr);
60 72
61 if (out != ofile) 73 if (out != ofile)
62 pclose (out); 74 pclose (out);
63 75
64 return 0; 76 /* Mark enclosing message as read */
77 if (mailbox_get_message (mbox, msgset->msg_part[0], &mesg) == 0)
78 {
79 attribute_t attr;
80 message_get_attribute (mesg, &attr);
81 attribute_set_read (attr);
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 {
...@@ -80,7 +95,6 @@ display_message (message_t mesg, FILE *out, int select_hdr) ...@@ -80,7 +95,6 @@ display_message (message_t mesg, FILE *out, int select_hdr)
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,34 +117,76 @@ display_message (message_t mesg, FILE *out, int select_hdr) ...@@ -103,34 +117,76 @@ 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);
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 }
149
150 static int
151 display_message0 (FILE *out, message_t mesg, const msgset_t *msgset,
152 int select_hdr)
153 {
154 size_t nparts = 0;
155 header_t hdr = NULL;
156 char *type;
157 char *encoding;
115 int ismime = 0; 158 int ismime = 0;
116 159
117 message_is_multipart (message, &ismime); 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);
118 if (ismime) 165 if (ismime)
119 { 166 {
120 display_message (message, out, 0); 167 int j;
121 continue;
122 }
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)
176 {
177 msgset_t *set = msgset_expand (msgset_dup (msgset),
178 msgset_make_1 (j));
179 display_message0 (out, message, set, 0);
180 msgset_free (set);
181 }
182 }
183 }
184 else if (strncasecmp (type, "message/rfc822", strlen (type)) == 0)
130 { 185 {
131 message_t submsg = NULL; 186 message_t submsg = NULL;
132 if (message_unencapsulate (message, &submsg, NULL) == 0) 187
133 display_message (submsg, out, select_hdr); 188 if (message_unencapsulate (mesg, &submsg, NULL) == 0)
189 display_message0 (out, submsg, msgset, select_hdr);
134 } 190 }
135 else if (mailcap_lookup (type)) 191 else if (mailcap_lookup (type))
136 { 192 {
...@@ -138,13 +194,16 @@ display_message (message_t mesg, FILE *out, int select_hdr) ...@@ -138,13 +194,16 @@ display_message (message_t mesg, FILE *out, int select_hdr)
138 an match engry is find. */ 194 an match engry is find. */
139 /* Do something, spawn a process etc .... */ 195 /* Do something, spawn a process etc .... */
140 } 196 }
141 else if (strncasecmp (type, "text/plain", strlen ("text/plain")) == 0 197 else /*if (strncasecmp (type, "text/plain", strlen ("text/plain")) == 0
142 || strncasecmp (type, "text/html", strlen ("text/html")) == 0) 198 || strncasecmp (type, "text/html", strlen ("text/html")) == 0)*/
143 { 199 {
144 body_t body = NULL; 200 body_t body = NULL;
145 stream_t b_stream = NULL; 201 stream_t b_stream = NULL;
146 202
147 if (message_get_body (message, &body) == 0 && 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 &&
148 body_get_stream (body, &b_stream) == 0) 207 body_get_stream (body, &b_stream) == 0)
149 { 208 {
150 stream_t d_stream = NULL; 209 stream_t d_stream = NULL;
...@@ -162,26 +221,10 @@ display_message (message_t mesg, FILE *out, int select_hdr) ...@@ -162,26 +221,10 @@ display_message (message_t mesg, FILE *out, int select_hdr)
162 stream_destroy (&d_stream, NULL); 221 stream_destroy (&d_stream, NULL);
163 } 222 }
164 } 223 }
165 else 224
166 {
167 int size = util_screen_columns () - 3;
168 int i;
169 fputc ('+', out);
170 for (i = 0; i <= size; i++)
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); 225 free (type);
182 free (encoding); 226 free (encoding);
183 } 227
184 }
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)
......