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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
|
/*
* Perl XS module for interacting with rspamd
*
* vi:ts=4
*/
#include "../src/config.h"
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#include "../src/main.h"
#include "../src/message.h"
#include "../src/cfg_file.h"
#include "../src/perl.h"
#include "../src/mem_pool.h"
#define XSINTERFACE_FUNC_RSPAMD_MESSAGE_SET(cv,f) \
CvXSUBANY(cv).any_dptr = (void (*) (pTHX_ void*))(CAT2( g_mime_message_,f ))
#define XSINTERFACE_FUNC_RSPAMD_PART_SET(cv,f) \
CvXSUBANY(cv).any_dptr = (void (*) (pTHX_ void*))(CAT2( g_mime_part_,f ))
#define XSINTERFACE_FUNC_RSPAMD_IA_SET(cv,f) \
CvXSUBANY(cv).any_dptr = (void (*) (pTHX_ void*))(CAT2( internet_address_,f ))
struct raw_header {
struct raw_header *next;
char *name;
char *value;
};
typedef struct _GMimeHeader {
GHashTable *hash;
GHashTable *writers;
struct raw_header *headers;
} local_GMimeHeader;
/* enums */
typedef GMimePartEncodingType Mail__Rspamd__PartEncodingType;
typedef InternetAddressType Mail__Rspamd__InternetAddressType;
/* C types */
typedef GMimeObject * Mail__Rspamd__Object;
typedef GMimeParam * Mail__Rspamd__Param;
typedef GMimePart * Mail__Rspamd__Part;
typedef GMimeParser * Mail__Rspamd__Parser;
typedef GMimeMultipart * Mail__Rspamd__MultiPart;
typedef GMimeMessage * Mail__Rspamd__Message;
typedef GMimeMessagePart * Mail__Rspamd__MessagePart;
typedef GMimeMessagePartial * Mail__Rspamd__MessagePartial;
typedef InternetAddress * Mail__Rspamd__InternetAddress;
typedef GMimeDisposition * Mail__Rspamd__Disposition;
typedef GMimeContentType * Mail__Rspamd__ContentType;
typedef GMimeCharset * Mail__Rspamd__Charset;
/*
* Declarations for message header hash array
*/
static gboolean
recipients_destroy (gpointer key, gpointer value, gpointer user_data)
{
InternetAddressList *recipients = value;
internet_address_list_destroy (recipients);
return TRUE;
}
typedef struct {
int keyindex; /* key index for firstkey */
char *fetchvalue; /* value for each() method fetched with FETCH */
Mail__Rspamd__Message objptr; /* any object pointer */
} hash_header;
typedef hash_header *Mail__Rspamd__Hash__Header;
/*
* Double linked list of perl allocated pointers (for DESTROY xsubs)
*/
static GList *plist = NULL;
/*
* Calling callback function for each mime part
*/
struct _user_data_sv {
SV * svfunc;
SV * svuser_data;
SV * svfunc_complete;
SV * svfunc_sizeout;
};
static void
call_sub_foreach(GMimeObject *mime_object, gpointer data)
{
SV * svpart;
SV * rvpart;
dSP ;
struct _user_data_sv *svdata;
svdata = (struct _user_data_sv *) data;
svpart = sv_newmortal ();
if (GMIME_IS_PART(mime_object)) {
rvpart = sv_setref_pv(svpart, "Mail::Rspamd::Part", (Mail__Rspamd__Part)mime_object);
} else {
rvpart = sv_setref_pv(svpart, "Mail::Rspamd::Object", mime_object);
}
PUSHMARK (sp);
XPUSHs (rvpart);
XPUSHs (sv_mortalcopy (svdata->svuser_data));
PUTBACK ;
if (svdata->svfunc) {
perl_call_sv (svdata->svfunc, G_DISCARD);
}
}
/* known header field types */
enum {
HEADER_FROM = 0,
HEADER_REPLY_TO,
HEADER_TO,
HEADER_CC,
HEADER_BCC,
HEADER_SUBJECT,
HEADER_DATE,
HEADER_MESSAGE_ID,
HEADER_UNKNOWN
};
static GList *
local_message_get_header(GMimeMessage *message, const char *field)
{
struct raw_header *h;
GList * gret = NULL;
if (field == NULL) {
return NULL;
}
h = GMIME_OBJECT(message)->headers->headers;
while (h) {
if (h->value && !g_strncasecmp(field, h->name, strlen(field))) {
gret = g_list_prepend(gret, g_strdup(h->value));
}
h = h->next;
}
return gret;
}
/**
* g_mime_message_set_date_from_string: Set the message sent-date
* @message: MIME Message
* @string: A string of date
*
* Set the sent-date on a MIME Message.
**/
static void
local_mime_message_set_date_from_string (GMimeMessage *message, const gchar *string)
{
time_t date;
int offset = 0;
date = g_mime_utils_header_decode_date (string, &offset);
g_mime_message_set_date (message, date, offset);
}
/* different declarations for different types of set and get functions */
typedef const char *(*GetFunc) (GMimeMessage *message);
typedef InternetAddressList *(*GetRcptFunc) (GMimeMessage *message, const char *type );
typedef GList *(*GetListFunc) (GMimeMessage *message, const char *type );
typedef void (*SetFunc) (GMimeMessage *message, const char *value);
typedef void (*SetListFunc) (GMimeMessage *message, const char *field, const char *value);
/** different types of functions
*
* FUNC_CHARPTR
* - function with no arguments
* - get returns char*
*
* FUNC_IA (from Internet Address)
* - function with additional "field" argument from the fieldfunc table,
* - get returns Glist*
*
* FUNC_LIST
* - function with additional "field" argument (given arbitrary header field name)
* - get returns Glist*
**/
enum {
FUNC_CHARPTR = 0,
FUNC_CHARFREEPTR,
FUNC_IA,
FUNC_LIST
};
/**
* fieldfunc struct: structure of MIME fields and corresponding get and set
* functions.
**/
static struct {
char * name;
GetFunc func;
GetRcptFunc rcptfunc;
GetListFunc getlistfunc;
SetFunc setfunc;
SetListFunc setlfunc;
gint functype;
} fieldfunc[] = {
{ "From", g_mime_message_get_sender, NULL, NULL, g_mime_message_set_sender, NULL, FUNC_CHARPTR },
{ "Reply-To", g_mime_message_get_reply_to, NULL, NULL, g_mime_message_set_reply_to, NULL, FUNC_CHARPTR },
{ "To", NULL, g_mime_message_get_recipients, NULL, NULL, g_mime_message_add_recipients_from_string, FUNC_IA },
{ "Cc", NULL, g_mime_message_get_recipients, NULL, NULL, g_mime_message_add_recipients_from_string, FUNC_IA },
{ "Bcc", NULL, g_mime_message_get_recipients, NULL, NULL, g_mime_message_add_recipients_from_string, FUNC_IA },
{ "Subject", g_mime_message_get_subject, NULL, NULL, g_mime_message_set_subject, NULL, FUNC_CHARPTR },
{ "Date", g_mime_message_get_date_string, NULL, NULL, local_mime_message_set_date_from_string, NULL, FUNC_CHARFREEPTR },
{ "Message-Id", g_mime_message_get_message_id, NULL, NULL, g_mime_message_set_message_id, NULL, FUNC_CHARPTR },
{ NULL, NULL, NULL, local_message_get_header, NULL, g_mime_message_add_header, FUNC_LIST }
};
/**
* message_set_header: set header of any type excluding special (Content- and MIME-Version:)
**/
static void
message_set_header (GMimeMessage *message, const char *field, const char *value)
{
gint i;
if (!g_strcasecmp (field, "MIME-Version:") || !g_strncasecmp (field, "Content-", 8)) {
return;
}
for (i=0; i<=HEADER_UNKNOWN; ++i) {
if (!fieldfunc[i].name || !g_strncasecmp(field, fieldfunc[i].name, strlen(fieldfunc[i].name))) {
switch (fieldfunc[i].functype) {
case FUNC_CHARPTR:
(*(fieldfunc[i].setfunc))(message, value);
break;
case FUNC_IA:
(*(fieldfunc[i].setlfunc))(message, fieldfunc[i].name, value);
break;
case FUNC_LIST:
(*(fieldfunc[i].setlfunc))(message, field, value);
break;
}
break;
}
}
}
/**
* message_get_header: returns the list of 'any header' values
* (except of unsupported yet Content- and MIME-Version special headers)
*
* You should free the GList list by yourself.
**/
static
GList *
message_get_header(GMimeMessage *message, const char *field) {
gint i;
char * ret = NULL;
GList * gret = NULL;
InternetAddressList *ia_list = NULL, *ia;
for (i = 0; i <= HEADER_UNKNOWN; ++i) {
if (!fieldfunc[i].name || !g_strncasecmp(field, fieldfunc[i].name, strlen(fieldfunc[i].name))) {
switch (fieldfunc[i].functype) {
case FUNC_CHARFREEPTR:
ret = (char *)(*(fieldfunc[i].func))(message);
break;
case FUNC_CHARPTR:
ret = (char *)(*(fieldfunc[i].func))(message);
break;
case FUNC_IA:
ia_list = (*(fieldfunc[i].rcptfunc))(message, field);
gret = g_list_alloc();
ia = ia_list;
while (ia && ia->address) {
char *ia_string;
ia_string = internet_address_to_string ((InternetAddress *)ia->address, FALSE);
gret = g_list_append (gret, ia_string);
ia = ia->next;
}
break;
case FUNC_LIST:
gret = (*(fieldfunc[i].getlistfunc))(message, field);
break;
}
break;
}
}
if (gret == NULL && ret != NULL) {
gret = g_list_prepend (gret, g_strdup (ret));
}
if (fieldfunc[i].functype == FUNC_CHARFREEPTR && ret) {
g_free (ret);
}
return gret;
}
MODULE = Mail::Rspamd PACKAGE = Mail::Rspamd::Log PREFIX = rspamd_log_
PROTOTYPES: DISABLE
void
rspamd_log_log (level, str)
int level
const char *str
CODE:
g_log (G_LOG_DOMAIN, level, "%s", str);
MODULE = Mail::Rspamd PACKAGE = Mail::Rspamd
INCLUDE: Rspamd/Object.xs
INCLUDE: Rspamd/ContentType.xs
INCLUDE: Rspamd/Part.xs
INCLUDE: Rspamd/Message.xs
INCLUDE: Rspamd/InternetAddress.xs
INCLUDE: Rspamd/Hash.xs
|