#include <unicode/uchar.h>
#include "libserver/cfg_file_private.h"
#include "lua/lua_common.h"
+#include "contrib/uthash/utlist.h"
#define GTUBE_SYMBOL "GTUBE"
part->raw_data.len = len;
part->parsed_data.begin = start;
part->parsed_data.len = len;
- part->id = task->parts->len;
- part->raw_headers = g_hash_table_new_full (rspamd_strcase_hash,
- rspamd_strcase_equal, NULL, rspamd_ptr_array_free_hard);
- part->headers_order = g_queue_new ();
-
-
+ part->id = MESSAGE_FIELD (task, parts)->len;
+ part->raw_headers = rspamd_message_headers_new ();
+ part->headers_order = NULL;
tok = rspamd_task_get_request_header (task, "Filename");
part->cd = rspamd_content_disposition_parse (cdbuf, strlen (cdbuf),
task->task_pool);
- g_ptr_array_add (task->parts, part);
+ g_ptr_array_add (MESSAGE_FIELD (task, parts), part);
rspamd_mime_parser_calc_digest (part);
/* Generate message ID */
mid = rspamd_mime_message_id_generate ("localhost.localdomain");
rspamd_mempool_add_destructor (task->task_pool,
(rspamd_mempool_destruct_t) g_free, mid);
- task->message_id = mid;
+ MESSAGE_FIELD (task, message_id) = mid;
task->queue_id = mid;
}
static void
rspamd_message_dtor (struct rspamd_message *msg)
{
+ guint i;
+ struct rspamd_mime_part *p;
+ struct rspamd_mime_text_part *tp;
+
+ PTR_ARRAY_FOREACH (msg->parts, i, p) {
+ if (p->raw_headers) {
+ rspamd_message_headers_destroy (p->raw_headers);
+ }
+
+ if (IS_CT_MULTIPART (p->ct)) {
+ if (p->specific.mp->children) {
+ g_ptr_array_free (p->specific.mp->children, TRUE);
+ }
+ }
+ }
+
+ PTR_ARRAY_FOREACH (msg->text_parts, i, tp) {
+ if (tp->utf_words) {
+ g_array_free (tp->utf_words, TRUE);
+ }
+ if (tp->normalized_hashes) {
+ g_array_free (tp->normalized_hashes, TRUE);
+ }
+ if (tp->languages) {
+ g_ptr_array_unref (tp->languages);
+ }
+ }
+
+ g_ptr_array_unref (msg->text_parts);
+ g_ptr_array_unref (msg->parts);
+
+ g_ptr_array_unref (msg->from_mime);
+ g_ptr_array_unref (msg->rcpt_mime);
+
+ g_hash_table_unref (msg->urls);
+ g_hash_table_unref (msg->emails);
}
struct rspamd_message*
{
struct rspamd_message *msg;
- msg = rspamd_mempool_alloc0 (sizeof (*msg));
+ msg = rspamd_mempool_alloc0 (task->task_pool, sizeof (*msg));
+ msg->raw_headers = rspamd_message_headers_new ();
+ msg->emails = g_hash_table_new (rspamd_email_hash, rspamd_emails_cmp);
+ msg->urls = g_hash_table_new (rspamd_url_hash, rspamd_urls_cmp);
+ msg->parts = g_ptr_array_sized_new (4);
+ msg->text_parts = g_ptr_array_sized_new (2);
+ return msg;
}
gboolean
task->msg.begin = p;
task->msg.len = len;
rspamd_cryptobox_hash_init (&st, NULL, 0);
+ task->message = rspamd_message_new (task);s
if (task->flags & RSPAMD_TASK_FLAG_MIME) {
enum rspamd_mime_parse_error ret;
}
- if (task->message_id == NULL) {
- task->message_id = "undef";
+ if (MESSAGE_FIELD (task, message_id) == NULL) {
+ MESSAGE_FIELD (task, message_id) = "undef";
}
debug_task ("found %ud parts in message", task->parts->len);
task->queue_id = "undef";
}
- if (task->received->len > 0) {
+ if (MESSAGE_FIELD (task, received)) {
gboolean need_recv_correction = FALSE;
rspamd_inet_addr_t *raddr;
- recv = g_ptr_array_index (task->received, 0);
+ recv = MESSAGE_FIELD (task, received);
/*
* For the first header we must ensure that
* received is consistent with the IP that we obtain through
trecv->from_hostname = trecv->real_hostname;
}
-#ifdef GLIB_VERSION_2_40
- g_ptr_array_insert (task->received, 0, trecv);
-#else
- /*
- * Unfortunately, before glib 2.40 we cannot insert element into a
- * ptr array
- */
- GPtrArray *nar = g_ptr_array_sized_new (task->received->len + 1);
-
- g_ptr_array_add (nar, trecv);
- PTR_ARRAY_FOREACH (task->received, i, recv) {
- g_ptr_array_add (nar, recv);
- }
- rspamd_mempool_add_destructor (task->task_pool,
- rspamd_ptr_array_free_hard, nar);
- task->received = nar;
-#endif
+ DL_PREPEND (MESSAGE_FIELD (task, received), trecv);
}
}
/* We also validate utf8 and replace all non-valid utf8 chars */
rspamd_mime_charset_utf_enforce (nh->decoded, strlen (nh->decoded));
nh->order = norder ++;
- rspamd_mime_header_add (task, target, order, nh, check_newlines);
+ rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
nh = NULL;
state = 0;
break;
nh->decoded = "";
nh->raw_len = p - nh->raw_value;
nh->order = norder ++;
- rspamd_mime_header_add (task, target, order, nh, check_newlines);
+ rspamd_mime_header_add (task, target, order_ptr, nh, check_newlines);
nh = NULL;
state = 0;
break;
}
}
+ /* Since we have prepended headers, we need to reverse the list to get the actual order */
+ LL_REVERSE (*order_ptr);
+
if (check_newlines) {
guint max_cnt = 0;
gint sel = 0;
- GList *cur;
rspamd_cryptobox_hash_state_t hs;
guchar hout[rspamd_cryptobox_HASHBYTES], *hexout;
MESSAGE_FIELD (task, nlines_type) = sel;
- cur = order->head;
rspamd_cryptobox_hash_init (&hs, NULL, 0);
- while (cur) {
- nh = cur->data;
-
+ LL_FOREACH (*order_ptr, nh) {
if (nh->name && nh->flags != RSPAMD_HEADER_RECEIVED) {
rspamd_cryptobox_hash_update (&hs, nh->name, strlen (nh->name));
}
-
- cur = g_list_next (cur);
}
rspamd_cryptobox_hash_final (&hs, hout);
{
return rspamd_message_get_header_from_hash (MESSAGE_FIELD (task, raw_headers),
field);
+}
+
+void
+rspamd_message_headers_destroy (khash_t(rspamd_mime_headers_htb) *htb)
+{
+ kh_destroy (rspamd_mime_headers_htb, htb);
+}
+
+khash_t(rspamd_mime_headers_htb) *
+rspamd_message_headers_new (void)
+{
+ return kh_init (rspamd_mime_headers_htb);
}
\ No newline at end of file
void
rspamd_task_free (struct rspamd_task *task)
{
- struct rspamd_mime_part *p;
- struct rspamd_mime_text_part *tp;
struct rspamd_email_address *addr;
struct rspamd_lua_cached_entry *entry;
static guint free_iters = 0;
if (task) {
debug_task ("free pointer %p", task);
- for (i = 0; i < task->parts->len; i ++) {
- p = g_ptr_array_index (task->parts, i);
-
- if (p->raw_headers) {
- g_hash_table_unref (p->raw_headers);
- }
-
- if (p->headers_order) {
- g_queue_free (p->headers_order);
- }
-
- if (IS_CT_MULTIPART (p->ct)) {
- if (p->specific.mp->children) {
- g_ptr_array_free (p->specific.mp->children, TRUE);
- }
- }
- }
-
- for (i = 0; i < task->text_parts->len; i ++) {
- tp = g_ptr_array_index (task->text_parts, i);
-
- if (tp->utf_words) {
- g_array_free (tp->utf_words, TRUE);
- }
- if (tp->normalized_hashes) {
- g_array_free (tp->normalized_hashes, TRUE);
- }
- if (tp->languages) {
- g_ptr_array_unref (tp->languages);
- }
- }
-
if (task->rcpt_envelope) {
for (i = 0; i < task->rcpt_envelope->len; i ++) {
addr = g_ptr_array_index (task->rcpt_envelope, i);