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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
#ifndef H_MAILNEW_ANALYZE
#define H_MAILNEW_ANALYZE
#include "stellar/session.h"
#include "stellar/mail.h"
#include "mail_decoder_util.h"
#include "mail_decoder_mime.h"
#define MAIL_GETLINE_ERR -1
#define MAIL_INPUT_END 0
#define MAIL_FOLD_LINE_END 1
#define MAIL_FOLD_LINE_START 2
#define MAIL_LINE_END 3
#define MAIL_PACKET_END 4
#define MAILAMALYZE_MODULE "[MAIL_ANALYZE]"
#define NULL_FILENAME "UNKNOWN_FILENAME.UNKNOWN"
#define NULL_FILENAME_LEN 24
#define MAIL_HEADER_FIELD_SPLIT_PART_NAME 0
#define MAIL_HEADER_FIELD_SPLIT_PART_VALUE 1
#define MAIL_HEADER_FIELD_SPLIT_PART_MAX 2
#define MAIL_HEADER_FIELDS_MAX 16
#define MAIL_COMMAND_BUFFER_MAX 1024
#define MAIL_BODY_BUFFER_MAX 4096
#define MAIL_ATTACHMENT_BUFFER_MAX 4096
#define MAIL_ATTACHMENT_NAME_BUFFER_MAX 256
#define MAIL_EML_BUFFER_MAX 4096
#define MAIL_STAT_INIT 0x10//�ʼ���δ״̬
#define MAIL_STAT_HEADER 0x11//�ʼ�ͷ������״̬
#define MAIL_STAT_BODY 0x12//�ʼ��崦��״̬
#define MAIL_STAT_END 0x13//�ʼ�����״̬
#define MAIL_NO_USER 0
#define MAIL_HAVE_USER (1<<0)
#define MAIL_BEFORE_USER (1<<1)
#define MAIL_BEFORE_PASS (1<<2)
#define MAIL_AFTER_PASS (1<<3)
#define MAIL_AUTH_PLAIN (1<<4)
#define MAIL_GET_NONE -1
#define MAIL_GET_FROM 9
#define MAIL_GET_TO 10
#define MAIL_GET_SUB 11
#define MAIL_GET_DATE 12
#define MAIL_GET_COUNT 13
#define MAIL_GET_FILENAME 14
#define MAIL_GET_FILECOUNT 15
#define MAIL_GET_OTHER 16
#define MAIL_GET_FILECOUNT_END 17
#define MAIL_GET_COUNT_END 18
#define MAIL_TRANS_ENC_UNKNOWN 0
#define MAIL_TRANS_ENC_BASE64 1
#define MAIL_TRANS_ENC_QP 2
enum EML_LINE_TYPE
{
EML_DATA_HEAD_IN=1,
EML_DATA_BODY_IN,
EML_DATA_FROM,
EML_DATA_TO,
EML_DATA_CC,
EML_DATA_BCC, /*6*/
EML_DATA_SUBJECT,
EML_DATA_CONTENTTYPE,
EML_DATA_CONTENTTRANSFERENCODING,
EML_DATA_HEAD_BODY_BORDER,
EML_DATA_END, /*11*/
EML_DATA_RETURN_PATH,
EML_DATA_DELIVERED_TO,
EML_DATA_RECEIVED,
EML_DATA_DATE, /*15*/
EML_DATA_REPLY_TO,
EML_UNKNOWN,
};
enum MAIL_MESSAGE_TYPE {
MAIL_MESSAGE_TYPE_COMMAND,
MAIL_MESSAGE_TYPE_HEADER,
MAIL_MESSAGE_TYPE_BODY,
MAIL_MESSAGE_TYPE_ATTACHMENT,
MAIL_MESSAGE_TYPE_EML,
MAIL_MESSAGE_TYPE_MAX,
};
struct mail_command {
enum MAIL_COMMAND cmd;
char *arg;
size_t arg_len;
char *cmd_line;
size_t cmd_line_len;
};
struct mail_body {
char *body;
size_t body_len;
size_t body_offset;
int is_body_finished;
};
struct mail_attachment {
char *attachment_name;
size_t attachment_name_len;
char *attachment;
size_t attachment_len;
size_t attachment_offset;
int is_attachment_finished;
};
struct mail_eml {
char *eml;
size_t eml_len;
size_t eml_offset;
int is_eml_finished;
};
struct mail_message {
enum MAIL_MESSAGE_TYPE type;
enum MAIL_PROTOCOL mail_protocol;
size_t mail_seq;
struct mail_command *command;
struct mail_header *header;
struct mail_body *body;
struct mail_attachment *attachment;
struct mail_eml *eml;
struct session *sess_ref;
};
typedef struct _new_mail_key_info
{
stMailElem username; //�û���
stMailElem password; //�û�����
stMailElem sendaddr; // ���ŵ�ַ
stMailElem recvaddr; // ���ŵ�ַ
stMailElem subject; //�ʼ�����
stMailElem date; //����ʱ��
}new_stKeyInfo;
typedef struct _mail_key_info
{
stMailElem *username; //用户名
stMailElem *password; //用户密码
stMailElem *sendaddr; //发信地址
stMailElem *recvaddr; //收信地址
stMailElem *subject; //邮件主题
stMailElem *date; //发信时间
} stKeyInfo;
typedef struct _field_element
{
long long prot_flag;
char* current_charset; //当前字符集编码格式 ;可能为NULL;
void *buf; //当前字段对应原始数据内容, 经过传输编码解析
int buflen; //当前字段对应原始数据长度
}stFieldElem;
typedef struct _mail_pme_info
{
char protocol; //邮件协议SMTP_PROTOCOL/POP3_PROTOCOL/IMAP_PROTOCOL
char trans_enc; //传输编码,只有邮件正文、附件有;主题和附件名中是否有传输编码需要业务插件自行解析
short session_seq; //当前会话是TCP链接中的第几个会话,从0开始;
int buflen; //邮件原始数据长度
char* buf; //邮件原始数据内容,不经过传输编码解析
char* current_charset; //当前字符集编码格式 ;可能为NULL;
stKeyInfo *pMailInfo; //保留邮件的关键信息
//NEXT: ADD BY ZCW
int cur_offset; //当前字段在原始数据buf中的的偏移
int lostlen;
stFieldElem *elem;
int elem_num;
} tdMailInfo;
struct email_parser
{
stBufCache pDataLineBuf; //��ȡ���ݰ�������
stBufCache pOriginalData; //ԭʼ�ʼ���Ϣ
stBufCache pMimeDecodeBuf; //MIME�����Ĵ洢��
stBufCache pBizRegion; //�����ص�ҵ�����ֶ�buf������
tdMailInfo plug_mailinfo; //session_info->app_info
stKeyInfo pMailinfo; //plug_mailinfo.pMailinfo
new_stKeyInfo keyinfo; //plug_mailinfo.pMailinfo�ij�Ա
stFieldElem elem;
MimeParse_t *mimeinfo; // mime����ṹ
char subject_charset[MAIL_MAX_CHARSET_LEN]; //�����ַ��������ʽ
int iDataPointer; // ��ǰ��ȡ������ƫ�ƣ����ڰ��ж�ȡ��
int max_elem_num;
int EmlAnalyseState;// ��־�ʼ�����״̬
int MailInfoState; //zcw add, from stMailInfoOld
int iProtoType; //�ʼ��Ự�ṹ��Э������
int thread_num;
char is_drop; //��ǰ�ʼ��Ự��ҵ���DROP
char pending_state; //1-Ϊ�µĻỰ�����������ģ�0-�»Ự�ѵ���
char is_called; //��ǰ�Ự�ص���ҵ���
char pre_dir; //��һ�����ķ�������˫�������ݵ�ʱ��ʹ�ã�����SMTPֻ���ǵ�����ĺ��Դ˱���
//NEXT ONLY IMAP4
char is_continue;
char maybe_end; //')'����ȷ����������ʱ�����ñ��
short obtain_line; //������������ȡһ�У���������۵���
int pre_opsate; //')'����ȷ����������ʱ������һ�ε�������
long pre_uid; //IMAPЭ�鰴���ֽ�����ȡ�ʼ����ݵ����
long uid;
size_t mail_seq;
enum MAIL_PROTOCOL protocol;
struct mail_header *header;
struct mail_body *body;
struct mail_attachment *current_attachment;
struct mail_eml *eml;
int eml_topic_id;
int header_topic_id;
int body_topic_id;
int attachment_topic_id;
struct mq_runtime *mq;
};
int mail_command_update(struct mail_command *command, enum MAIL_COMMAND cmd, const char *cmd_arg, size_t cmd_arg_len, const char *cmd_line, size_t cmd_line_len);
void mail_command_free(struct mail_command *command);
struct mail_command *mail_command_clone(struct mail_command *command);
struct mail_command * mail_command_new(void);
void mail_header_free(struct mail_header *header);
struct mail_header *mail_header_new(void);
int mail_header_add_field(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_date(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_from(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_to(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_cc(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_bcc(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_reply_to(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_header_add_subject(struct mail_header *header, const char *line, size_t len, int enable_mime_decode);
int mail_body_update(struct mail_body *body, const char *line, size_t len, int is_finished);
int mail_body_reset(struct mail_body *body);
void mail_body_free(struct mail_body *body);
struct mail_body * mail_body_clone(struct mail_body *body);
struct mail_body * mail_body_new(void);
int mail_attachment_set_filename(struct mail_attachment *attachment, const char *name, size_t len);
int mail_attachment_update(struct mail_attachment *attachment, const char *line, size_t len, int is_attachment_finished);
int mail_attachment_reset(struct mail_attachment *attachment);
void mail_attachment_free(struct mail_attachment *attachment);
struct mail_attachment *mail_attachment_clone(struct mail_attachment *attachment);
struct mail_attachment * mail_attachment_new(void);
int mail_eml_update(struct mail_eml *eml, const char *data, size_t len, int is_eml_finished);
int mail_eml_reset(struct mail_eml *eml);
void mail_eml_free(struct mail_eml *eml);
struct mail_eml *mail_eml_clone(struct mail_eml *eml);
struct mail_eml *mail_eml_new(void);
int mail_publish_command(struct mq_runtime *mq, int topic_id, struct session *sess, enum MAIL_PROTOCOL protocol, size_t mail_seq, struct mail_command *cmd);
int mail_publish_header(struct mq_runtime *mq, int topic_id, struct session *sess, enum MAIL_PROTOCOL protocol, size_t mail_seq, struct mail_header *header);
int mail_publish_body(struct mq_runtime *mq, int topic_id, struct session *sess, enum MAIL_PROTOCOL protocol, size_t mail_seq, struct mail_body *body);
int mail_publish_attachment(struct mq_runtime *mq, int topic_id, struct session *sess, enum MAIL_PROTOCOL protocol, size_t mail_seq, struct mail_attachment *attachment);
int mail_publish_eml(struct mq_runtime *mq, int topic_id, struct session *sess, enum MAIL_PROTOCOL protocol, struct mail_eml *eml);
void mail_message_free(void *msg, void *arg);
char *mail_message_to_json(struct mail_message *msg);
void email_parser_set_drop(struct email_parser* eml_parser);
size_t email_parser_get_seq(struct email_parser *eml_parser);
enum EML_LINE_TYPE email_parser_parse_line(const char *a_mail_line,int datalen,int EmlAnalyseState);
int email_parser_get_atcpline_c2s(struct email_parser* eml_parser, const char *payload, size_t payload_len, char ** output_buf,int *output_len);
int email_parser_get_tcpdata(struct email_parser* eml_parser, const char *payload, size_t payload_len, int need_folding, char ** output_buf,int *output_len);
int email_parser_entry(struct email_parser *eml_parser, struct session *sess, const char *email_line, size_t line_len, enum EML_LINE_TYPE line_type);
int email_parser_reset(struct email_parser * eml_parser);
void email_parser_free(struct email_parser *eml_parser);
struct email_parser * email_parser_new(struct mq_runtime *mq, struct mail_topics *topics, enum MAIL_PROTOCOL protocol, int is_c2s);
#endif
|