masqmail

view src/message.c @ 429:5593964ec779

Added route conditions based on the From header New route config directives: allowed_from_hdrs, denied_from_hdrs. This feature was motivated by Philipp Takacs <philipp29@t-online.de>.
author markus schnalke <meillo@marmaro.de>
date Thu, 20 Nov 2014 20:36:20 +0100
parents 41958685480d
children
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999-2001 Oliver Kurth
4 ** Copyright (C) 2010 markus schnalke <meillo@marmaro.de>
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
21 #include "masqmail.h"
23 message*
24 create_message()
25 {
26 message *msg = (message *) g_malloc(sizeof(message));
27 if (msg) {
28 memset(msg, 0, sizeof(message));
29 msg->data_size = -1;
30 }
31 return msg;
32 }
34 /*
35 ** This function is currently (0.2.24) only used for client side SMTP
36 ** SIZE support (RFC 1870). The flag is_smtp is always true therefore.
37 **
38 ** Their definition of the message size in RFC 1870 is:
39 **
40 ** The message size is defined as the number of octets, including
41 ** CR-LF pairs, but not the SMTP DATA command's terminating dot
42 ** or doubled quoting dots, to be transmitted by the SMTP client
43 ** after receiving reply code 354 to the DATA command.
44 **
45 ** l_cnt (line count) covers '\r' characters which are not present in
46 ** masqmail's internal format. Dots are also not stuffed in the
47 ** internal format. Dot-stuffing is ignored in the size.
48 */
49 gint
50 msg_calc_size(message *msg, gboolean is_smtp)
51 {
52 GList *node;
53 gint l_cnt = 0; /* line count (we need to add so many '\r' for SMTP) */
54 gint c_cnt = 0; /* character count */
56 /* message header size */
57 if (msg->hdr_list) {
58 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
59 if (node->data) {
60 header *hdr = (header *) (node->data);
61 if (hdr->header) {
62 char *p = hdr->header;
63 while (*p) {
64 if (*p++ == '\n')
65 l_cnt++;
66 c_cnt++;
67 }
68 }
69 }
70 }
71 }
73 /* empty line separating headers from data: */
74 c_cnt++;
75 l_cnt++;
77 /* message data size */
78 if (msg->data_list) {
79 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
80 if (node->data) {
81 char *p = node->data;
82 while (*p) {
83 if (*p++ == '\n')
84 l_cnt++;
85 c_cnt++;
86 }
87 }
88 }
89 }
91 return is_smtp ? c_cnt + l_cnt : c_cnt;
92 }
94 void
95 msg_free_data(message *msg)
96 {
97 GList *node;
99 if (msg->data_list) {
100 for (node = g_list_first(msg->data_list); node; node = g_list_next(node)) {
101 if (node->data)
102 g_free(node->data);
103 }
104 g_list_free(msg->data_list);
105 msg->data_list = NULL;
106 }
107 }
109 void
110 destroy_message(message *msg)
111 {
112 GList *node;
114 if (!msg) {
115 return;
116 }
118 if (msg->uid)
119 g_free(msg->uid);
120 if (msg->ident)
121 g_free(msg->ident);
122 if (msg->return_path)
123 g_free(msg->return_path);
125 if (msg->rcpt_list) {
126 for (node = g_list_first(msg->rcpt_list); node; node = g_list_next(node)) {
127 if (node->data)
128 g_free(node->data);
129 }
130 g_list_free(msg->rcpt_list);
131 }
132 if (msg->hdr_list) {
133 for (node = g_list_first(msg->hdr_list); node; node = g_list_next(node)) {
134 if (node->data) {
135 header *hdr = (header *) (node->data);
136 if (hdr->header)
137 g_free(hdr->header);
138 g_free(node->data);
139 }
140 }
141 g_list_free(msg->hdr_list);
142 }
144 if (msg->full_sender_name)
145 g_free(msg->full_sender_name);
147 msg_free_data(msg);
149 g_free(msg);
150 }
152 void
153 destroy_msg_list(GList *msg_list)
154 {
155 GList *msg_node;
157 foreach(msg_list, msg_node) {
158 message *msg = (message *) (msg_node->data);
159 destroy_message(msg);
160 }
161 g_list_free(msg_list);
162 }
164 msg_out*
165 create_msg_out(message *msg)
166 {
167 msg_out *msgout = g_malloc0(sizeof(msg_out));
168 msgout->msg = msg;
169 return msgout;
170 }
172 msg_out*
173 clone_msg_out(msg_out *msgout_orig)
174 {
175 if (msgout_orig) {
176 msg_out *msgout = create_msg_out(msgout_orig->msg);
177 if (msgout) {
178 msgout->msg = msgout_orig->msg;
179 if (msgout_orig->return_path)
180 msgout->return_path = copy_address(msgout_orig->return_path);
181 if (msgout_orig->hdr_list)
182 msgout->hdr_list = g_list_copy(msgout_orig->hdr_list);
183 /* FIXME: if this lives longer than the original
184 and we access one of the xtra hdrs, we will segfault
185 or cause some weird bugs: */
186 msgout->xtra_hdr_list = NULL;
187 if (msgout_orig->rcpt_list)
188 msgout->rcpt_list = g_list_copy(msgout_orig->rcpt_list);
189 }
190 return msgout;
191 }
192 return NULL;
193 }
195 void
196 destroy_msg_out(msg_out *msgout)
197 {
198 if (msgout) {
199 if (msgout->return_path)
200 destroy_address(msgout->return_path);
201 if (msgout->hdr_list)
202 g_list_free(msgout->hdr_list);
203 if (msgout->xtra_hdr_list) {
204 GList *hdr_node;
205 foreach(msgout->xtra_hdr_list, hdr_node) {
206 header *hdr = (header *) (hdr_node->data);
207 destroy_header(hdr);
208 }
209 g_list_free(msgout->xtra_hdr_list);
210 }
211 g_free(msgout);
212 }
213 }
215 void
216 destroy_msg_out_list(GList *msgout_list)
217 {
218 GList *msgout_node;
220 foreach(msgout_list, msgout_node) {
221 msg_out *msgout = (msg_out *) (msgout_node->data);
222 destroy_msg_out(msgout);
223 }
224 g_list_free(msgout_list);
225 }