aboutsummaryrefslogtreecommitdiffstats
path: root/utils/mime_tool.c
blob: c2dde4fddf9d848d6f3c82d191b8d4ff3e59067e (plain)
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
/*-
 * Copyright 2016 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "config.h"
#include "printf.h"
#include "message.h"
#include "util.h"
#include "content_type.h"
#include "task.h"
#include "mime_parser.h"
#include "unix-std.h"


static void
rspamd_show_normal (struct rspamd_mime_part *part)
{
	rspamd_printf ("got normal part %p: parent: %p, type: %T/%T,"
			"length: %z (%z raw)\n",
			part, part->parent_part,
			&part->ct->type, &part->ct->subtype,
			part->parsed_data.len,
			part->raw_data.len);
}

static void
rspamd_show_multipart (struct rspamd_mime_part *part)
{
	struct rspamd_mime_part *cur;
	guint i;

	rspamd_printf ("got multipart part %p, boundary: %T: parent: %p, type: %T/%T, children: [",
			part, &part->ct->boundary,
			part->parent_part,
			&part->ct->type, &part->ct->subtype);

	if (part->children) {
		for (i = 0; i < part->children->len; i ++) {
			cur = g_ptr_array_index (part->children, i);

			if (i != 0) {
				rspamd_printf (", %p{%T/%T}", cur,
						&cur->ct->type, &cur->ct->subtype);
			}
			else {
				rspamd_printf ("%p{%T/%T}", cur,
						&cur->ct->type, &cur->ct->subtype);
			}
		}
	}

	rspamd_printf ("]\n");
}

static void
rspamd_show_message (struct rspamd_mime_part *part)
{
	rspamd_printf ("got message part %p: parent: %p\n",
				part, part->parent_part);
}

static void
rspamd_process_file (struct rspamd_config *cfg, const gchar *fname, gint mode)
{
	struct rspamd_task *task;
	gint fd;
	gpointer map;
	struct stat st;
	GError *err = NULL;
	struct rspamd_mime_part *part;
	guint i;

	fd = open (fname, O_RDONLY);

	if (fd == -1) {
		rspamd_fprintf (stderr, "cannot open %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	if (fstat (fd, &st) == -1) {
		rspamd_fprintf (stderr, "cannot stat %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
	close (fd);

	if (map == MAP_FAILED) {
		rspamd_fprintf (stderr, "cannot mmap %s: %s\n", fname, strerror (errno));
		exit (EXIT_FAILURE);
	}

	task = rspamd_task_new (NULL, cfg);
	task->msg.begin = map;
	task->msg.len = st.st_size;

	if (!rspamd_mime_parse_task (task, &err)) {
		rspamd_fprintf (stderr, "cannot parse %s: %e\n", fname, err);
		g_error_free (err);
	}

	for (i = 0; i < task->parts->len; i ++) {
		part = g_ptr_array_index (task->parts, i);

		if (part->ct->flags & RSPAMD_CONTENT_TYPE_MULTIPART) {
			rspamd_show_multipart (part);
		}
		else if (part->ct->flags & RSPAMD_CONTENT_TYPE_MESSAGE) {
			rspamd_show_message (part);
		}
		else {
			rspamd_show_normal (part);
		}
	}

	rspamd_task_free (task);
	munmap (map, st.st_size);
}

int
main (int argc, char **argv)
{
	gint i, start = 1;
	struct rspamd_config *cfg;
	rspamd_logger_t *logger = NULL;

	cfg = rspamd_config_new ();
	cfg->libs_ctx = rspamd_init_libs ();
	cfg->log_type = RSPAMD_LOG_CONSOLE;
	rspamd_set_logger (cfg, g_quark_from_static_string ("mime"), &logger, NULL);
	(void) rspamd_log_open (logger);
	g_log_set_default_handler (rspamd_glib_log_function, logger);
	g_set_printerr_handler (rspamd_glib_printerr_function);
	rspamd_config_post_load (cfg,
			RSPAMD_CONFIG_INIT_LIBS|RSPAMD_CONFIG_INIT_URL|RSPAMD_CONFIG_INIT_NO_TLD);

	for (i = start; i < argc; i ++) {
		if (argv[i]) {
			rspamd_process_file (cfg, argv[i], 0);
		}
	}

	rspamd_log_close (logger);
	REF_RELEASE (cfg);

	return 0;
}