-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange_helpers.c
216 lines (190 loc) · 5.58 KB
/
exchange_helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include "sp_fetchmail.h"
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#if 0
/* https://technet.microsoft.com/en-us/library/bb331962(v=exchg.141).aspx
*first mimepart fields:
*
* Basic fields:
* Sender
* Subject
* Message-ID
* Recipient
*
* Extended fields:
* On-Behalf-Of
* To
* Cc
* Bcc
*
* Expanded and Forwarded Fields:
* Expanded
* Forwarded
*
*
* Examples:
* To: [email protected], Expanded: [email protected]
* To: [email protected], Expanded: [email protected]
* Cc: [email protected]
* Cc: [email protected]
* Bcc: [email protected]
* Sender: [email protected]
* Subject: Test im auftrag von cyan
* Message-Id: <[email protected]>
* Recipient: [email protected]
*
*
* */
#endif
void parse_ms_envelope(char *content, size_t content_len,
addressinfo_t * addinfo)
{
char *endl, *startl, *tmpc;
int left, n;
int sender = 0;
left = content_len;
startl = content;
/* iterate over the block */
while ((1 < left) && (endl = memchr(startl, '\n', left))) {
if ('\r' == *(endl - 1))
memset(endl - 1, 0, 2);
else
memset(endl, 0, 1);
left -= (endl - startl) + 1;
if ((n = 3) && !strncasecmp(startl, "to:", n)) {
} else if ((n = 10) && !strncasecmp(startl, "recipient:", n)) {
} else if ((n = 3) && !strncasecmp(startl, "cc:", n)) {
} else if ((n = 4) && !strncasecmp(startl, "bcc:", n)) {
} else if ((n = 7) && !strncasecmp(startl, "sender:", n)) {
sender = 1;
} else {
goto cont;
}
startl += n;
/* check if there is an extension attribute to the field */
if (memchr(startl, ',', (endl - startl))) {
if ((tmpc = strcasestr(startl, "expanded:"))) {
startl = tmpc + 9;
} else if ((tmpc = strcasestr(startl, "forwarded:"))) {
/* ignore forwarded lines */
goto cont;
} else {
debug("unknown extension field %s", startl);
}
}
/* remove leading spaces */
while (' ' == *startl)
startl++;
if (!addinfo->sender && sender) {
addinfo->sender = strdup(startl);
} else {
clist_uniadd(addinfo->recipients, startl);
}
cont:
startl = endl + 1;
}
}
int get_msrecipients(struct mailmessage *msg, addressinfo_t * addinfo)
{
char *rawcontent = NULL;
size_t rawlen;
struct mailmime *mime = NULL;
int tmpret, ret = 1;
if ((tmpret = mailmessage_get_bodystructure(msg, &msg->msg_mime))) {
debug("cant parse structure of mail id %d (%d)", msg->msg_index, tmpret);
} else if (!msg->msg_mime->mm_data.mm_message.mm_msg_mime
|| !msg->msg_mime->mm_data.mm_message.mm_msg_mime->mm_data.mm_multipart.
mm_mp_list) {
debug("mime structure doesnt look like MS-Journal");
} else if (2 >
clist_count(msg->msg_mime->mm_data.mm_message.mm_msg_mime->mm_data.
mm_multipart.mm_mp_list)) {
/* blurp */
} else if (!(mime =
clist_nth_data(msg->msg_mime->mm_data.mm_message.mm_msg_mime->mm_data.
mm_multipart.mm_mp_list, 0))) {
/* hurz */
} else if ((tmpret =
mailmessage_fetch_section(msg, mime, &rawcontent, &rawlen))) {
debug("cant fetch envelope mime content of mail id %d (%d)",
msg->msg_index, tmpret);
} else {
parse_ms_envelope(rawcontent, rawlen, addinfo);
if (0 < clist_count(addinfo->recipients))
ret = 0;
}
if (rawcontent)
mailmessage_fetch_result_free(msg, rawcontent);
return ret;
}
int check_for_msenvelope(struct mailmessage *msg)
{
char *rawcontent = NULL;
struct mailimf_fields *fields = NULL;
int ret = 1;
if (!msg)
return -1;
if ((init_msg_headerfields(msg))) {
debug("error on headerparsing");
} else if (!get_optional_field_p(msg->msg_fields, "X-MS-Journal-Report")) {
/* keine arme, keine kekse */
} else {
ret = 0;
}
if (fields)
mailimf_fields_free(fields);
if (rawcontent)
mailmessage_fetch_result_free(msg, rawcontent);
return ret;
}
/*
*
*
*
*
*/
int check_for_msjournal(struct mailmessage *msg)
{
addressinfo_t *addinfo = NULL;
int ret = 1;
struct mailmime *mime = NULL;
char *data = NULL;
size_t len;
if (clist_inlistremove(evilmails, msg->msg_uid)) {
debug("mail had problems with parsing. will do non envelope fetch");
} else if (check_for_msenvelope(msg)) {
/* no msmail */
} else if (!(addinfo = addinfo_new())) {
debug("oom in %s", __func__);
} else if (get_msrecipients(msg, addinfo)) {
debug("found journalheader but no envelope recipients in mail id %d",
msg->msg_index);
clist_uniadd(evilmails, msg->msg_uid);
} else if (!(mime =
clist_nth_data(msg->msg_mime->mm_data.mm_message.mm_msg_mime->mm_data.
mm_multipart.mm_mp_list, 1))) {
debug("no mail-mimepart found in mail id %d", msg->msg_index);
} else if ((ret = mailmessage_fetch_section(msg, mime, &data, &len))) {
debug("cant fetch mime content of mail id %d (%d)", msg->msg_index, ret);
//} else if (!(headerend = strstr(data, "\r\n\r\n"))) {
// debug("cant detect header in mime part mail id %d", msg->msg_index);
} else {
ret = deliver_lmtp(data, len, addinfo, NULL);
}
if (data)
mailmessage_fetch_result_free(msg, data);
/* } else {
//fprintf(stderr, "sender is: %s\nrecipients:\n", addinfo->sender);
//for (clistiter *iter = clist_begin(addinfo->recipients); iter;
// iter = clist_next(iter)) {
// fprintf(stderr, " %s\n", (char *) clist_content(iter));
//}
ret = deliver_msjournal_mail(addinfo, msg);
}*/
if (addinfo)
addinfo_free(addinfo);
return ret;
}