summaryrefslogtreecommitdiffabout
path: root/kmicromail/libetpan/tests/frm-common.c
Unidiff
Diffstat (limited to 'kmicromail/libetpan/tests/frm-common.c') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libetpan/tests/frm-common.c158
1 files changed, 158 insertions, 0 deletions
diff --git a/kmicromail/libetpan/tests/frm-common.c b/kmicromail/libetpan/tests/frm-common.c
new file mode 100644
index 0000000..eeeadf0
--- a/dev/null
+++ b/kmicromail/libetpan/tests/frm-common.c
@@ -0,0 +1,158 @@
1#include "frm-common.h"
2
3#include <string.h>
4#include <stdlib.h>
5
6#define DEST_CHARSET "iso-8859-1"
7
8/* get part of the from field to display */
9
10void get_from_value(struct mailimf_single_fields * fields,
11 char ** from, int * is_addr)
12{
13 struct mailimf_mailbox * mb;
14
15 if (fields->fld_from == NULL) {
16 * from = NULL;
17 * is_addr = 0;
18 return;
19 }
20
21 if (clist_isempty(fields->fld_from->frm_mb_list->mb_list)) {
22 * from = NULL;
23 * is_addr = 0;
24 return;
25 }
26
27 mb = clist_begin(fields->fld_from->frm_mb_list->mb_list)->data;
28
29 if (mb->mb_display_name != NULL) {
30 * from = mb->mb_display_name;
31 * is_addr = 0;
32 }
33 else {
34 * from = mb->mb_addr_spec;
35 * is_addr = 1;
36 }
37}
38
39/* remove all CR and LF of a string and replace them with SP */
40
41void strip_crlf(char * str)
42{
43 char * p;
44
45 for(p = str ; * p != '\0' ; p ++) {
46 if ((* p == '\n') || (* p == '\r'))
47 * p = ' ';
48 }
49}
50
51#define MAX_OUTPUT 81
52
53/* display information for one message */
54
55void print_mail_info(char * prefix, mailmessage * msg)
56{
57 char * from;
58 char * subject;
59 char * decoded_from;
60 char * decoded_subject;
61 size_t cur_token;
62 int r;
63 int is_addr;
64 char * dsp_from;
65 char * dsp_subject;
66 char output[MAX_OUTPUT];
67 struct mailimf_single_fields single_fields;
68
69 is_addr = 0;
70 from = NULL;
71 subject = NULL;
72
73 decoded_subject = NULL;
74 decoded_from = NULL;
75
76 /* from field */
77
78 if (msg->msg_fields != NULL)
79 mailimf_single_fields_init(&single_fields, msg->msg_fields);
80 else
81 memset(&single_fields, 0, sizeof(single_fields));
82
83 get_from_value(&single_fields, &from, &is_addr);
84
85 if (from == NULL)
86 decoded_from = NULL;
87 else {
88 if (!is_addr) {
89 cur_token = 0;
90 r = mailmime_encoded_phrase_parse(DEST_CHARSET,
91 from, strlen(from),
92 &cur_token, DEST_CHARSET,
93 &decoded_from);
94 if (r != MAILIMF_NO_ERROR) {
95 decoded_from = strdup(from);
96 if (decoded_from == NULL)
97 goto err;
98 }
99 }
100 else {
101 decoded_from = strdup(from);
102 if (decoded_from == NULL) {
103 goto err;
104 }
105 }
106 }
107
108 if (decoded_from == NULL)
109 dsp_from = "";
110 else {
111 dsp_from = decoded_from;
112 strip_crlf(dsp_from);
113 }
114
115 /* subject */
116
117 if (single_fields.fld_subject != NULL)
118 subject = single_fields.fld_subject->sbj_value;
119
120 if (subject == NULL)
121 decoded_subject = NULL;
122 else {
123 cur_token = 0;
124 r = mailmime_encoded_phrase_parse(DEST_CHARSET,
125 subject, strlen(subject),
126 &cur_token, DEST_CHARSET,
127 &decoded_subject);
128 if (r != MAILIMF_NO_ERROR) {
129 decoded_subject = strdup(subject);
130 if (decoded_subject == NULL)
131 goto free_from;
132 }
133 }
134
135 if (decoded_subject == NULL)
136 dsp_subject = "";
137 else {
138 dsp_subject = decoded_subject;
139 strip_crlf(dsp_subject);
140 }
141
142 snprintf(output, MAX_OUTPUT, "%3i: %-21.21s %s%-53.53s",
143 msg->msg_index, dsp_from, prefix, dsp_subject);
144
145 printf("%s\n", output);
146
147 if (decoded_subject != NULL)
148 free(decoded_subject);
149 if (decoded_from != NULL)
150 free(decoded_from);
151
152 return;
153
154 free_from:
155 if (decoded_from)
156 free(decoded_from);
157 err:
158}